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