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