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

Annotation of texi2mdoc/main.c, Revision 1.54

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

CVSweb