Annotation of texi2mdoc/util.c, Revision 1.19
1.19 ! kristaps 1: /* $Id: util.c,v 1.18 2015/02/28 08:41:59 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 <getopt.h>
24: #include <libgen.h>
25: #include <limits.h>
26: #include <stdarg.h>
27: #include <stdio.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <time.h>
31: #include <unistd.h>
32:
33: #include "extern.h"
34:
35: /*
36: * Unmap the top-most file in the stack of files currently opened (that
37: * is, nested calls to parsefile()).
38: */
39: void
40: texifilepop(struct texi *p)
41: {
42: struct texifile *f;
43:
44: assert(p->filepos > 0);
45: f = &p->files[--p->filepos];
1.14 kristaps 46: free(f->map);
1.1 kristaps 47: }
48:
1.7 kristaps 49: static void
50: teximacrofree(struct teximacro *p)
51: {
52: size_t i;
53:
54: for (i = 0; i < p->argsz; i++)
55: free(p->args[i]);
56:
57: free(p->args);
58: free(p->key);
59: free(p->value);
60: }
61:
62: static void
63: texivaluefree(struct texivalue *p)
64: {
65:
66: free(p->key);
67: free(p->value);
68: }
69:
1.1 kristaps 70: /*
71: * Unmap all files that we're currently using and free all resources
72: * that we've allocated during the parse.
73: * The utility should exit(...) after this is called.
74: */
75: void
76: texiexit(struct texi *p)
77: {
78: size_t i;
79:
80: /* Make sure we're newline-terminated. */
81: if (p->outcol)
82: putchar('\n');
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)
183: fputs("\\&", stdout);
1.10 kristaps 184: if ('\'' == c && 0 == p->outcol)
185: fputs("\\&", stdout);
1.1 kristaps 186:
187: putchar(c);
1.13 kristaps 188: if ('\\' == c)
189: putchar('e');
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)
210: fputs("\\&", stdout);
211: if ('\'' == *s && 0 == p->outcol)
212: fputs("\\&", stdout);
213: p->outcol += fputs(s, stdout);
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) {
242: putchar('\n');
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) {
262: putchar('\n');
263: p->outcol = 0;
264: }
265:
266: if (0 == p->outmacro)
267: putchar('.');
268: else
269: putchar(' ');
270:
271: if (EOF != (rc = fputs(s, stdout)))
272: p->outcol += rc;
273:
274: putchar(' ');
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)
296: putchar('\n');
297:
298: putchar('.');
299: puts(s);
300: p->outcol = p->seenws = 0;
301: }
302:
303: /*
304: * Introduce vertical space during normal (non-macro) input.
305: */
306: void
307: texivspace(struct texi *p)
308: {
309:
1.5 kristaps 310: if (p->seenvs || TEXILIST_TABLE == p->list)
1.1 kristaps 311: return;
312: teximacro(p, "Pp");
313: p->seenvs = 1;
314: }
315:
316: /*
317: * Advance by a single byte in the input stream, adjusting our location
318: * in the current input file.
319: */
320: void
1.14 kristaps 321: advance(struct texi *p, size_t *pos)
1.1 kristaps 322: {
1.15 kristaps 323: struct texifile *f;
1.1 kristaps 324:
1.15 kristaps 325: f = &p->files[p->filepos - 1];
326:
327: if (0 == f->insplice) {
328: if ('\n' == BUF(p)[*pos]) {
329: f->line++;
330: f->col = 0;
331: } else
332: f->col++;
1.17 kristaps 333: } else {
1.15 kristaps 334: --f->insplice;
1.17 kristaps 335: if (0 == f->insplice)
336: f->depth = 0;
337: }
1.1 kristaps 338:
339: (*pos)++;
340: }
341:
342: /*
343: * It's common to wait punctuation to float on the right side of macro
344: * lines in mdoc(7), e.g., ".Em hello ) ."
345: * This function does so, and should be called before teximacroclose().
346: * It will detect that it's the last in the nested macros and
347: * appropriately flush-left punctuation alongside the macro.
348: */
349: void
1.14 kristaps 350: texipunctuate(struct texi *p, size_t *pos)
1.1 kristaps 351: {
352: size_t start, end;
353:
354: if (1 != p->outmacro)
355: return;
356:
1.14 kristaps 357: for (start = end = *pos; end < BUFSZ(p); end++) {
358: switch (BUF(p)[end]) {
1.1 kristaps 359: case (','):
360: case (')'):
361: case ('.'):
362: case ('"'):
363: case (':'):
364: case ('!'):
365: case ('?'):
366: continue;
367: default:
368: break;
369: }
370: break;
371: }
372: if (end == *pos)
373: return;
1.14 kristaps 374: if (end + 1 == BUFSZ(p) || ' ' == BUF(p)[end] ||
375: '\n' == BUF(p)[end]) {
1.1 kristaps 376: for ( ; start < end; start++) {
377: texiputchar(p, ' ');
1.14 kristaps 378: texiputchar(p, BUF(p)[start]);
379: advance(p, pos);
1.1 kristaps 380: }
381: }
382: }
383:
384: /*
385: * Advance to the next non-whitespace word in the input stream.
386: * If we're in literal mode, then print all of the whitespace as we're
387: * doing so.
388: */
389: static size_t
1.14 kristaps 390: advancenext(struct texi *p, size_t *pos)
1.1 kristaps 391: {
392:
393: if (p->literal) {
1.14 kristaps 394: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos])) {
395: texiputchar(p, BUF(p)[*pos]);
396: advance(p, pos);
1.1 kristaps 397: }
398: return(*pos);
399: }
400:
1.14 kristaps 401: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos])) {
1.1 kristaps 402: p->seenws = 1;
403: /*
404: * If it looks like we've printed a double-line, then
405: * output a paragraph.
406: * FIXME: this is stupid.
407: */
1.14 kristaps 408: if (*pos && '\n' == BUF(p)[*pos] && '\n' == BUF(p)[*pos - 1])
1.1 kristaps 409: texivspace(p);
1.14 kristaps 410: advance(p, pos);
1.1 kristaps 411: }
412: return(*pos);
413: }
414:
415: /*
416: * Advance to the EOLN in the input stream.
417: * NOTE: THIS SHOULD NOT BE CALLED ON BLANK TEXT, as it will read up to
418: * the @\n.
419: */
420: size_t
1.14 kristaps 421: advanceeoln(struct texi *p, size_t *pos, int consumenl)
1.1 kristaps 422: {
423:
1.14 kristaps 424: while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
425: advance(p, pos);
426: if (*pos < BUFSZ(p) && consumenl)
427: advance(p, pos);
1.1 kristaps 428: return(*pos);
429: }
430:
431: /*
432: * Advance to position "end", which is an absolute position in the
433: * current buffer greater than or equal to the current position.
434: */
435: void
1.14 kristaps 436: advanceto(struct texi *p, size_t *pos, size_t end)
1.1 kristaps 437: {
438:
439: assert(*pos <= end);
440: while (*pos < end)
1.14 kristaps 441: advance(p, pos);
1.1 kristaps 442: }
443:
1.7 kristaps 444: static void
1.17 kristaps 445: texiexecmacro(struct texi *p, struct teximacro *m, size_t sv, size_t *pos)
1.7 kristaps 446: {
1.11 kristaps 447: size_t valsz, realsz, aasz, asz,
448: ssz, i, j, k, start, end;
449: char *val;
450: char **args;
451: const char *cp;
1.7 kristaps 452:
1.17 kristaps 453: /* Disregard empty macros. */
454: if (0 == (valsz = realsz = strlen(m->value)))
455: return;
456:
457: /*
458: * This is important: it protect us from macros that invoke more
459: * macros, possibly going on infinitely.
460: * We use "sv" instead of the current position because we might
461: * be invoked at the end of the macro (i.e., insplice == 0).
462: * The "sv" value was initialised at the start of the macro.
463: */
464: if (sv > 0)
465: if (++p->files[p->filepos].depth > 64)
466: texierr(p, "maximium recursive depth");
467:
1.14 kristaps 468: args = argparse(p, pos, &asz, m->argsz);
1.7 kristaps 469: if (asz != m->argsz)
470: texiwarn(p, "invalid macro argument length");
471: aasz = asz < m->argsz ? asz : m->argsz;
472:
473: if (0 == aasz) {
1.17 kristaps 474: texisplice(p, m->value, valsz, pos);
1.7 kristaps 475: return;
476: }
477:
478: val = strdup(m->value);
479:
480: for (i = j = 0; i < realsz; i++) {
481: /* Parse blindly til the backslash delimiter. */
482: if ('\\' != m->value[i]) {
483: val[j++] = m->value[i];
484: val[j] = '\0';
485: continue;
486: } else if (i == realsz - 1)
487: texierr(p, "trailing argument name delimiter");
488:
489: /* Double-backslash is escaped. */
490: if ('\\' == m->value[i + 1]) {
491: val[j++] = m->value[i++];
492: val[j] = '\0';
493: continue;
494: }
495:
496: assert('\\' == m->value[i] && i < realsz - 1);
497:
498: /* Parse to terminating delimiter. */
499: /* FIXME: embedded, escaped delimiters? */
500: for (start = end = i + 1; end < realsz; end++)
501: if ('\\' == m->value[end])
502: break;
503: if (end == realsz)
504: texierr(p, "unterminated argument name");
505:
506: for (k = 0; k < aasz; k++) {
507: if ((ssz = strlen(m->args[k])) != (end - start))
508: continue;
509: if (strncmp(&m->value[start], m->args[k], ssz))
510: continue;
511: break;
512: }
513:
514: /*
515: * Argument didn't exist in argument table.
1.14 kristaps 516: * Just ignore it.
1.7 kristaps 517: */
518: if (k == aasz) {
1.14 kristaps 519: i = end;
1.7 kristaps 520: continue;
521: }
522:
523: if (strlen(args[k]) > ssz) {
524: valsz += strlen(args[k]);
525: val = realloc(val, valsz + 1);
526: if (NULL == val)
527: texiabort(p, NULL);
528: }
529:
1.11 kristaps 530: for (cp = args[k]; '\0' != *cp; cp++)
531: val[j++] = *cp;
532:
533: val[j] = '\0';
1.7 kristaps 534: i = end;
535: }
536:
1.14 kristaps 537: texisplice(p, val, strlen(val), pos);
1.7 kristaps 538:
539: for (i = 0; i < asz; i++)
540: free(args[i]);
541: free(args);
542: free(val);
543: }
544:
1.1 kristaps 545: /*
546: * Output a free-form word in the input stream, progressing to the next
547: * command or white-space.
548: * This also will advance the input stream.
549: */
550: static void
1.14 kristaps 551: parseword(struct texi *p, size_t *pos, char extra)
1.1 kristaps 552: {
553:
554: if (p->seenws && 0 == p->outmacro &&
555: p->outcol > 72 && 0 == p->literal)
556: texiputchar(p, '\n');
557: /* FIXME: abstract this: we use it elsewhere. */
558: if (p->seenws && p->outcol && 0 == p->literal)
559: texiputchar(p, ' ');
560:
561: p->seenws = 0;
562:
1.14 kristaps 563: while (*pos < BUFSZ(p) && ! ismspace(BUF(p)[*pos])) {
564: switch (BUF(p)[*pos]) {
1.1 kristaps 565: case ('@'):
566: case ('}'):
567: case ('{'):
568: return;
569: }
1.14 kristaps 570: if ('\0' != extra && BUF(p)[*pos] == extra)
1.1 kristaps 571: return;
1.14 kristaps 572: if (*pos < BUFSZ(p) - 1 &&
573: '`' == BUF(p)[*pos] &&
574: '`' == BUF(p)[*pos + 1]) {
1.1 kristaps 575: texiputchars(p, "\\(lq");
1.14 kristaps 576: advance(p, pos);
577: } else if (*pos < BUFSZ(p) - 1 &&
578: '\'' == BUF(p)[*pos] &&
579: '\'' == BUF(p)[*pos + 1]) {
1.1 kristaps 580: texiputchars(p, "\\(rq");
1.14 kristaps 581: advance(p, pos);
1.1 kristaps 582: } else
1.14 kristaps 583: texiputchar(p, BUF(p)[*pos]);
584: advance(p, pos);
1.1 kristaps 585: }
586: }
587:
588: /*
589: * Look up the command at position "pos" in the buffer, returning it (or
590: * TEXICMD__MAX if none found) and setting "end" to be the absolute
591: * index after the command name.
592: */
593: enum texicmd
1.19 ! kristaps 594: texicmd(const struct texi *p, size_t pos, size_t *end, struct teximacro **macro)
1.1 kristaps 595: {
1.4 kristaps 596: size_t i, len, toksz;
1.1 kristaps 597:
1.14 kristaps 598: assert('@' == BUF(p)[pos]);
1.1 kristaps 599:
1.7 kristaps 600: if (NULL != macro)
601: *macro = NULL;
602:
1.14 kristaps 603: if ((*end = pos) == BUFSZ(p))
1.1 kristaps 604: return(TEXICMD__MAX);
1.14 kristaps 605: else if ((*end = ++pos) == BUFSZ(p))
1.1 kristaps 606: return(TEXICMD__MAX);
607:
608: /* Alphabetic commands are special. */
1.16 kristaps 609: if ( ! isalpha((unsigned char)BUF(p)[pos])) {
1.14 kristaps 610: if ((*end = pos + 1) == BUFSZ(p))
1.1 kristaps 611: return(TEXICMD__MAX);
612: for (i = 0; i < TEXICMD__MAX; i++) {
613: if (1 != texitoks[i].len)
614: continue;
1.14 kristaps 615: if (0 == strncmp(texitoks[i].tok, &BUF(p)[pos], 1))
1.1 kristaps 616: return(i);
617: }
1.14 kristaps 618: texiwarn(p, "bad command: @%c", BUF(p)[pos]);
1.1 kristaps 619: return(TEXICMD__MAX);
620: }
621:
1.4 kristaps 622: /* Scan to the end of the possible command name. */
1.14 kristaps 623: for (*end = pos; *end < BUFSZ(p) && ! ismspace(BUF(p)[*end]); (*end)++)
624: if ((*end > pos && ('@' == BUF(p)[*end] ||
625: '{' == BUF(p)[*end] || '}' == BUF(p)[*end])))
1.1 kristaps 626: break;
627:
1.4 kristaps 628: /* Look for the command. */
1.1 kristaps 629: len = *end - pos;
630: for (i = 0; i < TEXICMD__MAX; i++) {
631: if (len != texitoks[i].len)
632: continue;
1.14 kristaps 633: if (0 == strncmp(texitoks[i].tok, &BUF(p)[pos], len))
1.1 kristaps 634: return(i);
635: }
636:
1.4 kristaps 637: /* Look for it in our indices. */
638: for (i = 0; i < p->indexsz; i++) {
639: toksz = strlen(p->indexs[i]);
640: if (len != 5 + toksz)
641: continue;
1.14 kristaps 642: if (strncmp(&BUF(p)[pos], p->indexs[i], toksz))
1.4 kristaps 643: continue;
1.14 kristaps 644: if (0 == strncmp(&BUF(p)[pos + toksz], "index", 5))
1.7 kristaps 645: return(TEXICMD_USER_INDEX);
646: }
647:
648: for (i = 0; i < p->macrosz; i++) {
649: if (len != strlen(p->macros[i].key))
650: continue;
1.14 kristaps 651: if (strncmp(&BUF(p)[pos], p->macros[i].key, len))
1.7 kristaps 652: continue;
653: if (NULL != macro)
654: *macro = &p->macros[i];
655: return(TEXICMD__MAX);
1.4 kristaps 656: }
657:
1.14 kristaps 658: texiwarn(p, "bad command: @%.*s", (int)len, &BUF(p)[pos]);
1.1 kristaps 659: return(TEXICMD__MAX);
660: }
661:
662: /*
663: * Parse an argument from a bracketed command, e.g., @url{foo, baz}.
664: * Num should be set to the argument we're currently parsing, although
665: * it suffixes for it to be zero or non-zero.
666: * This will return 1 if there are more arguments, 0 otherwise.
667: * This will stop (returning 0) in the event of EOF or if we're not at a
668: * bracket for the zeroth parse.
669: */
670: int
1.14 kristaps 671: parsearg(struct texi *p, size_t *pos, size_t num)
1.1 kristaps 672: {
1.17 kristaps 673: size_t end, sv;
1.7 kristaps 674: enum texicmd cmd;
675: struct teximacro *macro;
1.1 kristaps 676:
1.14 kristaps 677: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos]))
678: advance(p, pos);
679: if (*pos == BUFSZ(p) || (0 == num && '{' != BUF(p)[*pos]))
1.1 kristaps 680: return(0);
681: if (0 == num)
1.14 kristaps 682: advance(p, pos);
1.1 kristaps 683:
1.14 kristaps 684: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
685: switch (BUF(p)[*pos]) {
1.1 kristaps 686: case (','):
1.14 kristaps 687: advance(p, pos);
1.1 kristaps 688: return(1);
689: case ('}'):
1.14 kristaps 690: advance(p, pos);
1.1 kristaps 691: return(0);
692: case ('{'):
693: if (0 == p->ign)
694: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 695: advance(p, pos);
1.1 kristaps 696: continue;
697: case ('@'):
698: break;
699: default:
1.14 kristaps 700: parseword(p, pos, ',');
1.1 kristaps 701: continue;
702: }
703:
1.17 kristaps 704: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 705: cmd = texicmd(p, *pos, &end, ¯o);
706: advanceto(p, pos, end);
1.7 kristaps 707: if (NULL != macro)
1.17 kristaps 708: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 709: if (TEXICMD__MAX == cmd)
710: continue;
711: if (NULL != texitoks[cmd].fp)
1.14 kristaps 712: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 713: }
714: return(0);
715: }
716:
717: /*
718: * Parse until the end of a bracketed statement, e.g., @foo{bar baz}.
719: * This will stop in the event of EOF or if we're not at a bracket.
720: */
721: void
1.18 kristaps 722: parsebracket(struct texi *p, size_t *pos, int dostack)
1.1 kristaps 723: {
1.18 kristaps 724: size_t end, sv, stack;
1.7 kristaps 725: enum texicmd cmd;
726: struct teximacro *macro;
1.1 kristaps 727:
1.14 kristaps 728: while (*pos < BUFSZ(p) && ismspace(BUF(p)[*pos]))
729: advance(p, pos);
1.1 kristaps 730:
1.14 kristaps 731: if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.1 kristaps 732: return;
1.14 kristaps 733: advance(p, pos);
1.1 kristaps 734:
1.18 kristaps 735: stack = 0;
1.14 kristaps 736: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
737: switch (BUF(p)[*pos]) {
1.1 kristaps 738: case ('}'):
1.18 kristaps 739: if (stack > 0) {
740: stack--;
741: advance(p, pos);
742: texiputchar(p, '}');
743: continue;
744: }
1.14 kristaps 745: advance(p, pos);
1.1 kristaps 746: return;
747: case ('{'):
1.18 kristaps 748: if (dostack) {
749: stack++;
750: advance(p, pos);
751: texiputchar(p, '{');
752: continue;
753: }
1.1 kristaps 754: if (0 == p->ign)
755: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 756: advance(p, pos);
1.1 kristaps 757: continue;
758: case ('@'):
759: break;
760: default:
1.14 kristaps 761: parseword(p, pos, '\0');
1.1 kristaps 762: continue;
763: }
764:
1.17 kristaps 765: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 766: cmd = texicmd(p, *pos, &end, ¯o);
767: advanceto(p, pos, end);
1.7 kristaps 768: if (NULL != macro)
1.17 kristaps 769: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 770: if (TEXICMD__MAX == cmd)
771: continue;
772: if (NULL != texitoks[cmd].fp)
1.14 kristaps 773: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 774: }
775: }
776:
777: /*
778: * This should be invoked when we're on a macro line and want to process
779: * to the end of the current input line, doing all of our macros along
780: * the way.
781: */
782: void
1.14 kristaps 783: parseeoln(struct texi *p, size_t *pos)
1.1 kristaps 784: {
1.17 kristaps 785: size_t end, sv;
1.7 kristaps 786: enum texicmd cmd;
787: struct teximacro *macro;
1.1 kristaps 788:
1.14 kristaps 789: while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) {
790: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) {
1.1 kristaps 791: p->seenws = 1;
792: if (p->literal)
1.14 kristaps 793: texiputchar(p, BUF(p)[*pos]);
794: advance(p, pos);
1.1 kristaps 795: }
1.14 kristaps 796: switch (BUF(p)[*pos]) {
1.1 kristaps 797: case ('}'):
798: if (0 == p->ign)
799: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 800: advance(p, pos);
1.1 kristaps 801: continue;
802: case ('{'):
803: if (0 == p->ign)
804: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 805: advance(p, pos);
1.1 kristaps 806: continue;
807: case ('@'):
808: break;
809: default:
1.14 kristaps 810: parseword(p, pos, '\0');
1.1 kristaps 811: continue;
812: }
813:
1.17 kristaps 814: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 815: cmd = texicmd(p, *pos, &end, ¯o);
816: advanceto(p, pos, end);
1.7 kristaps 817: if (NULL != macro)
1.17 kristaps 818: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 819: if (TEXICMD__MAX == cmd)
820: continue;
821: if (NULL != texitoks[cmd].fp)
1.14 kristaps 822: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 823: }
1.14 kristaps 824:
825: if (*pos < BUFSZ(p) && '\n' == BUF(p)[*pos])
826: advance(p, pos);
1.19 ! kristaps 827: }
! 828:
! 829: /*
! 830: * Peek to see if there's a command after subsequent whitespace.
! 831: * If so, return the macro identifier.
! 832: * This DOES NOT work with user-defined macros.
! 833: */
! 834: enum texicmd
! 835: peekcmd(const struct texi *p, size_t pos)
! 836: {
! 837: size_t end;
! 838:
! 839: while (pos < BUFSZ(p) && ismspace(BUF(p)[pos]))
! 840: pos++;
! 841: if (pos == BUFSZ(p) || '@' != BUF(p)[pos])
! 842: return(TEXICMD__MAX);
! 843: return(texicmd(p, pos, &end, NULL));
1.1 kristaps 844: }
845:
846: /*
847: * Parse a single word or command.
848: * This will return immediately at the EOF.
849: */
1.14 kristaps 850: static void
851: parsesingle(struct texi *p, size_t *pos)
1.1 kristaps 852: {
1.17 kristaps 853: size_t end, sv;
1.7 kristaps 854: enum texicmd cmd;
855: struct teximacro *macro;
1.1 kristaps 856:
1.14 kristaps 857: if ((*pos = advancenext(p, pos)) >= BUFSZ(p))
1.1 kristaps 858: return;
859:
1.14 kristaps 860: switch (BUF(p)[*pos]) {
1.1 kristaps 861: case ('}'):
862: if (0 == p->ign)
863: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 864: advance(p, pos);
1.1 kristaps 865: return;
866: case ('{'):
867: if (0 == p->ign)
868: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 869: advance(p, pos);
1.1 kristaps 870: return;
871: case ('@'):
872: break;
873: default:
1.14 kristaps 874: parseword(p, pos, '\0');
1.1 kristaps 875: return;
876: }
877:
1.17 kristaps 878: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 879: cmd = texicmd(p, *pos, &end, ¯o);
880: advanceto(p, pos, end);
1.7 kristaps 881: if (NULL != macro)
1.17 kristaps 882: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 883: if (TEXICMD__MAX == cmd)
884: return;
885: if (NULL != texitoks[cmd].fp)
1.14 kristaps 886: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 887: }
888:
889: /*
890: * This is used in the @deffn type of command.
891: * These have an arbitrary number of line arguments; however, these
892: * arguments may or may not be surrounded by brackets.
893: * In this function, we parse each one as either a bracketed or
894: * non-bracketed argument, returning 0 when we've reached the end of
895: * line or 1 otherwise.
896: */
897: int
1.14 kristaps 898: parselinearg(struct texi *p, size_t *pos)
1.1 kristaps 899: {
900:
1.14 kristaps 901: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) {
1.1 kristaps 902: p->seenws = 1;
1.14 kristaps 903: advance(p, pos);
1.1 kristaps 904: }
905:
1.14 kristaps 906: if (*pos < BUFSZ(p) && '{' == BUF(p)[*pos])
1.18 kristaps 907: parsebracket(p, pos, 0);
1.14 kristaps 908: else if (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
909: parsesingle(p, pos);
1.1 kristaps 910: else
911: return(0);
912:
913: return(1);
914: }
915:
916: /*
917: * Parse til the end of the buffer.
918: */
1.14 kristaps 919: static void
920: parseeof(struct texi *p)
1.1 kristaps 921: {
922: size_t pos;
923:
1.14 kristaps 924: for (pos = 0; pos < BUFSZ(p); )
925: parsesingle(p, &pos);
1.1 kristaps 926: }
927:
1.8 kristaps 928: void
1.14 kristaps 929: texisplice(struct texi *p, const char *buf, size_t sz, size_t *pos)
1.8 kristaps 930: {
1.14 kristaps 931: char *cp;
932: struct texifile *f;
1.8 kristaps 933:
1.14 kristaps 934: assert(p->filepos > 0);
935: f = &p->files[p->filepos - 1];
1.8 kristaps 936:
1.14 kristaps 937: if (f->mapsz + sz > f->mapmaxsz) {
938: f->mapmaxsz = f->mapsz + sz + 1024;
939: cp = realloc(f->map, f->mapmaxsz);
940: if (NULL == cp)
941: texiabort(p, NULL);
942: f->map = cp;
943: }
1.8 kristaps 944:
1.15 kristaps 945: f->insplice += sz;
1.14 kristaps 946: memmove(f->map + *pos + sz, f->map + *pos, f->mapsz - *pos);
947: memcpy(f->map + *pos, buf, sz);
948: f->mapsz += sz;
1.8 kristaps 949: }
950:
951: /*
1.1 kristaps 952: * Parse a block sequence until we have the "@end endtoken" command
953: * invocation.
954: * This will return immediately at EOF.
955: */
956: void
1.14 kristaps 957: parseto(struct texi *p, size_t *pos, const char *endtoken)
1.1 kristaps 958: {
1.17 kristaps 959: size_t end, sv;
1.7 kristaps 960: enum texicmd cmd;
961: size_t endtoksz;
962: struct teximacro *macro;
1.1 kristaps 963:
964: endtoksz = strlen(endtoken);
965: assert(endtoksz > 0);
966:
1.14 kristaps 967: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
968: switch (BUF(p)[*pos]) {
1.1 kristaps 969: case ('}'):
970: if (0 == p->ign)
971: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 972: advance(p, pos);
1.1 kristaps 973: continue;
974: case ('{'):
975: if (0 == p->ign)
976: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 977: advance(p, pos);
1.1 kristaps 978: continue;
979: case ('@'):
980: break;
981: default:
1.14 kristaps 982: parseword(p, pos, '\0');
1.1 kristaps 983: continue;
984: }
985:
1.17 kristaps 986: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 987: cmd = texicmd(p, *pos, &end, ¯o);
988: advanceto(p, pos, end);
1.1 kristaps 989: if (TEXICMD_END == cmd) {
1.14 kristaps 990: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
991: advance(p, pos);
1.1 kristaps 992: /*
993: * FIXME: check the full word, not just its
994: * initial substring!
995: */
1.14 kristaps 996: if (BUFSZ(p) - *pos >= endtoksz && 0 == strncmp
997: (&BUF(p)[*pos], endtoken, endtoksz)) {
998: advanceeoln(p, pos, 0);
1.1 kristaps 999: break;
1000: }
1001: if (0 == p->ign)
1002: texiwarn(p, "unexpected \"end\"");
1.14 kristaps 1003: advanceeoln(p, pos, 0);
1.1 kristaps 1004: continue;
1.7 kristaps 1005: }
1006: if (NULL != macro)
1.17 kristaps 1007: texiexecmacro(p, macro, sv, pos);
1.7 kristaps 1008: if (TEXICMD__MAX == cmd)
1009: continue;
1010: if (NULL != texitoks[cmd].fp)
1.14 kristaps 1011: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 1012: }
1013: }
1014:
1015: /*
1.12 kristaps 1016: * Like parsefile() but used for reading from stdandard input.
1017: * This can only be called for the first file!
1018: */
1019: void
1020: parsestdin(struct texi *p)
1021: {
1022: struct texifile *f;
1023: ssize_t ssz;
1024:
1025: assert(0 == p->filepos);
1026: f = &p->files[p->filepos];
1027: memset(f, 0, sizeof(struct texifile));
1028:
1029: f->type = TEXISRC_STDIN;
1030: f->name = "<stdin>";
1031:
1.14 kristaps 1032: for (f->mapsz = 0; ; f->mapsz += (size_t)ssz) {
1033: if (f->mapsz == f->mapmaxsz) {
1034: if (f->mapmaxsz == (1U << 31))
1.12 kristaps 1035: texierr(p, "stdin buffer too long");
1.14 kristaps 1036: f->mapmaxsz = f->mapmaxsz > 65536 / 2 ?
1037: 2 * f->mapmaxsz : 65536;
1038: f->map = realloc(f->map, f->mapmaxsz);
1.12 kristaps 1039: if (NULL == f->map)
1040: texiabort(p, NULL);
1041: }
1.14 kristaps 1042: ssz = read(STDIN_FILENO, f->map +
1043: (int)f->mapsz, f->mapmaxsz - f->mapsz);
1.12 kristaps 1044: if (0 == ssz)
1045: break;
1046: else if (-1 == ssz)
1047: texiabort(p, NULL);
1048: }
1049:
1050: p->filepos++;
1.14 kristaps 1051: parseeof(p);
1.12 kristaps 1052: texifilepop(p);
1053: }
1054:
1055: /*
1.1 kristaps 1056: * Memory-map the file "fname" and begin parsing it unless "parse" is
1057: * zero, in which case we just dump the file to stdout (making sure it
1058: * doesn't trip up mdoc(7) along the way).
1059: * This can be called in a nested context.
1060: */
1061: void
1062: parsefile(struct texi *p, const char *fname, int parse)
1063: {
1064: struct texifile *f;
1065: int fd;
1066: struct stat st;
1067: size_t i;
1.14 kristaps 1068: char *map;
1.1 kristaps 1069:
1.5 kristaps 1070: if (64 == p->filepos)
1.6 kristaps 1071: texierr(p, "too many open files");
1.1 kristaps 1072: f = &p->files[p->filepos];
1073: memset(f, 0, sizeof(struct texifile));
1074:
1.12 kristaps 1075: f->type = TEXISRC_FILE;
1.1 kristaps 1076: f->name = fname;
1077: if (-1 == (fd = open(fname, O_RDONLY, 0))) {
1078: texiabort(p, fname);
1079: } else if (-1 == fstat(fd, &st)) {
1080: close(fd);
1081: texiabort(p, fname);
1082: }
1083:
1.14 kristaps 1084: f->mapsz = f->mapmaxsz = st.st_size;
1085: map = mmap(NULL, f->mapsz,
1.1 kristaps 1086: PROT_READ, MAP_SHARED, fd, 0);
1087: close(fd);
1088:
1.14 kristaps 1089: if (MAP_FAILED == map)
1.1 kristaps 1090: texiabort(p, fname);
1091:
1092: if ( ! parse) {
1.13 kristaps 1093: for (i = 0; i < f->mapsz; i++)
1.14 kristaps 1094: texiputchar(p, map[i]);
1.13 kristaps 1095: if (p->outcol)
1096: texiputchar(p, '\n');
1.14 kristaps 1097: munmap(map, f->mapsz);
1098: return;
1099: }
1100:
1101: p->filepos++;
1102: f->map = malloc(f->mapsz);
1103: memcpy(f->map, map, f->mapsz);
1104: munmap(map, f->mapsz);
1105: parseeof(p);
1.1 kristaps 1106: texifilepop(p);
1107: }
1108:
1.2 kristaps 1109: /*
1110: * Look up the value to a stored pair's value starting in "buf" from
1111: * start to end.
1112: * Return the pointer to the value memory, which can be NULL if the
1113: * pointer key does not exist.
1114: * The pointer can point to NULL if the value has been unset.
1115: */
1116: static char **
1.14 kristaps 1117: valuequery(const struct texi *p, size_t start, size_t end)
1.2 kristaps 1118: {
1119: size_t i, sz, len;
1120:
1121: assert(end >= start);
1122: /* Ignore zero-length. */
1123: if (0 == (len = (end - start)))
1124: return(NULL);
1125: for (i = 0; i < p->valsz; i++) {
1126: sz = strlen(p->vals[i].key);
1127: if (sz != len)
1128: continue;
1.14 kristaps 1129: if (0 == strncmp(p->vals[i].key, &BUF(p)[start], len))
1.2 kristaps 1130: return(&p->vals[i].value);
1131: }
1132: return(NULL);
1133: }
1134:
1135: /*
1136: * Parse a key until the end of line, e.g., @clear foo\n, and return the
1137: * pointer to its value via valuequery().
1138: */
1139: static char **
1.14 kristaps 1140: valuelquery(struct texi *p, size_t *pos)
1.2 kristaps 1141: {
1142: size_t start, end;
1143: char **ret;
1144:
1.14 kristaps 1145: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1146: advance(p, pos);
1147: if (*pos == BUFSZ(p))
1.2 kristaps 1148: return(NULL);
1.14 kristaps 1149: for (start = end = *pos; end < BUFSZ(p); end++)
1150: if ('\n' == BUF(p)[end])
1.2 kristaps 1151: break;
1.14 kristaps 1152: advanceto(p, pos, end);
1153: if (*pos < BUFSZ(p)) {
1154: assert('\n' == BUF(p)[*pos]);
1155: advance(p, pos);
1.2 kristaps 1156: }
1.14 kristaps 1157: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1158: return(NULL);
1159: return(ret);
1160: }
1161:
1162: void
1.14 kristaps 1163: valuelclear(struct texi *p, size_t *pos)
1.2 kristaps 1164: {
1165: char **ret;
1166:
1.14 kristaps 1167: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1168: return;
1169: free(*ret);
1170: *ret = NULL;
1171: }
1172:
1173: const char *
1.14 kristaps 1174: valuellookup(struct texi *p, size_t *pos)
1.2 kristaps 1175: {
1176: char **ret;
1177:
1.14 kristaps 1178: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1179: return(NULL);
1180: return(*ret);
1181: }
1182:
1183: /*
1184: * Parse a key from a bracketed string, e.g., @value{foo}, and return
1185: * the pointer to its value.
1186: * If the returned pointer is NULL, either there was no string within
1187: * the brackets (or no brackets), or the value was not found, or the
1188: * value had previously been unset.
1189: */
1190: const char *
1.14 kristaps 1191: valueblookup(struct texi *p, size_t *pos)
1.2 kristaps 1192: {
1193: size_t start, end;
1194: char **ret;
1195:
1.14 kristaps 1196: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1197: advance(p, pos);
1198: if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.2 kristaps 1199: return(NULL);
1.14 kristaps 1200: advance(p, pos);
1201: for (start = end = *pos; end < BUFSZ(p); end++)
1202: if ('}' == BUF(p)[end])
1.2 kristaps 1203: break;
1.14 kristaps 1204: advanceto(p, pos, end);
1205: if (*pos < BUFSZ(p)) {
1206: assert('}' == BUF(p)[*pos]);
1207: advance(p, pos);
1.2 kristaps 1208: }
1.14 kristaps 1209: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1210: return(NULL);
1211: return(*ret);
1212: }
1213:
1214: void
1215: valueadd(struct texi *p, char *key, char *val)
1216: {
1217: size_t i;
1218:
1219: assert(NULL != key);
1220: assert(NULL != val);
1221:
1222: for (i = 0; i < p->valsz; i++)
1223: if (0 == strcmp(p->vals[i].key, key))
1224: break;
1225:
1226: if (i < p->valsz) {
1227: free(key);
1228: free(p->vals[i].value);
1229: p->vals[i].value = val;
1230: } else {
1.4 kristaps 1231: /* FIXME: reallocarray() */
1.2 kristaps 1232: p->vals = realloc(p->vals,
1233: (p->valsz + 1) *
1234: sizeof(struct texivalue));
1.4 kristaps 1235: if (NULL == p->vals)
1236: texiabort(p, NULL);
1.2 kristaps 1237: p->vals[p->valsz].key = key;
1238: p->vals[p->valsz].value = val;
1239: p->valsz++;
1240: }
1.7 kristaps 1241: }
1242:
1243: /*
1244: * Take the arguments to a macro, e.g., @foo{bar, baz, xyzzy} (or the
1245: * declaration form, @macro foo {arg1, ...}) and textually convert it to
1246: * an array of arguments of size "argsz".
1247: * These need to be freed individually and as a whole.
1248: * NOTE: this will puke on @, or @} macros, which can trick it into
1249: * stopping argument parsing earlier.
1250: * Ergo, textual: this doesn't interpret the arguments in any way.
1251: */
1252: char **
1.14 kristaps 1253: argparse(struct texi *p, size_t *pos, size_t *argsz, size_t hint)
1.7 kristaps 1254: {
1255: char **args;
1256: size_t start, end, stack;
1257:
1.14 kristaps 1258: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1259: advance(p, pos);
1.7 kristaps 1260:
1261: args = NULL;
1262: *argsz = 0;
1263:
1.17 kristaps 1264: if (*pos == BUFSZ(p))
1265: return(args);
1266:
1.14 kristaps 1267: if ('{' != BUF(p)[*pos] && hint) {
1.10 kristaps 1268: /*
1269: * Special case: if we encounter an unbracketed argument
1270: * and we're being invoked with non-zero arguments
1271: * (versus being set, i.e., hint>0), then parse until
1272: * the end of line.
1273: */
1274: *argsz = 1;
1275: args = calloc(1, sizeof(char *));
1276: if (NULL == args)
1277: texiabort(p, NULL);
1278: start = *pos;
1.14 kristaps 1279: while (*pos < BUFSZ(p)) {
1280: if ('\n' == BUF(p)[*pos])
1.10 kristaps 1281: break;
1.14 kristaps 1282: advance(p, pos);
1.10 kristaps 1283: }
1284: args[0] = malloc(*pos - start + 1);
1.14 kristaps 1285: memcpy(args[0], &BUF(p)[start], *pos - start);
1.10 kristaps 1286: args[0][*pos - start] = '\0';
1.14 kristaps 1287: if (*pos < BUFSZ(p) && '\n' == BUF(p)[*pos])
1288: advance(p, pos);
1.10 kristaps 1289: return(args);
1.14 kristaps 1290: } else if ('{' != BUF(p)[*pos])
1.7 kristaps 1291: return(args);
1.17 kristaps 1292:
1293: assert('{' == BUF(p)[*pos]);
1.7 kristaps 1294:
1295: /* Parse til the closing '}', putting into the array. */
1.14 kristaps 1296: advance(p, pos);
1297: while (*pos < BUFSZ(p)) {
1298: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1299: advance(p, pos);
1.7 kristaps 1300: start = *pos;
1301: stack = 0;
1.14 kristaps 1302: while (*pos < BUFSZ(p)) {
1.7 kristaps 1303: /*
1304: * According to the manual, commas within
1305: * embedded commands are escaped.
1306: * We keep track of embedded-ness in the "stack"
1307: * state anyway, so this is free.
1308: */
1.14 kristaps 1309: if (',' == BUF(p)[*pos] && 0 == stack && 1 != hint)
1.7 kristaps 1310: break;
1.14 kristaps 1311: else if (0 == stack && '}' == BUF(p)[*pos])
1.7 kristaps 1312: break;
1.14 kristaps 1313: else if (0 != stack && '}' == BUF(p)[*pos])
1.7 kristaps 1314: stack--;
1.14 kristaps 1315: else if ('{' == BUF(p)[*pos])
1.7 kristaps 1316: stack++;
1.14 kristaps 1317: advance(p, pos);
1.7 kristaps 1318: }
1319: if (stack)
1320: texiwarn(p, "unterminated macro "
1321: "in macro arguments");
1.14 kristaps 1322: if ((end = *pos) == BUFSZ(p))
1.7 kristaps 1323: break;
1324: /* Test for zero-length '{ }'. */
1.14 kristaps 1325: if (start == end && '}' == BUF(p)[*pos] && 0 == *argsz)
1.7 kristaps 1326: break;
1327: /* FIXME: use reallocarray. */
1328: args = realloc
1329: (args, sizeof(char *) *
1330: (*argsz + 1));
1331: if (NULL == args)
1332: texiabort(p, NULL);
1333: args[*argsz] = malloc(end - start + 1);
1334: if (NULL == args[*argsz])
1335: texiabort(p, NULL);
1336: memcpy(args[*argsz],
1.14 kristaps 1337: &BUF(p)[start], end - start);
1.7 kristaps 1338: args[*argsz][end - start] = '\0';
1339: (*argsz)++;
1.14 kristaps 1340: if ('}' == BUF(p)[*pos])
1.7 kristaps 1341: break;
1.14 kristaps 1342: advance(p, pos);
1.7 kristaps 1343: }
1344:
1.14 kristaps 1345: if (*pos == BUFSZ(p))
1.7 kristaps 1346: texierr(p, "unterminated arguments");
1.14 kristaps 1347: assert('}' == BUF(p)[*pos]);
1348: advance(p, pos);
1.7 kristaps 1349: return(args);
1.2 kristaps 1350: }
CVSweb