1 /*	$Id: extern.h,v 1.16 2015/02/25 14:49:14 kristaps Exp $ */
2 /*
3  * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef EXTERN_H
18 #define EXTERN_H
19 
20 /*
21  * This defines each one of the Texinfo commands that we understand.
22  * Obviously this only refers to native commands; overriden names are a
23  * different story.
24  */
25 enum	texicmd {
26 	TEXICMD_A4PAPER,
27 	TEXICMD_AA,
28 	TEXICMD_AASMALL,
29 	TEXICMD_ACRONYM,
30 	TEXICMD_ACUTE,
31 	TEXICMD_AE,
32 	TEXICMD_AESMALL,
33 	TEXICMD_ANCHOR,
34 	TEXICMD_APPENDIX,
35 	TEXICMD_APPENDIXSEC,
36 	TEXICMD_APPENDIXSUBSEC,
37 	TEXICMD_APPENDIXSUBSUBSEC,
38 	TEXICMD_ASIS,
39 	TEXICMD_ASTERISK,
40 	TEXICMD_AT,
41 	TEXICMD_AUTHOR,
42 	TEXICMD_B,
43 	TEXICMD_BANG,
44 	TEXICMD_BULLET,
45 	TEXICMD_BYE,
46 	TEXICMD_CARTOUCHE,
47 	TEXICMD_CEDILLA,
48 	TEXICMD_CENTER,
49 	TEXICMD_CHAPTER,
50 	TEXICMD_CINDEX,
51 	TEXICMD_CIRCUMFLEX,
52 	TEXICMD_CITE,
53 	TEXICMD_CLEAR,
54 	TEXICMD_CODE,
55 	TEXICMD_COLON,
56 	TEXICMD_COLUMNFRACTIONS,
57 	TEXICMD_COMMA,
58 	TEXICMD_COMMAND,
59 	TEXICMD_COMMENT,
60 	TEXICMD_COMMENT_LONG,
61 	TEXICMD_CONTENTS,
62 	TEXICMD_COPYING,
63 	TEXICMD_COPYRIGHT,
64 	TEXICMD_DEFCODEINDEX,
65 	TEXICMD_DEFFN,
66 	TEXICMD_DEFFNX,
67 	TEXICMD_DEFINDEX,
68 	TEXICMD_DEFMAC,
69 	TEXICMD_DEFMACX,
70 	TEXICMD_DEFTP,
71 	TEXICMD_DEFTPX,
72 	TEXICMD_DEFTYPEFN,
73 	TEXICMD_DEFTYPEFNX,
74 	TEXICMD_DEFTYPEFUN,
75 	TEXICMD_DEFTYPEFUNX,
76 	TEXICMD_DEFTYPEMETHOD,
77 	TEXICMD_DEFTYPEMETHODX,
78 	TEXICMD_DEFTYPEVAR,
79 	TEXICMD_DEFTYPEVARX,
80 	TEXICMD_DEFTYPEVR,
81 	TEXICMD_DEFTYPEVRX,
82 	TEXICMD_DEFUN,
83 	TEXICMD_DEFUNX,
84 	TEXICMD_DEFVAR,
85 	TEXICMD_DEFVARX,
86 	TEXICMD_DEFVR,
87 	TEXICMD_DEFVRX,
88 	TEXICMD_DETAILMENU,
89 	TEXICMD_DFN,
90 	TEXICMD_DH,
91 	TEXICMD_DHSMALL,
92 	TEXICMD_DIRCATEGORY,
93 	TEXICMD_DIRENTRY,
94 	TEXICMD_DISPLAY,
95 	TEXICMD_DMN,
96 	TEXICMD_DOCUMENTDESCRPITION,
97 	TEXICMD_DOCUMENTENCODING,
98 	TEXICMD_DOCUMENTLANGUAGE,
99 	TEXICMD_DOTACCENT,
100 	TEXICMD_DOTLESS,
101 	TEXICMD_DOTS,
102 	TEXICMD_EMAIL,
103 	TEXICMD_EMPH,
104 	TEXICMD_END,
105 	TEXICMD_ENDDOTS,
106 	TEXICMD_ENUMERATE,
107 	TEXICMD_ENV,
108 	TEXICMD_EQUIV,
109 	TEXICMD_ERROR,
110 	TEXICMD_EURO,
111 	TEXICMD_EXAMPLE,
112 	TEXICMD_EXCLAMDOWN,
113 	TEXICMD_EXDENT,
114 	TEXICMD_EXPANSION,
115 	TEXICMD_FILE,
116 	TEXICMD_FINALOUT,
117 	TEXICMD_FINDEX,
118 	TEXICMD_FLUSHLEFT,
119 	TEXICMD_FLUSHRIGHT,
120 	TEXICMD_FIRSTPARAGRAPHINDENT,
121 	TEXICMD_FOOTNOTE,
122 	TEXICMD_FOOTNOTESTYLE,
123 	TEXICMD_FTABLE,
124 	TEXICMD_FORMAT,
125 	TEXICMD_GEQ,
126 	TEXICMD_GRAVE,
127 	TEXICMD_GROUP,
128 	TEXICMD_GUILLEMETLEFT,
129 	TEXICMD_GUILLEMETRIGHT,
130 	TEXICMD_GUILLEMOTLEFT,
131 	TEXICMD_GUILLEMOTRIGHT,
132 	TEXICMD_GUILSINGLLEFT,
133 	TEXICMD_GUILSINGLRIGHT,
134 	TEXICMD_H,
135 	TEXICMD_HEADING,
136 	TEXICMD_HEADINGS,
137 	TEXICMD_HEADITEM,
138 	TEXICMD_HTML,
139 	TEXICMD_HYPHEN,
140 	TEXICMD_I,
141 	TEXICMD_IFCLEAR,
142 	TEXICMD_IFDOCBOOK,
143 	TEXICMD_IFHTML,
144 	TEXICMD_IFINFO,
145 	TEXICMD_IFNOTDOCBOOK,
146 	TEXICMD_IFNOTHTML,
147 	TEXICMD_IFNOTINFO,
148 	TEXICMD_IFNOTPLAINTEXT,
149 	TEXICMD_IFNOTTEX,
150 	TEXICMD_IFNOTXML,
151 	TEXICMD_IFPLAINTEXT,
152 	TEXICMD_IFTEX,
153 	TEXICMD_IFSET,
154 	TEXICMD_IFXML,
155 	TEXICMD_IGNORE,
156 	TEXICMD_IMAGE,
157 	TEXICMD_INCLUDE,
158 	TEXICMD_INDENTBLOCK,
159 	TEXICMD_INDICATEURL,
160 	TEXICMD_INFOREF,
161 	TEXICMD_INSERTCOPYING,
162 	TEXICMD_ITEM,
163 	TEXICMD_ITEMIZE,
164 	TEXICMD_ITEMX,
165 	TEXICMD_KBD,
166 	TEXICMD_KEY,
167 	TEXICMD_KINDEX,
168 	TEXICMD_L,
169 	TEXICMD_LATEX,
170 	TEXICMD_LEQ,
171 	TEXICMD_LOWERSECTIONS,
172 	TEXICMD_LSMALL,
173 	TEXICMD_MACRO,
174 	TEXICMD_MACRON,
175 	TEXICMD_MATH,
176 	TEXICMD_MENU,
177 	TEXICMD_MINUS,
178 	TEXICMD_MULTITABLE,
179 	TEXICMD_NEED,
180 	TEXICMD_NEWLINE,
181 	TEXICMD_NODE,
182 	TEXICMD_NOINDENT,
183 	TEXICMD_O,
184 	TEXICMD_OE,
185 	TEXICMD_OESMALL,
186 	TEXICMD_OGONEK,
187 	TEXICMD_OPTION,
188 	TEXICMD_ORDF,
189 	TEXICMD_ORDM,
190 	TEXICMD_OSMALL,
191 	TEXICMD_PAGE,
192 	TEXICMD_PARINDENT,
193 	TEXICMD_PERIOD,
194 	TEXICMD_PINDEX,
195 	TEXICMD_POUNDS,
196 	TEXICMD_PRINTINDEX,
197 	TEXICMD_PXREF,
198 	TEXICMD_QUESTIONDOWN,
199 	TEXICMD_QUESTIONMARK,
200 	TEXICMD_QUOTATION,
201 	TEXICMD_QUOTEDBLBASE,
202 	TEXICMD_QUOTEDBLLEFT,
203 	TEXICMD_QUOTEDBLRIGHT,
204 	TEXICMD_QUOTESINGLBASE,
205 	TEXICMD_QUOTELEFT,
206 	TEXICMD_QUOTERIGHT,
207 	TEXICMD_R,
208 	TEXICMD_RAISESECTIONS,
209 	TEXICMD_REF,
210 	TEXICMD_REFILL,
211 	TEXICMD_REGISTEREDSYMBOL,
212 	TEXICMD_RESULT,
213 	TEXICMD_RINGACCENT,
214 	TEXICMD_SAMP,
215 	TEXICMD_SANSSERIF,
216 	TEXICMD_SC,
217 	TEXICMD_SECTION,
218 	TEXICMD_SET,
219 	TEXICMD_SETCHAPNEWPAGE,
220 	TEXICMD_SETCONTENTSAFTER,
221 	TEXICMD_SETFILENAME,
222 	TEXICMD_SETTITLE,
223 	TEXICMD_SHORTCONTENTS,
224 	TEXICMD_SLANTED,
225 	TEXICMD_SLASH,
226 	TEXICMD_SP,
227 	TEXICMD_SPACE,
228 	TEXICMD_SMALLBOOK,
229 	TEXICMD_SMALLDISPLAY,
230 	TEXICMD_SMALLEXAMPLE,
231 	TEXICMD_SMALLFORMAT,
232 	TEXICMD_SMALLINDENTBLOCK,
233 	TEXICMD_SQUIGGLE_LEFT,
234 	TEXICMD_SQUIGGLE_RIGHT,
235 	TEXICMD_SS,
236 	TEXICMD_STRONG,
237 	TEXICMD_SUBHEADING,
238 	TEXICMD_SUBSECTION,
239 	TEXICMD_SUBSUBHEADING,
240 	TEXICMD_SUBSUBSECTION,
241 	TEXICMD_SUBTITLE,
242 	TEXICMD_SUMMARYCONTENTS,
243 	TEXICMD_SYNINDEX,
244 	TEXICMD_SYNCODEINDEX,
245 	TEXICMD_T,
246 	TEXICMD_TAB,
247 	TEXICMD_TABSYM,
248 	TEXICMD_TABLE,
249 	TEXICMD_TEX,
250 	TEXICMD_TEXSYM,
251 	TEXICMD_TEXTDEGREE,
252 	TEXICMD_TH,
253 	TEXICMD_THSMALL,
254 	TEXICMD_TIE,
255 	TEXICMD_TIEACCENT,
256 	TEXICMD_TILDE,
257 	TEXICMD_TINDEX,
258 	TEXICMD_TITLE,
259 	TEXICMD_TITLEFONT,
260 	TEXICMD_TITLEPAGE,
261 	TEXICMD_TOP,
262 	TEXICMD_U,
263 	TEXICMD_UBARACCENT,
264 	TEXICMD_UDOTACCENT,
265 	TEXICMD_UMLAUT,
266 	TEXICMD_UNNUMBERED,
267 	TEXICMD_UNNUMBEREDSEC,
268 	TEXICMD_UNNUMBEREDSUBSEC,
269 	TEXICMD_UNNUMBEREDSUBSUBSEC,
270 	TEXICMD_UREF,
271 	TEXICMD_URL,
272 	TEXICMD_USER_INDEX,
273 	TEXICMD_V,
274 	TEXICMD_VALUE,
275 	TEXICMD_VAR,
276 	TEXICMD_VERB,
277 	TEXICMD_VERBATIM,
278 	TEXICMD_VERBATIMINCLUDE,
279 	TEXICMD_VINDEX,
280 	TEXICMD_VSKIP,
281 	TEXICMD_VTABLE,
282 	TEXICMD_W,
283 	TEXICMD_XREF,
284 	TEXICMD__MAX
285 };
286 
287 enum	texisrc {
288 	TEXISRC_FILE,
289 	TEXISRC_STDIN
290 };
291 
292 /*
293  * The file currently being parsed.
294  * This keeps track of our location within that file.
295  */
296 struct	texifile {
297 	enum texisrc	 type; /* type of file */
298 	const char	*name; /* name of the file */
299 	size_t	  	 line; /* current line (from zero) */
300 	size_t	  	 col; /* current column in line (from zero) */
301 	char		*map; /* allocated file buffer */
302 	size_t		 mapsz; /* size of map */
303 	size_t		 mapmaxsz; /* full size of map */
304 	size_t		 insplice; /* how many bytes left in splice */
305 };
306 
307 struct	texi;
308 
309 /*
310  * Callback for functions implementing texi commands.
311  */
312 typedef	void (*texicmdfp)(struct texi *, enum texicmd, size_t *);
313 
314 /*
315  * Describes Texinfo commands, whether native or overriden.
316  */
317 struct	texitok {
318 	texicmdfp	 fp; /* callback (or NULL if none) */
319 	const char	*tok; /* name of the token */
320 	size_t		 len; /* strlen(tok) */
321 };
322 
323 /*
324  * These values instruct us on whether a list (or table) of some type is
325  * currently being parsed.
326  */
327 enum	texilist {
328 	TEXILIST_NONE = 0,
329 	TEXILIST_ITEM,
330 	TEXILIST_NOITEM,
331 	TEXILIST_TABLE
332 };
333 
334 /*
335  * Hold values assigned with @set and retrieved with @value.
336  * These values can contain arbitrary Texinfo.
337  */
338 struct	texivalue {
339 	char		*key; /* the nil-terminated value name */
340 	char		*value; /* the nil-terminated value */
341 };
342 
343 /*
344  * Macros are (possibly-recursive) Texinfo sequences created with @macro
345  * and filled in by arguments when invoked.
346  */
347 struct	teximacro {
348 	char		 *key; /* nil-terminated macro name */
349 	char		 *value; /* nil-terminated value */
350 	char		**args; /* array of argument names (or NULL) */
351 	size_t		  argsz; /* array size */
352 };
353 
354 /*
355  * The main parse structure.
356  * This keeps any necessary information handy.
357  */
358 struct	texi {
359 	struct texifile   files[64]; /* stack of open files */
360 	size_t		  filepos; /* number of open files */
361 	const char	 *valstack[64]; /* stack of opened values */
362 	size_t		  valstackpos; /* position in valstack */
363 	size_t		  outcol; /* column in output line */
364 	char		**dirs; /* texi directories */
365 	size_t		  dirsz; /* number of texi directories */
366 	char		 *title; /* title of document */
367 	char		 *subtitle; /* subtitle of document */
368 	int		  secoffs; /* see sectioner() */
369 	char		**indexs; /* @defindex indices */
370 	size_t		  indexsz; /* entries in indexs */
371 	struct texivalue *vals; /* @value entries */
372 	size_t		  valsz; /* entries in vals */
373 	struct teximacro *macros; /* @macro entries */
374 	size_t		  macrosz; /* entries in macros */
375 	/*
376 	 * The following control what we output to the screen.
377 	 * The complexity is required to accomodate for mdoc(7).
378 	 */
379 	enum texilist	  list; /* current list (set recursively) */
380 	int		  outmacro; /* if >0, output is in line macro */
381 	int		  seenws; /* ws has been seen (and ignored) */
382 	int		  seenvs; /* newline has been Pp'd */
383 	int		  ign; /* if >0, don't print anything */
384 	int		  literal; /* if >0, literal context */
385 };
386 
387 #define	BUF(_p) ((_p)->files[(_p)->filepos - 1].map)
388 #define	BUFSZ(_p) ((_p)->files[(_p)->filepos - 1].mapsz)
389 
390 #define	isws(_x) \
391 	(' ' == (_x) || '\t' == (_x))
392 #define	ismspace(_x) \
393 	(isws((_x)) || '\n' == (_x))
394 
395 __BEGIN_DECLS
396 
397 void	advance(struct texi *, size_t *);
398 size_t	advanceeoln(struct texi *, size_t *, int);
399 void	advanceto(struct texi *, size_t *, size_t);
400 
401 char  **argparse(struct texi *, size_t *, size_t *, size_t);
402 
403 int	parsearg(struct texi *, size_t *, size_t);
404 void	parsebracket(struct texi *, size_t *);
405 void	parsestdin(struct texi *);
406 void	parsefile(struct texi *, const char *, int);
407 int	parselinearg(struct texi *, size_t *);
408 void	parseeoln(struct texi *, size_t *);
409 void	parseto(struct texi *, size_t *, const char *);
410 
411 void	texiabort(struct texi *, const char *)
412 		__attribute__((noreturn));
413 enum texicmd
414 	texicmd(struct texi *, size_t,
415 		size_t *, struct teximacro **);
416 void	texierr(struct texi *, const char *, ...)
417 		__attribute__((format(printf, 2, 3)))
418 		__attribute__((noreturn));
419 void	texiexit(struct texi *p);
420 void	texifilepop(struct texi *);
421 void	teximacro(struct texi *, const char *);
422 void	teximacroclose(struct texi *);
423 void	teximacroopen(struct texi *, const char *);
424 void 	texipunctuate(struct texi *, size_t *);
425 void	texiputbuf(struct texi *p, size_t, size_t);
426 void	texiputchar(struct texi *p, char);
427 void	texiputchars(struct texi *, const char *);
428 void	texivspace(struct texi *);
429 void	texiwarn(const struct texi *, const char *, ...)
430 		__attribute__((format(printf, 2, 3)));
431 void	texisplice(struct texi *, const char *, size_t, size_t *);
432 
433 void	valueadd(struct texi *, char *, char *);
434 const char *
435 	valueblookup(struct texi *, size_t *);
436 void	valuelclear(struct texi *, size_t *);
437 const char *
438 	valuellookup(struct texi *, size_t *);
439 
440 extern	const struct texitok *const texitoks;
441 
442 __END_DECLS
443 
444 #endif
445