Annotation of texi2mdoc/util.c, Revision 1.18
1.18 ! kristaps 1: /* $Id: util.c,v 1.17 2015/02/28 00:03:20 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.14 kristaps 594: texicmd(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.1 kristaps 827: }
828:
829: /*
830: * Parse a single word or command.
831: * This will return immediately at the EOF.
832: */
1.14 kristaps 833: static void
834: parsesingle(struct texi *p, size_t *pos)
1.1 kristaps 835: {
1.17 kristaps 836: size_t end, sv;
1.7 kristaps 837: enum texicmd cmd;
838: struct teximacro *macro;
1.1 kristaps 839:
1.14 kristaps 840: if ((*pos = advancenext(p, pos)) >= BUFSZ(p))
1.1 kristaps 841: return;
842:
1.14 kristaps 843: switch (BUF(p)[*pos]) {
1.1 kristaps 844: case ('}'):
845: if (0 == p->ign)
846: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 847: advance(p, pos);
1.1 kristaps 848: return;
849: case ('{'):
850: if (0 == p->ign)
851: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 852: advance(p, pos);
1.1 kristaps 853: return;
854: case ('@'):
855: break;
856: default:
1.14 kristaps 857: parseword(p, pos, '\0');
1.1 kristaps 858: return;
859: }
860:
1.17 kristaps 861: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 862: cmd = texicmd(p, *pos, &end, ¯o);
863: advanceto(p, pos, end);
1.7 kristaps 864: if (NULL != macro)
1.17 kristaps 865: texiexecmacro(p, macro, sv, pos);
1.1 kristaps 866: if (TEXICMD__MAX == cmd)
867: return;
868: if (NULL != texitoks[cmd].fp)
1.14 kristaps 869: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 870: }
871:
872: /*
873: * This is used in the @deffn type of command.
874: * These have an arbitrary number of line arguments; however, these
875: * arguments may or may not be surrounded by brackets.
876: * In this function, we parse each one as either a bracketed or
877: * non-bracketed argument, returning 0 when we've reached the end of
878: * line or 1 otherwise.
879: */
880: int
1.14 kristaps 881: parselinearg(struct texi *p, size_t *pos)
1.1 kristaps 882: {
883:
1.14 kristaps 884: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos])) {
1.1 kristaps 885: p->seenws = 1;
1.14 kristaps 886: advance(p, pos);
1.1 kristaps 887: }
888:
1.14 kristaps 889: if (*pos < BUFSZ(p) && '{' == BUF(p)[*pos])
1.18 ! kristaps 890: parsebracket(p, pos, 0);
1.14 kristaps 891: else if (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
892: parsesingle(p, pos);
1.1 kristaps 893: else
894: return(0);
895:
896: return(1);
897: }
898:
899: /*
900: * Parse til the end of the buffer.
901: */
1.14 kristaps 902: static void
903: parseeof(struct texi *p)
1.1 kristaps 904: {
905: size_t pos;
906:
1.14 kristaps 907: for (pos = 0; pos < BUFSZ(p); )
908: parsesingle(p, &pos);
1.1 kristaps 909: }
910:
1.8 kristaps 911: void
1.14 kristaps 912: texisplice(struct texi *p, const char *buf, size_t sz, size_t *pos)
1.8 kristaps 913: {
1.14 kristaps 914: char *cp;
915: struct texifile *f;
1.8 kristaps 916:
1.14 kristaps 917: assert(p->filepos > 0);
918: f = &p->files[p->filepos - 1];
1.8 kristaps 919:
1.14 kristaps 920: if (f->mapsz + sz > f->mapmaxsz) {
921: f->mapmaxsz = f->mapsz + sz + 1024;
922: cp = realloc(f->map, f->mapmaxsz);
923: if (NULL == cp)
924: texiabort(p, NULL);
925: f->map = cp;
926: }
1.8 kristaps 927:
1.15 kristaps 928: f->insplice += sz;
1.14 kristaps 929: memmove(f->map + *pos + sz, f->map + *pos, f->mapsz - *pos);
930: memcpy(f->map + *pos, buf, sz);
931: f->mapsz += sz;
1.8 kristaps 932: }
933:
934: /*
1.1 kristaps 935: * Parse a block sequence until we have the "@end endtoken" command
936: * invocation.
937: * This will return immediately at EOF.
938: */
939: void
1.14 kristaps 940: parseto(struct texi *p, size_t *pos, const char *endtoken)
1.1 kristaps 941: {
1.17 kristaps 942: size_t end, sv;
1.7 kristaps 943: enum texicmd cmd;
944: size_t endtoksz;
945: struct teximacro *macro;
1.1 kristaps 946:
947: endtoksz = strlen(endtoken);
948: assert(endtoksz > 0);
949:
1.14 kristaps 950: while ((*pos = advancenext(p, pos)) < BUFSZ(p)) {
951: switch (BUF(p)[*pos]) {
1.1 kristaps 952: case ('}'):
953: if (0 == p->ign)
954: texiwarn(p, "unexpected \"}\"");
1.14 kristaps 955: advance(p, pos);
1.1 kristaps 956: continue;
957: case ('{'):
958: if (0 == p->ign)
959: texiwarn(p, "unexpected \"{\"");
1.14 kristaps 960: advance(p, pos);
1.1 kristaps 961: continue;
962: case ('@'):
963: break;
964: default:
1.14 kristaps 965: parseword(p, pos, '\0');
1.1 kristaps 966: continue;
967: }
968:
1.17 kristaps 969: sv = p->files[p->filepos - 1].insplice;
1.14 kristaps 970: cmd = texicmd(p, *pos, &end, ¯o);
971: advanceto(p, pos, end);
1.1 kristaps 972: if (TEXICMD_END == cmd) {
1.14 kristaps 973: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
974: advance(p, pos);
1.1 kristaps 975: /*
976: * FIXME: check the full word, not just its
977: * initial substring!
978: */
1.14 kristaps 979: if (BUFSZ(p) - *pos >= endtoksz && 0 == strncmp
980: (&BUF(p)[*pos], endtoken, endtoksz)) {
981: advanceeoln(p, pos, 0);
1.1 kristaps 982: break;
983: }
984: if (0 == p->ign)
985: texiwarn(p, "unexpected \"end\"");
1.14 kristaps 986: advanceeoln(p, pos, 0);
1.1 kristaps 987: continue;
1.7 kristaps 988: }
989: if (NULL != macro)
1.17 kristaps 990: texiexecmacro(p, macro, sv, pos);
1.7 kristaps 991: if (TEXICMD__MAX == cmd)
992: continue;
993: if (NULL != texitoks[cmd].fp)
1.14 kristaps 994: (*texitoks[cmd].fp)(p, cmd, pos);
1.1 kristaps 995: }
996: }
997:
998: /*
1.12 kristaps 999: * Like parsefile() but used for reading from stdandard input.
1000: * This can only be called for the first file!
1001: */
1002: void
1003: parsestdin(struct texi *p)
1004: {
1005: struct texifile *f;
1006: ssize_t ssz;
1007:
1008: assert(0 == p->filepos);
1009: f = &p->files[p->filepos];
1010: memset(f, 0, sizeof(struct texifile));
1011:
1012: f->type = TEXISRC_STDIN;
1013: f->name = "<stdin>";
1014:
1.14 kristaps 1015: for (f->mapsz = 0; ; f->mapsz += (size_t)ssz) {
1016: if (f->mapsz == f->mapmaxsz) {
1017: if (f->mapmaxsz == (1U << 31))
1.12 kristaps 1018: texierr(p, "stdin buffer too long");
1.14 kristaps 1019: f->mapmaxsz = f->mapmaxsz > 65536 / 2 ?
1020: 2 * f->mapmaxsz : 65536;
1021: f->map = realloc(f->map, f->mapmaxsz);
1.12 kristaps 1022: if (NULL == f->map)
1023: texiabort(p, NULL);
1024: }
1.14 kristaps 1025: ssz = read(STDIN_FILENO, f->map +
1026: (int)f->mapsz, f->mapmaxsz - f->mapsz);
1.12 kristaps 1027: if (0 == ssz)
1028: break;
1029: else if (-1 == ssz)
1030: texiabort(p, NULL);
1031: }
1032:
1033: p->filepos++;
1.14 kristaps 1034: parseeof(p);
1.12 kristaps 1035: texifilepop(p);
1036: }
1037:
1038: /*
1.1 kristaps 1039: * Memory-map the file "fname" and begin parsing it unless "parse" is
1040: * zero, in which case we just dump the file to stdout (making sure it
1041: * doesn't trip up mdoc(7) along the way).
1042: * This can be called in a nested context.
1043: */
1044: void
1045: parsefile(struct texi *p, const char *fname, int parse)
1046: {
1047: struct texifile *f;
1048: int fd;
1049: struct stat st;
1050: size_t i;
1.14 kristaps 1051: char *map;
1.1 kristaps 1052:
1.5 kristaps 1053: if (64 == p->filepos)
1.6 kristaps 1054: texierr(p, "too many open files");
1.1 kristaps 1055: f = &p->files[p->filepos];
1056: memset(f, 0, sizeof(struct texifile));
1057:
1.12 kristaps 1058: f->type = TEXISRC_FILE;
1.1 kristaps 1059: f->name = fname;
1060: if (-1 == (fd = open(fname, O_RDONLY, 0))) {
1061: texiabort(p, fname);
1062: } else if (-1 == fstat(fd, &st)) {
1063: close(fd);
1064: texiabort(p, fname);
1065: }
1066:
1.14 kristaps 1067: f->mapsz = f->mapmaxsz = st.st_size;
1068: map = mmap(NULL, f->mapsz,
1.1 kristaps 1069: PROT_READ, MAP_SHARED, fd, 0);
1070: close(fd);
1071:
1.14 kristaps 1072: if (MAP_FAILED == map)
1.1 kristaps 1073: texiabort(p, fname);
1074:
1075: if ( ! parse) {
1.13 kristaps 1076: for (i = 0; i < f->mapsz; i++)
1.14 kristaps 1077: texiputchar(p, map[i]);
1.13 kristaps 1078: if (p->outcol)
1079: texiputchar(p, '\n');
1.14 kristaps 1080: munmap(map, f->mapsz);
1081: return;
1082: }
1083:
1084: p->filepos++;
1085: f->map = malloc(f->mapsz);
1086: memcpy(f->map, map, f->mapsz);
1087: munmap(map, f->mapsz);
1088: parseeof(p);
1.1 kristaps 1089: texifilepop(p);
1090: }
1091:
1.2 kristaps 1092: /*
1093: * Look up the value to a stored pair's value starting in "buf" from
1094: * start to end.
1095: * Return the pointer to the value memory, which can be NULL if the
1096: * pointer key does not exist.
1097: * The pointer can point to NULL if the value has been unset.
1098: */
1099: static char **
1.14 kristaps 1100: valuequery(const struct texi *p, size_t start, size_t end)
1.2 kristaps 1101: {
1102: size_t i, sz, len;
1103:
1104: assert(end >= start);
1105: /* Ignore zero-length. */
1106: if (0 == (len = (end - start)))
1107: return(NULL);
1108: for (i = 0; i < p->valsz; i++) {
1109: sz = strlen(p->vals[i].key);
1110: if (sz != len)
1111: continue;
1.14 kristaps 1112: if (0 == strncmp(p->vals[i].key, &BUF(p)[start], len))
1.2 kristaps 1113: return(&p->vals[i].value);
1114: }
1115: return(NULL);
1116: }
1117:
1118: /*
1119: * Parse a key until the end of line, e.g., @clear foo\n, and return the
1120: * pointer to its value via valuequery().
1121: */
1122: static char **
1.14 kristaps 1123: valuelquery(struct texi *p, size_t *pos)
1.2 kristaps 1124: {
1125: size_t start, end;
1126: char **ret;
1127:
1.14 kristaps 1128: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1129: advance(p, pos);
1130: if (*pos == BUFSZ(p))
1.2 kristaps 1131: return(NULL);
1.14 kristaps 1132: for (start = end = *pos; end < BUFSZ(p); end++)
1133: if ('\n' == BUF(p)[end])
1.2 kristaps 1134: break;
1.14 kristaps 1135: advanceto(p, pos, end);
1136: if (*pos < BUFSZ(p)) {
1137: assert('\n' == BUF(p)[*pos]);
1138: advance(p, pos);
1.2 kristaps 1139: }
1.14 kristaps 1140: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1141: return(NULL);
1142: return(ret);
1143: }
1144:
1145: void
1.14 kristaps 1146: valuelclear(struct texi *p, size_t *pos)
1.2 kristaps 1147: {
1148: char **ret;
1149:
1.14 kristaps 1150: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1151: return;
1152: free(*ret);
1153: *ret = NULL;
1154: }
1155:
1156: const char *
1.14 kristaps 1157: valuellookup(struct texi *p, size_t *pos)
1.2 kristaps 1158: {
1159: char **ret;
1160:
1.14 kristaps 1161: if (NULL == (ret = valuelquery(p, pos)))
1.2 kristaps 1162: return(NULL);
1163: return(*ret);
1164: }
1165:
1166: /*
1167: * Parse a key from a bracketed string, e.g., @value{foo}, and return
1168: * the pointer to its value.
1169: * If the returned pointer is NULL, either there was no string within
1170: * the brackets (or no brackets), or the value was not found, or the
1171: * value had previously been unset.
1172: */
1173: const char *
1.14 kristaps 1174: valueblookup(struct texi *p, size_t *pos)
1.2 kristaps 1175: {
1176: size_t start, end;
1177: char **ret;
1178:
1.14 kristaps 1179: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1180: advance(p, pos);
1181: if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.2 kristaps 1182: return(NULL);
1.14 kristaps 1183: advance(p, pos);
1184: for (start = end = *pos; end < BUFSZ(p); end++)
1185: if ('}' == BUF(p)[end])
1.2 kristaps 1186: break;
1.14 kristaps 1187: advanceto(p, pos, end);
1188: if (*pos < BUFSZ(p)) {
1189: assert('}' == BUF(p)[*pos]);
1190: advance(p, pos);
1.2 kristaps 1191: }
1.14 kristaps 1192: if (NULL == (ret = valuequery(p, start, end)))
1.2 kristaps 1193: return(NULL);
1194: return(*ret);
1195: }
1196:
1197: void
1198: valueadd(struct texi *p, char *key, char *val)
1199: {
1200: size_t i;
1201:
1202: assert(NULL != key);
1203: assert(NULL != val);
1204:
1205: for (i = 0; i < p->valsz; i++)
1206: if (0 == strcmp(p->vals[i].key, key))
1207: break;
1208:
1209: if (i < p->valsz) {
1210: free(key);
1211: free(p->vals[i].value);
1212: p->vals[i].value = val;
1213: } else {
1.4 kristaps 1214: /* FIXME: reallocarray() */
1.2 kristaps 1215: p->vals = realloc(p->vals,
1216: (p->valsz + 1) *
1217: sizeof(struct texivalue));
1.4 kristaps 1218: if (NULL == p->vals)
1219: texiabort(p, NULL);
1.2 kristaps 1220: p->vals[p->valsz].key = key;
1221: p->vals[p->valsz].value = val;
1222: p->valsz++;
1223: }
1.7 kristaps 1224: }
1225:
1226: /*
1227: * Take the arguments to a macro, e.g., @foo{bar, baz, xyzzy} (or the
1228: * declaration form, @macro foo {arg1, ...}) and textually convert it to
1229: * an array of arguments of size "argsz".
1230: * These need to be freed individually and as a whole.
1231: * NOTE: this will puke on @, or @} macros, which can trick it into
1232: * stopping argument parsing earlier.
1233: * Ergo, textual: this doesn't interpret the arguments in any way.
1234: */
1235: char **
1.14 kristaps 1236: argparse(struct texi *p, size_t *pos, size_t *argsz, size_t hint)
1.7 kristaps 1237: {
1238: char **args;
1239: size_t start, end, stack;
1240:
1.14 kristaps 1241: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1242: advance(p, pos);
1.7 kristaps 1243:
1244: args = NULL;
1245: *argsz = 0;
1246:
1.17 kristaps 1247: if (*pos == BUFSZ(p))
1248: return(args);
1249:
1.14 kristaps 1250: if ('{' != BUF(p)[*pos] && hint) {
1.10 kristaps 1251: /*
1252: * Special case: if we encounter an unbracketed argument
1253: * and we're being invoked with non-zero arguments
1254: * (versus being set, i.e., hint>0), then parse until
1255: * the end of line.
1256: */
1257: *argsz = 1;
1258: args = calloc(1, sizeof(char *));
1259: if (NULL == args)
1260: texiabort(p, NULL);
1261: start = *pos;
1.14 kristaps 1262: while (*pos < BUFSZ(p)) {
1263: if ('\n' == BUF(p)[*pos])
1.10 kristaps 1264: break;
1.14 kristaps 1265: advance(p, pos);
1.10 kristaps 1266: }
1267: args[0] = malloc(*pos - start + 1);
1.14 kristaps 1268: memcpy(args[0], &BUF(p)[start], *pos - start);
1.10 kristaps 1269: args[0][*pos - start] = '\0';
1.14 kristaps 1270: if (*pos < BUFSZ(p) && '\n' == BUF(p)[*pos])
1271: advance(p, pos);
1.10 kristaps 1272: return(args);
1.14 kristaps 1273: } else if ('{' != BUF(p)[*pos])
1.7 kristaps 1274: return(args);
1.17 kristaps 1275:
1276: assert('{' == BUF(p)[*pos]);
1.7 kristaps 1277:
1278: /* Parse til the closing '}', putting into the array. */
1.14 kristaps 1279: advance(p, pos);
1280: while (*pos < BUFSZ(p)) {
1281: while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
1282: advance(p, pos);
1.7 kristaps 1283: start = *pos;
1284: stack = 0;
1.14 kristaps 1285: while (*pos < BUFSZ(p)) {
1.7 kristaps 1286: /*
1287: * According to the manual, commas within
1288: * embedded commands are escaped.
1289: * We keep track of embedded-ness in the "stack"
1290: * state anyway, so this is free.
1291: */
1.14 kristaps 1292: if (',' == BUF(p)[*pos] && 0 == stack && 1 != hint)
1.7 kristaps 1293: break;
1.14 kristaps 1294: else if (0 == stack && '}' == BUF(p)[*pos])
1.7 kristaps 1295: break;
1.14 kristaps 1296: else if (0 != stack && '}' == BUF(p)[*pos])
1.7 kristaps 1297: stack--;
1.14 kristaps 1298: else if ('{' == BUF(p)[*pos])
1.7 kristaps 1299: stack++;
1.14 kristaps 1300: advance(p, pos);
1.7 kristaps 1301: }
1302: if (stack)
1303: texiwarn(p, "unterminated macro "
1304: "in macro arguments");
1.14 kristaps 1305: if ((end = *pos) == BUFSZ(p))
1.7 kristaps 1306: break;
1307: /* Test for zero-length '{ }'. */
1.14 kristaps 1308: if (start == end && '}' == BUF(p)[*pos] && 0 == *argsz)
1.7 kristaps 1309: break;
1310: /* FIXME: use reallocarray. */
1311: args = realloc
1312: (args, sizeof(char *) *
1313: (*argsz + 1));
1314: if (NULL == args)
1315: texiabort(p, NULL);
1316: args[*argsz] = malloc(end - start + 1);
1317: if (NULL == args[*argsz])
1318: texiabort(p, NULL);
1319: memcpy(args[*argsz],
1.14 kristaps 1320: &BUF(p)[start], end - start);
1.7 kristaps 1321: args[*argsz][end - start] = '\0';
1322: (*argsz)++;
1.14 kristaps 1323: if ('}' == BUF(p)[*pos])
1.7 kristaps 1324: break;
1.14 kristaps 1325: advance(p, pos);
1.7 kristaps 1326: }
1327:
1.14 kristaps 1328: if (*pos == BUFSZ(p))
1.7 kristaps 1329: texierr(p, "unterminated arguments");
1.14 kristaps 1330: assert('}' == BUF(p)[*pos]);
1331: advance(p, pos);
1.7 kristaps 1332: return(args);
1.2 kristaps 1333: }
CVSweb