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