1 /* $OpenBSD: mdoc_macro.c,v 1.166 2017/01/10 13:46:53 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2010, 2012-2016 Ingo Schwarze <schwarze@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 #include <sys/types.h> 19 20 #include <assert.h> 21 #include <ctype.h> 22 #include <stdlib.h> 23 #include <stdio.h> 24 #include <string.h> 25 #include <time.h> 26 27 #include "mandoc.h" 28 #include "roff.h" 29 #include "mdoc.h" 30 #include "libmandoc.h" 31 #include "roff_int.h" 32 #include "libmdoc.h" 33 34 static void blk_full(MACRO_PROT_ARGS); 35 static void blk_exp_close(MACRO_PROT_ARGS); 36 static void blk_part_exp(MACRO_PROT_ARGS); 37 static void blk_part_imp(MACRO_PROT_ARGS); 38 static void ctx_synopsis(MACRO_PROT_ARGS); 39 static void in_line_eoln(MACRO_PROT_ARGS); 40 static void in_line_argn(MACRO_PROT_ARGS); 41 static void in_line(MACRO_PROT_ARGS); 42 static void phrase_ta(MACRO_PROT_ARGS); 43 44 static void append_delims(struct roff_man *, int, int *, char *); 45 static void dword(struct roff_man *, int, int, const char *, 46 enum mdelim, int); 47 static int find_pending(struct roff_man *, int, int, int, 48 struct roff_node *); 49 static int lookup(struct roff_man *, int, int, int, const char *); 50 static int macro_or_word(MACRO_PROT_ARGS, int); 51 static int parse_rest(struct roff_man *, int, int, int *, char *); 52 static int rew_alt(int); 53 static void rew_elem(struct roff_man *, int); 54 static void rew_last(struct roff_man *, const struct roff_node *); 55 static void rew_pending(struct roff_man *, 56 const struct roff_node *); 57 58 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = { 59 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */ 60 { in_line_eoln, MDOC_PROLOGUE }, /* Dd */ 61 { in_line_eoln, MDOC_PROLOGUE }, /* Dt */ 62 { in_line_eoln, MDOC_PROLOGUE }, /* Os */ 63 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */ 64 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */ 65 { in_line_eoln, 0 }, /* Pp */ 66 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */ 67 { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */ 68 { blk_full, MDOC_EXPLICIT }, /* Bd */ 69 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */ 70 { blk_full, MDOC_EXPLICIT }, /* Bl */ 71 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */ 72 { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */ 73 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */ 74 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */ 75 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */ 76 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */ 77 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */ 78 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */ 79 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */ 80 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */ 81 { in_line_eoln, 0 }, /* Ex */ 82 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */ 83 { in_line_eoln, 0 }, /* Fd */ 84 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */ 85 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */ 86 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */ 87 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */ 88 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */ 89 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */ 90 { blk_full, MDOC_JOIN }, /* Nd */ 91 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */ 92 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */ 93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */ 94 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */ 95 { in_line_eoln, 0 }, /* Rv */ 96 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */ 97 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */ 98 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */ 99 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */ 100 { in_line_eoln, MDOC_JOIN }, /* %A */ 101 { in_line_eoln, MDOC_JOIN }, /* %B */ 102 { in_line_eoln, MDOC_JOIN }, /* %D */ 103 { in_line_eoln, MDOC_JOIN }, /* %I */ 104 { in_line_eoln, MDOC_JOIN }, /* %J */ 105 { in_line_eoln, 0 }, /* %N */ 106 { in_line_eoln, MDOC_JOIN }, /* %O */ 107 { in_line_eoln, 0 }, /* %P */ 108 { in_line_eoln, MDOC_JOIN }, /* %R */ 109 { in_line_eoln, MDOC_JOIN }, /* %T */ 110 { in_line_eoln, 0 }, /* %V */ 111 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 112 MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */ 113 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 114 MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */ 115 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */ 116 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */ 117 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 118 MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */ 119 { blk_full, MDOC_EXPLICIT }, /* Bf */ 120 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 121 MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */ 122 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */ 123 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */ 124 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */ 125 { in_line_eoln, 0 }, /* Db */ 126 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 127 MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */ 128 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 129 MDOC_EXPLICIT | MDOC_JOIN }, /* Do */ 130 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */ 131 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */ 132 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */ 133 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */ 134 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */ 135 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */ 136 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */ 137 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* No */ 138 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | 139 MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */ 140 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */ 141 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */ 142 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 143 MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */ 144 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */ 145 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 146 MDOC_EXPLICIT | MDOC_JOIN }, /* Po */ 147 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */ 148 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 149 MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */ 150 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */ 151 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 152 MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */ 153 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */ 154 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */ 155 { blk_full, MDOC_EXPLICIT }, /* Rs */ 156 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 157 MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */ 158 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 159 MDOC_EXPLICIT | MDOC_JOIN }, /* So */ 160 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */ 161 { in_line_argn, 0 }, /* Sm */ 162 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */ 163 { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */ 164 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */ 165 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */ 166 { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */ 167 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */ 168 { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */ 169 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 170 MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */ 171 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 172 MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */ 173 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 174 MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */ 175 { blk_full, MDOC_EXPLICIT }, /* Bk */ 176 { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */ 177 { in_line_eoln, 0 }, /* Bt */ 178 { in_line_eoln, 0 }, /* Hf */ 179 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */ 180 { in_line_eoln, 0 }, /* Ud */ 181 { in_line, 0 }, /* Lb */ 182 { in_line_eoln, 0 }, /* Lp */ 183 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */ 184 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */ 185 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */ 186 { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | 187 MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */ 188 { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | 189 MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */ 190 { in_line_eoln, MDOC_JOIN }, /* %C */ 191 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */ 192 { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */ 193 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */ 194 { in_line_eoln, MDOC_JOIN }, /* %Q */ 195 { in_line_eoln, 0 }, /* br */ 196 { in_line_eoln, 0 }, /* sp */ 197 { in_line_eoln, 0 }, /* %U */ 198 { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */ 199 { in_line_eoln, MDOC_PROLOGUE }, /* ll */ 200 }; 201 202 const struct mdoc_macro * const mdoc_macros = __mdoc_macros; 203 204 205 /* 206 * This is called at the end of parsing. It must traverse up the tree, 207 * closing out open [implicit] scopes. Obviously, open explicit scopes 208 * are errors. 209 */ 210 void 211 mdoc_endparse(struct roff_man *mdoc) 212 { 213 struct roff_node *n; 214 215 /* Scan for open explicit scopes. */ 216 217 n = mdoc->last->flags & NODE_VALID ? 218 mdoc->last->parent : mdoc->last; 219 220 for ( ; n; n = n->parent) 221 if (n->type == ROFFT_BLOCK && 222 mdoc_macros[n->tok].flags & MDOC_EXPLICIT) 223 mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse, 224 n->line, n->pos, mdoc_macronames[n->tok]); 225 226 /* Rewind to the first. */ 227 228 rew_last(mdoc, mdoc->first); 229 mdoc_state_reset(mdoc); 230 } 231 232 /* 233 * Look up the macro at *p called by "from", 234 * or as a line macro if from == TOKEN_NONE. 235 */ 236 static int 237 lookup(struct roff_man *mdoc, int from, int line, int ppos, const char *p) 238 { 239 int res; 240 241 if (mdoc->flags & MDOC_PHRASEQF) { 242 mdoc->flags &= ~MDOC_PHRASEQF; 243 return TOKEN_NONE; 244 } 245 if (from == TOKEN_NONE || mdoc_macros[from].flags & MDOC_PARSED) { 246 res = mdoc_hash_find(p); 247 if (res != TOKEN_NONE) { 248 if (mdoc_macros[res].flags & MDOC_CALLABLE) 249 return res; 250 if (res != MDOC_br && res != MDOC_sp && res != MDOC_ll) 251 mandoc_msg(MANDOCERR_MACRO_CALL, 252 mdoc->parse, line, ppos, p); 253 } 254 } 255 return TOKEN_NONE; 256 } 257 258 /* 259 * Rewind up to and including a specific node. 260 */ 261 static void 262 rew_last(struct roff_man *mdoc, const struct roff_node *to) 263 { 264 265 if (to->flags & NODE_VALID) 266 return; 267 268 while (mdoc->last != to) { 269 mdoc_state(mdoc, mdoc->last); 270 mdoc->last->flags |= NODE_VALID | NODE_ENDED; 271 mdoc->last = mdoc->last->parent; 272 } 273 mdoc_state(mdoc, mdoc->last); 274 mdoc->last->flags |= NODE_VALID | NODE_ENDED; 275 mdoc->next = ROFF_NEXT_SIBLING; 276 } 277 278 /* 279 * Rewind up to a specific block, including all blocks that broke it. 280 */ 281 static void 282 rew_pending(struct roff_man *mdoc, const struct roff_node *n) 283 { 284 285 for (;;) { 286 rew_last(mdoc, n); 287 288 if (mdoc->last == n) { 289 switch (n->type) { 290 case ROFFT_HEAD: 291 roff_body_alloc(mdoc, n->line, n->pos, 292 n->tok); 293 break; 294 case ROFFT_BLOCK: 295 break; 296 default: 297 return; 298 } 299 if ( ! (n->flags & NODE_BROKEN)) 300 return; 301 } else 302 n = mdoc->last; 303 304 for (;;) { 305 if ((n = n->parent) == NULL) 306 return; 307 308 if (n->type == ROFFT_BLOCK || 309 n->type == ROFFT_HEAD) { 310 if (n->flags & NODE_ENDED) 311 break; 312 else 313 return; 314 } 315 } 316 } 317 } 318 319 /* 320 * For a block closing macro, return the corresponding opening one. 321 * Otherwise, return the macro itself. 322 */ 323 static int 324 rew_alt(int tok) 325 { 326 switch (tok) { 327 case MDOC_Ac: 328 return MDOC_Ao; 329 case MDOC_Bc: 330 return MDOC_Bo; 331 case MDOC_Brc: 332 return MDOC_Bro; 333 case MDOC_Dc: 334 return MDOC_Do; 335 case MDOC_Ec: 336 return MDOC_Eo; 337 case MDOC_Ed: 338 return MDOC_Bd; 339 case MDOC_Ef: 340 return MDOC_Bf; 341 case MDOC_Ek: 342 return MDOC_Bk; 343 case MDOC_El: 344 return MDOC_Bl; 345 case MDOC_Fc: 346 return MDOC_Fo; 347 case MDOC_Oc: 348 return MDOC_Oo; 349 case MDOC_Pc: 350 return MDOC_Po; 351 case MDOC_Qc: 352 return MDOC_Qo; 353 case MDOC_Re: 354 return MDOC_Rs; 355 case MDOC_Sc: 356 return MDOC_So; 357 case MDOC_Xc: 358 return MDOC_Xo; 359 default: 360 return tok; 361 } 362 } 363 364 static void 365 rew_elem(struct roff_man *mdoc, int tok) 366 { 367 struct roff_node *n; 368 369 n = mdoc->last; 370 if (n->type != ROFFT_ELEM) 371 n = n->parent; 372 assert(n->type == ROFFT_ELEM); 373 assert(tok == n->tok); 374 rew_last(mdoc, n); 375 } 376 377 /* 378 * If there is an open sub-block of the target requiring 379 * explicit close-out, postpone closing out the target until 380 * the rew_pending() call closing out the sub-block. 381 */ 382 static int 383 find_pending(struct roff_man *mdoc, int tok, int line, int ppos, 384 struct roff_node *target) 385 { 386 struct roff_node *n; 387 int irc; 388 389 irc = 0; 390 for (n = mdoc->last; n != NULL && n != target; n = n->parent) { 391 if (n->flags & NODE_ENDED) { 392 if ( ! (n->flags & NODE_VALID)) 393 n->flags |= NODE_BROKEN; 394 continue; 395 } 396 if (n->type == ROFFT_BLOCK && 397 mdoc_macros[n->tok].flags & MDOC_EXPLICIT) { 398 irc = 1; 399 n->flags = NODE_BROKEN; 400 if (target->type == ROFFT_HEAD) 401 target->flags = NODE_ENDED; 402 else if ( ! (target->flags & NODE_ENDED)) { 403 mandoc_vmsg(MANDOCERR_BLK_NEST, 404 mdoc->parse, line, ppos, 405 "%s breaks %s", mdoc_macronames[tok], 406 mdoc_macronames[n->tok]); 407 mdoc_endbody_alloc(mdoc, line, ppos, 408 tok, target, ENDBODY_NOSPACE); 409 } 410 } 411 } 412 return irc; 413 } 414 415 /* 416 * Allocate a word and check whether it's punctuation or not. 417 * Punctuation consists of those tokens found in mdoc_isdelim(). 418 */ 419 static void 420 dword(struct roff_man *mdoc, int line, int col, const char *p, 421 enum mdelim d, int may_append) 422 { 423 424 if (d == DELIM_MAX) 425 d = mdoc_isdelim(p); 426 427 if (may_append && 428 ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) && 429 d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT && 430 mdoc_isdelim(mdoc->last->string) == DELIM_NONE) { 431 roff_word_append(mdoc, p); 432 return; 433 } 434 435 roff_word_alloc(mdoc, line, col, p); 436 437 /* 438 * If the word consists of a bare delimiter, 439 * flag the new node accordingly, 440 * unless doing so was vetoed by the invoking macro. 441 * Always clear the veto, it is only valid for one word. 442 */ 443 444 if (d == DELIM_OPEN) 445 mdoc->last->flags |= NODE_DELIMO; 446 else if (d == DELIM_CLOSE && 447 ! (mdoc->flags & MDOC_NODELIMC) && 448 mdoc->last->parent->tok != MDOC_Fd) 449 mdoc->last->flags |= NODE_DELIMC; 450 mdoc->flags &= ~MDOC_NODELIMC; 451 } 452 453 static void 454 append_delims(struct roff_man *mdoc, int line, int *pos, char *buf) 455 { 456 char *p; 457 int la; 458 459 if (buf[*pos] == '\0') 460 return; 461 462 for (;;) { 463 la = *pos; 464 if (mdoc_args(mdoc, line, pos, buf, TOKEN_NONE, &p) == 465 ARGS_EOLN) 466 break; 467 dword(mdoc, line, la, p, DELIM_MAX, 1); 468 469 /* 470 * If we encounter end-of-sentence symbols, then trigger 471 * the double-space. 472 * 473 * XXX: it's easy to allow this to propagate outward to 474 * the last symbol, such that `. )' will cause the 475 * correct double-spacing. However, (1) groff isn't 476 * smart enough to do this and (2) it would require 477 * knowing which symbols break this behaviour, for 478 * example, `. ;' shouldn't propagate the double-space. 479 */ 480 481 if (mandoc_eos(p, strlen(p))) 482 mdoc->last->flags |= NODE_EOS; 483 } 484 } 485 486 /* 487 * Parse one word. 488 * If it is a macro, call it and return 1. 489 * Otherwise, allocate it and return 0. 490 */ 491 static int 492 macro_or_word(MACRO_PROT_ARGS, int parsed) 493 { 494 char *p; 495 int ntok; 496 497 p = buf + ppos; 498 ntok = TOKEN_NONE; 499 if (*p == '"') 500 p++; 501 else if (parsed && ! (mdoc->flags & MDOC_PHRASELIT)) 502 ntok = lookup(mdoc, tok, line, ppos, p); 503 504 if (ntok == TOKEN_NONE) { 505 dword(mdoc, line, ppos, p, DELIM_MAX, tok == TOKEN_NONE || 506 mdoc_macros[tok].flags & MDOC_JOIN); 507 return 0; 508 } else { 509 if (mdoc_macros[tok].fp == in_line_eoln) 510 rew_elem(mdoc, tok); 511 mdoc_macro(mdoc, ntok, line, ppos, pos, buf); 512 if (tok == TOKEN_NONE) 513 append_delims(mdoc, line, pos, buf); 514 return 1; 515 } 516 } 517 518 /* 519 * Close out block partial/full explicit. 520 */ 521 static void 522 blk_exp_close(MACRO_PROT_ARGS) 523 { 524 struct roff_node *body; /* Our own body. */ 525 struct roff_node *endbody; /* Our own end marker. */ 526 struct roff_node *itblk; /* An It block starting later. */ 527 struct roff_node *later; /* A sub-block starting later. */ 528 struct roff_node *n; /* Search back to our block. */ 529 struct roff_node *target; /* For find_pending(). */ 530 531 int j, lastarg, maxargs, nl, pending; 532 enum margserr ac; 533 int atok, ntok; 534 char *p; 535 536 nl = MDOC_NEWLINE & mdoc->flags; 537 538 switch (tok) { 539 case MDOC_Ec: 540 maxargs = 1; 541 break; 542 case MDOC_Ek: 543 mdoc->flags &= ~MDOC_KEEP; 544 /* FALLTHROUGH */ 545 default: 546 maxargs = 0; 547 break; 548 } 549 550 /* Search backwards for the beginning of our own body. */ 551 552 atok = rew_alt(tok); 553 body = NULL; 554 for (n = mdoc->last; n; n = n->parent) { 555 if (n->flags & NODE_ENDED || n->tok != atok || 556 n->type != ROFFT_BODY || n->end != ENDBODY_NOT) 557 continue; 558 body = n; 559 break; 560 } 561 562 /* 563 * Search backwards for beginnings of blocks, 564 * both of our own and of pending sub-blocks. 565 */ 566 567 endbody = itblk = later = NULL; 568 for (n = mdoc->last; n; n = n->parent) { 569 if (n->flags & NODE_ENDED) { 570 if ( ! (n->flags & NODE_VALID)) 571 n->flags |= NODE_BROKEN; 572 continue; 573 } 574 575 /* 576 * Mismatching end macros can never break anything, 577 * SYNOPSIS name blocks can never be broken, 578 * and we only care about the breaking of BLOCKs. 579 */ 580 581 if (body == NULL || 582 n->tok == MDOC_Nm || 583 n->type != ROFFT_BLOCK) 584 continue; 585 586 if (n->tok == MDOC_It) { 587 itblk = n; 588 continue; 589 } 590 591 if (atok == n->tok) { 592 assert(body); 593 594 /* 595 * Found the start of our own block. 596 * When there is no pending sub block, 597 * just proceed to closing out. 598 */ 599 600 if (later == NULL || 601 (tok == MDOC_El && itblk == NULL)) 602 break; 603 604 /* 605 * When there is a pending sub block, postpone 606 * closing out the current block until the 607 * rew_pending() closing out the sub-block. 608 * Mark the place where the formatting - but not 609 * the scope - of the current block ends. 610 */ 611 612 mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, 613 line, ppos, "%s breaks %s", 614 mdoc_macronames[atok], 615 mdoc_macronames[later->tok]); 616 617 endbody = mdoc_endbody_alloc(mdoc, line, ppos, 618 atok, body, ENDBODY_SPACE); 619 620 if (tok == MDOC_El) 621 itblk->flags |= NODE_ENDED | NODE_BROKEN; 622 623 /* 624 * If a block closing macro taking arguments 625 * breaks another block, put the arguments 626 * into the end marker. 627 */ 628 629 if (maxargs) 630 mdoc->next = ROFF_NEXT_CHILD; 631 break; 632 } 633 634 /* Explicit blocks close out description lines. */ 635 636 if (n->tok == MDOC_Nd) { 637 rew_last(mdoc, n); 638 continue; 639 } 640 641 /* Breaking an open sub block. */ 642 643 n->flags |= NODE_BROKEN; 644 if (later == NULL) 645 later = n; 646 } 647 648 if (body == NULL) { 649 mandoc_msg(MANDOCERR_BLK_NOTOPEN, mdoc->parse, 650 line, ppos, mdoc_macronames[tok]); 651 if (maxargs && endbody == NULL) { 652 /* 653 * Stray .Ec without previous .Eo: 654 * Break the output line, keep the arguments. 655 */ 656 roff_elem_alloc(mdoc, line, ppos, MDOC_br); 657 rew_elem(mdoc, MDOC_br); 658 } 659 } else if (endbody == NULL) { 660 rew_last(mdoc, body); 661 if (maxargs) 662 mdoc_tail_alloc(mdoc, line, ppos, atok); 663 } 664 665 if ( ! (mdoc_macros[tok].flags & MDOC_PARSED)) { 666 if (buf[*pos] != '\0') 667 mandoc_vmsg(MANDOCERR_ARG_SKIP, 668 mdoc->parse, line, ppos, 669 "%s %s", mdoc_macronames[tok], 670 buf + *pos); 671 if (endbody == NULL && n != NULL) 672 rew_pending(mdoc, n); 673 return; 674 } 675 676 if (endbody != NULL) 677 n = endbody; 678 679 ntok = TOKEN_NONE; 680 for (j = 0; ; j++) { 681 lastarg = *pos; 682 683 if (j == maxargs && n != NULL) 684 rew_last(mdoc, n); 685 686 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 687 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) 688 break; 689 690 ntok = ac == ARGS_QWORD ? TOKEN_NONE : 691 lookup(mdoc, tok, line, lastarg, p); 692 693 if (ntok == TOKEN_NONE) { 694 dword(mdoc, line, lastarg, p, DELIM_MAX, 695 MDOC_JOIN & mdoc_macros[tok].flags); 696 continue; 697 } 698 699 if (n != NULL) 700 rew_last(mdoc, n); 701 mdoc->flags &= ~MDOC_NEWLINE; 702 mdoc_macro(mdoc, ntok, line, lastarg, pos, buf); 703 break; 704 } 705 706 if (n != NULL) { 707 if (ntok != TOKEN_NONE && n->flags & NODE_BROKEN) { 708 target = n; 709 do 710 target = target->parent; 711 while ( ! (target->flags & NODE_ENDED)); 712 pending = find_pending(mdoc, ntok, line, ppos, 713 target); 714 } else 715 pending = 0; 716 if ( ! pending) 717 rew_pending(mdoc, n); 718 } 719 if (nl) 720 append_delims(mdoc, line, pos, buf); 721 } 722 723 static void 724 in_line(MACRO_PROT_ARGS) 725 { 726 int la, scope, cnt, firstarg, mayopen, nc, nl; 727 int ntok; 728 enum margserr ac; 729 enum mdelim d; 730 struct mdoc_arg *arg; 731 char *p; 732 733 nl = MDOC_NEWLINE & mdoc->flags; 734 735 /* 736 * Whether we allow ignored elements (those without content, 737 * usually because of reserved words) to squeak by. 738 */ 739 740 switch (tok) { 741 case MDOC_An: 742 case MDOC_Ar: 743 case MDOC_Fl: 744 case MDOC_Mt: 745 case MDOC_Nm: 746 case MDOC_Pa: 747 nc = 1; 748 break; 749 default: 750 nc = 0; 751 break; 752 } 753 754 mdoc_argv(mdoc, line, tok, &arg, pos, buf); 755 756 d = DELIM_NONE; 757 firstarg = 1; 758 mayopen = 1; 759 for (cnt = scope = 0;; ) { 760 la = *pos; 761 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 762 763 /* 764 * At the end of a macro line, 765 * opening delimiters do not suppress spacing. 766 */ 767 768 if (ac == ARGS_EOLN) { 769 if (d == DELIM_OPEN) 770 mdoc->last->flags &= ~NODE_DELIMO; 771 break; 772 } 773 774 /* 775 * The rest of the macro line is only punctuation, 776 * to be handled by append_delims(). 777 * If there were no other arguments, 778 * do not allow the first one to suppress spacing, 779 * even if it turns out to be a closing one. 780 */ 781 782 if (ac == ARGS_PUNCT) { 783 if (cnt == 0 && (nc == 0 || tok == MDOC_An)) 784 mdoc->flags |= MDOC_NODELIMC; 785 break; 786 } 787 788 ntok = (ac == ARGS_QWORD || (tok == MDOC_Fn && !cnt)) ? 789 TOKEN_NONE : lookup(mdoc, tok, line, la, p); 790 791 /* 792 * In this case, we've located a submacro and must 793 * execute it. Close out scope, if open. If no 794 * elements have been generated, either create one (nc) 795 * or raise a warning. 796 */ 797 798 if (ntok != TOKEN_NONE) { 799 if (scope) 800 rew_elem(mdoc, tok); 801 if (nc && ! cnt) { 802 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 803 rew_last(mdoc, mdoc->last); 804 } else if ( ! nc && ! cnt) { 805 mdoc_argv_free(arg); 806 mandoc_msg(MANDOCERR_MACRO_EMPTY, 807 mdoc->parse, line, ppos, 808 mdoc_macronames[tok]); 809 } 810 mdoc_macro(mdoc, ntok, line, la, pos, buf); 811 if (nl) 812 append_delims(mdoc, line, pos, buf); 813 return; 814 } 815 816 /* 817 * Non-quote-enclosed punctuation. Set up our scope, if 818 * a word; rewind the scope, if a delimiter; then append 819 * the word. 820 */ 821 822 d = ac == ARGS_QWORD ? DELIM_NONE : mdoc_isdelim(p); 823 824 if (DELIM_NONE != d) { 825 /* 826 * If we encounter closing punctuation, no word 827 * has been emitted, no scope is open, and we're 828 * allowed to have an empty element, then start 829 * a new scope. 830 */ 831 if ((d == DELIM_CLOSE || 832 (d == DELIM_MIDDLE && tok == MDOC_Fl)) && 833 !cnt && !scope && nc && mayopen) { 834 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 835 scope = 1; 836 cnt++; 837 if (tok == MDOC_Nm) 838 mayopen = 0; 839 } 840 /* 841 * Close out our scope, if one is open, before 842 * any punctuation. 843 */ 844 if (scope) 845 rew_elem(mdoc, tok); 846 scope = 0; 847 if (tok == MDOC_Fn) 848 mayopen = 0; 849 } else if (mayopen && !scope) { 850 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 851 scope = 1; 852 cnt++; 853 } 854 855 dword(mdoc, line, la, p, d, 856 MDOC_JOIN & mdoc_macros[tok].flags); 857 858 /* 859 * If the first argument is a closing delimiter, 860 * do not suppress spacing before it. 861 */ 862 863 if (firstarg && d == DELIM_CLOSE && !nc) 864 mdoc->last->flags &= ~NODE_DELIMC; 865 firstarg = 0; 866 867 /* 868 * `Fl' macros have their scope re-opened with each new 869 * word so that the `-' can be added to each one without 870 * having to parse out spaces. 871 */ 872 if (scope && tok == MDOC_Fl) { 873 rew_elem(mdoc, tok); 874 scope = 0; 875 } 876 } 877 878 if (scope) 879 rew_elem(mdoc, tok); 880 881 /* 882 * If no elements have been collected and we're allowed to have 883 * empties (nc), open a scope and close it out. Otherwise, 884 * raise a warning. 885 */ 886 887 if ( ! cnt) { 888 if (nc) { 889 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 890 rew_last(mdoc, mdoc->last); 891 } else { 892 mdoc_argv_free(arg); 893 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse, 894 line, ppos, mdoc_macronames[tok]); 895 } 896 } 897 if (nl) 898 append_delims(mdoc, line, pos, buf); 899 } 900 901 static void 902 blk_full(MACRO_PROT_ARGS) 903 { 904 int la, nl, parsed; 905 struct mdoc_arg *arg; 906 struct roff_node *blk; /* Our own or a broken block. */ 907 struct roff_node *head; /* Our own head. */ 908 struct roff_node *body; /* Our own body. */ 909 struct roff_node *n; 910 enum margserr ac, lac; 911 char *p; 912 913 nl = MDOC_NEWLINE & mdoc->flags; 914 915 if (buf[*pos] == '\0' && (tok == MDOC_Sh || tok == MDOC_Ss)) { 916 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse, 917 line, ppos, mdoc_macronames[tok]); 918 return; 919 } 920 921 if ( ! (mdoc_macros[tok].flags & MDOC_EXPLICIT)) { 922 923 /* Here, tok is one of Sh Ss Nm Nd It. */ 924 925 blk = NULL; 926 for (n = mdoc->last; n != NULL; n = n->parent) { 927 if (n->flags & NODE_ENDED) { 928 if ( ! (n->flags & NODE_VALID)) 929 n->flags |= NODE_BROKEN; 930 continue; 931 } 932 if (n->type != ROFFT_BLOCK) 933 continue; 934 935 if (tok == MDOC_It && n->tok == MDOC_Bl) { 936 if (blk != NULL) { 937 mandoc_vmsg(MANDOCERR_BLK_BROKEN, 938 mdoc->parse, line, ppos, 939 "It breaks %s", 940 mdoc_macronames[blk->tok]); 941 rew_pending(mdoc, blk); 942 } 943 break; 944 } 945 946 if (mdoc_macros[n->tok].flags & MDOC_EXPLICIT) { 947 switch (tok) { 948 case MDOC_Sh: 949 case MDOC_Ss: 950 mandoc_vmsg(MANDOCERR_BLK_BROKEN, 951 mdoc->parse, line, ppos, 952 "%s breaks %s", 953 mdoc_macronames[tok], 954 mdoc_macronames[n->tok]); 955 rew_pending(mdoc, n); 956 n = mdoc->last; 957 continue; 958 case MDOC_It: 959 /* Delay in case it's astray. */ 960 blk = n; 961 continue; 962 default: 963 break; 964 } 965 break; 966 } 967 968 /* Here, n is one of Sh Ss Nm Nd It. */ 969 970 if (tok != MDOC_Sh && (n->tok == MDOC_Sh || 971 (tok != MDOC_Ss && (n->tok == MDOC_Ss || 972 (tok != MDOC_It && n->tok == MDOC_It))))) 973 break; 974 975 /* Item breaking an explicit block. */ 976 977 if (blk != NULL) { 978 mandoc_vmsg(MANDOCERR_BLK_BROKEN, 979 mdoc->parse, line, ppos, 980 "It breaks %s", 981 mdoc_macronames[blk->tok]); 982 rew_pending(mdoc, blk); 983 blk = NULL; 984 } 985 986 /* Close out prior implicit scopes. */ 987 988 rew_last(mdoc, n); 989 } 990 991 /* Skip items outside lists. */ 992 993 if (tok == MDOC_It && (n == NULL || n->tok != MDOC_Bl)) { 994 mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse, 995 line, ppos, "It %s", buf + *pos); 996 roff_elem_alloc(mdoc, line, ppos, MDOC_br); 997 rew_elem(mdoc, MDOC_br); 998 return; 999 } 1000 } 1001 1002 /* 1003 * This routine accommodates implicitly- and explicitly-scoped 1004 * macro openings. Implicit ones first close out prior scope 1005 * (seen above). Delay opening the head until necessary to 1006 * allow leading punctuation to print. Special consideration 1007 * for `It -column', which has phrase-part syntax instead of 1008 * regular child nodes. 1009 */ 1010 1011 mdoc_argv(mdoc, line, tok, &arg, pos, buf); 1012 blk = mdoc_block_alloc(mdoc, line, ppos, tok, arg); 1013 head = body = NULL; 1014 1015 /* 1016 * Exception: Heads of `It' macros in `-diag' lists are not 1017 * parsed, even though `It' macros in general are parsed. 1018 */ 1019 1020 parsed = tok != MDOC_It || 1021 mdoc->last->parent->tok != MDOC_Bl || 1022 mdoc->last->parent->norm->Bl.type != LIST_diag; 1023 1024 /* 1025 * The `Nd' macro has all arguments in its body: it's a hybrid 1026 * of block partial-explicit and full-implicit. Stupid. 1027 */ 1028 1029 if (tok == MDOC_Nd) { 1030 head = roff_head_alloc(mdoc, line, ppos, tok); 1031 rew_last(mdoc, head); 1032 body = roff_body_alloc(mdoc, line, ppos, tok); 1033 } 1034 1035 if (tok == MDOC_Bk) 1036 mdoc->flags |= MDOC_KEEP; 1037 1038 ac = ARGS_EOLN; 1039 for (;;) { 1040 1041 /* 1042 * If we are right after a tab character, 1043 * do not parse the first word for macros. 1044 */ 1045 1046 if (mdoc->flags & MDOC_PHRASEQN) { 1047 mdoc->flags &= ~MDOC_PHRASEQN; 1048 mdoc->flags |= MDOC_PHRASEQF; 1049 } 1050 1051 la = *pos; 1052 lac = ac; 1053 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 1054 if (ac == ARGS_EOLN) { 1055 if (lac != ARGS_PHRASE || 1056 ! (mdoc->flags & MDOC_PHRASEQF)) 1057 break; 1058 1059 /* 1060 * This line ends in a tab; start the next 1061 * column now, with a leading blank. 1062 */ 1063 1064 if (body != NULL) 1065 rew_last(mdoc, body); 1066 body = roff_body_alloc(mdoc, line, ppos, tok); 1067 roff_word_alloc(mdoc, line, ppos, "\\&"); 1068 break; 1069 } 1070 1071 if (tok == MDOC_Bd || tok == MDOC_Bk) { 1072 mandoc_vmsg(MANDOCERR_ARG_EXCESS, 1073 mdoc->parse, line, la, "%s ... %s", 1074 mdoc_macronames[tok], buf + la); 1075 break; 1076 } 1077 if (tok == MDOC_Rs) { 1078 mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse, 1079 line, la, "Rs %s", buf + la); 1080 break; 1081 } 1082 if (ac == ARGS_PUNCT) 1083 break; 1084 1085 /* 1086 * Emit leading punctuation (i.e., punctuation before 1087 * the ROFFT_HEAD) for non-phrase types. 1088 */ 1089 1090 if (head == NULL && 1091 ac != ARGS_PHRASE && 1092 ac != ARGS_QWORD && 1093 mdoc_isdelim(p) == DELIM_OPEN) { 1094 dword(mdoc, line, la, p, DELIM_OPEN, 0); 1095 continue; 1096 } 1097 1098 /* Open a head if one hasn't been opened. */ 1099 1100 if (head == NULL) 1101 head = roff_head_alloc(mdoc, line, ppos, tok); 1102 1103 if (ac == ARGS_PHRASE) { 1104 1105 /* 1106 * If we haven't opened a body yet, rewind the 1107 * head; if we have, rewind that instead. 1108 */ 1109 1110 rew_last(mdoc, body == NULL ? head : body); 1111 body = roff_body_alloc(mdoc, line, ppos, tok); 1112 1113 /* Process to the tab or to the end of the line. */ 1114 1115 mdoc->flags |= MDOC_PHRASE; 1116 parse_rest(mdoc, TOKEN_NONE, line, &la, buf); 1117 mdoc->flags &= ~MDOC_PHRASE; 1118 1119 /* There may have been `Ta' macros. */ 1120 1121 while (body->next != NULL) 1122 body = body->next; 1123 continue; 1124 } 1125 1126 if (macro_or_word(mdoc, tok, line, la, pos, buf, parsed)) 1127 break; 1128 } 1129 1130 if (blk->flags & NODE_VALID) 1131 return; 1132 if (head == NULL) 1133 head = roff_head_alloc(mdoc, line, ppos, tok); 1134 if (nl && tok != MDOC_Bd && tok != MDOC_Bl && tok != MDOC_Rs) 1135 append_delims(mdoc, line, pos, buf); 1136 if (body != NULL) 1137 goto out; 1138 if (find_pending(mdoc, tok, line, ppos, head)) 1139 return; 1140 1141 /* Close out scopes to remain in a consistent state. */ 1142 1143 rew_last(mdoc, head); 1144 body = roff_body_alloc(mdoc, line, ppos, tok); 1145 out: 1146 if (mdoc->flags & MDOC_FREECOL) { 1147 rew_last(mdoc, body); 1148 rew_last(mdoc, blk); 1149 mdoc->flags &= ~MDOC_FREECOL; 1150 } 1151 } 1152 1153 static void 1154 blk_part_imp(MACRO_PROT_ARGS) 1155 { 1156 int la, nl; 1157 enum margserr ac; 1158 char *p; 1159 struct roff_node *blk; /* saved block context */ 1160 struct roff_node *body; /* saved body context */ 1161 struct roff_node *n; 1162 1163 nl = MDOC_NEWLINE & mdoc->flags; 1164 1165 /* 1166 * A macro that spans to the end of the line. This is generally 1167 * (but not necessarily) called as the first macro. The block 1168 * has a head as the immediate child, which is always empty, 1169 * followed by zero or more opening punctuation nodes, then the 1170 * body (which may be empty, depending on the macro), then zero 1171 * or more closing punctuation nodes. 1172 */ 1173 1174 blk = mdoc_block_alloc(mdoc, line, ppos, tok, NULL); 1175 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok)); 1176 1177 /* 1178 * Open the body scope "on-demand", that is, after we've 1179 * processed all our the leading delimiters (open parenthesis, 1180 * etc.). 1181 */ 1182 1183 for (body = NULL; ; ) { 1184 la = *pos; 1185 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 1186 if (ac == ARGS_EOLN || ac == ARGS_PUNCT) 1187 break; 1188 1189 if (body == NULL && ac != ARGS_QWORD && 1190 mdoc_isdelim(p) == DELIM_OPEN) { 1191 dword(mdoc, line, la, p, DELIM_OPEN, 0); 1192 continue; 1193 } 1194 1195 if (body == NULL) 1196 body = roff_body_alloc(mdoc, line, ppos, tok); 1197 1198 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1)) 1199 break; 1200 } 1201 if (body == NULL) 1202 body = roff_body_alloc(mdoc, line, ppos, tok); 1203 1204 if (find_pending(mdoc, tok, line, ppos, body)) 1205 return; 1206 1207 rew_last(mdoc, body); 1208 if (nl) 1209 append_delims(mdoc, line, pos, buf); 1210 rew_pending(mdoc, blk); 1211 1212 /* Move trailing .Ns out of scope. */ 1213 1214 for (n = body->child; n && n->next; n = n->next) 1215 /* Do nothing. */ ; 1216 if (n && n->tok == MDOC_Ns) 1217 mdoc_node_relink(mdoc, n); 1218 } 1219 1220 static void 1221 blk_part_exp(MACRO_PROT_ARGS) 1222 { 1223 int la, nl; 1224 enum margserr ac; 1225 struct roff_node *head; /* keep track of head */ 1226 char *p; 1227 1228 nl = MDOC_NEWLINE & mdoc->flags; 1229 1230 /* 1231 * The opening of an explicit macro having zero or more leading 1232 * punctuation nodes; a head with optional single element (the 1233 * case of `Eo'); and a body that may be empty. 1234 */ 1235 1236 roff_block_alloc(mdoc, line, ppos, tok); 1237 head = NULL; 1238 for (;;) { 1239 la = *pos; 1240 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 1241 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) 1242 break; 1243 1244 /* Flush out leading punctuation. */ 1245 1246 if (head == NULL && ac != ARGS_QWORD && 1247 mdoc_isdelim(p) == DELIM_OPEN) { 1248 dword(mdoc, line, la, p, DELIM_OPEN, 0); 1249 continue; 1250 } 1251 1252 if (head == NULL) { 1253 head = roff_head_alloc(mdoc, line, ppos, tok); 1254 if (tok == MDOC_Eo) /* Not parsed. */ 1255 dword(mdoc, line, la, p, DELIM_MAX, 0); 1256 rew_last(mdoc, head); 1257 roff_body_alloc(mdoc, line, ppos, tok); 1258 if (tok == MDOC_Eo) 1259 continue; 1260 } 1261 1262 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1)) 1263 break; 1264 } 1265 1266 /* Clean-up to leave in a consistent state. */ 1267 1268 if (head == NULL) { 1269 rew_last(mdoc, roff_head_alloc(mdoc, line, ppos, tok)); 1270 roff_body_alloc(mdoc, line, ppos, tok); 1271 } 1272 if (nl) 1273 append_delims(mdoc, line, pos, buf); 1274 } 1275 1276 static void 1277 in_line_argn(MACRO_PROT_ARGS) 1278 { 1279 struct mdoc_arg *arg; 1280 char *p; 1281 enum margserr ac; 1282 int ntok; 1283 int state; /* arg#; -1: not yet open; -2: closed */ 1284 int la, maxargs, nl; 1285 1286 nl = mdoc->flags & MDOC_NEWLINE; 1287 1288 /* 1289 * A line macro that has a fixed number of arguments (maxargs). 1290 * Only open the scope once the first non-leading-punctuation is 1291 * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then 1292 * keep it open until the maximum number of arguments are 1293 * exhausted. 1294 */ 1295 1296 switch (tok) { 1297 case MDOC_Ap: 1298 case MDOC_Ns: 1299 case MDOC_Ux: 1300 maxargs = 0; 1301 break; 1302 case MDOC_Bx: 1303 case MDOC_Es: 1304 case MDOC_Xr: 1305 maxargs = 2; 1306 break; 1307 default: 1308 maxargs = 1; 1309 break; 1310 } 1311 1312 mdoc_argv(mdoc, line, tok, &arg, pos, buf); 1313 1314 state = -1; 1315 p = NULL; 1316 for (;;) { 1317 la = *pos; 1318 ac = mdoc_args(mdoc, line, pos, buf, tok, &p); 1319 1320 if (ac == ARGS_WORD && state == -1 && 1321 ! (mdoc_macros[tok].flags & MDOC_IGNDELIM) && 1322 mdoc_isdelim(p) == DELIM_OPEN) { 1323 dword(mdoc, line, la, p, DELIM_OPEN, 0); 1324 continue; 1325 } 1326 1327 if (state == -1 && tok != MDOC_In && 1328 tok != MDOC_St && tok != MDOC_Xr) { 1329 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 1330 state = 0; 1331 } 1332 1333 if (ac == ARGS_PUNCT || ac == ARGS_EOLN) { 1334 if (abs(state) < 2 && tok == MDOC_Pf) 1335 mandoc_vmsg(MANDOCERR_PF_SKIP, 1336 mdoc->parse, line, ppos, "Pf %s", 1337 p == NULL ? "at eol" : p); 1338 break; 1339 } 1340 1341 if (state == maxargs) { 1342 rew_elem(mdoc, tok); 1343 state = -2; 1344 } 1345 1346 ntok = (ac == ARGS_QWORD || (tok == MDOC_Pf && state == 0)) ? 1347 TOKEN_NONE : lookup(mdoc, tok, line, la, p); 1348 1349 if (ntok != TOKEN_NONE) { 1350 if (state >= 0) { 1351 rew_elem(mdoc, tok); 1352 state = -2; 1353 } 1354 mdoc_macro(mdoc, ntok, line, la, pos, buf); 1355 break; 1356 } 1357 1358 if (ac == ARGS_QWORD || 1359 mdoc_macros[tok].flags & MDOC_IGNDELIM || 1360 mdoc_isdelim(p) == DELIM_NONE) { 1361 if (state == -1) { 1362 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 1363 state = 1; 1364 } else if (state >= 0) 1365 state++; 1366 } else if (state >= 0) { 1367 rew_elem(mdoc, tok); 1368 state = -2; 1369 } 1370 1371 dword(mdoc, line, la, p, DELIM_MAX, 1372 MDOC_JOIN & mdoc_macros[tok].flags); 1373 } 1374 1375 if (state == -1) { 1376 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse, 1377 line, ppos, mdoc_macronames[tok]); 1378 return; 1379 } 1380 1381 if (state == 0 && tok == MDOC_Pf) 1382 append_delims(mdoc, line, pos, buf); 1383 if (state >= 0) 1384 rew_elem(mdoc, tok); 1385 if (nl) 1386 append_delims(mdoc, line, pos, buf); 1387 } 1388 1389 static void 1390 in_line_eoln(MACRO_PROT_ARGS) 1391 { 1392 struct roff_node *n; 1393 struct mdoc_arg *arg; 1394 1395 if ((tok == MDOC_Pp || tok == MDOC_Lp) && 1396 ! (mdoc->flags & MDOC_SYNOPSIS)) { 1397 n = mdoc->last; 1398 if (mdoc->next == ROFF_NEXT_SIBLING) 1399 n = n->parent; 1400 if (n->tok == MDOC_Nm) 1401 rew_last(mdoc, n->parent); 1402 } 1403 1404 if (buf[*pos] == '\0' && 1405 (tok == MDOC_Fd || mdoc_macronames[tok][0] == '%')) { 1406 mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse, 1407 line, ppos, mdoc_macronames[tok]); 1408 return; 1409 } 1410 1411 mdoc_argv(mdoc, line, tok, &arg, pos, buf); 1412 mdoc_elem_alloc(mdoc, line, ppos, tok, arg); 1413 if (parse_rest(mdoc, tok, line, pos, buf)) 1414 return; 1415 rew_elem(mdoc, tok); 1416 } 1417 1418 /* 1419 * The simplest argument parser available: Parse the remaining 1420 * words until the end of the phrase or line and return 0 1421 * or until the next macro, call that macro, and return 1. 1422 */ 1423 static int 1424 parse_rest(struct roff_man *mdoc, int tok, int line, int *pos, char *buf) 1425 { 1426 int la; 1427 1428 for (;;) { 1429 la = *pos; 1430 if (mdoc_args(mdoc, line, pos, buf, tok, NULL) == ARGS_EOLN) 1431 return 0; 1432 if (macro_or_word(mdoc, tok, line, la, pos, buf, 1)) 1433 return 1; 1434 } 1435 } 1436 1437 static void 1438 ctx_synopsis(MACRO_PROT_ARGS) 1439 { 1440 1441 if (~mdoc->flags & (MDOC_SYNOPSIS | MDOC_NEWLINE)) 1442 in_line(mdoc, tok, line, ppos, pos, buf); 1443 else if (tok == MDOC_Nm) 1444 blk_full(mdoc, tok, line, ppos, pos, buf); 1445 else { 1446 assert(tok == MDOC_Vt); 1447 blk_part_imp(mdoc, tok, line, ppos, pos, buf); 1448 } 1449 } 1450 1451 /* 1452 * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs. 1453 * They're unusual because they're basically free-form text until a 1454 * macro is encountered. 1455 */ 1456 static void 1457 phrase_ta(MACRO_PROT_ARGS) 1458 { 1459 struct roff_node *body, *n; 1460 1461 /* Make sure we are in a column list or ignore this macro. */ 1462 1463 body = NULL; 1464 for (n = mdoc->last; n != NULL; n = n->parent) { 1465 if (n->flags & NODE_ENDED) 1466 continue; 1467 if (n->tok == MDOC_It && n->type == ROFFT_BODY) 1468 body = n; 1469 if (n->tok == MDOC_Bl && n->end == ENDBODY_NOT) 1470 break; 1471 } 1472 1473 if (n == NULL || n->norm->Bl.type != LIST_column) { 1474 mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse, 1475 line, ppos, "Ta"); 1476 return; 1477 } 1478 1479 /* Advance to the next column. */ 1480 1481 rew_last(mdoc, body); 1482 roff_body_alloc(mdoc, line, ppos, MDOC_It); 1483 parse_rest(mdoc, TOKEN_NONE, line, pos, buf); 1484 } 1485