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