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

Annotation of texi2mdoc/util.c, Revision 1.12

1.12    ! kristaps    1: /*     $Id: util.c,v 1.11 2015/02/23 17:24:51 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.
                    163:  * Makes sure we don't spurriously start a macro.
                    164:  * Adjusts our output status.
                    165:  * This shouldn't be called for macros: just for ordinary text.
                    166:  */
                    167: void
                    168: texiputchar(struct texi *p, char c)
                    169: {
                    170:
                    171:        if (p->ign)
                    172:                return;
                    173:
                    174:        if ('.' == c && 0 == p->outcol)
                    175:                fputs("\\&", stdout);
1.10      kristaps  176:        if ('\'' == c && 0 == p->outcol)
                    177:                fputs("\\&", stdout);
1.1       kristaps  178:
                    179:        putchar(c);
                    180:        p->seenvs = 0;
                    181:        if ('\n' == c) {
                    182:                p->outcol = 0;
                    183:                p->seenws = 0;
                    184:        } else
                    185:                p->outcol++;
                    186: }
                    187:
                    188: /*
                    189:  * Put multiple characters (see texiputchar()).
                    190:  * This shouldn't be called for macros: just for ordinary text.
                    191:  */
                    192: void
                    193: texiputchars(struct texi *p, const char *s)
                    194: {
                    195:
                    196:        while ('\0' != *s)
                    197:                texiputchar(p, *s++);
1.9       kristaps  198: }
                    199:
                    200: /*
                    201:  * This puts all characters onto the output stream but makes sure to
                    202:  * escape mdoc(7) slashes.
                    203:  */
                    204: void
                    205: texiputbuf(struct texi *p, const char *buf, size_t start, size_t end)
                    206: {
                    207:
                    208:        for ( ; start < end; start++) {
                    209:                texiputchar(p, buf[start]);
                    210:                if ('\\' == buf[start])
                    211:                        texiputchar(p, 'e');
                    212:        }
1.1       kristaps  213: }
                    214:
                    215: /*
                    216:  * Close an mdoc(7) macro opened with teximacroopen().
                    217:  * If there are no more macros on the line, prints a newline.
                    218:  */
                    219: void
                    220: teximacroclose(struct texi *p)
                    221: {
                    222:
                    223:        if (p->ign)
                    224:                return;
                    225:
                    226:        if (0 == --p->outmacro) {
                    227:                putchar('\n');
                    228:                p->outcol = p->seenws = 0;
                    229:        }
                    230: }
                    231:
                    232: /*
                    233:  * Open a mdoc(7) macro.
                    234:  * This is used for line macros, e.g., Qq [foo bar baz].
                    235:  * It can be invoked for nested macros, e.g., Qq Li foo .
                    236:  * TODO: flush-right punctuation (e.g., parenthesis).
                    237:  */
                    238: void
                    239: teximacroopen(struct texi *p, const char *s)
                    240: {
                    241:        int      rc;
                    242:
                    243:        if (p->ign)
                    244:                return;
                    245:
                    246:        if (p->outcol && 0 == p->outmacro) {
                    247:                putchar('\n');
                    248:                p->outcol = 0;
                    249:        }
                    250:
                    251:        if (0 == p->outmacro)
                    252:                putchar('.');
                    253:        else
                    254:                putchar(' ');
                    255:
                    256:        if (EOF != (rc = fputs(s, stdout)))
                    257:                p->outcol += rc;
                    258:
                    259:        putchar(' ');
                    260:        p->outcol++;
                    261:        p->outmacro++;
                    262:        p->seenws = 0;
                    263: }
                    264:
                    265: /*
                    266:  * Put a stadnalone mdoc(7) command with the trailing newline.
                    267:  */
                    268: void
                    269: teximacro(struct texi *p, const char *s)
                    270: {
                    271:
                    272:        if (p->ign)
                    273:                return;
                    274:
                    275:        if (p->outmacro)
                    276:                texierr(p, "\"%s\" in open line scope!?", s);
                    277:        if (p->literal)
                    278:                texierr(p, "\"%s\" in a literal scope!?", s);
                    279:
                    280:        if (p->outcol)
                    281:                putchar('\n');
                    282:
                    283:        putchar('.');
                    284:        puts(s);
                    285:        p->outcol = p->seenws = 0;
                    286: }
                    287:
                    288: /*
                    289:  * Introduce vertical space during normal (non-macro) input.
                    290:  */
                    291: void
                    292: texivspace(struct texi *p)
                    293: {
                    294:
1.5       kristaps  295:        if (p->seenvs || TEXILIST_TABLE == p->list)
1.1       kristaps  296:                return;
                    297:        teximacro(p, "Pp");
                    298:        p->seenvs = 1;
                    299: }
                    300:
                    301: /*
                    302:  * Advance by a single byte in the input stream, adjusting our location
                    303:  * in the current input file.
                    304:  */
                    305: void
                    306: advance(struct texi *p, const char *buf, size_t *pos)
                    307: {
                    308:
                    309:        if ('\n' == buf[*pos]) {
                    310:                p->files[p->filepos - 1].line++;
                    311:                p->files[p->filepos - 1].col = 0;
                    312:        } else
                    313:                p->files[p->filepos - 1].col++;
                    314:
                    315:        (*pos)++;
                    316: }
                    317:
                    318: /*
                    319:  * It's common to wait punctuation to float on the right side of macro
                    320:  * lines in mdoc(7), e.g., ".Em hello ) ."
                    321:  * This function does so, and should be called before teximacroclose().
                    322:  * It will detect that it's the last in the nested macros and
                    323:  * appropriately flush-left punctuation alongside the macro.
                    324:  */
                    325: void
                    326: texipunctuate(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    327: {
                    328:        size_t   start, end;
                    329:
                    330:        if (1 != p->outmacro)
                    331:                return;
                    332:
                    333:        for (start = end = *pos; end < sz; end++) {
                    334:                switch (buf[end]) {
                    335:                case (','):
                    336:                case (')'):
                    337:                case ('.'):
                    338:                case ('"'):
                    339:                case (':'):
                    340:                case ('!'):
                    341:                case ('?'):
                    342:                        continue;
                    343:                default:
                    344:                        break;
                    345:                }
                    346:                break;
                    347:        }
                    348:        if (end == *pos)
                    349:                return;
                    350:        if (end + 1 == sz || ' ' == buf[end] || '\n' == buf[end]) {
                    351:                for ( ; start < end; start++) {
                    352:                        texiputchar(p, ' ');
                    353:                        texiputchar(p, buf[start]);
                    354:                        advance(p, buf, pos);
                    355:                }
                    356:        }
                    357: }
                    358:
                    359: /*
                    360:  * Advance to the next non-whitespace word in the input stream.
                    361:  * If we're in literal mode, then print all of the whitespace as we're
                    362:  * doing so.
                    363:  */
                    364: static size_t
                    365: advancenext(struct texi *p, const char *buf, size_t sz, size_t *pos)
                    366: {
                    367:
                    368:        if (p->literal) {
                    369:                while (*pos < sz && ismspace(buf[*pos])) {
                    370:                        if (*pos && '\n' == buf[*pos] &&
                    371:                                '\\' == buf[*pos - 1])
                    372:                                texiputchar(p, 'e');
                    373:                        texiputchar(p, buf[*pos]);
                    374:                        advance(p, buf, pos);
                    375:                }
                    376:                return(*pos);
                    377:        }
                    378:
                    379:        while (*pos < sz && ismspace(buf[*pos])) {
                    380:                p->seenws = 1;
                    381:                /*
                    382:                 * If it looks like we've printed a double-line, then
                    383:                 * output a paragraph.
                    384:                 * FIXME: this is stupid.
                    385:                 */
                    386:                if (*pos && '\n' == buf[*pos] && '\n' == buf[*pos - 1])
                    387:                        texivspace(p);
                    388:                advance(p, buf, pos);
                    389:        }
                    390:        return(*pos);
                    391: }
                    392:
                    393: /*
                    394:  * Advance to the EOLN in the input stream.
                    395:  * NOTE: THIS SHOULD NOT BE CALLED ON BLANK TEXT, as it will read up to
                    396:  * the @\n.
                    397:  */
                    398: size_t
                    399: advanceeoln(struct texi *p, const char *buf,
                    400:        size_t sz, size_t *pos, int consumenl)
                    401: {
                    402:
                    403:        while (*pos < sz && '\n' != buf[*pos])
                    404:                advance(p, buf, pos);
                    405:        if (*pos < sz && consumenl)
                    406:                advance(p, buf, pos);
                    407:        return(*pos);
                    408: }
                    409:
                    410: /*
                    411:  * Advance to position "end", which is an absolute position in the
                    412:  * current buffer greater than or equal to the current position.
                    413:  */
                    414: void
                    415: advanceto(struct texi *p, const char *buf, size_t *pos, size_t end)
                    416: {
                    417:
                    418:        assert(*pos <= end);
                    419:        while (*pos < end)
                    420:                advance(p, buf, pos);
                    421: }
                    422:
1.7       kristaps  423: static void
                    424: texiexecmacro(struct texi *p, struct teximacro *m,
                    425:        const char *buf, size_t sz, size_t *pos)
                    426: {
1.11      kristaps  427:        size_t            valsz, realsz, aasz, asz,
                    428:                           ssz, i, j, k, start, end;
                    429:        char             *val;
                    430:        char            **args;
                    431:        const char       *cp;
1.7       kristaps  432:
1.8       kristaps  433:        args = argparse(p, buf, sz, pos, &asz, m->argsz);
1.7       kristaps  434:        if (asz != m->argsz)
                    435:                texiwarn(p, "invalid macro argument length");
                    436:        aasz = asz < m->argsz ? asz : m->argsz;
                    437:
                    438:        if (0 == aasz) {
1.8       kristaps  439:                parsemembuf(p, m->value, strlen(m->value));
1.7       kristaps  440:                return;
                    441:        }
                    442:
                    443:        valsz = realsz = strlen(m->value);
                    444:        val = strdup(m->value);
                    445:
                    446:        for (i = j = 0; i < realsz; i++) {
                    447:                /* Parse blindly til the backslash delimiter. */
                    448:                if ('\\' != m->value[i]) {
                    449:                        val[j++] = m->value[i];
                    450:                        val[j] = '\0';
                    451:                        continue;
                    452:                } else if (i == realsz - 1)
                    453:                        texierr(p, "trailing argument name delimiter");
                    454:
                    455:                /* Double-backslash is escaped. */
                    456:                if ('\\' == m->value[i + 1]) {
                    457:                        val[j++] = m->value[i++];
                    458:                        val[j] = '\0';
                    459:                        continue;
                    460:                }
                    461:
                    462:                assert('\\' == m->value[i] && i < realsz - 1);
                    463:
                    464:                /* Parse to terminating delimiter. */
                    465:                /* FIXME: embedded, escaped delimiters? */
                    466:                for (start = end = i + 1; end < realsz; end++)
                    467:                        if ('\\' == m->value[end])
                    468:                                break;
                    469:                if (end == realsz)
                    470:                        texierr(p, "unterminated argument name");
                    471:
                    472:                for (k = 0; k < aasz; k++) {
                    473:                        if ((ssz = strlen(m->args[k])) != (end - start))
                    474:                                continue;
                    475:                        if (strncmp(&m->value[start], m->args[k], ssz))
                    476:                                continue;
                    477:                        break;
                    478:                }
                    479:
                    480:                /*
                    481:                 * Argument didn't exist in argument table.
                    482:                 * No need to reallocate here: we just copy the text
                    483:                 * directly from the macro value into the buffer.
                    484:                 */
                    485:                if (k == aasz) {
                    486:                        for ( ; i < end; i++)
                    487:                                val[j++] = m->value[i];
                    488:                        assert('\\' == m->value[i]);
                    489:                        val[j++] = m->value[i];
                    490:                        val[j] = '\0';
                    491:                        continue;
                    492:                }
                    493:
                    494:                if (strlen(args[k]) > ssz) {
                    495:                        valsz += strlen(args[k]);
                    496:                        val = realloc(val, valsz + 1);
                    497:                        if (NULL == val)
                    498:                                texiabort(p, NULL);
                    499:                }
                    500:
1.11      kristaps  501:                for (cp = args[k]; '\0' != *cp; cp++)
                    502:                        val[j++] = *cp;
                    503:
                    504:                val[j] = '\0';
1.7       kristaps  505:                i = end;
                    506:        }
                    507:
1.8       kristaps  508:        parsemembuf(p, val, strlen(val));
1.7       kristaps  509:
                    510:        for (i = 0; i < asz; i++)
                    511:                free(args[i]);
                    512:        free(args);
                    513:        free(val);
                    514: }
                    515:
1.1       kristaps  516: /*
                    517:  * Output a free-form word in the input stream, progressing to the next
                    518:  * command or white-space.
                    519:  * This also will advance the input stream.
                    520:  */
                    521: static void
                    522: texiword(struct texi *p, const char *buf,
                    523:        size_t sz, size_t *pos, char extra)
                    524: {
                    525:
                    526:        if (p->seenws && 0 == p->outmacro &&
                    527:                 p->outcol > 72 && 0 == p->literal)
                    528:                texiputchar(p, '\n');
                    529:        /* FIXME: abstract this: we use it elsewhere. */
                    530:        if (p->seenws && p->outcol && 0 == p->literal)
                    531:                texiputchar(p, ' ');
                    532:
                    533:        p->seenws = 0;
                    534:
                    535:        while (*pos < sz && ! ismspace(buf[*pos])) {
                    536:                switch (buf[*pos]) {
                    537:                case ('@'):
                    538:                case ('}'):
                    539:                case ('{'):
                    540:                        return;
                    541:                }
                    542:                if ('\0' != extra && buf[*pos] == extra)
                    543:                        return;
                    544:                if (*pos < sz - 1 &&
                    545:                         '`' == buf[*pos] &&
                    546:                         '`' == buf[*pos + 1]) {
                    547:                        texiputchars(p, "\\(lq");
                    548:                        advance(p, buf, pos);
                    549:                } else if (*pos < sz - 1 &&
                    550:                         '\'' == buf[*pos] &&
                    551:                         '\'' == buf[*pos + 1]) {
                    552:                        texiputchars(p, "\\(rq");
                    553:                        advance(p, buf, pos);
1.10      kristaps  554:                } else if ('\\' == buf[*pos]) {
                    555:                        texiputchar(p, buf[*pos]);
                    556:                        texiputchar(p, 'e');
1.1       kristaps  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) {
                   1039:                /*
                   1040:                 * We're printing verbatim output.
                   1041:                 * Make sure it doesn't get interpreted as mdoc by
                   1042:                 * escaping escapes and making sure leading dots don't
                   1043:                 * trigger mdoc(7) expansion.
                   1044:                 */
                   1045:                for (i = 0; i < f->mapsz; i++) {
                   1046:                        if (i > 0 && '.' == f->map[i])
                   1047:                                if ('\n' == f->map[i - 1])
                   1048:                                        fputs("\\&", stdout);
                   1049:                        putchar(f->map[i]);
                   1050:                        if ('\\' == f->map[i])
                   1051:                                putchar('e');
                   1052:                }
                   1053:        } else
                   1054:                parseeof(p, f->map, f->mapsz);
                   1055:        texifilepop(p);
                   1056: }
                   1057:
1.2       kristaps 1058: /*
                   1059:  * Look up the value to a stored pair's value starting in "buf" from
                   1060:  * start to end.
                   1061:  * Return the pointer to the value memory, which can be NULL if the
                   1062:  * pointer key does not exist.
                   1063:  * The pointer can point to NULL if the value has been unset.
                   1064:  */
                   1065: static char **
                   1066: valuequery(const struct texi *p,
                   1067:        const char *buf, size_t start, size_t end)
                   1068: {
                   1069:        size_t   i, sz, len;
                   1070:
                   1071:        assert(end >= start);
                   1072:        /* Ignore zero-length. */
                   1073:        if (0 == (len = (end - start)))
                   1074:                return(NULL);
                   1075:        for (i = 0; i < p->valsz; i++) {
                   1076:                sz = strlen(p->vals[i].key);
                   1077:                if (sz != len)
                   1078:                        continue;
                   1079:                if (0 == strncmp(p->vals[i].key, &buf[start], len))
                   1080:                        return(&p->vals[i].value);
                   1081:        }
                   1082:        return(NULL);
                   1083: }
                   1084:
                   1085: /*
                   1086:  * Parse a key until the end of line, e.g., @clear foo\n, and return the
                   1087:  * pointer to its value via valuequery().
                   1088:  */
                   1089: static char **
                   1090: valuelquery(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1091: {
                   1092:        size_t    start, end;
                   1093:        char    **ret;
                   1094:
                   1095:        while (*pos < sz && isws(buf[*pos]))
                   1096:                advance(p, buf, pos);
                   1097:        if (*pos == sz)
                   1098:                return(NULL);
                   1099:        for (start = end = *pos; end < sz; end++)
                   1100:                if ('\n' == buf[end])
                   1101:                        break;
                   1102:        advanceto(p, buf, pos, end);
                   1103:        if (*pos < sz) {
                   1104:                assert('\n' == buf[*pos]);
                   1105:                advance(p, buf, pos);
                   1106:        }
                   1107:        if (NULL == (ret = valuequery(p, buf, start, end)))
                   1108:                return(NULL);
                   1109:        return(ret);
                   1110: }
                   1111:
                   1112: void
                   1113: valuelclear(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1114: {
                   1115:        char    **ret;
                   1116:
                   1117:        if (NULL == (ret = valuelquery(p, buf, sz, pos)))
                   1118:                return;
                   1119:        free(*ret);
                   1120:        *ret = NULL;
                   1121: }
                   1122:
                   1123: const char *
                   1124: valuellookup(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1125: {
                   1126:        char    **ret;
                   1127:
                   1128:        if (NULL == (ret = valuelquery(p, buf, sz, pos)))
                   1129:                return(NULL);
                   1130:        return(*ret);
                   1131: }
                   1132:
                   1133: /*
                   1134:  * Parse a key from a bracketed string, e.g., @value{foo}, and return
                   1135:  * the pointer to its value.
                   1136:  * If the returned pointer is NULL, either there was no string within
                   1137:  * the brackets (or no brackets), or the value was not found, or the
                   1138:  * value had previously been unset.
                   1139:  */
                   1140: const char *
                   1141: valueblookup(struct texi *p, const char *buf, size_t sz, size_t *pos)
                   1142: {
                   1143:        size_t    start, end;
                   1144:        char    **ret;
                   1145:
                   1146:        while (*pos < sz && isws(buf[*pos]))
                   1147:                advance(p, buf, pos);
                   1148:        if (*pos == sz || '{' != buf[*pos])
                   1149:                return(NULL);
                   1150:        advance(p, buf, pos);
                   1151:        for (start = end = *pos; end < sz; end++)
                   1152:                if ('}' == buf[end])
                   1153:                        break;
                   1154:        advanceto(p, buf, pos, end);
                   1155:        if (*pos < sz) {
                   1156:                assert('}' == buf[*pos]);
                   1157:                advance(p, buf, pos);
                   1158:        }
                   1159:        if (NULL == (ret = valuequery(p, buf, start, end)))
                   1160:                return(NULL);
                   1161:        return(*ret);
                   1162: }
                   1163:
                   1164: void
                   1165: valueadd(struct texi *p, char *key, char *val)
                   1166: {
                   1167:        size_t   i;
                   1168:
                   1169:        assert(NULL != key);
                   1170:        assert(NULL != val);
                   1171:
                   1172:        for (i = 0; i < p->valsz; i++)
                   1173:                if (0 == strcmp(p->vals[i].key, key))
                   1174:                        break;
                   1175:
                   1176:        if (i < p->valsz) {
                   1177:                free(key);
                   1178:                free(p->vals[i].value);
                   1179:                p->vals[i].value = val;
                   1180:        } else {
1.4       kristaps 1181:                /* FIXME: reallocarray() */
1.2       kristaps 1182:                p->vals = realloc(p->vals,
                   1183:                        (p->valsz + 1) *
                   1184:                         sizeof(struct texivalue));
1.4       kristaps 1185:                if (NULL == p->vals)
                   1186:                        texiabort(p, NULL);
1.2       kristaps 1187:                p->vals[p->valsz].key = key;
                   1188:                p->vals[p->valsz].value = val;
                   1189:                p->valsz++;
                   1190:        }
1.7       kristaps 1191: }
                   1192:
                   1193: /*
                   1194:  * Take the arguments to a macro, e.g., @foo{bar, baz, xyzzy} (or the
                   1195:  * declaration form, @macro foo {arg1, ...}) and textually convert it to
                   1196:  * an array of arguments of size "argsz".
                   1197:  * These need to be freed individually and as a whole.
                   1198:  * NOTE: this will puke on @, or @} macros, which can trick it into
                   1199:  * stopping argument parsing earlier.
                   1200:  * Ergo, textual: this doesn't interpret the arguments in any way.
                   1201:  */
                   1202: char **
                   1203: argparse(struct texi *p, const char *buf,
1.8       kristaps 1204:        size_t sz, size_t *pos, size_t *argsz, size_t hint)
1.7       kristaps 1205: {
                   1206:        char    **args;
                   1207:        size_t    start, end, stack;
                   1208:
                   1209:        while (*pos < sz && isws(buf[*pos]))
                   1210:                advance(p, buf, pos);
                   1211:
                   1212:        args = NULL;
                   1213:        *argsz = 0;
                   1214:
1.10      kristaps 1215:        if ('{' != buf[*pos] && hint) {
                   1216:                /*
                   1217:                 * Special case: if we encounter an unbracketed argument
                   1218:                 * and we're being invoked with non-zero arguments
                   1219:                 * (versus being set, i.e., hint>0), then parse until
                   1220:                 * the end of line.
                   1221:                 */
                   1222:                *argsz = 1;
                   1223:                args = calloc(1, sizeof(char *));
                   1224:                if (NULL == args)
                   1225:                        texiabort(p, NULL);
                   1226:                start = *pos;
                   1227:                while (*pos < sz) {
                   1228:                        if ('\n' == buf[*pos])
                   1229:                                break;
                   1230:                        advance(p, buf, pos);
                   1231:                }
                   1232:                args[0] = malloc(*pos - start + 1);
                   1233:                memcpy(args[0], &buf[start], *pos - start);
                   1234:                args[0][*pos - start] = '\0';
                   1235:                if (*pos < sz && '\n' == buf[*pos])
                   1236:                        advance(p, buf, pos);
                   1237:                return(args);
                   1238:        } else if ('{' != buf[*pos])
1.7       kristaps 1239:                return(args);
                   1240:
                   1241:        /* Parse til the closing '}', putting into the array. */
                   1242:        advance(p, buf, pos);
                   1243:        while (*pos < sz) {
                   1244:                while (*pos < sz && isws(buf[*pos]))
                   1245:                        advance(p, buf, pos);
                   1246:                start = *pos;
                   1247:                stack = 0;
                   1248:                while (*pos < sz) {
                   1249:                        /*
                   1250:                         * According to the manual, commas within
                   1251:                         * embedded commands are escaped.
                   1252:                         * We keep track of embedded-ness in the "stack"
                   1253:                         * state anyway, so this is free.
                   1254:                         */
1.8       kristaps 1255:                        if (',' == buf[*pos] && 0 == stack && 1 != hint)
1.7       kristaps 1256:                                break;
                   1257:                        else if (0 == stack && '}' == buf[*pos])
                   1258:                                break;
                   1259:                        else if (0 != stack && '}' == buf[*pos])
                   1260:                                stack--;
                   1261:                        else if ('{' == buf[*pos])
                   1262:                                stack++;
                   1263:                        advance(p, buf, pos);
                   1264:                }
                   1265:                if (stack)
                   1266:                        texiwarn(p, "unterminated macro "
                   1267:                                "in macro arguments");
                   1268:                if ((end = *pos) == sz)
                   1269:                        break;
                   1270:                /* Test for zero-length '{  }'. */
                   1271:                if (start == end && '}' == buf[*pos] && 0 == *argsz)
                   1272:                        break;
                   1273:                /* FIXME: use reallocarray. */
                   1274:                args = realloc
                   1275:                        (args, sizeof(char *) *
                   1276:                         (*argsz + 1));
                   1277:                if (NULL == args)
                   1278:                        texiabort(p, NULL);
                   1279:                args[*argsz] = malloc(end - start + 1);
                   1280:                if (NULL == args[*argsz])
                   1281:                        texiabort(p, NULL);
                   1282:                memcpy(args[*argsz],
                   1283:                        &buf[start], end - start);
                   1284:                args[*argsz][end - start] = '\0';
                   1285:                (*argsz)++;
                   1286:                if ('}' == buf[*pos])
                   1287:                        break;
                   1288:                advance(p, buf, pos);
                   1289:        }
                   1290:
                   1291:        if (*pos == sz)
                   1292:                texierr(p, "unterminated arguments");
                   1293:        assert('}' == buf[*pos]);
                   1294:        advance(p, buf, pos);
                   1295:        return(args);
1.2       kristaps 1296: }

CVSweb