Annotation of texi2mdoc/util.c, Revision 1.21
1.21 ! kristaps 1: /* $Id: util.c,v 1.20 2015/03/01 00:25:08 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/mman.h>
18: #include <sys/stat.h>
19:
20: #include <assert.h>
21: #include <ctype.h>
22: #include <fcntl.h>
23: #include <limits.h>
24: #include <stdarg.h>
25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <time.h>
29: #include <unistd.h>
30:
31: #include "extern.h"
32:
33: /*
34: * Unmap the top-most file in the stack of files currently opened (that
35: * is, nested calls to parsefile()).
36: */
37: void
38: texifilepop(struct texi *p)
39: {
40: struct texifile *f;
41:
42: assert(p->filepos > 0);
43: f = &p->files[--p->filepos];
1.14 kristaps 44: free(f->map);
1.1 kristaps 45: }
46:
1.7 kristaps 47: static void
48: teximacrofree(struct teximacro *p)
49: {
50: size_t i;
51:
52: for (i = 0; i < p->argsz; i++)
53: free(p->args[i]);
54:
55: free(p->args);
56: free(p->key);
57: free(p->value);
58: }
59:
60: static void
61: texivaluefree(struct texivalue *p)
62: {
63:
64: free(p->key);
65: free(p->value);
66: }
67:
1.1 kristaps 68: /*
69: * Unmap all files that we're currently using and free all resources
70: * that we've allocated during the parse.
71: * The utility should exit(...) after this is called.
72: */
73: void
74: texiexit(struct texi *p)
75: {
76: size_t i;
77:
78: /* Make sure we're newline-terminated. */
79: if (p->outcol)
1.20 kristaps 80: fputc('\n', p->outfile);
81: if (NULL != p->chapters)
82: teximdocclose(p, 1);
1.1 kristaps 83:
84: /* Unmap all files. */
85: while (p->filepos > 0)
86: texifilepop(p);
87:
1.7 kristaps 88: for (i = 0; i < p->macrosz; i++)
89: teximacrofree(&p->macros[i]);
1.1 kristaps 90: for (i = 0; i < p->dirsz; i++)
91: free(p->dirs[i]);
1.4 kristaps 92: for (i = 0; i < p->indexsz; i++)
93: free(p->indexs[i]);
1.7 kristaps 94: for (i = 0; i < p->valsz; i++)
95: texivaluefree(&p->vals[i]);
1.4 kristaps 96:
1.7 kristaps 97: free(p->macros);
1.1 kristaps 98: free(p->vals);
1.4 kristaps 99: free(p->indexs);
1.1 kristaps 100: free(p->dirs);
101: free(p->subtitle);
102: free(p->title);
103: }
104:
105: /*
106: * Fatal error: unmap all files and exit.
107: * The "errstring" is passed to perror(3).
108: */
109: void
110: texiabort(struct texi *p, const char *errstring)
111: {
112:
113: perror(errstring);
114: texiexit(p);
115: exit(EXIT_FAILURE);
116: }
117:
118: /*
119: * Print a generic warning message (to stderr) tied to our current
120: * location in the parse sequence.
121: */
122: void
123: texiwarn(const struct texi *p, const char *fmt, ...)
124: {
1.15 kristaps 125: va_list ap;
126: const struct texifile *f;
127:
128: f = &p->files[p->filepos - 1];
129:
130: if (f->insplice)
131: fprintf(stderr, "%s:%zu:%zu (%zuB left in splice): "
132: "warning: ", f->name, f->line + 1,
133: f->col + 1, f->insplice);
134: else
135: fprintf(stderr, "%s:%zu:%zu: warning: ",
136: f->name, f->line + 1, f->col + 1);
1.1 kristaps 137:
138: va_start(ap, fmt);
139: vfprintf(stderr, fmt, ap);
140: va_end(ap);
141: fputc('\n', stderr);
142: }
143:
144: /*
145: * Print an error message (to stderr) tied to our current location in
146: * the parse sequence, invoke texiexit(), then die.
147: */
148: void
149: texierr(struct texi *p, const char *fmt, ...)
150: {
1.15 kristaps 151: va_list ap;
152: struct texifile *f;
153:
154: f = &p->files[p->filepos - 1];
155:
156: if (f->insplice)
157: fprintf(stderr, "%s:%zu:%zu: (%zuB left in splice): "
158: "error: ", f->name, f->line + 1,
159: f->col + 1, f->insplice);
160: else
161: fprintf(stderr, "%s:%zu:%zu: error: ",
162: f->name, f->line + 1, f->col + 1);
1.1 kristaps 163:
164: va_start(ap, fmt);
165: vfprintf(stderr, fmt, ap);
166: va_end(ap);
167: fputc('\n', stderr);
168: texiexit(p);
169: exit(EXIT_FAILURE);
170: }
171:
172: /*
173: * Put a single data character to the output if we're not ignoring.
1.13 kristaps 174: * Escape starting a line with a control character and slashes.
1.1 kristaps 175: */
176: void
177: texiputchar(struct texi *p, char c)
178: {
179:
180: if (p->ign)
181: return;
182: if ('.' == c && 0 == p->outcol)
1.20 kristaps 183: fputs("\\&", p->outfile);
1.10 kristaps 184: if ('\'' == c && 0 == p->outcol)
1.20 kristaps 185: fputs("\\&", p->outfile);
1.1 kristaps 186:
1.20 kristaps 187: fputc(c, p->outfile);
1.13 kristaps 188: if ('\\' == c)
1.20 kristaps 189: fputc('e', p->outfile);
1.1 kristaps 190: p->seenvs = 0;
191: if ('\n' == c) {
192: p->outcol = 0;
193: p->seenws = 0;
194: } else
195: p->outcol++;
196: }
197:
198: /*
1.13 kristaps 199: * Put an opaque series of characters.
200: * Characters starting a line with a control character are escaped, but
201: * that's it, so don't use this for non-controlled sequences of text.
1.1 kristaps 202: */
203: void
204: texiputchars(struct texi *p, const char *s)
205: {
206:
1.13 kristaps 207: if (p->ign)
208: return;
209: if ('.' == *s && 0 == p->outcol)
1.20 kristaps 210: fputs("\\&", p->outfile);
1.13 kristaps 211: if ('\'' == *s && 0 == p->outcol)
1.20 kristaps 212: fputs("\\&", p->outfile);
213: p->outcol += fputs(s, p->outfile);
1.13 kristaps 214: p->seenvs = 0;
1.9 kristaps 215: }
216:
217: /*
218: * This puts all characters onto the output stream but makes sure to
219: * escape mdoc(7) slashes.
1.14 kristaps 220: * FIXME: useless.
1.9 kristaps 221: */
222: void
1.14 kristaps 223: texiputbuf(struct texi *p, size_t start, size_t end)
1.9 kristaps 224: {
225:
1.14 kristaps 226: for ( ; start < end; start++)
227: texiputchar(p, BUF(p)[start]);
1.1 kristaps 228: }
229:
230: /*
231: * Close an mdoc(7) macro opened with teximacroopen().
232: * If there are no more macros on the line, prints a newline.
233: */
234: void
235: teximacroclose(struct texi *p)
236: {
237:
238: if (p->ign)
239: return;
240:
241: if (0 == --p->outmacro) {
1.20 kristaps 242: fputc('\n', p->outfile);
1.1 kristaps 243: p->outcol = p->seenws = 0;
244: }
245: }
246:
247: /*
248: * Open a mdoc(7) macro.
249: * This is used for line macros, e.g., Qq [foo bar baz].
250: * It can be invoked for nested macros, e.g., Qq Li foo .
251: * TODO: flush-right punctuation (e.g., parenthesis).
252: */
253: void
254: teximacroopen(struct texi *p, const char *s)
255: {
256: int rc;
257:
258: if (p->ign)
259: return;
260:
261: if (p->outcol && 0 == p->outmacro) {
1.20 kristaps 262: fputc('\n', p->outfile);
1.1 kristaps 263: p->outcol = 0;
264: }
265:
266: if (0 == p->outmacro)
1.20 kristaps 267: fputc('.', p->outfile);
1.1 kristaps 268: else
1.20 kristaps 269: fputc(' ', p->outfile);
1.1 kristaps 270:
1.20 kristaps 271: if (EOF != (rc = fputs(s, p->outfile)))
1.1 kristaps 272: p->outcol += rc;
273:
1.20 kristaps 274: fputc(' ', p->outfile);
1.1 kristaps 275: p->outcol++;
276: p->outmacro++;
277: p->seenws = 0;
278: }
279:
280: /*
281: * Put a stadnalone mdoc(7) command with the trailing newline.
282: */
283: void
284: teximacro(struct texi *p, const char *s)
285: {
286:
287: if (p->ign)
288: return;
289:
290: if (p->outmacro)
291: texierr(p, "\"%s\" in open line scope!?", s);
292: if (p->literal)
293: texierr(p, "\"%s\" in a literal scope!?", s);
294:
295: if (p->outcol)
1.20 kristaps 296: fputc('\n', p->outfile);
1.1 kristaps 297:
1.20 kristaps 298: fputc('.', p->outfile);
299: fputs(s, p->outfile);
300: fputc('\n', p->outfile);
1.1 kristaps 301: p->outcol = p->seenws = 0;
302: }
303:
304: /*
305: * Introduce vertical space during normal (non-macro) input.
306: */
307: void
308: texivspace(struct texi *p)
309: {
310:
1.5 kristaps 311: if (p->seenvs || TEXILIST_TABLE == p->list)
1.1 kristaps 312: return;
313: teximacro(p, "Pp");
314: p->seenvs = 1;
315: }
316:
317: /*
318: * Advance by a single byte in the input stream, adjusting our location
319: * in the current input file.
320: */
321: void
1.14 kristaps 322: advance(struct texi *p, size_t *pos)
1.1 kristaps 323: {
1.15 kristaps 324: struct texifile *f;
1.1 kristaps 325:
1.15 kristaps 326: f = &p->files[p->filepos - 1];
327:
328: if (0 == f->insplice) {
329: if ('\n' == BUF(p)[*pos]) {
330: f->line++;
331: f->col = 0;
332: } else
333: f->col++;
1.17 kristaps 334: } else {
1.15 kristaps 335: --f->insplice;
1.17 kristaps 336: if (0 == f->insplice)
337: f->depth = 0;
338: }
1.1 kristaps 339:
340: (*pos)++;
341: }
342:
343: /*
344: * It's common to wait punctuation to float on the right side of macro
345: * lines in mdoc(7), e.g., ".Em hello ) ."
346: * This function does so, and should be called before teximacroclose().
347: * It will detect that it's the last in the nested macros and
348: * appropriately flush-left punctuation alongside the macro.
349: */
350: void
1.14 kristaps 351: texipunctuate(struct texi *p, size_t *pos)
1.1 kristaps 352: {
353: size_t start, end;
354:
355: if (1 != p->outmacro)
356: return;
357:
1.14 kristaps 358: for (start = end = *pos; end < BUFSZ(p); end++) {
359: switch (BUF(p)[end]) {
1.1 kristaps 360: case (','):
361: case (')'):
362: case ('.'):
363: case ('"'):
364: case (':'):
365: case ('!'):
366: case ('?'):
367: continue;
368: default:
369: break;
370: }
371: break;
372: }
373: if (end == *pos)
374: return;
1.14 kristaps 375: if (end + 1 == BUFSZ(p) || ' ' == BUF(p)[end] ||
376: '\n' == BUF(p)[end]) {
1.1 kristaps 377: for ( ; start < end; start++) {
378: texiputchar(p, ' ');
1.14 kristaps 379: texiputchar(p, BUF(p)[start]);
380: advance(p, pos);
1.1 kristaps 381: }
382: }
383: }
384:
385: /*
386: * Advance to the next non-whitespace word in the input stream.
387: * If we're in literal mode, then print all of the whitespace as we're
388: * doing so.
389: */
390: static size_t
1.14 kristaps 391: advancenext(struct texi *p, size_t *pos)
1.1 kristaps 392: {
393:
394: if (p->literal) {
1.14 kristaps 395: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos])) {
396: texiputchar(p, BUF(p)[*pos]);
397: advance(p, pos);
1.1 kristaps 398: }
399: return(*pos);
400: }
401:
1.14 kristaps 402: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos])) {
1.1 kristaps 403: p->seenws = 1;
404: /*
405: * If it looks like we've printed a double-line, then
406: * output a paragraph.
407: * FIXME: this is stupid.
408: */
1.14 kristaps 409: if (*pos && '\n' == BUF(p)[*pos] && '\n' == BUF(p)[*pos - 1])
1.1 kristaps 410: texivspace(p);
1.14 kristaps 411: advance(p, pos);
1.1 kristaps 412: }
413: return(*pos);
414: }
415:
416: /*
417: * Advance to the EOLN in the input stream.
418: * NOTE: THIS SHOULD NOT BE CALLED ON BLANK TEXT, as it will read up to
419: * the @\n.
420: */
421: size_t
1.14 kristaps 422: advanceeoln(struct texi *p, size_t *pos, int consumenl)
1.1 kristaps 423: {
424:
1.14 kristaps 425: while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
426: advance(p, pos);
427: if (*pos < BUFSZ(p) && consumenl)
428: advance(p, pos);
1.1 kristaps 429: return(*pos);
430: }
431:
432: /*
433: * Advance to position "end", which is an absolute position in the
434: * current buffer greater than or equal to the current position.
435: */
436: void
1.14 kristaps 437: advanceto(struct texi *p, size_t *pos, size_t end)
1.1 kristaps 438: {
439:
440: assert(*pos <= end);
441: while (*pos < end)
1.14 kristaps 442: advance(p, pos);
1.1 kristaps 443: }
444:
1.7 kristaps 445: static void
1.17 kristaps 446: texiexecmacro(struct texi *p, struct teximacro *m, size_t sv, size_t *pos)
1.7 kristaps 447: {
1.11 kristaps 448: size_t valsz, realsz, aasz, asz,
449: ssz, i, j, k, start, end;
450: char *val;
451: char **args;
452: const char *cp;
1.7 kristaps 453:
1.17 kristaps 454: /* Disregard empty macros. */
455: if (0 == (valsz = realsz = strlen(m->value)))
456: return;
457:
458: /*
459: * This is important: it protect us from macros that invoke more
460: * macros, possibly going on infinitely.
461: * We use "sv" instead of the current position because we might
462: * be invoked at the end of the macro (i.e., insplice == 0).
463: * The "sv" value was initialised at the start of the macro.
464: */
465: if (sv > 0)
466: if (++p->files[p->filepos].depth > 64)
467: texierr(p, "maximium recursive depth");
468:
1.14 kristaps 469: args = argparse(p, pos, &asz, m->argsz);
1.7 kristaps 470: if (asz != m->argsz)
471: texiwarn(p, "invalid macro argument length");
472: aasz = asz < m->argsz ? asz : m->argsz;
473:
474: if (0 == aasz) {
1.21 ! kristaps 475: texisplice(p, m->value, valsz, *pos);
1.7 kristaps 476: return;
477: }
478:
479: val = strdup(m->value);
480:
481: for (i = j = 0; i < realsz; i++) {
482: /* Parse blindly til the backslash delimiter. */
483: if ('\\' != m->value[i]) {
484: val[j++] = m->value[i];
485: val[j] = '\0';
486: continue;
487: } else if (i == realsz - 1)
488: texierr(p, "trailing argument name delimiter");
489:
490: /* Double-backslash is escaped. */
491: if ('\\' == m->value[i + 1]) {
492: val[j++] = m->value[i++];
493: val[j] = '\0';
494: continue;
495: }
496:
497: assert('\\' == m->value[i] && i < realsz - 1);
498:
499: /* Parse to terminating delimiter. */
500: /* FIXME: embedded, escaped delimiters? */
501: for (start = end = i + 1; end < realsz; end++)
502: if ('\\' == m->value[end])
503: break;
504: if (end == realsz)
505: texierr(p, "unterminated argument name");
506:
507: for (k = 0; k < aasz; k++) {
508: if ((ssz = strlen(m->args[k])) != (end - start))
509: continue;
510: if (strncmp(&m->value[start], m->args[k], ssz))
511: continue;
512: break;
513: }
514:
515: /*
516: * Argument didn't exist in argument table.
1.14 kristaps 517: * Just ignore it.
1.7 kristaps 518: */
519: if (k == aasz) {
1.14 kristaps 520: i = end;
1.7 kristaps 521: continue;
522: }
523:
524: if (strlen(args[k]) > ssz) {
525: valsz += strlen(args[k]);
526: val = realloc(val, valsz + 1);
527: if (NULL == val)
528: texiabort(p, NULL);
529: }
530:
1.11 kristaps 531: for (cp = args[k]; '\0' != *cp; cp++)
532: val[j++] = *cp;
533:
534: val[j] = '\0';
1.7 kristaps 535: i = end;
536: }
537:
1.21 ! kristaps 538: texisplice(p, val, strlen(val), *pos);
1.7 kristaps 539:
540: for (i = 0; i < asz; i++)
541: free(args[i]);
542: free(args);
543: free(val);
544: }
545:
1.1 kristaps 546: /*
547: * Output a free-form word in the input stream, progressing to the next
548: * command or white-space.
549: * This also will advance the input stream.
550: */
551: static void
1.14 kristaps 552: parseword(struct texi *p, size_t *pos, char extra)
1.1 kristaps 553: {
554:
555: if (p->seenws && 0 == p->outmacro &&
556: p->outcol > 72 && 0 == p->literal)
557: texiputchar(p, '\n');
558: /* FIXME: abstract this: we use it elsewhere. */
559: if (p->seenws && p->outcol && 0 == p->literal)
560: texiputchar(p, ' ');
561:
562: p->seenws = 0;
563:
1.14 kristaps 564: while (*pos < BUFSZ(p) && ! ismspace(BUF(p)[*pos])) {
565: switch (BUF(p)[*pos]) {
1.1 kristaps 566: case ('@'):
567: case ('}'):
568: case ('{'):
569: return;
570: }
1.14 kristaps 571: if ('\0' != extra && BUF(p)[*pos] == extra)
1.1 kristaps 572: return;
1.14 kristaps 573: if (*pos < BUFSZ(p) - 1 &&
574: '`' == BUF(p)[*pos] &&
575: '`' == BUF(p)[*pos + 1]) {
1.1 kristaps 576: texiputchars(p, "\\(lq");
1.14 kristaps 577: advance(p, pos);
578: } else if (*pos < BUFSZ(p) - 1 &&
579: '\'' == BUF(p)[*pos] &&
580: '\'' == BUF(p)[*pos + 1]) {
1.1 kristaps 581: texiputchars(p, "\\(rq");
1.14 kristaps 582: advance(p, pos);
1.1 kristaps 583: } else
1.14 kristaps 584: texiputchar(p, BUF(p)[*pos]);
585: advance(p, pos);
1.1 kristaps 586: }
587: }
588:
589: /*
590: * Look up the command at position "pos" in the buffer, returning it (or
591: * TEXICMD__MAX if none found) and setting "end" to be the absolute
592: * index after the command name.
593: */
594: enum texicmd
1.19 kristaps 595: texicmd(const struct texi *p, size_t pos, size_t *end, struct teximacro **macro)
1.1 kristaps 596: {
1.4 kristaps 597: size_t i, len, toksz;
1.1 kristaps 598:
1.14 kristaps 599: assert('@' == BUF(p)[pos]);
1.1 kristaps 600:
1.7 kristaps 601: if (NULL != macro)
602: *macro = NULL;
603:
1.14 kristaps 604: if ((*end = pos) == BUFSZ(p))
1.1 kristaps 605: return(TEXICMD__MAX);
1.14 kristaps 606: else if ((*end = ++pos) == BUFSZ(p))
1.1 kristaps 607: return(TEXICMD__MAX);
608:
609: /* Alphabetic commands are special. */
1.16 kristaps 610: if ( ! isalpha((unsigned char)BUF(p)[pos])) {
1.14 kristaps 611: if ((*end = pos + 1) == BUFSZ(p))
1.1 kristaps 612: return(TEXICMD__MAX);
613: for (i = 0; i < TEXICMD__MAX; i++) {
614: if (1 != texitoks[i].len)
615: continue;
1.14 kristaps 616: if (0 == strncmp(texitoks[i].tok, &BUF(p)[pos], 1))
1.1 kristaps 617: return(i);
618: }
1.14 kristaps 619: texiwarn(p, "bad command: @%c", BUF(p)[pos]);
1.1 kristaps 620: return(TEXICMD__MAX);
621: }
622:
1.4 kristaps 623: /* Scan to the end of the possible command name. */
1.14 kristaps 624: for (*end = pos; *end < BUFSZ(p) && ! ismspace(BUF(p)[*end]); (*end)++)
625: if ((*end > pos && ('@' == BUF(p)[*end] ||
626: '{' == BUF(p)[*end] || '}' == BUF(p)[*end])))
1.1 kristaps 627: break;
628:
1.4 kristaps 629: /* Look for the command. */
1.1 kristaps 630: len = *end - pos;
631: for (i = 0; i < TEXICMD__MAX; i++) {
632: if (len != texitoks[i].len)
633: continue;
1.14 kristaps 634: if (0 == strncmp(texitoks[i].tok, &BUF(p)[pos], len))
1.1 kristaps 635: return(i);
636: }
637:
1.4 kristaps 638: /* Look for it in our indices. */
639: for (i = 0; i < p->indexsz; i++) {
640: toksz = strlen(p->indexs[i]);
641: if (len != 5 + toksz)
642: continue;
1.14 kristaps 643: if (strncmp(&BUF(p)[pos], p->indexs[i], toksz))
1.4 kristaps 644: continue;
1.14 kristaps 645: if (0 == strncmp(&BUF(p)[pos + toksz], "index", 5))
1.7 kristaps 646: return(TEXICMD_USER_INDEX);
647: }
648:
649: for (i = 0; i < p->macrosz; i++) {
650: if (len != strlen(p->macros[i].key))
651: continue;
1.14 kristaps 652: if (strncmp(&BUF(p)[pos], p->macros[i].key, len))
1.7 kristaps 653: continue;
654: if (NULL != macro)
655: *macro = &p->macros[i];
656: return(TEXICMD__MAX);
1.4 kristaps 657: }
658:
1.14 kristaps 659: texiwarn(p, "bad command: @%.*s", (int)len, &BUF(p)[pos]);
1.1 kristaps 660: return(TEXICMD__MAX);
661: }
662:
663: /*
664: * Parse an argument from a bracketed command, e.g., @url{foo, baz}.
665: * Num should be set to the argument we're currently parsing, although
666: * it suffixes for it to be zero or non-zero.
667: * This will return 1 if there are more arguments, 0 otherwise.
668: * This will stop (returning 0) in the event of EOF or if we're not at a
669: * bracket for the zeroth parse.
670: */
671: int
1.14 kristaps 672: parsearg(struct texi *p, size_t *pos, size_t num)
1.1 kristaps 673: {
1.17 kristaps 674: size_t end, sv;
1.7 kristaps 675: enum texicmd cmd;
676: struct teximacro *macro;
1.1 kristaps 677:
1.14 kristaps 678: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos]))
679: advance(p, pos);
680: if (*pos == BUFSZ(p) || (0 == num && '{' != BUF(p)[*pos]))
1.1 kristaps 681: return(0);
682: if (0 == num)
1.14 kristaps 683: advance(p, pos);
1.1 kristaps 684:
1.14 kristaps 685: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
686: switch (BUF(p)[*pos]) {
1.1 kristaps 687: case (','):
1.14 kristaps 688: advance(p, pos);
1.1 kristaps 689: return(1);
690: case ('}'):
1.14 kristaps 691: advance(p, pos);
1.1 kristaps 692: return(0);
693: case ('{'):
694: if (0 == p->ign)
695: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 696: advance(p, pos);
1.1 kristaps 697: continue;
698: case ('@'):
699: break;
700: default:
1.14 kristaps 701: parseword(p, pos, ',');
1.1 kristaps 702: continue;
703: }
704:
1.17 kristaps 705: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 706: cmd = texicmd(p, *pos, &end, ¯o);
707: advanceto(p, pos, end);
1.7 kristaps 708: if (NULL != macro)
1.17 kristaps 709: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 710: if (TEXICMD__MAX == cmd)
711: continue;
712: if (NULL != texitoks[cmd].fp)
1.14 kristaps 713: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 714: }
715: return(0);
716: }
717:
718: /*
719: * Parse until the end of a bracketed statement, e.g., @foo{bar baz}.
720: * This will stop in the event of EOF or if we're not at a bracket.
721: */
722: void
1.18 kristaps 723: parsebracket(struct texi *p, size_t *pos, int dostack)
1.1 kristaps 724: {
1.18 kristaps 725: size_t end, sv, stack;
1.7 kristaps 726: enum texicmd cmd;
727: struct teximacro *macro;
1.1 kristaps 728:
1.14 kristaps 729: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos]))
730: advance(p, pos);
1.1 kristaps 731:
1.14 kristaps 732: if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.1 kristaps 733: return;
1.14 kristaps 734: advance(p, pos);
1.1 kristaps 735:
1.18 kristaps 736: stack = 0;
1.14 kristaps 737: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
738: switch (BUF(p)[*pos]) {
1.1 kristaps 739: case ('}'):
1.18 kristaps 740: if (stack > 0) {
741: stack--;
742: advance(p, pos);
743: texiputchar(p, '}');
744: continue;
745: }
1.14 kristaps 746: advance(p, pos);
1.1 kristaps 747: return;
748: case ('{'):
1.18 kristaps 749: if (dostack) {
750: stack++;
751: advance(p, pos);
752: texiputchar(p, '{');
753: continue;
754: }
1.1 kristaps 755: if (0 == p->ign)
756: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 757: advance(p, pos);
1.1 kristaps 758: continue;
759: case ('@'):
760: break;
761: default:
1.14 kristaps 762: parseword(p, pos, '\0');
1.1 kristaps 763: continue;
764: }
765:
1.17 kristaps 766: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 767: cmd = texicmd(p, *pos, &end, ¯o);
768: advanceto(p, pos, end);
1.7 kristaps 769: if (NULL != macro)
1.17 kristaps 770: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 771: if (TEXICMD__MAX == cmd)
772: continue;
773: if (NULL != texitoks[cmd].fp)
1.14 kristaps 774: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 775: }
776: }
777:
778: /*
779: * This should be invoked when we're on a macro line and want to process
780: * to the end of the current input line, doing all of our macros along
781: * the way.
782: */
783: void
1.14 kristaps 784: parseeoln(struct texi *p, size_t *pos)
1.1 kristaps 785: {
1.17 kristaps 786: size_t end, sv;
1.7 kristaps 787: enum texicmd cmd;
788: struct teximacro *macro;
1.1 kristaps 789:
1.14 kristaps 790: while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) {
791: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) {
1.1 kristaps 792: p->seenws = 1;
793: if (p->literal)
1.14 kristaps 794: texiputchar(p, BUF(p)[*pos]);
795: advance(p, pos);
1.1 kristaps 796: }
1.14 kristaps 797: switch (BUF(p)[*pos]) {
1.1 kristaps 798: case ('}'):
799: if (0 == p->ign)
800: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 801: advance(p, pos);
1.1 kristaps 802: continue;
803: case ('{'):
804: if (0 == p->ign)
805: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 806: advance(p, pos);
1.1 kristaps 807: continue;
808: case ('@'):
809: break;
810: default:
1.14 kristaps 811: parseword(p, pos, '\0');
1.1 kristaps 812: continue;
813: }
814:
1.17 kristaps 815: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 816: cmd = texicmd(p, *pos, &end, ¯o);
817: advanceto(p, pos, end);
1.7 kristaps 818: if (NULL != macro)
1.17 kristaps 819: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 820: if (TEXICMD__MAX == cmd)
821: continue;
822: if (NULL != texitoks[cmd].fp)
1.14 kristaps 823: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 824: }
1.14 kristaps 825:
826: if (*pos < BUFSZ(p) && '\n' == BUF(p)[*pos])
827: advance(p, pos);
1.19 kristaps 828: }
829:
830: /*
831: * Peek to see if there's a command after subsequent whitespace.
832: * If so, return the macro identifier.
833: * This DOES NOT work with user-defined macros.
834: */
835: enum texicmd
836: peekcmd(const struct texi *p, size_t pos)
837: {
838: size_t end;
839:
840: while (pos < BUFSZ(p) && ismspace(BUF(p)[pos]))
841: pos++;
842: if (pos == BUFSZ(p) || '@' != BUF(p)[pos])
843: return(TEXICMD__MAX);
844: return(texicmd(p, pos, &end, NULL));
1.1 kristaps 845: }
846:
847: /*
848: * Parse a single word or command.
849: * This will return immediately at the EOF.
850: */
1.14 kristaps 851: static void
852: parsesingle(struct texi *p, size_t *pos)
1.1 kristaps 853: {
1.17 kristaps 854: size_t end, sv;
1.7 kristaps 855: enum texicmd cmd;
856: struct teximacro *macro;
1.1 kristaps 857:
1.14 kristaps 858: if ((*pos = advancenext(p, pos)) >= BUFSZ(p))
1.1 kristaps 859: return;
860:
1.14 kristaps 861: switch (BUF(p)[*pos]) {
1.1 kristaps 862: case ('}'):
863: if (0 == p->ign)
864: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 865: advance(p, pos);
1.1 kristaps 866: return;
867: case ('{'):
868: if (0 == p->ign)
869: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 870: advance(p, pos);
1.1 kristaps 871: return;
872: case ('@'):
873: break;
874: default:
1.14 kristaps 875: parseword(p, pos, '\0');
1.1 kristaps 876: return;
877: }
878:
1.17 kristaps 879: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 880: cmd = texicmd(p, *pos, &end, ¯o);
881: advanceto(p, pos, end);
1.7 kristaps 882: if (NULL != macro)
1.17 kristaps 883: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 884: if (TEXICMD__MAX == cmd)
885: return;
886: if (NULL != texitoks[cmd].fp)
1.14 kristaps 887: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 888: }
889:
890: /*
891: * This is used in the @deffn type of command.
892: * These have an arbitrary number of line arguments; however, these
893: * arguments may or may not be surrounded by brackets.
894: * In this function, we parse each one as either a bracketed or
895: * non-bracketed argument, returning 0 when we've reached the end of
896: * line or 1 otherwise.
897: */
898: int
1.14 kristaps 899: parselinearg(struct texi *p, size_t *pos)
1.1 kristaps 900: {
901:
1.14 kristaps 902: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) {
1.1 kristaps 903: p->seenws = 1;
1.14 kristaps 904: advance(p, pos);
1.1 kristaps 905: }
906:
1.14 kristaps 907: if (*pos < BUFSZ(p) && '{' == BUF(p)[*pos])
1.18 kristaps 908: parsebracket(p, pos, 0);
1.14 kristaps 909: else if (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
910: parsesingle(p, pos);
1.1 kristaps 911: else
912: return(0);
913:
914: return(1);
915: }
916:
917: /*
918: * Parse til the end of the buffer.
919: */
1.14 kristaps 920: static void
921: parseeof(struct texi *p)
1.1 kristaps 922: {
923: size_t pos;
924:
1.14 kristaps 925: for (pos = 0; pos < BUFSZ(p); )
926: parsesingle(p, &pos);
1.1 kristaps 927: }
928:
1.8 kristaps 929: void
1.21 ! kristaps 930: texisplice(struct texi *p, const char *buf, size_t sz, size_t pos)
1.8 kristaps 931: {
1.14 kristaps 932: char *cp;
933: struct texifile *f;
1.8 kristaps 934:
1.14 kristaps 935: assert(p->filepos > 0);
936: f = &p->files[p->filepos - 1];
1.8 kristaps 937:
1.14 kristaps 938: if (f->mapsz + sz > f->mapmaxsz) {
939: f->mapmaxsz = f->mapsz + sz + 1024;
940: cp = realloc(f->map, f->mapmaxsz);
941: if (NULL == cp)
942: texiabort(p, NULL);
943: f->map = cp;
944: }
1.8 kristaps 945:
1.15 kristaps 946: f->insplice += sz;
1.21 ! kristaps 947: memmove(f->map + pos + sz, f->map + pos, f->mapsz - pos);
! 948: memcpy(f->map + pos, buf, sz);
1.14 kristaps 949: f->mapsz += sz;
1.8 kristaps 950: }
951:
952: /*
1.1 kristaps 953: * Parse a block sequence until we have the "@end endtoken" command
954: * invocation.
955: * This will return immediately at EOF.
956: */
957: void
1.14 kristaps 958: parseto(struct texi *p, size_t *pos, const char *endtoken)
1.1 kristaps 959: {
1.17 kristaps 960: size_t end, sv;
1.7 kristaps 961: enum texicmd cmd;
962: size_t endtoksz;
963: struct teximacro *macro;
1.1 kristaps 964:
965: endtoksz = strlen(endtoken);
966: assert(endtoksz > 0);
967:
1.14 kristaps 968: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
969: switch (BUF(p)[*pos]) {
1.1 kristaps 970: case ('}'):
971: if (0 == p->ign)
972: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 973: advance(p, pos);
1.1 kristaps 974: continue;
975: case ('{'):
976: if (0 == p->ign)
977: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 978: advance(p, pos);
1.1 kristaps 979: continue;
980: case ('@'):
981: break;
982: default:
1.14 kristaps 983: parseword(p, pos, '\0');
1.1 kristaps 984: continue;
985: }
986:
1.17 kristaps 987: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 988: cmd = texicmd(p, *pos, &end, ¯o);
989: advanceto(p, pos, end);
1.1 kristaps 990: if (TEXICMD_END == cmd) {
1.14 kristaps 991: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
992: advance(p, pos);
1.1 kristaps 993: /*
994: * FIXME: check the full word, not just its
995: * initial substring!
996: */
1.14 kristaps 997: if (BUFSZ(p) - *pos >= endtoksz && 0 == strncmp
998: (&BUF(p)[*pos], endtoken, endtoksz)) {
999: advanceeoln(p, pos, 0);
1.1 kristaps 1000: break;
1001: }
1002: if (0 == p->ign)
1003: texiwarn(p, "unexpected \"end\"");
1.14 kristaps 1004: advanceeoln(p, pos, 0);
1.1 kristaps 1005: continue;
1.7 kristaps 1006: }
1007: if (NULL != macro)
1.17 kristaps 1008: texiexecmacro(p, macro, sv, pos);
1.7 kristaps 1009: if (TEXICMD__MAX == cmd)
1010: continue;
1011: if (NULL != texitoks[cmd].fp)
1.14 kristaps 1012: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 1013: }
1014: }
1015:
1016: /*
1.12 kristaps 1017: * Like parsefile() but used for reading from stdandard input.
1018: * This can only be called for the first file!
1019: */
1020: void
1021: parsestdin(struct texi *p)
1022: {
1023: struct texifile *f;
1024: ssize_t ssz;
1025:
1026: assert(0 == p->filepos);
1027: f = &p->files[p->filepos];
1028: memset(f, 0, sizeof(struct texifile));
1029:
1030: f->type = TEXISRC_STDIN;
1031: f->name = "<stdin>";
1032:
1.14 kristaps 1033: for (f->mapsz = 0; ; f->mapsz += (size_t)ssz) {
1034: if (f->mapsz == f->mapmaxsz) {
1035: if (f->mapmaxsz == (1U << 31))
1.12 kristaps 1036: texierr(p, "stdin buffer too long");
1.14 kristaps 1037: f->mapmaxsz = f->mapmaxsz > 65536 / 2 ?
1038: 2 * f->mapmaxsz : 65536;
1039: f->map = realloc(f->map, f->mapmaxsz);
1.12 kristaps 1040: if (NULL == f->map)
1041: texiabort(p, NULL);
1042: }
1.14 kristaps 1043: ssz = read(STDIN_FILENO, f->map +
1044: (int)f->mapsz, f->mapmaxsz - f->mapsz);
1.12 kristaps 1045: if (0 == ssz)
1046: break;
1047: else if (-1 == ssz)
1048: texiabort(p, NULL);
1049: }
1050:
1051: p->filepos++;
1.14 kristaps 1052: parseeof(p);
1.12 kristaps 1053: texifilepop(p);
1054: }
1055:
1056: /*
1.1 kristaps 1057: * Memory-map the file "fname" and begin parsing it unless "parse" is
1058: * zero, in which case we just dump the file to stdout (making sure it
1059: * doesn't trip up mdoc(7) along the way).
1060: * This can be called in a nested context.
1061: */
1062: void
1063: parsefile(struct texi *p, const char *fname, int parse)
1064: {
1065: struct texifile *f;
1066: int fd;
1067: struct stat st;
1068: size_t i;
1.14 kristaps 1069: char *map;
1.1 kristaps 1070:
1.5 kristaps 1071: if (64 == p->filepos)
1.6 kristaps 1072: texierr(p, "too many open files");
1.1 kristaps 1073: f = &p->files[p->filepos];
1074: memset(f, 0, sizeof(struct texifile));
1075:
1.12 kristaps 1076: f->type = TEXISRC_FILE;
1.1 kristaps 1077: f->name = fname;
1078: if (-1 == (fd = open(fname, O_RDONLY, 0))) {
1079: texiabort(p, fname);
1080: } else if (-1 == fstat(fd, &st)) {
1081: close(fd);
1082: texiabort(p, fname);
1083: }
1084:
1.14 kristaps 1085: f->mapsz = f->mapmaxsz = st.st_size;
1086: map = mmap(NULL, f->mapsz,
1.1 kristaps 1087: PROT_READ, MAP_SHARED, fd, 0);
1088: close(fd);
1089:
1.14 kristaps 1090: if (MAP_FAILED == map)
1.1 kristaps 1091: texiabort(p, fname);
1092:
1093: if ( ! parse) {
1.13 kristaps 1094: for (i = 0; i < f->mapsz; i++)
1.14 kristaps 1095: texiputchar(p, map[i]);
1.13 kristaps 1096: if (p->outcol)
1097: texiputchar(p, '\n');
1.14 kristaps 1098: munmap(map, f->mapsz);
1099: return;
1100: }
1101:
1102: p->filepos++;
1103: f->map = malloc(f->mapsz);
1104: memcpy(f->map, map, f->mapsz);
1105: munmap(map, f->mapsz);
1106: parseeof(p);
1.1 kristaps 1107: texifilepop(p);
1108: }
1109:
1.2 kristaps 1110: /*
1111: * Look up the value to a stored pair's value starting in "buf" from
1112: * start to end.
1113: * Return the pointer to the value memory, which can be NULL if the
1114: * pointer key does not exist.
1115: * The pointer can point to NULL if the value has been unset.
1116: */
1117: static char **
1.14 kristaps 1118: valuequery(const struct texi *p, size_t start, size_t end)
1.2 kristaps 1119: {
1120: size_t i, sz, len;
1121:
1122: assert(end >= start);
1123: /* Ignore zero-length. */
1124: if (0 == (len = (end - start)))
1125: return(NULL);
1126: for (i = 0; i < p->valsz; i++) {
1127: sz = strlen(p->vals[i].key);
1128: if (sz != len)
1129: continue;
1.14 kristaps 1130: if (0 == strncmp(p->vals[i].key, &BUF(p)[start], len))
1.2 kristaps 1131: return(&p->vals[i].value);
1132: }
1133: return(NULL);
1134: }
1135:
1136: /*
1137: * Parse a key until the end of line, e.g., @clear foo\n, and return the
1138: * pointer to its value via valuequery().
1139: */
1140: static char **
1.14 kristaps 1141: valuelquery(struct texi *p, size_t *pos)
1.2 kristaps 1142: {
1143: size_t start, end;
1144: char **ret;
1145:
1.14 kristaps 1146: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1147: advance(p, pos);
1148: if (*pos == BUFSZ(p))
1.2 kristaps 1149: return(NULL);
1.14 kristaps 1150: for (start = end = *pos; end < BUFSZ(p); end++)
1151: if ('\n' == BUF(p)[end])
1.2 kristaps 1152: break;
1.14 kristaps 1153: advanceto(p, pos, end);
1154: if (*pos < BUFSZ(p)) {
1155: assert('\n' == BUF(p)[*pos]);
1156: advance(p, pos);
1.2 kristaps 1157: }
1.14 kristaps 1158: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1159: return(NULL);
1160: return(ret);
1161: }
1162:
1163: void
1.14 kristaps 1164: valuelclear(struct texi *p, size_t *pos)
1.2 kristaps 1165: {
1166: char **ret;
1167:
1.14 kristaps 1168: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1169: return;
1170: free(*ret);
1171: *ret = NULL;
1172: }
1173:
1174: const char *
1.14 kristaps 1175: valuellookup(struct texi *p, size_t *pos)
1.2 kristaps 1176: {
1177: char **ret;
1178:
1.14 kristaps 1179: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1180: return(NULL);
1181: return(*ret);
1182: }
1183:
1184: /*
1185: * Parse a key from a bracketed string, e.g., @value{foo}, and return
1186: * the pointer to its value.
1187: * If the returned pointer is NULL, either there was no string within
1188: * the brackets (or no brackets), or the value was not found, or the
1189: * value had previously been unset.
1190: */
1191: const char *
1.14 kristaps 1192: valueblookup(struct texi *p, size_t *pos)
1.2 kristaps 1193: {
1194: size_t start, end;
1195: char **ret;
1196:
1.14 kristaps 1197: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1198: advance(p, pos);
1199: if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.2 kristaps 1200: return(NULL);
1.14 kristaps 1201: advance(p, pos);
1202: for (start = end = *pos; end < BUFSZ(p); end++)
1203: if ('}' == BUF(p)[end])
1.2 kristaps 1204: break;
1.14 kristaps 1205: advanceto(p, pos, end);
1206: if (*pos < BUFSZ(p)) {
1207: assert('}' == BUF(p)[*pos]);
1208: advance(p, pos);
1.2 kristaps 1209: }
1.14 kristaps 1210: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1211: return(NULL);
1212: return(*ret);
1213: }
1214:
1215: void
1216: valueadd(struct texi *p, char *key, char *val)
1217: {
1218: size_t i;
1219:
1220: assert(NULL != key);
1221: assert(NULL != val);
1222:
1223: for (i = 0; i < p->valsz; i++)
1224: if (0 == strcmp(p->vals[i].key, key))
1225: break;
1226:
1227: if (i < p->valsz) {
1228: free(key);
1229: free(p->vals[i].value);
1230: p->vals[i].value = val;
1231: } else {
1.4 kristaps 1232: /* FIXME: reallocarray() */
1.2 kristaps 1233: p->vals = realloc(p->vals,
1234: (p->valsz + 1) *
1235: sizeof(struct texivalue));
1.4 kristaps 1236: if (NULL == p->vals)
1237: texiabort(p, NULL);
1.2 kristaps 1238: p->vals[p->valsz].key = key;
1239: p->vals[p->valsz].value = val;
1240: p->valsz++;
1241: }
1.7 kristaps 1242: }
1243:
1244: /*
1245: * Take the arguments to a macro, e.g., @foo{bar, baz, xyzzy} (or the
1246: * declaration form, @macro foo {arg1, ...}) and textually convert it to
1247: * an array of arguments of size "argsz".
1248: * These need to be freed individually and as a whole.
1249: * NOTE: this will puke on @, or @} macros, which can trick it into
1250: * stopping argument parsing earlier.
1251: * Ergo, textual: this doesn't interpret the arguments in any way.
1252: */
1253: char **
1.14 kristaps 1254: argparse(struct texi *p, size_t *pos, size_t *argsz, size_t hint)
1.7 kristaps 1255: {
1256: char **args;
1257: size_t start, end, stack;
1258:
1.14 kristaps 1259: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1260: advance(p, pos);
1.7 kristaps 1261:
1262: args = NULL;
1263: *argsz = 0;
1264:
1.17 kristaps 1265: if (*pos == BUFSZ(p))
1266: return(args);
1267:
1.14 kristaps 1268: if ('{' != BUF(p)[*pos] && hint) {
1.10 kristaps 1269: /*
1270: * Special case: if we encounter an unbracketed argument
1271: * and we're being invoked with non-zero arguments
1272: * (versus being set, i.e., hint>0), then parse until
1273: * the end of line.
1274: */
1275: *argsz = 1;
1276: args = calloc(1, sizeof(char *));
1277: if (NULL == args)
1278: texiabort(p, NULL);
1279: start = *pos;
1.14 kristaps 1280: while (*pos < BUFSZ(p)) {
1281: if ('\n' == BUF(p)[*pos])
1.10 kristaps 1282: break;
1.14 kristaps 1283: advance(p, pos);
1.10 kristaps 1284: }
1285: args[0] = malloc(*pos - start + 1);
1.14 kristaps 1286: memcpy(args[0], &BUF(p)[start], *pos - start);
1.10 kristaps 1287: args[0][*pos - start] = '\0';
1.14 kristaps 1288: if (*pos < BUFSZ(p) && '\n' == BUF(p)[*pos])
1289: advance(p, pos);
1.10 kristaps 1290: return(args);
1.14 kristaps 1291: } else if ('{' != BUF(p)[*pos])
1.7 kristaps 1292: return(args);
1.17 kristaps 1293:
1294: assert('{' == BUF(p)[*pos]);
1.7 kristaps 1295:
1296: /* Parse til the closing '}', putting into the array. */
1.14 kristaps 1297: advance(p, pos);
1298: while (*pos < BUFSZ(p)) {
1299: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1300: advance(p, pos);
1.7 kristaps 1301: start = *pos;
1302: stack = 0;
1.14 kristaps 1303: while (*pos < BUFSZ(p)) {
1.7 kristaps 1304: /*
1305: * According to the manual, commas within
1306: * embedded commands are escaped.
1307: * We keep track of embedded-ness in the "stack"
1308: * state anyway, so this is free.
1309: */
1.14 kristaps 1310: if (',' == BUF(p)[*pos] && 0 == stack && 1 != hint)
1.7 kristaps 1311: break;
1.14 kristaps 1312: else if (0 == stack && '}' == BUF(p)[*pos])
1.7 kristaps 1313: break;
1.14 kristaps 1314: else if (0 != stack && '}' == BUF(p)[*pos])
1.7 kristaps 1315: stack--;
1.14 kristaps 1316: else if ('{' == BUF(p)[*pos])
1.7 kristaps 1317: stack++;
1.14 kristaps 1318: advance(p, pos);
1.7 kristaps 1319: }
1320: if (stack)
1321: texiwarn(p, "unterminated macro "
1322: "in macro arguments");
1.14 kristaps 1323: if ((end = *pos) == BUFSZ(p))
1.7 kristaps 1324: break;
1325: /* Test for zero-length '{ }'. */
1.14 kristaps 1326: if (start == end && '}' == BUF(p)[*pos] && 0 == *argsz)
1.7 kristaps 1327: break;
1328: /* FIXME: use reallocarray. */
1329: args = realloc
1330: (args, sizeof(char *) *
1331: (*argsz + 1));
1332: if (NULL == args)
1333: texiabort(p, NULL);
1334: args[*argsz] = malloc(end - start + 1);
1335: if (NULL == args[*argsz])
1336: texiabort(p, NULL);
1337: memcpy(args[*argsz],
1.14 kristaps 1338: &BUF(p)[start], end - start);
1.7 kristaps 1339: args[*argsz][end - start] = '\0';
1340: (*argsz)++;
1.14 kristaps 1341: if ('}' == BUF(p)[*pos])
1.7 kristaps 1342: break;
1.14 kristaps 1343: advance(p, pos);
1.7 kristaps 1344: }
1345:
1.14 kristaps 1346: if (*pos == BUFSZ(p))
1.7 kristaps 1347: texierr(p, "unterminated arguments");
1.14 kristaps 1348: assert('}' == BUF(p)[*pos]);
1349: advance(p, pos);
1.7 kristaps 1350: return(args);
1.2 kristaps 1351: }
1.20 kristaps 1352:
1353: /*
1354: * If we're printing chapters, then do some naviation here and then
1355: * close our outfile.
1356: * I want to call this the SEE ALSO section, but that's not really what
1357: * it is: we'll refer to the "initial" (top) node and the next and
1358: * previous chapters.
1359: */
1360: void
1361: teximdocclose(struct texi *p, int last)
1362: {
1363: char buf[PATH_MAX];
1364:
1365: if (NULL == p->chapters || 0 == p->chapnum)
1366: return;
1367:
1368: teximacro(p, "Sh INFO NAVIGATION");
1369:
1370: /* Print a reference to the "top" node. */
1371: if (p->chapnum > 1) {
1372: snprintf(buf, sizeof(buf), "node1 7");
1373: teximacroopen(p, "Xr ");
1374: texiputchars(p, buf);
1375: texiputchars(p, " ,");
1376: teximacroclose(p);
1377: }
1378:
1379: /* Print a reference to the previous node. */
1380: if (p->chapnum > 2) {
1381: snprintf(buf, sizeof(buf),
1382: "node%zu 7", p->chapnum - 1);
1383: teximacroopen(p, "Xr ");
1384: texiputchars(p, buf);
1385: if ( ! last)
1386: texiputchars(p, " ,");
1387: teximacroclose(p);
1388: }
1389:
1390: /* Print a reference to the next node. */
1391: if ( ! last) {
1392: snprintf(buf, sizeof(buf),
1393: "node%zu 7", p->chapnum + 1);
1394: teximacroopen(p, "Xr ");
1395: texiputchars(p, buf);
1396: teximacroclose(p);
1397: }
1398:
1399: fclose(p->outfile);
1400: }
1401:
1402: /*
1403: * Open a mdoc(7) context.
1404: * If we're printing chapters, then open the outfile here, too.
1405: * Otherwise just print the mdoc(7) prologue.
1406: */
1407: void
1.21 ! kristaps 1408: teximdocopen(struct texi *p, size_t *pos)
1.20 kristaps 1409: {
1410: const char *cp;
1411: time_t t;
1412: char date[32];
1413: char fname[PATH_MAX];
1414:
1415: if (NULL != p->chapters) {
1416: snprintf(fname, sizeof(fname), "%s/node%zu.7",
1417: p->chapters, ++p->chapnum);
1418: p->outfile = fopen(fname, "w");
1419: if (NULL == p->outfile)
1420: texiabort(p, fname);
1421: }
1422:
1423: /*
1424: * Here we print our standard mdoc(7) prologue.
1425: * We use the title set with @settitle for the `Nd' description
1426: * and the source document filename (the first one as invoked on
1427: * the command line) for the title.
1428: * The date is set to the current date.
1429: */
1430: t = time(NULL);
1431: strftime(date, sizeof(date), "%F", localtime(&t));
1432:
1433: teximacroopen(p, "Dd");
1434: texiputchars(p, date);
1435: teximacroclose(p);
1436: teximacroopen(p, "Dt");
1437: for (cp = p->title; '\0' != *cp; cp++)
1438: texiputchar(p, toupper((unsigned int)*cp));
1439: texiputchars(p, " 7");
1440: teximacroclose(p);
1441: teximacro(p, "Os");
1442: teximacro(p, "Sh NAME");
1443: teximacroopen(p, "Nm");
1444: for (cp = p->title; '\0' != *cp; cp++)
1445: texiputchar(p, *cp);
1446: teximacroclose(p);
1447: teximacroopen(p, "Nd");
1.21 ! kristaps 1448: /*
! 1449: * The subtitle `Nd' can consist of arbitrary macros, so paste
! 1450: * it and parse to the end of the line.
! 1451: */
! 1452: if (NULL != p->subtitle) {
! 1453: texisplice(p, p->subtitle, strlen(p->subtitle), *pos);
! 1454: parseeoln(p, pos);
! 1455: } else
1.20 kristaps 1456: texiputchars(p, "Unknown description");
1457: teximacroclose(p);
1458: p->seenvs = 1;
1459: }
1460:
CVSweb