\lcont and \quote were being confused by whitespace (in particular,
[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 t.type == tok_rbrace) { /* might be } terminating \lcont */
597 if (t.type == tok_rbrace)
598 already = TRUE;
599 break;
600 } else if (t.type == tok_cmd && t.cmd == c_c) {
601 wtype = word_WeakCode;
602 } else if (t.type == tok_cmd && t.cmd == c_e &&
603 wtype == word_WeakCode) {
604 wtype = word_Emph;
605 } else {
606 error(err_brokencodepara, &t.pos);
607 prev_para_type = par.type;
608 addpara(par, ret);
609 while (t.type != tok_eop) /* error recovery: */
610 dtor(t), t = get_token(in); /* eat rest of paragraph */
611 goto codeparabroken; /* ick, but such is life */
612 }
613 }
614 prev_para_type = par.type;
615 addpara(par, ret);
616 codeparabroken:
617 continue;
618 }
619
620 /*
621 * Spot the special commands that define a grouping of more
622 * than one paragraph, and also the closing braces that
623 * finish them.
624 */
625 if (t.type == tok_cmd &&
626 (t.cmd == c_lcont || t.cmd == c_quote)) {
627 struct crossparaitem *sitem, *stop;
628 int cmd = t.cmd;
629
630 /*
631 * Expect, and swallow, an open brace.
632 */
633 dtor(t), t = get_token(in);
634 if (t.type != tok_lbrace) {
635 error(err_explbr, &t.pos);
636 continue;
637 }
638
639 /*
640 * Also expect, and swallow, any whitespace after that
641 * (a newline before a code paragraph wouldn't be
642 * surprising).
643 */
644 do {
645 dtor(t), t = get_token(in);
646 } while (t.type == tok_white);
647 already = TRUE;
648
649 if (cmd == c_lcont) {
650 /*
651 * \lcont causes a continuation of a list item into
652 * multiple paragraphs (which may in turn contain
653 * nested lists, code paras etc). Hence, the previous
654 * paragraph must be of a list type.
655 */
656 sitem = mknew(struct crossparaitem);
657 stop = (struct crossparaitem *)stk_top(crossparastk);
658 if (stop)
659 *sitem = *stop;
660 else
661 sitem->seen_quote = sitem->seen_lcont = 0;
662
663 if (prev_para_type == para_Bullet ||
664 prev_para_type == para_NumberedList ||
665 prev_para_type == para_Description) {
666 sitem->type = c_lcont;
667 sitem->seen_lcont = 1;
668 par.type = para_LcontPush;
669 prev_para_type = par.type;
670 addpara(par, ret);
671 } else {
672 /*
673 * Push a null item on the cross-para stack so that
674 * when we see the corresponding closing brace we
675 * don't give a cascade error.
676 */
677 sitem->type = -1;
678 error(err_misplacedlcont, &t.pos);
679 }
680 } else {
681 /*
682 * \quote causes a group of paragraphs to be
683 * block-quoted (typically they will be indented a
684 * bit).
685 */
686 sitem = mknew(struct crossparaitem);
687 stop = (struct crossparaitem *)stk_top(crossparastk);
688 if (stop)
689 *sitem = *stop;
690 else
691 sitem->seen_quote = sitem->seen_lcont = 0;
692 sitem->type = c_quote;
693 sitem->seen_quote = 1;
694 par.type = para_QuotePush;
695 prev_para_type = par.type;
696 addpara(par, ret);
697 }
698 stk_push(crossparastk, sitem);
699 continue;
700 } else if (t.type == tok_rbrace) {
701 struct crossparaitem *sitem = stk_pop(crossparastk);
702 if (!sitem)
703 error(err_unexbrace, &t.pos);
704 else {
705 switch (sitem->type) {
706 case c_lcont:
707 par.type = para_LcontPop;
708 prev_para_type = par.type;
709 addpara(par, ret);
710 break;
711 case c_quote:
712 par.type = para_QuotePop;
713 prev_para_type = par.type;
714 addpara(par, ret);
715 break;
716 }
717 sfree(sitem);
718 }
719 continue;
720 }
721
722 /*
723 * This token begins a paragraph. See if it's one of the
724 * special commands that define a paragraph type.
725 *
726 * (note that \# is special in a way, and \nocite takes no
727 * text)
728 */
729 par.type = para_Normal;
730 if (t.type == tok_cmd) {
731 int needkw;
732 int is_macro = FALSE;
733
734 par.fpos = t.pos;
735 switch (t.cmd) {
736 default:
737 needkw = -1;
738 break;
739 case c__invalid:
740 error(err_badparatype, t.text, &t.pos);
741 needkw = 4;
742 break;
743 case c__comment:
744 if (isbrace(in))
745 break; /* `\#{': isn't a comment para */
746 do {
747 dtor(t), t = get_token(in);
748 } while (t.type != tok_eop && t.type != tok_eof);
749 continue; /* next paragraph */
750 /*
751 * `needkw' values:
752 *
753 * 1 -- exactly one keyword
754 * 2 -- at least one keyword
755 * 4 -- any number of keywords including zero
756 * 8 -- at least one keyword and then nothing else
757 * 16 -- nothing at all! no keywords, no body
758 * 32 -- no keywords at all
759 */
760 case c_A: needkw = 2; par.type = para_Appendix; break;
761 case c_B: needkw = 2; par.type = para_Biblio; break;
762 case c_BR: needkw = 1; par.type = para_BR;
763 start_cmd = c_BR; break;
764 case c_C: needkw = 2; par.type = para_Chapter; break;
765 case c_H: needkw = 2; par.type = para_Heading;
766 par.aux = 0;
767 break;
768 case c_IM: needkw = 2; par.type = para_IM;
769 start_cmd = c_IM; break;
770 case c_S: needkw = 2; par.type = para_Subsect;
771 par.aux = t.aux; break;
772 case c_U: needkw = 32; par.type = para_UnnumberedChapter; break;
773 /* For \b and \n the keyword is optional */
774 case c_b: needkw = 4; par.type = para_Bullet; break;
775 case c_dt: needkw = 4; par.type = para_DescribedThing; break;
776 case c_dd: needkw = 4; par.type = para_Description; break;
777 case c_n: needkw = 4; par.type = para_NumberedList; break;
778 case c_cfg: needkw = 8; par.type = para_Config;
779 start_cmd = c_cfg; break;
780 case c_copyright: needkw = 32; par.type = para_Copyright; break;
781 case c_define: is_macro = TRUE; needkw = 1; break;
782 /* For \nocite the keyword is _everything_ */
783 case c_nocite: needkw = 8; par.type = para_NoCite; break;
784 case c_preamble: needkw = 32; par.type = para_Normal; break;
785 case c_rule: needkw = 16; par.type = para_Rule; break;
786 case c_title: needkw = 32; par.type = para_Title; break;
787 case c_versionid: needkw = 32; par.type = para_VersionID; break;
788 }
789
790 if (par.type == para_Chapter ||
791 par.type == para_Heading ||
792 par.type == para_Subsect ||
793 par.type == para_Appendix ||
794 par.type == para_UnnumberedChapter) {
795 struct crossparaitem *sitem = stk_top(crossparastk);
796 if (sitem && (sitem->seen_lcont || sitem->seen_quote)) {
797 error(err_sectmarkerinblock,
798 &t.pos,
799 (sitem->seen_lcont ? "lcont" : "quote"));
800 }
801 }
802
803 if (needkw > 0) {
804 rdstring rs = { 0, 0, NULL };
805 int nkeys = 0;
806 filepos fp;
807
808 /* Get keywords. */
809 dtor(t), t = get_token(in);
810 fp = t.pos;
811 while (t.type == tok_lbrace) {
812 /* This is a keyword. */
813 nkeys++;
814 /* FIXME: there will be bugs if anyone specifies an
815 * empty keyword (\foo{}), so trap this case. */
816 while (dtor(t), t = get_token(in),
817 t.type == tok_word ||
818 t.type == tok_white ||
819 (t.type == tok_cmd && t.cmd == c__nbsp) ||
820 (t.type == tok_cmd && t.cmd == c__escaped)) {
821 if (t.type == tok_white ||
822 (t.type == tok_cmd && t.cmd == c__nbsp))
823 rdadd(&rs, ' ');
824 else
825 rdadds(&rs, t.text);
826 }
827 if (t.type != tok_rbrace) {
828 error(err_kwunclosed, &t.pos);
829 continue;
830 }
831 rdadd(&rs, 0); /* add string terminator */
832 dtor(t), t = get_token(in); /* eat right brace */
833 }
834
835 rdadd(&rs, 0); /* add string terminator */
836
837 /* See whether we have the right number of keywords. */
838 if ((needkw & 48) && nkeys > 0)
839 error(err_kwillegal, &fp);
840 if ((needkw & 11) && nkeys == 0)
841 error(err_kwexpected, &fp);
842 if ((needkw & 5) && nkeys > 1)
843 error(err_kwtoomany, &fp);
844
845 if (is_macro) {
846 /*
847 * Macro definition. Get the rest of the line
848 * as a code-paragraph token, repeatedly until
849 * there's nothing more left of it. Separate
850 * with newlines.
851 */
852 rdstring macrotext = { 0, 0, NULL };
853 while (1) {
854 dtor(t), t = get_codepar_token(in);
855 if (macrotext.pos > 0)
856 rdadd(&macrotext, L'\n');
857 rdadds(&macrotext, t.text);
858 dtor(t), t = get_token(in);
859 if (t.type == tok_eop) break;
860 }
861 macrodef(macros, rs.text, macrotext.text, fp);
862 continue; /* next paragraph */
863 }
864
865 par.keyword = rdtrim(&rs);
866
867 /* Move to EOP in case of needkw==8 or 16 (no body) */
868 if (needkw & 24) {
869 /* We allow whitespace even when we expect no para body */
870 while (t.type == tok_white)
871 dtor(t), t = get_token(in);
872 if (t.type != tok_eop && t.type != tok_eof &&
873 (start_cmd == c__invalid ||
874 t.type != tok_cmd || t.cmd != start_cmd)) {
875 error(err_bodyillegal, &t.pos);
876 /* Error recovery: eat the rest of the paragraph */
877 while (t.type != tok_eop && t.type != tok_eof &&
878 (start_cmd == c__invalid ||
879 t.type != tok_cmd || t.cmd != start_cmd))
880 dtor(t), t = get_token(in);
881 }
882 if (t.type == tok_cmd)
883 already = TRUE;/* inhibit get_token at top of loop */
884 prev_para_type = par.type;
885 addpara(par, ret);
886 continue; /* next paragraph */
887 }
888 }
889 }
890
891 /*
892 * Now read the actual paragraph, word by word, adding to
893 * the paragraph list.
894 *
895 * Mid-paragraph commands:
896 *
897 * \K \k
898 * \c \cw
899 * \e
900 * \i \ii
901 * \I
902 * \u
903 * \W
904 * \date
905 * \\ \{ \}
906 */
907 parsestk = stk_new();
908 style = word_Normal;
909 spcstyle = word_WhiteSpace;
910 indexing = FALSE;
911 seenwhite = TRUE;
912 while (t.type != tok_eop && t.type != tok_eof) {
913 iswhite = FALSE;
914 already = FALSE;
915
916 /* Handle implicit paragraph breaks after \IM, \BR etc */
917 if (start_cmd != c__invalid &&
918 t.type == tok_cmd && t.cmd == start_cmd) {
919 already = TRUE; /* inhibit get_token at top of loop */
920 break;
921 }
922
923 if (t.type == tok_cmd && t.cmd == c__nop) {
924 dtor(t), t = get_token(in);
925 continue; /* do nothing! */
926 }
927
928 if (t.type == tok_cmd && t.cmd == c__escaped) {
929 t.type = tok_word; /* nice and simple */
930 t.aux = 0; /* even if `\-' - nonbreaking! */
931 }
932 if (t.type == tok_cmd && t.cmd == c__nbsp) {
933 t.type = tok_word; /* nice and simple */
934 sfree(t.text);
935 t.text = ustrdup(L" "); /* text is ` ' not `_' */
936 t.aux = 0; /* (nonbreaking) */
937 }
938 switch (t.type) {
939 case tok_white:
940 if (whptr == &par.words)
941 break; /* strip whitespace at start of para */
942 wd.text = NULL;
943 wd.type = spcstyle;
944 wd.alt = NULL;
945 wd.aux = 0;
946 wd.fpos = t.pos;
947 wd.breaks = FALSE;
948
949 /*
950 * Inhibit use of whitespace if it's (probably the
951 * newline) before a repeat \IM / \BR type
952 * directive.
953 */
954 if (start_cmd != c__invalid) {
955 dtor(t), t = get_token(in);
956 already = TRUE;
957 if (t.type == tok_cmd && t.cmd == start_cmd)
958 break;
959 }
960
961 if (indexing)
962 rdadd(&indexstr, ' ');
963 if (!indexing || index_visible)
964 addword(wd, &whptr);
965 if (indexing)
966 addword(wd, &idximplicit);
967 iswhite = TRUE;
968 break;
969 case tok_word:
970 if (indexing)
971 rdadds(&indexstr, t.text);
972 wd.type = style;
973 wd.alt = NULL;
974 wd.aux = 0;
975 wd.fpos = t.pos;
976 wd.breaks = t.aux;
977 if (!indexing || index_visible) {
978 wd.text = ustrdup(t.text);
979 addword(wd, &whptr);
980 }
981 if (indexing) {
982 wd.text = ustrdup(t.text);
983 addword(wd, &idximplicit);
984 }
985 break;
986 case tok_lbrace:
987 error(err_unexbrace, &t.pos);
988 /* Error recovery: push nop */
989 sitem = mknew(struct stack_item);
990 sitem->type = stack_nop;
991 stk_push(parsestk, sitem);
992 break;
993 case tok_rbrace:
994 sitem = stk_pop(parsestk);
995 if (!sitem) {
996 /*
997 * This closing brace could have been an
998 * indication that the cross-paragraph stack
999 * wants popping. Accordingly, we treat it here
1000 * as an indication that the paragraph is over.
1001 */
1002 already = TRUE;
1003 goto finished_para;
1004 } else {
1005 if (sitem->type & stack_ualt) {
1006 whptr = sitem->whptr;
1007 idximplicit = sitem->idximplicit;
1008 }
1009 if (sitem->type & stack_style) {
1010 style = word_Normal;
1011 spcstyle = word_WhiteSpace;
1012 }
1013 if (sitem->type & stack_idx) {
1014 indexword->text = ustrdup(indexstr.text);
1015 if (index_downcase) {
1016 word *w;
1017
1018 ustrlow(indexword->text);
1019 ustrlow(indexstr.text);
1020
1021 for (w = idxwordlist; w; w = w->next)
1022 if (w->text)
1023 ustrlow(w->text);
1024 }
1025 indexing = FALSE;
1026 rdadd(&indexstr, L'\0');
1027 index_merge(idx, FALSE, indexstr.text, idxwordlist);
1028 sfree(indexstr.text);
1029 }
1030 if (sitem->type & stack_hyper) {
1031 wd.text = NULL;
1032 wd.type = word_HyperEnd;
1033 wd.alt = NULL;
1034 wd.aux = 0;
1035 wd.fpos = t.pos;
1036 wd.breaks = FALSE;
1037 if (!indexing || index_visible)
1038 addword(wd, &whptr);
1039 if (indexing)
1040 addword(wd, &idximplicit);
1041 }
1042 if (sitem->type & stack_quote) {
1043 wd.text = NULL;
1044 wd.type = toquotestyle(style);
1045 wd.alt = NULL;
1046 wd.aux = quote_Close;
1047 wd.fpos = t.pos;
1048 wd.breaks = FALSE;
1049 if (!indexing || index_visible)
1050 addword(wd, &whptr);
1051 if (indexing) {
1052 rdadd(&indexstr, L'"');
1053 addword(wd, &idximplicit);
1054 }
1055 }
1056 }
1057 sfree(sitem);
1058 break;
1059 case tok_cmd:
1060 switch (t.cmd) {
1061 case c__comment:
1062 /*
1063 * In-paragraph comment: \#{ balanced braces }
1064 *
1065 * Anything goes here; even tok_eop. We should
1066 * eat whitespace after the close brace _if_
1067 * there was whitespace before the \#.
1068 */
1069 dtor(t), t = get_token(in);
1070 if (t.type != tok_lbrace) {
1071 error(err_explbr, &t.pos);
1072 } else {
1073 int braces = 1;
1074 while (braces > 0) {
1075 dtor(t), t = get_token(in);
1076 if (t.type == tok_lbrace)
1077 braces++;
1078 else if (t.type == tok_rbrace)
1079 braces--;
1080 else if (t.type == tok_eof) {
1081 error(err_commenteof, &t.pos);
1082 break;
1083 }
1084 }
1085 }
1086 if (seenwhite) {
1087 already = TRUE;
1088 dtor(t), t = get_token(in);
1089 if (t.type == tok_white) {
1090 iswhite = TRUE;
1091 already = FALSE;
1092 }
1093 }
1094 break;
1095 case c_q:
1096 dtor(t), t = get_token(in);
1097 if (t.type != tok_lbrace) {
1098 error(err_explbr, &t.pos);
1099 } else {
1100 wd.text = NULL;
1101 wd.type = toquotestyle(style);
1102 wd.alt = NULL;
1103 wd.aux = quote_Open;
1104 wd.fpos = t.pos;
1105 wd.breaks = FALSE;
1106 if (!indexing || index_visible)
1107 addword(wd, &whptr);
1108 if (indexing) {
1109 rdadd(&indexstr, L'"');
1110 addword(wd, &idximplicit);
1111 }
1112 sitem = mknew(struct stack_item);
1113 sitem->type = stack_quote;
1114 stk_push(parsestk, sitem);
1115 }
1116 break;
1117 case c_K:
1118 case c_k:
1119 case c_W:
1120 case c_date:
1121 /*
1122 * Keyword, hyperlink, or \date. We expect a
1123 * left brace, some text, and then a right
1124 * brace. No nesting; no arguments.
1125 */
1126 wd.fpos = t.pos;
1127 wd.breaks = FALSE;
1128 if (t.cmd == c_K)
1129 wd.type = word_UpperXref;
1130 else if (t.cmd == c_k)
1131 wd.type = word_LowerXref;
1132 else if (t.cmd == c_W)
1133 wd.type = word_HyperLink;
1134 else
1135 wd.type = word_Normal;
1136 dtor(t), t = get_token(in);
1137 if (t.type != tok_lbrace) {
1138 if (wd.type == word_Normal) {
1139 time_t thetime = time(NULL);
1140 struct tm *broken = localtime(&thetime);
1141 already = TRUE;
1142 wdtext = ustrftime(NULL, broken);
1143 wd.type = style;
1144 } else {
1145 error(err_explbr, &t.pos);
1146 wdtext = NULL;
1147 }
1148 } else {
1149 rdstring rs = { 0, 0, NULL };
1150 while (dtor(t), t = get_token(in),
1151 t.type == tok_word || t.type == tok_white) {
1152 if (t.type == tok_white)
1153 rdadd(&rs, ' ');
1154 else
1155 rdadds(&rs, t.text);
1156 }
1157 if (wd.type == word_Normal) {
1158 time_t thetime = time(NULL);
1159 struct tm *broken = localtime(&thetime);
1160 wdtext = ustrftime(rs.text, broken);
1161 wd.type = style;
1162 } else {
1163 wdtext = ustrdup(rs.text);
1164 }
1165 sfree(rs.text);
1166 if (t.type != tok_rbrace) {
1167 error(err_kwexprbr, &t.pos);
1168 }
1169 }
1170 wd.alt = NULL;
1171 wd.aux = 0;
1172 if (!indexing || index_visible) {
1173 wd.text = ustrdup(wdtext);
1174 addword(wd, &whptr);
1175 }
1176 if (indexing) {
1177 wd.text = ustrdup(wdtext);
1178 addword(wd, &idximplicit);
1179 }
1180 sfree(wdtext);
1181 if (wd.type == word_HyperLink) {
1182 /*
1183 * Hyperlinks are different: they then
1184 * expect another left brace, to begin
1185 * delimiting the text marked by the link.
1186 */
1187 dtor(t), t = get_token(in);
1188 /*
1189 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1190 */
1191 sitem = mknew(struct stack_item);
1192 sitem->type = stack_hyper;
1193 if (t.type == tok_cmd &&
1194 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1195 if (style != word_Normal)
1196 error(err_nestedstyles, &t.pos);
1197 else {
1198 style = (t.cmd == c_c ? word_Code :
1199 t.cmd == c_cw ? word_WeakCode :
1200 word_Emph);
1201 spcstyle = tospacestyle(style);
1202 sitem->type |= stack_style;
1203 }
1204 dtor(t), t = get_token(in);
1205 }
1206 if (t.type != tok_lbrace) {
1207 error(err_explbr, &t.pos);
1208 sfree(sitem);
1209 } else {
1210 stk_push(parsestk, sitem);
1211 }
1212 }
1213 break;
1214 case c_c:
1215 case c_cw:
1216 case c_e:
1217 type = t.cmd;
1218 if (style != word_Normal) {
1219 error(err_nestedstyles, &t.pos);
1220 /* Error recovery: eat lbrace, push nop. */
1221 dtor(t), t = get_token(in);
1222 sitem = mknew(struct stack_item);
1223 sitem->type = stack_nop;
1224 stk_push(parsestk, sitem);
1225 }
1226 dtor(t), t = get_token(in);
1227 if (t.type != tok_lbrace) {
1228 error(err_explbr, &t.pos);
1229 } else {
1230 style = (type == c_c ? word_Code :
1231 type == c_cw ? word_WeakCode :
1232 word_Emph);
1233 spcstyle = tospacestyle(style);
1234 sitem = mknew(struct stack_item);
1235 sitem->type = stack_style;
1236 stk_push(parsestk, sitem);
1237 }
1238 break;
1239 case c_i:
1240 case c_ii:
1241 case c_I:
1242 type = t.cmd;
1243 if (indexing) {
1244 error(err_nestedindex, &t.pos);
1245 /* Error recovery: eat lbrace, push nop. */
1246 dtor(t), t = get_token(in);
1247 sitem = mknew(struct stack_item);
1248 sitem->type = stack_nop;
1249 stk_push(parsestk, sitem);
1250 }
1251 sitem = mknew(struct stack_item);
1252 sitem->type = stack_idx;
1253 dtor(t), t = get_token(in);
1254 /*
1255 * Special cases: \i\c, \i\e, \i\cw
1256 */
1257 wd.fpos = t.pos;
1258 if (t.type == tok_cmd &&
1259 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1260 if (style != word_Normal)
1261 error(err_nestedstyles, &t.pos);
1262 else {
1263 style = (t.cmd == c_c ? word_Code :
1264 t.cmd == c_cw ? word_WeakCode :
1265 word_Emph);
1266 spcstyle = tospacestyle(style);
1267 sitem->type |= stack_style;
1268 }
1269 dtor(t), t = get_token(in);
1270 }
1271 if (t.type != tok_lbrace) {
1272 sfree(sitem);
1273 error(err_explbr, &t.pos);
1274 } else {
1275 /* Add an index-reference word with no text as yet */
1276 wd.type = word_IndexRef;
1277 wd.text = NULL;
1278 wd.alt = NULL;
1279 wd.aux = 0;
1280 wd.breaks = FALSE;
1281 indexword = addword(wd, &whptr);
1282 /* Set up a rdstring to read the index text */
1283 indexstr = nullrs;
1284 /* Flags so that we do the Right Things with text */
1285 index_visible = (type != c_I);
1286 index_downcase = (type == c_ii);
1287 indexing = TRUE;
1288 idxwordlist = NULL;
1289 idximplicit = &idxwordlist;
1290 /* Stack item to close the indexing on exit */
1291 stk_push(parsestk, sitem);
1292 }
1293 break;
1294 case c_u:
1295 uchr = t.aux;
1296 utext[0] = uchr; utext[1] = 0;
1297 wd.type = style;
1298 wd.breaks = FALSE;
1299 wd.alt = NULL;
1300 wd.aux = 0;
1301 wd.fpos = t.pos;
1302 if (!indexing || index_visible) {
1303 wd.text = ustrdup(utext);
1304 uword = addword(wd, &whptr);
1305 } else
1306 uword = NULL;
1307 if (indexing) {
1308 wd.text = ustrdup(utext);
1309 iword = addword(wd, &idximplicit);
1310 } else
1311 iword = NULL;
1312 dtor(t), t = get_token(in);
1313 if (t.type == tok_lbrace) {
1314 /*
1315 * \u with a left brace. Until the brace
1316 * closes, all further words go on a
1317 * sidetrack from the main thread of the
1318 * paragraph.
1319 */
1320 sitem = mknew(struct stack_item);
1321 sitem->type = stack_ualt;
1322 sitem->whptr = whptr;
1323 sitem->idximplicit = idximplicit;
1324 stk_push(parsestk, sitem);
1325 whptr = uword ? &uword->alt : NULL;
1326 idximplicit = iword ? &iword->alt : NULL;
1327 } else {
1328 if (indexing)
1329 rdadd(&indexstr, uchr);
1330 already = TRUE;
1331 }
1332 break;
1333 default:
1334 if (!macrolookup(macros, in, t.text, &t.pos))
1335 error(err_badmidcmd, t.text, &t.pos);
1336 break;
1337 }
1338 }
1339 if (!already)
1340 dtor(t), t = get_token(in);
1341 seenwhite = iswhite;
1342 }
1343 finished_para:
1344 /* Check the stack is empty */
1345 if (stk_top(parsestk)) {
1346 while ((sitem = stk_pop(parsestk)))
1347 sfree(sitem);
1348 error(err_missingrbrace, &t.pos);
1349 }
1350 stk_free(parsestk);
1351 prev_para_type = par.type;
1352 addpara(par, ret);
1353 if (t.type == tok_eof)
1354 already = TRUE;
1355 }
1356
1357 if (stk_top(crossparastk)) {
1358 void *p;
1359
1360 error(err_missingrbrace2, &t.pos);
1361 while ((p = stk_pop(crossparastk)))
1362 sfree(p);
1363 }
1364
1365 /*
1366 * We break to here rather than returning, because otherwise
1367 * this cleanup doesn't happen.
1368 */
1369 dtor(t);
1370 macrocleanup(macros);
1371
1372 stk_free(crossparastk);
1373 }
1374
1375 paragraph *read_input(input *in, indexdata *idx) {
1376 paragraph *head = NULL;
1377 paragraph **hptr = &head;
1378
1379 while (in->currindex < in->nfiles) {
1380 in->currfp = fopen(in->filenames[in->currindex], "r");
1381 if (in->currfp) {
1382 setpos(in, in->filenames[in->currindex]);
1383 read_file(&hptr, in, idx);
1384 }
1385 in->currindex++;
1386 }
1387
1388 return head;
1389 }