Man-page back end for Halibut. Also, a couple of additional markup
[sgt/halibut] / input.c
1 /*
2 * input.c: read the source form
3 */
4
5 #include <stdio.h>
6 #include <assert.h>
7 #include <time.h>
8 #include "halibut.h"
9
10 #define TAB_STOP 8 /* for column number tracking */
11
12 static void setpos(input *in, char *fname) {
13 in->pos.filename = fname;
14 in->pos.line = 1;
15 in->pos.col = (in->reportcols ? 1 : -1);
16 }
17
18 static void unget(input *in, int c, filepos *pos) {
19 if (in->npushback >= in->pushbacksize) {
20 in->pushbacksize = in->npushback + 16;
21 in->pushback = resize(in->pushback, in->pushbacksize);
22 }
23 in->pushback[in->npushback].chr = c;
24 in->pushback[in->npushback].pos = *pos; /* structure copy */
25 in->npushback++;
26 }
27
28 /* ---------------------------------------------------------------------- */
29 /*
30 * Macro subsystem
31 */
32 typedef struct macro_Tag macro;
33 struct macro_Tag {
34 wchar_t *name, *text;
35 };
36 struct macrostack_Tag {
37 macrostack *next;
38 wchar_t *text;
39 int ptr, npushback;
40 filepos pos;
41 };
42 static int macrocmp(void *av, void *bv) {
43 macro *a = (macro *)av, *b = (macro *)bv;
44 return ustrcmp(a->name, b->name);
45 }
46 static void macrodef(tree234 *macros, wchar_t *name, wchar_t *text,
47 filepos fpos) {
48 macro *m = mknew(macro);
49 m->name = name;
50 m->text = text;
51 if (add234(macros, m) != m) {
52 error(err_macroexists, &fpos, name);
53 sfree(name);
54 sfree(text);
55 }
56 }
57 static int macrolookup(tree234 *macros, input *in, wchar_t *name,
58 filepos *pos) {
59 macro m, *gotit;
60 m.name = name;
61 gotit = find234(macros, &m, NULL);
62 if (gotit) {
63 macrostack *expansion = mknew(macrostack);
64 expansion->next = in->stack;
65 expansion->text = gotit->text;
66 expansion->pos = *pos; /* structure copy */
67 expansion->ptr = 0;
68 expansion->npushback = in->npushback;
69 in->stack = expansion;
70 return TRUE;
71 } else
72 return FALSE;
73 }
74 static void macrocleanup(tree234 *macros) {
75 int ti;
76 macro *m;
77 for (ti = 0; (m = (macro *)index234(macros, ti)) != NULL; ti++) {
78 sfree(m->name);
79 sfree(m->text);
80 sfree(m);
81 }
82 freetree234(macros);
83 }
84
85 /*
86 * Can return EOF
87 */
88 static int get(input *in, filepos *pos) {
89 int pushbackpt = in->stack ? in->stack->npushback : 0;
90 if (in->npushback > pushbackpt) {
91 --in->npushback;
92 if (pos)
93 *pos = in->pushback[in->npushback].pos; /* structure copy */
94 return in->pushback[in->npushback].chr;
95 }
96 else if (in->stack) {
97 wchar_t c = in->stack->text[in->stack->ptr];
98 if (in->stack->text[++in->stack->ptr] == L'\0') {
99 macrostack *tmp = in->stack;
100 in->stack = tmp->next;
101 sfree(tmp);
102 }
103 return c;
104 }
105 else if (in->currfp) {
106 int c = getc(in->currfp);
107
108 if (c == EOF) {
109 fclose(in->currfp);
110 in->currfp = NULL;
111 }
112 /* Track line numbers, for error reporting */
113 if (pos)
114 *pos = in->pos;
115 if (in->reportcols) {
116 switch (c) {
117 case '\t':
118 in->pos.col = 1 + (in->pos.col + TAB_STOP-1) % TAB_STOP;
119 break;
120 case '\n':
121 in->pos.col = 1;
122 in->pos.line++;
123 break;
124 default:
125 in->pos.col++;
126 break;
127 }
128 } else {
129 in->pos.col = -1;
130 if (c == '\n')
131 in->pos.line++;
132 }
133 /* FIXME: do input charmap translation. We should be returning
134 * Unicode here. */
135 return c;
136 } else
137 return EOF;
138 }
139
140 /*
141 * Lexical analysis of source files.
142 */
143 typedef struct token_Tag token;
144 struct token_Tag {
145 int type;
146 int cmd, aux;
147 wchar_t *text;
148 filepos pos;
149 };
150 enum {
151 tok_eof, /* end of file */
152 tok_eop, /* end of paragraph */
153 tok_white, /* whitespace */
154 tok_word, /* a word or word fragment */
155 tok_cmd, /* \command */
156 tok_lbrace, /* { */
157 tok_rbrace /* } */
158 };
159
160 /* Halibut command keywords. */
161 enum {
162 c__invalid, /* invalid command */
163 c__comment, /* comment command (\#) */
164 c__escaped, /* escaped character */
165 c__nbsp, /* nonbreaking space */
166 c_A, /* appendix heading */
167 c_B, /* bibliography entry */
168 c_BR, /* bibliography rewrite */
169 c_C, /* chapter heading */
170 c_H, /* heading */
171 c_I, /* invisible index mark */
172 c_IM, /* index merge/rewrite */
173 c_K, /* capitalised cross-reference */
174 c_S, /* aux field is 0, 1, 2, ... */
175 c_U, /* unnumbered-chapter heading */
176 c_W, /* Web hyperlink */
177 c_b, /* bulletted list */
178 c_c, /* code */
179 c_cfg, /* configuration directive */
180 c_copyright, /* copyright statement */
181 c_cw, /* weak code */
182 c_date, /* document processing date */
183 c_dd, /* description list: description */
184 c_define, /* macro definition */
185 c_dt, /* description list: described thing */
186 c_e, /* emphasis */
187 c_i, /* visible index mark */
188 c_ii, /* uncapitalised visible index mark */
189 c_k, /* uncapitalised cross-reference */
190 c_lcont, /* continuation para(s) for list item */
191 c_n, /* numbered list */
192 c_nocite, /* bibliography trickery */
193 c_preamble, /* document preamble text */
194 c_q, /* quote marks */
195 c_rule, /* horizontal rule */
196 c_title, /* document title */
197 c_u, /* aux field is char code */
198 c_versionid /* document RCS id */
199 };
200
201 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
202 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
203 #define isnl(c) ( (c)==10 )
204 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
205 #define fromdec(c) ( (c)-'0' )
206 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
207 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
208 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
209
210 /*
211 * Keyword comparison function. Like strcmp, but between a wchar_t *
212 * and a char *.
213 */
214 static int kwcmp(wchar_t const *p, char const *q) {
215 int i;
216 do {
217 i = *p - *q;
218 } while (*p++ && *q++ && !i);
219 return i;
220 }
221
222 /*
223 * Match a keyword.
224 */
225 static void match_kw(token *tok) {
226 /*
227 * FIXME. The ids are explicit in here so as to allow long-name
228 * equivalents to the various very short keywords.
229 */
230 static const struct { char const *name; int id; } keywords[] = {
231 {"#", c__comment}, /* comment command (\#) */
232 {"-", c__escaped}, /* nonbreaking hyphen */
233 {"A", c_A}, /* appendix heading */
234 {"B", c_B}, /* bibliography entry */
235 {"BR", c_BR}, /* bibliography rewrite */
236 {"C", c_C}, /* chapter heading */
237 {"H", c_H}, /* heading */
238 {"I", c_I}, /* invisible index mark */
239 {"IM", c_IM}, /* index merge/rewrite */
240 {"K", c_K}, /* capitalised cross-reference */
241 {"U", c_U}, /* unnumbered-chapter heading */
242 {"W", c_W}, /* Web hyperlink */
243 {"\\", c__escaped}, /* escaped backslash (\\) */
244 {"_", c__nbsp}, /* nonbreaking space (\_) */
245 {"b", c_b}, /* bulletted list */
246 {"c", c_c}, /* code */
247 {"cfg", c_cfg}, /* configuration directive */
248 {"copyright", c_copyright}, /* copyright statement */
249 {"cw", c_cw}, /* weak code */
250 {"date", c_date}, /* document processing date */
251 {"dd", c_dd}, /* description list: description */
252 {"define", c_define}, /* macro definition */
253 {"dt", c_dt}, /* description list: described thing */
254 {"e", c_e}, /* emphasis */
255 {"i", c_i}, /* visible index mark */
256 {"ii", c_ii}, /* uncapitalised visible index mark */
257 {"k", c_k}, /* uncapitalised cross-reference */
258 {"lcont", c_lcont}, /* continuation para(s) for list item */
259 {"n", c_n}, /* numbered list */
260 {"nocite", c_nocite}, /* bibliography trickery */
261 {"preamble", c_preamble}, /* document preamble text */
262 {"q", c_q}, /* quote marks */
263 {"rule", c_rule}, /* horizontal rule */
264 {"title", c_title}, /* document title */
265 {"versionid", c_versionid}, /* document RCS id */
266 {"{", c__escaped}, /* escaped lbrace (\{) */
267 {"}", c__escaped}, /* escaped rbrace (\}) */
268 };
269 int i, j, k, c;
270
271 /*
272 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
273 * doesn't match correctly, we just fall through to the
274 * binary-search phase.
275 */
276 if (tok->text[0] == 'S') {
277 /* We expect numeric characters thereafter. */
278 wchar_t *p = tok->text+1;
279 int n;
280 if (!*p)
281 n = 1;
282 else {
283 n = 0;
284 while (*p && isdec(*p)) {
285 n = 10 * n + fromdec(*p);
286 p++;
287 }
288 }
289 if (!*p) {
290 tok->cmd = c_S;
291 tok->aux = n;
292 return;
293 }
294 } else if (tok->text[0] == 'u') {
295 /* We expect hex characters thereafter. */
296 wchar_t *p = tok->text+1;
297 int n = 0;
298 while (*p && ishex(*p)) {
299 n = 16 * n + fromhex(*p);
300 p++;
301 }
302 if (!*p) {
303 tok->cmd = c_u;
304 tok->aux = n;
305 return;
306 }
307 }
308
309 i = -1;
310 j = sizeof(keywords)/sizeof(*keywords);
311 while (j-i > 1) {
312 k = (i+j)/2;
313 c = kwcmp(tok->text, keywords[k].name);
314 if (c < 0)
315 j = k;
316 else if (c > 0)
317 i = k;
318 else /* c == 0 */ {
319 tok->cmd = keywords[k].id;
320 return;
321 }
322 }
323
324 tok->cmd = c__invalid;
325 }
326
327
328 /*
329 * Read a token from the input file, in the normal way (`normal' in
330 * the sense that code paragraphs work a different way).
331 */
332 token get_token(input *in) {
333 int c;
334 int nls;
335 token ret;
336 rdstring rs = { 0, 0, NULL };
337 filepos cpos;
338
339 ret.text = NULL; /* default */
340 c = get(in, &cpos);
341 ret.pos = cpos;
342 if (iswhite(c)) { /* tok_white or tok_eop */
343 nls = 0;
344 do {
345 if (isnl(c))
346 nls++;
347 } while ((c = get(in, &cpos)) != EOF && iswhite(c));
348 if (c == EOF) {
349 ret.type = tok_eof;
350 return ret;
351 }
352 unget(in, c, &cpos);
353 ret.type = (nls > 1 ? tok_eop : tok_white);
354 return ret;
355 } else if (c == EOF) { /* tok_eof */
356 ret.type = tok_eof;
357 return ret;
358 } else if (c == '\\') { /* tok_cmd */
359 c = get(in, &cpos);
360 if (c == '-' || c == '\\' || c == '_' ||
361 c == '#' || c == '{' || c == '}') {
362 /* single-char command */
363 rdadd(&rs, c);
364 } else if (c == 'u') {
365 int len = 0;
366 do {
367 rdadd(&rs, c);
368 len++;
369 c = get(in, &cpos);
370 } while (ishex(c) && len < 5);
371 unget(in, c, &cpos);
372 } else if (iscmd(c)) {
373 do {
374 rdadd(&rs, c);
375 c = get(in, &cpos);
376 } while (iscmd(c));
377 unget(in, c, &cpos);
378 }
379 /*
380 * Now match the command against the list of available
381 * ones.
382 */
383 ret.type = tok_cmd;
384 ret.text = ustrdup(rs.text);
385 match_kw(&ret);
386 sfree(rs.text);
387 return ret;
388 } else if (c == '{') { /* tok_lbrace */
389 ret.type = tok_lbrace;
390 return ret;
391 } else if (c == '}') { /* tok_rbrace */
392 ret.type = tok_rbrace;
393 return ret;
394 } else { /* tok_word */
395 /*
396 * Read a word: the longest possible contiguous sequence of
397 * things other than whitespace, backslash, braces and
398 * hyphen. A hyphen terminates the word but is returned as
399 * part of it; everything else is pushed back for the next
400 * token. The `aux' field contains TRUE if the word ends in
401 * a hyphen.
402 */
403 ret.aux = FALSE; /* assumed for now */
404 while (1) {
405 if (iswhite(c) || c=='{' || c=='}' || c=='\\' || c==EOF) {
406 /* Put back the character that caused termination */
407 unget(in, c, &cpos);
408 break;
409 } else {
410 rdadd(&rs, c);
411 if (c == '-') {
412 ret.aux = TRUE;
413 break; /* hyphen terminates word */
414 }
415 }
416 c = get(in, &cpos);
417 }
418 ret.type = tok_word;
419 ret.text = ustrdup(rs.text);
420 sfree(rs.text);
421 return ret;
422 }
423 }
424
425 /*
426 * Determine whether the next input character is an open brace (for
427 * telling code paragraphs from paragraphs which merely start with
428 * code).
429 */
430 int isbrace(input *in) {
431 int c;
432 filepos cpos;
433
434 c = get(in, &cpos);
435 unget(in, c, &cpos);
436 return (c == '{');
437 }
438
439 /*
440 * Read the rest of a line that starts `\c'. Including nothing at
441 * all (tok_word with empty text).
442 */
443 token get_codepar_token(input *in) {
444 int c;
445 token ret;
446 rdstring rs = { 0, 0, NULL };
447 filepos cpos;
448
449 ret.type = tok_word;
450 c = get(in, &cpos); /* expect (and discard) one space */
451 ret.pos = cpos;
452 if (c == ' ') {
453 c = get(in, &cpos);
454 ret.pos = cpos;
455 }
456 while (!isnl(c) && c != EOF) {
457 int c2 = c;
458 c = get(in, &cpos);
459 /* Discard \r just before \n. */
460 if (c2 != 13 || !isnl(c))
461 rdadd(&rs, c2);
462 }
463 unget(in, c, &cpos);
464 ret.text = ustrdup(rs.text);
465 sfree(rs.text);
466 return ret;
467 }
468
469 /*
470 * Adds a new word to a linked list
471 */
472 static word *addword(word newword, word ***hptrptr) {
473 word *mnewword;
474 if (!hptrptr)
475 return NULL;
476 mnewword = mknew(word);
477 *mnewword = newword; /* structure copy */
478 mnewword->next = NULL;
479 **hptrptr = mnewword;
480 *hptrptr = &mnewword->next;
481 return mnewword;
482 }
483
484 /*
485 * Adds a new paragraph to a linked list
486 */
487 static paragraph *addpara(paragraph newpara, paragraph ***hptrptr) {
488 paragraph *mnewpara = mknew(paragraph);
489 *mnewpara = newpara; /* structure copy */
490 mnewpara->next = NULL;
491 **hptrptr = mnewpara;
492 *hptrptr = &mnewpara->next;
493 return mnewpara;
494 }
495
496 /*
497 * Destructor before token is reassigned; should catch most memory
498 * leaks
499 */
500 #define dtor(t) ( sfree(t.text) )
501
502 /*
503 * Reads a single file (ie until get() returns EOF)
504 */
505 static void read_file(paragraph ***ret, input *in, indexdata *idx) {
506 token t;
507 paragraph par;
508 word wd, **whptr, **idximplicit;
509 tree234 *macros;
510 wchar_t utext[2], *wdtext;
511 int style, spcstyle;
512 int already;
513 int iswhite, seenwhite;
514 int type;
515 int prev_para_type;
516 struct stack_item {
517 enum {
518 stack_nop = 0, /* do nothing (for error recovery) */
519 stack_ualt = 1, /* \u alternative */
520 stack_style = 2, /* \e, \c, \cw */
521 stack_idx = 4, /* \I, \i, \ii */
522 stack_hyper = 8, /* \W */
523 stack_quote = 16, /* \q */
524 } type;
525 word **whptr; /* to restore from \u alternatives */
526 word **idximplicit; /* to restore from \u alternatives */
527 } *sitem;
528 stack parsestk;
529 struct crossparaitem {
530 int type; /* currently c_lcont or -1 */
531 int seen_lcont;
532 };
533 stack crossparastk;
534 word *indexword, *uword, *iword;
535 word *idxwordlist;
536 rdstring indexstr;
537 int index_downcase, index_visible, indexing;
538 const rdstring nullrs = { 0, 0, NULL };
539 wchar_t uchr;
540
541 t.text = NULL;
542 macros = newtree234(macrocmp);
543 already = FALSE;
544
545 crossparastk = stk_new();
546
547 /*
548 * Loop on each paragraph.
549 */
550 while (1) {
551 int start_cmd = c__invalid;
552 par.words = NULL;
553 par.keyword = NULL;
554 whptr = &par.words;
555
556 /*
557 * Get a token.
558 */
559 do {
560 if (!already) {
561 dtor(t), t = get_token(in);
562 }
563 already = FALSE;
564 } while (t.type == tok_eop);
565 if (t.type == tok_eof)
566 break;
567
568 /*
569 * Parse code paragraphs separately.
570 */
571 if (t.type == tok_cmd && t.cmd == c_c && !isbrace(in)) {
572 par.type = para_Code;
573 par.fpos = t.pos;
574 while (1) {
575 dtor(t), t = get_codepar_token(in);
576 wd.type = word_WeakCode;
577 wd.breaks = FALSE; /* shouldn't need this... */
578 wd.text = ustrdup(t.text);
579 wd.alt = NULL;
580 wd.fpos = t.pos;
581 addword(wd, &whptr);
582 dtor(t), t = get_token(in);
583 if (t.type == tok_white) {
584 /*
585 * The newline after a code-paragraph line
586 */
587 dtor(t), t = get_token(in);
588 }
589 if (t.type == tok_eop || t.type == tok_eof)
590 break;
591 else if (t.type != tok_cmd || t.cmd != c_c) {
592 error(err_brokencodepara, &t.pos);
593 prev_para_type = par.type;
594 addpara(par, ret);
595 while (t.type != tok_eop) /* error recovery: */
596 dtor(t), t = get_token(in); /* eat rest of paragraph */
597 goto codeparabroken; /* ick, but such is life */
598 }
599 }
600 prev_para_type = par.type;
601 addpara(par, ret);
602 codeparabroken:
603 continue;
604 }
605
606 /*
607 * Spot the special commands that define a grouping of more
608 * than one paragraph, and also the closing braces that
609 * finish them.
610 */
611 if (t.type == tok_cmd &&
612 t.cmd == c_lcont) {
613 struct crossparaitem *sitem, *stop;
614
615 /*
616 * Expect, and swallow, an open brace.
617 */
618 dtor(t), t = get_token(in);
619 if (t.type != tok_lbrace) {
620 error(err_explbr, &t.pos);
621 continue;
622 }
623
624 /*
625 * \lcont causes a continuation of a list item into
626 * multiple paragraphs (which may in turn contain
627 * nested lists, code paras etc). Hence, the previous
628 * paragraph must be of a list type.
629 */
630 sitem = mknew(struct crossparaitem);
631 stop = (struct crossparaitem *)stk_top(crossparastk);
632 if (prev_para_type == para_Bullet ||
633 prev_para_type == para_NumberedList ||
634 prev_para_type == para_Description) {
635 sitem->type = c_lcont;
636 sitem->seen_lcont = 1;
637 par.type = para_LcontPush;
638 prev_para_type = par.type;
639 addpara(par, ret);
640 } else {
641 /*
642 * Push a null item on the cross-para stack so that
643 * when we see the corresponding closing brace we
644 * don't give a cascade error.
645 */
646 sitem->type = -1;
647 sitem->seen_lcont = (stop ? stop->seen_lcont : 0);
648 error(err_misplacedlcont, &t.pos);
649 }
650 stk_push(crossparastk, sitem);
651 continue;
652 } else if (t.type == tok_rbrace) {
653 struct crossparaitem *sitem = stk_pop(crossparastk);
654 if (!sitem)
655 error(err_unexbrace, &t.pos);
656 else {
657 switch (sitem->type) {
658 case c_lcont:
659 par.type = para_LcontPop;
660 prev_para_type = par.type;
661 addpara(par, ret);
662 break;
663 }
664 sfree(sitem);
665 }
666 continue;
667 }
668
669 /*
670 * This token begins a paragraph. See if it's one of the
671 * special commands that define a paragraph type.
672 *
673 * (note that \# is special in a way, and \nocite takes no
674 * text)
675 */
676 par.type = para_Normal;
677 if (t.type == tok_cmd) {
678 int needkw;
679 int is_macro = FALSE;
680
681 par.fpos = t.pos;
682 switch (t.cmd) {
683 default:
684 needkw = -1;
685 break;
686 case c__invalid:
687 error(err_badparatype, t.text, &t.pos);
688 needkw = 4;
689 break;
690 case c__comment:
691 if (isbrace(in))
692 break; /* `\#{': isn't a comment para */
693 do {
694 dtor(t), t = get_token(in);
695 } while (t.type != tok_eop && t.type != tok_eof);
696 continue; /* next paragraph */
697 /*
698 * `needkw' values:
699 *
700 * 1 -- exactly one keyword
701 * 2 -- at least one keyword
702 * 4 -- any number of keywords including zero
703 * 8 -- at least one keyword and then nothing else
704 * 16 -- nothing at all! no keywords, no body
705 * 32 -- no keywords at all
706 */
707 case c_A: needkw = 2; par.type = para_Appendix; break;
708 case c_B: needkw = 2; par.type = para_Biblio; break;
709 case c_BR: needkw = 1; par.type = para_BR;
710 start_cmd = c_BR; break;
711 case c_C: needkw = 2; par.type = para_Chapter; break;
712 case c_H: needkw = 2; par.type = para_Heading;
713 par.aux = 0;
714 break;
715 case c_IM: needkw = 2; par.type = para_IM;
716 start_cmd = c_IM; break;
717 case c_S: needkw = 2; par.type = para_Subsect;
718 par.aux = t.aux; break;
719 case c_U: needkw = 32; par.type = para_UnnumberedChapter; break;
720 /* For \b and \n the keyword is optional */
721 case c_b: needkw = 4; par.type = para_Bullet; break;
722 case c_dt: needkw = 4; par.type = para_DescribedThing; break;
723 case c_dd: needkw = 4; par.type = para_Description; break;
724 case c_n: needkw = 4; par.type = para_NumberedList; break;
725 case c_cfg: needkw = 8; par.type = para_Config;
726 start_cmd = c_cfg; break;
727 case c_copyright: needkw = 32; par.type = para_Copyright; break;
728 case c_define: is_macro = TRUE; needkw = 1; break;
729 /* For \nocite the keyword is _everything_ */
730 case c_nocite: needkw = 8; par.type = para_NoCite; break;
731 case c_preamble: needkw = 32; par.type = para_Preamble; break;
732 case c_rule: needkw = 16; par.type = para_Rule; break;
733 case c_title: needkw = 32; par.type = para_Title; break;
734 case c_versionid: needkw = 32; par.type = para_VersionID; break;
735 }
736
737 if (par.type == para_Chapter ||
738 par.type == para_Heading ||
739 par.type == para_Subsect ||
740 par.type == para_Appendix ||
741 par.type == para_UnnumberedChapter) {
742 struct crossparaitem *sitem = stk_top(crossparastk);
743 if (sitem && sitem->seen_lcont) {
744 error(err_sectmarkerinlcont, &t.pos);
745 }
746 }
747
748 if (needkw > 0) {
749 rdstring rs = { 0, 0, NULL };
750 int nkeys = 0;
751 filepos fp;
752
753 /* Get keywords. */
754 dtor(t), t = get_token(in);
755 fp = t.pos;
756 while (t.type == tok_lbrace) {
757 /* This is a keyword. */
758 nkeys++;
759 /* FIXME: there will be bugs if anyone specifies an
760 * empty keyword (\foo{}), so trap this case. */
761 while (dtor(t), t = get_token(in),
762 t.type == tok_word ||
763 t.type == tok_white ||
764 (t.type == tok_cmd && t.cmd == c__nbsp) ||
765 (t.type == tok_cmd && t.cmd == c__escaped)) {
766 if (t.type == tok_white ||
767 (t.type == tok_cmd && t.cmd == c__nbsp))
768 rdadd(&rs, ' ');
769 else
770 rdadds(&rs, t.text);
771 }
772 if (t.type != tok_rbrace) {
773 error(err_kwunclosed, &t.pos);
774 continue;
775 }
776 rdadd(&rs, 0); /* add string terminator */
777 dtor(t), t = get_token(in); /* eat right brace */
778 }
779
780 rdadd(&rs, 0); /* add string terminator */
781
782 /* See whether we have the right number of keywords. */
783 if ((needkw & 48) && nkeys > 0)
784 error(err_kwillegal, &fp);
785 if ((needkw & 11) && nkeys == 0)
786 error(err_kwexpected, &fp);
787 if ((needkw & 5) && nkeys > 1)
788 error(err_kwtoomany, &fp);
789
790 if (is_macro) {
791 /*
792 * Macro definition. Get the rest of the line
793 * as a code-paragraph token, repeatedly until
794 * there's nothing more left of it. Separate
795 * with newlines.
796 */
797 rdstring macrotext = { 0, 0, NULL };
798 while (1) {
799 dtor(t), t = get_codepar_token(in);
800 if (macrotext.pos > 0)
801 rdadd(&macrotext, L'\n');
802 rdadds(&macrotext, t.text);
803 dtor(t), t = get_token(in);
804 if (t.type == tok_eop) break;
805 }
806 macrodef(macros, rs.text, macrotext.text, fp);
807 continue; /* next paragraph */
808 }
809
810 par.keyword = rdtrim(&rs);
811
812 /* Move to EOP in case of needkw==8 or 16 (no body) */
813 if (needkw & 24) {
814 /* We allow whitespace even when we expect no para body */
815 while (t.type == tok_white)
816 dtor(t), t = get_token(in);
817 if (t.type != tok_eop && t.type != tok_eof &&
818 (start_cmd == c__invalid ||
819 t.type != tok_cmd || t.cmd != start_cmd)) {
820 error(err_bodyillegal, &t.pos);
821 /* Error recovery: eat the rest of the paragraph */
822 while (t.type != tok_eop && t.type != tok_eof &&
823 (start_cmd == c__invalid ||
824 t.type != tok_cmd || t.cmd != start_cmd))
825 dtor(t), t = get_token(in);
826 }
827 if (t.type == tok_cmd)
828 already = TRUE;/* inhibit get_token at top of loop */
829 prev_para_type = par.type;
830 addpara(par, ret);
831 continue; /* next paragraph */
832 }
833 }
834 }
835
836 /*
837 * Now read the actual paragraph, word by word, adding to
838 * the paragraph list.
839 *
840 * Mid-paragraph commands:
841 *
842 * \K \k
843 * \c \cw
844 * \e
845 * \i \ii
846 * \I
847 * \u
848 * \W
849 * \date
850 * \\ \{ \}
851 */
852 parsestk = stk_new();
853 style = word_Normal;
854 spcstyle = word_WhiteSpace;
855 indexing = FALSE;
856 seenwhite = TRUE;
857 while (t.type != tok_eop && t.type != tok_eof) {
858 iswhite = FALSE;
859 already = FALSE;
860
861 /* Handle implicit paragraph breaks after \IM, \BR etc */
862 if (start_cmd != c__invalid &&
863 t.type == tok_cmd && t.cmd == start_cmd) {
864 already = TRUE; /* inhibit get_token at top of loop */
865 break;
866 }
867
868 if (t.type == tok_cmd && t.cmd == c__escaped) {
869 t.type = tok_word; /* nice and simple */
870 t.aux = 0; /* even if `\-' - nonbreaking! */
871 }
872 if (t.type == tok_cmd && t.cmd == c__nbsp) {
873 t.type = tok_word; /* nice and simple */
874 sfree(t.text);
875 t.text = ustrdup(L" "); /* text is ` ' not `_' */
876 t.aux = 0; /* (nonbreaking) */
877 }
878 switch (t.type) {
879 case tok_white:
880 if (whptr == &par.words)
881 break; /* strip whitespace at start of para */
882 wd.text = NULL;
883 wd.type = spcstyle;
884 wd.alt = NULL;
885 wd.aux = 0;
886 wd.fpos = t.pos;
887 wd.breaks = FALSE;
888
889 /*
890 * Inhibit use of whitespace if it's (probably the
891 * newline) before a repeat \IM / \BR type
892 * directive.
893 */
894 if (start_cmd != c__invalid) {
895 dtor(t), t = get_token(in);
896 already = TRUE;
897 if (t.type == tok_cmd && t.cmd == start_cmd)
898 break;
899 }
900
901 if (indexing)
902 rdadd(&indexstr, ' ');
903 if (!indexing || index_visible)
904 addword(wd, &whptr);
905 if (indexing)
906 addword(wd, &idximplicit);
907 iswhite = TRUE;
908 break;
909 case tok_word:
910 if (indexing)
911 rdadds(&indexstr, t.text);
912 wd.type = style;
913 wd.alt = NULL;
914 wd.aux = 0;
915 wd.fpos = t.pos;
916 wd.breaks = t.aux;
917 if (!indexing || index_visible) {
918 wd.text = ustrdup(t.text);
919 addword(wd, &whptr);
920 }
921 if (indexing) {
922 wd.text = ustrdup(t.text);
923 addword(wd, &idximplicit);
924 }
925 break;
926 case tok_lbrace:
927 error(err_unexbrace, &t.pos);
928 /* Error recovery: push nop */
929 sitem = mknew(struct stack_item);
930 sitem->type = stack_nop;
931 stk_push(parsestk, sitem);
932 break;
933 case tok_rbrace:
934 sitem = stk_pop(parsestk);
935 if (!sitem) {
936 /*
937 * This closing brace could have been an
938 * indication that the cross-paragraph stack
939 * wants popping. Accordingly, we treat it here
940 * as an indication that the paragraph is over.
941 */
942 already = TRUE;
943 goto finished_para;
944 } else {
945 if (sitem->type & stack_ualt) {
946 whptr = sitem->whptr;
947 idximplicit = sitem->idximplicit;
948 }
949 if (sitem->type & stack_style) {
950 style = word_Normal;
951 spcstyle = word_WhiteSpace;
952 }
953 if (sitem->type & stack_idx) {
954 indexword->text = ustrdup(indexstr.text);
955 if (index_downcase)
956 ustrlow(indexword->text);
957 indexing = FALSE;
958 rdadd(&indexstr, L'\0');
959 index_merge(idx, FALSE, indexstr.text, idxwordlist);
960 sfree(indexstr.text);
961 }
962 if (sitem->type & stack_hyper) {
963 wd.text = NULL;
964 wd.type = word_HyperEnd;
965 wd.alt = NULL;
966 wd.aux = 0;
967 wd.fpos = t.pos;
968 wd.breaks = FALSE;
969 if (!indexing || index_visible)
970 addword(wd, &whptr);
971 if (indexing)
972 addword(wd, &idximplicit);
973 }
974 if (sitem->type & stack_quote) {
975 wd.text = NULL;
976 wd.type = toquotestyle(style);
977 wd.alt = NULL;
978 wd.aux = quote_Close;
979 wd.fpos = t.pos;
980 wd.breaks = FALSE;
981 if (!indexing || index_visible)
982 addword(wd, &whptr);
983 if (indexing) {
984 rdadd(&indexstr, L'"');
985 addword(wd, &idximplicit);
986 }
987 }
988 }
989 sfree(sitem);
990 break;
991 case tok_cmd:
992 switch (t.cmd) {
993 case c__comment:
994 /*
995 * In-paragraph comment: \#{ balanced braces }
996 *
997 * Anything goes here; even tok_eop. We should
998 * eat whitespace after the close brace _if_
999 * there was whitespace before the \#.
1000 */
1001 dtor(t), t = get_token(in);
1002 if (t.type != tok_lbrace) {
1003 error(err_explbr, &t.pos);
1004 } else {
1005 int braces = 1;
1006 while (braces > 0) {
1007 dtor(t), t = get_token(in);
1008 if (t.type == tok_lbrace)
1009 braces++;
1010 else if (t.type == tok_rbrace)
1011 braces--;
1012 else if (t.type == tok_eof) {
1013 error(err_commenteof, &t.pos);
1014 break;
1015 }
1016 }
1017 }
1018 if (seenwhite) {
1019 already = TRUE;
1020 dtor(t), t = get_token(in);
1021 if (t.type == tok_white) {
1022 iswhite = TRUE;
1023 already = FALSE;
1024 }
1025 }
1026 break;
1027 case c_q:
1028 dtor(t), t = get_token(in);
1029 if (t.type != tok_lbrace) {
1030 error(err_explbr, &t.pos);
1031 } else {
1032 wd.text = NULL;
1033 wd.type = toquotestyle(style);
1034 wd.alt = NULL;
1035 wd.aux = quote_Open;
1036 wd.fpos = t.pos;
1037 wd.breaks = FALSE;
1038 if (!indexing || index_visible)
1039 addword(wd, &whptr);
1040 if (indexing) {
1041 rdadd(&indexstr, L'"');
1042 addword(wd, &idximplicit);
1043 }
1044 sitem = mknew(struct stack_item);
1045 sitem->type = stack_quote;
1046 stk_push(parsestk, sitem);
1047 }
1048 break;
1049 case c_K:
1050 case c_k:
1051 case c_W:
1052 case c_date:
1053 /*
1054 * Keyword, hyperlink, or \date. We expect a
1055 * left brace, some text, and then a right
1056 * brace. No nesting; no arguments.
1057 */
1058 wd.fpos = t.pos;
1059 wd.breaks = FALSE;
1060 if (t.cmd == c_K)
1061 wd.type = word_UpperXref;
1062 else if (t.cmd == c_k)
1063 wd.type = word_LowerXref;
1064 else if (t.cmd == c_W)
1065 wd.type = word_HyperLink;
1066 else
1067 wd.type = word_Normal;
1068 dtor(t), t = get_token(in);
1069 if (t.type != tok_lbrace) {
1070 if (wd.type == word_Normal) {
1071 time_t thetime = time(NULL);
1072 struct tm *broken = localtime(&thetime);
1073 already = TRUE;
1074 wdtext = ustrftime(NULL, broken);
1075 wd.type = style;
1076 } else {
1077 error(err_explbr, &t.pos);
1078 wdtext = NULL;
1079 }
1080 } else {
1081 rdstring rs = { 0, 0, NULL };
1082 while (dtor(t), t = get_token(in),
1083 t.type == tok_word || t.type == tok_white) {
1084 if (t.type == tok_white)
1085 rdadd(&rs, ' ');
1086 else
1087 rdadds(&rs, t.text);
1088 }
1089 if (wd.type == word_Normal) {
1090 time_t thetime = time(NULL);
1091 struct tm *broken = localtime(&thetime);
1092 wdtext = ustrftime(rs.text, broken);
1093 wd.type = style;
1094 } else {
1095 wdtext = ustrdup(rs.text);
1096 }
1097 sfree(rs.text);
1098 if (t.type != tok_rbrace) {
1099 error(err_kwexprbr, &t.pos);
1100 }
1101 }
1102 wd.alt = NULL;
1103 wd.aux = 0;
1104 if (!indexing || index_visible) {
1105 wd.text = ustrdup(wdtext);
1106 addword(wd, &whptr);
1107 }
1108 if (indexing) {
1109 wd.text = ustrdup(wdtext);
1110 addword(wd, &idximplicit);
1111 }
1112 sfree(wdtext);
1113 if (wd.type == word_HyperLink) {
1114 /*
1115 * Hyperlinks are different: they then
1116 * expect another left brace, to begin
1117 * delimiting the text marked by the link.
1118 */
1119 dtor(t), t = get_token(in);
1120 /*
1121 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1122 */
1123 sitem = mknew(struct stack_item);
1124 sitem->type = stack_hyper;
1125 if (t.type == tok_cmd &&
1126 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1127 if (style != word_Normal)
1128 error(err_nestedstyles, &t.pos);
1129 else {
1130 style = (t.cmd == c_c ? word_Code :
1131 t.cmd == c_cw ? word_WeakCode :
1132 word_Emph);
1133 spcstyle = tospacestyle(style);
1134 sitem->type |= stack_style;
1135 }
1136 dtor(t), t = get_token(in);
1137 }
1138 if (t.type != tok_lbrace) {
1139 error(err_explbr, &t.pos);
1140 sfree(sitem);
1141 } else {
1142 stk_push(parsestk, sitem);
1143 }
1144 }
1145 break;
1146 case c_c:
1147 case c_cw:
1148 case c_e:
1149 type = t.cmd;
1150 if (style != word_Normal) {
1151 error(err_nestedstyles, &t.pos);
1152 /* Error recovery: eat lbrace, push nop. */
1153 dtor(t), t = get_token(in);
1154 sitem = mknew(struct stack_item);
1155 sitem->type = stack_nop;
1156 stk_push(parsestk, sitem);
1157 }
1158 dtor(t), t = get_token(in);
1159 if (t.type != tok_lbrace) {
1160 error(err_explbr, &t.pos);
1161 } else {
1162 style = (type == c_c ? word_Code :
1163 type == c_cw ? word_WeakCode :
1164 word_Emph);
1165 spcstyle = tospacestyle(style);
1166 sitem = mknew(struct stack_item);
1167 sitem->type = stack_style;
1168 stk_push(parsestk, sitem);
1169 }
1170 break;
1171 case c_i:
1172 case c_ii:
1173 case c_I:
1174 type = t.cmd;
1175 if (indexing) {
1176 error(err_nestedindex, &t.pos);
1177 /* Error recovery: eat lbrace, push nop. */
1178 dtor(t), t = get_token(in);
1179 sitem = mknew(struct stack_item);
1180 sitem->type = stack_nop;
1181 stk_push(parsestk, sitem);
1182 }
1183 sitem = mknew(struct stack_item);
1184 sitem->type = stack_idx;
1185 dtor(t), t = get_token(in);
1186 /*
1187 * Special cases: \i\c, \i\e, \i\cw
1188 */
1189 wd.fpos = t.pos;
1190 if (t.type == tok_cmd &&
1191 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1192 if (style != word_Normal)
1193 error(err_nestedstyles, &t.pos);
1194 else {
1195 style = (t.cmd == c_c ? word_Code :
1196 t.cmd == c_cw ? word_WeakCode :
1197 word_Emph);
1198 spcstyle = tospacestyle(style);
1199 sitem->type |= stack_style;
1200 }
1201 dtor(t), t = get_token(in);
1202 }
1203 if (t.type != tok_lbrace) {
1204 sfree(sitem);
1205 error(err_explbr, &t.pos);
1206 } else {
1207 /* Add an index-reference word with no text as yet */
1208 wd.type = word_IndexRef;
1209 wd.text = NULL;
1210 wd.alt = NULL;
1211 wd.aux = 0;
1212 wd.breaks = FALSE;
1213 indexword = addword(wd, &whptr);
1214 /* Set up a rdstring to read the index text */
1215 indexstr = nullrs;
1216 /* Flags so that we do the Right Things with text */
1217 index_visible = (type != c_I);
1218 index_downcase = (type == c_ii);
1219 indexing = TRUE;
1220 idxwordlist = NULL;
1221 idximplicit = &idxwordlist;
1222 /* Stack item to close the indexing on exit */
1223 stk_push(parsestk, sitem);
1224 }
1225 break;
1226 case c_u:
1227 uchr = t.aux;
1228 utext[0] = uchr; utext[1] = 0;
1229 wd.type = style;
1230 wd.breaks = FALSE;
1231 wd.alt = NULL;
1232 wd.aux = 0;
1233 wd.fpos = t.pos;
1234 if (!indexing || index_visible) {
1235 wd.text = ustrdup(utext);
1236 uword = addword(wd, &whptr);
1237 } else
1238 uword = NULL;
1239 if (indexing) {
1240 wd.text = ustrdup(utext);
1241 iword = addword(wd, &idximplicit);
1242 } else
1243 iword = NULL;
1244 dtor(t), t = get_token(in);
1245 if (t.type == tok_lbrace) {
1246 /*
1247 * \u with a left brace. Until the brace
1248 * closes, all further words go on a
1249 * sidetrack from the main thread of the
1250 * paragraph.
1251 */
1252 sitem = mknew(struct stack_item);
1253 sitem->type = stack_ualt;
1254 sitem->whptr = whptr;
1255 sitem->idximplicit = idximplicit;
1256 stk_push(parsestk, sitem);
1257 whptr = uword ? &uword->alt : NULL;
1258 idximplicit = iword ? &iword->alt : NULL;
1259 } else {
1260 if (indexing)
1261 rdadd(&indexstr, uchr);
1262 already = TRUE;
1263 }
1264 break;
1265 default:
1266 if (!macrolookup(macros, in, t.text, &t.pos))
1267 error(err_badmidcmd, t.text, &t.pos);
1268 break;
1269 }
1270 }
1271 if (!already)
1272 dtor(t), t = get_token(in);
1273 seenwhite = iswhite;
1274 }
1275 finished_para:
1276 /* Check the stack is empty */
1277 if (stk_top(parsestk)) {
1278 while ((sitem = stk_pop(parsestk)))
1279 sfree(sitem);
1280 error(err_missingrbrace, &t.pos);
1281 }
1282 stk_free(parsestk);
1283 prev_para_type = par.type;
1284 addpara(par, ret);
1285 if (t.type == tok_eof)
1286 already = TRUE;
1287 }
1288
1289 if (stk_top(crossparastk)) {
1290 void *p;
1291
1292 error(err_missingrbrace2, &t.pos);
1293 while ((p = stk_pop(crossparastk)))
1294 sfree(p);
1295 }
1296
1297 /*
1298 * We break to here rather than returning, because otherwise
1299 * this cleanup doesn't happen.
1300 */
1301 dtor(t);
1302 macrocleanup(macros);
1303
1304 stk_free(crossparastk);
1305 }
1306
1307 paragraph *read_input(input *in, indexdata *idx) {
1308 paragraph *head = NULL;
1309 paragraph **hptr = &head;
1310
1311 while (in->currindex < in->nfiles) {
1312 in->currfp = fopen(in->filenames[in->currindex], "r");
1313 if (in->currfp) {
1314 setpos(in, in->filenames[in->currindex]);
1315 read_file(&hptr, in, idx);
1316 }
1317 in->currindex++;
1318 }
1319
1320 return head;
1321 }