[BACK]Return to util.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / texi2mdoc

Annotation of texi2mdoc/util.c, Revision 1.13

1.13    ! kristaps    1: /*     $Id: util.c,v 1.12 2015/02/23 22:50:11 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.12      kristaps   46:        if (TEXISRC_FILE == f->type)
                     47:                munmap(f->map, f->mapsz);
                     48:        else
                     49:                free(f->map);
1.1       kristaps   50: }
                     51:
1.7       kristaps   52: static void
                     53: teximacrofree(struct teximacro *p)
                     54: {
                     55:        size_t   i;
                     56:
                     57:        for (i = 0; i < p->argsz; i++)
                     58:                free(p->args[i]);
                     59:
                     60:        free(p->args);
                     61:        free(p->key);
                     62:        free(p->value);
                     63: }
                     64:
                     65: static void
                     66: texivaluefree(struct texivalue *p)
                     67: {
                     68:
                     69:        free(p->key);
                     70:        free(p->value);
                     71: }
                     72:
1.1       kristaps   73: /*
                     74:  * Unmap all files that we're currently using and free all resources
                     75:  * that we've allocated during the parse.
                     76:  * The utility should exit(...) after this is called.
                     77:  */
                     78: void
                     79: texiexit(struct texi *p)
                     80: {
                     81:        size_t   i;
                     82:
                     83:        /* Make sure we're newline-terminated. */
                     84:        if (p->outcol)
                     85:                putchar('\n');
                     86:
                     87:        /* Unmap all files. */
                     88:        while (p->filepos > 0)
                     89:                texifilepop(p);
                     90:
1.7       kristaps   91:        for (i = 0; i < p->macrosz; i++)
                     92:                teximacrofree(&p->macros[i]);
1.1       kristaps   93:        for (i = 0; i < p->dirsz; i++)
                     94:                free(p->dirs[i]);
1.4       kristaps   95:        for (i = 0; i < p->indexsz; i++)
                     96:                free(p->indexs[i]);
1.7       kristaps   97:        for (i = 0; i < p->valsz; i++)
                     98:                texivaluefree(&p->vals[i]);
1.4       kristaps   99:
1.7       kristaps  100:        free(p->macros);
1.1       kristaps  101:        free(p->vals);
1.4       kristaps  102:        free(p->indexs);
1.1       kristaps  103:        free(p->dirs);
                    104:        free(p->subtitle);
                    105:        free(p->title);
                    106: }
                    107:
                    108: /*
                    109:  * Fatal error: unmap all files and exit.
                    110:  * The "errstring" is passed to perror(3).
                    111:  */
                    112: void
                    113: texiabort(struct texi *p, const char *errstring)
                    114: {
                    115:
                    116:        perror(errstring);
                    117:        texiexit(p);
                    118:        exit(EXIT_FAILURE);
                    119: }
                    120:
                    121: /*
                    122:  * Print a generic warning message (to stderr) tied to our current
                    123:  * location in the parse sequence.
                    124:  */
                    125: void
                    126: texiwarn(const struct texi *p, const char *fmt, ...)
                    127: {
                    128:        va_list  ap;
                    129:
                    130:        fprintf(stderr, "%s:%zu:%zu: warning: ",
                    131:                p->files[p->filepos - 1].name,
                    132:                p->files[p->filepos - 1].line + 1,
                    133:                p->files[p->filepos - 1].col + 1);
                    134:        va_start(ap, fmt);
                    135:        vfprintf(stderr, fmt, ap);
                    136:        va_end(ap);
                    137:        fputc('\n', stderr);
                    138: }
                    139:
                    140: /*
                    141:  * Print an error message (to stderr) tied to our current location in
                    142:  * the parse sequence, invoke texiexit(), then die.
                    143:  */
                    144: void
                    145: texierr(struct texi *p, const char *fmt, ...)
                    146: {
                    147:        va_list  ap;
                    148:
                    149:        fprintf(stderr, "%s:%zu:%zu: error: ",
                    150:                p->files[p->filepos - 1].name,
                    151:                p->files[p->filepos - 1].line + 1,
                    152:                p->files[p->filepos - 1].col + 1);
                    153:        va_start(ap, fmt);
                    154:        vfprintf(stderr, fmt, ap);
                    155:        va_end(ap);
                    156:        fputc('\n', stderr);
                    157:        texiexit(p);
                    158:        exit(EXIT_FAILURE);
                    159: }
                    160:
                    161: /*
                    162:  * Put a single data character to the output if we're not ignoring.
1.13    ! kristaps  163:  * Escape starting a line with a control character and slashes.
1.1       kristaps  164:  */
                    165: void
                    166: texiputchar(struct texi *p, char c)
                    167: {
                    168:
                    169:        if (p->ign)
                    170:                return;
                    171:        if ('.' == c && 0 == p->outcol)
                    172:                fputs("\\&", stdout);
1.10      kristaps  173:        if ('\'' == c && 0 == p->outcol)
                    174:                fputs("\\&", stdout);
1.1       kristaps  175:
                    176:        putchar(c);
1.13    ! kristaps  177:        if ('\\' == c)
        !           178:                putchar('e');
1.1       kristaps  179:        p->seenvs = 0;
                    180:        if ('\n' == c) {
                    181:                p->outcol = 0;
                    182:                p->seenws = 0;
                    183:        } else
                    184:                p->outcol++;
                    185: }
                    186:
                    187: /*
1.13    ! kristaps  188:  * Put an opaque series of characters.
        !           189:  * Characters starting a line with a control character are escaped, but
        !           190:  * that's it, so don't use this for non-controlled sequences of text.
1.1       kristaps  191:  */
                    192: void
                    193: texiputchars(struct texi *p, const char *s)
                    194: {
                    195:
1.13    ! kristaps  196:        if (p->ign)
        !           197:                return;
        !           198:        if ('.' == *s && 0 == p->outcol)
        !           199:                fputs("\\&", stdout);
        !           200:        if ('\'' == *s && 0 == p->outcol)
        !           201:                fputs("\\&", stdout);
        !           202:        p->outcol += fputs(s, stdout);
        !           203:        p->seenvs = 0;
1.9       kristaps  204: }
                    205:
                    206: /*
                    207:  * This puts all characters onto the output stream but makes sure to
                    208:  * escape mdoc(7) slashes.
                    209:  */
                    210: void
                    211: texiputbuf(struct texi *p, const char *buf, size_t start, size_t end)
                    212: {
                    213:
                    214:        for ( ; start < end; start++) {
                    215:                texiputchar(p, buf[start]);
                    216:                if ('\\' == buf[start])
                    217:                        texiputchar(p, 'e');
                    218:        }
1.1       kristaps  219: }
                    220:
                    221: /*
                    222:  * Close an mdoc(7) macro opened with teximacroopen().
                    223:  * If there are no more macros on the line, prints a newline.
                    224:  */
                    225: void
                    226: teximacroclose(struct texi *p)
                    227: {
                    228:
                    229:        if (p->ign)
                    230:                return;
                    231:
                    232:        if (0 == --p->outmacro) {
                    233:                putchar('\n');
                    234:                p->outcol = p->seenws = 0;
                    235:        }
                    236: }
                    237:
                    238: /*
                    239:  * Open a mdoc(7) macro.
                    240:  * This is used for line macros, e.g., Qq [foo bar baz].
                    241:  * It can be invoked for nested macros, e.g., Qq Li foo .
                    242:  * TODO: flush-right punctuation (e.g., parenthesis).
                    243:  */
                    244: void
                    245: teximacroopen(struct texi *p, const char *s)
                    246: {
                    247:        int      rc;
                    248:
                    249:        if (p->ign)
                    250:                return;
                    251:
                    252:        if (p->outcol && 0 == p->outmacro) {
                    253:                putchar('\n');
                    254:                p->outcol = 0;
                    255:        }
                    256:
                    257:        if (0 == p->outmacro)
                    258:                putchar('.');
                    259:        else
                    260:                putchar(' ');
                    261:
                    262:        if (EOF != (rc = fputs(s, stdout)))
                    263:                p->outcol += rc;
                    264:
                    265:        putchar(' ');
                    266:        p->outcol++;
                    267:        p->outmacro++;
                    268:        p->seenws = 0;
                    269: }
                    270:
                    271: /*
                    272:  * Put a stadnalone mdoc(7) command with the trailing newline.
                    273:  */
                    274: void
                    275: teximacro(struct texi *p, const char *s)
                    276: {
                    277:
                    278:        if (p->ign)
                    279:                return;
                    280:
                    281:        if (p->outmacro)
                    282:                texierr(p, "\"%s\" in open line scope!?", s);
                    283:        if (p->literal)
                    284:                texierr(p, "\"%s\" in a literal scope!?", s);
                    285:
                    286:        if (p->outcol)
                    287:                putchar('\n');
                    288:
                    289:        putchar('.');
                    290:        puts(s);
                    291:        p->outcol = p->seenws = 0;
                    292: }
                    293:
                    294: /*
                    295:  * Introduce vertical space during normal (non-macro) input.
                    296:  */
                    297: void
                    298: texivspace(struct texi *p)
                    299: {
                    300:
1.5       kristaps  301:        if (p->seenvs || TEXILIST_TABLE == p->list)
1.1       kristaps  302:                return;
                    303:        teximacro(p, "Pp");
                    304:        p->seenvs = 1;
                    305: }
                    306:
                    307: /*
                    308:  * Advance by a single byte in the input stream, adjusting our location
                    309:  * in the current input file.
                    310:  */
                    311: void
                    312: advance(struct texi *p, const char *buf, size_t *pos)
                    313: {
                    314:
                    315:        if ('\n' == buf[*pos]) {
                    316:                p->files[p->filepos - 1].line++;
                    317:                p->files[p->filepos - 1].col = 0;
                    318:        } else
                    319:                p->files[p->filepos - 1].col++;
                    320:
                    321:        (*pos)++;
                    322: }
                    323:
                    324: /*
                    325:  * It's common to wait punctuation to float on the right side of macro
                    326:  * lines in mdoc(7), e.g., ".Em hello ) ."
                    327:  * This function does so, and should be called before teximacroclose().
                    328:  * It will detect that it's the last in the nested macros and
                    329:  * appropriately flush-left punctuation alongside the macro.
                    330:  */
                    331: void
                    332: texipunctuate(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    333: {
                    334:        size_t   start, end;
                    335:
                    336:        if (1 != p->outmacro)
                    337:                return;
                    338:
                    339:        for (start = end = *pos; end < sz; end++) {
                    340:                switch (buf[end]) {
                    341:                case (','):
                    342:                case (')'):
                    343:                case ('.'):
                    344:                case ('"'):
                    345:                case (':'):
                    346:                case ('!'):
                    347:                case ('?'):
                    348:                        continue;
                    349:                default:
                    350:                        break;
                    351:                }
                    352:                break;
                    353:        }
                    354:        if (end == *pos)
                    355:                return;
                    356:        if (end + 1 == sz || ' ' == buf[end] || '\n' == buf[end]) {
                    357:                for ( ; start < end; start++) {
                    358:                        texiputchar(p, ' ');
                    359:                        texiputchar(p, buf[start]);
                    360:                        advance(p, buf, pos);
                    361:                }
                    362:        }
                    363: }
                    364:
                    365: /*
                    366:  * Advance to the next non-whitespace word in the input stream.
                    367:  * If we're in literal mode, then print all of the whitespace as we're
                    368:  * doing so.
                    369:  */
                    370: static size_t
                    371: advancenext(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    372: {
                    373:
                    374:        if (p->literal) {
                    375:                while (*pos < sz && ismspace(buf[*pos])) {
                    376:                        texiputchar(p, buf[*pos]);
                    377:                        advance(p, buf, pos);
                    378:                }
                    379:                return(*pos);
                    380:        }
                    381:
                    382:        while (*pos < sz && ismspace(buf[*pos])) {
                    383:                p->seenws = 1;
                    384:                /*
                    385:                 * If it looks like we've printed a double-line, then
                    386:                 * output a paragraph.
                    387:                 * FIXME: this is stupid.
                    388:                 */
                    389:                if (*pos && '\n' == buf[*pos] && '\n' == buf[*pos - 1])
                    390:                        texivspace(p);
                    391:                advance(p, buf, pos);
                    392:        }
                    393:        return(*pos);
                    394: }
                    395:
                    396: /*
                    397:  * Advance to the EOLN in the input stream.
                    398:  * NOTE: THIS SHOULD NOT BE CALLED ON BLANK TEXT, as it will read up to
                    399:  * the @\n.
                    400:  */
                    401: size_t
                    402: advanceeoln(struct texi *p, const char *buf,
                    403:        size_t sz, size_t *pos, int consumenl)
                    404: {
                    405:
                    406:        while (*pos < sz && '\n' != buf[*pos])
                    407:                advance(p, buf, pos);
                    408:        if (*pos < sz && consumenl)
                    409:                advance(p, buf, pos);
                    410:        return(*pos);
                    411: }
                    412:
                    413: /*
                    414:  * Advance to position "end", which is an absolute position in the
                    415:  * current buffer greater than or equal to the current position.
                    416:  */
                    417: void
                    418: advanceto(struct texi *p, const char *buf, size_t *pos, size_t end)
                    419: {
                    420:
                    421:        assert(*pos <= end);
                    422:        while (*pos < end)
                    423:                advance(p, buf, pos);
                    424: }
                    425:
1.7       kristaps  426: static void
                    427: texiexecmacro(struct texi *p, struct teximacro *m,
                    428:        const char *buf, size_t sz, size_t *pos)
                    429: {
1.11      kristaps  430:        size_t            valsz, realsz, aasz, asz,
                    431:                           ssz, i, j, k, start, end;
                    432:        char             *val;
                    433:        char            **args;
                    434:        const char       *cp;
1.7       kristaps  435:
1.8       kristaps  436:        args = argparse(p, buf, sz, pos, &asz, m->argsz);
1.7       kristaps  437:        if (asz != m->argsz)
                    438:                texiwarn(p, "invalid macro argument length");
                    439:        aasz = asz < m->argsz ? asz : m->argsz;
                    440:
                    441:        if (0 == aasz) {
1.8       kristaps  442:                parsemembuf(p, m->value, strlen(m->value));
1.7       kristaps  443:                return;
                    444:        }
                    445:
                    446:        valsz = realsz = strlen(m->value);
                    447:        val = strdup(m->value);
                    448:
                    449:        for (i = j = 0; i < realsz; i++) {
                    450:                /* Parse blindly til the backslash delimiter. */
                    451:                if ('\\' != m->value[i]) {
                    452:                        val[j++] = m->value[i];
                    453:                        val[j] = '\0';
                    454:                        continue;
                    455:                } else if (i == realsz - 1)
                    456:                        texierr(p, "trailing argument name delimiter");
                    457:
                    458:                /* Double-backslash is escaped. */
                    459:                if ('\\' == m->value[i + 1]) {
                    460:                        val[j++] = m->value[i++];
                    461:                        val[j] = '\0';
                    462:                        continue;
                    463:                }
                    464:
                    465:                assert('\\' == m->value[i] && i < realsz - 1);
                    466:
                    467:                /* Parse to terminating delimiter. */
                    468:                /* FIXME: embedded, escaped delimiters? */
                    469:                for (start = end = i + 1; end < realsz; end++)
                    470:                        if ('\\' == m->value[end])
                    471:                                break;
                    472:                if (end == realsz)
                    473:                        texierr(p, "unterminated argument name");
                    474:
                    475:                for (k = 0; k < aasz; k++) {
                    476:                        if ((ssz = strlen(m->args[k])) != (end - start))
                    477:                                continue;
                    478:                        if (strncmp(&m->value[start], m->args[k], ssz))
                    479:                                continue;
                    480:                        break;
                    481:                }
                    482:
                    483:                /*
                    484:                 * Argument didn't exist in argument table.
                    485:                 * No need to reallocate here: we just copy the text
                    486:                 * directly from the macro value into the buffer.
                    487:                 */
                    488:                if (k == aasz) {
                    489:                        for ( ; i < end; i++)
                    490:                                val[j++] = m->value[i];
                    491:                        assert('\\' == m->value[i]);
                    492:                        val[j++] = m->value[i];
                    493:                        val[j] = '\0';
                    494:                        continue;
                    495:                }
                    496:
                    497:                if (strlen(args[k]) > ssz) {
                    498:                        valsz += strlen(args[k]);
                    499:                        val = realloc(val, valsz + 1);
                    500:                        if (NULL == val)
                    501:                                texiabort(p, NULL);
                    502:                }
                    503:
1.11      kristaps  504:                for (cp = args[k]; '\0' != *cp; cp++)
                    505:                        val[j++] = *cp;
                    506:
                    507:                val[j] = '\0';
1.7       kristaps  508:                i = end;
                    509:        }
                    510:
1.8       kristaps  511:        parsemembuf(p, val, strlen(val));
1.7       kristaps  512:
                    513:        for (i = 0; i < asz; i++)
                    514:                free(args[i]);
                    515:        free(args);
                    516:        free(val);
                    517: }
                    518:
1.1       kristaps  519: /*
                    520:  * Output a free-form word in the input stream, progressing to the next
                    521:  * command or white-space.
                    522:  * This also will advance the input stream.
                    523:  */
                    524: static void
                    525: texiword(struct texi *p, const char *buf,
                    526:        size_t sz, size_t *pos, char extra)
                    527: {
                    528:
                    529:        if (p->seenws && 0 == p->outmacro &&
                    530:                 p->outcol > 72 && 0 == p->literal)
                    531:                texiputchar(p, '\n');
                    532:        /* FIXME: abstract this: we use it elsewhere. */
                    533:        if (p->seenws && p->outcol && 0 == p->literal)
                    534:                texiputchar(p, ' ');
                    535:
                    536:        p->seenws = 0;
                    537:
                    538:        while (*pos < sz && ! ismspace(buf[*pos])) {
                    539:                switch (buf[*pos]) {
                    540:                case ('@'):
                    541:                case ('}'):
                    542:                case ('{'):
                    543:                        return;
                    544:                }
                    545:                if ('\0' != extra && buf[*pos] == extra)
                    546:                        return;
                    547:                if (*pos < sz - 1 &&
                    548:                         '`' == buf[*pos] &&
                    549:                         '`' == buf[*pos + 1]) {
                    550:                        texiputchars(p, "\\(lq");
                    551:                        advance(p, buf, pos);
                    552:                } else if (*pos < sz - 1 &&
                    553:                         '\'' == buf[*pos] &&
                    554:                         '\'' == buf[*pos + 1]) {
                    555:                        texiputchars(p, "\\(rq");
                    556:                        advance(p, buf, pos);
                    557:                } else
                    558:                        texiputchar(p, buf[*pos]);
                    559:                advance(p, buf, pos);
                    560:        }
                    561: }
                    562:
                    563: /*
                    564:  * Look up the command at position "pos" in the buffer, returning it (or
                    565:  * TEXICMD__MAX if none found) and setting "end" to be the absolute
                    566:  * index after the command name.
                    567:  */
                    568: enum texicmd
1.7       kristaps  569: texicmd(struct texi *p, const char *buf, size_t pos,
                    570:        size_t sz, size_t *end, struct teximacro **macro)
1.1       kristaps  571: {
1.4       kristaps  572:        size_t   i, len, toksz;
1.1       kristaps  573:
                    574:        assert('@' == buf[pos]);
                    575:
1.7       kristaps  576:        if (NULL != macro)
                    577:                *macro = NULL;
                    578:
1.1       kristaps  579:        if ((*end = pos) == sz)
                    580:                return(TEXICMD__MAX);
                    581:        else if ((*end = ++pos) == sz)
                    582:                return(TEXICMD__MAX);
                    583:
                    584:        /* Alphabetic commands are special. */
                    585:        if ( ! isalpha(buf[pos])) {
                    586:                if ((*end = pos + 1) == sz)
                    587:                        return(TEXICMD__MAX);
                    588:                for (i = 0; i < TEXICMD__MAX; i++) {
                    589:                        if (1 != texitoks[i].len)
                    590:                                continue;
                    591:                        if (0 == strncmp(texitoks[i].tok, &buf[pos], 1))
                    592:                                return(i);
                    593:                }
                    594:                texiwarn(p, "bad command: @%c", buf[pos]);
                    595:                return(TEXICMD__MAX);
                    596:        }
                    597:
1.4       kristaps  598:        /* Scan to the end of the possible command name. */
1.1       kristaps  599:        for (*end = pos; *end < sz && ! ismspace(buf[*end]); (*end)++)
                    600:                if ((*end > pos && ('@' == buf[*end] ||
                    601:                          '{' == buf[*end] || '}' == buf[*end])))
                    602:                        break;
                    603:
1.4       kristaps  604:        /* Look for the command. */
1.1       kristaps  605:        len = *end - pos;
                    606:        for (i = 0; i < TEXICMD__MAX; i++) {
                    607:                if (len != texitoks[i].len)
                    608:                        continue;
                    609:                if (0 == strncmp(texitoks[i].tok, &buf[pos], len))
                    610:                        return(i);
                    611:        }
                    612:
1.4       kristaps  613:        /* Look for it in our indices. */
                    614:        for (i = 0; i < p->indexsz; i++) {
                    615:                toksz = strlen(p->indexs[i]);
                    616:                if (len != 5 + toksz)
                    617:                        continue;
                    618:                if (strncmp(&buf[pos], p->indexs[i], toksz))
                    619:                        continue;
                    620:                if (0 == strncmp(&buf[pos + toksz], "index", 5))
1.7       kristaps  621:                        return(TEXICMD_USER_INDEX);
                    622:        }
                    623:
                    624:        for (i = 0; i < p->macrosz; i++) {
                    625:                if (len != strlen(p->macros[i].key))
                    626:                        continue;
                    627:                if (strncmp(&buf[pos], p->macros[i].key, len))
                    628:                        continue;
                    629:                if (NULL != macro)
                    630:                        *macro = &p->macros[i];
                    631:                return(TEXICMD__MAX);
1.4       kristaps  632:        }
                    633:
1.1       kristaps  634:        texiwarn(p, "bad command: @%.*s", (int)len, &buf[pos]);
                    635:        return(TEXICMD__MAX);
                    636: }
                    637:
                    638: /*
                    639:  * Parse an argument from a bracketed command, e.g., @url{foo, baz}.
                    640:  * Num should be set to the argument we're currently parsing, although
                    641:  * it suffixes for it to be zero or non-zero.
                    642:  * This will return 1 if there are more arguments, 0 otherwise.
                    643:  * This will stop (returning 0) in the event of EOF or if we're not at a
                    644:  * bracket for the zeroth parse.
                    645:  */
                    646: int
                    647: parsearg(struct texi *p, const char *buf,
                    648:        size_t sz, size_t *pos, size_t num)
                    649: {
1.7       kristaps  650:        size_t            end;
                    651:        enum texicmd      cmd;
                    652:        struct teximacro *macro;
1.1       kristaps  653:
                    654:        while (*pos < sz && ismspace(buf[*pos]))
                    655:                advance(p, buf, pos);
                    656:        if (*pos == sz || (0 == num && '{' != buf[*pos]))
                    657:                return(0);
                    658:        if (0 == num)
                    659:                advance(p, buf, pos);
                    660:
                    661:        while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
                    662:                switch (buf[*pos]) {
                    663:                case (','):
                    664:                        advance(p, buf, pos);
                    665:                        return(1);
                    666:                case ('}'):
                    667:                        advance(p, buf, pos);
                    668:                        return(0);
                    669:                case ('{'):
                    670:                        if (0 == p->ign)
                    671:                                texiwarn(p, "unexpected \"{\"");
                    672:                        advance(p, buf, pos);
                    673:                        continue;
                    674:                case ('@'):
                    675:                        break;
                    676:                default:
                    677:                        texiword(p, buf, sz, pos, ',');
                    678:                        continue;
                    679:                }
                    680:
1.7       kristaps  681:                cmd = texicmd(p, buf, *pos, sz, &end, &macro);
1.1       kristaps  682:                advanceto(p, buf, pos, end);
1.7       kristaps  683:                if (NULL != macro)
                    684:                        texiexecmacro(p, macro, buf, sz, pos);
1.1       kristaps  685:                if (TEXICMD__MAX == cmd)
                    686:                        continue;
                    687:                if (NULL != texitoks[cmd].fp)
                    688:                        (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
                    689:        }
                    690:        return(0);
                    691: }
                    692:
                    693: /*
                    694:  * Parse until the end of a bracketed statement, e.g., @foo{bar baz}.
                    695:  * This will stop in the event of EOF or if we're not at a bracket.
                    696:  */
                    697: void
                    698: parsebracket(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    699: {
1.7       kristaps  700:        size_t            end;
                    701:        enum texicmd      cmd;
                    702:        struct teximacro *macro;
1.1       kristaps  703:
                    704:        while (*pos < sz && ismspace(buf[*pos]))
                    705:                advance(p, buf, pos);
                    706:
                    707:        if (*pos == sz || '{' != buf[*pos])
                    708:                return;
                    709:        advance(p, buf, pos);
                    710:
                    711:        while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
                    712:                switch (buf[*pos]) {
                    713:                case ('}'):
                    714:                        advance(p, buf, pos);
                    715:                        return;
                    716:                case ('{'):
                    717:                        if (0 == p->ign)
                    718:                                texiwarn(p, "unexpected \"{\"");
                    719:                        advance(p, buf, pos);
                    720:                        continue;
                    721:                case ('@'):
                    722:                        break;
                    723:                default:
                    724:                        texiword(p, buf, sz, pos, '\0');
                    725:                        continue;
                    726:                }
                    727:
1.7       kristaps  728:                cmd = texicmd(p, buf, *pos, sz, &end, &macro);
1.1       kristaps  729:                advanceto(p, buf, pos, end);
1.7       kristaps  730:                if (NULL != macro)
                    731:                        texiexecmacro(p, macro, buf, sz, pos);
1.1       kristaps  732:                if (TEXICMD__MAX == cmd)
                    733:                        continue;
                    734:                if (NULL != texitoks[cmd].fp)
                    735:                        (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
                    736:        }
                    737: }
                    738:
                    739: /*
                    740:  * This should be invoked when we're on a macro line and want to process
                    741:  * to the end of the current input line, doing all of our macros along
                    742:  * the way.
                    743:  */
                    744: void
                    745: parseeoln(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    746: {
1.7       kristaps  747:        size_t            end;
                    748:        enum texicmd      cmd;
                    749:        struct teximacro *macro;
1.1       kristaps  750:
                    751:        while (*pos < sz && '\n' != buf[*pos]) {
                    752:                while (*pos < sz && isws(buf[*pos])) {
                    753:                        p->seenws = 1;
                    754:                        if (p->literal)
                    755:                                texiputchar(p, buf[*pos]);
                    756:                        advance(p, buf, pos);
                    757:                }
                    758:                switch (buf[*pos]) {
                    759:                case ('}'):
                    760:                        if (0 == p->ign)
                    761:                                texiwarn(p, "unexpected \"}\"");
                    762:                        advance(p, buf, pos);
                    763:                        continue;
                    764:                case ('{'):
                    765:                        if (0 == p->ign)
                    766:                                texiwarn(p, "unexpected \"{\"");
                    767:                        advance(p, buf, pos);
                    768:                        continue;
                    769:                case ('@'):
                    770:                        break;
                    771:                default:
                    772:                        texiword(p, buf, sz, pos, '\0');
                    773:                        continue;
                    774:                }
                    775:
1.7       kristaps  776:                cmd = texicmd(p, buf, *pos, sz, &end, &macro);
1.1       kristaps  777:                advanceto(p, buf, pos, end);
1.7       kristaps  778:                if (NULL != macro)
                    779:                        texiexecmacro(p, macro, buf, sz, pos);
1.1       kristaps  780:                if (TEXICMD__MAX == cmd)
                    781:                        continue;
                    782:                if (NULL != texitoks[cmd].fp)
                    783:                        (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
                    784:        }
                    785: }
                    786:
                    787: /*
                    788:  * Parse a single word or command.
                    789:  * This will return immediately at the EOF.
                    790:  */
                    791: void
                    792: parsesingle(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    793: {
1.7       kristaps  794:        size_t            end;
                    795:        enum texicmd      cmd;
                    796:        struct teximacro *macro;
1.1       kristaps  797:
                    798:        if ((*pos = advancenext(p, buf, sz, pos)) >= sz)
                    799:                return;
                    800:
                    801:        switch (buf[*pos]) {
                    802:        case ('}'):
                    803:                if (0 == p->ign)
                    804:                        texiwarn(p, "unexpected \"}\"");
                    805:                advance(p, buf, pos);
                    806:                return;
                    807:        case ('{'):
                    808:                if (0 == p->ign)
                    809:                        texiwarn(p, "unexpected \"{\"");
                    810:                advance(p, buf, pos);
                    811:                return;
                    812:        case ('@'):
                    813:                break;
                    814:        default:
                    815:                texiword(p, buf, sz, pos, '\0');
                    816:                return;
                    817:        }
                    818:
1.7       kristaps  819:        cmd = texicmd(p, buf, *pos, sz, &end, &macro);
1.1       kristaps  820:        advanceto(p, buf, pos, end);
1.7       kristaps  821:        if (NULL != macro)
                    822:                texiexecmacro(p, macro, buf, sz, pos);
1.1       kristaps  823:        if (TEXICMD__MAX == cmd)
                    824:                return;
                    825:        if (NULL != texitoks[cmd].fp)
                    826:                (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
                    827: }
                    828:
                    829: /*
                    830:  * This is used in the @deffn type of command.
                    831:  * These have an arbitrary number of line arguments; however, these
                    832:  * arguments may or may not be surrounded by brackets.
                    833:  * In this function, we parse each one as either a bracketed or
                    834:  * non-bracketed argument, returning 0 when we've reached the end of
                    835:  * line or 1 otherwise.
                    836:  */
                    837: int
                    838: parselinearg(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    839: {
                    840:
                    841:        while (*pos < sz && isws(buf[*pos])) {
                    842:                p->seenws = 1;
                    843:                advance(p, buf, pos);
                    844:        }
                    845:
                    846:        if (*pos < sz && '{' == buf[*pos])
                    847:                parsebracket(p, buf, sz, pos);
1.3       kristaps  848:        else if (*pos < sz && '\n' != buf[*pos])
1.1       kristaps  849:                parsesingle(p, buf, sz, pos);
                    850:        else
                    851:                return(0);
                    852:
                    853:        return(1);
                    854: }
                    855:
                    856: /*
                    857:  * Parse til the end of the buffer.
                    858:  */
                    859: void
                    860: parseeof(struct texi *p, const char *buf, size_t sz)
                    861: {
                    862:        size_t   pos;
                    863:
                    864:        for (pos = 0; pos < sz; )
                    865:                parsesingle(p, buf, sz, &pos);
                    866: }
                    867:
                    868: /*
1.8       kristaps  869:  * This is like parseeof() except that it's to be invoked on memory
                    870:  * buffers while parsing a larger scope.
                    871:  * This is useful for parsing macro sequences.
                    872:  * The line, column, and name of the calling file context are saved, the
                    873:  * column and line reset, then all of these restored after parse.
                    874:  */
                    875: void
                    876: parsemembuf(struct texi *p, const char *buf, size_t sz)
                    877: {
                    878:        size_t           svln, svcol;
                    879:        const char      *svname;
                    880:
                    881:        svln = p->files[p->filepos - 1].line;
                    882:        svcol = p->files[p->filepos - 1].col;
                    883:        svname = p->files[p->filepos - 1].name;
                    884:
                    885:        p->files[p->filepos - 1].line = 0;
                    886:        p->files[p->filepos - 1].col = 0;
                    887:        p->files[p->filepos - 1].name = "<macro buffer>";
                    888:
                    889:        parseeof(p, buf, sz);
                    890:
                    891:        p->files[p->filepos - 1].line = svln;
                    892:        p->files[p->filepos - 1].col = svcol;
                    893:        p->files[p->filepos - 1].name = svname;
                    894: }
                    895:
                    896: /*
1.1       kristaps  897:  * Parse a block sequence until we have the "@end endtoken" command
                    898:  * invocation.
                    899:  * This will return immediately at EOF.
                    900:  */
                    901: void
                    902: parseto(struct texi *p, const char *buf,
                    903:        size_t sz, size_t *pos, const char *endtoken)
                    904: {
1.7       kristaps  905:        size_t            end;
                    906:        enum texicmd      cmd;
                    907:        size_t            endtoksz;
                    908:        struct teximacro *macro;
1.1       kristaps  909:
                    910:        endtoksz = strlen(endtoken);
                    911:        assert(endtoksz > 0);
                    912:
                    913:        while ((*pos = advancenext(p, buf, sz, pos)) < sz) {
                    914:                switch (buf[*pos]) {
                    915:                case ('}'):
                    916:                        if (0 == p->ign)
                    917:                                texiwarn(p, "unexpected \"}\"");
                    918:                        advance(p, buf, pos);
                    919:                        continue;
                    920:                case ('{'):
                    921:                        if (0 == p->ign)
                    922:                                texiwarn(p, "unexpected \"{\"");
                    923:                        advance(p, buf, pos);
                    924:                        continue;
                    925:                case ('@'):
                    926:                        break;
                    927:                default:
                    928:                        texiword(p, buf, sz, pos, '\0');
                    929:                        continue;
                    930:                }
                    931:
1.7       kristaps  932:                cmd = texicmd(p, buf, *pos, sz, &end, &macro);
1.1       kristaps  933:                advanceto(p, buf, pos, end);
                    934:                if (TEXICMD_END == cmd) {
                    935:                        while (*pos < sz && isws(buf[*pos]))
                    936:                                advance(p, buf, pos);
                    937:                        /*
                    938:                         * FIXME: check the full word, not just its
                    939:                         * initial substring!
                    940:                         */
                    941:                        if (sz - *pos >= endtoksz && 0 == strncmp
                    942:                                 (&buf[*pos], endtoken, endtoksz)) {
                    943:                                advanceeoln(p, buf, sz, pos, 0);
                    944:                                break;
                    945:                        }
                    946:                        if (0 == p->ign)
                    947:                                texiwarn(p, "unexpected \"end\"");
                    948:                        advanceeoln(p, buf, sz, pos, 0);
                    949:                        continue;
1.7       kristaps  950:                }
                    951:                if (NULL != macro)
                    952:                        texiexecmacro(p, macro, buf, sz, pos);
                    953:                if (TEXICMD__MAX == cmd)
                    954:                        continue;
                    955:                if (NULL != texitoks[cmd].fp)
                    956:                        (*texitoks[cmd].fp)(p, cmd, buf, sz, pos);
1.1       kristaps  957:        }
                    958: }
                    959:
                    960: /*
1.12      kristaps  961:  * Like parsefile() but used for reading from stdandard input.
                    962:  * This can only be called for the first file!
                    963:  */
                    964: void
                    965: parsestdin(struct texi *p)
                    966: {
                    967:        struct texifile *f;
                    968:        size_t           off;
                    969:        ssize_t          ssz;
                    970:
                    971:        assert(0 == p->filepos);
                    972:        f = &p->files[p->filepos];
                    973:        memset(f, 0, sizeof(struct texifile));
                    974:
                    975:        f->type = TEXISRC_STDIN;
                    976:        f->name = "<stdin>";
                    977:
                    978:        for (off = 0; ; off += (size_t)ssz) {
                    979:                if (off == f->mapsz) {
                    980:                        if (f->mapsz == (1U << 31))
                    981:                                texierr(p, "stdin buffer too long");
                    982:                        f->mapsz = f->mapsz > 65536 / 2 ?
                    983:                                2 * f->mapsz : 65536;
                    984:                        f->map = realloc(f->map, f->mapsz);
                    985:                        if (NULL == f->map)
                    986:                                texiabort(p, NULL);
                    987:                }
                    988:                ssz = read(STDIN_FILENO,
                    989:                        f->map + (int)off, f->mapsz - off);
                    990:                if (0 == ssz)
                    991:                        break;
                    992:                else if (-1 == ssz)
                    993:                        texiabort(p, NULL);
                    994:        }
                    995:
                    996:        p->filepos++;
                    997:        parseeof(p, f->map, off);
                    998:        texifilepop(p);
                    999: }
                   1000:
                   1001: /*
1.1       kristaps 1002:  * Memory-map the file "fname" and begin parsing it unless "parse" is
                   1003:  * zero, in which case we just dump the file to stdout (making sure it
                   1004:  * doesn't trip up mdoc(7) along the way).
                   1005:  * This can be called in a nested context.
                   1006:  */
                   1007: void
                   1008: parsefile(struct texi *p, const char *fname, int parse)
                   1009: {
                   1010:        struct texifile *f;
                   1011:        int              fd;
                   1012:        struct stat      st;
                   1013:        size_t           i;
                   1014:
1.5       kristaps 1015:        if (64 == p->filepos)
1.6       kristaps 1016:                texierr(p, "too many open files");
1.1       kristaps 1017:        f = &p->files[p->filepos];
                   1018:        memset(f, 0, sizeof(struct texifile));
                   1019:
1.12      kristaps 1020:        f->type = TEXISRC_FILE;
1.1       kristaps 1021:        f->name = fname;
                   1022:        if (-1 == (fd = open(fname, O_RDONLY, 0))) {
                   1023:                texiabort(p, fname);
                   1024:        } else if (-1 == fstat(fd, &st)) {
                   1025:                close(fd);
                   1026:                texiabort(p, fname);
                   1027:        }
                   1028:
                   1029:        f->mapsz = st.st_size;
                   1030:        f->map = mmap(NULL, f->mapsz,
                   1031:                PROT_READ, MAP_SHARED, fd, 0);
                   1032:        close(fd);
                   1033:
                   1034:        if (MAP_FAILED == f->map)
                   1035:                texiabort(p, fname);
                   1036:
                   1037:        p->filepos++;
                   1038:        if ( ! parse) {
1.13    ! kristaps 1039:                for (i = 0; i < f->mapsz; i++)
        !          1040:                        texiputchar(p, f->map[i]);
        !          1041:                if (p->outcol)
        !          1042:                        texiputchar(p, '\n');
1.1       kristaps 1043:        } else
                   1044:                parseeof(p, f->map, f->mapsz);
                   1045:        texifilepop(p);
                   1046: }
                   1047:
1.2       kristaps 1048: /*
                   1049:  * Look up the value to a stored pair's value starting in "buf" from
                   1050:  * start to end.
                   1051:  * Return the pointer to the value memory, which can be NULL if the
                   1052:  * pointer key does not exist.
                   1053:  * The pointer can point to NULL if the value has been unset.
                   1054:  */
                   1055: static char **
                   1056: valuequery(const struct texi *p,
                   1057:        const char *buf, size_t start, size_t end)
                   1058: {
                   1059:        size_t   i, sz, len;
                   1060:
                   1061:        assert(end >= start);
                   1062:        /* Ignore zero-length. */
                   1063:        if (0 == (len = (end - start)))
                   1064:                return(NULL);
                   1065:        for (i = 0; i < p->valsz; i++) {
                   1066:                sz = strlen(p->vals[i].key);
                   1067:                if (sz != len)
                   1068:                        continue;
                   1069:                if (0 == strncmp(p->vals[i].key, &buf[start], len))
                   1070:                        return(&p->vals[i].value);
                   1071:        }
                   1072:        return(NULL);
                   1073: }
                   1074:
                   1075: /*
                   1076:  * Parse a key until the end of line, e.g., @clear foo\n, and return the
                   1077:  * pointer to its value via valuequery().
                   1078:  */
                   1079: static char **
                   1080: valuelquery(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1081: {
                   1082:        size_t    start, end;
                   1083:        char    **ret;
                   1084:
                   1085:        while (*pos < sz && isws(buf[*pos]))
                   1086:                advance(p, buf, pos);
                   1087:        if (*pos == sz)
                   1088:                return(NULL);
                   1089:        for (start = end = *pos; end < sz; end++)
                   1090:                if ('\n' == buf[end])
                   1091:                        break;
                   1092:        advanceto(p, buf, pos, end);
                   1093:        if (*pos < sz) {
                   1094:                assert('\n' == buf[*pos]);
                   1095:                advance(p, buf, pos);
                   1096:        }
                   1097:        if (NULL == (ret = valuequery(p, buf, start, end)))
                   1098:                return(NULL);
                   1099:        return(ret);
                   1100: }
                   1101:
                   1102: void
                   1103: valuelclear(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1104: {
                   1105:        char    **ret;
                   1106:
                   1107:        if (NULL == (ret = valuelquery(p, buf, sz, pos)))
                   1108:                return;
                   1109:        free(*ret);
                   1110:        *ret = NULL;
                   1111: }
                   1112:
                   1113: const char *
                   1114: valuellookup(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1115: {
                   1116:        char    **ret;
                   1117:
                   1118:        if (NULL == (ret = valuelquery(p, buf, sz, pos)))
                   1119:                return(NULL);
                   1120:        return(*ret);
                   1121: }
                   1122:
                   1123: /*
                   1124:  * Parse a key from a bracketed string, e.g., @value{foo}, and return
                   1125:  * the pointer to its value.
                   1126:  * If the returned pointer is NULL, either there was no string within
                   1127:  * the brackets (or no brackets), or the value was not found, or the
                   1128:  * value had previously been unset.
                   1129:  */
                   1130: const char *
                   1131: valueblookup(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1132: {
                   1133:        size_t    start, end;
                   1134:        char    **ret;
                   1135:
                   1136:        while (*pos < sz && isws(buf[*pos]))
                   1137:                advance(p, buf, pos);
                   1138:        if (*pos == sz || '{' != buf[*pos])
                   1139:                return(NULL);
                   1140:        advance(p, buf, pos);
                   1141:        for (start = end = *pos; end < sz; end++)
                   1142:                if ('}' == buf[end])
                   1143:                        break;
                   1144:        advanceto(p, buf, pos, end);
                   1145:        if (*pos < sz) {
                   1146:                assert('}' == buf[*pos]);
                   1147:                advance(p, buf, pos);
                   1148:        }
                   1149:        if (NULL == (ret = valuequery(p, buf, start, end)))
                   1150:                return(NULL);
                   1151:        return(*ret);
                   1152: }
                   1153:
                   1154: void
                   1155: valueadd(struct texi *p, char *key, char *val)
                   1156: {
                   1157:        size_t   i;
                   1158:
                   1159:        assert(NULL != key);
                   1160:        assert(NULL != val);
                   1161:
                   1162:        for (i = 0; i < p->valsz; i++)
                   1163:                if (0 == strcmp(p->vals[i].key, key))
                   1164:                        break;
                   1165:
                   1166:        if (i < p->valsz) {
                   1167:                free(key);
                   1168:                free(p->vals[i].value);
                   1169:                p->vals[i].value = val;
                   1170:        } else {
1.4       kristaps 1171:                /* FIXME: reallocarray() */
1.2       kristaps 1172:                p->vals = realloc(p->vals,
                   1173:                        (p->valsz + 1) *
                   1174:                         sizeof(struct texivalue));
1.4       kristaps 1175:                if (NULL == p->vals)
                   1176:                        texiabort(p, NULL);
1.2       kristaps 1177:                p->vals[p->valsz].key = key;
                   1178:                p->vals[p->valsz].value = val;
                   1179:                p->valsz++;
                   1180:        }
1.7       kristaps 1181: }
                   1182:
                   1183: /*
                   1184:  * Take the arguments to a macro, e.g., @foo{bar, baz, xyzzy} (or the
                   1185:  * declaration form, @macro foo {arg1, ...}) and textually convert it to
                   1186:  * an array of arguments of size "argsz".
                   1187:  * These need to be freed individually and as a whole.
                   1188:  * NOTE: this will puke on @, or @} macros, which can trick it into
                   1189:  * stopping argument parsing earlier.
                   1190:  * Ergo, textual: this doesn't interpret the arguments in any way.
                   1191:  */
                   1192: char **
                   1193: argparse(struct texi *p, const char *buf,
1.8       kristaps 1194:        size_t sz, size_t *pos, size_t *argsz, size_t hint)
1.7       kristaps 1195: {
                   1196:        char    **args;
                   1197:        size_t    start, end, stack;
                   1198:
                   1199:        while (*pos < sz && isws(buf[*pos]))
                   1200:                advance(p, buf, pos);
                   1201:
                   1202:        args = NULL;
                   1203:        *argsz = 0;
                   1204:
1.10      kristaps 1205:        if ('{' != buf[*pos] && hint) {
                   1206:                /*
                   1207:                 * Special case: if we encounter an unbracketed argument
                   1208:                 * and we're being invoked with non-zero arguments
                   1209:                 * (versus being set, i.e., hint>0), then parse until
                   1210:                 * the end of line.
                   1211:                 */
                   1212:                *argsz = 1;
                   1213:                args = calloc(1, sizeof(char *));
                   1214:                if (NULL == args)
                   1215:                        texiabort(p, NULL);
                   1216:                start = *pos;
                   1217:                while (*pos < sz) {
                   1218:                        if ('\n' == buf[*pos])
                   1219:                                break;
                   1220:                        advance(p, buf, pos);
                   1221:                }
                   1222:                args[0] = malloc(*pos - start + 1);
                   1223:                memcpy(args[0], &buf[start], *pos - start);
                   1224:                args[0][*pos - start] = '\0';
                   1225:                if (*pos < sz && '\n' == buf[*pos])
                   1226:                        advance(p, buf, pos);
                   1227:                return(args);
                   1228:        } else if ('{' != buf[*pos])
1.7       kristaps 1229:                return(args);
                   1230:
                   1231:        /* Parse til the closing '}', putting into the array. */
                   1232:        advance(p, buf, pos);
                   1233:        while (*pos < sz) {
                   1234:                while (*pos < sz && isws(buf[*pos]))
                   1235:                        advance(p, buf, pos);
                   1236:                start = *pos;
                   1237:                stack = 0;
                   1238:                while (*pos < sz) {
                   1239:                        /*
                   1240:                         * According to the manual, commas within
                   1241:                         * embedded commands are escaped.
                   1242:                         * We keep track of embedded-ness in the "stack"
                   1243:                         * state anyway, so this is free.
                   1244:                         */
1.8       kristaps 1245:                        if (',' == buf[*pos] && 0 == stack && 1 != hint)
1.7       kristaps 1246:                                break;
                   1247:                        else if (0 == stack && '}' == buf[*pos])
                   1248:                                break;
                   1249:                        else if (0 != stack && '}' == buf[*pos])
                   1250:                                stack--;
                   1251:                        else if ('{' == buf[*pos])
                   1252:                                stack++;
                   1253:                        advance(p, buf, pos);
                   1254:                }
                   1255:                if (stack)
                   1256:                        texiwarn(p, "unterminated macro "
                   1257:                                "in macro arguments");
                   1258:                if ((end = *pos) == sz)
                   1259:                        break;
                   1260:                /* Test for zero-length '{  }'. */
                   1261:                if (start == end && '}' == buf[*pos] && 0 == *argsz)
                   1262:                        break;
                   1263:                /* FIXME: use reallocarray. */
                   1264:                args = realloc
                   1265:                        (args, sizeof(char *) *
                   1266:                         (*argsz + 1));
                   1267:                if (NULL == args)
                   1268:                        texiabort(p, NULL);
                   1269:                args[*argsz] = malloc(end - start + 1);
                   1270:                if (NULL == args[*argsz])
                   1271:                        texiabort(p, NULL);
                   1272:                memcpy(args[*argsz],
                   1273:                        &buf[start], end - start);
                   1274:                args[*argsz][end - start] = '\0';
                   1275:                (*argsz)++;
                   1276:                if ('}' == buf[*pos])
                   1277:                        break;
                   1278:                advance(p, buf, pos);
                   1279:        }
                   1280:
                   1281:        if (*pos == sz)
                   1282:                texierr(p, "unterminated arguments");
                   1283:        assert('}' == buf[*pos]);
                   1284:        advance(p, buf, pos);
                   1285:        return(args);
1.2       kristaps 1286: }

CVSweb