1 /*      $Id: mdoc.h,v 1.122 2011/03/22 14:05:45 kristaps 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_MAX
 144 };
 145 
 146 enum    mdocargt {
 147         MDOC_Split, /* -split */
 148         MDOC_Nosplit, /* -nospli */
 149         MDOC_Ragged, /* -ragged */
 150         MDOC_Unfilled, /* -unfilled */
 151         MDOC_Literal, /* -literal */
 152         MDOC_File, /* -file */
 153         MDOC_Offset, /* -offset */
 154         MDOC_Bullet, /* -bullet */
 155         MDOC_Dash, /* -dash */
 156         MDOC_Hyphen, /* -hyphen */
 157         MDOC_Item, /* -item */
 158         MDOC_Enum, /* -enum */
 159         MDOC_Tag, /* -tag */
 160         MDOC_Diag, /* -diag */
 161         MDOC_Hang, /* -hang */
 162         MDOC_Ohang, /* -ohang */
 163         MDOC_Inset, /* -inset */
 164         MDOC_Column, /* -column */
 165         MDOC_Width, /* -width */
 166         MDOC_Compact, /* -compact */
 167         MDOC_Std, /* -std */
 168         MDOC_Filled, /* -filled */
 169         MDOC_Words, /* -words */
 170         MDOC_Emphasis, /* -emphasis */
 171         MDOC_Symbolic, /* -symbolic */
 172         MDOC_Nested, /* -nested */
 173         MDOC_Centred, /* -centered */
 174         MDOC_ARG_MAX
 175 };
 176 
 177 enum    mdoc_type {
 178         MDOC_TEXT,
 179         MDOC_ELEM,
 180         MDOC_HEAD,
 181         MDOC_TAIL,
 182         MDOC_BODY,
 183         MDOC_BLOCK,
 184         MDOC_TBL,
 185         MDOC_EQN,
 186         MDOC_ROOT
 187 };
 188 
 189 /* 
 190  * Section (named/unnamed) of `Sh'.   Note that these appear in the
 191  * conventional order imposed by mdoc.7.  In the case of SEC_NONE, no
 192  * section has been invoked (this shouldn't happen).  SEC_CUSTOM refers
 193  * to other sections.
 194  */
 195 enum    mdoc_sec {
 196         SEC_NONE = 0,
 197         SEC_NAME, /* NAME */
 198         SEC_LIBRARY, /* LIBRARY */
 199         SEC_SYNOPSIS, /* SYNOPSIS */
 200         SEC_DESCRIPTION, /* DESCRIPTION */
 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_ARCHITECTURE,
 211         SEC_CODE_SET_INDEPENDENCE,
 212         SEC_INTERFACE_STABILITY,
 213         SEC_MULTITHREADING_LEVEL,
 214         SEC_SEE_ALSO, /* SEE ALSO */
 215         SEC_STANDARDS, /* STANDARDS */
 216         SEC_HISTORY, /* HISTORY */
 217         SEC_AUTHORS, /* AUTHORS */
 218         SEC_CAVEATS, /* CAVEATS */
 219         SEC_BUGS, /* BUGS */
 220         SEC_SECURITY, /* SECURITY */
 221         SEC_CUSTOM, 
 222         SEC__MAX
 223 };
 224 
 225 struct  mdoc_meta {
 226         char             *msec; /* `Dt' section (1, 3p, etc.) */
 227         char             *vol; /* `Dt' volume (implied) */
 228         char             *arch; /* `Dt' arch (i386, etc.) */
 229         char             *date; /* `Dd' normalised date */
 230         char             *title; /* `Dt' title (FOO, etc.) */
 231         char             *os; /* `Os' system (OpenBSD, etc.) */
 232         char             *name; /* leading `Nm' name */
 233 };
 234 
 235 /* 
 236  * An argument to a macro (multiple values = `-column xxx yyy'). 
 237  */
 238 struct  mdoc_argv {
 239         enum mdocargt     arg; /* type of argument */
 240         int               line;
 241         int               pos;
 242         size_t            sz; /* elements in "value" */
 243         char            **value; /* argument strings */
 244 };
 245 
 246 /*
 247  * Reference-counted macro arguments.  These are refcounted because
 248  * blocks have multiple instances of the same arguments spread across
 249  * the HEAD, BODY, TAIL, and BLOCK node types.
 250  */
 251 struct  mdoc_arg {
 252         size_t            argc;
 253         struct mdoc_argv *argv;
 254         unsigned int      refcnt;
 255 };
 256 
 257 /*
 258  * Indicates that a BODY's formatting has ended, but the scope is still
 259  * open.  Used for syntax-broken blocks.
 260  */
 261 enum    mdoc_endbody {
 262         ENDBODY_NOT = 0,
 263         ENDBODY_SPACE, /* is broken: append a space */
 264         ENDBODY_NOSPACE /* is broken: don't append a space */
 265 };
 266 
 267 enum    mdoc_list {
 268         LIST__NONE = 0,
 269         LIST_bullet, /* -bullet */
 270         LIST_column, /* -column */
 271         LIST_dash, /* -dash */
 272         LIST_diag, /* -diag */
 273         LIST_enum, /* -enum */
 274         LIST_hang, /* -hang */
 275         LIST_hyphen, /* -hyphen */
 276         LIST_inset, /* -inset */
 277         LIST_item, /* -item */
 278         LIST_ohang, /* -ohang */
 279         LIST_tag, /* -tag */
 280         LIST_MAX
 281 };
 282 
 283 enum    mdoc_disp {
 284         DISP__NONE = 0,
 285         DISP_centred, /* -centered */
 286         DISP_ragged, /* -ragged */
 287         DISP_unfilled, /* -unfilled */
 288         DISP_filled, /* -filled */
 289         DISP_literal /* -literal */
 290 };
 291 
 292 enum    mdoc_auth {
 293         AUTH__NONE = 0,
 294         AUTH_split, /* -split */
 295         AUTH_nosplit /* -nosplit */
 296 };
 297 
 298 enum    mdoc_font {
 299         FONT__NONE = 0,
 300         FONT_Em, /* Em, -emphasis */
 301         FONT_Li, /* Li, -literal */
 302         FONT_Sy /* Sy, -symbolic */
 303 };
 304 
 305 struct  mdoc_bd {
 306         const char       *offs; /* -offset */
 307         enum mdoc_disp    type; /* -ragged, etc. */
 308         int               comp; /* -compact */
 309 };
 310 
 311 struct  mdoc_bl {
 312         const char       *width; /* -width */
 313         const char       *offs; /* -offset */
 314         enum mdoc_list    type; /* -tag, -enum, etc. */
 315         int               comp; /* -compact */
 316         size_t            ncols; /* -column arg count */
 317         const char      **cols; /* -column val ptr */
 318 };
 319 
 320 struct  mdoc_bf {
 321         enum mdoc_font    font; /* font */
 322 };
 323 
 324 struct  mdoc_an {
 325         enum mdoc_auth    auth; /* -split, etc. */
 326 };
 327 
 328 struct  mdoc_rs {
 329         int               quote_T; /* whether to quote %T */
 330 };
 331 
 332 /*
 333  * Consists of normalised node arguments.  These should be used instead
 334  * of iterating through the mdoc_arg pointers of a node: defaults are
 335  * provided, etc.
 336  */
 337 union   mdoc_data {
 338         struct mdoc_an    An;
 339         struct mdoc_bd    Bd;
 340         struct mdoc_bf    Bf;
 341         struct mdoc_bl    Bl;
 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         enum mdoct        tok; /* tok or MDOC__MAX if none */
 358         int               flags;
 359 #define MDOC_VALID       (1 << 0) /* has been validated */
 360 #define MDOC_EOS         (1 << 2) /* at sentence boundary */
 361 #define MDOC_LINE        (1 << 3) /* first macro/text on line */
 362 #define MDOC_SYNPRETTY   (1 << 4) /* SYNOPSIS-style formatting */
 363 #define MDOC_ENDED       (1 << 5) /* rendering has been ended */
 364 #define MDOC_DELIMO      (1 << 6)
 365 #define MDOC_DELIMC      (1 << 7)
 366         enum mdoc_type    type; /* AST node type */
 367         enum mdoc_sec     sec; /* current named section */
 368         union mdoc_data  *norm; /* normalised args */
 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 
 394 __END_DECLS
 395 
 396 #endif /*!MDOC_H*/