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

Annotation of texi2mdoc/main.c, Revision 1.43

1.43    ! kristaps    1: /*     $Id: main.c,v 1.42 2015/02/24 20:06:32 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:  */
1.37      kristaps   17: #if defined(__linux__) || defined(__MINT__)
                     18: # define _GNU_SOURCE /* memmem */
                     19: #endif
1.1       kristaps   20: #include <assert.h>
                     21: #include <ctype.h>
                     22: #include <getopt.h>
1.2       kristaps   23: #include <libgen.h>
                     24: #include <limits.h>
1.1       kristaps   25: #include <stdarg.h>
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
1.10      kristaps   29: #include <time.h>
1.1       kristaps   30:
1.24      kristaps   31: #include "extern.h"
1.1       kristaps   32:
1.23      kristaps   33: #define        SECTSZ 4
                     34: static const char *const sects[SECTSZ] = {
                     35:        "Sh",
                     36:        "Ss",
                     37:        "Em",
                     38:        "No",
                     39: };
                     40:
1.16      kristaps   41: static void doaccent(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   42: static void doblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   43: static void dobracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     44: static void dobye(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.27      kristaps   45: static void dodefindex(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.15      kristaps   46: static void dodefn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   47: static void dodisplay(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2       kristaps   48: static void doenumerate(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   49: static void doexample(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8       kristaps   50: static void doignargn(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   51: static void doignblock(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     52: static void doignbracket(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     53: static void doignline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.12      kristaps   54: static void doinline(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.2       kristaps   55: static void doinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   56: static void doitem(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     57: static void doitemize(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.8       kristaps   58: static void dolink(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.30      kristaps   59: static void domacro(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   60: static void domath(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18      kristaps   61: static void domultitable(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   62: static void doquotation(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     63: static void dotable(struct texi *, enum texicmd, const char *, size_t, size_t *);
                     64: static void dotop(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23      kristaps   65: static void dosecoffs(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   66: static void dosection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.3       kristaps   67: static void dosp(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   68: static void dosubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.23      kristaps   69: static void dosubsubsection(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   70: static void dosymbol(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.18      kristaps   71: static void dotab(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.10      kristaps   72: static void dotitle(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.24      kristaps   73: static void dovalue(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.25      kristaps   74: static void doverb(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.33      kristaps   75: static void doverbatim(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.16      kristaps   76: static void doverbinclude(struct texi *, enum texicmd, const char *, size_t, size_t *);
1.1       kristaps   77:
1.24      kristaps   78: static const struct texitok __texitoks[TEXICMD__MAX] = {
1.20      kristaps   79:        /* TEXICMD__BEGIN */
1.42      kristaps   80:        { doignline, "afourpaper", 10 }, /* TEXICMD_A4PAPER */
                     81:        { dosymbol, "AA", 2 }, /* TEXICMD_AA */
                     82:        { dosymbol, "aa", 2 }, /* TEXICMD_AASMALL */
1.8       kristaps   83:        { doignargn, "acronym", 7 }, /* TEXICMD_ACRONYM */
1.16      kristaps   84:        { doaccent, "'", 1 }, /* TEXICMD_ACUTE */
1.42      kristaps   85:        { dosymbol, "AE", 2 }, /* TEXICMD_AE */
                     86:        { dosymbol, "ae", 2 }, /* TEXICMD_AESMALL */
1.1       kristaps   87:        { doignbracket, "anchor", 6 }, /* TEXICMD_ANCHOR */
1.12      kristaps   88:        { dosection, "appendix", 8 }, /* TEXICMD_APPENDIX */
                     89:        { dosection, "appendixsec", 11 }, /* TEXICMD_APPENDIXSEC */
1.20      kristaps   90:        { dosubsection, "appendixsubsec", 14 }, /* TEXICMD_APPENDIXSUBSEC */
1.23      kristaps   91:        { dosubsubsection, "appendixsubsubsec", 17 }, /* TEXICMD_APPENDIXSUBSUBSEC */
1.25      kristaps   92:        { doinline, "asis", 4 }, /* TEXICMD_ASIS */
1.3       kristaps   93:        { dosymbol, "*", 1 }, /* TEXICMD_ASTERISK */
1.1       kristaps   94:        { dosymbol, "@", 1 }, /* TEXICMD_AT */
1.3       kristaps   95:        { doignline, "author", 6 }, /* TEXICMD_AUTHOR */
1.21      kristaps   96:        { doinline, "b", 1 }, /* TEXICMD_BOLD */
1.3       kristaps   97:        { dosymbol, "!", 1 }, /* TEXICMD_BANG */
1.7       kristaps   98:        { dosymbol, "bullet", 6 }, /* TEXICMD_BULLET */
1.1       kristaps   99:        { dobye, "bye", 3 }, /* TEXICMD_BYE */
1.34      kristaps  100:        { doblock, "cartouche", 9 }, /* TEXICMD_CARTOUCHE */
                    101:        { doaccent, ",", 1 }, /* TEXICMD_CEDILLA */
1.12      kristaps  102:        { doignline, "center", 6 }, /* TEXICMD_CENTER */
                    103:        { dosection, "chapter", 7 }, /* TEXICMD_CHAPTER */
1.1       kristaps  104:        { doignline, "cindex", 6 }, /* TEXICMD_CINDEX */
1.16      kristaps  105:        { doaccent, "^", 1 }, /* TEXICMD_CIRCUMFLEX */
1.24      kristaps  106:        { doinline, "cite", 4 }, /* TEXICMD_CITE */
                    107:        { dovalue, "clear", 5 }, /* TEXICMD_CLEAR */
1.21      kristaps  108:        { doinline, "code", 4 }, /* TEXICMD_CODE */
1.3       kristaps  109:        { dosymbol, ":", 1 }, /* TEXICMD_COLON */
1.18      kristaps  110:        { NULL, "columnfractions", 15 }, /* TEXICMD_COLUMNFRACTIONS */
1.35      kristaps  111:        { dosymbol, "comma", 5 }, /* TEXICMD_COMMA */
1.12      kristaps  112:        { doinline, "command", 7 }, /* TEXICMD_COMMAND */
1.1       kristaps  113:        { doignline, "c", 1 }, /* TEXICMD_COMMENT */
1.2       kristaps  114:        { doignline, "comment", 7 }, /* TEXICMD_COMMENT_LONG */
1.1       kristaps  115:        { doignline, "contents", 8 }, /* TEXICMD_CONTENTS */
                    116:        { doignblock, "copying", 7 }, /* TEXICMD_COPYING */
                    117:        { dosymbol, "copyright", 9 }, /* TEXICMD_COPYRIGHT */
1.27      kristaps  118:        { dodefindex, "defcodeindex", 12 }, /* TEXICMD_DEFCODEINDEX */
1.15      kristaps  119:        { dodefn, "deffn", 5 }, /* TEXICMD_DEFFN */
                    120:        { dodefn, "deffnx", 6 }, /* TEXICMD_DEFFNX */
1.27      kristaps  121:        { dodefindex, "defindex", 8 }, /* TEXICMD_DEFINDEX */
1.15      kristaps  122:        { dodefn, "defmac", 6 }, /* TEXICMD_DEFMAC */
                    123:        { dodefn, "defmacx", 7 }, /* TEXICMD_DEFMACX */
                    124:        { dodefn, "deftp", 5 }, /* TEXICMD_DEFTP */
                    125:        { dodefn, "deftpx", 6 }, /* TEXICMD_DEFTPX */
                    126:        { dodefn, "deftypefn", 9 }, /* TEXICMD_DEFTYPEFN */
                    127:        { dodefn, "deftypefnx", 10 }, /* TEXICMD_DEFTYPEFNX */
                    128:        { dodefn, "deftypefun", 10 }, /* TEXICMD_DEFTYPEFUN */
                    129:        { dodefn, "deftypefunx", 11 }, /* TEXICMD_DEFTYPEFUNX */
1.34      kristaps  130:        { dodefn, "deftypemethod", 13 }, /* TEXICMD_DEFTYPEMETHOD */
                    131:        { dodefn, "deftypemethodx", 14 }, /* TEXICMD_DEFTYPEMETHODX */
1.15      kristaps  132:        { dodefn, "deftypevar", 10 }, /* TEXICMD_DEFTYPEVAR */
                    133:        { dodefn, "deftypevarx", 11 }, /* TEXICMD_DEFTYPEVARX */
                    134:        { dodefn, "deftypevr", 9 }, /* TEXICMD_DEFTYPEVR */
                    135:        { dodefn, "deftypevrx", 10 }, /* TEXICMD_DEFTYPEVRX */
                    136:        { dodefn, "defun", 5 }, /* TEXICMD_DEFUN */
                    137:        { dodefn, "defunx", 6 }, /* TEXICMD_DEFUNX */
                    138:        { dodefn, "defvar", 6 }, /* TEXICMD_DEFVAR */
                    139:        { dodefn, "defvarx", 7 }, /* TEXICMD_DEFVARX */
                    140:        { dodefn, "defvr", 5 }, /* TEXICMD_DEFVR */
                    141:        { dodefn, "defvrx", 6 }, /* TEXICMD_DEFVRX */
1.1       kristaps  142:        { doignblock, "detailmenu", 10 }, /* TEXICMD_DETAILMENU */
1.21      kristaps  143:        { doinline, "dfn", 3 }, /* TEXICMD_DFN */
1.42      kristaps  144:        { dosymbol, "DH", 2 }, /* TEXICMD_DH */
                    145:        { dosymbol, "dh", 2 }, /* TEXICMD_DHSMALL */
1.1       kristaps  146:        { doignline, "dircategory", 11 }, /* TEXICMD_DIRCATEGORY */
                    147:        { doignblock, "direntry", 8 }, /* TEXICMD_DIRENTRY */
1.3       kristaps  148:        { dodisplay, "display", 7 }, /* TEXICMD_DISPLAY */
1.28      kristaps  149:        { doignbracket, "dmn", 3 }, /* TEXICMD_DMN */
1.40      kristaps  150:        { doignblock, "documentdescription", 19 }, /* TEXICMD_DOCUMENTDESCRIPTION */
                    151:        { doignline, "documentencoding", 16 }, /* TEXICMD_DOCUMENTENCODING */
1.42      kristaps  152:        { doaccent, "dotaccent", 9 }, /* TEXICMD_DOTACCENT */
                    153:        { doaccent, "dotless", 7 }, /* TEXICMD_DOTLESS */
1.2       kristaps  154:        { dosymbol, "dots", 4 }, /* TEXICMD_DOTS */
1.8       kristaps  155:        { dolink, "email", 5 }, /* TEXICMD_EMAIL */
1.21      kristaps  156:        { doinline, "emph", 4 }, /* TEXICMD_EMPH */
1.1       kristaps  157:        { NULL, "end", 3 }, /* TEXICMD_END */
1.34      kristaps  158:        { dosymbol, "enddots", 7 }, /* TEXICMD_ENDDOTS */
1.2       kristaps  159:        { doenumerate, "enumerate", 9 }, /* TEXICMD_ENUMERATE */
1.12      kristaps  160:        { doinline, "env", 3 }, /* TEXICMD_ENV */
1.28      kristaps  161:        { dosymbol, "equiv", 5 }, /* TEXICMD_EQUIV */
1.15      kristaps  162:        { dosymbol, "error", 5 }, /* TEXICMD_ERROR */
1.43    ! kristaps  163:        { dosymbol, "euro", 4 }, /* TEXICMD_EURO */
1.1       kristaps  164:        { doexample, "example", 7 }, /* TEXICMD_EXAMPLE */
1.42      kristaps  165:        { dosymbol, "exclamdown", 10 }, /* TEXICMD_EXCLAMDOWN */
1.25      kristaps  166:        { doignline, "exdent", 6 }, /* TEXICMD_EXDENT */
1.17      kristaps  167:        { dosymbol, "expansion", 9 }, /* TEXICMD_EXPANSION */
1.12      kristaps  168:        { doinline, "file", 4 }, /* TEXICMD_FILE */
1.17      kristaps  169:        { doignline, "finalout", 8 }, /* TEXICMD_FINALOUT */
1.20      kristaps  170:        { doignline, "findex", 6 }, /* TEXICMD_FINDEX */
1.32      kristaps  171:        { doignbracket, "footnote", 8 }, /* TEXICMD_FOOTNOTE */
1.20      kristaps  172:        { dotable, "ftable", 6 }, /* TEXICMD_FTABLE */
                    173:        { dodisplay, "format", 6 }, /* TEXICMD_FORMAT */
1.43    ! kristaps  174:        { dosymbol, "geq", 3 }, /* TEXICMD_GEQ */
1.16      kristaps  175:        { doaccent, "`", 1 }, /* TEXICMD_GRAVE */
1.3       kristaps  176:        { doblock, "group", 5 }, /* TEXICMD_GROUP */
1.43    ! kristaps  177:        { dosymbol, "guillemetleft", 13 }, /* TEXICMD_GUILLEMETLEFT */
        !           178:        { dosymbol, "guillemetright", 14 }, /* TEXICMD_GUILLEMETRIGHT */
        !           179:        { dosymbol, "guillemotleft", 13 }, /* TEXICMD_GUILLEMOTLEFT */
        !           180:        { dosymbol, "guillemotright", 14 }, /* TEXICMD_GUILLEMOTRIGHT */
        !           181:        { dosymbol, "guilsinglleft", 13 }, /* TEXICMD_GUILSINGLLEFT */
        !           182:        { dosymbol, "guilsinglright", 14 }, /* TEXICMD_GUILSINGLRIGHT */
1.42      kristaps  183:        { doaccent, "H", 1 }, /* TEXICMD_H */
1.2       kristaps  184:        { dosection, "heading", 7 }, /* TEXICMD_HEADING */
1.3       kristaps  185:        { doignline, "headings", 8 }, /* TEXICMD_HEADINGS */
1.18      kristaps  186:        { doitem, "headitem", 8 }, /* TEXICMD_HEADITEM */
1.25      kristaps  187:        { doignblock, "html", 4 }, /* TEXICMD_HTML */
1.3       kristaps  188:        { dosymbol, "-", 1 }, /* TEXICMD_HYPHEN */
1.21      kristaps  189:        { doinline, "i", 1 }, /* TEXICMD_I */
1.24      kristaps  190:        { dovalue, "ifclear", 7 }, /* TEXICMD_IFCLEAR */
1.14      kristaps  191:        { doignblock, "ifdocbook", 9 }, /* TEXICMD_IFDOCBOOK */
1.1       kristaps  192:        { doignblock, "ifhtml", 6 }, /* TEXICMD_IFHTML */
1.26      kristaps  193:        { doblock, "ifinfo", 6 }, /* TEXICMD_IFINFO */
1.14      kristaps  194:        { doblock, "ifnotdocbook", 12 }, /* TEXICMD_IFNOTDOCBOOK */
                    195:        { doblock, "ifnothtml", 9 }, /* TEXICMD_IFNOTHTML */
                    196:        { doblock, "ifnotinfo", 9 }, /* TEXICMD_IFNOTINFO */
                    197:        { doignblock, "ifnotplaintext", 14 }, /* TEXICMD_IFNOTPLAINTEXT */
1.3       kristaps  198:        { doblock, "ifnottex", 8 }, /* TEXICMD_IFNOTTEX */
1.14      kristaps  199:        { doblock, "ifnotxml", 8 }, /* TEXICMD_IFNOTXML */
                    200:        { doblock, "ifplaintext", 11 }, /* TEXICMD_IFPLAINTEXT */
1.1       kristaps  201:        { doignblock, "iftex", 5 }, /* TEXICMD_IFTEX */
1.30      kristaps  202:        { dovalue, "ifset", 5 }, /* TEXICMD_IFSET */
1.14      kristaps  203:        { doignblock, "ifxml", 5 }, /* TEXICMD_IFXML */
1.17      kristaps  204:        { doignblock, "ignore", 6 }, /* TEXICMD_IGNORE */
1.1       kristaps  205:        { doignbracket, "image", 5 }, /* TEXICMD_IMAGE */
1.2       kristaps  206:        { doinclude, "include", 7 }, /* TEXICMD_INCLUDE */
1.13      kristaps  207:        { dodisplay, "indentblock", 11 }, /* TEXICMD_INDENTBLOCK */
1.34      kristaps  208:        { dolink, "inforef", 7 }, /* TEXICMD_INDENTBLOCK */
1.5       kristaps  209:        { doignline, "insertcopying", 13 }, /* TEXICMD_INSERTCOPYING */
1.1       kristaps  210:        { doitem, "item", 4 }, /* TEXICMD_ITEM */
                    211:        { doitemize, "itemize", 7 }, /* TEXICMD_ITEMIZE */
1.20      kristaps  212:        { doitem, "itemx", 5 }, /* TEXICMD_ITEMX */
1.21      kristaps  213:        { doinline, "kbd", 3 }, /* TEXICMD_KBD */
1.18      kristaps  214:        { dobracket, "key", 3 }, /* TEXICMD_KEY */
1.20      kristaps  215:        { doignline, "kindex", 6 }, /* TEXICMD_KINDEX */
1.42      kristaps  216:        { dosymbol, "L", 1 }, /* TEXICMD_L */
1.1       kristaps  217:        { dosymbol, "LaTeX", 5 }, /* TEXICMD_LATEX */
1.43    ! kristaps  218:        { dosymbol, "leq", 3 }, /* TEXICMD_LEQ */
1.23      kristaps  219:        { dosecoffs, "lowersections", 13 }, /* TEXICMD_LOWERSECTIONS */
1.42      kristaps  220:        { dosymbol, "l", 1 }, /* TEXICMD_LSMALL */
1.30      kristaps  221:        { domacro, "macro", 5 }, /* TEXICMD_MACRO */
1.42      kristaps  222:        { doaccent, "=", 1 }, /* TEXICMD_MACRON */
1.3       kristaps  223:        { domath, "math", 4 }, /* TEXICMD_MATH */
1.1       kristaps  224:        { doignblock, "menu", 4 }, /* TEXICMD_MENU */
1.25      kristaps  225:        { dosymbol, "minus", 5 }, /* TEXICMD_MINUS */
1.18      kristaps  226:        { domultitable, "multitable", 10 }, /* TEXICMD_MULTITABLE */
1.15      kristaps  227:        { doignline, "need", 4 }, /* TEXICMD_NEED */
1.3       kristaps  228:        { dosymbol, "\n", 1 }, /* TEXICMD_NEWLINE */
1.1       kristaps  229:        { doignline, "node", 4 }, /* TEXICMD_NODE */
1.3       kristaps  230:        { doignline, "noindent", 8 }, /* TEXICMD_NOINDENT */
1.42      kristaps  231:        { dosymbol, "O", 1 }, /* TEXICMD_O */
                    232:        { dosymbol, "OE", 2 }, /* TEXICMD_OE */
                    233:        { dosymbol, "oe", 2 }, /* TEXICMD_OESMALL */
                    234:        { doaccent, "ogonek", 6 }, /* TEXICMD_OGONEK */
1.16      kristaps  235:        { doinline, "option", 6 }, /* TEXICMD_OPTION */
1.42      kristaps  236:        { dosymbol, "ordf", 4 }, /* TEXICMD_ORDF */
                    237:        { dosymbol, "ordm", 4 }, /* TEXICMD_ORDM */
                    238:        { dosymbol, "o", 1 }, /* TEXICMD_OSMALL */
1.3       kristaps  239:        { doignline, "page", 4 }, /* TEXICMD_PAGE */
1.25      kristaps  240:        { doignline, "paragraphindent", 15 }, /* TEXICMD_PARINDENT */
                    241:        { dosymbol, ".", 1 }, /* TEXICMD_PERIOD */
1.28      kristaps  242:        { doignline, "pindex", 6 }, /* TEXICMD_PINDEX */
1.43    ! kristaps  243:        { dosymbol, "pounds", 6 }, /* TEXICMD_POUNDS */
1.2       kristaps  244:        { doignline, "printindex", 10 }, /* TEXICMD_PRINTINDEX */
1.42      kristaps  245:        { dolink, "pxref", 5 }, /* TEXICMD_PXREF */
                    246:        { dosymbol, "questiondown", 12 }, /* TEXICMD_QUESTIONDOWN */
                    247:        { dosymbol, "?", 1 }, /* TEXICMD_QUESTIONMARK */
                    248:        { doquotation, "quotation", 9 }, /* TEXICMD_QUOTATION */
1.43    ! kristaps  249:        { dosymbol, "quotedblbase", 12 }, /* TEXICMD_QUOTEDBLBASE */
        !           250:        { dosymbol, "quotedblleft", 12 }, /* TEXICMD_QUOTEDBLLEFT */
        !           251:        { dosymbol, "quotedblright", 13 }, /* TEXICMD_QUOTEDBLRIGHT */
        !           252:        { dosymbol, "quotesinglbase", 14 }, /* TEXICMD_QUOTESINGLBASE */
        !           253:        { dosymbol, "quoteleft", 9 }, /* TEXICMD_QUOTELEFT */
        !           254:        { dosymbol, "quoteright", 10 }, /* TEXICMD_QUOTERIGHT */
1.21      kristaps  255:        { doinline, "r", 1 }, /* TEXICMD_R */
1.23      kristaps  256:        { dosecoffs, "raisesections", 13 }, /* TEXICMD_RAISESECTIONS */
1.1       kristaps  257:        { dobracket, "ref", 3 }, /* TEXICMD_REF */
1.28      kristaps  258:        { doignline, "refill", 6 }, /* TEXICMD_REFILL */
1.43    ! kristaps  259:        { dosymbol, "registeredsymbol", 16 }, /* TEXICMD_REGISTEREDSYMBOL */
1.15      kristaps  260:        { dosymbol, "result", 6 }, /* TEXICMD_RESULT */
1.42      kristaps  261:        { doaccent, "ringaccent", 10 }, /* TEXICMD_RINGACCENT */
1.21      kristaps  262:        { doinline, "samp", 4 }, /* TEXICMD_SAMP */
                    263:        { doinline, "sansserif", 9 }, /* TEXICMD_SANSSERIF */
1.7       kristaps  264:        { dobracket, "sc", 2 }, /* TEXICMD_SC */
1.1       kristaps  265:        { dosection, "section", 7 }, /* TEXICMD_SECTION */
1.24      kristaps  266:        { dovalue, "set", 3 }, /* TEXICMD_SET */
1.1       kristaps  267:        { doignline, "setchapternewpage", 17 }, /* TEXICMD_SETCHAPNEWPAGE */
1.35      kristaps  268:        { doignline, "setcontentsaftertitlepage", 25 }, /* TEXICMD_SETCONTENTSAFTER */
1.1       kristaps  269:        { doignline, "setfilename", 11 }, /* TEXICMD_SETFILENAME */
1.10      kristaps  270:        { dotitle, "settitle", 8 }, /* TEXICMD_SETTITLE */
1.25      kristaps  271:        { doignline, "shortcontents", 13 }, /* TEXICMD_SHORTCONTENTS */
1.21      kristaps  272:        { doinline, "slanted", 7 }, /* TEXICMD_SLANTED */
1.34      kristaps  273:        { dosymbol, "/", 1 }, /* TEXICMD_SLASH */
1.3       kristaps  274:        { dosp, "sp", 2 }, /* TEXICMD_SP */
                    275:        { dosymbol, " ", 1 }, /* TEXICMD_SPACE */
1.17      kristaps  276:        { doignline, "smallbook", 9 }, /* TEXICMD_SMALLBOOK */
1.12      kristaps  277:        { dodisplay, "smalldisplay", 12 }, /* TEXICMD_SMALLDISPLAY */
1.3       kristaps  278:        { doexample, "smallexample", 12 }, /* TEXICMD_SMALLEXAMPLE */
1.20      kristaps  279:        { dodisplay, "smallformat", 11 }, /* TEXICMD_SMALLFORMAT */
1.13      kristaps  280:        { dodisplay, "smallindentblock", 16 }, /* TEXICMD_SMALLINDENTBLOCK */
1.3       kristaps  281:        { dosymbol, "{", 1 }, /* TEXICMD_SQUIGGLE_LEFT */
                    282:        { dosymbol, "}", 1 }, /* TEXICMD_SQUIGGLE_RIGHT */
1.35      kristaps  283:        { dosymbol, "ss", 2 }, /* TEXICMD_SS */
1.21      kristaps  284:        { doinline, "strong", 6 }, /* TEXICMD_STRONG */
1.20      kristaps  285:        { dosubsection, "subheading", 10 }, /* TEXICMD_SUBHEADING */
1.1       kristaps  286:        { dosubsection, "subsection", 10 }, /* TEXICMD_SUBSECTION */
1.28      kristaps  287:        { dosubsubsection, "subsubheading", 13 }, /* TEXICMD_SUBSUBHEADING */
1.23      kristaps  288:        { dosubsubsection, "subsubsection", 13 }, /* TEXICMD_SUBSUBSECTION */
1.3       kristaps  289:        { doignline, "subtitle", 8 }, /* TEXICMD_SUBTITLE */
1.25      kristaps  290:        { doignline, "summarycontents", 15 }, /* TEXICMD_SUMMARYCONTENTS */
1.27      kristaps  291:        { doignline, "synindex", 8 }, /* TEXICMD_SYNINDEX */
1.20      kristaps  292:        { doignline, "syncodeindex", 12 }, /* TEXICMD_SYNCODEINDEX */
1.21      kristaps  293:        { doinline, "t", 1 }, /* TEXICMD_T */
1.18      kristaps  294:        { dotab, "tab", 3 }, /* TEXICMD_TAB */
                    295:        { dosymbol, "\t", 1 }, /* TEXICMD_TABSYM */
1.1       kristaps  296:        { dotable, "table", 5 }, /* TEXICMD_TABLE */
                    297:        { doignblock, "tex", 3 }, /* TEXICMD_TEX */
                    298:        { dosymbol, "TeX", 3 }, /* TEXICMD_TEXSYM */
1.43    ! kristaps  299:        { dosymbol, "textdegree", 10 }, /* TEXICMD_TEXTDEGREE */
1.42      kristaps  300:        { dosymbol, "TH", 2 }, /* TEXICMD_TH */
                    301:        { dosymbol, "th", 2 }, /* TEXICMD_THSMALL */
1.35      kristaps  302:        { dosymbol, "tie", 3 }, /* TEXICMD_TIE */
1.42      kristaps  303:        { doaccent, "tieaccent", 9 }, /* TEXICMD_TIEACCENT */
1.16      kristaps  304:        { doaccent, "~", 1 }, /* TEXICMD_TILDE */
1.25      kristaps  305:        { doignline, "tindex", 6 }, /* TEXICMD_TINDEX */
1.3       kristaps  306:        { doignline, "title", 5 }, /* TEXICMD_TITLE */
1.1       kristaps  307:        { dobracket, "titlefont", 9 }, /* TEXICMD_TITLEFONT */
                    308:        { doignblock, "titlepage", 9 }, /* TEXICMD_TITLEPAGE */
                    309:        { dotop, "top", 3 }, /* TEXICMD_TOP */
1.42      kristaps  310:        { doaccent, "u", 1 }, /* TEXICMD_U */
                    311:        { doaccent, "ubaraccent", 10 }, /* TEXICMD_UBARACCENT */
                    312:        { doaccent, "udotaccent", 10 }, /* TEXICMD_UDOTACCENT */
1.16      kristaps  313:        { doaccent, "\"", 1 }, /* TEXICMD_UMLAUT */
1.12      kristaps  314:        { dosection, "unnumbered", 10 }, /* TEXICMD_UNNUMBERED */
1.2       kristaps  315:        { dosection, "unnumberedsec", 13 }, /* TEXICMD_UNNUMBEREDSEC */
1.20      kristaps  316:        { dosubsection, "unnumberedsubsec", 16 }, /* TEXICMD_UNNUMBEREDSUBSEC */
1.23      kristaps  317:        { dosubsubsection, "unnumberedsubsubsec", 19 }, /* TEXICMD_UNNUMBEREDSUBSUBSEC */
1.8       kristaps  318:        { dolink, "uref", 4 }, /* TEXICMD_UREF */
                    319:        { dolink, "url", 3 }, /* TEXICMD_URL */
1.34      kristaps  320:        { doignline, "", 0 }, /* TEXICMD_USER_INDEX */
1.42      kristaps  321:        { doaccent, "v", 1 }, /* TEXICMD_V */
1.24      kristaps  322:        { dovalue, "value", 5 }, /* TEXICMD_VALUE */
1.12      kristaps  323:        { doinline, "var", 3 }, /* TEXICMD_VAR */
1.33      kristaps  324:        { doverb, "verb", 4 }, /* TEXICMD_VERB */
                    325:        { doverbatim, "verbatim", 8 }, /* TEXICMD_VERBATIM */
1.16      kristaps  326:        { doverbinclude, "verbatiminclude", 15 }, /* TEXICMD_VERBATIMINCLUDE */
1.18      kristaps  327:        { doignline, "vindex", 6 }, /* TEXICMD_VINDEX */
1.9       kristaps  328:        { dosp, "vskip", 5 }, /* TEXICMD_VSKIP */
1.20      kristaps  329:        { dotable, "vtable", 6 }, /* TEXICMD_VTABLE */
1.3       kristaps  330:        { dobracket, "w", 1 }, /* TEXICMD_W */
1.8       kristaps  331:        { dolink, "xref", 4 }, /* TEXICMD_XREF */
1.20      kristaps  332:        /* TEXICMD__END */
1.1       kristaps  333: };
                    334:
1.24      kristaps  335: const  struct texitok *const texitoks = __texitoks;
1.18      kristaps  336:
1.2       kristaps  337: static void
1.27      kristaps  338: dodefindex(struct texi *p, enum texicmd cmd,
                    339:        const char *buf, size_t sz, size_t *pos)
                    340: {
                    341:        size_t   start, end;
                    342:        char    *cp;
                    343:
                    344:        while (*pos < sz && isws(buf[*pos]))
                    345:                advance(p, buf, pos);
                    346:
                    347:        start = end = *pos;
                    348:        while (end < sz && ! ismspace(buf[end]))
                    349:                end++;
                    350:
                    351:        if (start == end) {
                    352:                advanceeoln(p, buf, sz, pos, 1);
                    353:                return;
                    354:        } else if (NULL == (cp = malloc(end - start + 1)))
                    355:                texiabort(p, NULL);
                    356:
                    357:        memcpy(cp, &buf[start], end - start);
                    358:        cp[end - start] = '\0';
                    359:
                    360:        p->indexs = realloc(p->indexs,
                    361:                sizeof(char *) * (p->indexsz + 1));
                    362:
                    363:        if (NULL == p->indexs)
                    364:                texiabort(p, NULL);
                    365:        p->indexs[p->indexsz++] = cp;
                    366: }
                    367:
                    368: static void
1.15      kristaps  369: dodefn(struct texi *p, enum texicmd cmd,
1.3       kristaps  370:        const char *buf, size_t sz, size_t *pos)
                    371: {
                    372:        const char      *blk;
                    373:
1.5       kristaps  374:        blk = NULL;
1.3       kristaps  375:        switch (cmd) {
1.15      kristaps  376:        case (TEXICMD_DEFFN):
1.35      kristaps  377:        case (TEXICMD_DEFMAC):
1.15      kristaps  378:        case (TEXICMD_DEFTP):
1.3       kristaps  379:        case (TEXICMD_DEFTYPEFN):
                    380:        case (TEXICMD_DEFTYPEFUN):
1.34      kristaps  381:        case (TEXICMD_DEFTYPEMETHOD):
1.15      kristaps  382:        case (TEXICMD_DEFTYPEVAR):
                    383:        case (TEXICMD_DEFTYPEVR):
                    384:        case (TEXICMD_DEFUN):
                    385:        case (TEXICMD_DEFVAR):
                    386:        case (TEXICMD_DEFVR):
1.5       kristaps  387:                blk = texitoks[cmd].tok;
1.3       kristaps  388:                break;
1.5       kristaps  389:        default:
1.3       kristaps  390:                break;
                    391:        }
                    392:
                    393:        if (p->ign) {
1.15      kristaps  394:                NULL != blk ?
                    395:                        parseto(p, buf, sz, pos, blk) :
                    396:                        parseeoln(p, buf, sz, pos);
1.3       kristaps  397:                return;
                    398:        }
                    399:
1.15      kristaps  400:        if (NULL != blk)
                    401:                texivspace(p);
                    402:
1.3       kristaps  403:        switch (cmd) {
1.34      kristaps  404:        case (TEXICMD_DEFTYPEMETHOD):
                    405:        case (TEXICMD_DEFTYPEMETHODX):
                    406:                texiputchars(p, "Method");
                    407:                break;
1.15      kristaps  408:        case (TEXICMD_DEFMAC):
                    409:        case (TEXICMD_DEFMACX):
                    410:                texiputchars(p, "Macro");
                    411:                break;
                    412:        case (TEXICMD_DEFTYPEVAR):
                    413:        case (TEXICMD_DEFTYPEVARX):
                    414:        case (TEXICMD_DEFVAR):
                    415:        case (TEXICMD_DEFVARX):
                    416:                texiputchars(p, "Variable");
                    417:                break;
1.3       kristaps  418:        case (TEXICMD_DEFTYPEFUN):
1.15      kristaps  419:        case (TEXICMD_DEFTYPEFUNX):
                    420:        case (TEXICMD_DEFUN):
                    421:        case (TEXICMD_DEFUNX):
                    422:                texiputchars(p, "Function");
1.3       kristaps  423:                break;
                    424:        default:
1.15      kristaps  425:                parselinearg(p, buf, sz, pos);
1.3       kristaps  426:                break;
                    427:        }
1.15      kristaps  428:
                    429:        texiputchars(p, ":\n");
                    430:
                    431:        switch (cmd) {
                    432:        case (TEXICMD_DEFMAC):
                    433:        case (TEXICMD_DEFMACX):
                    434:                teximacroopen(p, "Dv");
                    435:                while (parselinearg(p, buf, sz, pos))
                    436:                        /* Spin. */ ;
                    437:                teximacroclose(p);
                    438:                break;
                    439:        case (TEXICMD_DEFFN):
                    440:        case (TEXICMD_DEFFNX):
                    441:        case (TEXICMD_DEFUN):
                    442:        case (TEXICMD_DEFUNX):
                    443:                teximacroopen(p, "Fo");
                    444:                parselinearg(p, buf, sz, pos);
                    445:                teximacroclose(p);
                    446:                teximacroopen(p, "Fa");
                    447:                while (parselinearg(p, buf, sz, pos))
                    448:                        /* Spin. */ ;
                    449:                teximacroclose(p);
                    450:                teximacro(p, "Fc");
                    451:                break;
                    452:        case (TEXICMD_DEFTYPEFUN):
                    453:        case (TEXICMD_DEFTYPEFUNX):
                    454:        case (TEXICMD_DEFTYPEFN):
                    455:        case (TEXICMD_DEFTYPEFNX):
1.34      kristaps  456:        case (TEXICMD_DEFTYPEMETHOD):
                    457:        case (TEXICMD_DEFTYPEMETHODX):
1.15      kristaps  458:                teximacroopen(p, "Ft");
                    459:                parselinearg(p, buf, sz, pos);
                    460:                teximacroclose(p);
                    461:                teximacroopen(p, "Fo");
                    462:                parselinearg(p, buf, sz, pos);
                    463:                teximacroclose(p);
                    464:                teximacroopen(p, "Fa");
                    465:                while (parselinearg(p, buf, sz, pos))
                    466:                        /* Spin. */ ;
                    467:                teximacroclose(p);
                    468:                teximacro(p, "Fc");
                    469:                break;
                    470:        case (TEXICMD_DEFTP):
                    471:        case (TEXICMD_DEFTPX):
                    472:        case (TEXICMD_DEFTYPEVAR):
                    473:        case (TEXICMD_DEFTYPEVARX):
                    474:        case (TEXICMD_DEFTYPEVR):
                    475:        case (TEXICMD_DEFTYPEVRX):
                    476:                teximacroopen(p, "Vt");
                    477:                while (parselinearg(p, buf, sz, pos))
                    478:                        /* Spin. */ ;
                    479:                teximacroclose(p);
                    480:                break;
                    481:        case (TEXICMD_DEFVAR):
                    482:        case (TEXICMD_DEFVARX):
                    483:        case (TEXICMD_DEFVR):
                    484:        case (TEXICMD_DEFVRX):
                    485:                teximacroopen(p, "Va");
                    486:                while (parselinearg(p, buf, sz, pos))
                    487:                        /* Spin. */ ;
                    488:                teximacroclose(p);
                    489:                break;
                    490:        default:
                    491:                abort();
1.3       kristaps  492:        }
1.15      kristaps  493:
1.11      kristaps  494:        texivspace(p);
1.3       kristaps  495:        if (NULL != blk)
                    496:                parseto(p, buf, sz, pos, blk);
                    497: }
                    498:
                    499: static void
1.30      kristaps  500: domacro(struct texi *p, enum texicmd cmd,
                    501:        const char *buf, size_t sz, size_t *pos)
                    502: {
                    503:        size_t            start, end, endtoksz, len;
                    504:        struct teximacro  m;
                    505:        const char       *endtok, *blk;
                    506:
                    507:        memset(&m, 0, sizeof(struct teximacro));
                    508:
                    509:        while (*pos < sz && isws(buf[*pos]))
                    510:                advance(p, buf, pos);
                    511:
                    512:        for (start = end = *pos; end < sz; end++)
                    513:                if (ismspace(buf[end]) || '{' == buf[end])
                    514:                        break;
                    515:
                    516:        if (start == end)
                    517:                texierr(p, "zero-length macro name");
                    518:
                    519:        advanceto(p, buf, pos, end);
                    520:
                    521:        m.key = malloc(end - start + 1);
                    522:        if (NULL == m.key)
                    523:                texiabort(p, NULL);
                    524:        memcpy(m.key, &buf[start], end - start);
                    525:        m.key[end - start] = '\0';
                    526:
1.31      kristaps  527:        m.args = argparse(p, buf, sz, pos, &m.argsz, 0);
1.30      kristaps  528:        advanceeoln(p, buf, sz, pos, 0);
                    529:
                    530:        start = *pos;
                    531:        endtok = "\n@end macro\n";
                    532:        endtoksz = strlen(endtok);
                    533:        blk = memmem(&buf[start], sz, endtok, endtoksz);
                    534:        if (NULL == blk)
                    535:                texierr(p, "unterminated macro body");
                    536:        while (&buf[*pos] != blk)
                    537:                advance(p, buf, pos);
                    538:        assert('\n' == buf[*pos]);
                    539:        advance(p, buf, pos);
                    540:        len = blk - &buf[start];
                    541:        m.value = malloc(len + 1);
                    542:        if (NULL == m.value)
                    543:                texiabort(p, NULL);
                    544:        memcpy(m.value, &buf[start], len);
                    545:        m.value[len] = '\0';
                    546:
                    547:        p->macros = realloc
                    548:                (p->macros,
                    549:                (p->macrosz + 1) *
                    550:                sizeof(struct teximacro));
                    551:        if (NULL == p->macros)
                    552:                texiabort(p, NULL);
                    553:
                    554:        p->macros[p->macrosz++] = m;
                    555:        advanceeoln(p, buf, sz, pos, 1);
                    556: }
                    557:
                    558: static void
1.1       kristaps  559: doignblock(struct texi *p, enum texicmd cmd,
                    560:        const char *buf, size_t sz, size_t *pos)
                    561: {
1.26      kristaps  562:        char             end[32];
                    563:        const char      *term;
                    564:        size_t           endsz, endpos;
                    565:
                    566:        /*
                    567:         * We want to completely ignore everything in these blocks, so
                    568:         * simply jump to the @end block.
                    569:         */
                    570:        endsz = snprintf(end, sizeof(end),
                    571:                "\n@end %s\n", texitoks[cmd].tok);
                    572:        assert(endsz < sizeof(end));
                    573:
                    574:        /*
                    575:         * Look up where our end token occurs.
                    576:         * Set our end position based on the relative offset of that
                    577:         * from our current position, or the EOF if we don't have a
                    578:         * proper ending point.
                    579:         */
                    580:        term = memmem(&buf[*pos], sz, end, endsz);
                    581:        endpos = NULL == term ? sz :
1.36      kristaps  582:                *pos + (size_t)(term - &buf[*pos]);
1.26      kristaps  583:        assert(endpos <= sz);
                    584:        while (*pos < endpos)
                    585:                advance(p, buf, pos);
                    586:
                    587:        /* Only do this if we're not already at the end. */
                    588:        if (endpos < sz)
                    589:                advanceto(p, buf, pos, endpos + endsz);
1.1       kristaps  590: }
                    591:
                    592: static void
1.3       kristaps  593: doblock(struct texi *p, enum texicmd cmd,
1.1       kristaps  594:        const char *buf, size_t sz, size_t *pos)
                    595: {
                    596:
1.5       kristaps  597:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.1       kristaps  598: }
                    599:
                    600: static void
1.12      kristaps  601: doinline(struct texi *p, enum texicmd cmd,
                    602:        const char *buf, size_t sz, size_t *pos)
1.1       kristaps  603: {
1.21      kristaps  604:        const char      *macro = NULL;
1.12      kristaps  605:
                    606:        switch (cmd) {
1.21      kristaps  607:        case (TEXICMD_CODE):
                    608:        case (TEXICMD_KBD):
                    609:        case (TEXICMD_SAMP):
                    610:        case (TEXICMD_T):
                    611:                macro = "Li";
                    612:                break;
                    613:        case (TEXICMD_CITE):
                    614:        case (TEXICMD_DFN):
                    615:        case (TEXICMD_EMPH):
                    616:        case (TEXICMD_I):
                    617:        case (TEXICMD_SLANTED):
                    618:                macro = "Em";
                    619:                break;
                    620:        case (TEXICMD_B):
                    621:        case (TEXICMD_STRONG):
                    622:                macro = "Sy";
                    623:                break;
1.12      kristaps  624:        case (TEXICMD_COMMAND):
                    625:                macro = "Xr";
                    626:                break;
                    627:        case (TEXICMD_ENV):
                    628:                macro = "Ev";
                    629:                break;
                    630:        case (TEXICMD_FILE):
                    631:                macro = "Pa";
                    632:                break;
1.16      kristaps  633:        case (TEXICMD_OPTION):
                    634:                macro = "Op";
                    635:                break;
1.12      kristaps  636:        case (TEXICMD_VAR):
                    637:                macro = "Va";
                    638:                break;
                    639:        default:
1.22      kristaps  640:                break;
1.12      kristaps  641:        }
                    642:
1.25      kristaps  643:        if (NULL == macro || p->literal || TEXILIST_TABLE == p->list) {
1.12      kristaps  644:                parsebracket(p, buf, sz, pos);
                    645:                return;
                    646:        }
1.1       kristaps  647:
1.5       kristaps  648:        teximacroopen(p, macro);
1.1       kristaps  649:        p->seenws = 0;
                    650:        parsebracket(p, buf, sz, pos);
1.11      kristaps  651:        texipunctuate(p, buf, sz, pos);
1.5       kristaps  652:        teximacroclose(p);
1.1       kristaps  653: }
                    654:
                    655: static void
1.25      kristaps  656: doverb(struct texi *p, enum texicmd cmd,
1.33      kristaps  657:        const char *buf, size_t sz, size_t *pos)
                    658: {
                    659:        char     delim;
1.34      kristaps  660:        size_t   start;
1.33      kristaps  661:
                    662:        while (*pos < sz && isws(buf[*pos]))
                    663:                advance(p, buf, pos);
                    664:        if (*pos == sz || '{' != buf[*pos])
                    665:                return;
                    666:        advance(p, buf, pos);
                    667:        if (*pos == sz)
                    668:                return;
                    669:
                    670:        delim = buf[*pos];
                    671:        advance(p, buf, pos);
                    672:        /* Make sure we flush out our initial whitespace... */
                    673:        if (p->seenws && p->outcol && 0 == p->literal)
                    674:                texiputchar(p, ' ');
                    675:        p->seenws = 0;
1.34      kristaps  676:        start = *pos;
1.33      kristaps  677:        /* Read until we see the delimiter then end-brace. */
                    678:        while (*pos < sz - 1) {
                    679:                if (buf[*pos] == delim && buf[*pos + 1] == '}')
                    680:                        break;
                    681:                advance(p, buf, pos);
                    682:        }
                    683:        if (*pos == sz - 1)
                    684:                return;
1.34      kristaps  685:        texiputbuf(p, buf, start, *pos);
                    686:
1.33      kristaps  687:        /* Make sure we read after the end-brace. */
                    688:        assert(delim == buf[*pos]);
                    689:        advance(p, buf, pos);
                    690:        assert('}' == buf[*pos]);
                    691:        advance(p, buf, pos);
                    692: }
                    693:
                    694: static void
                    695: doverbatim(struct texi *p, enum texicmd cmd,
1.25      kristaps  696:        const char *buf, size_t sz, size_t *pos)
                    697: {
                    698:        const char      *end, *term;
                    699:        size_t           endsz, endpos;
                    700:
1.26      kristaps  701:        advanceeoln(p, buf, sz, pos, 1);
                    702:
1.25      kristaps  703:        /* We end at exactly this token. */
                    704:        end = "\n@end verbatim\n";
                    705:        endsz = strlen(end);
                    706:
                    707:        /*
                    708:         * Look up where our end token occurs.
                    709:         * Set our end position based on the relative offset of that
                    710:         * from our current position.
                    711:         */
                    712:        term = memmem(&buf[*pos], sz, end, endsz);
                    713:        endpos = NULL == term ? sz :
1.36      kristaps  714:                *pos + (size_t)(term - &buf[*pos]);
1.25      kristaps  715:
                    716:        teximacro(p, "Bd -literal -offset indent");
                    717:        assert(endpos <= sz);
1.26      kristaps  718:        while (*pos < endpos) {
1.41      kristaps  719:                texiputchar(p, buf[*pos]);
1.25      kristaps  720:                advance(p, buf, pos);
                    721:        }
                    722:        teximacro(p, "Ed");
1.26      kristaps  723:        advanceto(p, buf, pos, endpos + endsz);
1.25      kristaps  724: }
                    725:
                    726: static void
1.16      kristaps  727: doverbinclude(struct texi *p, enum texicmd cmd,
                    728:        const char *buf, size_t sz, size_t *pos)
                    729: {
1.25      kristaps  730:        char             fname[PATH_MAX], path[PATH_MAX];
                    731:        int              rc;
                    732:        size_t           i, end;
                    733:        const char      *v;
                    734:        enum texicmd     type;
1.16      kristaps  735:
                    736:        while (*pos < sz && ' ' == buf[*pos])
                    737:                advance(p, buf, pos);
                    738:
1.25      kristaps  739:        for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.16      kristaps  740:                if (i == sizeof(fname) - 1)
                    741:                        break;
1.25      kristaps  742:                if ('@' != buf[*pos]) {
                    743:                        fname[i++] = buf[*pos];
                    744:                        advance(p, buf, pos);
                    745:                        continue;
                    746:                }
1.30      kristaps  747:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25      kristaps  748:                advanceto(p, buf, pos, end);
                    749:                if (TEXICMD_VALUE != type)
                    750:                        texierr(p, "unknown verbatiminclude command");
                    751:                v = valueblookup(p, buf, sz, pos);
                    752:                if (NULL == v)
                    753:                        continue;
                    754:                while ('\0' != *v) {
                    755:                        if (i == sizeof(fname) - 1)
                    756:                                break;
                    757:                        fname[i++] = *v++;
                    758:                }
                    759:                if ('\0' != *v)
                    760:                        break;
1.16      kristaps  761:        }
                    762:
                    763:        if (i == 0)
                    764:                texierr(p, "path too short");
                    765:        else if ('\n' != buf[*pos])
                    766:                texierr(p, "path too long");
                    767:        else if ('/' == fname[0])
                    768:                texierr(p, "no absolute paths");
                    769:        fname[i] = '\0';
                    770:
                    771:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    772:                texierr(p, "insecure path");
                    773:
                    774:        rc = snprintf(path, sizeof(path),
                    775:                "%s/%s", p->dirs[0], fname);
                    776:        if (rc < 0)
                    777:                texierr(p, "couldn't format path");
                    778:        else if ((size_t)rc >= sizeof(path))
                    779:                texierr(p, "path too long");
                    780:
                    781:        parsefile(p, path, 0);
                    782: }
                    783:
                    784: static void
1.2       kristaps  785: doinclude(struct texi *p, enum texicmd cmd,
                    786:        const char *buf, size_t sz, size_t *pos)
                    787: {
1.25      kristaps  788:        char             fname[PATH_MAX], path[PATH_MAX];
                    789:        size_t           i, end;
                    790:        int              rc;
                    791:        const char      *v;
                    792:        enum texicmd     type;
1.2       kristaps  793:
                    794:        while (*pos < sz && ' ' == buf[*pos])
                    795:                advance(p, buf, pos);
                    796:
                    797:        /* Read in the filename. */
1.25      kristaps  798:        for (i = 0; *pos < sz && '\n' != buf[*pos]; ) {
1.2       kristaps  799:                if (i == sizeof(fname) - 1)
                    800:                        break;
1.25      kristaps  801:                if ('@' != buf[*pos]) {
                    802:                        fname[i++] = buf[*pos];
                    803:                        advance(p, buf, pos);
                    804:                        continue;
                    805:                }
1.30      kristaps  806:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.25      kristaps  807:                advanceto(p, buf, pos, end);
                    808:                if (TEXICMD_VALUE != type)
                    809:                        texierr(p, "unknown include command");
                    810:                v = valueblookup(p, buf, sz, pos);
                    811:                if (NULL == v)
                    812:                        continue;
                    813:                while ('\0' != *v) {
                    814:                        if (i == sizeof(fname) - 1)
                    815:                                break;
                    816:                        fname[i++] = *v++;
                    817:                }
                    818:                if ('\0' != *v)
                    819:                        break;
1.2       kristaps  820:        }
                    821:
                    822:        if (i == 0)
                    823:                texierr(p, "path too short");
                    824:        else if ('\n' != buf[*pos])
                    825:                texierr(p, "path too long");
                    826:        else if ('/' == fname[0])
                    827:                texierr(p, "no absolute paths");
                    828:        fname[i] = '\0';
                    829:
                    830:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    831:                texierr(p, "insecure path");
                    832:
1.5       kristaps  833:        for (i = 0; i < p->dirsz; i++) {
                    834:                rc = snprintf(path, sizeof(path),
                    835:                        "%s/%s", p->dirs[i], fname);
                    836:                if (rc < 0)
                    837:                        texierr(p, "couldn't format path");
                    838:                else if ((size_t)rc >= sizeof(path))
                    839:                        texierr(p, "path too long");
                    840:                else if (-1 == access(path, R_OK))
                    841:                        continue;
                    842:
1.16      kristaps  843:                parsefile(p, path, 1);
1.5       kristaps  844:                return;
                    845:        }
1.2       kristaps  846:
1.5       kristaps  847:        texierr(p, "couldn't find %s in includes", fname);
1.2       kristaps  848: }
                    849:
                    850: static void
1.1       kristaps  851: dobracket(struct texi *p, enum texicmd cmd,
                    852:        const char *buf, size_t sz, size_t *pos)
                    853: {
                    854:
                    855:        parsebracket(p, buf, sz, pos);
                    856: }
                    857:
                    858: static void
1.3       kristaps  859: dodisplay(struct texi *p, enum texicmd cmd,
                    860:        const char *buf, size_t sz, size_t *pos)
                    861: {
                    862:
1.20      kristaps  863:        switch (cmd) {
                    864:        case (TEXICMD_FORMAT):
                    865:        case (TEXICMD_SMALLFORMAT):
                    866:                teximacro(p, "Bd -filled");
                    867:                break;
                    868:        default:
                    869:                teximacro(p, "Bd -filled -offset indent");
                    870:                break;
                    871:        }
                    872:
1.11      kristaps  873:        p->seenvs = 1;
1.12      kristaps  874:        /* FIXME: ignore and parseeoln. */
1.3       kristaps  875:        advanceeoln(p, buf, sz, pos, 1);
1.13      kristaps  876:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5       kristaps  877:        teximacro(p, "Ed");
1.3       kristaps  878: }
                    879:
                    880: static void
1.1       kristaps  881: doexample(struct texi *p, enum texicmd cmd,
                    882:        const char *buf, size_t sz, size_t *pos)
                    883: {
                    884:
1.5       kristaps  885:        teximacro(p, "Bd -literal -offset indent");
1.12      kristaps  886:        /* FIXME: ignore and parseeoln. */
1.3       kristaps  887:        advanceeoln(p, buf, sz, pos, 1);
                    888:        p->literal++;
1.13      kristaps  889:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.3       kristaps  890:        p->literal--;
1.5       kristaps  891:        teximacro(p, "Ed");
1.1       kristaps  892: }
                    893:
                    894: static void
                    895: dobye(struct texi *p, enum texicmd cmd,
                    896:        const char *buf, size_t sz, size_t *pos)
                    897: {
                    898:
                    899:        texiexit(p);
                    900:        exit(EXIT_SUCCESS);
                    901: }
                    902:
                    903: static void
1.10      kristaps  904: dotitle(struct texi *p, enum texicmd cmd,
                    905:        const char *buf, size_t sz, size_t *pos)
                    906: {
                    907:        size_t   start, end;
                    908:
                    909:        while (*pos < sz && isws(buf[*pos]))
                    910:                advance(p, buf, pos);
                    911:        start = end = *pos;
                    912:        while (end < sz && '\n' != buf[end])
                    913:                end++;
1.26      kristaps  914:        advanceeoln(p, buf, sz, pos, 1);
1.10      kristaps  915:        free(p->subtitle);
                    916:        p->subtitle = malloc(end - start + 1);
1.27      kristaps  917:        if (NULL == p->subtitle)
                    918:                texiabort(p, NULL);
1.10      kristaps  919:        memcpy(p->subtitle, &buf[start], end - start);
                    920:        p->subtitle[end - start] = '\0';
                    921: }
                    922:
                    923: static void
1.16      kristaps  924: doaccent(struct texi *p, enum texicmd cmd,
                    925:        const char *buf, size_t sz, size_t *pos)
                    926: {
1.42      kristaps  927:        int      brace = 0;
1.16      kristaps  928:
1.42      kristaps  929:        if (*pos == sz) {
                    930:                texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
1.16      kristaps  931:                return;
1.42      kristaps  932:        }
                    933:
                    934:        /* Pad us with space, if necessary. */
                    935:        if (p->seenws && p->outcol && 0 == p->literal) {
1.34      kristaps  936:                texiputchar(p, ' ');
1.42      kristaps  937:                p->seenws = 0;
                    938:        }
                    939:
                    940:        /*
                    941:         * If we're braced, then that's that.
                    942:         * Otherwise, in a special Texinfo case: if we're a non
                    943:         * alphabetic command of one letter, then the next character is
                    944:         * the critical one.
                    945:         * Otherwise, space can sit between us and our argument.
                    946:         */
                    947:        if ('{' == buf[*pos]) {
                    948:                brace = 1;
                    949:                advance(p, buf, pos);
                    950:        } else if (isalpha(texitoks[cmd].tok[0]))
1.34      kristaps  951:                while (*pos < sz && isws(buf[*pos]))
                    952:                        advance(p, buf, pos);
1.42      kristaps  953:
                    954:        if (*pos == sz) {
                    955:                texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
                    956:                return;
                    957:        }
                    958:
                    959:        switch (cmd) {
1.16      kristaps  960:        case (TEXICMD_ACUTE):
                    961:                switch (buf[*pos]) {
                    962:                case ('a'): case ('A'):
                    963:                case ('e'): case ('E'):
                    964:                case ('i'): case ('I'):
                    965:                case ('o'): case ('O'):
                    966:                case ('u'): case ('U'):
                    967:                        texiputchars(p, "\\(\'");
                    968:                        break;
                    969:                default:
1.42      kristaps  970:                        texiwarn(p, "ignoring accent");
                    971:                        break;
1.16      kristaps  972:                }
1.42      kristaps  973:                texiputchar(p, buf[*pos]);
                    974:                advance(p, buf, pos);
                    975:                break;
                    976:        case (TEXICMD_CEDILLA):
                    977:                if ('c' == buf[*pos] || 'C' == buf[*pos])
                    978:                        texiputchars(p, "\\(,");
                    979:                else
                    980:                        texiwarn(p, "ignoring accent");
                    981:                texiputchar(p, buf[*pos]);
                    982:                advance(p, buf, pos);
1.16      kristaps  983:                break;
                    984:        case (TEXICMD_CIRCUMFLEX):
                    985:                switch (buf[*pos]) {
                    986:                case ('a'): case ('A'):
                    987:                case ('e'): case ('E'):
                    988:                case ('i'): case ('I'):
                    989:                case ('o'): case ('O'):
                    990:                case ('u'): case ('U'):
                    991:                        texiputchars(p, "\\(^");
                    992:                        break;
                    993:                default:
1.42      kristaps  994:                        texiwarn(p, "ignoring accent");
1.34      kristaps  995:                        break;
1.16      kristaps  996:                }
1.42      kristaps  997:                texiputchar(p, buf[*pos]);
                    998:                advance(p, buf, pos);
                    999:                break;
                   1000:        case (TEXICMD_DOTLESS):
                   1001:                if ('i' == buf[*pos] || 'j' == buf[*pos])
                   1002:                        texiputchars(p, "\\(.");
                   1003:                else
                   1004:                        texiwarn(p, "ignoring accent");
                   1005:                texiputchar(p, buf[*pos]);
                   1006:                advance(p, buf, pos);
1.16      kristaps 1007:                break;
                   1008:        case (TEXICMD_GRAVE):
                   1009:                switch (buf[*pos]) {
                   1010:                case ('a'): case ('A'):
                   1011:                case ('e'): case ('E'):
                   1012:                case ('i'): case ('I'):
                   1013:                case ('o'): case ('O'):
                   1014:                case ('u'): case ('U'):
                   1015:                        texiputchars(p, "\\(`");
                   1016:                        break;
                   1017:                default:
1.42      kristaps 1018:                        texiwarn(p, "ignoring accent");
                   1019:                        break;
1.16      kristaps 1020:                }
1.42      kristaps 1021:                texiputchar(p, buf[*pos]);
                   1022:                advance(p, buf, pos);
1.16      kristaps 1023:                break;
                   1024:        case (TEXICMD_TILDE):
                   1025:                switch (buf[*pos]) {
                   1026:                case ('a'): case ('A'):
                   1027:                case ('n'): case ('N'):
                   1028:                case ('o'): case ('O'):
                   1029:                        texiputchars(p, "\\(~");
                   1030:                        break;
                   1031:                default:
1.42      kristaps 1032:                        texiwarn(p, "ignoring accent");
1.34      kristaps 1033:                        break;
1.16      kristaps 1034:                }
1.42      kristaps 1035:                texiputchar(p, buf[*pos]);
                   1036:                advance(p, buf, pos);
1.16      kristaps 1037:                break;
                   1038:        case (TEXICMD_UMLAUT):
                   1039:                switch (buf[*pos]) {
                   1040:                case ('a'): case ('A'):
                   1041:                case ('e'): case ('E'):
                   1042:                case ('i'): case ('I'):
                   1043:                case ('o'): case ('O'):
                   1044:                case ('u'): case ('U'):
                   1045:                case ('y'):
                   1046:                        texiputchars(p, "\\(:");
                   1047:                        break;
                   1048:                default:
1.42      kristaps 1049:                        texiwarn(p, "ignoring accent");
1.34      kristaps 1050:                        break;
1.16      kristaps 1051:                }
1.42      kristaps 1052:                texiputchar(p, buf[*pos]);
                   1053:                advance(p, buf, pos);
1.16      kristaps 1054:                break;
                   1055:        default:
1.42      kristaps 1056:                texiputchar(p, buf[*pos]);
                   1057:                advance(p, buf, pos);
                   1058:                break;
                   1059:        }
                   1060:
                   1061:        if (brace) {
                   1062:                while (*pos < sz && '}' != buf[*pos]) {
                   1063:                        texiputchar(p, buf[*pos]);
                   1064:                        advance(p, buf, pos);
                   1065:                }
                   1066:                if (*pos < sz)
                   1067:                        advance(p, buf, pos);
                   1068:        }
                   1069:
                   1070:        switch (cmd) {
                   1071:        case (TEXICMD_TIEACCENT):
                   1072:                texiputchar(p, ']');
                   1073:                break;
                   1074:        case (TEXICMD_DOTACCENT):
                   1075:                texiputchar(p, '*');
                   1076:                break;
                   1077:        default:
                   1078:                break;
1.16      kristaps 1079:        }
                   1080: }
                   1081:
                   1082: static void
1.1       kristaps 1083: dosymbol(struct texi *p, enum texicmd cmd,
                   1084:        const char *buf, size_t sz, size_t *pos)
                   1085: {
                   1086:
1.42      kristaps 1087:        /* Remember to pad us. */
1.43    ! kristaps 1088:        if (p->seenws && p->outcol && 0 == p->literal)
1.3       kristaps 1089:                texiputchar(p, ' ');
1.43    ! kristaps 1090:
        !          1091:        p->seenws = 0;
1.3       kristaps 1092:
1.1       kristaps 1093:        switch (cmd) {
1.42      kristaps 1094:        case (TEXICMD_AA):
                   1095:                texiputchars(p, "\\(oA");
                   1096:                break;
                   1097:        case (TEXICMD_AASMALL):
                   1098:                texiputchars(p, "\\(oa");
                   1099:                break;
                   1100:        case (TEXICMD_AE):
                   1101:                texiputchars(p, "\\(AE");
                   1102:                break;
                   1103:        case (TEXICMD_AESMALL):
                   1104:                texiputchars(p, "\\(ae");
                   1105:                break;
1.3       kristaps 1106:        case (TEXICMD_ASTERISK):
                   1107:        case (TEXICMD_NEWLINE):
                   1108:        case (TEXICMD_SPACE):
1.18      kristaps 1109:        case (TEXICMD_TABSYM):
1.3       kristaps 1110:                texiputchar(p, ' ');
                   1111:                break;
1.1       kristaps 1112:        case (TEXICMD_AT):
1.3       kristaps 1113:                texiputchar(p, '@');
                   1114:                break;
                   1115:        case (TEXICMD_BANG):
                   1116:                texiputchar(p, '!');
1.7       kristaps 1117:                break;
                   1118:        case (TEXICMD_BULLET):
                   1119:                texiputchars(p, "\\(bu");
1.1       kristaps 1120:                break;
1.35      kristaps 1121:        case (TEXICMD_COMMA):
                   1122:                texiputchar(p, ',');
                   1123:                break;
1.1       kristaps 1124:        case (TEXICMD_COPYRIGHT):
                   1125:                texiputchars(p, "\\(co");
                   1126:                break;
1.42      kristaps 1127:        case (TEXICMD_DH):
                   1128:                texiputchars(p, "\\(-D");
                   1129:                break;
                   1130:        case (TEXICMD_DHSMALL):
                   1131:                texiputchars(p, "\\(Sd");
                   1132:                break;
1.2       kristaps 1133:        case (TEXICMD_DOTS):
1.34      kristaps 1134:        case (TEXICMD_ENDDOTS):
1.2       kristaps 1135:                texiputchars(p, "...");
                   1136:                break;
1.28      kristaps 1137:        case (TEXICMD_EQUIV):
                   1138:                texiputchars(p, "\\(==");
                   1139:                break;
1.15      kristaps 1140:        case (TEXICMD_ERROR):
                   1141:                texiputchars(p, "error\\(->");
1.17      kristaps 1142:                break;
1.43    ! kristaps 1143:        case (TEXICMD_EURO):
        !          1144:                texiputchars(p, "\\(Eu");
        !          1145:                break;
1.42      kristaps 1146:        case (TEXICMD_EXCLAMDOWN):
                   1147:                texiputchars(p, "\\(r!");
                   1148:                break;
1.17      kristaps 1149:        case (TEXICMD_EXPANSION):
                   1150:                texiputchars(p, "\\(->");
1.15      kristaps 1151:                break;
1.43    ! kristaps 1152:        case (TEXICMD_GEQ):
        !          1153:                texiputchars(p, "\\(>=");
        !          1154:                break;
        !          1155:        case (TEXICMD_GUILLEMETLEFT):
        !          1156:        case (TEXICMD_GUILLEMOTLEFT):
        !          1157:                texiputchars(p, "\\(Fo");
        !          1158:                break;
        !          1159:        case (TEXICMD_GUILLEMETRIGHT):
        !          1160:        case (TEXICMD_GUILLEMOTRIGHT):
        !          1161:                texiputchars(p, "\\(Fc");
        !          1162:                break;
        !          1163:        case (TEXICMD_GUILSINGLLEFT):
        !          1164:                texiputchars(p, "\\(fo");
        !          1165:                break;
        !          1166:        case (TEXICMD_GUILSINGLRIGHT):
        !          1167:                texiputchars(p, "\\(fc");
        !          1168:                break;
1.42      kristaps 1169:        case (TEXICMD_L):
                   1170:                texiputchars(p, "\\(/L");
                   1171:                break;
1.1       kristaps 1172:        case (TEXICMD_LATEX):
                   1173:                texiputchars(p, "LaTeX");
                   1174:                break;
1.43    ! kristaps 1175:        case (TEXICMD_LEQ):
        !          1176:                texiputchars(p, "\\(<=");
        !          1177:                break;
1.42      kristaps 1178:        case (TEXICMD_LSMALL):
                   1179:                texiputchars(p, "\\(/l");
                   1180:                break;
1.25      kristaps 1181:        case (TEXICMD_MINUS):
                   1182:                texiputchars(p, "\\-");
                   1183:                break;
1.42      kristaps 1184:        case (TEXICMD_O):
                   1185:                texiputchars(p, "\\(/O");
                   1186:                break;
                   1187:        case (TEXICMD_OE):
                   1188:                texiputchars(p, "\\(OE");
                   1189:                break;
                   1190:        case (TEXICMD_OESMALL):
                   1191:                texiputchars(p, "\\(oe");
                   1192:                break;
                   1193:        case (TEXICMD_ORDF):
                   1194:                texiputchars(p, "a");
                   1195:                break;
                   1196:        case (TEXICMD_ORDM):
                   1197:                texiputchars(p, "o");
                   1198:                break;
                   1199:        case (TEXICMD_OSMALL):
                   1200:                texiputchars(p, "\\(/o");
                   1201:                break;
1.25      kristaps 1202:        case (TEXICMD_PERIOD):
                   1203:                texiputchar(p, '.');
                   1204:                break;
1.43    ! kristaps 1205:        case (TEXICMD_POUNDS):
        !          1206:                texiputchars(p, "\\(Po");
        !          1207:                break;
1.42      kristaps 1208:        case (TEXICMD_QUESTIONDOWN):
                   1209:                texiputchars(p, "\\(r?");
                   1210:                break;
1.3       kristaps 1211:        case (TEXICMD_QUESTIONMARK):
                   1212:                texiputchar(p, '?');
1.15      kristaps 1213:                break;
1.43    ! kristaps 1214:        case (TEXICMD_QUOTEDBLBASE):
        !          1215:                texiputchars(p, "\\(Bq");
        !          1216:                break;
        !          1217:        case (TEXICMD_QUOTEDBLLEFT):
        !          1218:                texiputchars(p, "\\(lq");
        !          1219:                break;
        !          1220:        case (TEXICMD_QUOTEDBLRIGHT):
        !          1221:                texiputchars(p, "\\(rq");
        !          1222:                break;
        !          1223:        case (TEXICMD_QUOTESINGLBASE):
        !          1224:                texiputchars(p, "\\(bq");
        !          1225:                break;
        !          1226:        case (TEXICMD_QUOTELEFT):
        !          1227:                texiputchars(p, "\\(oq");
        !          1228:                break;
        !          1229:        case (TEXICMD_QUOTERIGHT):
        !          1230:                texiputchars(p, "\\(cq");
        !          1231:                break;
        !          1232:        case (TEXICMD_REGISTEREDSYMBOL):
        !          1233:                texiputchars(p, "\\(rg");
        !          1234:                break;
1.15      kristaps 1235:        case (TEXICMD_RESULT):
                   1236:                texiputchars(p, "\\(rA");
1.3       kristaps 1237:                break;
1.34      kristaps 1238:        case (TEXICMD_SLASH):
                   1239:                texiputchar(p, '/');
                   1240:                break;
1.35      kristaps 1241:        case (TEXICMD_SS):
                   1242:                texiputchars(p, "\\(ss");
                   1243:                break;
1.3       kristaps 1244:        case (TEXICMD_SQUIGGLE_LEFT):
                   1245:                texiputchars(p, "{");
                   1246:                break;
                   1247:        case (TEXICMD_SQUIGGLE_RIGHT):
                   1248:                texiputchars(p, "}");
                   1249:                break;
1.38      kristaps 1250:        case (TEXICMD_TEXSYM):
1.35      kristaps 1251:                texiputchars(p, "TeX");
1.43    ! kristaps 1252:                break;
        !          1253:        case (TEXICMD_TEXTDEGREE):
        !          1254:                texiputchars(p, "\\(de");
1.35      kristaps 1255:                break;
1.42      kristaps 1256:        case (TEXICMD_TH):
                   1257:                texiputchars(p, "\\(TP");
                   1258:                break;
                   1259:        case (TEXICMD_THSMALL):
                   1260:                texiputchars(p, "\\(Tp");
                   1261:                break;
1.38      kristaps 1262:        case (TEXICMD_TIE):
1.35      kristaps 1263:                texiputchars(p, "\\ ");
1.1       kristaps 1264:                break;
1.3       kristaps 1265:        case (TEXICMD_COLON):
                   1266:        case (TEXICMD_HYPHEN):
                   1267:                break;
1.1       kristaps 1268:        default:
                   1269:                abort();
                   1270:        }
                   1271:
1.42      kristaps 1272:        /* Alphabetic commands have braces we ignore. */
                   1273:        if (isalpha(texitoks[cmd].tok[0]))
1.5       kristaps 1274:                doignbracket(p, cmd, buf, sz, pos);
1.1       kristaps 1275: }
                   1276:
                   1277: static void
                   1278: doquotation(struct texi *p, enum texicmd cmd,
                   1279:        const char *buf, size_t sz, size_t *pos)
                   1280: {
                   1281:
1.5       kristaps 1282:        teximacro(p, "Qo");
1.1       kristaps 1283:        parseto(p, buf, sz, pos, "quotation");
1.5       kristaps 1284:        teximacro(p, "Qc");
1.1       kristaps 1285: }
                   1286:
1.3       kristaps 1287: static void
                   1288: domath(struct texi *p, enum texicmd cmd,
                   1289:        const char *buf, size_t sz, size_t *pos)
                   1290: {
1.34      kristaps 1291:        size_t   nest, start;
1.3       kristaps 1292:
                   1293:        /*
                   1294:         * Math handling is different from everything else.
                   1295:         * We don't allow any subcomponents, and we ignore the rules in
                   1296:         * terms of @-commands.
                   1297:         * This departs from GNU's rules, but whatever.
                   1298:         */
                   1299:        while (*pos < sz && isws(buf[*pos]))
                   1300:                advance(p, buf, pos);
                   1301:        if (*pos == sz || '{' != buf[*pos])
                   1302:                return;
                   1303:        advance(p, buf, pos);
                   1304:        if (p->seenws && p->outcol && 0 == p->literal)
                   1305:                texiputchar(p, ' ');
                   1306:        p->seenws = 0;
1.34      kristaps 1307:        for (nest = 1, start = *pos; *pos < sz && nest > 0; ) {
1.3       kristaps 1308:                if ('{' == buf[*pos])
                   1309:                        nest++;
                   1310:                else if ('}' == buf[*pos])
                   1311:                        if (0 == --nest)
                   1312:                                continue;
                   1313:                advance(p, buf, pos);
                   1314:        }
                   1315:        if (*pos == sz)
                   1316:                return;
                   1317:        assert('}' == buf[*pos]);
1.34      kristaps 1318:        texiputbuf(p, buf, start, *pos);
1.3       kristaps 1319:        advance(p, buf, pos);
1.24      kristaps 1320: }
                   1321:
                   1322: static void
                   1323: dovalue(struct texi *p, enum texicmd cmd,
                   1324:        const char *buf, size_t sz, size_t *pos)
                   1325: {
1.28      kristaps 1326:        size_t           start, end, i;
1.25      kristaps 1327:        char            *key, *val;
                   1328:        const char      *cp;
1.24      kristaps 1329:
                   1330:        if (TEXICMD_SET == cmd) {
                   1331:                while (*pos < sz && isws(buf[*pos]))
                   1332:                        advance(p, buf, pos);
                   1333:                for (start = end = *pos; end < sz; end++)
                   1334:                        if (ismspace(buf[end]))
                   1335:                                break;
1.25      kristaps 1336:                /* We don't allow empty keys. */
1.24      kristaps 1337:                if (start == end)
                   1338:                        return;
1.25      kristaps 1339:                advanceto(p, buf, pos, end);
1.24      kristaps 1340:
                   1341:                key = malloc(end - start + 1);
1.27      kristaps 1342:                if (NULL == key)
                   1343:                        texiabort(p, NULL);
1.24      kristaps 1344:                memcpy(key, &buf[start], end - start);
                   1345:                key[end - start] = '\0';
                   1346:
                   1347:                while (*pos < sz && isws(buf[*pos]))
                   1348:                        advance(p, buf, pos);
                   1349:                for (start = end = *pos; end < sz; end++)
                   1350:                        if ('\n' == buf[end])
                   1351:                                break;
1.25      kristaps 1352:                /* We do allow empty values. */
                   1353:                advanceeoln(p, buf, sz, pos, 1);
1.24      kristaps 1354:
                   1355:                val = malloc(end - start + 1);
1.27      kristaps 1356:                if (NULL == val)
                   1357:                        texiabort(p, NULL);
1.24      kristaps 1358:                memcpy(val, &buf[start], end - start);
                   1359:                val[end - start] = '\0';
1.25      kristaps 1360:                valueadd(p, key, val);
1.24      kristaps 1361:        } else if (TEXICMD_VALUE == cmd) {
                   1362:                if (p->seenws)
                   1363:                        texiputchar(p, ' ');
                   1364:                p->seenws = 0;
1.28      kristaps 1365:                if (NULL != (cp = valueblookup(p, buf, sz, pos))) {
                   1366:                        for (i = 0; i < p->valstackpos; i++)
                   1367:                                if (cp == p->valstack[i])
                   1368:                                        break;
                   1369:                        if (i < p->valstackpos)
                   1370:                                texierr(p, "recursive value");
1.29      kristaps 1371:                        if (64 == p->valstackpos)
                   1372:                                texierr(p, "too many nested values");
1.28      kristaps 1373:                        p->valstack[p->valstackpos++] = cp;
1.31      kristaps 1374:                        parsemembuf(p, cp, strlen(cp));
1.28      kristaps 1375:                        p->valstackpos--;
                   1376:                } else
1.25      kristaps 1377:                        texiputchars(p, "{No value}");
1.24      kristaps 1378:        } else if (TEXICMD_IFCLEAR == cmd) {
1.26      kristaps 1379:                if (NULL != valuellookup(p, buf, sz, pos))
                   1380:                        doignblock(p, cmd, buf, sz, pos);
                   1381:                else
                   1382:                        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.30      kristaps 1383:        } else if (TEXICMD_IFSET == cmd) {
                   1384:                if (NULL == valuellookup(p, buf, sz, pos))
                   1385:                        doignblock(p, cmd, buf, sz, pos);
                   1386:                else
                   1387:                        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.25      kristaps 1388:        } else if (TEXICMD_CLEAR == cmd)
                   1389:                valuelclear(p, buf, sz, pos);
1.3       kristaps 1390: }
                   1391:
1.1       kristaps 1392: static void
1.8       kristaps 1393: dolink(struct texi *p, enum texicmd cmd,
1.1       kristaps 1394:        const char *buf, size_t sz, size_t *pos)
                   1395: {
1.8       kristaps 1396:        int      c;
1.1       kristaps 1397:
                   1398:        switch (cmd) {
                   1399:        case (TEXICMD_EMAIL):
1.5       kristaps 1400:                teximacroopen(p, "Mt");
1.1       kristaps 1401:                break;
1.3       kristaps 1402:        case (TEXICMD_UREF):
1.1       kristaps 1403:        case (TEXICMD_URL):
1.5       kristaps 1404:                teximacroopen(p, "Lk");
1.1       kristaps 1405:                break;
1.8       kristaps 1406:        case (TEXICMD_XREF):
                   1407:                texiputchars(p, "See Section");
1.39      kristaps 1408:                teximacroopen(p, "Dq");
1.8       kristaps 1409:                break;
                   1410:        case (TEXICMD_PXREF):
                   1411:                texiputchars(p, "see Section");
1.39      kristaps 1412:                teximacroopen(p, "Dq");
1.8       kristaps 1413:                break;
1.34      kristaps 1414:        case (TEXICMD_INFOREF):
                   1415:                texiputchars(p, "See Info file node");
1.39      kristaps 1416:                teximacroopen(p, "Dq");
1.34      kristaps 1417:                break;
1.1       kristaps 1418:        default:
1.8       kristaps 1419:                abort();
1.1       kristaps 1420:        }
1.8       kristaps 1421:
                   1422:        c = parsearg(p, buf, sz, pos, 0);
                   1423:        p->ign++;
                   1424:        while (c > 0)
                   1425:                c = parsearg(p, buf, sz, pos, 1);
                   1426:        p->ign--;
                   1427:
1.11      kristaps 1428:        texipunctuate(p, buf, sz, pos);
1.8       kristaps 1429:        teximacroclose(p);
                   1430: }
                   1431:
                   1432: static void
                   1433: doignargn(struct texi *p, enum texicmd cmd,
                   1434:        const char *buf, size_t sz, size_t *pos)
                   1435: {
                   1436:        int      c;
                   1437:
                   1438:        c = parsearg(p, buf, sz, pos, 0);
                   1439:        p->ign++;
                   1440:        while (c > 0)
                   1441:                c = parsearg(p, buf, sz, pos, 1);
                   1442:        p->ign--;
1.1       kristaps 1443: }
                   1444:
1.23      kristaps 1445: /*
                   1446:  * Sections can be made subsections and so on by way of the
                   1447:  * @raiseections and @lowersections commands.
                   1448:  * Perform this check here and return the actual section number adjusted
                   1449:  * to the raise level.
                   1450:  */
                   1451: static int
                   1452: sectioner(struct texi *p, int sec)
                   1453: {
                   1454:
                   1455:        if ((sec -= p->secoffs) < 0) {
                   1456:                texiwarn(p, "section below minimum, clamping");
                   1457:                return(0);
                   1458:        } else if (sec >= SECTSZ) {
                   1459:                texiwarn(p, "section above maximum, clamping");
                   1460:                return(SECTSZ - 1);
                   1461:        }
                   1462:        return(sec);
                   1463: }
                   1464:
                   1465: static void
                   1466: dosubsubsection(struct texi *p, enum texicmd cmd,
                   1467:                const char *buf, size_t sz, size_t *pos)
                   1468: {
                   1469:        int      sec;
                   1470:
                   1471:        sec = sectioner(p, 3);
                   1472:
                   1473:        /* We don't have a subsubsubsection, so make one up. */
                   1474:        texivspace(p);
                   1475:        teximacroopen(p, sects[sec]);
                   1476:        parseeoln(p, buf, sz, pos);
                   1477:        teximacroclose(p);
                   1478:        texivspace(p);
                   1479: }
                   1480:
1.1       kristaps 1481: static void
                   1482: dosubsection(struct texi *p, enum texicmd cmd,
                   1483:                const char *buf, size_t sz, size_t *pos)
                   1484: {
1.23      kristaps 1485:        int      sec;
                   1486:
                   1487:        sec = sectioner(p, 2);
1.13      kristaps 1488:
                   1489:        if (p->outmacro)
1.23      kristaps 1490:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13      kristaps 1491:        else if (p->literal)
1.23      kristaps 1492:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1       kristaps 1493:
1.21      kristaps 1494:        /* We don't have a subsubsection, so make one up. */
1.34      kristaps 1495:        if (sec > 1)
                   1496:                texivspace(p);
1.23      kristaps 1497:        teximacroopen(p, sects[sec]);
1.3       kristaps 1498:        parseeoln(p, buf, sz, pos);
1.5       kristaps 1499:        teximacroclose(p);
1.34      kristaps 1500:        if (sec > 1)
                   1501:                texivspace(p);
1.1       kristaps 1502: }
                   1503:
                   1504: static void
1.23      kristaps 1505: dosecoffs(struct texi *p, enum texicmd cmd,
                   1506:        const char *buf, size_t sz, size_t *pos)
                   1507: {
                   1508:
                   1509:        if (TEXICMD_RAISESECTIONS == cmd)
                   1510:                p->secoffs++;
                   1511:        else
                   1512:                p->secoffs--;
                   1513: }
                   1514:
                   1515: static void
1.1       kristaps 1516: dosection(struct texi *p, enum texicmd cmd,
1.23      kristaps 1517:        const char *buf, size_t sz, size_t *pos)
1.1       kristaps 1518: {
1.23      kristaps 1519:        int              sec;
1.12      kristaps 1520:
                   1521:        switch (cmd) {
                   1522:        case (TEXICMD_APPENDIX):
                   1523:        case (TEXICMD_CHAPTER):
                   1524:        case (TEXICMD_TOP):
                   1525:        case (TEXICMD_UNNUMBERED):
1.23      kristaps 1526:                sec = sectioner(p, 0);
1.12      kristaps 1527:                break;
                   1528:        case (TEXICMD_APPENDIXSEC):
                   1529:        case (TEXICMD_HEADING):
                   1530:        case (TEXICMD_SECTION):
                   1531:        case (TEXICMD_UNNUMBEREDSEC):
1.23      kristaps 1532:                sec = sectioner(p, 1);
1.12      kristaps 1533:                break;
                   1534:        default:
                   1535:                abort();
                   1536:        }
1.1       kristaps 1537:
1.3       kristaps 1538:        if (p->outmacro)
1.23      kristaps 1539:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3       kristaps 1540:        else if (p->literal)
1.23      kristaps 1541:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3       kristaps 1542:
1.23      kristaps 1543:        teximacroopen(p, sects[sec]);
1.3       kristaps 1544:        parseeoln(p, buf, sz, pos);
                   1545:        teximacroclose(p);
1.11      kristaps 1546:        p->seenvs = 1;
1.3       kristaps 1547: }
                   1548:
                   1549: static void
                   1550: dosp(struct texi *p, enum texicmd cmd,
                   1551:        const char *buf, size_t sz, size_t *pos)
                   1552: {
                   1553:
1.37      kristaps 1554:        if (p->literal)
                   1555:                texiputchar(p, '\n');
                   1556:        else
                   1557:                texivspace(p);
1.12      kristaps 1558:        /* FIXME: ignore and parseeoln. */
1.3       kristaps 1559:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1560: }
                   1561:
                   1562: static void
                   1563: dotop(struct texi *p, enum texicmd cmd,
                   1564:        const char *buf, size_t sz, size_t *pos)
                   1565: {
1.10      kristaps 1566:        const char      *cp;
                   1567:        time_t           t;
                   1568:        char             date[32];
                   1569:
1.26      kristaps 1570:        if (--p->ign)
                   1571:                texierr(p, "@top command while ignoring (%d)", p->ign);
                   1572:
1.18      kristaps 1573:        /*
                   1574:         * Here we print our standard mdoc(7) prologue.
                   1575:         * We use the title set with @settitle for the `Nd' description
                   1576:         * and the source document filename (the first one as invoked on
                   1577:         * the command line) for the title.
                   1578:         * The date is set to the current date.
                   1579:         */
1.10      kristaps 1580:        t = time(NULL);
                   1581:        strftime(date, sizeof(date), "%F", localtime(&t));
1.1       kristaps 1582:
1.10      kristaps 1583:        teximacroopen(p, "Dd");
                   1584:        texiputchars(p, date);
                   1585:        teximacroclose(p);
                   1586:        teximacroopen(p, "Dt");
                   1587:        for (cp = p->title; '\0' != *cp; cp++)
                   1588:                texiputchar(p, toupper(*cp));
1.11      kristaps 1589:        texiputchars(p, " 7");
1.10      kristaps 1590:        teximacroclose(p);
1.5       kristaps 1591:        teximacro(p, "Os");
                   1592:        teximacro(p, "Sh NAME");
1.10      kristaps 1593:        teximacroopen(p, "Nm");
1.41      kristaps 1594:        for (cp = p->title; '\0' != *cp; cp++)
                   1595:                texiputchar(p, *cp);
1.10      kristaps 1596:        teximacroclose(p);
                   1597:        teximacroopen(p, "Nd");
1.41      kristaps 1598:        if (NULL != p->subtitle)
                   1599:                for (cp = p->subtitle; '\0' != *cp; cp++)
                   1600:                        texiputchar(p, *cp);
                   1601:        else
                   1602:                texiputchars(p, "Unknown description");
1.10      kristaps 1603:        teximacroclose(p);
1.11      kristaps 1604:        p->seenvs = 1;
1.12      kristaps 1605:        dosection(p, cmd, buf, sz, pos);
1.1       kristaps 1606: }
                   1607:
                   1608: static void
                   1609: doitem(struct texi *p, enum texicmd cmd,
                   1610:        const char *buf, size_t sz, size_t *pos)
                   1611: {
                   1612:
1.18      kristaps 1613:        /* Multitable is using raw tbl(7). */
                   1614:        if (TEXILIST_TABLE == p->list) {
                   1615:                texiputchar(p, '\n');
                   1616:                return;
                   1617:        }
                   1618:
1.3       kristaps 1619:        if (p->outmacro)
                   1620:                texierr(p, "item in open line scope!?");
                   1621:        else if (p->literal)
                   1622:                texierr(p, "item in a literal scope!?");
                   1623:
                   1624:        switch (p->list) {
                   1625:        case (TEXILIST_ITEM):
1.5       kristaps 1626:                teximacroopen(p, "It");
1.3       kristaps 1627:                break;
                   1628:        case (TEXILIST_NOITEM):
1.5       kristaps 1629:                teximacro(p, "It");
1.3       kristaps 1630:                break;
                   1631:        default:
1.11      kristaps 1632:                texivspace(p);
1.3       kristaps 1633:                break;
                   1634:        }
1.18      kristaps 1635:
                   1636:        /* Trick so we don't start with Pp. */
1.11      kristaps 1637:        p->seenvs = 1;
1.3       kristaps 1638:        parseeoln(p, buf, sz, pos);
1.1       kristaps 1639:
1.3       kristaps 1640:        if (TEXILIST_ITEM == p->list)
                   1641:                teximacroclose(p);
1.9       kristaps 1642:        else if (p->outcol > 0)
1.1       kristaps 1643:                texiputchar(p, '\n');
1.18      kristaps 1644: }
                   1645:
                   1646: static void
                   1647: dotab(struct texi *p, enum texicmd cmd,
                   1648:        const char *buf, size_t sz, size_t *pos)
                   1649: {
                   1650:
                   1651:        /* This command is only useful in @multitable. */
                   1652:        if (TEXILIST_TABLE == p->list)
                   1653:                texiputchar(p, '\t');
                   1654: }
                   1655:
                   1656: static void
                   1657: domultitable(struct texi *p, enum texicmd cmd,
                   1658:        const char *buf, size_t sz, size_t *pos)
                   1659: {
                   1660:        enum texilist   sv = p->list;
1.32      kristaps 1661:        int             svliteral = p->literal;
1.18      kristaps 1662:        enum texicmd    type;
                   1663:        size_t          i, end, columns;
                   1664:
                   1665:        p->list = TEXILIST_TABLE;
1.32      kristaps 1666:        /*
                   1667:         * TS/TE blocks aren't "in mdoc(7)", so we can disregard the
                   1668:         * fact that we're in literal mode right now.
                   1669:         */
                   1670:        p->literal = 0;
1.18      kristaps 1671:        teximacro(p, "TS");
                   1672:        columns = 0;
                   1673:
                   1674:        /* Advance to the first argument... */
                   1675:        while (*pos < sz && isws(buf[*pos]))
                   1676:                advance(p, buf, pos);
                   1677:
                   1678:        /* Make sure we don't print anything when scanning. */
                   1679:        p->ign++;
                   1680:        if ('@' == buf[*pos]) {
                   1681:                /*
                   1682:                 * Look for @columnfractions.
                   1683:                 * We ignore these, but we do use the number of
                   1684:                 * arguments to set the number of columns that we'll
                   1685:                 * have.
                   1686:                 */
1.30      kristaps 1687:                type = texicmd(p, buf, *pos, sz, &end, NULL);
1.18      kristaps 1688:                advanceto(p, buf, pos, end);
                   1689:                if (TEXICMD_COLUMNFRACTIONS != type)
1.25      kristaps 1690:                        texierr(p, "unknown multitable command");
1.18      kristaps 1691:                while (*pos < sz && '\n' != buf[*pos]) {
                   1692:                        while (*pos < sz && isws(buf[*pos]))
                   1693:                                advance(p, buf, pos);
                   1694:                        while (*pos < sz && ! isws(buf[*pos])) {
                   1695:                                if ('\n' == buf[*pos])
                   1696:                                        break;
                   1697:                                advance(p, buf, pos);
                   1698:                        }
                   1699:                        columns++;
                   1700:                }
                   1701:        } else
                   1702:                /*
                   1703:                 * We have arguments.
                   1704:                 * We could parse these, but it's easier to just let
                   1705:                 * tbl(7) figure it out.
                   1706:                 * So use this only to count arguments.
                   1707:                 */
                   1708:                while (parselinearg(p, buf, sz, pos) > 0)
                   1709:                        columns++;
                   1710:        p->ign--;
                   1711:
                   1712:        /* Left-justify each table entry. */
                   1713:        for (i = 0; i < columns; i++) {
                   1714:                if (i > 0)
                   1715:                        texiputchar(p, ' ');
                   1716:                texiputchar(p, 'l');
                   1717:        }
                   1718:        texiputchars(p, ".\n");
                   1719:        p->outmacro++;
                   1720:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
                   1721:        p->outmacro--;
                   1722:        teximacro(p, "TE");
1.32      kristaps 1723:        p->literal = svliteral;
1.18      kristaps 1724:        p->list = sv;
1.1       kristaps 1725: }
                   1726:
                   1727: static void
                   1728: dotable(struct texi *p, enum texicmd cmd,
                   1729:        const char *buf, size_t sz, size_t *pos)
                   1730: {
1.3       kristaps 1731:        enum texilist   sv = p->list;
                   1732:
                   1733:        p->list = TEXILIST_ITEM;
1.5       kristaps 1734:        teximacro(p, "Bl -tag -width Ds");
1.12      kristaps 1735:        /* FIXME: ignore and parseeoln. */
                   1736:        advanceeoln(p, buf, sz, pos, 1);
1.11      kristaps 1737:        p->seenvs = 1;
1.20      kristaps 1738:        parseto(p, buf, sz, pos, texitoks[cmd].tok);
1.5       kristaps 1739:        teximacro(p, "El");
1.3       kristaps 1740:        p->list = sv;
1.1       kristaps 1741: }
                   1742:
                   1743: static void
1.2       kristaps 1744: doenumerate(struct texi *p, enum texicmd cmd,
                   1745:        const char *buf, size_t sz, size_t *pos)
                   1746: {
1.3       kristaps 1747:        enum texilist    sv = p->list;
1.2       kristaps 1748:
1.3       kristaps 1749:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1750:        teximacro(p, "Bl -enum");
1.11      kristaps 1751:        p->seenvs = 1;
1.12      kristaps 1752:        /* FIXME: ignore and parseeoln. */
                   1753:        advanceeoln(p, buf, sz, pos, 1);
1.2       kristaps 1754:        parseto(p, buf, sz, pos, "enumerate");
1.5       kristaps 1755:        teximacro(p, "El");
1.3       kristaps 1756:        p->list = sv;
1.2       kristaps 1757: }
                   1758:
                   1759: static void
1.1       kristaps 1760: doitemize(struct texi *p, enum texicmd cmd,
                   1761:        const char *buf, size_t sz, size_t *pos)
                   1762: {
1.3       kristaps 1763:        enum texilist   sv = p->list;
1.1       kristaps 1764:
1.21      kristaps 1765:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1766:        teximacro(p, "Bl -bullet");
1.11      kristaps 1767:        p->seenvs = 1;
1.12      kristaps 1768:        /* FIXME: ignore and parseeoln. */
                   1769:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1770:        parseto(p, buf, sz, pos, "itemize");
1.5       kristaps 1771:        teximacro(p, "El");
1.3       kristaps 1772:        p->list = sv;
1.1       kristaps 1773: }
                   1774:
                   1775: static void
                   1776: doignbracket(struct texi *p, enum texicmd cmd,
                   1777:        const char *buf, size_t sz, size_t *pos)
                   1778: {
                   1779:
1.3       kristaps 1780:        p->ign++;
1.1       kristaps 1781:        parsebracket(p, buf, sz, pos);
1.3       kristaps 1782:        p->ign--;
1.1       kristaps 1783: }
                   1784:
                   1785: static void
                   1786: doignline(struct texi *p, enum texicmd cmd,
                   1787:        const char *buf, size_t sz, size_t *pos)
                   1788: {
                   1789:
1.12      kristaps 1790:        /* FIXME: ignore and parseeoln. */
1.3       kristaps 1791:        advanceeoln(p, buf, sz, pos, 1);
1.1       kristaps 1792: }
                   1793:
1.8       kristaps 1794: /*
                   1795:  * Parse colon-separated directories from "cp" (if not NULL) and returns
                   1796:  * the array of pointers.
1.40      kristaps 1797:  * Prepends "base" to the array, if found.
1.8       kristaps 1798:  * This does NOT sanitise the directories!
                   1799:  */
1.5       kristaps 1800: static char **
1.27      kristaps 1801: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5       kristaps 1802: {
                   1803:        char             *tok, *str, *tofree;
                   1804:        const char       *cpp;
1.40      kristaps 1805:        size_t            i = 0;
1.5       kristaps 1806:        char            **dirs;
                   1807:
1.40      kristaps 1808:        /* Count up our expected arguments. */
                   1809:        *sz = NULL != base;
                   1810:        if (NULL != (cpp = cp))
                   1811:                for ((*sz)++; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1.5       kristaps 1812:                        cpp++;
                   1813:
1.40      kristaps 1814:        if (0 == *sz)
                   1815:                return(NULL);
1.27      kristaps 1816:        if (NULL == (dirs = calloc(*sz, sizeof(char *))))
                   1817:                texiabort(p, NULL);
1.40      kristaps 1818:        if (NULL != base && NULL == (dirs[i++] = strdup(base)))
1.27      kristaps 1819:                texiabort(p, NULL);
1.5       kristaps 1820:        if (NULL == cp)
                   1821:                return(dirs);
1.27      kristaps 1822:        if (NULL == (tofree = tok = str = strdup(cp)))
                   1823:                texiabort(p, NULL);
1.5       kristaps 1824:
1.40      kristaps 1825:        for ( ; NULL != (tok = strsep(&str, ":")); i++)
1.27      kristaps 1826:                if (NULL == (dirs[i] = strdup(tok)))
                   1827:                        texiabort(p, NULL);
1.5       kristaps 1828:
                   1829:        free(tofree);
                   1830:        return(dirs);
                   1831: }
                   1832:
1.1       kristaps 1833: int
                   1834: main(int argc, char *argv[])
                   1835: {
                   1836:        struct texi      texi;
1.2       kristaps 1837:        int              c;
1.37      kristaps 1838:        char            *dirpath, *dir, *ccp;
1.10      kristaps 1839:        const char      *progname, *Idir, *cp;
1.1       kristaps 1840:
                   1841:        progname = strrchr(argv[0], '/');
                   1842:        if (progname == NULL)
                   1843:                progname = argv[0];
                   1844:        else
                   1845:                ++progname;
                   1846:
1.10      kristaps 1847:        memset(&texi, 0, sizeof(struct texi));
1.40      kristaps 1848:        texi.ign = 1;
1.5       kristaps 1849:        Idir = NULL;
1.10      kristaps 1850:
1.5       kristaps 1851:        while (-1 != (c = getopt(argc, argv, "I:")))
1.1       kristaps 1852:                switch (c) {
1.5       kristaps 1853:                case ('I'):
                   1854:                        Idir = optarg;
                   1855:                        break;
1.1       kristaps 1856:                default:
                   1857:                        goto usage;
                   1858:                }
                   1859:
                   1860:        argv += optind;
1.40      kristaps 1861:        argc -= optind;
1.1       kristaps 1862:
1.40      kristaps 1863:        if (argc > 0) {
                   1864:                if (NULL == (dirpath = strdup(argv[0])))
                   1865:                        texiabort(&texi, NULL);
                   1866:                if (NULL == (dir = dirname(dirpath)))
                   1867:                        texiabort(&texi, NULL);
                   1868:                if (NULL != (cp = strrchr(argv[0], '/')))
                   1869:                        texi.title = strdup(cp + 1);
                   1870:                else
                   1871:                        texi.title = strdup(argv[0]);
                   1872:                if (NULL == texi.title)
                   1873:                        texiabort(&texi, NULL);
                   1874:                else if (NULL != (ccp = strchr(texi.title, '.')))
                   1875:                        *ccp = '\0';
                   1876:                texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
                   1877:                free(dirpath);
                   1878:                parsefile(&texi, argv[0], 1);
                   1879:        } else {
                   1880:                texi.title = strdup("Unknown Manual");
                   1881:                texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
                   1882:                parsestdin(&texi);
                   1883:        }
1.27      kristaps 1884:
1.2       kristaps 1885:        texiexit(&texi);
                   1886:        return(EXIT_FAILURE);
1.1       kristaps 1887: usage:
1.40      kristaps 1888:        fprintf(stderr, "usage: %s [-Idirs] [file]\n", progname);
1.1       kristaps 1889:        return(EXIT_FAILURE);
                   1890: }

CVSweb