d7482997 |
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; |
f1530049 |
21 | in->pushback = sresize(in->pushback, in->pushbacksize, pushback); |
d7482997 |
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) { |
f1530049 |
48 | macro *m = snew(macro); |
d7482997 |
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) { |
f1530049 |
63 | macrostack *expansion = snew(macrostack); |
d7482997 |
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 | |
e34ba5c3 |
85 | static void input_configure(input *in, paragraph *cfg) { |
86 | assert(cfg->type == para_Config); |
87 | |
88 | if (!ustricmp(cfg->keyword, L"input-charset")) { |
0960a3d8 |
89 | in->charset = charset_from_ustr(&cfg->fpos, uadv(cfg->keyword)); |
e34ba5c3 |
90 | } |
91 | } |
92 | |
d7482997 |
93 | /* |
94 | * Can return EOF |
95 | */ |
e4ea58f8 |
96 | static int get(input *in, filepos *pos, rdstringc *rsc) { |
d7482997 |
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]; |
086907e6 |
106 | if (pos) |
107 | *pos = in->stack->pos; |
d7482997 |
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) { |
d7482997 |
116 | |
e34ba5c3 |
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 | } |
e4ea58f8 |
126 | |
127 | if (rsc) |
128 | rdaddc(rsc, c); |
129 | |
e34ba5c3 |
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; |
d7482997 |
172 | } |
d7482997 |
173 | } |
e34ba5c3 |
174 | |
175 | return in->wc[in->wcpos++]; |
176 | |
d7482997 |
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; |
e4ea58f8 |
189 | char *origtext; |
d7482997 |
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 */ |
672e4626 |
207 | c__nop, /* no-op */ |
d7482997 |
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 */ |
b7747050 |
224 | c_cq, /* quoted code (sugar for \q{\cw{x}}) */ |
d7482997 |
225 | c_cw, /* weak code */ |
226 | c_date, /* document processing date */ |
7136a6c7 |
227 | c_dd, /* description list: description */ |
d7482997 |
228 | c_define, /* macro definition */ |
7136a6c7 |
229 | c_dt, /* description list: described thing */ |
d7482997 |
230 | c_e, /* emphasis */ |
231 | c_i, /* visible index mark */ |
232 | c_ii, /* uncapitalised visible index mark */ |
233 | c_k, /* uncapitalised cross-reference */ |
7136a6c7 |
234 | c_lcont, /* continuation para(s) for list item */ |
d7482997 |
235 | c_n, /* numbered list */ |
236 | c_nocite, /* bibliography trickery */ |
8902e0ed |
237 | c_preamble, /* (obsolete) preamble text */ |
d7482997 |
238 | c_q, /* quote marks */ |
2614b01d |
239 | c_quote, /* block-quoted paragraphs */ |
d7482997 |
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 */ |
672e4626 |
278 | {".", c__nop}, /* no-op */ |
d7482997 |
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 */ |
b7747050 |
295 | {"cq", c_cq}, /* quoted code (sugar for \q{\cw{x}}) */ |
d7482997 |
296 | {"cw", c_cw}, /* weak code */ |
297 | {"date", c_date}, /* document processing date */ |
7136a6c7 |
298 | {"dd", c_dd}, /* description list: description */ |
d7482997 |
299 | {"define", c_define}, /* macro definition */ |
7136a6c7 |
300 | {"dt", c_dt}, /* description list: described thing */ |
d7482997 |
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 */ |
7136a6c7 |
305 | {"lcont", c_lcont}, /* continuation para(s) for list item */ |
d7482997 |
306 | {"n", c_n}, /* numbered list */ |
307 | {"nocite", c_nocite}, /* bibliography trickery */ |
8902e0ed |
308 | {"preamble", c_preamble}, /* (obsolete) preamble text */ |
d7482997 |
309 | {"q", c_q}, /* quote marks */ |
2614b01d |
310 | {"quote", c_quote}, /* block-quoted paragraphs */ |
d7482997 |
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; |
e4ea58f8 |
383 | int prevpos; |
d7482997 |
384 | token ret; |
385 | rdstring rs = { 0, 0, NULL }; |
e4ea58f8 |
386 | rdstringc rsc = { 0, 0, NULL }; |
d7482997 |
387 | filepos cpos; |
388 | |
389 | ret.text = NULL; /* default */ |
e4ea58f8 |
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); |
d7482997 |
397 | ret.pos = cpos; |
398 | if (iswhite(c)) { /* tok_white or tok_eop */ |
399 | nls = 0; |
e4ea58f8 |
400 | prevpos = 0; |
d7482997 |
401 | do { |
402 | if (isnl(c)) |
403 | nls++; |
e4ea58f8 |
404 | prevpos = rsc.pos; |
405 | } while ((c = get(in, &cpos, &rsc)) != EOF && iswhite(c)); |
d7482997 |
406 | if (c == EOF) { |
407 | ret.type = tok_eof; |
e4ea58f8 |
408 | sfree(rsc.text); |
d7482997 |
409 | return ret; |
410 | } |
e4ea58f8 |
411 | if (rsc.text) { |
412 | in->pushback_chars = dupstr(rsc.text + prevpos); |
413 | sfree(rsc.text); |
414 | } |
d7482997 |
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; |
e4ea58f8 |
420 | sfree(rsc.text); |
d7482997 |
421 | return ret; |
422 | } else if (c == '\\') { /* tok_cmd */ |
e4ea58f8 |
423 | rsc.pos = prevpos = 0; |
424 | c = get(in, &cpos, &rsc); |
d7482997 |
425 | if (c == '-' || c == '\\' || c == '_' || |
672e4626 |
426 | c == '#' || c == '{' || c == '}' || c == '.') { |
d7482997 |
427 | /* single-char command */ |
428 | rdadd(&rs, c); |
429 | } else if (c == 'u') { |
430 | int len = 0; |
431 | do { |
432 | rdadd(&rs, c); |
433 | len++; |
e4ea58f8 |
434 | prevpos = rsc.pos; |
435 | c = get(in, &cpos, &rsc); |
d7482997 |
436 | } while (ishex(c) && len < 5); |
437 | unget(in, c, &cpos); |
438 | } else if (iscmd(c)) { |
439 | do { |
440 | rdadd(&rs, c); |
e4ea58f8 |
441 | prevpos = rsc.pos; |
442 | c = get(in, &cpos, &rsc); |
d7482997 |
443 | } while (iscmd(c)); |
444 | unget(in, c, &cpos); |
445 | } |
446 | /* |
447 | * Now match the command against the list of available |
448 | * ones. |
449 | */ |
450 | ret.type = tok_cmd; |
451 | ret.text = ustrdup(rs.text); |
e4ea58f8 |
452 | if (rsc.text) { |
453 | in->pushback_chars = dupstr(rsc.text + prevpos); |
454 | rsc.text[prevpos] = '\0'; |
455 | ret.origtext = dupstr(rsc.text); |
456 | } else { |
457 | ret.origtext = dupstr(""); |
458 | } |
d7482997 |
459 | match_kw(&ret); |
460 | sfree(rs.text); |
e4ea58f8 |
461 | sfree(rsc.text); |
d7482997 |
462 | return ret; |
463 | } else if (c == '{') { /* tok_lbrace */ |
464 | ret.type = tok_lbrace; |
e4ea58f8 |
465 | sfree(rsc.text); |
d7482997 |
466 | return ret; |
467 | } else if (c == '}') { /* tok_rbrace */ |
468 | ret.type = tok_rbrace; |
e4ea58f8 |
469 | sfree(rsc.text); |
d7482997 |
470 | return ret; |
471 | } else { /* tok_word */ |
472 | /* |
473 | * Read a word: the longest possible contiguous sequence of |
474 | * things other than whitespace, backslash, braces and |
475 | * hyphen. A hyphen terminates the word but is returned as |
476 | * part of it; everything else is pushed back for the next |
477 | * token. The `aux' field contains TRUE if the word ends in |
478 | * a hyphen. |
479 | */ |
480 | ret.aux = FALSE; /* assumed for now */ |
e4ea58f8 |
481 | prevpos = 0; |
d7482997 |
482 | while (1) { |
483 | if (iswhite(c) || c=='{' || c=='}' || c=='\\' || c==EOF) { |
484 | /* Put back the character that caused termination */ |
485 | unget(in, c, &cpos); |
486 | break; |
487 | } else { |
488 | rdadd(&rs, c); |
489 | if (c == '-') { |
e4ea58f8 |
490 | prevpos = rsc.pos; |
d7482997 |
491 | ret.aux = TRUE; |
492 | break; /* hyphen terminates word */ |
493 | } |
494 | } |
e4ea58f8 |
495 | prevpos = rsc.pos; |
496 | c = get(in, &cpos, &rsc); |
d7482997 |
497 | } |
498 | ret.type = tok_word; |
499 | ret.text = ustrdup(rs.text); |
e4ea58f8 |
500 | if (rsc.text) { |
501 | in->pushback_chars = dupstr(rsc.text + prevpos); |
502 | rsc.text[prevpos] = '\0'; |
503 | ret.origtext = dupstr(rsc.text); |
504 | } else { |
505 | ret.origtext = dupstr(""); |
506 | } |
d7482997 |
507 | sfree(rs.text); |
e4ea58f8 |
508 | sfree(rsc.text); |
d7482997 |
509 | return ret; |
510 | } |
511 | } |
512 | |
513 | /* |
514 | * Determine whether the next input character is an open brace (for |
515 | * telling code paragraphs from paragraphs which merely start with |
516 | * code). |
517 | */ |
518 | int isbrace(input *in) { |
519 | int c; |
520 | filepos cpos; |
521 | |
e4ea58f8 |
522 | c = get(in, &cpos, NULL); |
d7482997 |
523 | unget(in, c, &cpos); |
524 | return (c == '{'); |
525 | } |
526 | |
527 | /* |
528 | * Read the rest of a line that starts `\c'. Including nothing at |
529 | * all (tok_word with empty text). |
530 | */ |
531 | token get_codepar_token(input *in) { |
532 | int c; |
533 | token ret; |
534 | rdstring rs = { 0, 0, NULL }; |
535 | filepos cpos; |
536 | |
537 | ret.type = tok_word; |
e4ea58f8 |
538 | ret.origtext = NULL; |
539 | c = get(in, &cpos, NULL); /* expect (and discard) one space */ |
d7482997 |
540 | ret.pos = cpos; |
541 | if (c == ' ') { |
e4ea58f8 |
542 | c = get(in, &cpos, NULL); |
d7482997 |
543 | ret.pos = cpos; |
544 | } |
545 | while (!isnl(c) && c != EOF) { |
546 | int c2 = c; |
e4ea58f8 |
547 | c = get(in, &cpos, NULL); |
d7482997 |
548 | /* Discard \r just before \n. */ |
549 | if (c2 != 13 || !isnl(c)) |
550 | rdadd(&rs, c2); |
551 | } |
552 | unget(in, c, &cpos); |
553 | ret.text = ustrdup(rs.text); |
554 | sfree(rs.text); |
555 | return ret; |
556 | } |
557 | |
558 | /* |
559 | * Adds a new word to a linked list |
560 | */ |
561 | static word *addword(word newword, word ***hptrptr) { |
562 | word *mnewword; |
563 | if (!hptrptr) |
564 | return NULL; |
f1530049 |
565 | mnewword = snew(word); |
d7482997 |
566 | *mnewword = newword; /* structure copy */ |
567 | mnewword->next = NULL; |
568 | **hptrptr = mnewword; |
569 | *hptrptr = &mnewword->next; |
570 | return mnewword; |
571 | } |
572 | |
573 | /* |
574 | * Adds a new paragraph to a linked list |
575 | */ |
576 | static paragraph *addpara(paragraph newpara, paragraph ***hptrptr) { |
f1530049 |
577 | paragraph *mnewpara = snew(paragraph); |
d7482997 |
578 | *mnewpara = newpara; /* structure copy */ |
579 | mnewpara->next = NULL; |
580 | **hptrptr = mnewpara; |
581 | *hptrptr = &mnewpara->next; |
582 | return mnewpara; |
583 | } |
584 | |
585 | /* |
586 | * Destructor before token is reassigned; should catch most memory |
587 | * leaks |
588 | */ |
e4ea58f8 |
589 | #define dtor(t) ( sfree(t.text), sfree(t.origtext) ) |
d7482997 |
590 | |
591 | /* |
592 | * Reads a single file (ie until get() returns EOF) |
593 | */ |
594 | static void read_file(paragraph ***ret, input *in, indexdata *idx) { |
595 | token t; |
596 | paragraph par; |
597 | word wd, **whptr, **idximplicit; |
598 | tree234 *macros; |
599 | wchar_t utext[2], *wdtext; |
600 | int style, spcstyle; |
601 | int already; |
602 | int iswhite, seenwhite; |
603 | int type; |
7136a6c7 |
604 | int prev_para_type; |
d7482997 |
605 | struct stack_item { |
606 | enum { |
607 | stack_nop = 0, /* do nothing (for error recovery) */ |
608 | stack_ualt = 1, /* \u alternative */ |
609 | stack_style = 2, /* \e, \c, \cw */ |
610 | stack_idx = 4, /* \I, \i, \ii */ |
611 | stack_hyper = 8, /* \W */ |
612 | stack_quote = 16, /* \q */ |
613 | } type; |
614 | word **whptr; /* to restore from \u alternatives */ |
615 | word **idximplicit; /* to restore from \u alternatives */ |
f4551933 |
616 | filepos fpos; |
6ff15f2b |
617 | int in_code; |
d7482997 |
618 | } *sitem; |
619 | stack parsestk; |
7136a6c7 |
620 | struct crossparaitem { |
2614b01d |
621 | int type; /* currently c_lcont, c_quote or -1 */ |
622 | int seen_lcont, seen_quote; |
7136a6c7 |
623 | }; |
624 | stack crossparastk; |
d7482997 |
625 | word *indexword, *uword, *iword; |
626 | word *idxwordlist; |
627 | rdstring indexstr; |
628 | int index_downcase, index_visible, indexing; |
629 | const rdstring nullrs = { 0, 0, NULL }; |
630 | wchar_t uchr; |
631 | |
632 | t.text = NULL; |
e4ea58f8 |
633 | t.origtext = NULL; |
d7482997 |
634 | macros = newtree234(macrocmp); |
b3c80b25 |
635 | already = FALSE; |
d7482997 |
636 | |
7136a6c7 |
637 | crossparastk = stk_new(); |
638 | |
d7482997 |
639 | /* |
640 | * Loop on each paragraph. |
641 | */ |
642 | while (1) { |
b3c80b25 |
643 | int start_cmd = c__invalid; |
d7482997 |
644 | par.words = NULL; |
645 | par.keyword = NULL; |
e4ea58f8 |
646 | par.origkeyword = NULL; |
d7482997 |
647 | whptr = &par.words; |
648 | |
649 | /* |
650 | * Get a token. |
651 | */ |
7136a6c7 |
652 | do { |
653 | if (!already) { |
654 | dtor(t), t = get_token(in); |
655 | } |
656 | already = FALSE; |
657 | } while (t.type == tok_eop); |
d7482997 |
658 | if (t.type == tok_eof) |
677e18a2 |
659 | break; |
d7482997 |
660 | |
661 | /* |
662 | * Parse code paragraphs separately. |
663 | */ |
664 | if (t.type == tok_cmd && t.cmd == c_c && !isbrace(in)) { |
4b3c5afb |
665 | int wtype = word_WeakCode; |
666 | |
d7482997 |
667 | par.type = para_Code; |
668 | par.fpos = t.pos; |
669 | while (1) { |
670 | dtor(t), t = get_codepar_token(in); |
4b3c5afb |
671 | wd.type = wtype; |
d7482997 |
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 | } |
780f45d4 |
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; |
d7482997 |
688 | break; |
780f45d4 |
689 | } else if (t.type == tok_cmd && t.cmd == c_c) { |
4b3c5afb |
690 | wtype = word_WeakCode; |
780f45d4 |
691 | } else if (t.type == tok_cmd && t.cmd == c_e && |
692 | wtype == word_WeakCode) { |
4b3c5afb |
693 | wtype = word_Emph; |
780f45d4 |
694 | } else { |
d7482997 |
695 | error(err_brokencodepara, &t.pos); |
7136a6c7 |
696 | prev_para_type = par.type; |
d7482997 |
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 | } |
7136a6c7 |
703 | prev_para_type = par.type; |
d7482997 |
704 | addpara(par, ret); |
705 | codeparabroken: |
706 | continue; |
707 | } |
708 | |
709 | /* |
7136a6c7 |
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 && |
2614b01d |
715 | (t.cmd == c_lcont || t.cmd == c_quote)) { |
7136a6c7 |
716 | struct crossparaitem *sitem, *stop; |
2614b01d |
717 | int cmd = t.cmd; |
7136a6c7 |
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 | |
780f45d4 |
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 | |
2614b01d |
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 | */ |
f1530049 |
745 | sitem = snew(struct crossparaitem); |
2614b01d |
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 | } |
7136a6c7 |
769 | } else { |
770 | /* |
2614b01d |
771 | * \quote causes a group of paragraphs to be |
772 | * block-quoted (typically they will be indented a |
773 | * bit). |
7136a6c7 |
774 | */ |
f1530049 |
775 | sitem = snew(struct crossparaitem); |
2614b01d |
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); |
7136a6c7 |
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; |
2614b01d |
800 | case c_quote: |
801 | par.type = para_QuotePop; |
802 | prev_para_type = par.type; |
803 | addpara(par, ret); |
804 | break; |
7136a6c7 |
805 | } |
806 | sfree(sitem); |
807 | } |
808 | continue; |
809 | } |
810 | |
811 | /* |
d7482997 |
812 | * This token begins a paragraph. See if it's one of the |
813 | * special commands that define a paragraph type. |
814 | * |
815 | * (note that \# is special in a way, and \nocite takes no |
816 | * text) |
817 | */ |
818 | par.type = para_Normal; |
819 | if (t.type == tok_cmd) { |
820 | int needkw; |
821 | int is_macro = FALSE; |
822 | |
823 | par.fpos = t.pos; |
824 | switch (t.cmd) { |
825 | default: |
826 | needkw = -1; |
827 | break; |
828 | case c__invalid: |
829 | error(err_badparatype, t.text, &t.pos); |
830 | needkw = 4; |
831 | break; |
832 | case c__comment: |
833 | if (isbrace(in)) |
834 | break; /* `\#{': isn't a comment para */ |
835 | do { |
836 | dtor(t), t = get_token(in); |
837 | } while (t.type != tok_eop && t.type != tok_eof); |
838 | continue; /* next paragraph */ |
839 | /* |
840 | * `needkw' values: |
841 | * |
842 | * 1 -- exactly one keyword |
843 | * 2 -- at least one keyword |
844 | * 4 -- any number of keywords including zero |
845 | * 8 -- at least one keyword and then nothing else |
846 | * 16 -- nothing at all! no keywords, no body |
847 | * 32 -- no keywords at all |
848 | */ |
849 | case c_A: needkw = 2; par.type = para_Appendix; break; |
850 | case c_B: needkw = 2; par.type = para_Biblio; break; |
b3c80b25 |
851 | case c_BR: needkw = 1; par.type = para_BR; |
852 | start_cmd = c_BR; break; |
d7482997 |
853 | case c_C: needkw = 2; par.type = para_Chapter; break; |
854 | case c_H: needkw = 2; par.type = para_Heading; |
855 | par.aux = 0; |
856 | break; |
b3c80b25 |
857 | case c_IM: needkw = 2; par.type = para_IM; |
858 | start_cmd = c_IM; break; |
d7482997 |
859 | case c_S: needkw = 2; par.type = para_Subsect; |
860 | par.aux = t.aux; break; |
861 | case c_U: needkw = 32; par.type = para_UnnumberedChapter; break; |
862 | /* For \b and \n the keyword is optional */ |
863 | case c_b: needkw = 4; par.type = para_Bullet; break; |
7136a6c7 |
864 | case c_dt: needkw = 4; par.type = para_DescribedThing; break; |
865 | case c_dd: needkw = 4; par.type = para_Description; break; |
d7482997 |
866 | case c_n: needkw = 4; par.type = para_NumberedList; break; |
b3c80b25 |
867 | case c_cfg: needkw = 8; par.type = para_Config; |
868 | start_cmd = c_cfg; break; |
d7482997 |
869 | case c_copyright: needkw = 32; par.type = para_Copyright; break; |
870 | case c_define: is_macro = TRUE; needkw = 1; break; |
871 | /* For \nocite the keyword is _everything_ */ |
872 | case c_nocite: needkw = 8; par.type = para_NoCite; break; |
8902e0ed |
873 | case c_preamble: needkw = 32; par.type = para_Normal; break; |
d7482997 |
874 | case c_rule: needkw = 16; par.type = para_Rule; break; |
875 | case c_title: needkw = 32; par.type = para_Title; break; |
876 | case c_versionid: needkw = 32; par.type = para_VersionID; break; |
877 | } |
878 | |
7136a6c7 |
879 | if (par.type == para_Chapter || |
880 | par.type == para_Heading || |
881 | par.type == para_Subsect || |
882 | par.type == para_Appendix || |
883 | par.type == para_UnnumberedChapter) { |
884 | struct crossparaitem *sitem = stk_top(crossparastk); |
2614b01d |
885 | if (sitem && (sitem->seen_lcont || sitem->seen_quote)) { |
886 | error(err_sectmarkerinblock, |
887 | &t.pos, |
888 | (sitem->seen_lcont ? "lcont" : "quote")); |
7136a6c7 |
889 | } |
890 | } |
891 | |
d7482997 |
892 | if (needkw > 0) { |
893 | rdstring rs = { 0, 0, NULL }; |
e4ea58f8 |
894 | rdstringc rsc = { 0, 0, NULL }; |
d7482997 |
895 | int nkeys = 0; |
896 | filepos fp; |
897 | |
898 | /* Get keywords. */ |
899 | dtor(t), t = get_token(in); |
900 | fp = t.pos; |
901 | while (t.type == tok_lbrace) { |
902 | /* This is a keyword. */ |
903 | nkeys++; |
904 | /* FIXME: there will be bugs if anyone specifies an |
905 | * empty keyword (\foo{}), so trap this case. */ |
906 | while (dtor(t), t = get_token(in), |
907 | t.type == tok_word || |
908 | t.type == tok_white || |
909 | (t.type == tok_cmd && t.cmd == c__nbsp) || |
db662ca1 |
910 | (t.type == tok_cmd && t.cmd == c__escaped) || |
911 | (t.type == tok_cmd && t.cmd == c_u)) { |
d7482997 |
912 | if (t.type == tok_white || |
e4ea58f8 |
913 | (t.type == tok_cmd && t.cmd == c__nbsp)) { |
d7482997 |
914 | rdadd(&rs, ' '); |
e4ea58f8 |
915 | rdaddc(&rsc, ' '); |
db662ca1 |
916 | } else if (t.type == tok_cmd && t.cmd == c_u) { |
917 | rdadd(&rs, t.aux); |
918 | rdaddc(&rsc, '\\'); |
919 | rdaddsc(&rsc, t.origtext); |
e4ea58f8 |
920 | } else { |
d7482997 |
921 | rdadds(&rs, t.text); |
e4ea58f8 |
922 | rdaddsc(&rsc, t.origtext); |
923 | } |
d7482997 |
924 | } |
925 | if (t.type != tok_rbrace) { |
926 | error(err_kwunclosed, &t.pos); |
927 | continue; |
928 | } |
929 | rdadd(&rs, 0); /* add string terminator */ |
e4ea58f8 |
930 | rdaddc(&rsc, 0); /* add string terminator */ |
d7482997 |
931 | dtor(t), t = get_token(in); /* eat right brace */ |
932 | } |
933 | |
e4ea58f8 |
934 | rdadd(&rs, 0); /* add string terminator */ |
935 | rdaddc(&rsc, 0); /* add string terminator */ |
d7482997 |
936 | |
937 | /* See whether we have the right number of keywords. */ |
938 | if ((needkw & 48) && nkeys > 0) |
939 | error(err_kwillegal, &fp); |
940 | if ((needkw & 11) && nkeys == 0) |
941 | error(err_kwexpected, &fp); |
942 | if ((needkw & 5) && nkeys > 1) |
943 | error(err_kwtoomany, &fp); |
944 | |
945 | if (is_macro) { |
946 | /* |
947 | * Macro definition. Get the rest of the line |
948 | * as a code-paragraph token, repeatedly until |
949 | * there's nothing more left of it. Separate |
950 | * with newlines. |
951 | */ |
952 | rdstring macrotext = { 0, 0, NULL }; |
953 | while (1) { |
954 | dtor(t), t = get_codepar_token(in); |
955 | if (macrotext.pos > 0) |
956 | rdadd(¯otext, L'\n'); |
957 | rdadds(¯otext, t.text); |
958 | dtor(t), t = get_token(in); |
959 | if (t.type == tok_eop) break; |
960 | } |
961 | macrodef(macros, rs.text, macrotext.text, fp); |
962 | continue; /* next paragraph */ |
963 | } |
964 | |
965 | par.keyword = rdtrim(&rs); |
e4ea58f8 |
966 | par.origkeyword = rdtrimc(&rsc); |
d7482997 |
967 | |
968 | /* Move to EOP in case of needkw==8 or 16 (no body) */ |
969 | if (needkw & 24) { |
b3c80b25 |
970 | /* We allow whitespace even when we expect no para body */ |
971 | while (t.type == tok_white) |
972 | dtor(t), t = get_token(in); |
973 | if (t.type != tok_eop && t.type != tok_eof && |
974 | (start_cmd == c__invalid || |
975 | t.type != tok_cmd || t.cmd != start_cmd)) { |
d7482997 |
976 | error(err_bodyillegal, &t.pos); |
977 | /* Error recovery: eat the rest of the paragraph */ |
b3c80b25 |
978 | while (t.type != tok_eop && t.type != tok_eof && |
979 | (start_cmd == c__invalid || |
980 | t.type != tok_cmd || t.cmd != start_cmd)) |
d7482997 |
981 | dtor(t), t = get_token(in); |
982 | } |
b3c80b25 |
983 | if (t.type == tok_cmd) |
984 | already = TRUE;/* inhibit get_token at top of loop */ |
7136a6c7 |
985 | prev_para_type = par.type; |
d7482997 |
986 | addpara(par, ret); |
e34ba5c3 |
987 | |
988 | if (par.type == para_Config) { |
989 | input_configure(in, &par); |
990 | } |
d7482997 |
991 | continue; /* next paragraph */ |
992 | } |
993 | } |
994 | } |
995 | |
996 | /* |
997 | * Now read the actual paragraph, word by word, adding to |
998 | * the paragraph list. |
999 | * |
1000 | * Mid-paragraph commands: |
1001 | * |
1002 | * \K \k |
b7747050 |
1003 | * \c \cw \cq |
d7482997 |
1004 | * \e |
1005 | * \i \ii |
1006 | * \I |
b7747050 |
1007 | * \q |
d7482997 |
1008 | * \u |
1009 | * \W |
1010 | * \date |
1011 | * \\ \{ \} |
1012 | */ |
1013 | parsestk = stk_new(); |
1014 | style = word_Normal; |
1015 | spcstyle = word_WhiteSpace; |
1016 | indexing = FALSE; |
1017 | seenwhite = TRUE; |
1018 | while (t.type != tok_eop && t.type != tok_eof) { |
1019 | iswhite = FALSE; |
1020 | already = FALSE; |
b3c80b25 |
1021 | |
1022 | /* Handle implicit paragraph breaks after \IM, \BR etc */ |
1023 | if (start_cmd != c__invalid && |
1024 | t.type == tok_cmd && t.cmd == start_cmd) { |
1025 | already = TRUE; /* inhibit get_token at top of loop */ |
1026 | break; |
1027 | } |
1028 | |
672e4626 |
1029 | if (t.type == tok_cmd && t.cmd == c__nop) { |
1030 | dtor(t), t = get_token(in); |
1031 | continue; /* do nothing! */ |
1032 | } |
1033 | |
d7482997 |
1034 | if (t.type == tok_cmd && t.cmd == c__escaped) { |
1035 | t.type = tok_word; /* nice and simple */ |
1036 | t.aux = 0; /* even if `\-' - nonbreaking! */ |
1037 | } |
1038 | if (t.type == tok_cmd && t.cmd == c__nbsp) { |
1039 | t.type = tok_word; /* nice and simple */ |
1040 | sfree(t.text); |
1041 | t.text = ustrdup(L" "); /* text is ` ' not `_' */ |
1042 | t.aux = 0; /* (nonbreaking) */ |
1043 | } |
1044 | switch (t.type) { |
1045 | case tok_white: |
1046 | if (whptr == &par.words) |
1047 | break; /* strip whitespace at start of para */ |
1048 | wd.text = NULL; |
1049 | wd.type = spcstyle; |
1050 | wd.alt = NULL; |
1051 | wd.aux = 0; |
1052 | wd.fpos = t.pos; |
1053 | wd.breaks = FALSE; |
b3c80b25 |
1054 | |
1055 | /* |
1056 | * Inhibit use of whitespace if it's (probably the |
1057 | * newline) before a repeat \IM / \BR type |
1058 | * directive. |
1059 | */ |
1060 | if (start_cmd != c__invalid) { |
1061 | dtor(t), t = get_token(in); |
1062 | already = TRUE; |
1063 | if (t.type == tok_cmd && t.cmd == start_cmd) |
1064 | break; |
1065 | } |
1066 | |
d7482997 |
1067 | if (indexing) |
1068 | rdadd(&indexstr, ' '); |
1069 | if (!indexing || index_visible) |
1070 | addword(wd, &whptr); |
1071 | if (indexing) |
1072 | addword(wd, &idximplicit); |
1073 | iswhite = TRUE; |
1074 | break; |
1075 | case tok_word: |
1076 | if (indexing) |
1077 | rdadds(&indexstr, t.text); |
1078 | wd.type = style; |
1079 | wd.alt = NULL; |
1080 | wd.aux = 0; |
1081 | wd.fpos = t.pos; |
1082 | wd.breaks = t.aux; |
1083 | if (!indexing || index_visible) { |
1084 | wd.text = ustrdup(t.text); |
1085 | addword(wd, &whptr); |
1086 | } |
1087 | if (indexing) { |
1088 | wd.text = ustrdup(t.text); |
1089 | addword(wd, &idximplicit); |
1090 | } |
1091 | break; |
1092 | case tok_lbrace: |
1093 | error(err_unexbrace, &t.pos); |
1094 | /* Error recovery: push nop */ |
f1530049 |
1095 | sitem = snew(struct stack_item); |
d7482997 |
1096 | sitem->type = stack_nop; |
f4551933 |
1097 | sitem->fpos = t.pos; |
d7482997 |
1098 | stk_push(parsestk, sitem); |
1099 | break; |
1100 | case tok_rbrace: |
1101 | sitem = stk_pop(parsestk); |
7136a6c7 |
1102 | if (!sitem) { |
1103 | /* |
1104 | * This closing brace could have been an |
1105 | * indication that the cross-paragraph stack |
1106 | * wants popping. Accordingly, we treat it here |
1107 | * as an indication that the paragraph is over. |
1108 | */ |
1109 | already = TRUE; |
1110 | goto finished_para; |
1111 | } else { |
d7482997 |
1112 | if (sitem->type & stack_ualt) { |
1113 | whptr = sitem->whptr; |
1114 | idximplicit = sitem->idximplicit; |
1115 | } |
1116 | if (sitem->type & stack_style) { |
1117 | style = word_Normal; |
1118 | spcstyle = word_WhiteSpace; |
1119 | } |
1120 | if (sitem->type & stack_idx) { |
1121 | indexword->text = ustrdup(indexstr.text); |
8b7d993d |
1122 | if (index_downcase) { |
1123 | word *w; |
1124 | |
d7482997 |
1125 | ustrlow(indexword->text); |
8b7d993d |
1126 | ustrlow(indexstr.text); |
1127 | |
1128 | for (w = idxwordlist; w; w = w->next) |
1129 | if (w->text) |
1130 | ustrlow(w->text); |
1131 | } |
d7482997 |
1132 | indexing = FALSE; |
1133 | rdadd(&indexstr, L'\0'); |
f4551933 |
1134 | index_merge(idx, FALSE, indexstr.text, |
1135 | idxwordlist, &sitem->fpos); |
d7482997 |
1136 | sfree(indexstr.text); |
1137 | } |
1138 | if (sitem->type & stack_hyper) { |
1139 | wd.text = NULL; |
1140 | wd.type = word_HyperEnd; |
1141 | wd.alt = NULL; |
1142 | wd.aux = 0; |
1143 | wd.fpos = t.pos; |
1144 | wd.breaks = FALSE; |
1145 | if (!indexing || index_visible) |
1146 | addword(wd, &whptr); |
1147 | if (indexing) |
1148 | addword(wd, &idximplicit); |
1149 | } |
1150 | if (sitem->type & stack_quote) { |
1151 | wd.text = NULL; |
1152 | wd.type = toquotestyle(style); |
1153 | wd.alt = NULL; |
1154 | wd.aux = quote_Close; |
1155 | wd.fpos = t.pos; |
1156 | wd.breaks = FALSE; |
1157 | if (!indexing || index_visible) |
1158 | addword(wd, &whptr); |
1159 | if (indexing) { |
1160 | rdadd(&indexstr, L'"'); |
1161 | addword(wd, &idximplicit); |
1162 | } |
1163 | } |
1164 | } |
1165 | sfree(sitem); |
1166 | break; |
1167 | case tok_cmd: |
1168 | switch (t.cmd) { |
1169 | case c__comment: |
1170 | /* |
1171 | * In-paragraph comment: \#{ balanced braces } |
1172 | * |
1173 | * Anything goes here; even tok_eop. We should |
1174 | * eat whitespace after the close brace _if_ |
1175 | * there was whitespace before the \#. |
1176 | */ |
1177 | dtor(t), t = get_token(in); |
1178 | if (t.type != tok_lbrace) { |
1179 | error(err_explbr, &t.pos); |
1180 | } else { |
1181 | int braces = 1; |
1182 | while (braces > 0) { |
1183 | dtor(t), t = get_token(in); |
1184 | if (t.type == tok_lbrace) |
1185 | braces++; |
1186 | else if (t.type == tok_rbrace) |
1187 | braces--; |
1188 | else if (t.type == tok_eof) { |
1189 | error(err_commenteof, &t.pos); |
1190 | break; |
1191 | } |
1192 | } |
1193 | } |
1194 | if (seenwhite) { |
1195 | already = TRUE; |
1196 | dtor(t), t = get_token(in); |
1197 | if (t.type == tok_white) { |
1198 | iswhite = TRUE; |
1199 | already = FALSE; |
1200 | } |
1201 | } |
1202 | break; |
1203 | case c_q: |
b7747050 |
1204 | case c_cq: |
1205 | type = t.cmd; |
d7482997 |
1206 | dtor(t), t = get_token(in); |
1207 | if (t.type != tok_lbrace) { |
1208 | error(err_explbr, &t.pos); |
1209 | } else { |
6ff15f2b |
1210 | /* |
1211 | * Enforce that \q may not be used anywhere |
1212 | * within \c. (It shouldn't be necessary |
1213 | * since the whole point of \c should be |
1214 | * that the user wants to exercise exact |
1215 | * control over the glyphs used, and |
1216 | * forbidding it has the useful effect of |
1217 | * relieving some backends of having to |
1218 | * make difficult decisions.) |
1219 | */ |
1220 | int stype; |
1221 | |
1222 | if (style != word_Code && style != word_WeakCode) { |
1223 | wd.text = NULL; |
1224 | wd.type = toquotestyle(style); |
1225 | wd.alt = NULL; |
1226 | wd.aux = quote_Open; |
1227 | wd.fpos = t.pos; |
1228 | wd.breaks = FALSE; |
1229 | if (!indexing || index_visible) |
1230 | addword(wd, &whptr); |
1231 | if (indexing) { |
1232 | rdadd(&indexstr, L'"'); |
1233 | addword(wd, &idximplicit); |
1234 | } |
1235 | stype = stack_quote; |
1236 | } else { |
1237 | error(err_codequote, &t.pos); |
1238 | stype = stack_nop; |
d7482997 |
1239 | } |
f1530049 |
1240 | sitem = snew(struct stack_item); |
f4551933 |
1241 | sitem->fpos = t.pos; |
6ff15f2b |
1242 | sitem->type = stype; |
b7747050 |
1243 | if (type == c_cq) { |
1244 | if (style != word_Normal) { |
1245 | error(err_nestedstyles, &t.pos); |
1246 | } else { |
1247 | style = word_WeakCode; |
1248 | spcstyle = tospacestyle(style); |
1249 | sitem->type |= stack_style; |
1250 | } |
1251 | } |
d7482997 |
1252 | stk_push(parsestk, sitem); |
1253 | } |
1254 | break; |
1255 | case c_K: |
1256 | case c_k: |
1257 | case c_W: |
1258 | case c_date: |
1259 | /* |
1260 | * Keyword, hyperlink, or \date. We expect a |
1261 | * left brace, some text, and then a right |
1262 | * brace. No nesting; no arguments. |
1263 | */ |
1264 | wd.fpos = t.pos; |
1265 | wd.breaks = FALSE; |
1266 | if (t.cmd == c_K) |
1267 | wd.type = word_UpperXref; |
1268 | else if (t.cmd == c_k) |
1269 | wd.type = word_LowerXref; |
1270 | else if (t.cmd == c_W) |
1271 | wd.type = word_HyperLink; |
1272 | else |
1273 | wd.type = word_Normal; |
1274 | dtor(t), t = get_token(in); |
1275 | if (t.type != tok_lbrace) { |
1276 | if (wd.type == word_Normal) { |
1277 | time_t thetime = time(NULL); |
1278 | struct tm *broken = localtime(&thetime); |
1279 | already = TRUE; |
1280 | wdtext = ustrftime(NULL, broken); |
1281 | wd.type = style; |
1282 | } else { |
1283 | error(err_explbr, &t.pos); |
1284 | wdtext = NULL; |
1285 | } |
1286 | } else { |
1287 | rdstring rs = { 0, 0, NULL }; |
1288 | while (dtor(t), t = get_token(in), |
1289 | t.type == tok_word || t.type == tok_white) { |
1290 | if (t.type == tok_white) |
1291 | rdadd(&rs, ' '); |
1292 | else |
1293 | rdadds(&rs, t.text); |
1294 | } |
1295 | if (wd.type == word_Normal) { |
1296 | time_t thetime = time(NULL); |
1297 | struct tm *broken = localtime(&thetime); |
1298 | wdtext = ustrftime(rs.text, broken); |
1299 | wd.type = style; |
1300 | } else { |
1301 | wdtext = ustrdup(rs.text); |
1302 | } |
1303 | sfree(rs.text); |
1304 | if (t.type != tok_rbrace) { |
1305 | error(err_kwexprbr, &t.pos); |
1306 | } |
1307 | } |
1308 | wd.alt = NULL; |
1309 | wd.aux = 0; |
1310 | if (!indexing || index_visible) { |
1311 | wd.text = ustrdup(wdtext); |
1312 | addword(wd, &whptr); |
1313 | } |
1314 | if (indexing) { |
1315 | wd.text = ustrdup(wdtext); |
1316 | addword(wd, &idximplicit); |
1317 | } |
1318 | sfree(wdtext); |
1319 | if (wd.type == word_HyperLink) { |
1320 | /* |
1321 | * Hyperlinks are different: they then |
1322 | * expect another left brace, to begin |
1323 | * delimiting the text marked by the link. |
1324 | */ |
1325 | dtor(t), t = get_token(in); |
f1530049 |
1326 | sitem = snew(struct stack_item); |
f4551933 |
1327 | sitem->fpos = wd.fpos; |
d7482997 |
1328 | sitem->type = stack_hyper; |
b80802ba |
1329 | /* |
1330 | * Special cases: \W{}\i, \W{}\ii |
1331 | */ |
1332 | if (t.type == tok_cmd && |
1333 | (t.cmd == c_i || t.cmd == c_ii)) { |
1334 | if (indexing) { |
1335 | error(err_nestedindex, &t.pos); |
1336 | } else { |
1337 | /* Add an index-reference word with no |
1338 | * text as yet */ |
1339 | wd.type = word_IndexRef; |
1340 | wd.text = NULL; |
1341 | wd.alt = NULL; |
1342 | wd.aux = 0; |
1343 | wd.breaks = FALSE; |
1344 | indexword = addword(wd, &whptr); |
1345 | /* Set up a rdstring to read the |
1346 | * index text */ |
1347 | indexstr = nullrs; |
1348 | /* Flags so that we do the Right |
1349 | * Things with text */ |
1350 | index_visible = (type != c_I); |
1351 | index_downcase = (type == c_ii); |
1352 | indexing = TRUE; |
1353 | idxwordlist = NULL; |
1354 | idximplicit = &idxwordlist; |
1355 | |
1356 | sitem->type |= stack_idx; |
1357 | } |
1358 | dtor(t), t = get_token(in); |
1359 | } |
1360 | /* |
1361 | * Special cases: \W{}\c, \W{}\e, \W{}\cw |
1362 | */ |
d7482997 |
1363 | if (t.type == tok_cmd && |
1364 | (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) { |
1365 | if (style != word_Normal) |
1366 | error(err_nestedstyles, &t.pos); |
1367 | else { |
1368 | style = (t.cmd == c_c ? word_Code : |
1369 | t.cmd == c_cw ? word_WeakCode : |
1370 | word_Emph); |
1371 | spcstyle = tospacestyle(style); |
1372 | sitem->type |= stack_style; |
1373 | } |
1374 | dtor(t), t = get_token(in); |
1375 | } |
1376 | if (t.type != tok_lbrace) { |
1377 | error(err_explbr, &t.pos); |
1378 | sfree(sitem); |
1379 | } else { |
1380 | stk_push(parsestk, sitem); |
1381 | } |
1382 | } |
1383 | break; |
1384 | case c_c: |
1385 | case c_cw: |
1386 | case c_e: |
1387 | type = t.cmd; |
1388 | if (style != word_Normal) { |
1389 | error(err_nestedstyles, &t.pos); |
1390 | /* Error recovery: eat lbrace, push nop. */ |
1391 | dtor(t), t = get_token(in); |
f1530049 |
1392 | sitem = snew(struct stack_item); |
f4551933 |
1393 | sitem->fpos = t.pos; |
d7482997 |
1394 | sitem->type = stack_nop; |
1395 | stk_push(parsestk, sitem); |
1396 | } |
1397 | dtor(t), t = get_token(in); |
1398 | if (t.type != tok_lbrace) { |
1399 | error(err_explbr, &t.pos); |
1400 | } else { |
1401 | style = (type == c_c ? word_Code : |
1402 | type == c_cw ? word_WeakCode : |
1403 | word_Emph); |
1404 | spcstyle = tospacestyle(style); |
f1530049 |
1405 | sitem = snew(struct stack_item); |
f4551933 |
1406 | sitem->fpos = t.pos; |
d7482997 |
1407 | sitem->type = stack_style; |
1408 | stk_push(parsestk, sitem); |
1409 | } |
1410 | break; |
1411 | case c_i: |
1412 | case c_ii: |
1413 | case c_I: |
1414 | type = t.cmd; |
1415 | if (indexing) { |
1416 | error(err_nestedindex, &t.pos); |
1417 | /* Error recovery: eat lbrace, push nop. */ |
1418 | dtor(t), t = get_token(in); |
f1530049 |
1419 | sitem = snew(struct stack_item); |
f4551933 |
1420 | sitem->fpos = t.pos; |
d7482997 |
1421 | sitem->type = stack_nop; |
1422 | stk_push(parsestk, sitem); |
1423 | } |
f1530049 |
1424 | sitem = snew(struct stack_item); |
f4551933 |
1425 | sitem->fpos = t.pos; |
d7482997 |
1426 | sitem->type = stack_idx; |
1427 | dtor(t), t = get_token(in); |
1428 | /* |
1429 | * Special cases: \i\c, \i\e, \i\cw |
1430 | */ |
1431 | wd.fpos = t.pos; |
1432 | if (t.type == tok_cmd && |
1433 | (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) { |
1434 | if (style != word_Normal) |
1435 | error(err_nestedstyles, &t.pos); |
1436 | else { |
1437 | style = (t.cmd == c_c ? word_Code : |
1438 | t.cmd == c_cw ? word_WeakCode : |
1439 | word_Emph); |
1440 | spcstyle = tospacestyle(style); |
1441 | sitem->type |= stack_style; |
1442 | } |
1443 | dtor(t), t = get_token(in); |
1444 | } |
1445 | if (t.type != tok_lbrace) { |
1446 | sfree(sitem); |
1447 | error(err_explbr, &t.pos); |
1448 | } else { |
1449 | /* Add an index-reference word with no text as yet */ |
1450 | wd.type = word_IndexRef; |
1451 | wd.text = NULL; |
1452 | wd.alt = NULL; |
1453 | wd.aux = 0; |
1454 | wd.breaks = FALSE; |
1455 | indexword = addword(wd, &whptr); |
1456 | /* Set up a rdstring to read the index text */ |
1457 | indexstr = nullrs; |
1458 | /* Flags so that we do the Right Things with text */ |
1459 | index_visible = (type != c_I); |
1460 | index_downcase = (type == c_ii); |
1461 | indexing = TRUE; |
1462 | idxwordlist = NULL; |
1463 | idximplicit = &idxwordlist; |
1464 | /* Stack item to close the indexing on exit */ |
1465 | stk_push(parsestk, sitem); |
1466 | } |
1467 | break; |
1468 | case c_u: |
1469 | uchr = t.aux; |
1470 | utext[0] = uchr; utext[1] = 0; |
1471 | wd.type = style; |
1472 | wd.breaks = FALSE; |
1473 | wd.alt = NULL; |
1474 | wd.aux = 0; |
1475 | wd.fpos = t.pos; |
1476 | if (!indexing || index_visible) { |
1477 | wd.text = ustrdup(utext); |
1478 | uword = addword(wd, &whptr); |
1479 | } else |
1480 | uword = NULL; |
1481 | if (indexing) { |
1482 | wd.text = ustrdup(utext); |
1483 | iword = addword(wd, &idximplicit); |
1484 | } else |
1485 | iword = NULL; |
1486 | dtor(t), t = get_token(in); |
1487 | if (t.type == tok_lbrace) { |
1488 | /* |
1489 | * \u with a left brace. Until the brace |
1490 | * closes, all further words go on a |
1491 | * sidetrack from the main thread of the |
1492 | * paragraph. |
1493 | */ |
f1530049 |
1494 | sitem = snew(struct stack_item); |
f4551933 |
1495 | sitem->fpos = t.pos; |
d7482997 |
1496 | sitem->type = stack_ualt; |
1497 | sitem->whptr = whptr; |
1498 | sitem->idximplicit = idximplicit; |
1499 | stk_push(parsestk, sitem); |
1500 | whptr = uword ? &uword->alt : NULL; |
1501 | idximplicit = iword ? &iword->alt : NULL; |
1502 | } else { |
1503 | if (indexing) |
1504 | rdadd(&indexstr, uchr); |
1505 | already = TRUE; |
1506 | } |
1507 | break; |
1508 | default: |
1509 | if (!macrolookup(macros, in, t.text, &t.pos)) |
1510 | error(err_badmidcmd, t.text, &t.pos); |
1511 | break; |
1512 | } |
1513 | } |
1514 | if (!already) |
1515 | dtor(t), t = get_token(in); |
1516 | seenwhite = iswhite; |
1517 | } |
7136a6c7 |
1518 | finished_para: |
d7482997 |
1519 | /* Check the stack is empty */ |
7136a6c7 |
1520 | if (stk_top(parsestk)) { |
1521 | while ((sitem = stk_pop(parsestk))) |
d7482997 |
1522 | sfree(sitem); |
d7482997 |
1523 | error(err_missingrbrace, &t.pos); |
1524 | } |
1525 | stk_free(parsestk); |
7136a6c7 |
1526 | prev_para_type = par.type; |
d7482997 |
1527 | addpara(par, ret); |
7136a6c7 |
1528 | if (t.type == tok_eof) |
1529 | already = TRUE; |
1530 | } |
1531 | |
1532 | if (stk_top(crossparastk)) { |
1533 | void *p; |
1534 | |
1535 | error(err_missingrbrace2, &t.pos); |
1536 | while ((p = stk_pop(crossparastk))) |
1537 | sfree(p); |
d7482997 |
1538 | } |
677e18a2 |
1539 | |
1540 | /* |
1541 | * We break to here rather than returning, because otherwise |
1542 | * this cleanup doesn't happen. |
1543 | */ |
d7482997 |
1544 | dtor(t); |
1545 | macrocleanup(macros); |
7136a6c7 |
1546 | |
1547 | stk_free(crossparastk); |
d7482997 |
1548 | } |
1549 | |
1550 | paragraph *read_input(input *in, indexdata *idx) { |
1551 | paragraph *head = NULL; |
1552 | paragraph **hptr = &head; |
1553 | |
1554 | while (in->currindex < in->nfiles) { |
1555 | in->currfp = fopen(in->filenames[in->currindex], "r"); |
1556 | if (in->currfp) { |
1557 | setpos(in, in->filenames[in->currindex]); |
e34ba5c3 |
1558 | in->charset = in->defcharset; |
1559 | in->csstate = charset_init_state; |
e4ea58f8 |
1560 | in->wcpos = in->nwc = 0; |
1561 | in->pushback_chars = NULL; |
d7482997 |
1562 | read_file(&hptr, in, idx); |
1563 | } |
1564 | in->currindex++; |
1565 | } |
1566 | |
1567 | return head; |
1568 | } |