| Commit | Line | Data |
|---|---|---|
| 32c903ac | 1 | /* $Id: mdoc_macro.c,v 1.38 2009/10/26 17:05:44 kristaps Exp $ */ |
| 589e7c1d SW |
2 | /* |
| 3 | * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> | |
| 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 | #include <assert.h> | |
| 18 | #include <ctype.h> | |
| 19 | #include <stdlib.h> | |
| 20 | #include <stdio.h> | |
| 21 | #include <string.h> | |
| 32c903ac | 22 | #include <time.h> |
| 589e7c1d SW |
23 | |
| 24 | #include "libmdoc.h" | |
| 25 | ||
| 26 | #define REWIND_REWIND (1 << 0) | |
| 27 | #define REWIND_NOHALT (1 << 1) | |
| 28 | #define REWIND_HALT (1 << 2) | |
| 29 | ||
| 30 | static int obsolete(MACRO_PROT_ARGS); | |
| 31 | static int blk_part_exp(MACRO_PROT_ARGS); | |
| 32 | static int in_line_eoln(MACRO_PROT_ARGS); | |
| 33 | static int in_line_argn(MACRO_PROT_ARGS); | |
| 34 | static int in_line(MACRO_PROT_ARGS); | |
| 35 | static int blk_full(MACRO_PROT_ARGS); | |
| 36 | static int blk_exp_close(MACRO_PROT_ARGS); | |
| 37 | static int blk_part_imp(MACRO_PROT_ARGS); | |
| 38 | ||
| 39 | static int phrase(struct mdoc *, int, int, char *); | |
| 40 | static int rew_dohalt(int, enum mdoc_type, | |
| 41 | const struct mdoc_node *); | |
| 42 | static int rew_alt(int); | |
| 43 | static int rew_dobreak(int, const struct mdoc_node *); | |
| 44 | static int rew_elem(struct mdoc *, int); | |
| 45 | static int rew_sub(enum mdoc_type, struct mdoc *, | |
| 46 | int, int, int); | |
| 47 | static int rew_last(struct mdoc *, | |
| 48 | const struct mdoc_node *); | |
| 49 | static int append_delims(struct mdoc *, int, int *, char *); | |
| 50 | static int lookup(int, const char *); | |
| 51 | static int lookup_raw(const char *); | |
| 52 | static int swarn(struct mdoc *, enum mdoc_type, int, int, | |
| 53 | const struct mdoc_node *); | |
| 54 | ||
| 55 | /* Central table of library: who gets parsed how. */ | |
| 56 | ||
| 57 | const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { | |
| 58 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */ | |
| 59 | { in_line_eoln, MDOC_PROLOGUE }, /* Dd */ | |
| 60 | { in_line_eoln, MDOC_PROLOGUE }, /* Dt */ | |
| 61 | { in_line_eoln, MDOC_PROLOGUE }, /* Os */ | |
| 62 | { blk_full, 0 }, /* Sh */ | |
| 63 | { blk_full, 0 }, /* Ss */ | |
| 64 | { in_line_eoln, 0 }, /* Pp */ | |
| 65 | { blk_part_imp, MDOC_PARSED }, /* D1 */ | |
| 66 | { blk_part_imp, MDOC_PARSED }, /* Dl */ | |
| 67 | { blk_full, MDOC_EXPLICIT }, /* Bd */ | |
| 68 | { blk_exp_close, MDOC_EXPLICIT }, /* Ed */ | |
| 69 | { blk_full, MDOC_EXPLICIT }, /* Bl */ | |
| 70 | { blk_exp_close, MDOC_EXPLICIT }, /* El */ | |
| 71 | { blk_full, MDOC_PARSED }, /* It */ | |
| 72 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */ | |
| 73 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */ | |
| 74 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */ | |
| 75 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */ | |
| 76 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */ | |
| 77 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */ | |
| 78 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */ | |
| 79 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */ | |
| 80 | { in_line_eoln, 0 }, /* Ex */ | |
| 81 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */ | |
| 82 | { in_line_eoln, 0 }, /* Fd */ | |
| 83 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */ | |
| 84 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */ | |
| 85 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */ | |
| 86 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */ | |
| 87 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */ | |
| 88 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */ | |
| 89 | { blk_full, 0 }, /* Nd */ | |
| 90 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */ | |
| 91 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */ | |
| 92 | { obsolete, 0 }, /* Ot */ | |
| 93 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */ | |
| 94 | { in_line_eoln, 0 }, /* Rv */ | |
| 95 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */ | |
| 96 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */ | |
| 97 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */ | |
| 98 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */ | |
| 99 | { in_line_eoln, 0 }, /* %A */ | |
| 100 | { in_line_eoln, 0 }, /* %B */ | |
| 101 | { in_line_eoln, 0 }, /* %D */ | |
| 102 | { in_line_eoln, 0 }, /* %I */ | |
| 103 | { in_line_eoln, 0 }, /* %J */ | |
| 104 | { in_line_eoln, 0 }, /* %N */ | |
| 105 | { in_line_eoln, 0 }, /* %O */ | |
| 106 | { in_line_eoln, 0 }, /* %P */ | |
| 107 | { in_line_eoln, 0 }, /* %R */ | |
| 108 | { in_line_eoln, 0 }, /* %T */ | |
| 109 | { in_line_eoln, 0 }, /* %V */ | |
| 110 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ac */ | |
| 111 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ao */ | |
| 112 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Aq */ | |
| 113 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */ | |
| 114 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Bc */ | |
| 115 | { blk_full, MDOC_EXPLICIT }, /* Bf */ | |
| 116 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bo */ | |
| 117 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Bq */ | |
| 118 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */ | |
| 119 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */ | |
| 120 | { in_line_eoln, 0 }, /* Db */ | |
| 121 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Dc */ | |
| 122 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Do */ | |
| 123 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Dq */ | |
| 124 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Ec */ | |
| 125 | { blk_exp_close, MDOC_EXPLICIT }, /* Ef */ | |
| 126 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Em */ | |
| 127 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */ | |
| 128 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */ | |
| 129 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */ | |
| 130 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* No */ | |
| 131 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ns */ | |
| 132 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */ | |
| 133 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */ | |
| 134 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Pc */ | |
| 135 | { in_line_argn, MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */ | |
| 136 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Po */ | |
| 137 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Pq */ | |
| 138 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Qc */ | |
| 139 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Ql */ | |
| 140 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Qo */ | |
| 141 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Qq */ | |
| 142 | { blk_exp_close, MDOC_EXPLICIT }, /* Re */ | |
| 143 | { blk_full, MDOC_EXPLICIT }, /* Rs */ | |
| 144 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Sc */ | |
| 145 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* So */ | |
| 146 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Sq */ | |
| 147 | { in_line_eoln, 0 }, /* Sm */ | |
| 148 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sx */ | |
| 149 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Sy */ | |
| 150 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */ | |
| 151 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ux */ | |
| 152 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */ | |
| 153 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */ | |
| 154 | { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */ | |
| 155 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Fc */ | |
| 156 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Oo */ | |
| 157 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Oc */ | |
| 158 | { blk_full, MDOC_EXPLICIT }, /* Bk */ | |
| 159 | { blk_exp_close, MDOC_EXPLICIT }, /* Ek */ | |
| 160 | { in_line_eoln, 0 }, /* Bt */ | |
| 161 | { in_line_eoln, 0 }, /* Hf */ | |
| 162 | { obsolete, 0 }, /* Fr */ | |
| 163 | { in_line_eoln, 0 }, /* Ud */ | |
| 164 | { in_line_eoln, 0 }, /* Lb */ | |
| 165 | { in_line_eoln, 0 }, /* Lp */ | |
| 166 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */ | |
| 167 | { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */ | |
| 168 | { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Brq */ | |
| 169 | { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Bro */ | |
| 170 | { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Brc */ | |
| 171 | { in_line_eoln, 0 }, /* %C */ | |
| 172 | { obsolete, 0 }, /* Es */ | |
| 173 | { obsolete, 0 }, /* En */ | |
| 174 | { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */ | |
| 175 | { in_line_eoln, 0 }, /* %Q */ | |
| 176 | { in_line_eoln, 0 }, /* br */ | |
| 177 | { in_line_eoln, 0 }, /* sp */ | |
| cbce6d97 | 178 | { in_line_eoln, 0 }, /* %U */ |
| 589e7c1d SW |
179 | }; |
| 180 | ||
| 181 | const struct mdoc_macro * const mdoc_macros = __mdoc_macros; | |
| 182 | ||
| 183 | ||
| 184 | static int | |
| 185 | swarn(struct mdoc *mdoc, enum mdoc_type type, | |
| 186 | int line, int pos, const struct mdoc_node *p) | |
| 187 | { | |
| 188 | const char *n, *t, *tt; | |
| 189 | ||
| 190 | n = t = "<root>"; | |
| 191 | tt = "block"; | |
| 192 | ||
| 193 | switch (type) { | |
| 194 | case (MDOC_BODY): | |
| 195 | tt = "multi-line"; | |
| 196 | break; | |
| 197 | case (MDOC_HEAD): | |
| 198 | tt = "line"; | |
| 199 | break; | |
| 200 | default: | |
| 201 | break; | |
| 202 | } | |
| 203 | ||
| 204 | switch (p->type) { | |
| 205 | case (MDOC_BLOCK): | |
| 206 | n = mdoc_macronames[p->tok]; | |
| 207 | t = "block"; | |
| 208 | break; | |
| 209 | case (MDOC_BODY): | |
| 210 | n = mdoc_macronames[p->tok]; | |
| 211 | t = "multi-line"; | |
| 212 | break; | |
| 213 | case (MDOC_HEAD): | |
| 214 | n = mdoc_macronames[p->tok]; | |
| 215 | t = "line"; | |
| 216 | break; | |
| 217 | default: | |
| 218 | break; | |
| 219 | } | |
| 220 | ||
| 221 | if ( ! (MDOC_IGN_SCOPE & mdoc->pflags)) | |
| 222 | return(mdoc_verr(mdoc, line, pos, | |
| 223 | "%s scope breaks %s scope of %s", | |
| 224 | tt, t, n)); | |
| 225 | return(mdoc_vwarn(mdoc, line, pos, | |
| 226 | "%s scope breaks %s scope of %s", | |
| 227 | tt, t, n)); | |
| 228 | } | |
| 229 | ||
| 230 | ||
| 231 | /* | |
| 232 | * This is called at the end of parsing. It must traverse up the tree, | |
| 233 | * closing out open [implicit] scopes. Obviously, open explicit scopes | |
| 234 | * are errors. | |
| 235 | */ | |
| 236 | int | |
| 237 | mdoc_macroend(struct mdoc *m) | |
| 238 | { | |
| 239 | struct mdoc_node *n; | |
| 240 | ||
| 241 | /* Scan for open explicit scopes. */ | |
| 242 | ||
| 243 | n = MDOC_VALID & m->last->flags ? m->last->parent : m->last; | |
| 244 | ||
| 245 | for ( ; n; n = n->parent) { | |
| 246 | if (MDOC_BLOCK != n->type) | |
| 247 | continue; | |
| 248 | if ( ! (MDOC_EXPLICIT & mdoc_macros[n->tok].flags)) | |
| 249 | continue; | |
| 250 | return(mdoc_nerr(m, n, EOPEN)); | |
| 251 | } | |
| 252 | ||
| 253 | /* Rewind to the first. */ | |
| 254 | ||
| 255 | return(rew_last(m, m->first)); | |
| 256 | } | |
| 257 | ||
| 258 | ||
| 259 | /* | |
| 260 | * Look up a macro from within a subsequent context. | |
| 261 | */ | |
| 262 | static int | |
| 263 | lookup(int from, const char *p) | |
| 264 | { | |
| 265 | /* FIXME: make -diag lists be un-PARSED. */ | |
| 266 | ||
| 267 | if ( ! (MDOC_PARSED & mdoc_macros[from].flags)) | |
| 268 | return(MDOC_MAX); | |
| 269 | return(lookup_raw(p)); | |
| 270 | } | |
| 271 | ||
| 272 | ||
| 273 | /* | |
| 274 | * Lookup a macro following the initial line macro. | |
| 275 | */ | |
| 276 | static int | |
| 277 | lookup_raw(const char *p) | |
| 278 | { | |
| 279 | int res; | |
| 280 | ||
| 281 | if (MDOC_MAX == (res = mdoc_hash_find(p))) | |
| 282 | return(MDOC_MAX); | |
| 283 | if (MDOC_CALLABLE & mdoc_macros[res].flags) | |
| 284 | return(res); | |
| 285 | return(MDOC_MAX); | |
| 286 | } | |
| 287 | ||
| 288 | ||
| 289 | static int | |
| 290 | rew_last(struct mdoc *mdoc, const struct mdoc_node *to) | |
| 291 | { | |
| 292 | ||
| 293 | assert(to); | |
| 294 | mdoc->next = MDOC_NEXT_SIBLING; | |
| 295 | ||
| 296 | /* LINTED */ | |
| 297 | while (mdoc->last != to) { | |
| 298 | if ( ! mdoc_valid_post(mdoc)) | |
| 299 | return(0); | |
| 300 | if ( ! mdoc_action_post(mdoc)) | |
| 301 | return(0); | |
| 302 | mdoc->last = mdoc->last->parent; | |
| 303 | assert(mdoc->last); | |
| 304 | } | |
| 305 | ||
| 306 | if ( ! mdoc_valid_post(mdoc)) | |
| 307 | return(0); | |
| 308 | return(mdoc_action_post(mdoc)); | |
| 309 | } | |
| 310 | ||
| 311 | ||
| 312 | /* | |
| 313 | * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its | |
| 314 | * matching pair. | |
| 315 | */ | |
| 316 | static int | |
| 317 | rew_alt(int tok) | |
| 318 | { | |
| 319 | switch (tok) { | |
| 320 | case (MDOC_Ac): | |
| 321 | return(MDOC_Ao); | |
| 322 | case (MDOC_Bc): | |
| 323 | return(MDOC_Bo); | |
| 324 | case (MDOC_Brc): | |
| 325 | return(MDOC_Bro); | |
| 326 | case (MDOC_Dc): | |
| 327 | return(MDOC_Do); | |
| 328 | case (MDOC_Ec): | |
| 329 | return(MDOC_Eo); | |
| 330 | case (MDOC_Ed): | |
| 331 | return(MDOC_Bd); | |
| 332 | case (MDOC_Ef): | |
| 333 | return(MDOC_Bf); | |
| 334 | case (MDOC_Ek): | |
| 335 | return(MDOC_Bk); | |
| 336 | case (MDOC_El): | |
| 337 | return(MDOC_Bl); | |
| 338 | case (MDOC_Fc): | |
| 339 | return(MDOC_Fo); | |
| 340 | case (MDOC_Oc): | |
| 341 | return(MDOC_Oo); | |
| 342 | case (MDOC_Pc): | |
| 343 | return(MDOC_Po); | |
| 344 | case (MDOC_Qc): | |
| 345 | return(MDOC_Qo); | |
| 346 | case (MDOC_Re): | |
| 347 | return(MDOC_Rs); | |
| 348 | case (MDOC_Sc): | |
| 349 | return(MDOC_So); | |
| 350 | case (MDOC_Xc): | |
| 351 | return(MDOC_Xo); | |
| 352 | default: | |
| 353 | break; | |
| 354 | } | |
| 355 | abort(); | |
| 356 | /* NOTREACHED */ | |
| 357 | } | |
| 358 | ||
| 359 | ||
| 360 | /* | |
| 361 | * Rewind rules. This indicates whether to stop rewinding | |
| 362 | * (REWIND_HALT) without touching our current scope, stop rewinding and | |
| 363 | * close our current scope (REWIND_REWIND), or continue (REWIND_NOHALT). | |
| 364 | * The scope-closing and so on occurs in the various rew_* routines. | |
| 365 | */ | |
| 366 | static int | |
| 367 | rew_dohalt(int tok, enum mdoc_type type, const struct mdoc_node *p) | |
| 368 | { | |
| 369 | ||
| 370 | if (MDOC_ROOT == p->type) | |
| 371 | return(REWIND_HALT); | |
| 372 | if (MDOC_VALID & p->flags) | |
| 373 | return(REWIND_NOHALT); | |
| 374 | ||
| 375 | switch (tok) { | |
| 376 | case (MDOC_Aq): | |
| 377 | /* FALLTHROUGH */ | |
| 378 | case (MDOC_Bq): | |
| 379 | /* FALLTHROUGH */ | |
| 380 | case (MDOC_Brq): | |
| 381 | /* FALLTHROUGH */ | |
| 382 | case (MDOC_D1): | |
| 383 | /* FALLTHROUGH */ | |
| 384 | case (MDOC_Dl): | |
| 385 | /* FALLTHROUGH */ | |
| 386 | case (MDOC_Dq): | |
| 387 | /* FALLTHROUGH */ | |
| 388 | case (MDOC_Op): | |
| 389 | /* FALLTHROUGH */ | |
| 390 | case (MDOC_Pq): | |
| 391 | /* FALLTHROUGH */ | |
| 392 | case (MDOC_Ql): | |
| 393 | /* FALLTHROUGH */ | |
| 394 | case (MDOC_Qq): | |
| 395 | /* FALLTHROUGH */ | |
| 396 | case (MDOC_Sq): | |
| 397 | assert(MDOC_TAIL != type); | |
| 398 | if (type == p->type && tok == p->tok) | |
| 399 | return(REWIND_REWIND); | |
| 400 | break; | |
| 401 | case (MDOC_It): | |
| 402 | assert(MDOC_TAIL != type); | |
| 403 | if (type == p->type && tok == p->tok) | |
| 404 | return(REWIND_REWIND); | |
| 405 | if (MDOC_BODY == p->type && MDOC_Bl == p->tok) | |
| 406 | return(REWIND_HALT); | |
| 407 | break; | |
| 408 | case (MDOC_Sh): | |
| 409 | if (type == p->type && tok == p->tok) | |
| 410 | return(REWIND_REWIND); | |
| 411 | break; | |
| 412 | case (MDOC_Nd): | |
| 413 | /* FALLTHROUGH */ | |
| 414 | case (MDOC_Ss): | |
| 415 | assert(MDOC_TAIL != type); | |
| 416 | if (type == p->type && tok == p->tok) | |
| 417 | return(REWIND_REWIND); | |
| 418 | if (MDOC_BODY == p->type && MDOC_Sh == p->tok) | |
| 419 | return(REWIND_HALT); | |
| 420 | break; | |
| 421 | case (MDOC_Ao): | |
| 422 | /* FALLTHROUGH */ | |
| 423 | case (MDOC_Bd): | |
| 424 | /* FALLTHROUGH */ | |
| 425 | case (MDOC_Bf): | |
| 426 | /* FALLTHROUGH */ | |
| 427 | case (MDOC_Bk): | |
| 428 | /* FALLTHROUGH */ | |
| 429 | case (MDOC_Bl): | |
| 430 | /* FALLTHROUGH */ | |
| 431 | case (MDOC_Bo): | |
| 432 | /* FALLTHROUGH */ | |
| 433 | case (MDOC_Bro): | |
| 434 | /* FALLTHROUGH */ | |
| 435 | case (MDOC_Do): | |
| 436 | /* FALLTHROUGH */ | |
| 437 | case (MDOC_Eo): | |
| 438 | /* FALLTHROUGH */ | |
| 439 | case (MDOC_Fo): | |
| 440 | /* FALLTHROUGH */ | |
| 441 | case (MDOC_Oo): | |
| 442 | /* FALLTHROUGH */ | |
| 443 | case (MDOC_Po): | |
| 444 | /* FALLTHROUGH */ | |
| 445 | case (MDOC_Qo): | |
| 446 | /* FALLTHROUGH */ | |
| 447 | case (MDOC_Rs): | |
| 448 | /* FALLTHROUGH */ | |
| 449 | case (MDOC_So): | |
| 450 | /* FALLTHROUGH */ | |
| 451 | case (MDOC_Xo): | |
| 452 | if (type == p->type && tok == p->tok) | |
| 453 | return(REWIND_REWIND); | |
| 454 | break; | |
| 455 | /* Multi-line explicit scope close. */ | |
| 456 | case (MDOC_Ac): | |
| 457 | /* FALLTHROUGH */ | |
| 458 | case (MDOC_Bc): | |
| 459 | /* FALLTHROUGH */ | |
| 460 | case (MDOC_Brc): | |
| 461 | /* FALLTHROUGH */ | |
| 462 | case (MDOC_Dc): | |
| 463 | /* FALLTHROUGH */ | |
| 464 | case (MDOC_Ec): | |
| 465 | /* FALLTHROUGH */ | |
| 466 | case (MDOC_Ed): | |
| 467 | /* FALLTHROUGH */ | |
| 468 | case (MDOC_Ek): | |
| 469 | /* FALLTHROUGH */ | |
| 470 | case (MDOC_El): | |
| 471 | /* FALLTHROUGH */ | |
| 472 | case (MDOC_Fc): | |
| 473 | /* FALLTHROUGH */ | |
| 474 | case (MDOC_Ef): | |
| 475 | /* FALLTHROUGH */ | |
| 476 | case (MDOC_Oc): | |
| 477 | /* FALLTHROUGH */ | |
| 478 | case (MDOC_Pc): | |
| 479 | /* FALLTHROUGH */ | |
| 480 | case (MDOC_Qc): | |
| 481 | /* FALLTHROUGH */ | |
| 482 | case (MDOC_Re): | |
| 483 | /* FALLTHROUGH */ | |
| 484 | case (MDOC_Sc): | |
| 485 | /* FALLTHROUGH */ | |
| 486 | case (MDOC_Xc): | |
| 487 | if (type == p->type && rew_alt(tok) == p->tok) | |
| 488 | return(REWIND_REWIND); | |
| 489 | break; | |
| 490 | default: | |
| 491 | abort(); | |
| 492 | /* NOTREACHED */ | |
| 493 | } | |
| 494 | ||
| 495 | return(REWIND_NOHALT); | |
| 496 | } | |
| 497 | ||
| 498 | ||
| 499 | /* | |
| 500 | * See if we can break an encountered scope (the rew_dohalt has returned | |
| 501 | * REWIND_NOHALT). | |
| 502 | */ | |
| 503 | static int | |
| 504 | rew_dobreak(int tok, const struct mdoc_node *p) | |
| 505 | { | |
| 506 | ||
| 507 | assert(MDOC_ROOT != p->type); | |
| 508 | if (MDOC_ELEM == p->type) | |
| 509 | return(1); | |
| 510 | if (MDOC_TEXT == p->type) | |
| 511 | return(1); | |
| 512 | if (MDOC_VALID & p->flags) | |
| 513 | return(1); | |
| 514 | ||
| 515 | switch (tok) { | |
| 516 | case (MDOC_It): | |
| 517 | return(MDOC_It == p->tok); | |
| 518 | case (MDOC_Nd): | |
| 519 | return(MDOC_Nd == p->tok); | |
| 520 | case (MDOC_Ss): | |
| 521 | return(MDOC_Ss == p->tok); | |
| 522 | case (MDOC_Sh): | |
| 523 | if (MDOC_Nd == p->tok) | |
| 524 | return(1); | |
| 525 | if (MDOC_Ss == p->tok) | |
| 526 | return(1); | |
| 527 | return(MDOC_Sh == p->tok); | |
| 528 | case (MDOC_El): | |
| 529 | if (MDOC_It == p->tok) | |
| 530 | return(1); | |
| 531 | break; | |
| 532 | case (MDOC_Oc): | |
| 533 | /* XXX - experimental! */ | |
| 534 | if (MDOC_Op == p->tok) | |
| 535 | return(1); | |
| 536 | break; | |
| 537 | default: | |
| 538 | break; | |
| 539 | } | |
| 540 | ||
| 541 | if (MDOC_EXPLICIT & mdoc_macros[tok].flags) | |
| 542 | return(p->tok == rew_alt(tok)); | |
| 543 | else if (MDOC_BLOCK == p->type) | |
| 544 | return(1); | |
| 545 | ||
| 546 | return(tok == p->tok); | |
| 547 | } | |
| 548 | ||
| 549 | ||
| 550 | static int | |
| 551 | rew_elem(struct mdoc *mdoc, int tok) | |
| 552 | { | |
| 553 | struct mdoc_node *n; | |
| 554 | ||
| 555 | n = mdoc->last; | |
| 556 | if (MDOC_ELEM != n->type) | |
| 557 | n = n->parent; | |
| 558 | assert(MDOC_ELEM == n->type); | |
| 559 | assert(tok == n->tok); | |
| 560 | ||
| 561 | return(rew_last(mdoc, n)); | |
| 562 | } | |
| 563 | ||
| 564 | ||
| 565 | static int | |
| 566 | rew_sub(enum mdoc_type t, struct mdoc *m, | |
| 567 | int tok, int line, int ppos) | |
| 568 | { | |
| 569 | struct mdoc_node *n; | |
| 570 | int c; | |
| 571 | ||
| 572 | /* LINTED */ | |
| 573 | for (n = m->last; n; n = n->parent) { | |
| 574 | c = rew_dohalt(tok, t, n); | |
| 575 | if (REWIND_HALT == c) { | |
| 576 | if (MDOC_BLOCK != t) | |
| 577 | return(1); | |
| 578 | if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) | |
| 579 | return(1); | |
| 580 | return(mdoc_perr(m, line, ppos, ENOCTX)); | |
| 581 | } | |
| 582 | if (REWIND_REWIND == c) | |
| 583 | break; | |
| 584 | else if (rew_dobreak(tok, n)) | |
| 585 | continue; | |
| 586 | if ( ! swarn(m, t, line, ppos, n)) | |
| 587 | return(0); | |
| 588 | } | |
| 589 | ||
| 590 | assert(n); | |
| 591 | return(rew_last(m, n)); | |
| 592 | } | |
| 593 | ||
| 594 | ||
| 595 | static int | |
| 596 | append_delims(struct mdoc *mdoc, int line, int *pos, char *buf) | |
| 597 | { | |
| 598 | int c, lastarg; | |
| 599 | char *p; | |
| 600 | ||
| 601 | if (0 == buf[*pos]) | |
| 602 | return(1); | |
| 603 | ||
| 604 | for (;;) { | |
| 605 | lastarg = *pos; | |
| 606 | c = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p); | |
| 607 | assert(ARGS_PHRASE != c); | |
| 608 | ||
| 609 | if (ARGS_ERROR == c) | |
| 610 | return(0); | |
| 611 | else if (ARGS_EOLN == c) | |
| 612 | break; | |
| 613 | assert(mdoc_isdelim(p)); | |
| 614 | if ( ! mdoc_word_alloc(mdoc, line, lastarg, p)) | |
| 615 | return(0); | |
| 616 | } | |
| 617 | ||
| 618 | return(1); | |
| 619 | } | |
| 620 | ||
| 621 | ||
| 622 | /* | |
| 623 | * Close out block partial/full explicit. | |
| 624 | */ | |
| 625 | static int | |
| 626 | blk_exp_close(MACRO_PROT_ARGS) | |
| 627 | { | |
| 628 | int j, c, lastarg, maxargs, flushed; | |
| 629 | char *p; | |
| 630 | ||
| 631 | switch (tok) { | |
| 632 | case (MDOC_Ec): | |
| 633 | maxargs = 1; | |
| 634 | break; | |
| 635 | default: | |
| 636 | maxargs = 0; | |
| 637 | break; | |
| 638 | } | |
| 639 | ||
| 640 | if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) { | |
| 641 | if (buf[*pos]) | |
| 642 | if ( ! mdoc_pwarn(m, line, ppos, ENOLINE)) | |
| 643 | return(0); | |
| 644 | ||
| 645 | if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) | |
| 646 | return(0); | |
| 647 | return(rew_sub(MDOC_BLOCK, m, tok, line, ppos)); | |
| 648 | } | |
| 649 | ||
| 650 | if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) | |
| 651 | return(0); | |
| 652 | ||
| 653 | if (maxargs > 0) | |
| 654 | if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok))) | |
| 655 | return(0); | |
| 656 | ||
| 657 | for (flushed = j = 0; ; j++) { | |
| 658 | lastarg = *pos; | |
| 659 | ||
| 660 | if (j == maxargs && ! flushed) { | |
| 661 | if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) | |
| 662 | return(0); | |
| 663 | flushed = 1; | |
| 664 | } | |
| 665 | ||
| 666 | c = mdoc_args(m, line, pos, buf, tok, &p); | |
| 667 | ||
| 668 | if (ARGS_ERROR == c) | |
| 669 | return(0); | |
| 670 | if (ARGS_PUNCT == c) | |
| 671 | break; | |
| 672 | if (ARGS_EOLN == c) | |
| 673 | break; | |
| 674 | ||
| 675 | if (MDOC_MAX != (c = lookup(tok, p))) { | |
| 676 | if ( ! flushed) { | |
| 677 | if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) | |
| 678 | return(0); | |
| 679 | flushed = 1; | |
| 680 | } | |
| 681 | if ( ! mdoc_macro(m, c, line, lastarg, pos, buf)) | |
| 682 | return(0); | |
| 683 | break; | |
| 684 | } | |
| 685 | ||
| 686 | if ( ! mdoc_word_alloc(m, line, lastarg, p)) | |
| 687 | return(0); | |
| 688 | } | |
| 689 | ||
| 690 | if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) | |
| 691 | return(0); | |
| 692 | ||
| 693 | if (ppos > 1) | |
| 694 | return(1); | |
| 695 | return(append_delims(m, line, pos, buf)); | |
| 696 | } | |
| 697 | ||
| 698 | ||
| 699 | static int | |
| 700 | in_line(MACRO_PROT_ARGS) | |
| 701 | { | |
| 702 | int la, lastpunct, c, w, cnt, d, nc; | |
| 703 | struct mdoc_arg *arg; | |
| 704 | char *p; | |
| 705 | ||
| 706 | /* | |
| 707 | * Whether we allow ignored elements (those without content, | |
| 708 | * usually because of reserved words) to squeak by. | |
| 709 | */ | |
| 710 | switch (tok) { | |
| 711 | case (MDOC_An): | |
| 712 | /* FALLTHROUGH */ | |
| 713 | case (MDOC_Ar): | |
| 714 | /* FALLTHROUGH */ | |
| 715 | case (MDOC_Fl): | |
| 716 | /* FALLTHROUGH */ | |
| 717 | case (MDOC_Lk): | |
| 718 | /* FALLTHROUGH */ | |
| 719 | case (MDOC_Nm): | |
| 720 | /* FALLTHROUGH */ | |
| 721 | case (MDOC_Pa): | |
| 722 | nc = 1; | |
| 723 | break; | |
| 724 | default: | |
| 725 | nc = 0; | |
| 726 | break; | |
| 727 | } | |
| 728 | ||
| 729 | for (arg = NULL;; ) { | |
| 730 | la = *pos; | |
| 731 | c = mdoc_argv(m, line, tok, &arg, pos, buf); | |
| 732 | ||
| 733 | if (ARGV_WORD == c) { | |
| 734 | *pos = la; | |
| 735 | break; | |
| 736 | } | |
| 737 | if (ARGV_EOLN == c) | |
| 738 | break; | |
| 739 | if (ARGV_ARG == c) | |
| 740 | continue; | |
| 741 | ||
| 742 | mdoc_argv_free(arg); | |
| 743 | return(0); | |
| 744 | } | |
| 745 | ||
| 746 | for (cnt = 0, lastpunct = 1;; ) { | |
| 747 | la = *pos; | |
| 748 | w = mdoc_args(m, line, pos, buf, tok, &p); | |
| 749 | ||
| 750 | if (ARGS_ERROR == w) | |
| 751 | return(0); | |
| 752 | if (ARGS_EOLN == w) | |
| 753 | break; | |
| 754 | if (ARGS_PUNCT == w) | |
| 755 | break; | |
| 756 | ||
| 757 | /* Quoted words shouldn't be looked-up. */ | |
| 758 | ||
| 759 | c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p); | |
| 760 | ||
| 761 | /* | |
| 762 | * In this case, we've located a submacro and must | |
| 763 | * execute it. Close out scope, if open. If no | |
| 764 | * elements have been generated, either create one (nc) | |
| 765 | * or raise a warning. | |
| 766 | */ | |
| 767 | ||
| 768 | if (MDOC_MAX != c) { | |
| 769 | if (0 == lastpunct && ! rew_elem(m, tok)) | |
| 770 | return(0); | |
| 771 | if (nc && 0 == cnt) { | |
| 772 | if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) | |
| 773 | return(0); | |
| 774 | if ( ! rew_last(m, m->last)) | |
| 775 | return(0); | |
| 776 | } else if ( ! nc && 0 == cnt) { | |
| 777 | mdoc_argv_free(arg); | |
| 778 | if ( ! mdoc_pwarn(m, line, ppos, EIGNE)) | |
| 779 | return(0); | |
| 780 | } | |
| 781 | c = mdoc_macro(m, c, line, la, pos, buf); | |
| 782 | if (0 == c) | |
| 783 | return(0); | |
| 784 | if (ppos > 1) | |
| 785 | return(1); | |
| 786 | return(append_delims(m, line, pos, buf)); | |
| 787 | } | |
| 788 | ||
| 789 | /* | |
| 790 | * Non-quote-enclosed punctuation. Set up our scope, if | |
| 791 | * a word; rewind the scope, if a delimiter; then append | |
| 792 | * the word. | |
| 793 | */ | |
| 794 | ||
| 795 | d = mdoc_isdelim(p); | |
| 796 | ||
| 797 | if (ARGS_QWORD != w && d) { | |
| 798 | if (0 == lastpunct && ! rew_elem(m, tok)) | |
| 799 | return(0); | |
| 800 | lastpunct = 1; | |
| 801 | } else if (lastpunct) { | |
| 802 | if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) | |
| 803 | return(0); | |
| 804 | lastpunct = 0; | |
| 805 | } | |
| 806 | ||
| 807 | if ( ! d) | |
| 808 | cnt++; | |
| 809 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 810 | return(0); | |
| 811 | } | |
| 812 | ||
| 813 | if (0 == lastpunct && ! rew_elem(m, tok)) | |
| 814 | return(0); | |
| 815 | ||
| 816 | /* | |
| 817 | * If no elements have been collected and we're allowed to have | |
| 818 | * empties (nc), open a scope and close it out. Otherwise, | |
| 819 | * raise a warning. | |
| 820 | * | |
| 821 | */ | |
| 822 | if (nc && 0 == cnt) { | |
| 823 | if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) | |
| 824 | return(0); | |
| 825 | if ( ! rew_last(m, m->last)) | |
| 826 | return(0); | |
| 827 | } else if ( ! nc && 0 == cnt) { | |
| 828 | mdoc_argv_free(arg); | |
| 829 | if ( ! mdoc_pwarn(m, line, ppos, EIGNE)) | |
| 830 | return(0); | |
| 831 | } | |
| 832 | ||
| 833 | if (ppos > 1) | |
| 834 | return(1); | |
| 835 | return(append_delims(m, line, pos, buf)); | |
| 836 | } | |
| 837 | ||
| 838 | ||
| 839 | static int | |
| 840 | blk_full(MACRO_PROT_ARGS) | |
| 841 | { | |
| 842 | int c, lastarg, reopen, dohead; | |
| 843 | struct mdoc_arg *arg; | |
| 844 | char *p; | |
| 845 | ||
| 846 | /* | |
| 847 | * Whether to process a block-head section. If this is | |
| 848 | * non-zero, then a head will be opened for all line arguments. | |
| 849 | * If not, then the head will always be empty and only a body | |
| 850 | * will be opened, which will stay open at the eoln. | |
| 851 | */ | |
| 852 | ||
| 853 | switch (tok) { | |
| 854 | case (MDOC_Nd): | |
| 855 | dohead = 0; | |
| 856 | break; | |
| 857 | default: | |
| 858 | dohead = 1; | |
| 859 | break; | |
| 860 | } | |
| 861 | ||
| 862 | if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) { | |
| 863 | if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) | |
| 864 | return(0); | |
| 865 | if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) | |
| 866 | return(0); | |
| 867 | } | |
| 868 | ||
| 869 | for (arg = NULL;; ) { | |
| 870 | lastarg = *pos; | |
| 871 | c = mdoc_argv(m, line, tok, &arg, pos, buf); | |
| 872 | ||
| 873 | if (ARGV_WORD == c) { | |
| 874 | *pos = lastarg; | |
| 875 | break; | |
| 876 | } | |
| 877 | ||
| 878 | if (ARGV_EOLN == c) | |
| 879 | break; | |
| 880 | if (ARGV_ARG == c) | |
| 881 | continue; | |
| 882 | ||
| 883 | mdoc_argv_free(arg); | |
| 884 | return(0); | |
| 885 | } | |
| 886 | ||
| 887 | if ( ! mdoc_block_alloc(m, line, ppos, tok, arg)) | |
| 888 | return(0); | |
| 889 | ||
| 890 | if (0 == buf[*pos]) { | |
| 891 | if ( ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 892 | return(0); | |
| 893 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 894 | return(0); | |
| 895 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 896 | return(0); | |
| 897 | return(1); | |
| 898 | } | |
| 899 | ||
| 900 | if ( ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 901 | return(0); | |
| 902 | ||
| 903 | /* Immediately close out head and enter body, if applicable. */ | |
| 904 | ||
| 905 | if (0 == dohead) { | |
| 906 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 907 | return(0); | |
| 908 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 909 | return(0); | |
| 910 | } | |
| 911 | ||
| 912 | for (reopen = 0;; ) { | |
| 913 | lastarg = *pos; | |
| 914 | c = mdoc_args(m, line, pos, buf, tok, &p); | |
| 915 | ||
| 916 | if (ARGS_ERROR == c) | |
| 917 | return(0); | |
| 918 | if (ARGS_EOLN == c) | |
| 919 | break; | |
| 920 | if (ARGS_PHRASE == c) { | |
| 921 | assert(dohead); | |
| 922 | if (reopen && ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 923 | return(0); | |
| 924 | /* | |
| 925 | * Phrases are self-contained macro phrases used | |
| 926 | * in the columnar output of a macro. They need | |
| 927 | * special handling. | |
| 928 | */ | |
| 929 | if ( ! phrase(m, line, lastarg, buf)) | |
| 930 | return(0); | |
| 931 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 932 | return(0); | |
| 933 | ||
| 934 | reopen = 1; | |
| 935 | continue; | |
| 936 | } | |
| 937 | ||
| 938 | if (MDOC_MAX == (c = lookup(tok, p))) { | |
| 939 | if ( ! mdoc_word_alloc(m, line, lastarg, p)) | |
| 940 | return(0); | |
| 941 | continue; | |
| 942 | } | |
| 943 | ||
| 944 | if ( ! mdoc_macro(m, c, line, lastarg, pos, buf)) | |
| 945 | return(0); | |
| 946 | break; | |
| 947 | } | |
| 948 | ||
| 949 | if (1 == ppos && ! append_delims(m, line, pos, buf)) | |
| 950 | return(0); | |
| 951 | ||
| 952 | /* If the body's already open, then just return. */ | |
| 953 | if (0 == dohead) | |
| 954 | return(1); | |
| 955 | ||
| 956 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 957 | return(0); | |
| 958 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 959 | return(0); | |
| 960 | ||
| 961 | return(1); | |
| 962 | } | |
| 963 | ||
| 964 | ||
| 965 | static int | |
| 966 | blk_part_imp(MACRO_PROT_ARGS) | |
| 967 | { | |
| 968 | int la, c; | |
| 969 | char *p; | |
| 970 | struct mdoc_node *blk, *body, *n; | |
| 971 | ||
| 972 | /* If applicable, close out prior scopes. */ | |
| 973 | ||
| 974 | if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL)) | |
| 975 | return(0); | |
| 976 | /* Saved for later close-out. */ | |
| 977 | blk = m->last; | |
| 978 | if ( ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 979 | return(0); | |
| 980 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 981 | return(0); | |
| 982 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 983 | return(0); | |
| 984 | /* Saved for later close-out. */ | |
| 985 | body = m->last; | |
| 986 | ||
| 987 | /* Body argument processing. */ | |
| 988 | ||
| 989 | for (;;) { | |
| 990 | la = *pos; | |
| 991 | c = mdoc_args(m, line, pos, buf, tok, &p); | |
| 992 | assert(ARGS_PHRASE != c); | |
| 993 | ||
| 994 | if (ARGS_ERROR == c) | |
| 995 | return(0); | |
| 996 | if (ARGS_PUNCT == c) | |
| 997 | break; | |
| 998 | if (ARGS_EOLN == c) | |
| 999 | break; | |
| 1000 | ||
| 1001 | if (MDOC_MAX == (c = lookup(tok, p))) { | |
| 1002 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 1003 | return(0); | |
| 1004 | continue; | |
| 1005 | } | |
| 1006 | ||
| 1007 | if ( ! mdoc_macro(m, c, line, la, pos, buf)) | |
| 1008 | return(0); | |
| 1009 | break; | |
| 1010 | } | |
| 1011 | ||
| 1012 | /* | |
| 1013 | * If we can't rewind to our body, then our scope has already | |
| 1014 | * been closed by another macro (like `Oc' closing `Op'). This | |
| 1015 | * is ugly behaviour nodding its head to OpenBSD's overwhelming | |
| 1016 | * crufty use of `Op' breakage--XXX, deprecate in time. | |
| 1017 | */ | |
| 1018 | for (n = m->last; n; n = n->parent) | |
| 1019 | if (body == n) | |
| 1020 | break; | |
| 1021 | if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK)) | |
| 1022 | return(0); | |
| 1023 | if (n && ! rew_last(m, body)) | |
| 1024 | return(0); | |
| 1025 | ||
| 1026 | /* Standard appending of delimiters. */ | |
| 1027 | ||
| 1028 | if (1 == ppos && ! append_delims(m, line, pos, buf)) | |
| 1029 | return(0); | |
| 1030 | ||
| 1031 | /* Rewind scope, if applicable. */ | |
| 1032 | ||
| 1033 | if (n && ! rew_last(m, blk)) | |
| 1034 | return(0); | |
| 1035 | ||
| 1036 | return(1); | |
| 1037 | } | |
| 1038 | ||
| 1039 | ||
| 1040 | static int | |
| 1041 | blk_part_exp(MACRO_PROT_ARGS) | |
| 1042 | { | |
| 1043 | int la, flushed, j, c, maxargs; | |
| 1044 | char *p; | |
| 1045 | ||
| 1046 | /* Number of head arguments. Only `Eo' has these, */ | |
| 1047 | ||
| 1048 | switch (tok) { | |
| 1049 | case (MDOC_Eo): | |
| 1050 | maxargs = 1; | |
| 1051 | break; | |
| 1052 | default: | |
| 1053 | maxargs = 0; | |
| 1054 | break; | |
| 1055 | } | |
| 1056 | ||
| 1057 | /* Begin the block scope. */ | |
| 1058 | ||
| 1059 | if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL)) | |
| 1060 | return(0); | |
| 1061 | ||
| 1062 | /* | |
| 1063 | * If no head arguments, open and then close out a head, noting | |
| 1064 | * that we've flushed our terms. `flushed' means that we've | |
| 1065 | * flushed out the head and the body is open. | |
| 1066 | */ | |
| 1067 | ||
| 1068 | if (0 == maxargs) { | |
| 1069 | if ( ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 1070 | return(0); | |
| 1071 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 1072 | return(0); | |
| 1073 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 1074 | return(0); | |
| 1075 | flushed = 1; | |
| 1076 | } else { | |
| 1077 | if ( ! mdoc_head_alloc(m, line, ppos, tok)) | |
| 1078 | return(0); | |
| 1079 | flushed = 0; | |
| 1080 | } | |
| 1081 | ||
| 1082 | /* Process the head/head+body line arguments. */ | |
| 1083 | ||
| 1084 | for (j = 0; ; j++) { | |
| 1085 | la = *pos; | |
| 1086 | if (j == maxargs && ! flushed) { | |
| 1087 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 1088 | return(0); | |
| 1089 | flushed = 1; | |
| 1090 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 1091 | return(0); | |
| 1092 | } | |
| 1093 | ||
| 1094 | c = mdoc_args(m, line, pos, buf, tok, &p); | |
| 1095 | assert(ARGS_PHRASE != c); | |
| 1096 | ||
| 1097 | if (ARGS_ERROR == c) | |
| 1098 | return(0); | |
| 1099 | if (ARGS_PUNCT == c) | |
| 1100 | break; | |
| 1101 | if (ARGS_EOLN == c) | |
| 1102 | break; | |
| 1103 | ||
| 1104 | if (MDOC_MAX != (c = lookup(tok, p))) { | |
| 1105 | if ( ! flushed) { | |
| 1106 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 1107 | return(0); | |
| 1108 | flushed = 1; | |
| 1109 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 1110 | return(0); | |
| 1111 | } | |
| 1112 | if ( ! mdoc_macro(m, c, line, la, pos, buf)) | |
| 1113 | return(0); | |
| 1114 | break; | |
| 1115 | } | |
| 1116 | ||
| 1117 | if ( ! flushed && mdoc_isdelim(p)) { | |
| 1118 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 1119 | return(0); | |
| 1120 | flushed = 1; | |
| 1121 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 1122 | return(0); | |
| 1123 | } | |
| 1124 | ||
| 1125 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 1126 | return(0); | |
| 1127 | } | |
| 1128 | ||
| 1129 | /* Close the head and open the body, if applicable. */ | |
| 1130 | ||
| 1131 | if ( ! flushed) { | |
| 1132 | if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) | |
| 1133 | return(0); | |
| 1134 | if ( ! mdoc_body_alloc(m, line, ppos, tok)) | |
| 1135 | return(0); | |
| 1136 | } | |
| 1137 | ||
| 1138 | /* Standard appending of delimiters. */ | |
| 1139 | ||
| 1140 | if (ppos > 1) | |
| 1141 | return(1); | |
| 1142 | return(append_delims(m, line, pos, buf)); | |
| 1143 | } | |
| 1144 | ||
| 1145 | ||
| 1146 | static int | |
| 1147 | in_line_argn(MACRO_PROT_ARGS) | |
| 1148 | { | |
| 1149 | int la, flushed, j, c, maxargs; | |
| 1150 | struct mdoc_arg *arg; | |
| 1151 | char *p; | |
| 1152 | ||
| 1153 | /* Fixed maximum arguments per macro, if applicable. */ | |
| 1154 | ||
| 1155 | switch (tok) { | |
| 1156 | case (MDOC_Ap): | |
| 1157 | /* FALLTHROUGH */ | |
| 1158 | case (MDOC_No): | |
| 1159 | /* FALLTHROUGH */ | |
| 1160 | case (MDOC_Ns): | |
| 1161 | /* FALLTHROUGH */ | |
| 1162 | case (MDOC_Ux): | |
| 1163 | maxargs = 0; | |
| 1164 | break; | |
| 1165 | default: | |
| 1166 | maxargs = 1; | |
| 1167 | break; | |
| 1168 | } | |
| 1169 | ||
| 1170 | /* Macro argument processing. */ | |
| 1171 | ||
| 1172 | for (arg = NULL;; ) { | |
| 1173 | la = *pos; | |
| 1174 | c = mdoc_argv(m, line, tok, &arg, pos, buf); | |
| 1175 | ||
| 1176 | if (ARGV_WORD == c) { | |
| 1177 | *pos = la; | |
| 1178 | break; | |
| 1179 | } | |
| 1180 | ||
| 1181 | if (ARGV_EOLN == c) | |
| 1182 | break; | |
| 1183 | if (ARGV_ARG == c) | |
| 1184 | continue; | |
| 1185 | ||
| 1186 | mdoc_argv_free(arg); | |
| 1187 | return(0); | |
| 1188 | } | |
| 1189 | ||
| 1190 | /* Open the element scope. */ | |
| 1191 | ||
| 1192 | if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) | |
| 1193 | return(0); | |
| 1194 | ||
| 1195 | /* Process element arguments. */ | |
| 1196 | ||
| 1197 | for (flushed = j = 0; ; j++) { | |
| 1198 | la = *pos; | |
| 1199 | ||
| 1200 | if (j == maxargs && ! flushed) { | |
| 1201 | if ( ! rew_elem(m, tok)) | |
| 1202 | return(0); | |
| 1203 | flushed = 1; | |
| 1204 | } | |
| 1205 | ||
| 1206 | c = mdoc_args(m, line, pos, buf, tok, &p); | |
| 1207 | ||
| 1208 | if (ARGS_ERROR == c) | |
| 1209 | return(0); | |
| 1210 | if (ARGS_PUNCT == c) | |
| 1211 | break; | |
| 1212 | if (ARGS_EOLN == c) | |
| 1213 | break; | |
| 1214 | ||
| 1215 | if (MDOC_MAX != (c = lookup(tok, p))) { | |
| 1216 | if ( ! flushed && ! rew_elem(m, tok)) | |
| 1217 | return(0); | |
| 1218 | flushed = 1; | |
| 1219 | if ( ! mdoc_macro(m, c, line, la, pos, buf)) | |
| 1220 | return(0); | |
| 1221 | break; | |
| 1222 | } | |
| 1223 | ||
| 1224 | if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) && | |
| 1225 | ! flushed && mdoc_isdelim(p)) { | |
| 1226 | if ( ! rew_elem(m, tok)) | |
| 1227 | return(0); | |
| 1228 | flushed = 1; | |
| 1229 | } | |
| 1230 | ||
| 1231 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 1232 | return(0); | |
| 1233 | } | |
| 1234 | ||
| 1235 | /* Close out and append delimiters. */ | |
| 1236 | ||
| 1237 | if ( ! flushed && ! rew_elem(m, tok)) | |
| 1238 | return(0); | |
| 1239 | ||
| 1240 | if (ppos > 1) | |
| 1241 | return(1); | |
| 1242 | return(append_delims(m, line, pos, buf)); | |
| 1243 | } | |
| 1244 | ||
| 1245 | ||
| 1246 | static int | |
| 1247 | in_line_eoln(MACRO_PROT_ARGS) | |
| 1248 | { | |
| 1249 | int c, w, la; | |
| 1250 | struct mdoc_arg *arg; | |
| 1251 | char *p; | |
| 1252 | ||
| 1253 | assert( ! (MDOC_PARSED & mdoc_macros[tok].flags)); | |
| 1254 | ||
| 1255 | /* Parse macro arguments. */ | |
| 1256 | ||
| 1257 | for (arg = NULL; ; ) { | |
| 1258 | la = *pos; | |
| 1259 | c = mdoc_argv(m, line, tok, &arg, pos, buf); | |
| 1260 | ||
| 1261 | if (ARGV_WORD == c) { | |
| 1262 | *pos = la; | |
| 1263 | break; | |
| 1264 | } | |
| 1265 | if (ARGV_EOLN == c) | |
| 1266 | break; | |
| 1267 | if (ARGV_ARG == c) | |
| 1268 | continue; | |
| 1269 | ||
| 1270 | mdoc_argv_free(arg); | |
| 1271 | return(0); | |
| 1272 | } | |
| 1273 | ||
| 1274 | /* Open element scope. */ | |
| 1275 | ||
| 1276 | if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) | |
| 1277 | return(0); | |
| 1278 | ||
| 1279 | /* Parse argument terms. */ | |
| 1280 | ||
| 1281 | for (;;) { | |
| 1282 | la = *pos; | |
| 1283 | w = mdoc_args(m, line, pos, buf, tok, &p); | |
| 1284 | ||
| 1285 | if (ARGS_ERROR == w) | |
| 1286 | return(0); | |
| 1287 | if (ARGS_EOLN == w) | |
| 1288 | break; | |
| 1289 | ||
| 1290 | c = ARGS_QWORD == w ? MDOC_MAX : lookup(tok, p); | |
| 1291 | ||
| 1292 | if (MDOC_MAX != c) { | |
| 1293 | if ( ! rew_elem(m, tok)) | |
| 1294 | return(0); | |
| 1295 | return(mdoc_macro(m, c, line, la, pos, buf)); | |
| 1296 | } | |
| 1297 | ||
| 1298 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 1299 | return(0); | |
| 1300 | } | |
| 1301 | ||
| 1302 | /* Close out (no delimiters). */ | |
| 1303 | ||
| 1304 | return(rew_elem(m, tok)); | |
| 1305 | } | |
| 1306 | ||
| 1307 | ||
| 1308 | /* ARGSUSED */ | |
| 1309 | static int | |
| 1310 | obsolete(MACRO_PROT_ARGS) | |
| 1311 | { | |
| 1312 | ||
| 1313 | return(mdoc_pwarn(m, line, ppos, EOBS)); | |
| 1314 | } | |
| 1315 | ||
| 1316 | ||
| 1317 | /* | |
| 1318 | * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs. | |
| 1319 | * They're unusual because they're basically free-form text until a | |
| 1320 | * macro is encountered. | |
| 1321 | */ | |
| 1322 | static int | |
| 1323 | phrase(struct mdoc *m, int line, int ppos, char *buf) | |
| 1324 | { | |
| 1325 | int c, w, la, pos; | |
| 1326 | char *p; | |
| 1327 | ||
| 1328 | for (pos = ppos; ; ) { | |
| 1329 | la = pos; | |
| 1330 | ||
| 1331 | /* Note: no calling context! */ | |
| 1332 | w = mdoc_zargs(m, line, &pos, buf, 0, &p); | |
| 1333 | ||
| 1334 | if (ARGS_ERROR == w) | |
| 1335 | return(0); | |
| 1336 | if (ARGS_EOLN == w) | |
| 1337 | break; | |
| 1338 | ||
| 1339 | c = ARGS_QWORD == w ? MDOC_MAX : lookup_raw(p); | |
| 1340 | ||
| 1341 | if (MDOC_MAX != c) { | |
| 1342 | if ( ! mdoc_macro(m, c, line, la, &pos, buf)) | |
| 1343 | return(0); | |
| 1344 | return(append_delims(m, line, &pos, buf)); | |
| 1345 | } | |
| 1346 | ||
| 1347 | if ( ! mdoc_word_alloc(m, line, la, p)) | |
| 1348 | return(0); | |
| 1349 | } | |
| 1350 | ||
| 1351 | return(1); | |
| 1352 | } |