-
Notifications
You must be signed in to change notification settings - Fork 26
/
dmmarkup.c
4845 lines (4392 loc) · 125 KB
/
dmmarkup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dmmarkup.c */
/* Copyright 1995 by Steve Kirkendall */
/* This file contains some fairly generic text formatting code -- generic
* in the sense that it can be easily tweaked to format a variety of types
* marked-up text. Currently, it supports useful subsets of NROFF, HTML,
* and TeX instructions.
*/
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_dmmarkup[] = "$Id: dmmarkup.c,v 2.140 2004/03/21 19:30:12 steve Exp $";
#endif
#ifdef DISPLAY_ANYMARKUP
#define SGML_HACK
#define GRANULARITY 64 /* number of LINEINFOs to allocate at a time */
typedef enum { TWRAP_NO, TWRAP_AFTER, TWRAP_BEFORE } twrap_t;
typedef struct
{
CHAR text[256]; /* raw text of token */
long offset[256]; /* offsets of characters */
int nchars; /* number of characters in text[] */
int width; /* normal displayed width of text[] */
struct markup_s *markup;/* info about markup token */
} TOKEN;
typedef struct markup_s
{
char *name; /* name of the markup */
char attr[8]; /* attributes of markup */
twrap_t (*fn)P_((TOKEN *));/* ptr to special function */
} MARKUP;
#define TITLE attr[0] /* in title: -, N, Y */
#define BREAKLN attr[1] /* line break: -, 0, 1, 2, c, or p */
#define INDENT attr[2] /* -, <, >, or a number */
#define LIST attr[3] /* in list: -, N, Y, # */
#define FONT attr[4] /* font: -,=,n,b,u,i,f,e,N,B,U,I,F,E */
#define FILL attr[5] /* Y=fill, N=preformatted, -=no chg. */
#define DEST attr[6] /* S=section, P=paragraph, T=<tab> key */
typedef struct
{
long offset; /* offset of start of line */
struct
{
unsigned indent : 8; /* indentation amount, in spaces */
unsigned listcnt : 8; /* Counter for nest#1 numbered list; 0=not numbered */
unsigned nest : 6; /* nesting level of list/menu; 0=not in list*/
unsigned prefmt : 1; /* 1=literal whitespace, 0=fill */
unsigned graphic : 1; /* 1=replace |-^. with graphic chars */
unsigned midline: 1; /* 1=after a newline, 0=after other char */
unsigned reduce : 1; /* 1=fewer newlines, 0=normal qty newlines */
unsigned deffont : 4; /* index into fonttable[] of default font char */
unsigned curfont : 4; /* index into fonttable[] of current font char */
} state;
} LINEINFO;
typedef struct
{
TOKEN *(*get)P_((CHAR **)); /* mode-dependent get() */
void (*escape)P_((TOKEN *)); /* mode-dependent escape() */
LINEINFO *line; /* line array */
long nlines; /* number of lines in line array */
long endtitle; /* offset of the end of the title */
CHAR *title; /* title of document, or NULL */
CHAR **defs; /* macros within the text */
#ifdef SGML_HACK
int flavor; /* which macro package to emulate */
#endif
} MUINFO;
static ELVBOOL first; /* is this the first token on this line? */
static ELVBOOL anyspc; /* has whitespace been encountered? */
static ELVBOOL title; /* collecting characters of the title */
static ELVBOOL list; /* o_list && o_listchars.markup */
static ELVBOOL readonly;/* o_readonly -- affects &entity with no ; */
static int textwidth;/* o_columns */
static int tabstop;/* repeating part of o_tabstop */
static int listind;/* o_shiftwidth/2, or 2 if shiftwidth<=4 */
static int col; /* logical column number */
static MUINFO *mui; /* pointer to muinfo */
static ELVBOOL prefmt; /* ElvTrue=literal whitespace, ElvFalse=fill */
static ELVBOOL graphic;/* ElvTrue=replace |-^. with graphic chars */
static ELVBOOL midline;/* ElvFalse=after newline, ElvTrue=after other character */
static ELVBOOL reduce; /* ElvTrue=fewer newlines, ElvFalse=normal qty newlines */
static char deffont;/* default font */
static char curfont;/* current font */
static int indent; /* indentation amount */
static int nest; /* nesting level of list/menu; 0=not in list */
static int listcnt;/* Counter for nest#1 numbered list; 0=not numbered */
/* Because the MUINFO font fields are only 4 bits long, we can only store a
* few specific font codes. The following table is used to convert the font
* codes from the letter format to the MUINFO format.
*/
static char fonttable[] = "nbiufeldg*m\b\r\\\t\f";
/* These variables store the string and font collected by the manarg() function
* and output by the manput() function. The "manlen" variable should be
* initialized to 0 before the first call to manarg(). NOTE: These variables
* and the manput() function are also used by htmlimg().
*/
static int manlen; /* length of "mantext" string */
static CHAR mantext[80]; /* buffer, holds args from .XX macro */
static long manoffset[80]; /* holds offsets of mantext[] chars */
static char manfont[80]; /* holds fonts of mantext[] chars */
#ifdef SGML_HACK
/* This stores the text of an expected terminator tag. SGML allows the use of
* "<tt/some text/" as a shorthand for "<tt>some text</tt>", so if this var
* is set to a non-empty string then the next "/" character is interpreted
* as this string.
*/
static CHAR sgmltag[20];
#endif /* SGML_HACK */
/* Forward declarations of some functions which are static to this file */
#ifdef DISPLAY_HTML
static void initfonts P_((void));
static void htmlescape P_((TOKEN *tok));
static twrap_t htmlimg P_((TOKEN *token));
static twrap_t htmlpre P_((TOKEN *token));
static twrap_t htmlli P_((TOKEN *token));
static twrap_t htmlinput P_((TOKEN *token));
static twrap_t htmla P_((TOKEN *token));
static int htmlmarkup P_((TOKEN *token));
static TOKEN *htmlget P_((CHAR **refp));
static DMINFO *htmlinit P_((WINDOW win));
static CHAR *htmltagatcursor P_((WINDOW win, MARK cursor));
static MARK htmltagload P_((CHAR *tagname, MARK from));
static MARK htmltagnext P_((MARK cursor));
#endif /* defined(DISPLAY_HTML) */
#if defined(DISPLAY_HTML) || defined(DISPLAY_TEX)
static twrap_t htmlhr P_((TOKEN *token));
#endif
#if defined(DISPLAY_HTML) || defined(DISPLAY_MAN)
static twrap_t manput P_((void));
#endif
#ifdef DISPLAY_MAN
static void manescape P_((TOKEN *tok));
static int manarg P_((TOKEN *token, int start, _char_ font, ELVBOOL spc));
static twrap_t manTH P_((TOKEN *token));
static twrap_t manSH P_((TOKEN *token));
static twrap_t manBI P_((TOKEN *token));
static twrap_t manIP P_((TOKEN *token));
static void manmarkup P_((TOKEN *token));
static TOKEN *manget P_((CHAR **refp));
static DMINFO *maninit P_((WINDOW win));
#endif /* defined(DISPLAY_MAN) */
#ifdef DISPLAY_TEX
static void texescape P_((TOKEN *tok));
static twrap_t texscope P_((TOKEN *token));
static twrap_t texoutput P_((TOKEN *token));
static twrap_t texitem P_((TOKEN *token));
static twrap_t textitle P_((TOKEN *token));
static twrap_t texdigraph P_((TOKEN *token));
static long texpair P_((CHAR **refp, TOKEN *token));
static TOKEN *texget P_((CHAR **refp));
static DMINFO *texinit P_((WINDOW win));
#endif /* defined(DISPLAY_TEX) */
static void countchar P_((CHAR *p, long qty, _char_ font, long offset));
static twrap_t put P_((TOKEN *token));
static void term P_((DMINFO *info));
static long mark2col P_((WINDOW w, MARK mark, ELVBOOL cmd));
static MARK move P_((WINDOW w, MARK from, long linedelta, long column, ELVBOOL cmd));
static MARK setup P_((WINDOW win, MARK top, long cursor, MARK bottom, DMINFO *info));
static MARK image P_((WINDOW w, MARK line, DMINFO *info, void (*draw)(CHAR *p, long qty, _char_ font, long offset)));
static int start P_((WINDOW win, MARK from, void (*draw)(CHAR *p, long qty, _char_ font, long offset)));
static void storestate P_((long offset, LINEINFO *dest));
static void findtitle P_((BUFFER buf));
#ifdef FEATURE_LPR
static void header P_((WINDOW w, int pagenum, DMINFO *info, void (*draw)(CHAR *p, long qty, _char_ font, long offset)));
#endif
/* Only a single TOKEN is ever really needed at one time */
static TOKEN rettok;
/* Offset of cursor. This affects the expansion of escapes, and the visibility
* of markups.
*/
static long cursoff;
/* Offset & font of a space character, if "anyspc" is ElvTrue. */
static long spcoffset;
static char spcfont;
/* This the drawchar pointer points to a function for outputting a single
* character.
*/
static void (*drawchar) P_((CHAR *p, long qty, _char_ font, long offset));
/* Special characters. These are stored in variables rather than macros so
* that we can pass their address to (*drawchar)().
*/
static CHAR hyphen[1] = {'-'};
static CHAR newline[1] = {'\n'};
static CHAR formfeed[1] = {'\f'};
static CHAR vtab[1] = {'\013'};
static CHAR space[1] = {' '};
static CHAR bullet1[1] = {'*'};
static CHAR bullet2[1] = {'o'};
#ifdef DISPLAY_HTML
/*----------------------------------------------------------------------------*/
/* Array for converting single-letter font names into font codes. The long
* names of the fonts are converted into font codes in the initialization
* function.
*/
static char fontcode[128];
static void initfonts()
{
static ELVBOOL first = ElvTrue;
char form;
/* if not first time, then do nothing */
if (!first)
return;
first = ElvFalse;
/* initialize the fontcode[] array */
fontcode['n'] = fontcode['N'] = colorfind(toCHAR("formatted"));
fontcode['b'] = fontcode['B'] = colorfind(toCHAR("bold"));
fontcode['i'] = fontcode['I'] = colorfind(toCHAR("italic"));
fontcode['u'] = fontcode['U'] = colorfind(toCHAR("underlined"));
fontcode['f'] = fontcode['F'] = colorfind(toCHAR("fixed"));
fontcode['e'] = fontcode['E'] = colorfind(toCHAR("emphasized"));
fontcode['l'] = fontcode['L'] = colorfind(toCHAR("link"));
fontcode['d'] = fontcode['D'] = colorfind(toCHAR("definition"));
fontcode['g'] = fontcode['G'] = colorfind(toCHAR("graphic"));
fontcode['m'] = fontcode['M'] = colorfind(toCHAR("markup"));
fontcode['*'] = colorfind(toCHAR("bullet"));
fontcode['\b'] = colorfind(toCHAR("form_button"));
fontcode['\r'] = colorfind(toCHAR("form_radio"));
fontcode['\\'] = colorfind(toCHAR("form_check"));
fontcode['\t'] = colorfind(toCHAR("form_text"));
fontcode['\f'] = colorfind(toCHAR("form_other"));
form = colorfind(toCHAR("form"));
/* some hardcoded defaults for the appearances of those fonts */
colorset(fontcode['n'], toCHAR("proportional"), ElvFalse);
colorset(fontcode['b'], toCHAR("bold like formatted"), ElvFalse);
colorset(fontcode['i'], toCHAR("italic like formatted"), ElvFalse);
colorset(fontcode['u'], toCHAR("underlined like formatted"), ElvFalse);
colorset(fontcode['e'], toCHAR("like bold"), ElvFalse);
colorset(fontcode['f'], toCHAR("fixed like formatted"), ElvFalse);
colorset(fontcode['l'], toCHAR("underlined blue like formatted"), ElvFalse);
colorset(fontcode['d'], toCHAR("like bold"), ElvFalse);
colorset(fontcode['g'], toCHAR("graphic like fixed"), ElvFalse);
colorset(fontcode['*'], toCHAR("graphic"), ElvFalse);
colorset(fontcode['m'], toCHAR("bold green"), ElvFalse);
colorset(form, toCHAR("red"), ElvFalse);
colorset(fontcode['\b'], toCHAR("boxed like form"), ElvFalse);
colorset(fontcode['\r'], toCHAR("boxed like form"), ElvFalse);
colorset(fontcode['\\'], toCHAR("boxed like form"), ElvFalse);
colorset(fontcode['\t'], toCHAR("underlined like form"), ElvFalse);
colorset(fontcode['\f'], toCHAR("boxed like form"), ElvFalse);
}
/*----------------------------------------------------------------------------*/
/* HTML-specific functions and variables */
/* Replace entities such as < with their single-character equivelent. */
static void htmlescape(token)
TOKEN *token; /* a token whose text is to be expanded */
{
char *src, *dst;
long *off;
size_t len, truelen;
int width;
int i;
static struct {
size_t len; /* overall length of the entity */
char name[6];/* name of the entity, without & ; or variable char */
CHAR c1,c2; /* digraph chars; c2=0 if variable; c1=0 if c2 ascii */
} entities[] = {
{4, "lt", 0, '<'},
{4, "gt", 0, '>'},
{5, "amp", 0, '&'},
{6, "quot", 0, '"'},
{6, "nbsp", 0, ' '},
{7, "AElig", 'E', 'A'},
{7, "aelig", 'e', 'a'},
{7, "szlig", 's', 'z'},
{8, "grave", '`', 0},
{8, "acute", '\'', 0},
{7, "circ", '^', 0},
{8, "tilde", '~', 0},
{6, "uml", '"', 0},
{7, "ring", '*', 0},
{8, "cedil", ',', 0},
{8, "slash", '/', 0},
{7, "ldquo", 0, '"'},
{7, "rdquo", 0, '"'},
{7, "lsquo", 0, '`'},
{7, "rsquo", 0,'\''},
{7, "tilde", 0, '~'},
{5, "shy", 0, '-'},
{5, "ETH", '-', 'D'},
{5, "eth", '-', 'd'},
{7, "THORN", 'T', 'P'},
{7, "thorn", 't', 'p'},
{6, "copy", 'O', 'c'},
{5, "reg", 'O', 'r'},
{7, "iexcl", '~', '!'},
{7, "laquo", '<', '<'},
{7, "raquo", '>', '>'},
{7, "pound", '$', 'L'},
{6, "cent", '$', 'C'},
{5, "yen", '$', 'Y'},
{6, "euro", '$', 'E'},
{5, "deg", '*', '*'},
#if USE_PROTOTYPES /* because K&R C can't handle 6 chars in a char[6] field */
{8, "iquest", '~', '?'},
{8, "curren", '$', 'X'},
{8, "percnt", 0, '%'},
#endif
{4, "LT", 0, '<'},
{4, "GT", 0, '>'},
{5, "AMP", 0, '&'},
{6, "QUOT", 0, '"'}
};
/* step through the string */
for (src = dst = (char *)token->text, off = token->offset, width = 0;
src < (char *)&token->text[token->nchars];
src++, off++, width++)
{
/* if not a &, then this can't be an escape */
if (*src != '&')
goto NoEscape;
/* find the length of this escape's name */
for (len = truelen = 1; src[len] != ';'; truelen++, len++)
{
if (!src[len] || elvspace(src[len]))
{
if (readonly)
{
truelen--;
break;
}
else
goto NoEscape;
}
}
len++, truelen++;
/* if the cursor is on this escape, then don't expand it */
if ((o_showmarkups && off[0] <= cursoff && cursoff <= off[len - 1]) || list)
{
/* Tweak the value of "width" so that after all of
* this escape's characters have been counted, "width"
* will have been incremented by only 1 since that's
* how wide the escape would normally be.
*/
width -= (len - 1);
/* Don't expand the escape */
goto NoEscape;
}
/* recognize it? */
if (src[1] == '#')
*dst++ = atoi(&src[2]);
else
{
for (i = 0; i < QTY(entities); i++)
{
if (len != entities[i].len)
continue;
if (entities[i].c2 == 0)
{
if (!strncmp(&src[2], entities[i].name, len - 3))
{
*dst++ = digraph(entities[i].c1, (_CHAR_)src[1]);
break;
}
}
else
{
if (!strncmp(&src[1], entities[i].name, len - 2))
{
if (entities[i].c1)
*dst++ = digraph(entities[i].c1, entities[i].c2);
else
*dst++ = entities[i].c2;
break;
}
}
}
/* did we find it? */
if (i >= QTY(entities))
{
/* NO! Tweak the value of "width" so that after
* all of this escape's characters have been
* counted, "width" will have been incremented
* by only 1 since that's how wide the escape
* would normally be if it was recognized.
*/
width -= (truelen - 1);
/* Don't expand the escape */
goto NoEscape;
}
}
/* Skip past the escape sequence. */
token->offset[(int)(dst - (char *)token->text) - 1] = *off;
src += truelen - 1;
off += truelen - 1;
/* plus one more at the for-loop */
continue;
NoEscape:
/* Not an escape -- copy it literally */
*dst++ = *src;
token->offset[(int)(dst - (char *)token->text) - 1] = *off;
}
/* compute the new length */
token->nchars = (int)(dst - (char *)token->text);
token->text[token->nchars] = '\0';
token->width = width;
}
/* Output the "alt" text from an <img> tag */
static twrap_t htmlimg(token)
TOKEN *token;
{
int i, j;
ELVBOOL htmlurl;
/* detect whether this is an SGML <htmlurl> or <ulink> tag */
htmlurl = (ELVBOOL)(token->text[1] == 'h' || token->text[1] == 'u');
/* look for an "alt=..." argument */
for (i = 5; i < token->nchars && CHARncmp(&token->text[i - 5], toCHAR(" alt="), 5); i++)
{
}
/* if no "alt=" then search for "name=" (This is for <frame ...>) */
if (i >= token->nchars)
{
for (i = 6; i < token->nchars && CHARncmp(&token->text[i - 6], toCHAR(" name="), 6); i++)
{
}
}
/* if we still have no "alt=" then try "src=" or "url=" */
if (i >= token->nchars)
{
for (i = 5;
i < token->nchars
&& CHARncmp(&token->text[i - 5], toCHAR(" src="), 5)
&& CHARncmp(&token->text[i - 5], toCHAR(" url="), 5);
i++)
{
}
/* some images have long names -- can we trim this? */
for (j = i + 1; j < token->nchars && token->text[j] != ' ' && token->text[j] != '"'; j++)
{
}
while (--j >= i && token->text[j] != '/')
{
}
i = j + 1;
}
/* decide how to display this image */
if (i >= token->nchars)
{
/* there is no "alt=..." string, so display the tag name */
i = 1;
for (j = 1; j + i < token->nchars && elvalpha(token->text[j + i]); j++)
{
}
}
else if (token->text[i] == '"')
{
/* the "alt=" argument has a quoted argument */
i++;
for (j = i; j < token->nchars && token->text[j] != '"'; j++)
{
}
}
else
{
/* the "alt=" argument is unquoted */
for (j = i;
j < token->nchars && token->text[j] != ' '
&& token->text[j] != '"' && token->text[j] != '>';
j++)
{
}
}
/* limit length to 14 characters -- tables look better that way */
if (!htmlurl && j > i + 14)
j = i + 14;
/* Insert a little arrow in front of the label, pointing to the label.
* This serves two purposes: It ensures that a character will be output
* which has the same offset as the first character of the tag (which
* is desirable because that's where the <Tab> key leaves the cursor).
* And it gives the user an obvious place to double-click, to download
* the image.
*/
manlen = 0;
if (!htmlurl)
{
mantext[0] = '>';
manoffset[0] = token->offset[0];
manfont[0] = fontcode['l'];
manlen++;
}
/* copy it into the manput() arguments, and then put it. */
for ( ; i < j && manlen < QTY(mantext); i++, manlen++)
{
mantext[manlen] = token->text[i];
manoffset[manlen] = token->offset[i];
manfont[manlen] = fontcode[htmlurl ? 'u' : 'N'];
}
return manput();
}
/* Set the modeinfo's "graphic" flag if <pre graphic> */
static twrap_t htmlpre(token)
TOKEN *token;
{
int i;
for (graphic = ElvFalse, i = 0; i < token->nchars; i++)
{
if (token->text[i] == 'g')
{
graphic = ElvTrue;
break;
}
}
return TWRAP_NO;
}
/* List items are preceded by a less-indented number or bullet */
static twrap_t htmlli(token)
TOKEN *token;
{
CHAR buf[10];
int len;
assert(col == 0);
/* output a bullet or count */
if (nest == 1 && listcnt > 0)
{
/* convert item# to characters */
long2CHAR(buf, (long)listcnt++);
CHARcat(buf, toCHAR(")"));
len = CHARlen(buf);
/* output whitespace for indentation */
if (indent - len > 1)
{
(*drawchar)(space, 1 + len - indent, 0, -1);
col += indent - len - 1;
}
/* output the item number */
(*drawchar)(buf, len, 0, -1);
col += len;
}
else
{
/* output whitespace for indentation */
if (indent > 2)
{
(*drawchar)(space, 2 - indent, 0, -1);
col += indent - 2;
}
/* output a bullet */
(*drawchar)((nest & 1) ? bullet1 : bullet2, 1, fontcode['*'], -1);
col++;
}
/* Note: We would like to do an assert(mui->col == mui->indent - 1)
* here, but if the number/bullet doesn't fit within the indentation
* space then our indentation might be off. So we won't.
*/
return TWRAP_NO;
}
/* Form elements are shown as reverse-video areas */
static twrap_t htmlinput(token)
TOKEN *token;
{
int height; /* 1 for input, 2 for textarea */
int width; /* displayed width of item */
int vallen; /* length of the value */
int validx; /* index into token->text[] of initial value */
ELVBOOL button; /* does this form item appear to be a button? */
ELVBOOL radio; /* does this form item appear to be a radio button? */
char font; /* font - 'E' for buttons, or 'N' for any other */
int mycol;
int i;
/* parse the arguments */
height = (token->text[1] == 't') ? 3 : 1;
width = vallen = validx = 0;
button = radio = ElvFalse;
font = fontcode['\t'];
for (i = 4; i < token->nchars; i++)
{
if (!CHARncmp(&token->text[i], toCHAR("value="), 6))
{
i += 6;
if (token->text[i] == '"')
{
i++;
validx = i;
for (vallen = i; i < token->nchars && token->text[i] != '"'; i++)
{
}
}
else
{
validx = i;
for (vallen = i; i < token->nchars && elvalnum(token->text[i]); i++)
{
}
}
vallen = i - vallen;
}
else if (!CHARncmp(&token->text[i], toCHAR("size="), 5)
|| !CHARncmp(&token->text[i], toCHAR("cols="), 5))
{
i += 5;
width = atoi(tochar8(&token->text[i]));
}
else if (!CHARncmp(&token->text[i], toCHAR("type="), 5))
{
i += 5;
if (token->text[i] == '"')
i++;
if (!CHARncmp(&token->text[i], toCHAR("checkbox"), 8)
|| !CHARncmp(&token->text[i], toCHAR("CHECKBOX"), 8))
{
/* CHECKBOX button */
button = radio = ElvTrue;
i += 8;
font = fontcode['\\'];
}
else if (!CHARncmp(&token->text[i], toCHAR("hidden"), 6)
|| !CHARncmp(&token->text[i], toCHAR("HIDDEN"), 6))
{
/* HIDDEN field -- do nothing with it */
return TWRAP_NO;
}
else if (!CHARncmp(&token->text[i], toCHAR("radio"), 5)
|| !CHARncmp(&token->text[i], toCHAR("RADIO"), 5))
{
/* RADIO button */
button = radio = ElvTrue;
i += 5;
font = fontcode['\r'];
}
else if (token->text[i] != 't' && token->text[i] != 'T')
{
/* not TEXT, probably SUBMIT or RESET button */
button = ElvTrue;
font = fontcode['\b'];
/* SUBMIT/RESET buttons have default values*/
if (validx == 0)
{
validx = i;
for (vallen = 1; elvalnum(token->text[i + vallen]); vallen++)
{
}
}
}
}
else if (!CHARncmp(&token->text[i], toCHAR("checked"), 7))
{
/* font = fontcode['\\'] BUT WITH HIGHLIGHTING */
i += 7;
}
}
/* Most buttons are always as wide as their value, but radio & checkbox
* buttons only need to show a single character.
*/
if (radio)
{
vallen = 1;
}
if (button)
{
width = vallen;
}
/* remember the column */
mycol = col;
if (mycol < indent)
mycol = indent;
else if (anyspc)
{
mycol++;
anyspc = ElvFalse;
}
/* will it fit on this line? */
if (!first && mycol + width > textwidth)
{
/* no it won't */
(*drawchar)(newline, 1, 0, -1);
col = 0;
return TWRAP_BEFORE;
}
/* output the image */
for (i = 1; i <= height; i++)
{
if (col > mycol)
{
(*drawchar)(newline, 1, 0, -1);
col = 0;
}
if (col < mycol)
{
(*drawchar)(space, col - mycol , 0, -1);
col = mycol;
}
if (vallen > 0)
(*drawchar)(&token->text[validx], vallen,
font, token->offset[validx]);
if (width > vallen)
(*drawchar)(space, vallen - width, /* <- negative! */
font, token->offset[token->nchars - 1]);
col += width;
}
anyspc = ElvFalse;
return TWRAP_NO;
}
/* switch to underline if this is an href anchor (else leave the font unchanged
* for name anchor).
*/
static twrap_t htmla(token)
TOKEN *token;
{
/* whether we set the font or not, </a> will reset it so we always
* need to store the current font.
*/
deffont = curfont;
/* if the token starts with "a href" then force font to "link" */
if (!CHARncmp(token->text, toCHAR("<a href="), 8))
curfont = fontcode['l'];
/* zero width, always fits on line */
return TWRAP_NO;
}
/* Look up an html markup token in a table */
static int htmlmarkup(token)
TOKEN *token; /* the token to lookup */
{
static MARKUP tbl[] =
{
/* Tag Effects Function */
/* TBILFFD */
#ifdef SGML_HACK
/* These are SGML tags. We only consider using these when
* the display mode is "html sgml".
*/
{ "title", "N0--b--" },
{ "/title", "N2|-=--" },
{ "subtitle", "N0--i--" },
{ "/subtitle", "N2|-=--" },
{ "/", "N---=--" },
{ "author", "N0--I--" },
{ "date", "N0--N--" },
{ "sect", "Np0-BYS" }, /* <h1> */
{ "sect1", "Nc1-BYS" }, /* <h2> */
{ "sect2", "N12-BYS" }, /* <h3> */
{ "para", "N1|-NYP" }, /* <p> */
{ "simpara", "N1--NYP" }, /* <p> */
{ "orderedlist","N->#-YP" }, /* <ol> */
{ "/orderedlist","N0<N-Y-" }, /* </ol> */
{ "itemizedlist","N->Y-YP" }, /* <ul> */
{ "/itemizedlist","N0<N-Y-" }, /* </ul> */
{ "listitem", "N0-----", htmlli }, /* <li> */
{ "enum", "N->#-YP" }, /* <ol> */
{ "/enum", "N0<N-Y-" }, /* </ol> */
{ "itemize", "N->Y-YP" }, /* <ul> */
{ "/itemize", "N0<N-Y-" }, /* </ul> */
{ "item", "N0-----", htmlli }, /* <li> */
{ "abstract", "N14-NYP" }, /* </blockquote> */
{ "/abstract", "N12-NY-" }, /* </blockquote> */
{ "verb", "N0---NP" },
{ "/verb", "N0---Y-" },
{ "tscreen", "N0>-F--", htmlpre },
{ "/tscreen", "N0<-N--" },
{ "descrip", "N-2-NYS" }, /* <dl> */
{ "/descrip", "N02-NY-" }, /* </dl> */
{ "tag", "N12-BYP" }, /* <dt> */
{ "/tag", "N03-NY-" }, /* <dd> */
{ "variablelist","N-2-NYS" }, /* <dl> */
{ "/variablelist","N02-NY-" }, /* </dl> */
{ "term", "N12-BYP" }, /* <dt> */
{ "/term", "N03-NY-" }, /* <dd> */
{ "htmlurl", "N-----T", htmlimg }, /* <img> */
{ "url", "N-----T", htmlimg }, /* <img> */
{ "ulink", "N---u-T" }, /* <a> */
{ "/ulink", "N---=--" }, /* </a> */
{ "replaceable","N---i--" }, /* <var> */
{ "/replaceable","N---=--" }, /* </var> */
{ "bf", "N---b--" }, /* <b> */
{ "/bf", "N---=--" }, /* </b> */
{ "sf", "N---b--" }, /* <strong>*/
{ "/sf", "N---=--" }, /*</strong>*/
{ "it", "N---i--" }, /* <i> */
{ "/it", "N---=--" }, /* </i> */
{ "sl", "N---i--" }, /* <i> */
{ "/sl", "N---=--" }, /* </i> */
{ "informaltable", "N0|-NY-" }, /* <table> */
{ "/informaltable", "N0@-NY-" }, /* </table>*/
{ "row", "N02--Y-" }, /* <tr> */
{ "entry", "N-=-NY-" }, /* <td> */
{ "literallayout","N0---NP" },
{ "/literallayout","N0---Y-" },
#endif /* SGML_HACK */
/* These are HTML tags */
/* Tag Effects Function */
/* TBILFFD */
{ "html", "Y-2-NY-" },
{ "/html", "N-2-NY-" },
{ "head", "Y-2-NY-" },
{ "/head", "N-2-NY-" },
{ "title", "Y-2-NY-" },
{ "/title", "N-2-NY-" },
{ "body", "N-2-NY-" },
{ "/body", "N-2-NY-" },
{ "h1", "Np0-BYS" },
{ "/h1", "N12-NY-" },
{ "h2", "Nc1-BYS" },
{ "/h2", "N12-NY-" },
{ "h3", "N12-BYS" },
{ "/h3", "N02-NY-" },
{ "h4", "N12-IY-" },
{ "/h4", "N02-NY-" },
{ "h5", "N12-IY-" },
{ "/h5", "N02-NY-" },
{ "h6", "N12-IY-" },
{ "/h6", "N02-NY-" },
{ "p", "N1|-NYP" },
{ "hr", "N0-----", htmlhr },
{ "img", "N------", htmlimg },
{ "frame", "N-----T", htmlimg },
{ "embed", "N-----T", htmlimg },
{ "br", "N0---Y-" },
{ "table", "N0|-NY-" },
{ "/table", "N0@-NY-" },
{ "tr", "N0@--Y-" },
{ "th", "N-=-BY-" },
{ "td", "N-=-NY-" },
{ "blockquote", "N14-NYP" },
{ "/blockquote","N12-NY-" },
{ "pre", "N0--FNP", htmlpre },
{ "/pre", "N0--NY-" },
{ "dir", "N0>-FNP", htmlpre },
{ "/dir", "N0<-NY-" },
{ "xmp", "N0>-FN-", htmlpre },
{ "/xmp", "N0<-NY-" },
{ "dl", "N-2-NYS" },
{ "/dl", "N02-NY-" },
{ "dt", "N12-DYP" },
{ "dd", "N03-NY-" },
{ "ol", "N->#-YP" },
{ "/ol", "N0<N-Y-" },
{ "ul", "N->Y-YP" },
{ "/ul", "N0<N-Y-" },
{ "menu", "N->Y-Y-" },
{ "/menu", "N-<N-Y-" },
{ "li", "N0-----", htmlli },
{ "input", "N-----T", htmlinput },
{ "textarea", "N-----T", htmlinput },
{ "a", "N-----T", htmla },
{ "/a", "N---=--" },
{ "cite", "N---i--" },
{ "/cite", "N---=--" },
{ "dfn", "N---d--" },
{ "/dfn", "N---=--" },
{ "em", "N---i--" },
{ "/em", "N---=--" },
{ "kbd", "N---b--" },
{ "/kbd", "N---=--" },
{ "strong", "N---b--" },
{ "/strong", "N---=--" },
{ "var", "N---i--" },
{ "/var", "N---=--" },
{ "address", "N---i--" },
{ "/address", "N---=--" },
{ "code", "N---f--" },
{ "/code", "N---=--" },
{ "b", "N---b--" },
{ "/b", "N---=--" },
{ "i", "N---i--" },
{ "/i", "N---=--" },
{ "u", "N---u--" },
{ "/u", "N---=--" },
{ "tt", "N---f--" },
{ "/tt", "N---=--" },
{ (char *)0, "N------" }
};
MARKUP *scan; /* used for scanning the tbl[] array */
int len; /* length of the markup */
/* find the length of the markup's name */
assert(token->nchars > 1 && token->text[0] == '<');
for (len = 1;
len < token->nchars &&
((len == 1 && token->text[len] == '/') || elvalnum(token->text[len]));
len++)
{
}
len--; /* since we started at 1 */
/* look it up in the table */
#ifdef SGML_HACK
for (scan = mui ? &tbl[mui->flavor] : tbl; /* won't always have mui */
#else
for (scan = tbl;
#endif
scan->name &&
(strlen(scan->name) != (unsigned)len ||
strncmp(scan->name, (char *)token->text + 1, (size_t)len));
scan++)
{
}
/* remember it */
token->markup = scan;
/* return the index of the markup in tbl[] */
return (int)(scan - tbl);
}
/* Read the next token. */
static TOKEN *htmlget(refp)
CHAR **refp; /* address of a (CHAR *) used for scanning */
{
long offset;
ELVBOOL lower, nameonly;
int i;
/* if the CHAR pointer is NULL, then return NULL */
if (!*refp)
return NULL;
/* Get first character of token */
offset = markoffset(scanmark(refp));
rettok.text[0] = **refp;
rettok.offset[0] = offset++;
rettok.nchars = 1;
rettok.markup = NULL;
scannext(refp);
#ifdef SGML_HACK
/* If '/' or '>' and we were expecting an SGML-style terminator,
* then do that.
*/
if ((rettok.text[0] == '/' || rettok.text[0] == '>') && *sgmltag)
{
/* replace the '/' with the SGML terminator tag name */