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

Annotation of texi2mdoc/main.c, Revision 1.61

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

CVSweb