xref: /openbsd-src/usr.bin/mandoc/mdoc.h (revision 6f05df2d9be0954bec42d51d943d77bd250fb664)
1 /*	$OpenBSD: mdoc.h,v 1.57 2014/11/28 19:25:03 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 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 
18 enum	mdoct {
19 	MDOC_Ap = 0,
20 	MDOC_Dd,
21 	MDOC_Dt,
22 	MDOC_Os,
23 	MDOC_Sh,
24 	MDOC_Ss,
25 	MDOC_Pp,
26 	MDOC_D1,
27 	MDOC_Dl,
28 	MDOC_Bd,
29 	MDOC_Ed,
30 	MDOC_Bl,
31 	MDOC_El,
32 	MDOC_It,
33 	MDOC_Ad,
34 	MDOC_An,
35 	MDOC_Ar,
36 	MDOC_Cd,
37 	MDOC_Cm,
38 	MDOC_Dv,
39 	MDOC_Er,
40 	MDOC_Ev,
41 	MDOC_Ex,
42 	MDOC_Fa,
43 	MDOC_Fd,
44 	MDOC_Fl,
45 	MDOC_Fn,
46 	MDOC_Ft,
47 	MDOC_Ic,
48 	MDOC_In,
49 	MDOC_Li,
50 	MDOC_Nd,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C__C,
133 	MDOC_Es,
134 	MDOC_En,
135 	MDOC_Dx,
136 	MDOC__Q,
137 	MDOC_br,
138 	MDOC_sp,
139 	MDOC__U,
140 	MDOC_Ta,
141 	MDOC_ll,
142 	MDOC_MAX
143 };
144 
145 enum	mdocargt {
146 	MDOC_Split, /* -split */
147 	MDOC_Nosplit, /* -nospli */
148 	MDOC_Ragged, /* -ragged */
149 	MDOC_Unfilled, /* -unfilled */
150 	MDOC_Literal, /* -literal */
151 	MDOC_File, /* -file */
152 	MDOC_Offset, /* -offset */
153 	MDOC_Bullet, /* -bullet */
154 	MDOC_Dash, /* -dash */
155 	MDOC_Hyphen, /* -hyphen */
156 	MDOC_Item, /* -item */
157 	MDOC_Enum, /* -enum */
158 	MDOC_Tag, /* -tag */
159 	MDOC_Diag, /* -diag */
160 	MDOC_Hang, /* -hang */
161 	MDOC_Ohang, /* -ohang */
162 	MDOC_Inset, /* -inset */
163 	MDOC_Column, /* -column */
164 	MDOC_Width, /* -width */
165 	MDOC_Compact, /* -compact */
166 	MDOC_Std, /* -std */
167 	MDOC_Filled, /* -filled */
168 	MDOC_Words, /* -words */
169 	MDOC_Emphasis, /* -emphasis */
170 	MDOC_Symbolic, /* -symbolic */
171 	MDOC_Nested, /* -nested */
172 	MDOC_Centred, /* -centered */
173 	MDOC_ARG_MAX
174 };
175 
176 enum	mdoc_type {
177 	MDOC_TEXT,
178 	MDOC_ELEM,
179 	MDOC_HEAD,
180 	MDOC_TAIL,
181 	MDOC_BODY,
182 	MDOC_BLOCK,
183 	MDOC_TBL,
184 	MDOC_EQN,
185 	MDOC_ROOT
186 };
187 
188 /*
189  * Section (named/unnamed) of `Sh'.   Note that these appear in the
190  * conventional order imposed by mdoc.7.  In the case of SEC_NONE, no
191  * section has been invoked (this shouldn't happen).  SEC_CUSTOM refers
192  * to other sections.
193  */
194 enum	mdoc_sec {
195 	SEC_NONE = 0,
196 	SEC_NAME, /* NAME */
197 	SEC_LIBRARY, /* LIBRARY */
198 	SEC_SYNOPSIS, /* SYNOPSIS */
199 	SEC_DESCRIPTION, /* DESCRIPTION */
200 	SEC_CONTEXT, /* CONTEXT */
201 	SEC_IMPLEMENTATION, /* IMPLEMENTATION NOTES */
202 	SEC_RETURN_VALUES, /* RETURN VALUES */
203 	SEC_ENVIRONMENT,  /* ENVIRONMENT */
204 	SEC_FILES, /* FILES */
205 	SEC_EXIT_STATUS, /* EXIT STATUS */
206 	SEC_EXAMPLES, /* EXAMPLES */
207 	SEC_DIAGNOSTICS, /* DIAGNOSTICS */
208 	SEC_COMPATIBILITY, /* COMPATIBILITY */
209 	SEC_ERRORS, /* ERRORS */
210 	SEC_SEE_ALSO, /* SEE ALSO */
211 	SEC_STANDARDS, /* STANDARDS */
212 	SEC_HISTORY, /* HISTORY */
213 	SEC_AUTHORS, /* AUTHORS */
214 	SEC_CAVEATS, /* CAVEATS */
215 	SEC_BUGS, /* BUGS */
216 	SEC_SECURITY, /* SECURITY */
217 	SEC_CUSTOM,
218 	SEC__MAX
219 };
220 
221 struct	mdoc_meta {
222 	char		 *msec; /* `Dt' section (1, 3p, etc.) */
223 	char		 *vol; /* `Dt' volume (implied) */
224 	char		 *arch; /* `Dt' arch (i386, etc.) */
225 	char		 *date; /* `Dd' normalised date */
226 	char		 *title; /* `Dt' title (FOO, etc.) */
227 	char		 *os; /* `Os' system (OpenBSD, etc.) */
228 	char		 *name; /* leading `Nm' name */
229 };
230 
231 /*
232  * An argument to a macro (multiple values = `-column xxx yyy').
233  */
234 struct	mdoc_argv {
235 	enum mdocargt	  arg; /* type of argument */
236 	int		  line;
237 	int		  pos;
238 	size_t		  sz; /* elements in "value" */
239 	char		**value; /* argument strings */
240 };
241 
242 /*
243  * Reference-counted macro arguments.  These are refcounted because
244  * blocks have multiple instances of the same arguments spread across
245  * the HEAD, BODY, TAIL, and BLOCK node types.
246  */
247 struct	mdoc_arg {
248 	size_t		  argc;
249 	struct mdoc_argv *argv;
250 	unsigned int	  refcnt;
251 };
252 
253 /*
254  * Indicates that a BODY's formatting has ended, but the scope is still
255  * open.  Used for syntax-broken blocks.
256  */
257 enum	mdoc_endbody {
258 	ENDBODY_NOT = 0,
259 	ENDBODY_SPACE, /* is broken: append a space */
260 	ENDBODY_NOSPACE /* is broken: don't append a space */
261 };
262 
263 enum	mdoc_list {
264 	LIST__NONE = 0,
265 	LIST_bullet, /* -bullet */
266 	LIST_column, /* -column */
267 	LIST_dash, /* -dash */
268 	LIST_diag, /* -diag */
269 	LIST_enum, /* -enum */
270 	LIST_hang, /* -hang */
271 	LIST_hyphen, /* -hyphen */
272 	LIST_inset, /* -inset */
273 	LIST_item, /* -item */
274 	LIST_ohang, /* -ohang */
275 	LIST_tag, /* -tag */
276 	LIST_MAX
277 };
278 
279 enum	mdoc_disp {
280 	DISP__NONE = 0,
281 	DISP_centered, /* -centered */
282 	DISP_ragged, /* -ragged */
283 	DISP_unfilled, /* -unfilled */
284 	DISP_filled, /* -filled */
285 	DISP_literal /* -literal */
286 };
287 
288 enum	mdoc_auth {
289 	AUTH__NONE = 0,
290 	AUTH_split, /* -split */
291 	AUTH_nosplit /* -nosplit */
292 };
293 
294 enum	mdoc_font {
295 	FONT__NONE = 0,
296 	FONT_Em, /* Em, -emphasis */
297 	FONT_Li, /* Li, -literal */
298 	FONT_Sy /* Sy, -symbolic */
299 };
300 
301 struct	mdoc_bd {
302 	const char	 *offs; /* -offset */
303 	enum mdoc_disp	  type; /* -ragged, etc. */
304 	int		  comp; /* -compact */
305 };
306 
307 struct	mdoc_bl {
308 	const char	 *width; /* -width */
309 	const char	 *offs; /* -offset */
310 	enum mdoc_list	  type; /* -tag, -enum, etc. */
311 	int		  comp; /* -compact */
312 	size_t		  ncols; /* -column arg count */
313 	const char	**cols; /* -column val ptr */
314 	int		  count; /* -enum counter */
315 };
316 
317 struct	mdoc_bf {
318 	enum mdoc_font	  font; /* font */
319 };
320 
321 struct	mdoc_an {
322 	enum mdoc_auth	  auth; /* -split, etc. */
323 };
324 
325 struct	mdoc_rs {
326 	int		  quote_T; /* whether to quote %T */
327 };
328 
329 /*
330  * Consists of normalised node arguments.  These should be used instead
331  * of iterating through the mdoc_arg pointers of a node: defaults are
332  * provided, etc.
333  */
334 union	mdoc_data {
335 	struct mdoc_an	  An;
336 	struct mdoc_bd	  Bd;
337 	struct mdoc_bf	  Bf;
338 	struct mdoc_bl	  Bl;
339 	struct mdoc_node *Es;
340 	struct mdoc_rs	  Rs;
341 };
342 
343 /*
344  * Single node in tree-linked AST.
345  */
346 struct	mdoc_node {
347 	struct mdoc_node *parent; /* parent AST node */
348 	struct mdoc_node *child; /* first child AST node */
349 	struct mdoc_node *last; /* last child AST node */
350 	struct mdoc_node *next; /* sibling AST node */
351 	struct mdoc_node *prev; /* prior sibling AST node */
352 	int		  nchild; /* number children */
353 	int		  line; /* parse line */
354 	int		  pos; /* parse column */
355 	int		  lastline; /* the node ends on this line */
356 	enum mdoct	  tok; /* tok or MDOC__MAX if none */
357 	int		  flags;
358 #define	MDOC_VALID	 (1 << 0) /* has been validated */
359 #define	MDOC_EOS	 (1 << 2) /* at sentence boundary */
360 #define	MDOC_LINE	 (1 << 3) /* first macro/text on line */
361 #define	MDOC_SYNPRETTY	 (1 << 4) /* SYNOPSIS-style formatting */
362 #define	MDOC_ENDED	 (1 << 5) /* rendering has been ended */
363 #define	MDOC_DELIMO	 (1 << 6)
364 #define	MDOC_DELIMC	 (1 << 7)
365 	enum mdoc_type	  type; /* AST node type */
366 	enum mdoc_sec	  sec; /* current named section */
367 	union mdoc_data	 *norm; /* normalised args */
368 	const void	 *prev_font; /* before entering this node */
369 	/* FIXME: these can be union'd to shave a few bytes. */
370 	struct mdoc_arg	 *args; /* BLOCK/ELEM */
371 	struct mdoc_node *pending; /* BLOCK */
372 	struct mdoc_node *head; /* BLOCK */
373 	struct mdoc_node *body; /* BLOCK */
374 	struct mdoc_node *tail; /* BLOCK */
375 	char		 *string; /* TEXT */
376 	const struct tbl_span *span; /* TBL */
377 	const struct eqn *eqn; /* EQN */
378 	enum mdoc_endbody end; /* BODY */
379 };
380 
381 /* Names of macros.  Index is enum mdoct. */
382 extern	const char *const *mdoc_macronames;
383 
384 /* Names of macro args.  Index is enum mdocargt. */
385 extern	const char *const *mdoc_argnames;
386 
387 __BEGIN_DECLS
388 
389 struct	mdoc;
390 
391 const struct mdoc_node *mdoc_node(const struct mdoc *);
392 const struct mdoc_meta *mdoc_meta(const struct mdoc *);
393 void mdoc_deroff(char **, const struct mdoc_node *);
394 
395 __END_DECLS
396