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); |
126239bf |
429 | prevpos = rsc.pos; |
d7482997 |
430 | } else if (c == 'u') { |
431 | int len = 0; |
432 | do { |
433 | rdadd(&rs, c); |
434 | len++; |
e4ea58f8 |
435 | prevpos = rsc.pos; |
436 | c = get(in, &cpos, &rsc); |
d7482997 |
437 | } while (ishex(c) && len < 5); |
438 | unget(in, c, &cpos); |
439 | } else if (iscmd(c)) { |
440 | do { |
441 | rdadd(&rs, c); |
e4ea58f8 |
442 | prevpos = rsc.pos; |
443 | c = get(in, &cpos, &rsc); |
d7482997 |
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); |
e4ea58f8 |
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 | } |
d7482997 |
460 | match_kw(&ret); |
461 | sfree(rs.text); |
e4ea58f8 |
462 | sfree(rsc.text); |
d7482997 |
463 | return ret; |
464 | } else if (c == '{') { /* tok_lbrace */ |
465 | ret.type = tok_lbrace; |
e4ea58f8 |
466 | sfree(rsc.text); |
d7482997 |
467 | return ret; |
468 | } else if (c == '}') { /* tok_rbrace */ |
469 | ret.type = tok_rbrace; |
e4ea58f8 |
470 | sfree(rsc.text); |
d7482997 |
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 */ |
e4ea58f8 |
482 | prevpos = 0; |
d7482997 |
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 == '-') { |
e4ea58f8 |
491 | prevpos = rsc.pos; |
d7482997 |
492 | ret.aux = TRUE; |
493 | break; /* hyphen terminates word */ |
494 | } |
495 | } |
e4ea58f8 |
496 | prevpos = rsc.pos; |
497 | c = get(in, &cpos, &rsc); |
d7482997 |
498 | } |
499 | ret.type = tok_word; |
500 | ret.text = ustrdup(rs.text); |
e4ea58f8 |
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 | } |
d7482997 |
508 | sfree(rs.text); |
e4ea58f8 |
509 | sfree(rsc.text); |
d7482997 |
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 | |
e4ea58f8 |
523 | c = get(in, &cpos, NULL); |
d7482997 |
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; |
e4ea58f8 |
539 | ret.origtext = NULL; |
540 | c = get(in, &cpos, NULL); /* expect (and discard) one space */ |
d7482997 |
541 | ret.pos = cpos; |
542 | if (c == ' ') { |
e4ea58f8 |
543 | c = get(in, &cpos, NULL); |
d7482997 |
544 | ret.pos = cpos; |
545 | } |
546 | while (!isnl(c) && c != EOF) { |
547 | int c2 = c; |
e4ea58f8 |
548 | c = get(in, &cpos, NULL); |
d7482997 |
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; |
f1530049 |
566 | mnewword = snew(word); |
d7482997 |
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) { |
f1530049 |
578 | paragraph *mnewpara = snew(paragraph); |
d7482997 |
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 | */ |
e4ea58f8 |
590 | #define dtor(t) ( sfree(t.text), sfree(t.origtext) ) |
d7482997 |
591 | |
592 | /* |
593 | * Reads a single file (ie until get() returns EOF) |
594 | */ |
41848b3e |
595 | static void read_file(paragraph ***ret, input *in, indexdata *idx, |
596 | tree234 *macros) { |
d7482997 |
597 | token t; |
598 | paragraph par; |
599 | word wd, **whptr, **idximplicit; |
d7482997 |
600 | wchar_t utext[2], *wdtext; |
601 | int style, spcstyle; |
602 | int already; |
603 | int iswhite, seenwhite; |
604 | int type; |
7136a6c7 |
605 | int prev_para_type; |
d7482997 |
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 */ |
ee90d1f0 |
613 | stack_quote = 16 /* \q */ |
d7482997 |
614 | } type; |
615 | word **whptr; /* to restore from \u alternatives */ |
616 | word **idximplicit; /* to restore from \u alternatives */ |
f4551933 |
617 | filepos fpos; |
6ff15f2b |
618 | int in_code; |
d7482997 |
619 | } *sitem; |
620 | stack parsestk; |
7136a6c7 |
621 | struct crossparaitem { |
2614b01d |
622 | int type; /* currently c_lcont, c_quote or -1 */ |
623 | int seen_lcont, seen_quote; |
7136a6c7 |
624 | }; |
625 | stack crossparastk; |
d7482997 |
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; |
e4ea58f8 |
634 | t.origtext = NULL; |
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 | |
41848b3e |
811 | while (t.type == tok_cmd && |
812 | macrolookup(macros, in, t.text, &t.pos)) { |
813 | dtor(t), t = get_token(in); |
814 | } |
815 | |
7136a6c7 |
816 | /* |
d7482997 |
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 | break; /* `\#{': isn't a comment para */ |
840 | do { |
841 | dtor(t), t = get_token(in); |
842 | } while (t.type != tok_eop && t.type != tok_eof); |
843 | continue; /* next paragraph */ |
844 | /* |
845 | * `needkw' values: |
846 | * |
847 | * 1 -- exactly one keyword |
848 | * 2 -- at least one keyword |
849 | * 4 -- any number of keywords including zero |
850 | * 8 -- at least one keyword and then nothing else |
851 | * 16 -- nothing at all! no keywords, no body |
852 | * 32 -- no keywords at all |
853 | */ |
854 | case c_A: needkw = 2; par.type = para_Appendix; break; |
855 | case c_B: needkw = 2; par.type = para_Biblio; break; |
b3c80b25 |
856 | case c_BR: needkw = 1; par.type = para_BR; |
857 | start_cmd = c_BR; break; |
d7482997 |
858 | case c_C: needkw = 2; par.type = para_Chapter; break; |
859 | case c_H: needkw = 2; par.type = para_Heading; |
860 | par.aux = 0; |
861 | break; |
b3c80b25 |
862 | case c_IM: needkw = 2; par.type = para_IM; |
863 | start_cmd = c_IM; break; |
d7482997 |
864 | case c_S: needkw = 2; par.type = para_Subsect; |
865 | par.aux = t.aux; break; |
866 | case c_U: needkw = 32; par.type = para_UnnumberedChapter; break; |
867 | /* For \b and \n the keyword is optional */ |
868 | case c_b: needkw = 4; par.type = para_Bullet; break; |
7136a6c7 |
869 | case c_dt: needkw = 4; par.type = para_DescribedThing; break; |
870 | case c_dd: needkw = 4; par.type = para_Description; break; |
d7482997 |
871 | case c_n: needkw = 4; par.type = para_NumberedList; break; |
b3c80b25 |
872 | case c_cfg: needkw = 8; par.type = para_Config; |
873 | start_cmd = c_cfg; break; |
d7482997 |
874 | case c_copyright: needkw = 32; par.type = para_Copyright; break; |
875 | case c_define: is_macro = TRUE; needkw = 1; break; |
876 | /* For \nocite the keyword is _everything_ */ |
877 | case c_nocite: needkw = 8; par.type = para_NoCite; break; |
8902e0ed |
878 | case c_preamble: needkw = 32; par.type = para_Normal; break; |
d7482997 |
879 | case c_rule: needkw = 16; par.type = para_Rule; break; |
880 | case c_title: needkw = 32; par.type = para_Title; break; |
881 | case c_versionid: needkw = 32; par.type = para_VersionID; break; |
882 | } |
883 | |
7136a6c7 |
884 | if (par.type == para_Chapter || |
885 | par.type == para_Heading || |
886 | par.type == para_Subsect || |
887 | par.type == para_Appendix || |
888 | par.type == para_UnnumberedChapter) { |
889 | struct crossparaitem *sitem = stk_top(crossparastk); |
2614b01d |
890 | if (sitem && (sitem->seen_lcont || sitem->seen_quote)) { |
891 | error(err_sectmarkerinblock, |
892 | &t.pos, |
893 | (sitem->seen_lcont ? "lcont" : "quote")); |
7136a6c7 |
894 | } |
895 | } |
896 | |
d7482997 |
897 | if (needkw > 0) { |
898 | rdstring rs = { 0, 0, NULL }; |
e4ea58f8 |
899 | rdstringc rsc = { 0, 0, NULL }; |
d7482997 |
900 | int nkeys = 0; |
901 | filepos fp; |
902 | |
903 | /* Get keywords. */ |
904 | dtor(t), t = get_token(in); |
905 | fp = t.pos; |
906 | while (t.type == tok_lbrace) { |
907 | /* This is a keyword. */ |
908 | nkeys++; |
909 | /* FIXME: there will be bugs if anyone specifies an |
910 | * empty keyword (\foo{}), so trap this case. */ |
911 | while (dtor(t), t = get_token(in), |
912 | t.type == tok_word || |
913 | t.type == tok_white || |
914 | (t.type == tok_cmd && t.cmd == c__nbsp) || |
db662ca1 |
915 | (t.type == tok_cmd && t.cmd == c__escaped) || |
916 | (t.type == tok_cmd && t.cmd == c_u)) { |
d7482997 |
917 | if (t.type == tok_white || |
e4ea58f8 |
918 | (t.type == tok_cmd && t.cmd == c__nbsp)) { |
d7482997 |
919 | rdadd(&rs, ' '); |
e4ea58f8 |
920 | rdaddc(&rsc, ' '); |
db662ca1 |
921 | } else if (t.type == tok_cmd && t.cmd == c_u) { |
922 | rdadd(&rs, t.aux); |
923 | rdaddc(&rsc, '\\'); |
924 | rdaddsc(&rsc, t.origtext); |
e4ea58f8 |
925 | } else { |
d7482997 |
926 | rdadds(&rs, t.text); |
e4ea58f8 |
927 | rdaddsc(&rsc, t.origtext); |
928 | } |
d7482997 |
929 | } |
930 | if (t.type != tok_rbrace) { |
931 | error(err_kwunclosed, &t.pos); |
932 | continue; |
933 | } |
934 | rdadd(&rs, 0); /* add string terminator */ |
e4ea58f8 |
935 | rdaddc(&rsc, 0); /* add string terminator */ |
d7482997 |
936 | dtor(t), t = get_token(in); /* eat right brace */ |
937 | } |
938 | |
e4ea58f8 |
939 | rdadd(&rs, 0); /* add string terminator */ |
940 | rdaddc(&rsc, 0); /* add string terminator */ |
d7482997 |
941 | |
942 | /* See whether we have the right number of keywords. */ |
943 | if ((needkw & 48) && nkeys > 0) |
944 | error(err_kwillegal, &fp); |
945 | if ((needkw & 11) && nkeys == 0) |
946 | error(err_kwexpected, &fp); |
947 | if ((needkw & 5) && nkeys > 1) |
948 | error(err_kwtoomany, &fp); |
949 | |
950 | if (is_macro) { |
951 | /* |
952 | * Macro definition. Get the rest of the line |
953 | * as a code-paragraph token, repeatedly until |
954 | * there's nothing more left of it. Separate |
955 | * with newlines. |
956 | */ |
957 | rdstring macrotext = { 0, 0, NULL }; |
958 | while (1) { |
959 | dtor(t), t = get_codepar_token(in); |
960 | if (macrotext.pos > 0) |
961 | rdadd(¯otext, L'\n'); |
962 | rdadds(¯otext, t.text); |
963 | dtor(t), t = get_token(in); |
964 | if (t.type == tok_eop) break; |
965 | } |
966 | macrodef(macros, rs.text, macrotext.text, fp); |
967 | continue; /* next paragraph */ |
968 | } |
969 | |
970 | par.keyword = rdtrim(&rs); |
e4ea58f8 |
971 | par.origkeyword = rdtrimc(&rsc); |
d7482997 |
972 | |
973 | /* Move to EOP in case of needkw==8 or 16 (no body) */ |
974 | if (needkw & 24) { |
b3c80b25 |
975 | /* We allow whitespace even when we expect no para body */ |
976 | while (t.type == tok_white) |
977 | dtor(t), t = get_token(in); |
978 | if (t.type != tok_eop && t.type != tok_eof && |
979 | (start_cmd == c__invalid || |
980 | t.type != tok_cmd || t.cmd != start_cmd)) { |
d7482997 |
981 | error(err_bodyillegal, &t.pos); |
982 | /* Error recovery: eat the rest of the paragraph */ |
b3c80b25 |
983 | while (t.type != tok_eop && t.type != tok_eof && |
984 | (start_cmd == c__invalid || |
985 | t.type != tok_cmd || t.cmd != start_cmd)) |
d7482997 |
986 | dtor(t), t = get_token(in); |
987 | } |
b3c80b25 |
988 | if (t.type == tok_cmd) |
989 | already = TRUE;/* inhibit get_token at top of loop */ |
7136a6c7 |
990 | prev_para_type = par.type; |
d7482997 |
991 | addpara(par, ret); |
e34ba5c3 |
992 | |
993 | if (par.type == para_Config) { |
994 | input_configure(in, &par); |
995 | } |
d7482997 |
996 | continue; /* next paragraph */ |
997 | } |
998 | } |
999 | } |
1000 | |
1001 | /* |
1002 | * Now read the actual paragraph, word by word, adding to |
1003 | * the paragraph list. |
1004 | * |
1005 | * Mid-paragraph commands: |
1006 | * |
1007 | * \K \k |
b7747050 |
1008 | * \c \cw \cq |
d7482997 |
1009 | * \e |
1010 | * \i \ii |
1011 | * \I |
b7747050 |
1012 | * \q |
d7482997 |
1013 | * \u |
1014 | * \W |
1015 | * \date |
1016 | * \\ \{ \} |
1017 | */ |
1018 | parsestk = stk_new(); |
1019 | style = word_Normal; |
1020 | spcstyle = word_WhiteSpace; |
1021 | indexing = FALSE; |
1022 | seenwhite = TRUE; |
1023 | while (t.type != tok_eop && t.type != tok_eof) { |
1024 | iswhite = FALSE; |
1025 | already = FALSE; |
b3c80b25 |
1026 | |
1027 | /* Handle implicit paragraph breaks after \IM, \BR etc */ |
1028 | if (start_cmd != c__invalid && |
1029 | t.type == tok_cmd && t.cmd == start_cmd) { |
1030 | already = TRUE; /* inhibit get_token at top of loop */ |
1031 | break; |
1032 | } |
1033 | |
672e4626 |
1034 | if (t.type == tok_cmd && t.cmd == c__nop) { |
1035 | dtor(t), t = get_token(in); |
1036 | continue; /* do nothing! */ |
1037 | } |
1038 | |
d7482997 |
1039 | if (t.type == tok_cmd && t.cmd == c__escaped) { |
1040 | t.type = tok_word; /* nice and simple */ |
1041 | t.aux = 0; /* even if `\-' - nonbreaking! */ |
1042 | } |
1043 | if (t.type == tok_cmd && t.cmd == c__nbsp) { |
1044 | t.type = tok_word; /* nice and simple */ |
1045 | sfree(t.text); |
1046 | t.text = ustrdup(L" "); /* text is ` ' not `_' */ |
1047 | t.aux = 0; /* (nonbreaking) */ |
1048 | } |
1049 | switch (t.type) { |
1050 | case tok_white: |
1051 | if (whptr == &par.words) |
1052 | break; /* strip whitespace at start of para */ |
1053 | wd.text = NULL; |
1054 | wd.type = spcstyle; |
1055 | wd.alt = NULL; |
1056 | wd.aux = 0; |
1057 | wd.fpos = t.pos; |
1058 | wd.breaks = FALSE; |
b3c80b25 |
1059 | |
1060 | /* |
1061 | * Inhibit use of whitespace if it's (probably the |
1062 | * newline) before a repeat \IM / \BR type |
1063 | * directive. |
1064 | */ |
1065 | if (start_cmd != c__invalid) { |
1066 | dtor(t), t = get_token(in); |
1067 | already = TRUE; |
1068 | if (t.type == tok_cmd && t.cmd == start_cmd) |
1069 | break; |
1070 | } |
1071 | |
d7482997 |
1072 | if (indexing) |
1073 | rdadd(&indexstr, ' '); |
1074 | if (!indexing || index_visible) |
1075 | addword(wd, &whptr); |
1076 | if (indexing) |
1077 | addword(wd, &idximplicit); |
1078 | iswhite = TRUE; |
1079 | break; |
1080 | case tok_word: |
1081 | if (indexing) |
1082 | rdadds(&indexstr, t.text); |
1083 | wd.type = style; |
1084 | wd.alt = NULL; |
1085 | wd.aux = 0; |
1086 | wd.fpos = t.pos; |
1087 | wd.breaks = t.aux; |
1088 | if (!indexing || index_visible) { |
1089 | wd.text = ustrdup(t.text); |
1090 | addword(wd, &whptr); |
1091 | } |
1092 | if (indexing) { |
1093 | wd.text = ustrdup(t.text); |
1094 | addword(wd, &idximplicit); |
1095 | } |
1096 | break; |
1097 | case tok_lbrace: |
1098 | error(err_unexbrace, &t.pos); |
1099 | /* Error recovery: push nop */ |
f1530049 |
1100 | sitem = snew(struct stack_item); |
d7482997 |
1101 | sitem->type = stack_nop; |
f4551933 |
1102 | sitem->fpos = t.pos; |
d7482997 |
1103 | stk_push(parsestk, sitem); |
1104 | break; |
1105 | case tok_rbrace: |
1106 | sitem = stk_pop(parsestk); |
7136a6c7 |
1107 | if (!sitem) { |
1108 | /* |
1109 | * This closing brace could have been an |
1110 | * indication that the cross-paragraph stack |
1111 | * wants popping. Accordingly, we treat it here |
1112 | * as an indication that the paragraph is over. |
1113 | */ |
1114 | already = TRUE; |
1115 | goto finished_para; |
1116 | } else { |
d7482997 |
1117 | if (sitem->type & stack_ualt) { |
1118 | whptr = sitem->whptr; |
1119 | idximplicit = sitem->idximplicit; |
1120 | } |
1121 | if (sitem->type & stack_style) { |
1122 | style = word_Normal; |
1123 | spcstyle = word_WhiteSpace; |
1124 | } |
1125 | if (sitem->type & stack_idx) { |
1126 | indexword->text = ustrdup(indexstr.text); |
8b7d993d |
1127 | if (index_downcase) { |
1128 | word *w; |
1129 | |
d7482997 |
1130 | ustrlow(indexword->text); |
8b7d993d |
1131 | ustrlow(indexstr.text); |
1132 | |
1133 | for (w = idxwordlist; w; w = w->next) |
1134 | if (w->text) |
1135 | ustrlow(w->text); |
1136 | } |
d7482997 |
1137 | indexing = FALSE; |
1138 | rdadd(&indexstr, L'\0'); |
f4551933 |
1139 | index_merge(idx, FALSE, indexstr.text, |
1140 | idxwordlist, &sitem->fpos); |
d7482997 |
1141 | sfree(indexstr.text); |
1142 | } |
1143 | if (sitem->type & stack_hyper) { |
1144 | wd.text = NULL; |
1145 | wd.type = word_HyperEnd; |
1146 | wd.alt = NULL; |
1147 | wd.aux = 0; |
1148 | wd.fpos = t.pos; |
1149 | wd.breaks = FALSE; |
1150 | if (!indexing || index_visible) |
1151 | addword(wd, &whptr); |
1152 | if (indexing) |
1153 | addword(wd, &idximplicit); |
1154 | } |
1155 | if (sitem->type & stack_quote) { |
1156 | wd.text = NULL; |
1157 | wd.type = toquotestyle(style); |
1158 | wd.alt = NULL; |
1159 | wd.aux = quote_Close; |
1160 | wd.fpos = t.pos; |
1161 | wd.breaks = FALSE; |
1162 | if (!indexing || index_visible) |
1163 | addword(wd, &whptr); |
1164 | if (indexing) { |
1165 | rdadd(&indexstr, L'"'); |
1166 | addword(wd, &idximplicit); |
1167 | } |
1168 | } |
1169 | } |
1170 | sfree(sitem); |
1171 | break; |
1172 | case tok_cmd: |
1173 | switch (t.cmd) { |
1174 | case c__comment: |
1175 | /* |
1176 | * In-paragraph comment: \#{ balanced braces } |
1177 | * |
1178 | * Anything goes here; even tok_eop. We should |
1179 | * eat whitespace after the close brace _if_ |
1180 | * there was whitespace before the \#. |
1181 | */ |
1182 | dtor(t), t = get_token(in); |
1183 | if (t.type != tok_lbrace) { |
1184 | error(err_explbr, &t.pos); |
1185 | } else { |
1186 | int braces = 1; |
1187 | while (braces > 0) { |
1188 | dtor(t), t = get_token(in); |
1189 | if (t.type == tok_lbrace) |
1190 | braces++; |
1191 | else if (t.type == tok_rbrace) |
1192 | braces--; |
1193 | else if (t.type == tok_eof) { |
1194 | error(err_commenteof, &t.pos); |
1195 | break; |
1196 | } |
1197 | } |
1198 | } |
1199 | if (seenwhite) { |
1200 | already = TRUE; |
1201 | dtor(t), t = get_token(in); |
1202 | if (t.type == tok_white) { |
1203 | iswhite = TRUE; |
1204 | already = FALSE; |
1205 | } |
1206 | } |
1207 | break; |
1208 | case c_q: |
b7747050 |
1209 | case c_cq: |
1210 | type = t.cmd; |
d7482997 |
1211 | dtor(t), t = get_token(in); |
1212 | if (t.type != tok_lbrace) { |
1213 | error(err_explbr, &t.pos); |
1214 | } else { |
6ff15f2b |
1215 | /* |
1216 | * Enforce that \q may not be used anywhere |
1217 | * within \c. (It shouldn't be necessary |
1218 | * since the whole point of \c should be |
1219 | * that the user wants to exercise exact |
1220 | * control over the glyphs used, and |
1221 | * forbidding it has the useful effect of |
1222 | * relieving some backends of having to |
1223 | * make difficult decisions.) |
1224 | */ |
1225 | int stype; |
1226 | |
1227 | if (style != word_Code && style != word_WeakCode) { |
1228 | wd.text = NULL; |
1229 | wd.type = toquotestyle(style); |
1230 | wd.alt = NULL; |
1231 | wd.aux = quote_Open; |
1232 | wd.fpos = t.pos; |
1233 | wd.breaks = FALSE; |
1234 | if (!indexing || index_visible) |
1235 | addword(wd, &whptr); |
1236 | if (indexing) { |
1237 | rdadd(&indexstr, L'"'); |
1238 | addword(wd, &idximplicit); |
1239 | } |
1240 | stype = stack_quote; |
1241 | } else { |
1242 | error(err_codequote, &t.pos); |
1243 | stype = stack_nop; |
d7482997 |
1244 | } |
f1530049 |
1245 | sitem = snew(struct stack_item); |
f4551933 |
1246 | sitem->fpos = t.pos; |
6ff15f2b |
1247 | sitem->type = stype; |
b7747050 |
1248 | if (type == c_cq) { |
1249 | if (style != word_Normal) { |
1250 | error(err_nestedstyles, &t.pos); |
1251 | } else { |
1252 | style = word_WeakCode; |
1253 | spcstyle = tospacestyle(style); |
1254 | sitem->type |= stack_style; |
1255 | } |
1256 | } |
d7482997 |
1257 | stk_push(parsestk, sitem); |
1258 | } |
1259 | break; |
1260 | case c_K: |
1261 | case c_k: |
1262 | case c_W: |
1263 | case c_date: |
1264 | /* |
1265 | * Keyword, hyperlink, or \date. We expect a |
1266 | * left brace, some text, and then a right |
1267 | * brace. No nesting; no arguments. |
1268 | */ |
1269 | wd.fpos = t.pos; |
1270 | wd.breaks = FALSE; |
1271 | if (t.cmd == c_K) |
1272 | wd.type = word_UpperXref; |
1273 | else if (t.cmd == c_k) |
1274 | wd.type = word_LowerXref; |
1275 | else if (t.cmd == c_W) |
1276 | wd.type = word_HyperLink; |
1277 | else |
1278 | wd.type = word_Normal; |
1279 | dtor(t), t = get_token(in); |
1280 | if (t.type != tok_lbrace) { |
1281 | if (wd.type == word_Normal) { |
1282 | time_t thetime = time(NULL); |
1283 | struct tm *broken = localtime(&thetime); |
1284 | already = TRUE; |
1285 | wdtext = ustrftime(NULL, broken); |
1286 | wd.type = style; |
1287 | } else { |
1288 | error(err_explbr, &t.pos); |
1289 | wdtext = NULL; |
1290 | } |
1291 | } else { |
1292 | rdstring rs = { 0, 0, NULL }; |
1293 | while (dtor(t), t = get_token(in), |
1294 | t.type == tok_word || t.type == tok_white) { |
1295 | if (t.type == tok_white) |
1296 | rdadd(&rs, ' '); |
1297 | else |
1298 | rdadds(&rs, t.text); |
1299 | } |
1300 | if (wd.type == word_Normal) { |
1301 | time_t thetime = time(NULL); |
1302 | struct tm *broken = localtime(&thetime); |
1303 | wdtext = ustrftime(rs.text, broken); |
1304 | wd.type = style; |
1305 | } else { |
1306 | wdtext = ustrdup(rs.text); |
1307 | } |
1308 | sfree(rs.text); |
1309 | if (t.type != tok_rbrace) { |
1310 | error(err_kwexprbr, &t.pos); |
1311 | } |
1312 | } |
1313 | wd.alt = NULL; |
1314 | wd.aux = 0; |
1315 | if (!indexing || index_visible) { |
1316 | wd.text = ustrdup(wdtext); |
1317 | addword(wd, &whptr); |
1318 | } |
1319 | if (indexing) { |
1320 | wd.text = ustrdup(wdtext); |
1321 | addword(wd, &idximplicit); |
1322 | } |
1323 | sfree(wdtext); |
1324 | if (wd.type == word_HyperLink) { |
1325 | /* |
1326 | * Hyperlinks are different: they then |
1327 | * expect another left brace, to begin |
1328 | * delimiting the text marked by the link. |
1329 | */ |
1330 | dtor(t), t = get_token(in); |
f1530049 |
1331 | sitem = snew(struct stack_item); |
f4551933 |
1332 | sitem->fpos = wd.fpos; |
d7482997 |
1333 | sitem->type = stack_hyper; |
b80802ba |
1334 | /* |
1335 | * Special cases: \W{}\i, \W{}\ii |
1336 | */ |
1337 | if (t.type == tok_cmd && |
1338 | (t.cmd == c_i || t.cmd == c_ii)) { |
1339 | if (indexing) { |
1340 | error(err_nestedindex, &t.pos); |
1341 | } else { |
1342 | /* Add an index-reference word with no |
1343 | * text as yet */ |
1344 | wd.type = word_IndexRef; |
1345 | wd.text = NULL; |
1346 | wd.alt = NULL; |
1347 | wd.aux = 0; |
1348 | wd.breaks = FALSE; |
1349 | indexword = addword(wd, &whptr); |
1350 | /* Set up a rdstring to read the |
1351 | * index text */ |
1352 | indexstr = nullrs; |
1353 | /* Flags so that we do the Right |
1354 | * Things with text */ |
1355 | index_visible = (type != c_I); |
1356 | index_downcase = (type == c_ii); |
1357 | indexing = TRUE; |
1358 | idxwordlist = NULL; |
1359 | idximplicit = &idxwordlist; |
1360 | |
1361 | sitem->type |= stack_idx; |
1362 | } |
1363 | dtor(t), t = get_token(in); |
1364 | } |
1365 | /* |
1366 | * Special cases: \W{}\c, \W{}\e, \W{}\cw |
1367 | */ |
d7482997 |
1368 | if (t.type == tok_cmd && |
1369 | (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) { |
1370 | if (style != word_Normal) |
1371 | error(err_nestedstyles, &t.pos); |
1372 | else { |
1373 | style = (t.cmd == c_c ? word_Code : |
1374 | t.cmd == c_cw ? word_WeakCode : |
1375 | word_Emph); |
1376 | spcstyle = tospacestyle(style); |
1377 | sitem->type |= stack_style; |
1378 | } |
1379 | dtor(t), t = get_token(in); |
1380 | } |
1381 | if (t.type != tok_lbrace) { |
1382 | error(err_explbr, &t.pos); |
1383 | sfree(sitem); |
1384 | } else { |
1385 | stk_push(parsestk, sitem); |
1386 | } |
1387 | } |
1388 | break; |
1389 | case c_c: |
1390 | case c_cw: |
1391 | case c_e: |
1392 | type = t.cmd; |
1393 | if (style != word_Normal) { |
1394 | error(err_nestedstyles, &t.pos); |
1395 | /* Error recovery: eat lbrace, push nop. */ |
1396 | dtor(t), t = get_token(in); |
f1530049 |
1397 | sitem = snew(struct stack_item); |
f4551933 |
1398 | sitem->fpos = t.pos; |
d7482997 |
1399 | sitem->type = stack_nop; |
1400 | stk_push(parsestk, sitem); |
1401 | } |
1402 | dtor(t), t = get_token(in); |
1403 | if (t.type != tok_lbrace) { |
1404 | error(err_explbr, &t.pos); |
1405 | } else { |
1406 | style = (type == c_c ? word_Code : |
1407 | type == c_cw ? word_WeakCode : |
1408 | word_Emph); |
1409 | spcstyle = tospacestyle(style); |
f1530049 |
1410 | sitem = snew(struct stack_item); |
f4551933 |
1411 | sitem->fpos = t.pos; |
d7482997 |
1412 | sitem->type = stack_style; |
1413 | stk_push(parsestk, sitem); |
1414 | } |
1415 | break; |
1416 | case c_i: |
1417 | case c_ii: |
1418 | case c_I: |
1419 | type = t.cmd; |
1420 | if (indexing) { |
1421 | error(err_nestedindex, &t.pos); |
1422 | /* Error recovery: eat lbrace, push nop. */ |
1423 | dtor(t), t = get_token(in); |
f1530049 |
1424 | sitem = snew(struct stack_item); |
f4551933 |
1425 | sitem->fpos = t.pos; |
d7482997 |
1426 | sitem->type = stack_nop; |
1427 | stk_push(parsestk, sitem); |
1428 | } |
f1530049 |
1429 | sitem = snew(struct stack_item); |
f4551933 |
1430 | sitem->fpos = t.pos; |
d7482997 |
1431 | sitem->type = stack_idx; |
1432 | dtor(t), t = get_token(in); |
1433 | /* |
1434 | * Special cases: \i\c, \i\e, \i\cw |
1435 | */ |
1436 | wd.fpos = t.pos; |
1437 | if (t.type == tok_cmd && |
1438 | (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) { |
1439 | if (style != word_Normal) |
1440 | error(err_nestedstyles, &t.pos); |
1441 | else { |
1442 | style = (t.cmd == c_c ? word_Code : |
1443 | t.cmd == c_cw ? word_WeakCode : |
1444 | word_Emph); |
1445 | spcstyle = tospacestyle(style); |
1446 | sitem->type |= stack_style; |
1447 | } |
1448 | dtor(t), t = get_token(in); |
1449 | } |
1450 | if (t.type != tok_lbrace) { |
1451 | sfree(sitem); |
1452 | error(err_explbr, &t.pos); |
1453 | } else { |
1454 | /* Add an index-reference word with no text as yet */ |
1455 | wd.type = word_IndexRef; |
1456 | wd.text = NULL; |
1457 | wd.alt = NULL; |
1458 | wd.aux = 0; |
1459 | wd.breaks = FALSE; |
1460 | indexword = addword(wd, &whptr); |
1461 | /* Set up a rdstring to read the index text */ |
1462 | indexstr = nullrs; |
1463 | /* Flags so that we do the Right Things with text */ |
1464 | index_visible = (type != c_I); |
1465 | index_downcase = (type == c_ii); |
1466 | indexing = TRUE; |
1467 | idxwordlist = NULL; |
1468 | idximplicit = &idxwordlist; |
1469 | /* Stack item to close the indexing on exit */ |
1470 | stk_push(parsestk, sitem); |
1471 | } |
1472 | break; |
1473 | case c_u: |
1474 | uchr = t.aux; |
1475 | utext[0] = uchr; utext[1] = 0; |
1476 | wd.type = style; |
1477 | wd.breaks = FALSE; |
1478 | wd.alt = NULL; |
1479 | wd.aux = 0; |
1480 | wd.fpos = t.pos; |
1481 | if (!indexing || index_visible) { |
1482 | wd.text = ustrdup(utext); |
1483 | uword = addword(wd, &whptr); |
1484 | } else |
1485 | uword = NULL; |
1486 | if (indexing) { |
1487 | wd.text = ustrdup(utext); |
1488 | iword = addword(wd, &idximplicit); |
1489 | } else |
1490 | iword = NULL; |
1491 | dtor(t), t = get_token(in); |
1492 | if (t.type == tok_lbrace) { |
1493 | /* |
1494 | * \u with a left brace. Until the brace |
1495 | * closes, all further words go on a |
1496 | * sidetrack from the main thread of the |
1497 | * paragraph. |
1498 | */ |
f1530049 |
1499 | sitem = snew(struct stack_item); |
f4551933 |
1500 | sitem->fpos = t.pos; |
d7482997 |
1501 | sitem->type = stack_ualt; |
1502 | sitem->whptr = whptr; |
1503 | sitem->idximplicit = idximplicit; |
1504 | stk_push(parsestk, sitem); |
1505 | whptr = uword ? &uword->alt : NULL; |
1506 | idximplicit = iword ? &iword->alt : NULL; |
1507 | } else { |
1508 | if (indexing) |
1509 | rdadd(&indexstr, uchr); |
1510 | already = TRUE; |
1511 | } |
1512 | break; |
1513 | default: |
1514 | if (!macrolookup(macros, in, t.text, &t.pos)) |
1515 | error(err_badmidcmd, t.text, &t.pos); |
1516 | break; |
1517 | } |
1518 | } |
1519 | if (!already) |
1520 | dtor(t), t = get_token(in); |
1521 | seenwhite = iswhite; |
1522 | } |
7136a6c7 |
1523 | finished_para: |
d7482997 |
1524 | /* Check the stack is empty */ |
7136a6c7 |
1525 | if (stk_top(parsestk)) { |
1526 | while ((sitem = stk_pop(parsestk))) |
d7482997 |
1527 | sfree(sitem); |
d7482997 |
1528 | error(err_missingrbrace, &t.pos); |
1529 | } |
1530 | stk_free(parsestk); |
7136a6c7 |
1531 | prev_para_type = par.type; |
176f9548 |
1532 | /* |
1533 | * Before we add the paragraph to the output list, we |
1534 | * should check that there was any text in it at all; there |
1535 | * might not be if (for example) the paragraph contained |
1536 | * nothing but an unrecognised command sequence, and if we |
1537 | * put an empty paragraph on the list it may confuse the |
1538 | * back ends later on. |
1539 | */ |
1540 | if (par.words) { |
1541 | addpara(par, ret); |
176f9548 |
1542 | } |
7136a6c7 |
1543 | if (t.type == tok_eof) |
1544 | already = TRUE; |
1545 | } |
1546 | |
1547 | if (stk_top(crossparastk)) { |
1548 | void *p; |
1549 | |
1550 | error(err_missingrbrace2, &t.pos); |
1551 | while ((p = stk_pop(crossparastk))) |
1552 | sfree(p); |
d7482997 |
1553 | } |
677e18a2 |
1554 | |
1555 | /* |
1556 | * We break to here rather than returning, because otherwise |
1557 | * this cleanup doesn't happen. |
1558 | */ |
d7482997 |
1559 | dtor(t); |
7136a6c7 |
1560 | |
1561 | stk_free(crossparastk); |
d7482997 |
1562 | } |
1563 | |
1564 | paragraph *read_input(input *in, indexdata *idx) { |
1565 | paragraph *head = NULL; |
1566 | paragraph **hptr = &head; |
41848b3e |
1567 | tree234 *macros; |
1568 | |
1569 | macros = newtree234(macrocmp); |
d7482997 |
1570 | |
1571 | while (in->currindex < in->nfiles) { |
1572 | in->currfp = fopen(in->filenames[in->currindex], "r"); |
1573 | if (in->currfp) { |
1574 | setpos(in, in->filenames[in->currindex]); |
e34ba5c3 |
1575 | in->charset = in->defcharset; |
1576 | in->csstate = charset_init_state; |
e4ea58f8 |
1577 | in->wcpos = in->nwc = 0; |
1578 | in->pushback_chars = NULL; |
ba0fe3ec |
1579 | if (strcmp(in->filenames[in->currindex] + |
1580 | strlen(in->filenames[in->currindex]) - 4, ".afm") == 0) |
1581 | read_afm_file(in); |
44407fea |
1582 | else if (strcmp(in->filenames[in->currindex] + |
1583 | strlen(in->filenames[in->currindex]) - 4, ".pfa") == 0) |
1584 | read_pfa_file(in); |
ba0fe3ec |
1585 | else |
1586 | read_file(&hptr, in, idx, macros); |
d7482997 |
1587 | } |
1588 | in->currindex++; |
1589 | } |
1590 | |
41848b3e |
1591 | macrocleanup(macros); |
1592 | |
d7482997 |
1593 | return head; |
1594 | } |