1 /* $Id: mdoc.h,v 1.131 2014/07/29 13:58:18 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 #ifndef MDOC_H 18 #define MDOC_H 19 20 enum mdoct { 21 MDOC_Ap = 0, 22 MDOC_Dd, 23 MDOC_Dt, 24 MDOC_Os, 25 MDOC_Sh, 26 MDOC_Ss, 27 MDOC_Pp, 28 MDOC_D1, 29 MDOC_Dl, 30 MDOC_Bd, 31 MDOC_Ed, 32 MDOC_Bl, 33 MDOC_El, 34 MDOC_It, 35 MDOC_Ad, 36 MDOC_An, 37 MDOC_Ar, 38 MDOC_Cd, 39 MDOC_Cm, 40 MDOC_Dv, 41 MDOC_Er, 42 MDOC_Ev, 43 MDOC_Ex, 44 MDOC_Fa, 45 MDOC_Fd, 46 MDOC_Fl, 47 MDOC_Fn, 48 MDOC_Ft, 49 MDOC_Ic, 50 MDOC_In, 51 MDOC_Li, 52 MDOC_Nd, 53 MDOC_Nm, 54 MDOC_Op, 55 MDOC_Ot, 56 MDOC_Pa, 57 MDOC_Rv, 58 MDOC_St, 59 MDOC_Va, 60 MDOC_Vt, 61 MDOC_Xr, 62 MDOC__A, 63 MDOC__B, 64 MDOC__D, 65 MDOC__I, 66 MDOC__J, 67 MDOC__N, 68 MDOC__O, 69 MDOC__P, 70 MDOC__R, 71 MDOC__T, 72 MDOC__V, 73 MDOC_Ac, 74 MDOC_Ao, 75 MDOC_Aq, 76 MDOC_At, 77 MDOC_Bc, 78 MDOC_Bf, 79 MDOC_Bo, 80 MDOC_Bq, 81 MDOC_Bsx, 82 MDOC_Bx, 83 MDOC_Db, 84 MDOC_Dc, 85 MDOC_Do, 86 MDOC_Dq, 87 MDOC_Ec, 88 MDOC_Ef, 89 MDOC_Em, 90 MDOC_Eo, 91 MDOC_Fx, 92 MDOC_Ms, 93 MDOC_No, 94 MDOC_Ns, 95 MDOC_Nx, 96 MDOC_Ox, 97 MDOC_Pc, 98 MDOC_Pf, 99 MDOC_Po, 100 MDOC_Pq, 101 MDOC_Qc, 102 MDOC_Ql, 103 MDOC_Qo, 104 MDOC_Qq, 105 MDOC_Re, 106 MDOC_Rs, 107 MDOC_Sc, 108 MDOC_So, 109 MDOC_Sq, 110 MDOC_Sm, 111 MDOC_Sx, 112 MDOC_Sy, 113 MDOC_Tn, 114 MDOC_Ux, 115 MDOC_Xc, 116 MDOC_Xo, 117 MDOC_Fo, 118 MDOC_Fc, 119 MDOC_Oo, 120 MDOC_Oc, 121 MDOC_Bk, 122 MDOC_Ek, 123 MDOC_Bt, 124 MDOC_Hf, 125 MDOC_Fr, 126 MDOC_Ud, 127 MDOC_Lb, 128 MDOC_Lp, 129 MDOC_Lk, 130 MDOC_Mt, 131 MDOC_Brq, 132 MDOC_Bro, 133 MDOC_Brc, 134 MDOC__C, 135 MDOC_Es, 136 MDOC_En, 137 MDOC_Dx, 138 MDOC__Q, 139 MDOC_br, 140 MDOC_sp, 141 MDOC__U, 142 MDOC_Ta, 143 MDOC_ll, 144 MDOC_MAX 145 }; 146 147 enum mdocargt { 148 MDOC_Split, /* -split */ 149 MDOC_Nosplit, /* -nospli */ 150 MDOC_Ragged, /* -ragged */ 151 MDOC_Unfilled, /* -unfilled */ 152 MDOC_Literal, /* -literal */ 153 MDOC_File, /* -file */ 154 MDOC_Offset, /* -offset */ 155 MDOC_Bullet, /* -bullet */ 156 MDOC_Dash, /* -dash */ 157 MDOC_Hyphen, /* -hyphen */ 158 MDOC_Item, /* -item */ 159 MDOC_Enum, /* -enum */ 160 MDOC_Tag, /* -tag */ 161 MDOC_Diag, /* -diag */ 162 MDOC_Hang, /* -hang */ 163 MDOC_Ohang, /* -ohang */ 164 MDOC_Inset, /* -inset */ 165 MDOC_Column, /* -column */ 166 MDOC_Width, /* -width */ 167 MDOC_Compact, /* -compact */ 168 MDOC_Std, /* -std */ 169 MDOC_Filled, /* -filled */ 170 MDOC_Words, /* -words */ 171 MDOC_Emphasis, /* -emphasis */ 172 MDOC_Symbolic, /* -symbolic */ 173 MDOC_Nested, /* -nested */ 174 MDOC_Centred, /* -centered */ 175 MDOC_ARG_MAX 176 }; 177 178 enum mdoc_type { 179 MDOC_TEXT, 180 MDOC_ELEM, 181 MDOC_HEAD, 182 MDOC_TAIL, 183 MDOC_BODY, 184 MDOC_BLOCK, 185 MDOC_TBL, 186 MDOC_EQN, 187 MDOC_ROOT 188 }; 189 190 /* 191 * Section (named/unnamed) of `Sh'. Note that these appear in the 192 * conventional order imposed by mdoc.7. In the case of SEC_NONE, no 193 * section has been invoked (this shouldn't happen). SEC_CUSTOM refers 194 * to other sections. 195 */ 196 enum mdoc_sec { 197 SEC_NONE = 0, 198 SEC_NAME, /* NAME */ 199 SEC_LIBRARY, /* LIBRARY */ 200 SEC_SYNOPSIS, /* SYNOPSIS */ 201 SEC_DESCRIPTION, /* DESCRIPTION */ 202 SEC_CONTEXT, /* CONTEXT */ 203 SEC_IMPLEMENTATION, /* IMPLEMENTATION NOTES */ 204 SEC_RETURN_VALUES, /* RETURN VALUES */ 205 SEC_ENVIRONMENT, /* ENVIRONMENT */ 206 SEC_FILES, /* FILES */ 207 SEC_EXIT_STATUS, /* EXIT STATUS */ 208 SEC_EXAMPLES, /* EXAMPLES */ 209 SEC_DIAGNOSTICS, /* DIAGNOSTICS */ 210 SEC_COMPATIBILITY, /* COMPATIBILITY */ 211 SEC_ERRORS, /* ERRORS */ 212 SEC_SEE_ALSO, /* SEE ALSO */ 213 SEC_STANDARDS, /* STANDARDS */ 214 SEC_HISTORY, /* HISTORY */ 215 SEC_AUTHORS, /* AUTHORS */ 216 SEC_CAVEATS, /* CAVEATS */ 217 SEC_BUGS, /* BUGS */ 218 SEC_SECURITY, /* SECURITY */ 219 SEC_CUSTOM, 220 SEC__MAX 221 }; 222 223 struct mdoc_meta { 224 char *msec; /* `Dt' section (1, 3p, etc.) */ 225 char *vol; /* `Dt' volume (implied) */ 226 char *arch; /* `Dt' arch (i386, etc.) */ 227 char *date; /* `Dd' normalised date */ 228 char *title; /* `Dt' title (FOO, etc.) */ 229 char *os; /* `Os' system (OpenBSD, etc.) */ 230 char *name; /* leading `Nm' name */ 231 }; 232 233 /* 234 * An argument to a macro (multiple values = `-column xxx yyy'). 235 */ 236 struct mdoc_argv { 237 enum mdocargt arg; /* type of argument */ 238 int line; 239 int pos; 240 size_t sz; /* elements in "value" */ 241 char **value; /* argument strings */ 242 }; 243 244 /* 245 * Reference-counted macro arguments. These are refcounted because 246 * blocks have multiple instances of the same arguments spread across 247 * the HEAD, BODY, TAIL, and BLOCK node types. 248 */ 249 struct mdoc_arg { 250 size_t argc; 251 struct mdoc_argv *argv; 252 unsigned int refcnt; 253 }; 254 255 /* 256 * Indicates that a BODY's formatting has ended, but the scope is still 257 * open. Used for syntax-broken blocks. 258 */ 259 enum mdoc_endbody { 260 ENDBODY_NOT = 0, 261 ENDBODY_SPACE, /* is broken: append a space */ 262 ENDBODY_NOSPACE /* is broken: don't append a space */ 263 }; 264 265 enum mdoc_list { 266 LIST__NONE = 0, 267 LIST_bullet, /* -bullet */ 268 LIST_column, /* -column */ 269 LIST_dash, /* -dash */ 270 LIST_diag, /* -diag */ 271 LIST_enum, /* -enum */ 272 LIST_hang, /* -hang */ 273 LIST_hyphen, /* -hyphen */ 274 LIST_inset, /* -inset */ 275 LIST_item, /* -item */ 276 LIST_ohang, /* -ohang */ 277 LIST_tag, /* -tag */ 278 LIST_MAX 279 }; 280 281 enum mdoc_disp { 282 DISP__NONE = 0, 283 DISP_centered, /* -centered */ 284 DISP_ragged, /* -ragged */ 285 DISP_unfilled, /* -unfilled */ 286 DISP_filled, /* -filled */ 287 DISP_literal /* -literal */ 288 }; 289 290 enum mdoc_auth { 291 AUTH__NONE = 0, 292 AUTH_split, /* -split */ 293 AUTH_nosplit /* -nosplit */ 294 }; 295 296 enum mdoc_font { 297 FONT__NONE = 0, 298 FONT_Em, /* Em, -emphasis */ 299 FONT_Li, /* Li, -literal */ 300 FONT_Sy /* Sy, -symbolic */ 301 }; 302 303 struct mdoc_bd { 304 const char *offs; /* -offset */ 305 enum mdoc_disp type; /* -ragged, etc. */ 306 int comp; /* -compact */ 307 }; 308 309 struct mdoc_bl { 310 const char *width; /* -width */ 311 const char *offs; /* -offset */ 312 enum mdoc_list type; /* -tag, -enum, etc. */ 313 int comp; /* -compact */ 314 size_t ncols; /* -column arg count */ 315 const char **cols; /* -column val ptr */ 316 int count; /* -enum counter */ 317 }; 318 319 struct mdoc_bf { 320 enum mdoc_font font; /* font */ 321 }; 322 323 struct mdoc_an { 324 enum mdoc_auth auth; /* -split, etc. */ 325 }; 326 327 struct mdoc_rs { 328 int quote_T; /* whether to quote %T */ 329 }; 330 331 /* 332 * Consists of normalised node arguments. These should be used instead 333 * of iterating through the mdoc_arg pointers of a node: defaults are 334 * provided, etc. 335 */ 336 union mdoc_data { 337 struct mdoc_an An; 338 struct mdoc_bd Bd; 339 struct mdoc_bf Bf; 340 struct mdoc_bl Bl; 341 struct mdoc_node *Es; 342 struct mdoc_rs Rs; 343 }; 344 345 /* 346 * Single node in tree-linked AST. 347 */ 348 struct mdoc_node { 349 struct mdoc_node *parent; /* parent AST node */ 350 struct mdoc_node *child; /* first child AST node */ 351 struct mdoc_node *last; /* last child AST node */ 352 struct mdoc_node *next; /* sibling AST node */ 353 struct mdoc_node *prev; /* prior sibling AST node */ 354 int nchild; /* number children */ 355 int line; /* parse line */ 356 int pos; /* parse column */ 357 int lastline; /* the node ends on this line */ 358 enum mdoct tok; /* tok or MDOC__MAX if none */ 359 int flags; 360 #define MDOC_VALID (1 << 0) /* has been validated */ 361 #define MDOC_EOS (1 << 2) /* at sentence boundary */ 362 #define MDOC_LINE (1 << 3) /* first macro/text on line */ 363 #define MDOC_SYNPRETTY (1 << 4) /* SYNOPSIS-style formatting */ 364 #define MDOC_ENDED (1 << 5) /* rendering has been ended */ 365 #define MDOC_DELIMO (1 << 6) 366 #define MDOC_DELIMC (1 << 7) 367 enum mdoc_type type; /* AST node type */ 368 enum mdoc_sec sec; /* current named section */ 369 union mdoc_data *norm; /* normalised args */ 370 const void *prev_font; /* before entering this node */ 371 /* FIXME: these can be union'd to shave a few bytes. */ 372 struct mdoc_arg *args; /* BLOCK/ELEM */ 373 struct mdoc_node *pending; /* BLOCK */ 374 struct mdoc_node *head; /* BLOCK */ 375 struct mdoc_node *body; /* BLOCK */ 376 struct mdoc_node *tail; /* BLOCK */ 377 char *string; /* TEXT */ 378 const struct tbl_span *span; /* TBL */ 379 const struct eqn *eqn; /* EQN */ 380 enum mdoc_endbody end; /* BODY */ 381 }; 382 383 /* Names of macros. Index is enum mdoct. */ 384 extern const char *const *mdoc_macronames; 385 386 /* Names of macro args. Index is enum mdocargt. */ 387 extern const char *const *mdoc_argnames; 388 389 __BEGIN_DECLS 390 391 struct mdoc; 392 393 const struct mdoc_node *mdoc_node(const struct mdoc *); 394 const struct mdoc_meta *mdoc_meta(const struct mdoc *); 395 void mdoc_deroff(char **, const struct mdoc_node *); 396 397 __END_DECLS 398 399 #endif /*!MDOC_H*/ 400