Correct mishandling of paragraphs beginning with "\#{".
[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 = sresize(in->pushback, in->pushbacksize, pushback);
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 = snew(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 = snew(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 static void input_configure(input *in, paragraph *cfg) {
86 assert(cfg->type == para_Config);
87
88 if (!ustricmp(cfg->keyword, L"input-charset")) {
89 in->charset = charset_from_ustr(&cfg->fpos, uadv(cfg->keyword));
90 }
91 }
92
93 /*
94 * Can return EOF
95 */
96 static int get(input *in, filepos *pos, rdstringc *rsc) {
97 int pushbackpt = in->stack ? in->stack->npushback : 0;
98 if (in->npushback > pushbackpt) {
99 --in->npushback;
100 if (pos)
101 *pos = in->pushback[in->npushback].pos; /* structure copy */
102 return in->pushback[in->npushback].chr;
103 }
104 else if (in->stack) {
105 wchar_t c = in->stack->text[in->stack->ptr];
106 if (pos)
107 *pos = in->stack->pos;
108 if (in->stack->text[++in->stack->ptr] == L'\0') {
109 macrostack *tmp = in->stack;
110 in->stack = tmp->next;
111 sfree(tmp);
112 }
113 return c;
114 }
115 else if (in->currfp) {
116
117 while (in->wcpos >= in->nwc) {
118
119 int c = getc(in->currfp);
120
121 if (c == EOF) {
122 fclose(in->currfp);
123 in->currfp = NULL;
124 return EOF;
125 }
126
127 if (rsc)
128 rdaddc(rsc, c);
129
130 /* Track line numbers, for error reporting */
131 if (pos)
132 *pos = in->pos;
133 if (in->reportcols) {
134 switch (c) {
135 case '\t':
136 in->pos.col = 1 + (in->pos.col + TAB_STOP-1) % TAB_STOP;
137 break;
138 case '\n':
139 in->pos.col = 1;
140 in->pos.line++;
141 break;
142 default:
143 in->pos.col++;
144 break;
145 }
146 } else {
147 in->pos.col = -1;
148 if (c == '\n')
149 in->pos.line++;
150 }
151
152 /*
153 * Do input character set translation, so that we return
154 * Unicode.
155 */
156 {
157 char buf[1];
158 char const *p;
159 int inlen;
160
161 buf[0] = (char)c;
162 p = buf;
163 inlen = 1;
164
165 in->nwc = charset_to_unicode(&p, &inlen,
166 in->wc, lenof(in->wc),
167 in->charset, &in->csstate,
168 NULL, 0);
169 assert(p == buf+1 && inlen == 0);
170
171 in->wcpos = 0;
172 }
173 }
174
175 return in->wc[in->wcpos++];
176
177 } else
178 return EOF;
179 }
180
181 /*
182 * Lexical analysis of source files.
183 */
184 typedef struct token_Tag token;
185 struct token_Tag {
186 int type;
187 int cmd, aux;
188 wchar_t *text;
189 char *origtext;
190 filepos pos;
191 };
192 enum {
193 tok_eof, /* end of file */
194 tok_eop, /* end of paragraph */
195 tok_white, /* whitespace */
196 tok_word, /* a word or word fragment */
197 tok_cmd, /* \command */
198 tok_lbrace, /* { */
199 tok_rbrace /* } */
200 };
201
202 /* Halibut command keywords. */
203 enum {
204 c__invalid, /* invalid command */
205 c__comment, /* comment command (\#) */
206 c__escaped, /* escaped character */
207 c__nop, /* no-op */
208 c__nbsp, /* nonbreaking space */
209 c_A, /* appendix heading */
210 c_B, /* bibliography entry */
211 c_BR, /* bibliography rewrite */
212 c_C, /* chapter heading */
213 c_H, /* heading */
214 c_I, /* invisible index mark */
215 c_IM, /* index merge/rewrite */
216 c_K, /* capitalised cross-reference */
217 c_S, /* aux field is 0, 1, 2, ... */
218 c_U, /* unnumbered-chapter heading */
219 c_W, /* Web hyperlink */
220 c_b, /* bulletted list */
221 c_c, /* code */
222 c_cfg, /* configuration directive */
223 c_copyright, /* copyright statement */
224 c_cq, /* quoted code (sugar for \q{\cw{x}}) */
225 c_cw, /* weak code */
226 c_date, /* document processing date */
227 c_dd, /* description list: description */
228 c_define, /* macro definition */
229 c_dt, /* description list: described thing */
230 c_e, /* emphasis */
231 c_i, /* visible index mark */
232 c_ii, /* uncapitalised visible index mark */
233 c_k, /* uncapitalised cross-reference */
234 c_lcont, /* continuation para(s) for list item */
235 c_n, /* numbered list */
236 c_nocite, /* bibliography trickery */
237 c_preamble, /* (obsolete) preamble text */
238 c_q, /* quote marks */
239 c_quote, /* block-quoted paragraphs */
240 c_rule, /* horizontal rule */
241 c_title, /* document title */
242 c_u, /* aux field is char code */
243 c_versionid /* document RCS id */
244 };
245
246 /* Perhaps whitespace should be defined in a more Unicode-friendly way? */
247 #define iswhite(c) ( (c)==32 || (c)==9 || (c)==13 || (c)==10 )
248 #define isnl(c) ( (c)==10 )
249 #define isdec(c) ( ((c)>='0'&&(c)<='9') )
250 #define fromdec(c) ( (c)-'0' )
251 #define ishex(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='F') || ((c)>='a'&&(c)<='f'))
252 #define fromhex(c) ( (c)<='9' ? (c)-'0' : ((c)&0xDF) - ('A'-10) )
253 #define iscmd(c) ( ((c)>='0'&&(c)<='9') || ((c)>='A'&&(c)<='Z') || ((c)>='a'&&(c)<='z'))
254
255 /*
256 * Keyword comparison function. Like strcmp, but between a wchar_t *
257 * and a char *.
258 */
259 static int kwcmp(wchar_t const *p, char const *q) {
260 int i;
261 do {
262 i = *p - *q;
263 } while (*p++ && *q++ && !i);
264 return i;
265 }
266
267 /*
268 * Match a keyword.
269 */
270 static void match_kw(token *tok) {
271 /*
272 * FIXME. The ids are explicit in here so as to allow long-name
273 * equivalents to the various very short keywords.
274 */
275 static const struct { char const *name; int id; } keywords[] = {
276 {"#", c__comment}, /* comment command (\#) */
277 {"-", c__escaped}, /* nonbreaking hyphen */
278 {".", c__nop}, /* no-op */
279 {"A", c_A}, /* appendix heading */
280 {"B", c_B}, /* bibliography entry */
281 {"BR", c_BR}, /* bibliography rewrite */
282 {"C", c_C}, /* chapter heading */
283 {"H", c_H}, /* heading */
284 {"I", c_I}, /* invisible index mark */
285 {"IM", c_IM}, /* index merge/rewrite */
286 {"K", c_K}, /* capitalised cross-reference */
287 {"U", c_U}, /* unnumbered-chapter heading */
288 {"W", c_W}, /* Web hyperlink */
289 {"\\", c__escaped}, /* escaped backslash (\\) */
290 {"_", c__nbsp}, /* nonbreaking space (\_) */
291 {"b", c_b}, /* bulletted list */
292 {"c", c_c}, /* code */
293 {"cfg", c_cfg}, /* configuration directive */
294 {"copyright", c_copyright}, /* copyright statement */
295 {"cq", c_cq}, /* quoted code (sugar for \q{\cw{x}}) */
296 {"cw", c_cw}, /* weak code */
297 {"date", c_date}, /* document processing date */
298 {"dd", c_dd}, /* description list: description */
299 {"define", c_define}, /* macro definition */
300 {"dt", c_dt}, /* description list: described thing */
301 {"e", c_e}, /* emphasis */
302 {"i", c_i}, /* visible index mark */
303 {"ii", c_ii}, /* uncapitalised visible index mark */
304 {"k", c_k}, /* uncapitalised cross-reference */
305 {"lcont", c_lcont}, /* continuation para(s) for list item */
306 {"n", c_n}, /* numbered list */
307 {"nocite", c_nocite}, /* bibliography trickery */
308 {"preamble", c_preamble}, /* (obsolete) preamble text */
309 {"q", c_q}, /* quote marks */
310 {"quote", c_quote}, /* block-quoted paragraphs */
311 {"rule", c_rule}, /* horizontal rule */
312 {"title", c_title}, /* document title */
313 {"versionid", c_versionid}, /* document RCS id */
314 {"{", c__escaped}, /* escaped lbrace (\{) */
315 {"}", c__escaped}, /* escaped rbrace (\}) */
316 };
317 int i, j, k, c;
318
319 /*
320 * Special cases: \S{0,1,2,...} and \uABCD. If the syntax
321 * doesn't match correctly, we just fall through to the
322 * binary-search phase.
323 */
324 if (tok->text[0] == 'S') {
325 /* We expect numeric characters thereafter. */
326 wchar_t *p = tok->text+1;
327 int n;
328 if (!*p)
329 n = 1;
330 else {
331 n = 0;
332 while (*p && isdec(*p)) {
333 n = 10 * n + fromdec(*p);
334 p++;
335 }
336 }
337 if (!*p) {
338 tok->cmd = c_S;
339 tok->aux = n;
340 return;
341 }
342 } else if (tok->text[0] == 'u') {
343 /* We expect hex characters thereafter. */
344 wchar_t *p = tok->text+1;
345 int n = 0;
346 while (*p && ishex(*p)) {
347 n = 16 * n + fromhex(*p);
348 p++;
349 }
350 if (!*p) {
351 tok->cmd = c_u;
352 tok->aux = n;
353 return;
354 }
355 }
356
357 i = -1;
358 j = sizeof(keywords)/sizeof(*keywords);
359 while (j-i > 1) {
360 k = (i+j)/2;
361 c = kwcmp(tok->text, keywords[k].name);
362 if (c < 0)
363 j = k;
364 else if (c > 0)
365 i = k;
366 else /* c == 0 */ {
367 tok->cmd = keywords[k].id;
368 return;
369 }
370 }
371
372 tok->cmd = c__invalid;
373 }
374
375
376 /*
377 * Read a token from the input file, in the normal way (`normal' in
378 * the sense that code paragraphs work a different way).
379 */
380 token get_token(input *in) {
381 int c;
382 int nls;
383 int prevpos;
384 token ret;
385 rdstring rs = { 0, 0, NULL };
386 rdstringc rsc = { 0, 0, NULL };
387 filepos cpos;
388
389 ret.text = NULL; /* default */
390 ret.origtext = NULL; /* default */
391 if (in->pushback_chars) {
392 rdaddsc(&rsc, in->pushback_chars);
393 sfree(in->pushback_chars);
394 in->pushback_chars = NULL;
395 }
396 c = get(in, &cpos, &rsc);
397 ret.pos = cpos;
398 if (iswhite(c)) { /* tok_white or tok_eop */
399 nls = 0;
400 prevpos = 0;
401 do {
402 if (isnl(c))
403 nls++;
404 prevpos = rsc.pos;
405 } while ((c = get(in, &cpos, &rsc)) != EOF && iswhite(c));
406 if (c == EOF) {
407 ret.type = tok_eof;
408 sfree(rsc.text);
409 return ret;
410 }
411 if (rsc.text) {
412 in->pushback_chars = dupstr(rsc.text + prevpos);
413 sfree(rsc.text);
414 }
415 unget(in, c, &cpos);
416 ret.type = (nls > 1 ? tok_eop : tok_white);
417 return ret;
418 } else if (c == EOF) { /* tok_eof */
419 ret.type = tok_eof;
420 sfree(rsc.text);
421 return ret;
422 } else if (c == '\\') { /* tok_cmd */
423 rsc.pos = prevpos = 0;
424 c = get(in, &cpos, &rsc);
425 if (c == '-' || c == '\\' || c == '_' ||
426 c == '#' || c == '{' || c == '}' || c == '.') {
427 /* single-char command */
428 rdadd(&rs, c);
429 prevpos = rsc.pos;
430 } else if (c == 'u') {
431 int len = 0;
432 do {
433 rdadd(&rs, c);
434 len++;
435 prevpos = rsc.pos;
436 c = get(in, &cpos, &rsc);
437 } while (ishex(c) && len < 5);
438 unget(in, c, &cpos);
439 } else if (iscmd(c)) {
440 do {
441 rdadd(&rs, c);
442 prevpos = rsc.pos;
443 c = get(in, &cpos, &rsc);
444 } while (iscmd(c));
445 unget(in, c, &cpos);
446 }
447 /*
448 * Now match the command against the list of available
449 * ones.
450 */
451 ret.type = tok_cmd;
452 ret.text = ustrdup(rs.text);
453 if (rsc.text) {
454 in->pushback_chars = dupstr(rsc.text + prevpos);
455 rsc.text[prevpos] = '\0';
456 ret.origtext = dupstr(rsc.text);
457 } else {
458 ret.origtext = dupstr("");
459 }
460 match_kw(&ret);
461 sfree(rs.text);
462 sfree(rsc.text);
463 return ret;
464 } else if (c == '{') { /* tok_lbrace */
465 ret.type = tok_lbrace;
466 sfree(rsc.text);
467 return ret;
468 } else if (c == '}') { /* tok_rbrace */
469 ret.type = tok_rbrace;
470 sfree(rsc.text);
471 return ret;
472 } else { /* tok_word */
473 /*
474 * Read a word: the longest possible contiguous sequence of
475 * things other than whitespace, backslash, braces and
476 * hyphen. A hyphen terminates the word but is returned as
477 * part of it; everything else is pushed back for the next
478 * token. The `aux' field contains TRUE if the word ends in
479 * a hyphen.
480 */
481 ret.aux = FALSE; /* assumed for now */
482 prevpos = 0;
483 while (1) {
484 if (iswhite(c) || c=='{' || c=='}' || c=='\\' || c==EOF) {
485 /* Put back the character that caused termination */
486 unget(in, c, &cpos);
487 break;
488 } else {
489 rdadd(&rs, c);
490 if (c == '-') {
491 prevpos = rsc.pos;
492 ret.aux = TRUE;
493 break; /* hyphen terminates word */
494 }
495 }
496 prevpos = rsc.pos;
497 c = get(in, &cpos, &rsc);
498 }
499 ret.type = tok_word;
500 ret.text = ustrdup(rs.text);
501 if (rsc.text) {
502 in->pushback_chars = dupstr(rsc.text + prevpos);
503 rsc.text[prevpos] = '\0';
504 ret.origtext = dupstr(rsc.text);
505 } else {
506 ret.origtext = dupstr("");
507 }
508 sfree(rs.text);
509 sfree(rsc.text);
510 return ret;
511 }
512 }
513
514 /*
515 * Determine whether the next input character is an open brace (for
516 * telling code paragraphs from paragraphs which merely start with
517 * code).
518 */
519 int isbrace(input *in) {
520 int c;
521 filepos cpos;
522
523 c = get(in, &cpos, NULL);
524 unget(in, c, &cpos);
525 return (c == '{');
526 }
527
528 /*
529 * Read the rest of a line that starts `\c'. Including nothing at
530 * all (tok_word with empty text).
531 */
532 token get_codepar_token(input *in) {
533 int c;
534 token ret;
535 rdstring rs = { 0, 0, NULL };
536 filepos cpos;
537
538 ret.type = tok_word;
539 ret.origtext = NULL;
540 c = get(in, &cpos, NULL); /* expect (and discard) one space */
541 ret.pos = cpos;
542 if (c == ' ') {
543 c = get(in, &cpos, NULL);
544 ret.pos = cpos;
545 }
546 while (!isnl(c) && c != EOF) {
547 int c2 = c;
548 c = get(in, &cpos, NULL);
549 /* Discard \r just before \n. */
550 if (c2 != 13 || !isnl(c))
551 rdadd(&rs, c2);
552 }
553 unget(in, c, &cpos);
554 ret.text = ustrdup(rs.text);
555 sfree(rs.text);
556 return ret;
557 }
558
559 /*
560 * Adds a new word to a linked list
561 */
562 static word *addword(word newword, word ***hptrptr) {
563 word *mnewword;
564 if (!hptrptr)
565 return NULL;
566 mnewword = snew(word);
567 *mnewword = newword; /* structure copy */
568 mnewword->next = NULL;
569 **hptrptr = mnewword;
570 *hptrptr = &mnewword->next;
571 return mnewword;
572 }
573
574 /*
575 * Adds a new paragraph to a linked list
576 */
577 static paragraph *addpara(paragraph newpara, paragraph ***hptrptr) {
578 paragraph *mnewpara = snew(paragraph);
579 *mnewpara = newpara; /* structure copy */
580 mnewpara->next = NULL;
581 **hptrptr = mnewpara;
582 *hptrptr = &mnewpara->next;
583 return mnewpara;
584 }
585
586 /*
587 * Destructor before token is reassigned; should catch most memory
588 * leaks
589 */
590 #define dtor(t) ( sfree(t.text), sfree(t.origtext) )
591
592 /*
593 * Reads a single file (ie until get() returns EOF)
594 */
595 static void read_file(paragraph ***ret, input *in, indexdata *idx,
596 tree234 *macros) {
597 token t;
598 paragraph par;
599 word wd, **whptr, **idximplicit;
600 wchar_t utext[2], *wdtext;
601 int style, spcstyle;
602 int already;
603 int iswhite, seenwhite;
604 int type;
605 int prev_para_type;
606 struct stack_item {
607 enum {
608 stack_nop = 0, /* do nothing (for error recovery) */
609 stack_ualt = 1, /* \u alternative */
610 stack_style = 2, /* \e, \c, \cw */
611 stack_idx = 4, /* \I, \i, \ii */
612 stack_hyper = 8, /* \W */
613 stack_quote = 16 /* \q */
614 } type;
615 word **whptr; /* to restore from \u alternatives */
616 word **idximplicit; /* to restore from \u alternatives */
617 filepos fpos;
618 int in_code;
619 } *sitem;
620 stack parsestk;
621 struct crossparaitem {
622 int type; /* currently c_lcont, c_quote or -1 */
623 int seen_lcont, seen_quote;
624 };
625 stack crossparastk;
626 word *indexword, *uword, *iword;
627 word *idxwordlist;
628 rdstring indexstr;
629 int index_downcase, index_visible, indexing;
630 const rdstring nullrs = { 0, 0, NULL };
631 wchar_t uchr;
632
633 t.text = NULL;
634 t.origtext = NULL;
635 already = FALSE;
636
637 crossparastk = stk_new();
638
639 /*
640 * Loop on each paragraph.
641 */
642 while (1) {
643 int start_cmd = c__invalid;
644 par.words = NULL;
645 par.keyword = NULL;
646 par.origkeyword = NULL;
647 whptr = &par.words;
648
649 /*
650 * Get a token.
651 */
652 do {
653 if (!already) {
654 dtor(t), t = get_token(in);
655 }
656 already = FALSE;
657 } while (t.type == tok_eop);
658 if (t.type == tok_eof)
659 break;
660
661 /*
662 * Parse code paragraphs separately.
663 */
664 if (t.type == tok_cmd && t.cmd == c_c && !isbrace(in)) {
665 int wtype = word_WeakCode;
666
667 par.type = para_Code;
668 par.fpos = t.pos;
669 while (1) {
670 dtor(t), t = get_codepar_token(in);
671 wd.type = wtype;
672 wd.breaks = FALSE; /* shouldn't need this... */
673 wd.text = ustrdup(t.text);
674 wd.alt = NULL;
675 wd.fpos = t.pos;
676 addword(wd, &whptr);
677 dtor(t), t = get_token(in);
678 if (t.type == tok_white) {
679 /*
680 * The newline after a code-paragraph line
681 */
682 dtor(t), t = get_token(in);
683 }
684 if (t.type == tok_eop || t.type == tok_eof ||
685 t.type == tok_rbrace) { /* might be } terminating \lcont */
686 if (t.type == tok_rbrace)
687 already = TRUE;
688 break;
689 } else if (t.type == tok_cmd && t.cmd == c_c) {
690 wtype = word_WeakCode;
691 } else if (t.type == tok_cmd && t.cmd == c_e &&
692 wtype == word_WeakCode) {
693 wtype = word_Emph;
694 } else {
695 error(err_brokencodepara, &t.pos);
696 prev_para_type = par.type;
697 addpara(par, ret);
698 while (t.type != tok_eop) /* error recovery: */
699 dtor(t), t = get_token(in); /* eat rest of paragraph */
700 goto codeparabroken; /* ick, but such is life */
701 }
702 }
703 prev_para_type = par.type;
704 addpara(par, ret);
705 codeparabroken:
706 continue;
707 }
708
709 /*
710 * Spot the special commands that define a grouping of more
711 * than one paragraph, and also the closing braces that
712 * finish them.
713 */
714 if (t.type == tok_cmd &&
715 (t.cmd == c_lcont || t.cmd == c_quote)) {
716 struct crossparaitem *sitem, *stop;
717 int cmd = t.cmd;
718
719 /*
720 * Expect, and swallow, an open brace.
721 */
722 dtor(t), t = get_token(in);
723 if (t.type != tok_lbrace) {
724 error(err_explbr, &t.pos);
725 continue;
726 }
727
728 /*
729 * Also expect, and swallow, any whitespace after that
730 * (a newline before a code paragraph wouldn't be
731 * surprising).
732 */
733 do {
734 dtor(t), t = get_token(in);
735 } while (t.type == tok_white);
736 already = TRUE;
737
738 if (cmd == c_lcont) {
739 /*
740 * \lcont causes a continuation of a list item into
741 * multiple paragraphs (which may in turn contain
742 * nested lists, code paras etc). Hence, the previous
743 * paragraph must be of a list type.
744 */
745 sitem = snew(struct crossparaitem);
746 stop = (struct crossparaitem *)stk_top(crossparastk);
747 if (stop)
748 *sitem = *stop;
749 else
750 sitem->seen_quote = sitem->seen_lcont = 0;
751
752 if (prev_para_type == para_Bullet ||
753 prev_para_type == para_NumberedList ||
754 prev_para_type == para_Description) {
755 sitem->type = c_lcont;
756 sitem->seen_lcont = 1;
757 par.type = para_LcontPush;
758 prev_para_type = par.type;
759 addpara(par, ret);
760 } else {
761 /*
762 * Push a null item on the cross-para stack so that
763 * when we see the corresponding closing brace we
764 * don't give a cascade error.
765 */
766 sitem->type = -1;
767 error(err_misplacedlcont, &t.pos);
768 }
769 } else {
770 /*
771 * \quote causes a group of paragraphs to be
772 * block-quoted (typically they will be indented a
773 * bit).
774 */
775 sitem = snew(struct crossparaitem);
776 stop = (struct crossparaitem *)stk_top(crossparastk);
777 if (stop)
778 *sitem = *stop;
779 else
780 sitem->seen_quote = sitem->seen_lcont = 0;
781 sitem->type = c_quote;
782 sitem->seen_quote = 1;
783 par.type = para_QuotePush;
784 prev_para_type = par.type;
785 addpara(par, ret);
786 }
787 stk_push(crossparastk, sitem);
788 continue;
789 } else if (t.type == tok_rbrace) {
790 struct crossparaitem *sitem = stk_pop(crossparastk);
791 if (!sitem)
792 error(err_unexbrace, &t.pos);
793 else {
794 switch (sitem->type) {
795 case c_lcont:
796 par.type = para_LcontPop;
797 prev_para_type = par.type;
798 addpara(par, ret);
799 break;
800 case c_quote:
801 par.type = para_QuotePop;
802 prev_para_type = par.type;
803 addpara(par, ret);
804 break;
805 }
806 sfree(sitem);
807 }
808 continue;
809 }
810
811 while (t.type == tok_cmd &&
812 macrolookup(macros, in, t.text, &t.pos)) {
813 dtor(t), t = get_token(in);
814 }
815
816 /*
817 * This token begins a paragraph. See if it's one of the
818 * special commands that define a paragraph type.
819 *
820 * (note that \# is special in a way, and \nocite takes no
821 * text)
822 */
823 par.type = para_Normal;
824 if (t.type == tok_cmd) {
825 int needkw;
826 int is_macro = FALSE;
827
828 par.fpos = t.pos;
829 switch (t.cmd) {
830 default:
831 needkw = -1;
832 break;
833 case c__invalid:
834 error(err_badparatype, t.text, &t.pos);
835 needkw = 4;
836 break;
837 case c__comment:
838 if (isbrace(in)) {
839 needkw = -1;
840 break; /* `\#{': isn't a comment para */
841 }
842 do {
843 dtor(t), t = get_token(in);
844 } while (t.type != tok_eop && t.type != tok_eof);
845 continue; /* next paragraph */
846 /*
847 * `needkw' values:
848 *
849 * 1 -- exactly one keyword
850 * 2 -- at least one keyword
851 * 4 -- any number of keywords including zero
852 * 8 -- at least one keyword and then nothing else
853 * 16 -- nothing at all! no keywords, no body
854 * 32 -- no keywords at all
855 */
856 case c_A: needkw = 2; par.type = para_Appendix; break;
857 case c_B: needkw = 2; par.type = para_Biblio; break;
858 case c_BR: needkw = 1; par.type = para_BR;
859 start_cmd = c_BR; break;
860 case c_C: needkw = 2; par.type = para_Chapter; break;
861 case c_H: needkw = 2; par.type = para_Heading;
862 par.aux = 0;
863 break;
864 case c_IM: needkw = 2; par.type = para_IM;
865 start_cmd = c_IM; break;
866 case c_S: needkw = 2; par.type = para_Subsect;
867 par.aux = t.aux; break;
868 case c_U: needkw = 32; par.type = para_UnnumberedChapter; break;
869 /* For \b and \n the keyword is optional */
870 case c_b: needkw = 4; par.type = para_Bullet; break;
871 case c_dt: needkw = 4; par.type = para_DescribedThing; break;
872 case c_dd: needkw = 4; par.type = para_Description; break;
873 case c_n: needkw = 4; par.type = para_NumberedList; break;
874 case c_cfg: needkw = 8; par.type = para_Config;
875 start_cmd = c_cfg; break;
876 case c_copyright: needkw = 32; par.type = para_Copyright; break;
877 case c_define: is_macro = TRUE; needkw = 1; break;
878 /* For \nocite the keyword is _everything_ */
879 case c_nocite: needkw = 8; par.type = para_NoCite; break;
880 case c_preamble: needkw = 32; par.type = para_Normal; break;
881 case c_rule: needkw = 16; par.type = para_Rule; break;
882 case c_title: needkw = 32; par.type = para_Title; break;
883 case c_versionid: needkw = 32; par.type = para_VersionID; break;
884 }
885
886 if (par.type == para_Chapter ||
887 par.type == para_Heading ||
888 par.type == para_Subsect ||
889 par.type == para_Appendix ||
890 par.type == para_UnnumberedChapter) {
891 struct crossparaitem *sitem = stk_top(crossparastk);
892 if (sitem && (sitem->seen_lcont || sitem->seen_quote)) {
893 error(err_sectmarkerinblock,
894 &t.pos,
895 (sitem->seen_lcont ? "lcont" : "quote"));
896 }
897 }
898
899 if (needkw > 0) {
900 rdstring rs = { 0, 0, NULL };
901 rdstringc rsc = { 0, 0, NULL };
902 int nkeys = 0;
903 filepos fp;
904
905 /* Get keywords. */
906 dtor(t), t = get_token(in);
907 fp = t.pos;
908 while (t.type == tok_lbrace ||
909 (t.type == tok_white && (needkw & 24))) {
910 /*
911 * In paragraph types which can't accept any
912 * body text (such as \cfg), we are lenient
913 * about whitespace between keywords. This is
914 * important for \cfg in particular since it
915 * can often have many keywords which are long
916 * pieces of text, so it's useful to permit the
917 * user to wrap the line between them.
918 */
919 if (t.type == tok_white) {
920 dtor(t), t = get_token(in); /* eat the space */
921 continue;
922 }
923 /* This is a keyword. */
924 nkeys++;
925 /* FIXME: there will be bugs if anyone specifies an
926 * empty keyword (\foo{}), so trap this case. */
927 while (dtor(t), t = get_token(in),
928 t.type == tok_word ||
929 t.type == tok_white ||
930 (t.type == tok_cmd && t.cmd == c__nbsp) ||
931 (t.type == tok_cmd && t.cmd == c__escaped) ||
932 (t.type == tok_cmd && t.cmd == c_u)) {
933 if (t.type == tok_white ||
934 (t.type == tok_cmd && t.cmd == c__nbsp)) {
935 rdadd(&rs, ' ');
936 rdaddc(&rsc, ' ');
937 } else if (t.type == tok_cmd && t.cmd == c_u) {
938 rdadd(&rs, t.aux);
939 rdaddc(&rsc, '\\');
940 rdaddsc(&rsc, t.origtext);
941 } else {
942 rdadds(&rs, t.text);
943 rdaddsc(&rsc, t.origtext);
944 }
945 }
946 if (t.type != tok_rbrace) {
947 error(err_kwunclosed, &t.pos);
948 continue;
949 }
950 rdadd(&rs, 0); /* add string terminator */
951 rdaddc(&rsc, 0); /* add string terminator */
952 dtor(t), t = get_token(in); /* eat right brace */
953 }
954
955 rdadd(&rs, 0); /* add string terminator */
956 rdaddc(&rsc, 0); /* add string terminator */
957
958 /* See whether we have the right number of keywords. */
959 if ((needkw & 48) && nkeys > 0)
960 error(err_kwillegal, &fp);
961 if ((needkw & 11) && nkeys == 0)
962 error(err_kwexpected, &fp);
963 if ((needkw & 5) && nkeys > 1)
964 error(err_kwtoomany, &fp);
965
966 if (is_macro) {
967 /*
968 * Macro definition. Get the rest of the line
969 * as a code-paragraph token, repeatedly until
970 * there's nothing more left of it. Separate
971 * with newlines.
972 */
973 rdstring macrotext = { 0, 0, NULL };
974 while (1) {
975 dtor(t), t = get_codepar_token(in);
976 if (macrotext.pos > 0)
977 rdadd(&macrotext, L'\n');
978 rdadds(&macrotext, t.text);
979 dtor(t), t = get_token(in);
980 if (t.type == tok_eop) break;
981 }
982 macrodef(macros, rs.text, macrotext.text, fp);
983 continue; /* next paragraph */
984 }
985
986 par.keyword = rdtrim(&rs);
987 par.origkeyword = rdtrimc(&rsc);
988
989 /* Move to EOP in case of needkw==8 or 16 (no body) */
990 if (needkw & 24) {
991 /* We allow whitespace even when we expect no para body */
992 while (t.type == tok_white)
993 dtor(t), t = get_token(in);
994 if (t.type != tok_eop && t.type != tok_eof &&
995 (start_cmd == c__invalid ||
996 t.type != tok_cmd || t.cmd != start_cmd)) {
997 error(err_bodyillegal, &t.pos);
998 /* Error recovery: eat the rest of the paragraph */
999 while (t.type != tok_eop && t.type != tok_eof &&
1000 (start_cmd == c__invalid ||
1001 t.type != tok_cmd || t.cmd != start_cmd))
1002 dtor(t), t = get_token(in);
1003 }
1004 if (t.type == tok_cmd)
1005 already = TRUE;/* inhibit get_token at top of loop */
1006 prev_para_type = par.type;
1007 addpara(par, ret);
1008
1009 if (par.type == para_Config) {
1010 input_configure(in, &par);
1011 }
1012 continue; /* next paragraph */
1013 }
1014 }
1015 }
1016
1017 /*
1018 * Now read the actual paragraph, word by word, adding to
1019 * the paragraph list.
1020 *
1021 * Mid-paragraph commands:
1022 *
1023 * \K \k
1024 * \c \cw \cq
1025 * \e
1026 * \i \ii
1027 * \I
1028 * \q
1029 * \u
1030 * \W
1031 * \date
1032 * \\ \{ \}
1033 */
1034 parsestk = stk_new();
1035 style = word_Normal;
1036 spcstyle = word_WhiteSpace;
1037 indexing = FALSE;
1038 seenwhite = TRUE;
1039 while (t.type != tok_eop && t.type != tok_eof) {
1040 iswhite = FALSE;
1041 already = FALSE;
1042
1043 /* Handle implicit paragraph breaks after \IM, \BR etc */
1044 if (start_cmd != c__invalid &&
1045 t.type == tok_cmd && t.cmd == start_cmd) {
1046 already = TRUE; /* inhibit get_token at top of loop */
1047 break;
1048 }
1049
1050 if (t.type == tok_cmd && t.cmd == c__nop) {
1051 dtor(t), t = get_token(in);
1052 continue; /* do nothing! */
1053 }
1054
1055 if (t.type == tok_cmd && t.cmd == c__escaped) {
1056 t.type = tok_word; /* nice and simple */
1057 t.aux = 0; /* even if `\-' - nonbreaking! */
1058 }
1059 if (t.type == tok_cmd && t.cmd == c__nbsp) {
1060 t.type = tok_word; /* nice and simple */
1061 sfree(t.text);
1062 t.text = ustrdup(L" "); /* text is ` ' not `_' */
1063 t.aux = 0; /* (nonbreaking) */
1064 }
1065 switch (t.type) {
1066 case tok_white:
1067 if (whptr == &par.words)
1068 break; /* strip whitespace at start of para */
1069 wd.text = NULL;
1070 wd.type = spcstyle;
1071 wd.alt = NULL;
1072 wd.aux = 0;
1073 wd.fpos = t.pos;
1074 wd.breaks = FALSE;
1075
1076 /*
1077 * Inhibit use of whitespace if it's (probably the
1078 * newline) before a repeat \IM / \BR type
1079 * directive.
1080 */
1081 if (start_cmd != c__invalid) {
1082 dtor(t), t = get_token(in);
1083 already = TRUE;
1084 if (t.type == tok_cmd && t.cmd == start_cmd)
1085 break;
1086 }
1087
1088 if (indexing)
1089 rdadd(&indexstr, ' ');
1090 if (!indexing || index_visible)
1091 addword(wd, &whptr);
1092 if (indexing)
1093 addword(wd, &idximplicit);
1094 iswhite = TRUE;
1095 break;
1096 case tok_word:
1097 if (indexing)
1098 rdadds(&indexstr, t.text);
1099 wd.type = style;
1100 wd.alt = NULL;
1101 wd.aux = 0;
1102 wd.fpos = t.pos;
1103 wd.breaks = t.aux;
1104 if (!indexing || index_visible) {
1105 wd.text = ustrdup(t.text);
1106 addword(wd, &whptr);
1107 }
1108 if (indexing) {
1109 wd.text = ustrdup(t.text);
1110 addword(wd, &idximplicit);
1111 }
1112 break;
1113 case tok_lbrace:
1114 error(err_unexbrace, &t.pos);
1115 /* Error recovery: push nop */
1116 sitem = snew(struct stack_item);
1117 sitem->type = stack_nop;
1118 sitem->fpos = t.pos;
1119 stk_push(parsestk, sitem);
1120 break;
1121 case tok_rbrace:
1122 sitem = stk_pop(parsestk);
1123 if (!sitem) {
1124 /*
1125 * This closing brace could have been an
1126 * indication that the cross-paragraph stack
1127 * wants popping. Accordingly, we treat it here
1128 * as an indication that the paragraph is over.
1129 */
1130 already = TRUE;
1131 goto finished_para;
1132 } else {
1133 if (sitem->type & stack_ualt) {
1134 whptr = sitem->whptr;
1135 idximplicit = sitem->idximplicit;
1136 }
1137 if (sitem->type & stack_style) {
1138 style = word_Normal;
1139 spcstyle = word_WhiteSpace;
1140 }
1141 if (sitem->type & stack_idx) {
1142 indexword->text = ustrdup(indexstr.text);
1143 if (index_downcase) {
1144 word *w;
1145
1146 ustrlow(indexword->text);
1147 ustrlow(indexstr.text);
1148
1149 for (w = idxwordlist; w; w = w->next)
1150 if (w->text)
1151 ustrlow(w->text);
1152 }
1153 indexing = FALSE;
1154 rdadd(&indexstr, L'\0');
1155 index_merge(idx, FALSE, indexstr.text,
1156 idxwordlist, &sitem->fpos);
1157 sfree(indexstr.text);
1158 }
1159 if (sitem->type & stack_hyper) {
1160 wd.text = NULL;
1161 wd.type = word_HyperEnd;
1162 wd.alt = NULL;
1163 wd.aux = 0;
1164 wd.fpos = t.pos;
1165 wd.breaks = FALSE;
1166 if (!indexing || index_visible)
1167 addword(wd, &whptr);
1168 if (indexing)
1169 addword(wd, &idximplicit);
1170 }
1171 if (sitem->type & stack_quote) {
1172 wd.text = NULL;
1173 wd.type = toquotestyle(style);
1174 wd.alt = NULL;
1175 wd.aux = quote_Close;
1176 wd.fpos = t.pos;
1177 wd.breaks = FALSE;
1178 if (!indexing || index_visible)
1179 addword(wd, &whptr);
1180 if (indexing) {
1181 rdadd(&indexstr, L'"');
1182 addword(wd, &idximplicit);
1183 }
1184 }
1185 }
1186 sfree(sitem);
1187 break;
1188 case tok_cmd:
1189 switch (t.cmd) {
1190 case c__comment:
1191 /*
1192 * In-paragraph comment: \#{ balanced braces }
1193 *
1194 * Anything goes here; even tok_eop. We should
1195 * eat whitespace after the close brace _if_
1196 * there was whitespace before the \#.
1197 */
1198 dtor(t), t = get_token(in);
1199 if (t.type != tok_lbrace) {
1200 error(err_explbr, &t.pos);
1201 } else {
1202 int braces = 1;
1203 while (braces > 0) {
1204 dtor(t), t = get_token(in);
1205 if (t.type == tok_lbrace)
1206 braces++;
1207 else if (t.type == tok_rbrace)
1208 braces--;
1209 else if (t.type == tok_eof) {
1210 error(err_commenteof, &t.pos);
1211 break;
1212 }
1213 }
1214 }
1215 if (seenwhite) {
1216 already = TRUE;
1217 dtor(t), t = get_token(in);
1218 if (t.type == tok_white) {
1219 iswhite = TRUE;
1220 already = FALSE;
1221 }
1222 }
1223 break;
1224 case c_q:
1225 case c_cq:
1226 type = t.cmd;
1227 dtor(t), t = get_token(in);
1228 if (t.type != tok_lbrace) {
1229 error(err_explbr, &t.pos);
1230 } else {
1231 /*
1232 * Enforce that \q may not be used anywhere
1233 * within \c. (It shouldn't be necessary
1234 * since the whole point of \c should be
1235 * that the user wants to exercise exact
1236 * control over the glyphs used, and
1237 * forbidding it has the useful effect of
1238 * relieving some backends of having to
1239 * make difficult decisions.)
1240 */
1241 int stype;
1242
1243 if (style != word_Code && style != word_WeakCode) {
1244 wd.text = NULL;
1245 wd.type = toquotestyle(style);
1246 wd.alt = NULL;
1247 wd.aux = quote_Open;
1248 wd.fpos = t.pos;
1249 wd.breaks = FALSE;
1250 if (!indexing || index_visible)
1251 addword(wd, &whptr);
1252 if (indexing) {
1253 rdadd(&indexstr, L'"');
1254 addword(wd, &idximplicit);
1255 }
1256 stype = stack_quote;
1257 } else {
1258 error(err_codequote, &t.pos);
1259 stype = stack_nop;
1260 }
1261 sitem = snew(struct stack_item);
1262 sitem->fpos = t.pos;
1263 sitem->type = stype;
1264 if (type == c_cq) {
1265 if (style != word_Normal) {
1266 error(err_nestedstyles, &t.pos);
1267 } else {
1268 style = word_WeakCode;
1269 spcstyle = tospacestyle(style);
1270 sitem->type |= stack_style;
1271 }
1272 }
1273 stk_push(parsestk, sitem);
1274 }
1275 break;
1276 case c_K:
1277 case c_k:
1278 case c_W:
1279 case c_date:
1280 /*
1281 * Keyword, hyperlink, or \date. We expect a
1282 * left brace, some text, and then a right
1283 * brace. No nesting; no arguments.
1284 */
1285 wd.fpos = t.pos;
1286 wd.breaks = FALSE;
1287 if (t.cmd == c_K)
1288 wd.type = word_UpperXref;
1289 else if (t.cmd == c_k)
1290 wd.type = word_LowerXref;
1291 else if (t.cmd == c_W)
1292 wd.type = word_HyperLink;
1293 else
1294 wd.type = word_Normal;
1295 dtor(t), t = get_token(in);
1296 if (t.type != tok_lbrace) {
1297 if (wd.type == word_Normal) {
1298 time_t thetime = time(NULL);
1299 struct tm *broken = localtime(&thetime);
1300 already = TRUE;
1301 wdtext = ustrftime(NULL, broken);
1302 wd.type = style;
1303 } else {
1304 error(err_explbr, &t.pos);
1305 wdtext = NULL;
1306 }
1307 } else {
1308 rdstring rs = { 0, 0, NULL };
1309 while (dtor(t), t = get_token(in),
1310 t.type == tok_word || t.type == tok_white) {
1311 if (t.type == tok_white)
1312 rdadd(&rs, ' ');
1313 else
1314 rdadds(&rs, t.text);
1315 }
1316 if (wd.type == word_Normal) {
1317 time_t thetime = time(NULL);
1318 struct tm *broken = localtime(&thetime);
1319 wdtext = ustrftime(rs.text, broken);
1320 wd.type = style;
1321 } else {
1322 wdtext = ustrdup(rs.text);
1323 }
1324 sfree(rs.text);
1325 if (t.type != tok_rbrace) {
1326 error(err_kwexprbr, &t.pos);
1327 }
1328 }
1329 wd.alt = NULL;
1330 wd.aux = 0;
1331 if (!indexing || index_visible) {
1332 wd.text = ustrdup(wdtext);
1333 addword(wd, &whptr);
1334 }
1335 if (indexing) {
1336 wd.text = ustrdup(wdtext);
1337 addword(wd, &idximplicit);
1338 }
1339 sfree(wdtext);
1340 if (wd.type == word_HyperLink) {
1341 /*
1342 * Hyperlinks are different: they then
1343 * expect another left brace, to begin
1344 * delimiting the text marked by the link.
1345 */
1346 dtor(t), t = get_token(in);
1347 sitem = snew(struct stack_item);
1348 sitem->fpos = wd.fpos;
1349 sitem->type = stack_hyper;
1350 /*
1351 * Special cases: \W{}\i, \W{}\ii
1352 */
1353 if (t.type == tok_cmd &&
1354 (t.cmd == c_i || t.cmd == c_ii)) {
1355 if (indexing) {
1356 error(err_nestedindex, &t.pos);
1357 } else {
1358 /* Add an index-reference word with no
1359 * text as yet */
1360 wd.type = word_IndexRef;
1361 wd.text = NULL;
1362 wd.alt = NULL;
1363 wd.aux = 0;
1364 wd.breaks = FALSE;
1365 indexword = addword(wd, &whptr);
1366 /* Set up a rdstring to read the
1367 * index text */
1368 indexstr = nullrs;
1369 /* Flags so that we do the Right
1370 * Things with text */
1371 index_visible = (type != c_I);
1372 index_downcase = (type == c_ii);
1373 indexing = TRUE;
1374 idxwordlist = NULL;
1375 idximplicit = &idxwordlist;
1376
1377 sitem->type |= stack_idx;
1378 }
1379 dtor(t), t = get_token(in);
1380 }
1381 /*
1382 * Special cases: \W{}\c, \W{}\e, \W{}\cw
1383 */
1384 if (t.type == tok_cmd &&
1385 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1386 if (style != word_Normal)
1387 error(err_nestedstyles, &t.pos);
1388 else {
1389 style = (t.cmd == c_c ? word_Code :
1390 t.cmd == c_cw ? word_WeakCode :
1391 word_Emph);
1392 spcstyle = tospacestyle(style);
1393 sitem->type |= stack_style;
1394 }
1395 dtor(t), t = get_token(in);
1396 }
1397 if (t.type != tok_lbrace) {
1398 error(err_explbr, &t.pos);
1399 sfree(sitem);
1400 } else {
1401 stk_push(parsestk, sitem);
1402 }
1403 }
1404 break;
1405 case c_c:
1406 case c_cw:
1407 case c_e:
1408 type = t.cmd;
1409 if (style != word_Normal) {
1410 error(err_nestedstyles, &t.pos);
1411 /* Error recovery: eat lbrace, push nop. */
1412 dtor(t), t = get_token(in);
1413 sitem = snew(struct stack_item);
1414 sitem->fpos = t.pos;
1415 sitem->type = stack_nop;
1416 stk_push(parsestk, sitem);
1417 }
1418 dtor(t), t = get_token(in);
1419 if (t.type != tok_lbrace) {
1420 error(err_explbr, &t.pos);
1421 } else {
1422 style = (type == c_c ? word_Code :
1423 type == c_cw ? word_WeakCode :
1424 word_Emph);
1425 spcstyle = tospacestyle(style);
1426 sitem = snew(struct stack_item);
1427 sitem->fpos = t.pos;
1428 sitem->type = stack_style;
1429 stk_push(parsestk, sitem);
1430 }
1431 break;
1432 case c_i:
1433 case c_ii:
1434 case c_I:
1435 type = t.cmd;
1436 if (indexing) {
1437 error(err_nestedindex, &t.pos);
1438 /* Error recovery: eat lbrace, push nop. */
1439 dtor(t), t = get_token(in);
1440 sitem = snew(struct stack_item);
1441 sitem->fpos = t.pos;
1442 sitem->type = stack_nop;
1443 stk_push(parsestk, sitem);
1444 }
1445 sitem = snew(struct stack_item);
1446 sitem->fpos = t.pos;
1447 sitem->type = stack_idx;
1448 dtor(t), t = get_token(in);
1449 /*
1450 * Special cases: \i\c, \i\e, \i\cw
1451 */
1452 wd.fpos = t.pos;
1453 if (t.type == tok_cmd &&
1454 (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
1455 if (style != word_Normal)
1456 error(err_nestedstyles, &t.pos);
1457 else {
1458 style = (t.cmd == c_c ? word_Code :
1459 t.cmd == c_cw ? word_WeakCode :
1460 word_Emph);
1461 spcstyle = tospacestyle(style);
1462 sitem->type |= stack_style;
1463 }
1464 dtor(t), t = get_token(in);
1465 }
1466 if (t.type != tok_lbrace) {
1467 sfree(sitem);
1468 error(err_explbr, &t.pos);
1469 } else {
1470 /* Add an index-reference word with no text as yet */
1471 wd.type = word_IndexRef;
1472 wd.text = NULL;
1473 wd.alt = NULL;
1474 wd.aux = 0;
1475 wd.breaks = FALSE;
1476 indexword = addword(wd, &whptr);
1477 /* Set up a rdstring to read the index text */
1478 indexstr = nullrs;
1479 /* Flags so that we do the Right Things with text */
1480 index_visible = (type != c_I);
1481 index_downcase = (type == c_ii);
1482 indexing = TRUE;
1483 idxwordlist = NULL;
1484 idximplicit = &idxwordlist;
1485 /* Stack item to close the indexing on exit */
1486 stk_push(parsestk, sitem);
1487 }
1488 break;
1489 case c_u:
1490 uchr = t.aux;
1491 utext[0] = uchr; utext[1] = 0;
1492 wd.type = style;
1493 wd.breaks = FALSE;
1494 wd.alt = NULL;
1495 wd.aux = 0;
1496 wd.fpos = t.pos;
1497 if (!indexing || index_visible) {
1498 wd.text = ustrdup(utext);
1499 uword = addword(wd, &whptr);
1500 } else
1501 uword = NULL;
1502 if (indexing) {
1503 wd.text = ustrdup(utext);
1504 iword = addword(wd, &idximplicit);
1505 } else
1506 iword = NULL;
1507 dtor(t), t = get_token(in);
1508 if (t.type == tok_lbrace) {
1509 /*
1510 * \u with a left brace. Until the brace
1511 * closes, all further words go on a
1512 * sidetrack from the main thread of the
1513 * paragraph.
1514 */
1515 sitem = snew(struct stack_item);
1516 sitem->fpos = t.pos;
1517 sitem->type = stack_ualt;
1518 sitem->whptr = whptr;
1519 sitem->idximplicit = idximplicit;
1520 stk_push(parsestk, sitem);
1521 whptr = uword ? &uword->alt : NULL;
1522 idximplicit = iword ? &iword->alt : NULL;
1523 } else {
1524 if (indexing)
1525 rdadd(&indexstr, uchr);
1526 already = TRUE;
1527 }
1528 break;
1529 default:
1530 if (!macrolookup(macros, in, t.text, &t.pos))
1531 error(err_badmidcmd, t.text, &t.pos);
1532 break;
1533 }
1534 }
1535 if (!already)
1536 dtor(t), t = get_token(in);
1537 seenwhite = iswhite;
1538 }
1539 finished_para:
1540 /* Check the stack is empty */
1541 if (stk_top(parsestk)) {
1542 while ((sitem = stk_pop(parsestk)))
1543 sfree(sitem);
1544 error(err_missingrbrace, &t.pos);
1545 }
1546 stk_free(parsestk);
1547 prev_para_type = par.type;
1548 /*
1549 * Before we add the paragraph to the output list, we
1550 * should check that there was any text in it at all; there
1551 * might not be if (for example) the paragraph contained
1552 * nothing but an unrecognised command sequence, and if we
1553 * put an empty paragraph on the list it may confuse the
1554 * back ends later on.
1555 */
1556 if (par.words) {
1557 addpara(par, ret);
1558 }
1559 if (t.type == tok_eof)
1560 already = TRUE;
1561 }
1562
1563 if (stk_top(crossparastk)) {
1564 void *p;
1565
1566 error(err_missingrbrace2, &t.pos);
1567 while ((p = stk_pop(crossparastk)))
1568 sfree(p);
1569 }
1570
1571 /*
1572 * We break to here rather than returning, because otherwise
1573 * this cleanup doesn't happen.
1574 */
1575 dtor(t);
1576
1577 stk_free(crossparastk);
1578 }
1579
1580 struct {
1581 char const *magic;
1582 size_t nmagic;
1583 void (*reader)(input *);
1584 } magics[] = {
1585 { "%!FontType1-", 12, &read_pfa_file },
1586 { "%!PS-AdobeFont-", 15, &read_pfa_file },
1587 { "\x80\x01", 2, &read_pfb_file },
1588 { "StartFontMetrics", 16, &read_afm_file },
1589 { "\x00\x01\x00\x00", 4, &read_sfnt_file },
1590 { "true", 4, &read_sfnt_file },
1591 };
1592
1593 paragraph *read_input(input *in, indexdata *idx) {
1594 paragraph *head = NULL;
1595 paragraph **hptr = &head;
1596 tree234 *macros;
1597 char mag[16];
1598 size_t len, i;
1599 void (*reader)(input *);
1600
1601 macros = newtree234(macrocmp);
1602
1603 while (in->currindex < in->nfiles) {
1604 in->currfp = fopen(in->filenames[in->currindex], "r");
1605 if (in->currfp) {
1606 setpos(in, in->filenames[in->currindex]);
1607 in->charset = in->defcharset;
1608 in->csstate = charset_init_state;
1609 in->wcpos = in->nwc = 0;
1610 in->pushback_chars = NULL;
1611 reader = NULL;
1612 len = fread(mag, 1, sizeof(mag), in->currfp);
1613 for (i = 0; i < lenof(magics); i++) {
1614 if (len >= magics[i].nmagic &&
1615 memcmp(mag, magics[i].magic, magics[i].nmagic) == 0) {
1616 reader = magics[i].reader;
1617 break;
1618 }
1619 }
1620 rewind(in->currfp);
1621 if (reader == NULL)
1622 read_file(&hptr, in, idx, macros);
1623 else
1624 (*reader)(in);
1625 }
1626 in->currindex++;
1627 }
1628
1629 macrocleanup(macros);
1630
1631 return head;
1632 }