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

Annotation of texi2mdoc/main.c, Revision 1.63

1.63    ! kristaps    1: /*     $Id: main.c,v 1.62 2015/03/05 09:36:41 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:        parseto(p, pos, blk);
                    548:        teximacro(p, "Ed");
1.62      kristaps  549:        p->seenvs = 1;
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.63    ! kristaps  758:        if (TEXICMD_CODE == cmd)
        !           759:                p->literal++;
1.58      kristaps  760:        if (TEXICMD_SC == cmd)
                    761:                p->uppercase++;
1.53      kristaps  762:        parsebracket(p, pos, 0);
1.58      kristaps  763:        if (TEXICMD_SC == cmd)
                    764:                p->uppercase--;
1.63    ! kristaps  765:        if (TEXICMD_CODE == cmd)
        !           766:                p->literal--;
1.46      kristaps  767:        texipunctuate(p, pos);
1.5       kristaps  768:        teximacroclose(p);
1.1       kristaps  769: }
                    770:
                    771: static void
1.46      kristaps  772: doverb(struct texi *p, enum texicmd cmd, size_t *pos)
1.33      kristaps  773: {
                    774:        char     delim;
1.34      kristaps  775:        size_t   start;
1.33      kristaps  776:
1.46      kristaps  777:        while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                    778:                advance(p, pos);
                    779:        if (*pos == BUFSZ(p) || '{' != BUF(p)[*pos])
1.33      kristaps  780:                return;
1.46      kristaps  781:        advance(p, pos);
                    782:        if (*pos == BUFSZ(p))
1.33      kristaps  783:                return;
                    784:
1.46      kristaps  785:        delim = BUF(p)[*pos];
                    786:        advance(p, pos);
1.33      kristaps  787:        /* Make sure we flush out our initial whitespace... */
                    788:        if (p->seenws && p->outcol && 0 == p->literal)
                    789:                texiputchar(p, ' ');
                    790:        p->seenws = 0;
1.34      kristaps  791:        start = *pos;
1.33      kristaps  792:        /* Read until we see the delimiter then end-brace. */
1.46      kristaps  793:        while (*pos < BUFSZ(p) - 1) {
                    794:                if (BUF(p)[*pos] == delim && BUF(p)[*pos + 1] == '}')
1.33      kristaps  795:                        break;
1.46      kristaps  796:                advance(p, pos);
1.33      kristaps  797:        }
1.51      kristaps  798:        if (*pos >= BUFSZ(p) - 1)
1.33      kristaps  799:                return;
1.46      kristaps  800:        texiputbuf(p, start, *pos);
1.34      kristaps  801:
1.33      kristaps  802:        /* Make sure we read after the end-brace. */
1.46      kristaps  803:        assert(delim == BUF(p)[*pos]);
                    804:        advance(p, pos);
                    805:        assert('}' == BUF(p)[*pos]);
                    806:        advance(p, pos);
1.33      kristaps  807: }
                    808:
                    809: static void
1.61      kristaps  810: doinsertcopying(struct texi *p, enum texicmd cmd, size_t *pos)
                    811: {
                    812:
                    813:        advanceeoln(p, pos, 0);
                    814:        if (NULL == p->copying)
                    815:                return;
                    816:        texisplice(p, p->copying, p->copyingsz, *pos);
                    817: }
                    818:
                    819: static void
                    820: docopying(struct texi *p, enum texicmd cmd, size_t *pos)
                    821: {
                    822:        const char      *end, *term;
                    823:        size_t           endsz, endpos;
                    824:
                    825:        /* We retain our starting (but not ending) newlines. */
                    826:        end = "\n@end copying\n";
                    827:        endsz = strlen(end);
                    828:        advanceeoln(p, pos, 0);
                    829:        if (*pos == BUFSZ(p)) {
                    830:                texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok);
                    831:                return;
                    832:        }
                    833:
                    834:        term = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, endsz);
                    835:        if (NULL == term) {
                    836:                texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok);
                    837:                endpos = BUFSZ(p);
                    838:        } else
                    839:                endpos = *pos + (size_t)(term - &BUF(p)[*pos]);
                    840:
                    841:        assert(endpos <= BUFSZ(p));
                    842:        assert('\n' == BUF(p)[*pos]);
                    843:        advance(p, pos);
                    844:
                    845:        p->copying = malloc(endpos - *pos + 1);
                    846:        p->copyingsz = endpos - *pos;
                    847:        memcpy(p->copying, &BUF(p)[*pos], p->copyingsz);
                    848:        p->copying[endpos - *pos] = '\0';
                    849:
                    850:        while (*pos < endpos)
                    851:                advance(p, pos);
                    852:        if (*pos < BUFSZ(p))
                    853:                advanceto(p, pos, endpos + endsz);
                    854: }
                    855:
                    856: static void
1.46      kristaps  857: doverbatim(struct texi *p, enum texicmd cmd, size_t *pos)
1.25      kristaps  858: {
                    859:        const char      *end, *term;
                    860:        size_t           endsz, endpos;
                    861:
1.45      kristaps  862:        /* We read from the @verbatim\n newline inclusive! */
1.25      kristaps  863:        end = "\n@end verbatim\n";
                    864:        endsz = strlen(end);
1.46      kristaps  865:        advanceeoln(p, pos, 0);
                    866:        if (*pos == BUFSZ(p)) {
1.61      kristaps  867:                texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok);
1.45      kristaps  868:                return;
                    869:        }
1.25      kristaps  870:
1.46      kristaps  871:        term = memmem(&BUF(p)[*pos], BUFSZ(p) - *pos, end, endsz);
1.45      kristaps  872:        if (NULL == term) {
1.61      kristaps  873:                texiwarn(p, "unterminated \"%s\"", texitoks[cmd].tok);
1.46      kristaps  874:                endpos = BUFSZ(p);
1.45      kristaps  875:        } else
1.46      kristaps  876:                endpos = *pos + (size_t)(term - &BUF(p)[*pos]);
1.25      kristaps  877:
1.46      kristaps  878:        assert(endpos <= BUFSZ(p));
                    879:        assert('\n' == BUF(p)[*pos]);
                    880:        advance(p, pos);
1.25      kristaps  881:        teximacro(p, "Bd -literal -offset indent");
1.26      kristaps  882:        while (*pos < endpos) {
1.46      kristaps  883:                texiputchar(p, BUF(p)[*pos]);
                    884:                advance(p, pos);
1.25      kristaps  885:        }
                    886:        teximacro(p, "Ed");
1.62      kristaps  887:        p->seenvs = 1;
1.46      kristaps  888:        if (*pos < BUFSZ(p))
                    889:                advanceto(p, pos, endpos + endsz);
1.25      kristaps  890: }
                    891:
                    892: static void
1.46      kristaps  893: doverbinclude(struct texi *p, enum texicmd cmd, size_t *pos)
1.16      kristaps  894: {
1.25      kristaps  895:        char             fname[PATH_MAX], path[PATH_MAX];
                    896:        int              rc;
                    897:        size_t           i, end;
                    898:        const char      *v;
                    899:        enum texicmd     type;
1.16      kristaps  900:
1.46      kristaps  901:        while (*pos < BUFSZ(p) && ' ' == BUF(p)[*pos])
                    902:                advance(p, pos);
1.16      kristaps  903:
1.46      kristaps  904:        for (i = 0; *pos < BUFSZ(p) && '\n' != BUF(p)[*pos]; ) {
1.16      kristaps  905:                if (i == sizeof(fname) - 1)
                    906:                        break;
1.46      kristaps  907:                if ('@' != BUF(p)[*pos]) {
                    908:                        fname[i++] = BUF(p)[*pos];
                    909:                        advance(p, pos);
1.25      kristaps  910:                        continue;
                    911:                }
1.46      kristaps  912:                type = texicmd(p, *pos, &end, NULL);
                    913:                advanceto(p, pos, end);
1.25      kristaps  914:                if (TEXICMD_VALUE != type)
                    915:                        texierr(p, "unknown verbatiminclude command");
1.46      kristaps  916:                v = valueblookup(p, pos);
1.25      kristaps  917:                if (NULL == v)
                    918:                        continue;
                    919:                while ('\0' != *v) {
                    920:                        if (i == sizeof(fname) - 1)
                    921:                                break;
                    922:                        fname[i++] = *v++;
                    923:                }
                    924:                if ('\0' != *v)
                    925:                        break;
1.16      kristaps  926:        }
                    927:
                    928:        if (i == 0)
                    929:                texierr(p, "path too short");
1.46      kristaps  930:        else if ('\n' != BUF(p)[*pos])
1.16      kristaps  931:                texierr(p, "path too long");
                    932:        else if ('/' == fname[0])
                    933:                texierr(p, "no absolute paths");
                    934:        fname[i] = '\0';
                    935:
                    936:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    937:                texierr(p, "insecure path");
                    938:
                    939:        rc = snprintf(path, sizeof(path),
                    940:                "%s/%s", p->dirs[0], fname);
                    941:        if (rc < 0)
                    942:                texierr(p, "couldn't format path");
                    943:        else if ((size_t)rc >= sizeof(path))
                    944:                texierr(p, "path too long");
                    945:
                    946:        parsefile(p, path, 0);
                    947: }
                    948:
                    949: static void
1.46      kristaps  950: doinclude(struct texi *p, enum texicmd cmd, size_t *pos)
1.2       kristaps  951: {
1.25      kristaps  952:        char             fname[PATH_MAX], path[PATH_MAX];
                    953:        size_t           i, end;
                    954:        int              rc;
                    955:        const char      *v;
                    956:        enum texicmd     type;
1.2       kristaps  957:
1.46      kristaps  958:        while (*pos < BUFSZ(p) && ' ' == BUF(p)[*pos])
                    959:                advance(p, pos);
1.2       kristaps  960:
                    961:        /* Read in the filename. */
1.46      kristaps  962:        for (i = 0; *pos < BUFSZ(p) && '\n' != BUF(p)[*pos]; ) {
1.2       kristaps  963:                if (i == sizeof(fname) - 1)
                    964:                        break;
1.46      kristaps  965:                if ('@' != BUF(p)[*pos]) {
                    966:                        fname[i++] = BUF(p)[*pos];
                    967:                        advance(p, pos);
1.25      kristaps  968:                        continue;
                    969:                }
1.46      kristaps  970:                type = texicmd(p, *pos, &end, NULL);
                    971:                advanceto(p, pos, end);
1.25      kristaps  972:                if (TEXICMD_VALUE != type)
                    973:                        texierr(p, "unknown include command");
1.46      kristaps  974:                v = valueblookup(p, pos);
1.25      kristaps  975:                if (NULL == v)
                    976:                        continue;
                    977:                while ('\0' != *v) {
                    978:                        if (i == sizeof(fname) - 1)
                    979:                                break;
                    980:                        fname[i++] = *v++;
                    981:                }
                    982:                if ('\0' != *v)
                    983:                        break;
1.2       kristaps  984:        }
                    985:
                    986:        if (i == 0)
                    987:                texierr(p, "path too short");
1.46      kristaps  988:        else if ('\n' != BUF(p)[*pos])
1.2       kristaps  989:                texierr(p, "path too long");
                    990:        else if ('/' == fname[0])
                    991:                texierr(p, "no absolute paths");
                    992:        fname[i] = '\0';
                    993:
                    994:        if (strstr(fname, "../") || strstr(fname, "/.."))
                    995:                texierr(p, "insecure path");
                    996:
1.5       kristaps  997:        for (i = 0; i < p->dirsz; i++) {
                    998:                rc = snprintf(path, sizeof(path),
                    999:                        "%s/%s", p->dirs[i], fname);
                   1000:                if (rc < 0)
                   1001:                        texierr(p, "couldn't format path");
                   1002:                else if ((size_t)rc >= sizeof(path))
                   1003:                        texierr(p, "path too long");
                   1004:                else if (-1 == access(path, R_OK))
                   1005:                        continue;
                   1006:
1.16      kristaps 1007:                parsefile(p, path, 1);
1.5       kristaps 1008:                return;
                   1009:        }
1.2       kristaps 1010:
1.5       kristaps 1011:        texierr(p, "couldn't find %s in includes", fname);
1.2       kristaps 1012: }
                   1013:
                   1014: static void
1.46      kristaps 1015: dobracket(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1016: {
                   1017:
1.53      kristaps 1018:        parsebracket(p, pos, 0);
1.1       kristaps 1019: }
                   1020:
                   1021: static void
1.46      kristaps 1022: dodisplay(struct texi *p, enum texicmd cmd, size_t *pos)
1.3       kristaps 1023: {
                   1024:
1.57      kristaps 1025:        advanceeoln(p, pos, 1);
                   1026:
1.20      kristaps 1027:        switch (cmd) {
                   1028:        case (TEXICMD_FORMAT):
                   1029:        case (TEXICMD_SMALLFORMAT):
                   1030:                teximacro(p, "Bd -filled");
                   1031:                break;
                   1032:        default:
                   1033:                teximacro(p, "Bd -filled -offset indent");
                   1034:                break;
                   1035:        }
                   1036:
1.46      kristaps 1037:        parseto(p, pos, texitoks[cmd].tok);
1.5       kristaps 1038:        teximacro(p, "Ed");
1.62      kristaps 1039:        p->seenvs = 1;
1.3       kristaps 1040: }
                   1041:
                   1042: static void
1.46      kristaps 1043: doexample(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1044: {
                   1045:
1.57      kristaps 1046:        advanceeoln(p, pos, 1);
                   1047:
1.5       kristaps 1048:        teximacro(p, "Bd -literal -offset indent");
1.3       kristaps 1049:        p->literal++;
1.46      kristaps 1050:        parseto(p, pos, texitoks[cmd].tok);
1.3       kristaps 1051:        p->literal--;
1.5       kristaps 1052:        teximacro(p, "Ed");
1.62      kristaps 1053:        p->seenvs = 1;
1.1       kristaps 1054: }
                   1055:
                   1056: static void
1.46      kristaps 1057: dobye(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1058: {
                   1059:
                   1060:        texiexit(p);
                   1061:        exit(EXIT_SUCCESS);
                   1062: }
                   1063:
                   1064: static void
1.46      kristaps 1065: dotitle(struct texi *p, enum texicmd cmd, size_t *pos)
1.10      kristaps 1066: {
1.57      kristaps 1067:        size_t   start;
1.10      kristaps 1068:
1.46      kristaps 1069:        while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1070:                advance(p, pos);
1.57      kristaps 1071:
                   1072:        /* We want to suck down the entire line, inclusive \n. */
                   1073:        start = *pos;
                   1074:        while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) {
                   1075:                if ('@' == BUF(p)[*pos])
                   1076:                        advance(p, pos);
                   1077:                advance(p, pos);
                   1078:        }
                   1079:        if (*pos < BUFSZ(p))
                   1080:                advance(p, pos);
                   1081:
                   1082:        /* Copy this into a buffer. */
1.10      kristaps 1083:        free(p->subtitle);
1.57      kristaps 1084:        if (NULL == (p->subtitle = malloc(*pos - start + 1)))
1.27      kristaps 1085:                texiabort(p, NULL);
1.57      kristaps 1086:        memcpy(p->subtitle, &BUF(p)[start], *pos - start);
                   1087:        p->subtitle[*pos - start] = '\0';
1.10      kristaps 1088: }
                   1089:
                   1090: static void
1.46      kristaps 1091: doaccent(struct texi *p, enum texicmd cmd, size_t *pos)
1.16      kristaps 1092: {
1.42      kristaps 1093:        int      brace = 0;
1.16      kristaps 1094:
1.46      kristaps 1095:        if (*pos == BUFSZ(p)) {
1.42      kristaps 1096:                texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
1.16      kristaps 1097:                return;
1.42      kristaps 1098:        }
                   1099:
                   1100:        /* Pad us with space, if necessary. */
                   1101:        if (p->seenws && p->outcol && 0 == p->literal) {
1.34      kristaps 1102:                texiputchar(p, ' ');
1.42      kristaps 1103:                p->seenws = 0;
                   1104:        }
                   1105:
                   1106:        /*
                   1107:         * If we're braced, then that's that.
                   1108:         * Otherwise, in a special Texinfo case: if we're a non
                   1109:         * alphabetic command of one letter, then the next character is
                   1110:         * the critical one.
                   1111:         * Otherwise, space can sit between us and our argument.
                   1112:         */
1.46      kristaps 1113:        if ('{' == BUF(p)[*pos]) {
1.42      kristaps 1114:                brace = 1;
1.46      kristaps 1115:                advance(p, pos);
1.49      kristaps 1116:        } else if (isalpha((unsigned char)texitoks[cmd].tok[0]))
1.46      kristaps 1117:                while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1118:                        advance(p, pos);
1.42      kristaps 1119:
1.46      kristaps 1120:        if (*pos == BUFSZ(p)) {
1.42      kristaps 1121:                texiwarn(p, "truncated: @%s", texitoks[cmd].tok);
                   1122:                return;
                   1123:        }
                   1124:
                   1125:        switch (cmd) {
1.16      kristaps 1126:        case (TEXICMD_ACUTE):
1.46      kristaps 1127:                switch (BUF(p)[*pos]) {
1.16      kristaps 1128:                case ('a'): case ('A'):
                   1129:                case ('e'): case ('E'):
                   1130:                case ('i'): case ('I'):
                   1131:                case ('o'): case ('O'):
                   1132:                case ('u'): case ('U'):
                   1133:                        texiputchars(p, "\\(\'");
                   1134:                        break;
                   1135:                default:
1.42      kristaps 1136:                        texiwarn(p, "ignoring accent");
                   1137:                        break;
1.16      kristaps 1138:                }
1.46      kristaps 1139:                texiputchar(p, BUF(p)[*pos]);
                   1140:                advance(p, pos);
1.42      kristaps 1141:                break;
                   1142:        case (TEXICMD_CEDILLA):
1.46      kristaps 1143:                if ('c' == BUF(p)[*pos] || 'C' == BUF(p)[*pos])
1.42      kristaps 1144:                        texiputchars(p, "\\(,");
                   1145:                else
                   1146:                        texiwarn(p, "ignoring accent");
1.46      kristaps 1147:                texiputchar(p, BUF(p)[*pos]);
                   1148:                advance(p, pos);
1.16      kristaps 1149:                break;
                   1150:        case (TEXICMD_CIRCUMFLEX):
1.46      kristaps 1151:                switch (BUF(p)[*pos]) {
1.16      kristaps 1152:                case ('a'): case ('A'):
                   1153:                case ('e'): case ('E'):
                   1154:                case ('i'): case ('I'):
                   1155:                case ('o'): case ('O'):
                   1156:                case ('u'): case ('U'):
                   1157:                        texiputchars(p, "\\(^");
                   1158:                        break;
                   1159:                default:
1.42      kristaps 1160:                        texiwarn(p, "ignoring accent");
1.34      kristaps 1161:                        break;
1.16      kristaps 1162:                }
1.46      kristaps 1163:                texiputchar(p, BUF(p)[*pos]);
                   1164:                advance(p, pos);
1.42      kristaps 1165:                break;
                   1166:        case (TEXICMD_DOTLESS):
1.46      kristaps 1167:                if ('i' == BUF(p)[*pos] || 'j' == BUF(p)[*pos])
1.42      kristaps 1168:                        texiputchars(p, "\\(.");
                   1169:                else
                   1170:                        texiwarn(p, "ignoring accent");
1.46      kristaps 1171:                texiputchar(p, BUF(p)[*pos]);
                   1172:                advance(p, pos);
1.16      kristaps 1173:                break;
                   1174:        case (TEXICMD_GRAVE):
1.46      kristaps 1175:                switch (BUF(p)[*pos]) {
1.16      kristaps 1176:                case ('a'): case ('A'):
                   1177:                case ('e'): case ('E'):
                   1178:                case ('i'): case ('I'):
                   1179:                case ('o'): case ('O'):
                   1180:                case ('u'): case ('U'):
                   1181:                        texiputchars(p, "\\(`");
                   1182:                        break;
                   1183:                default:
1.42      kristaps 1184:                        texiwarn(p, "ignoring accent");
                   1185:                        break;
1.16      kristaps 1186:                }
1.46      kristaps 1187:                texiputchar(p, BUF(p)[*pos]);
                   1188:                advance(p, pos);
1.16      kristaps 1189:                break;
                   1190:        case (TEXICMD_TILDE):
1.46      kristaps 1191:                switch (BUF(p)[*pos]) {
1.16      kristaps 1192:                case ('a'): case ('A'):
                   1193:                case ('n'): case ('N'):
                   1194:                case ('o'): case ('O'):
                   1195:                        texiputchars(p, "\\(~");
                   1196:                        break;
                   1197:                default:
1.42      kristaps 1198:                        texiwarn(p, "ignoring accent");
1.34      kristaps 1199:                        break;
1.16      kristaps 1200:                }
1.46      kristaps 1201:                texiputchar(p, BUF(p)[*pos]);
                   1202:                advance(p, pos);
1.16      kristaps 1203:                break;
                   1204:        case (TEXICMD_UMLAUT):
1.46      kristaps 1205:                switch (BUF(p)[*pos]) {
1.16      kristaps 1206:                case ('a'): case ('A'):
                   1207:                case ('e'): case ('E'):
                   1208:                case ('i'): case ('I'):
                   1209:                case ('o'): case ('O'):
                   1210:                case ('u'): case ('U'):
                   1211:                case ('y'):
                   1212:                        texiputchars(p, "\\(:");
                   1213:                        break;
                   1214:                default:
1.42      kristaps 1215:                        texiwarn(p, "ignoring accent");
1.34      kristaps 1216:                        break;
1.16      kristaps 1217:                }
1.46      kristaps 1218:                texiputchar(p, BUF(p)[*pos]);
                   1219:                advance(p, pos);
1.16      kristaps 1220:                break;
                   1221:        default:
1.46      kristaps 1222:                texiputchar(p, BUF(p)[*pos]);
                   1223:                advance(p, pos);
1.42      kristaps 1224:                break;
                   1225:        }
                   1226:
                   1227:        if (brace) {
1.46      kristaps 1228:                while (*pos < BUFSZ(p) && '}' != BUF(p)[*pos]) {
                   1229:                        texiputchar(p, BUF(p)[*pos]);
                   1230:                        advance(p, pos);
1.42      kristaps 1231:                }
1.46      kristaps 1232:                if (*pos < BUFSZ(p))
                   1233:                        advance(p, pos);
1.42      kristaps 1234:        }
                   1235:
                   1236:        switch (cmd) {
                   1237:        case (TEXICMD_TIEACCENT):
                   1238:                texiputchar(p, ']');
                   1239:                break;
                   1240:        case (TEXICMD_DOTACCENT):
                   1241:                texiputchar(p, '*');
                   1242:                break;
                   1243:        default:
                   1244:                break;
1.16      kristaps 1245:        }
                   1246: }
                   1247:
                   1248: static void
1.46      kristaps 1249: dosymbol(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1250: {
                   1251:
1.42      kristaps 1252:        /* Remember to pad us. */
1.43      kristaps 1253:        if (p->seenws && p->outcol && 0 == p->literal)
1.3       kristaps 1254:                texiputchar(p, ' ');
1.43      kristaps 1255:
                   1256:        p->seenws = 0;
1.3       kristaps 1257:
1.1       kristaps 1258:        switch (cmd) {
1.42      kristaps 1259:        case (TEXICMD_AA):
                   1260:                texiputchars(p, "\\(oA");
                   1261:                break;
                   1262:        case (TEXICMD_AASMALL):
                   1263:                texiputchars(p, "\\(oa");
                   1264:                break;
                   1265:        case (TEXICMD_AE):
                   1266:                texiputchars(p, "\\(AE");
                   1267:                break;
                   1268:        case (TEXICMD_AESMALL):
                   1269:                texiputchars(p, "\\(ae");
                   1270:                break;
1.3       kristaps 1271:        case (TEXICMD_ASTERISK):
                   1272:        case (TEXICMD_NEWLINE):
                   1273:        case (TEXICMD_SPACE):
1.18      kristaps 1274:        case (TEXICMD_TABSYM):
1.3       kristaps 1275:                texiputchar(p, ' ');
                   1276:                break;
1.1       kristaps 1277:        case (TEXICMD_AT):
1.3       kristaps 1278:                texiputchar(p, '@');
1.59      kristaps 1279:                break;
                   1280:        case (TEXICMD_BACKSLASH):
                   1281:                texiputchar(p, '\\');
1.3       kristaps 1282:                break;
                   1283:        case (TEXICMD_BANG):
                   1284:                texiputchar(p, '!');
1.7       kristaps 1285:                break;
                   1286:        case (TEXICMD_BULLET):
                   1287:                texiputchars(p, "\\(bu");
1.1       kristaps 1288:                break;
1.35      kristaps 1289:        case (TEXICMD_COMMA):
                   1290:                texiputchar(p, ',');
                   1291:                break;
1.1       kristaps 1292:        case (TEXICMD_COPYRIGHT):
                   1293:                texiputchars(p, "\\(co");
                   1294:                break;
1.42      kristaps 1295:        case (TEXICMD_DH):
                   1296:                texiputchars(p, "\\(-D");
                   1297:                break;
                   1298:        case (TEXICMD_DHSMALL):
                   1299:                texiputchars(p, "\\(Sd");
                   1300:                break;
1.2       kristaps 1301:        case (TEXICMD_DOTS):
1.34      kristaps 1302:        case (TEXICMD_ENDDOTS):
1.2       kristaps 1303:                texiputchars(p, "...");
                   1304:                break;
1.28      kristaps 1305:        case (TEXICMD_EQUIV):
                   1306:                texiputchars(p, "\\(==");
                   1307:                break;
1.15      kristaps 1308:        case (TEXICMD_ERROR):
                   1309:                texiputchars(p, "error\\(->");
1.17      kristaps 1310:                break;
1.43      kristaps 1311:        case (TEXICMD_EURO):
                   1312:                texiputchars(p, "\\(Eu");
                   1313:                break;
1.42      kristaps 1314:        case (TEXICMD_EXCLAMDOWN):
                   1315:                texiputchars(p, "\\(r!");
                   1316:                break;
1.17      kristaps 1317:        case (TEXICMD_EXPANSION):
                   1318:                texiputchars(p, "\\(->");
1.15      kristaps 1319:                break;
1.43      kristaps 1320:        case (TEXICMD_GEQ):
                   1321:                texiputchars(p, "\\(>=");
                   1322:                break;
                   1323:        case (TEXICMD_GUILLEMETLEFT):
                   1324:        case (TEXICMD_GUILLEMOTLEFT):
                   1325:                texiputchars(p, "\\(Fo");
                   1326:                break;
                   1327:        case (TEXICMD_GUILLEMETRIGHT):
                   1328:        case (TEXICMD_GUILLEMOTRIGHT):
                   1329:                texiputchars(p, "\\(Fc");
                   1330:                break;
                   1331:        case (TEXICMD_GUILSINGLLEFT):
                   1332:                texiputchars(p, "\\(fo");
                   1333:                break;
                   1334:        case (TEXICMD_GUILSINGLRIGHT):
                   1335:                texiputchars(p, "\\(fc");
                   1336:                break;
1.42      kristaps 1337:        case (TEXICMD_L):
                   1338:                texiputchars(p, "\\(/L");
                   1339:                break;
1.1       kristaps 1340:        case (TEXICMD_LATEX):
                   1341:                texiputchars(p, "LaTeX");
                   1342:                break;
1.43      kristaps 1343:        case (TEXICMD_LEQ):
                   1344:                texiputchars(p, "\\(<=");
                   1345:                break;
1.42      kristaps 1346:        case (TEXICMD_LSMALL):
                   1347:                texiputchars(p, "\\(/l");
                   1348:                break;
1.25      kristaps 1349:        case (TEXICMD_MINUS):
                   1350:                texiputchars(p, "\\-");
                   1351:                break;
1.42      kristaps 1352:        case (TEXICMD_O):
                   1353:                texiputchars(p, "\\(/O");
                   1354:                break;
                   1355:        case (TEXICMD_OE):
                   1356:                texiputchars(p, "\\(OE");
                   1357:                break;
                   1358:        case (TEXICMD_OESMALL):
                   1359:                texiputchars(p, "\\(oe");
                   1360:                break;
                   1361:        case (TEXICMD_ORDF):
                   1362:                texiputchars(p, "a");
                   1363:                break;
                   1364:        case (TEXICMD_ORDM):
                   1365:                texiputchars(p, "o");
                   1366:                break;
                   1367:        case (TEXICMD_OSMALL):
                   1368:                texiputchars(p, "\\(/o");
                   1369:                break;
1.25      kristaps 1370:        case (TEXICMD_PERIOD):
                   1371:                texiputchar(p, '.');
                   1372:                break;
1.43      kristaps 1373:        case (TEXICMD_POUNDS):
                   1374:                texiputchars(p, "\\(Po");
                   1375:                break;
1.42      kristaps 1376:        case (TEXICMD_QUESTIONDOWN):
                   1377:                texiputchars(p, "\\(r?");
                   1378:                break;
1.3       kristaps 1379:        case (TEXICMD_QUESTIONMARK):
                   1380:                texiputchar(p, '?');
1.15      kristaps 1381:                break;
1.43      kristaps 1382:        case (TEXICMD_QUOTEDBLBASE):
                   1383:                texiputchars(p, "\\(Bq");
                   1384:                break;
                   1385:        case (TEXICMD_QUOTEDBLLEFT):
                   1386:                texiputchars(p, "\\(lq");
                   1387:                break;
                   1388:        case (TEXICMD_QUOTEDBLRIGHT):
                   1389:                texiputchars(p, "\\(rq");
                   1390:                break;
                   1391:        case (TEXICMD_QUOTESINGLBASE):
                   1392:                texiputchars(p, "\\(bq");
                   1393:                break;
                   1394:        case (TEXICMD_QUOTELEFT):
                   1395:                texiputchars(p, "\\(oq");
                   1396:                break;
                   1397:        case (TEXICMD_QUOTERIGHT):
                   1398:                texiputchars(p, "\\(cq");
                   1399:                break;
                   1400:        case (TEXICMD_REGISTEREDSYMBOL):
                   1401:                texiputchars(p, "\\(rg");
                   1402:                break;
1.15      kristaps 1403:        case (TEXICMD_RESULT):
                   1404:                texiputchars(p, "\\(rA");
1.3       kristaps 1405:                break;
1.34      kristaps 1406:        case (TEXICMD_SLASH):
                   1407:                texiputchar(p, '/');
                   1408:                break;
1.35      kristaps 1409:        case (TEXICMD_SS):
                   1410:                texiputchars(p, "\\(ss");
                   1411:                break;
1.3       kristaps 1412:        case (TEXICMD_SQUIGGLE_LEFT):
                   1413:                texiputchars(p, "{");
                   1414:                break;
                   1415:        case (TEXICMD_SQUIGGLE_RIGHT):
                   1416:                texiputchars(p, "}");
                   1417:                break;
1.38      kristaps 1418:        case (TEXICMD_TEXSYM):
1.35      kristaps 1419:                texiputchars(p, "TeX");
1.43      kristaps 1420:                break;
                   1421:        case (TEXICMD_TEXTDEGREE):
                   1422:                texiputchars(p, "\\(de");
1.35      kristaps 1423:                break;
1.42      kristaps 1424:        case (TEXICMD_TH):
                   1425:                texiputchars(p, "\\(TP");
                   1426:                break;
                   1427:        case (TEXICMD_THSMALL):
                   1428:                texiputchars(p, "\\(Tp");
                   1429:                break;
1.38      kristaps 1430:        case (TEXICMD_TIE):
1.35      kristaps 1431:                texiputchars(p, "\\ ");
1.1       kristaps 1432:                break;
1.3       kristaps 1433:        case (TEXICMD_COLON):
                   1434:        case (TEXICMD_HYPHEN):
                   1435:                break;
1.1       kristaps 1436:        default:
                   1437:                abort();
                   1438:        }
                   1439:
1.42      kristaps 1440:        /* Alphabetic commands have braces we ignore. */
1.49      kristaps 1441:        if (isalpha((unsigned char)texitoks[cmd].tok[0]))
1.46      kristaps 1442:                doignbracket(p, cmd, pos);
1.1       kristaps 1443: }
                   1444:
                   1445: static void
1.46      kristaps 1446: doquotation(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1447: {
                   1448:
1.5       kristaps 1449:        teximacro(p, "Qo");
1.46      kristaps 1450:        parseto(p, pos, "quotation");
1.5       kristaps 1451:        teximacro(p, "Qc");
1.1       kristaps 1452: }
                   1453:
1.3       kristaps 1454: static void
1.46      kristaps 1455: domath(struct texi *p, enum texicmd cmd, size_t *pos)
1.3       kristaps 1456: {
                   1457:
1.53      kristaps 1458:        parsebracket(p, pos, 1);
1.24      kristaps 1459: }
                   1460:
                   1461: static void
1.46      kristaps 1462: dovalue(struct texi *p, enum texicmd cmd, size_t *pos)
1.24      kristaps 1463: {
1.46      kristaps 1464:        size_t           start, end;
1.25      kristaps 1465:        char            *key, *val;
                   1466:        const char      *cp;
1.24      kristaps 1467:
                   1468:        if (TEXICMD_SET == cmd) {
1.46      kristaps 1469:                while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1470:                        advance(p, pos);
                   1471:                for (start = end = *pos; end < BUFSZ(p); end++)
                   1472:                        if (ismspace(BUF(p)[end]))
1.24      kristaps 1473:                                break;
1.25      kristaps 1474:                /* We don't allow empty keys. */
1.24      kristaps 1475:                if (start == end)
                   1476:                        return;
1.46      kristaps 1477:                advanceto(p, pos, end);
1.24      kristaps 1478:
                   1479:                key = malloc(end - start + 1);
1.27      kristaps 1480:                if (NULL == key)
                   1481:                        texiabort(p, NULL);
1.46      kristaps 1482:                memcpy(key, &BUF(p)[start], end - start);
1.24      kristaps 1483:                key[end - start] = '\0';
                   1484:
1.46      kristaps 1485:                while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1486:                        advance(p, pos);
                   1487:                for (start = end = *pos; end < BUFSZ(p); end++)
                   1488:                        if ('\n' == BUF(p)[end])
1.24      kristaps 1489:                                break;
1.25      kristaps 1490:                /* We do allow empty values. */
1.46      kristaps 1491:                advanceeoln(p, pos, 1);
1.24      kristaps 1492:
                   1493:                val = malloc(end - start + 1);
1.27      kristaps 1494:                if (NULL == val)
                   1495:                        texiabort(p, NULL);
1.46      kristaps 1496:                memcpy(val, &BUF(p)[start], end - start);
1.24      kristaps 1497:                val[end - start] = '\0';
1.25      kristaps 1498:                valueadd(p, key, val);
1.24      kristaps 1499:        } else if (TEXICMD_VALUE == cmd) {
                   1500:                if (p->seenws)
                   1501:                        texiputchar(p, ' ');
                   1502:                p->seenws = 0;
1.46      kristaps 1503:                if (NULL != (cp = valueblookup(p, pos)))
1.56      kristaps 1504:                        texisplice(p, cp, strlen(cp), *pos);
1.46      kristaps 1505:                else
1.25      kristaps 1506:                        texiputchars(p, "{No value}");
1.24      kristaps 1507:        } else if (TEXICMD_IFCLEAR == cmd) {
1.46      kristaps 1508:                if (NULL != valuellookup(p, pos))
                   1509:                        doignblock(p, cmd, pos);
1.26      kristaps 1510:                else
1.46      kristaps 1511:                        parseto(p, pos, texitoks[cmd].tok);
1.30      kristaps 1512:        } else if (TEXICMD_IFSET == cmd) {
1.46      kristaps 1513:                if (NULL == valuellookup(p, pos))
                   1514:                        doignblock(p, cmd,  pos);
1.30      kristaps 1515:                else
1.46      kristaps 1516:                        parseto(p, pos, texitoks[cmd].tok);
1.25      kristaps 1517:        } else if (TEXICMD_CLEAR == cmd)
1.46      kristaps 1518:                valuelclear(p, pos);
1.3       kristaps 1519: }
                   1520:
1.1       kristaps 1521: static void
1.46      kristaps 1522: dolink(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1523: {
1.8       kristaps 1524:        int      c;
1.1       kristaps 1525:
                   1526:        switch (cmd) {
                   1527:        case (TEXICMD_EMAIL):
1.5       kristaps 1528:                teximacroopen(p, "Mt");
1.1       kristaps 1529:                break;
1.3       kristaps 1530:        case (TEXICMD_UREF):
1.1       kristaps 1531:        case (TEXICMD_URL):
1.44      kristaps 1532:        case (TEXICMD_INDICATEURL):
1.5       kristaps 1533:                teximacroopen(p, "Lk");
1.1       kristaps 1534:                break;
1.8       kristaps 1535:        case (TEXICMD_XREF):
                   1536:                texiputchars(p, "See Section");
1.39      kristaps 1537:                teximacroopen(p, "Dq");
1.8       kristaps 1538:                break;
                   1539:        case (TEXICMD_PXREF):
                   1540:                texiputchars(p, "see Section");
1.39      kristaps 1541:                teximacroopen(p, "Dq");
1.8       kristaps 1542:                break;
1.34      kristaps 1543:        case (TEXICMD_INFOREF):
                   1544:                texiputchars(p, "See Info file node");
1.39      kristaps 1545:                teximacroopen(p, "Dq");
1.34      kristaps 1546:                break;
1.1       kristaps 1547:        default:
1.8       kristaps 1548:                abort();
1.1       kristaps 1549:        }
1.8       kristaps 1550:
1.46      kristaps 1551:        c = parsearg(p, pos, 0);
1.8       kristaps 1552:        p->ign++;
                   1553:        while (c > 0)
1.46      kristaps 1554:                c = parsearg(p, pos, 1);
1.8       kristaps 1555:        p->ign--;
                   1556:
1.46      kristaps 1557:        texipunctuate(p, pos);
1.8       kristaps 1558:        teximacroclose(p);
                   1559: }
                   1560:
                   1561: static void
1.46      kristaps 1562: doignargn(struct texi *p, enum texicmd cmd, size_t *pos)
1.8       kristaps 1563: {
                   1564:        int      c;
                   1565:
1.46      kristaps 1566:        c = parsearg(p, pos, 0);
1.8       kristaps 1567:        p->ign++;
                   1568:        while (c > 0)
1.46      kristaps 1569:                c = parsearg(p, pos, 1);
1.8       kristaps 1570:        p->ign--;
1.1       kristaps 1571: }
                   1572:
1.23      kristaps 1573: /*
                   1574:  * Sections can be made subsections and so on by way of the
                   1575:  * @raiseections and @lowersections commands.
                   1576:  * Perform this check here and return the actual section number adjusted
                   1577:  * to the raise level.
                   1578:  */
                   1579: static int
                   1580: sectioner(struct texi *p, int sec)
                   1581: {
                   1582:
                   1583:        if ((sec -= p->secoffs) < 0) {
                   1584:                texiwarn(p, "section below minimum, clamping");
                   1585:                return(0);
                   1586:        } else if (sec >= SECTSZ) {
                   1587:                texiwarn(p, "section above maximum, clamping");
                   1588:                return(SECTSZ - 1);
                   1589:        }
                   1590:        return(sec);
                   1591: }
                   1592:
                   1593: static void
1.46      kristaps 1594: dosubsubsection(struct texi *p, enum texicmd cmd, size_t *pos)
1.23      kristaps 1595: {
                   1596:        int      sec;
                   1597:
                   1598:        sec = sectioner(p, 3);
                   1599:
                   1600:        /* We don't have a subsubsubsection, so make one up. */
                   1601:        texivspace(p);
                   1602:        teximacroopen(p, sects[sec]);
1.46      kristaps 1603:        parseeoln(p, pos);
1.23      kristaps 1604:        teximacroclose(p);
                   1605:        texivspace(p);
                   1606: }
                   1607:
1.1       kristaps 1608: static void
1.46      kristaps 1609: dosubsection(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1610: {
1.23      kristaps 1611:        int      sec;
                   1612:
                   1613:        sec = sectioner(p, 2);
1.13      kristaps 1614:
                   1615:        if (p->outmacro)
1.23      kristaps 1616:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.13      kristaps 1617:        else if (p->literal)
1.23      kristaps 1618:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.1       kristaps 1619:
1.21      kristaps 1620:        /* We don't have a subsubsection, so make one up. */
1.34      kristaps 1621:        if (sec > 1)
                   1622:                texivspace(p);
1.23      kristaps 1623:        teximacroopen(p, sects[sec]);
1.46      kristaps 1624:        parseeoln(p, pos);
1.5       kristaps 1625:        teximacroclose(p);
1.34      kristaps 1626:        if (sec > 1)
                   1627:                texivspace(p);
1.1       kristaps 1628: }
                   1629:
                   1630: static void
1.46      kristaps 1631: dosecoffs(struct texi *p, enum texicmd cmd, size_t *pos)
1.23      kristaps 1632: {
                   1633:
                   1634:        if (TEXICMD_RAISESECTIONS == cmd)
                   1635:                p->secoffs++;
                   1636:        else
                   1637:                p->secoffs--;
                   1638: }
                   1639:
                   1640: static void
1.46      kristaps 1641: dosection(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1642: {
1.23      kristaps 1643:        int              sec;
1.12      kristaps 1644:
                   1645:        switch (cmd) {
1.55      kristaps 1646:        case (TEXICMD_TOP):
                   1647:                sec = 0;
                   1648:                break;
1.12      kristaps 1649:        case (TEXICMD_APPENDIX):
                   1650:        case (TEXICMD_CHAPTER):
                   1651:        case (TEXICMD_UNNUMBERED):
1.23      kristaps 1652:                sec = sectioner(p, 0);
1.12      kristaps 1653:                break;
                   1654:        case (TEXICMD_APPENDIXSEC):
                   1655:        case (TEXICMD_HEADING):
                   1656:        case (TEXICMD_SECTION):
                   1657:        case (TEXICMD_UNNUMBEREDSEC):
1.23      kristaps 1658:                sec = sectioner(p, 1);
1.12      kristaps 1659:                break;
                   1660:        default:
                   1661:                abort();
                   1662:        }
1.1       kristaps 1663:
1.3       kristaps 1664:        if (p->outmacro)
1.23      kristaps 1665:                texierr(p, "\"%s\" in open line scope!?", sects[sec]);
1.3       kristaps 1666:        else if (p->literal)
1.23      kristaps 1667:                texierr(p, "\"%s\" in a literal scope!?", sects[sec]);
1.3       kristaps 1668:
1.55      kristaps 1669:        if (0 == sec && NULL != p->chapters) {
                   1670:                teximdocclose(p, 0);
1.56      kristaps 1671:                teximdocopen(p, pos);
1.55      kristaps 1672:        }
                   1673:
1.23      kristaps 1674:        teximacroopen(p, sects[sec]);
1.46      kristaps 1675:        parseeoln(p, pos);
1.3       kristaps 1676:        teximacroclose(p);
                   1677: }
                   1678:
                   1679: static void
1.55      kristaps 1680: dotop(struct texi *p, enum texicmd cmd, size_t *pos)
                   1681: {
                   1682:
                   1683:        if (--p->ign)
                   1684:                texierr(p, "@top command while ignoring");
                   1685:
                   1686:        if (NULL == p->chapters)
1.56      kristaps 1687:                teximdocopen(p, pos);
1.55      kristaps 1688:        dosection(p, cmd, pos);
                   1689: }
                   1690:
                   1691: static void
1.46      kristaps 1692: dosp(struct texi *p, enum texicmd cmd, size_t *pos)
1.3       kristaps 1693: {
                   1694:
1.57      kristaps 1695:        advanceeoln(p, pos, 1);
1.37      kristaps 1696:        if (p->literal)
                   1697:                texiputchar(p, '\n');
                   1698:        else
                   1699:                texivspace(p);
1.1       kristaps 1700: }
                   1701:
                   1702: static void
1.46      kristaps 1703: doitem(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1704: {
                   1705:
1.18      kristaps 1706:        /* Multitable is using raw tbl(7). */
                   1707:        if (TEXILIST_TABLE == p->list) {
                   1708:                texiputchar(p, '\n');
                   1709:                return;
                   1710:        }
                   1711:
1.3       kristaps 1712:        if (p->outmacro)
                   1713:                texierr(p, "item in open line scope!?");
                   1714:        else if (p->literal)
                   1715:                texierr(p, "item in a literal scope!?");
                   1716:
                   1717:        switch (p->list) {
                   1718:        case (TEXILIST_ITEM):
1.5       kristaps 1719:                teximacroopen(p, "It");
1.3       kristaps 1720:                break;
                   1721:        case (TEXILIST_NOITEM):
1.5       kristaps 1722:                teximacro(p, "It");
1.3       kristaps 1723:                break;
                   1724:        default:
1.11      kristaps 1725:                texivspace(p);
1.3       kristaps 1726:                break;
                   1727:        }
1.18      kristaps 1728:
                   1729:        /* Trick so we don't start with Pp. */
1.46      kristaps 1730:        parseeoln(p, pos);
1.1       kristaps 1731:
1.3       kristaps 1732:        if (TEXILIST_ITEM == p->list)
                   1733:                teximacroclose(p);
1.9       kristaps 1734:        else if (p->outcol > 0)
1.1       kristaps 1735:                texiputchar(p, '\n');
1.18      kristaps 1736: }
                   1737:
                   1738: static void
1.46      kristaps 1739: dotab(struct texi *p, enum texicmd cmd, size_t *pos)
1.18      kristaps 1740: {
                   1741:
                   1742:        /* This command is only useful in @multitable. */
                   1743:        if (TEXILIST_TABLE == p->list)
                   1744:                texiputchar(p, '\t');
                   1745: }
                   1746:
                   1747: static void
1.46      kristaps 1748: domultitable(struct texi *p, enum texicmd cmd, size_t *pos)
1.18      kristaps 1749: {
                   1750:        enum texilist   sv = p->list;
1.32      kristaps 1751:        int             svliteral = p->literal;
1.18      kristaps 1752:        enum texicmd    type;
                   1753:        size_t          i, end, columns;
                   1754:
                   1755:        p->list = TEXILIST_TABLE;
1.32      kristaps 1756:        /*
                   1757:         * TS/TE blocks aren't "in mdoc(7)", so we can disregard the
                   1758:         * fact that we're in literal mode right now.
                   1759:         */
                   1760:        p->literal = 0;
1.18      kristaps 1761:        teximacro(p, "TS");
                   1762:        columns = 0;
                   1763:
                   1764:        /* Advance to the first argument... */
1.46      kristaps 1765:        while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1766:                advance(p, pos);
1.18      kristaps 1767:
                   1768:        /* Make sure we don't print anything when scanning. */
                   1769:        p->ign++;
1.57      kristaps 1770:        if (*pos < BUFSZ(p) && '@' == BUF(p)[*pos]) {
1.18      kristaps 1771:                /*
                   1772:                 * Look for @columnfractions.
                   1773:                 * We ignore these, but we do use the number of
                   1774:                 * arguments to set the number of columns that we'll
                   1775:                 * have.
                   1776:                 */
1.46      kristaps 1777:                type = texicmd(p, *pos, &end, NULL);
                   1778:                advanceto(p, pos, end);
1.18      kristaps 1779:                if (TEXICMD_COLUMNFRACTIONS != type)
1.25      kristaps 1780:                        texierr(p, "unknown multitable command");
1.46      kristaps 1781:                while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos]) {
                   1782:                        while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1783:                                advance(p, pos);
                   1784:                        while (*pos < BUFSZ(p) && ! isws(BUF(p)[*pos])) {
                   1785:                                if ('\n' == BUF(p)[*pos])
1.18      kristaps 1786:                                        break;
1.46      kristaps 1787:                                advance(p, pos);
1.18      kristaps 1788:                        }
                   1789:                        columns++;
                   1790:                }
                   1791:        } else
                   1792:                /*
                   1793:                 * We have arguments.
                   1794:                 * We could parse these, but it's easier to just let
                   1795:                 * tbl(7) figure it out.
                   1796:                 * So use this only to count arguments.
                   1797:                 */
1.46      kristaps 1798:                while (parselinearg(p, pos) > 0)
1.18      kristaps 1799:                        columns++;
                   1800:        p->ign--;
                   1801:
                   1802:        /* Left-justify each table entry. */
                   1803:        for (i = 0; i < columns; i++) {
                   1804:                if (i > 0)
                   1805:                        texiputchar(p, ' ');
                   1806:                texiputchar(p, 'l');
                   1807:        }
                   1808:        texiputchars(p, ".\n");
                   1809:        p->outmacro++;
1.46      kristaps 1810:        parseto(p, pos, texitoks[cmd].tok);
1.18      kristaps 1811:        p->outmacro--;
                   1812:        teximacro(p, "TE");
1.32      kristaps 1813:        p->literal = svliteral;
1.18      kristaps 1814:        p->list = sv;
1.1       kristaps 1815: }
                   1816:
                   1817: static void
1.46      kristaps 1818: dotable(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1819: {
1.3       kristaps 1820:        enum texilist   sv = p->list;
                   1821:
1.57      kristaps 1822:        advanceeoln(p, pos, 1);
                   1823:
1.3       kristaps 1824:        p->list = TEXILIST_ITEM;
1.5       kristaps 1825:        teximacro(p, "Bl -tag -width Ds");
1.46      kristaps 1826:        parseto(p, pos, texitoks[cmd].tok);
1.5       kristaps 1827:        teximacro(p, "El");
1.62      kristaps 1828:        p->seenvs = 1;
1.3       kristaps 1829:        p->list = sv;
1.44      kristaps 1830: }
                   1831:
                   1832: static void
1.46      kristaps 1833: doend(struct texi *p, enum texicmd cmd, size_t *pos)
1.44      kristaps 1834: {
                   1835:        size_t   start;
                   1836:
1.46      kristaps 1837:        while (*pos < BUFSZ(p) && isws(BUF(p)[*pos]))
                   1838:                advance(p, pos);
1.44      kristaps 1839:        start = *pos;
1.46      kristaps 1840:        while (*pos < BUFSZ(p) && '\n' != BUF(p)[*pos])
                   1841:                advance(p, pos);
1.44      kristaps 1842:
1.56      kristaps 1843:        texiwarn(p, "unexpected \"end\": %.*s",
                   1844:                (int)(*pos - start), &BUF(p)[start]);
1.46      kristaps 1845:        advanceeoln(p, pos, 1);
1.1       kristaps 1846: }
                   1847:
                   1848: static void
1.46      kristaps 1849: doenumerate(struct texi *p, enum texicmd cmd, size_t *pos)
1.2       kristaps 1850: {
1.3       kristaps 1851:        enum texilist    sv = p->list;
1.2       kristaps 1852:
1.57      kristaps 1853:        advanceeoln(p, pos, 1);
                   1854:
1.3       kristaps 1855:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1856:        teximacro(p, "Bl -enum");
1.57      kristaps 1857:        parseto(p, pos, texitoks[cmd].tok);
1.5       kristaps 1858:        teximacro(p, "El");
1.62      kristaps 1859:        p->seenvs = 1;
1.3       kristaps 1860:        p->list = sv;
1.2       kristaps 1861: }
                   1862:
                   1863: static void
1.46      kristaps 1864: doitemize(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1865: {
1.3       kristaps 1866:        enum texilist   sv = p->list;
1.1       kristaps 1867:
1.57      kristaps 1868:        advanceeoln(p, pos, 1);
                   1869:
1.21      kristaps 1870:        p->list = TEXILIST_NOITEM;
1.5       kristaps 1871:        teximacro(p, "Bl -bullet");
1.57      kristaps 1872:        parseto(p, pos, texitoks[cmd].tok);
1.5       kristaps 1873:        teximacro(p, "El");
1.62      kristaps 1874:        p->seenvs = 1;
1.3       kristaps 1875:        p->list = sv;
1.1       kristaps 1876: }
                   1877:
                   1878: static void
1.46      kristaps 1879: doignbracket(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1880: {
                   1881:
1.3       kristaps 1882:        p->ign++;
1.53      kristaps 1883:        parsebracket(p, pos, 0);
1.3       kristaps 1884:        p->ign--;
1.1       kristaps 1885: }
                   1886:
                   1887: static void
1.46      kristaps 1888: doignline(struct texi *p, enum texicmd cmd, size_t *pos)
1.1       kristaps 1889: {
                   1890:
1.57      kristaps 1891:        advanceeoln(p, pos, 1);
1.1       kristaps 1892: }
                   1893:
1.8       kristaps 1894: /*
                   1895:  * Parse colon-separated directories from "cp" (if not NULL) and returns
                   1896:  * the array of pointers.
1.40      kristaps 1897:  * Prepends "base" to the array, if found.
1.8       kristaps 1898:  * This does NOT sanitise the directories!
                   1899:  */
1.5       kristaps 1900: static char **
1.27      kristaps 1901: parsedirs(struct texi *p, const char *base, const char *cp, size_t *sz)
1.5       kristaps 1902: {
                   1903:        char             *tok, *str, *tofree;
                   1904:        const char       *cpp;
1.40      kristaps 1905:        size_t            i = 0;
1.5       kristaps 1906:        char            **dirs;
                   1907:
1.40      kristaps 1908:        /* Count up our expected arguments. */
                   1909:        *sz = NULL != base;
                   1910:        if (NULL != (cpp = cp))
                   1911:                for ((*sz)++; NULL != (cpp = strchr(cpp, ':')); (*sz)++)
1.5       kristaps 1912:                        cpp++;
                   1913:
1.40      kristaps 1914:        if (0 == *sz)
                   1915:                return(NULL);
1.27      kristaps 1916:        if (NULL == (dirs = calloc(*sz, sizeof(char *))))
                   1917:                texiabort(p, NULL);
1.40      kristaps 1918:        if (NULL != base && NULL == (dirs[i++] = strdup(base)))
1.27      kristaps 1919:                texiabort(p, NULL);
1.5       kristaps 1920:        if (NULL == cp)
                   1921:                return(dirs);
1.27      kristaps 1922:        if (NULL == (tofree = tok = str = strdup(cp)))
                   1923:                texiabort(p, NULL);
1.5       kristaps 1924:
1.40      kristaps 1925:        for ( ; NULL != (tok = strsep(&str, ":")); i++)
1.27      kristaps 1926:                if (NULL == (dirs[i] = strdup(tok)))
                   1927:                        texiabort(p, NULL);
1.5       kristaps 1928:
                   1929:        free(tofree);
                   1930:        return(dirs);
                   1931: }
                   1932:
1.1       kristaps 1933: int
                   1934: main(int argc, char *argv[])
                   1935: {
                   1936:        struct texi      texi;
1.2       kristaps 1937:        int              c;
1.37      kristaps 1938:        char            *dirpath, *dir, *ccp;
1.10      kristaps 1939:        const char      *progname, *Idir, *cp;
1.1       kristaps 1940:
                   1941:        progname = strrchr(argv[0], '/');
                   1942:        if (progname == NULL)
                   1943:                progname = argv[0];
                   1944:        else
                   1945:                ++progname;
                   1946:
1.10      kristaps 1947:        memset(&texi, 0, sizeof(struct texi));
1.40      kristaps 1948:        texi.ign = 1;
1.55      kristaps 1949:        texi.outfile = stdout;
1.5       kristaps 1950:        Idir = NULL;
1.10      kristaps 1951:
1.55      kristaps 1952:        while (-1 != (c = getopt(argc, argv, "C:I:")))
1.1       kristaps 1953:                switch (c) {
1.55      kristaps 1954:                case ('C'):
                   1955:                        texi.chapters = optarg;
                   1956:                        break;
1.5       kristaps 1957:                case ('I'):
                   1958:                        Idir = optarg;
                   1959:                        break;
1.1       kristaps 1960:                default:
                   1961:                        goto usage;
                   1962:                }
                   1963:
                   1964:        argv += optind;
1.40      kristaps 1965:        argc -= optind;
1.1       kristaps 1966:
1.40      kristaps 1967:        if (argc > 0) {
                   1968:                if (NULL == (dirpath = strdup(argv[0])))
                   1969:                        texiabort(&texi, NULL);
                   1970:                if (NULL == (dir = dirname(dirpath)))
                   1971:                        texiabort(&texi, NULL);
                   1972:                if (NULL != (cp = strrchr(argv[0], '/')))
                   1973:                        texi.title = strdup(cp + 1);
                   1974:                else
                   1975:                        texi.title = strdup(argv[0]);
                   1976:                if (NULL == texi.title)
                   1977:                        texiabort(&texi, NULL);
                   1978:                else if (NULL != (ccp = strchr(texi.title, '.')))
                   1979:                        *ccp = '\0';
                   1980:                texi.dirs = parsedirs(&texi, dir, Idir, &texi.dirsz);
                   1981:                free(dirpath);
                   1982:                parsefile(&texi, argv[0], 1);
                   1983:        } else {
                   1984:                texi.title = strdup("Unknown Manual");
                   1985:                texi.dirs = parsedirs(&texi, NULL, Idir, &texi.dirsz);
                   1986:                parsestdin(&texi);
                   1987:        }
1.27      kristaps 1988:
1.2       kristaps 1989:        texiexit(&texi);
1.56      kristaps 1990:        exit(EXIT_SUCCESS);
1.1       kristaps 1991: usage:
1.55      kristaps 1992:        fprintf(stderr, "usage: %s [-Cdir] [-Idirs] [file]\n", progname);
1.1       kristaps 1993:        return(EXIT_FAILURE);
                   1994: }

CVSweb