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