1 /* $OpenBSD: man_macro.c,v 1.106 2019/01/05 18:59:37 schwarze Exp $ */ 2 /* 3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> 4 * Copyright (c) 2012-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org> 5 * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 #include <sys/types.h> 20 21 #include <assert.h> 22 #include <ctype.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 27 #include "mandoc.h" 28 #include "roff.h" 29 #include "man.h" 30 #include "libmandoc.h" 31 #include "roff_int.h" 32 #include "libman.h" 33 34 static void blk_close(MACRO_PROT_ARGS); 35 static void blk_exp(MACRO_PROT_ARGS); 36 static void blk_imp(MACRO_PROT_ARGS); 37 static void in_line_eoln(MACRO_PROT_ARGS); 38 static int man_args(struct roff_man *, int, 39 int *, char *, char **); 40 static void rew_scope(struct roff_man *, enum roff_tok); 41 42 static const struct man_macro man_macros[MAN_MAX - MAN_TH] = { 43 { in_line_eoln, MAN_XSCOPE }, /* TH */ 44 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SH */ 45 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* SS */ 46 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TP */ 47 { blk_imp, MAN_XSCOPE | MAN_BSCOPED }, /* TQ */ 48 { blk_imp, MAN_XSCOPE }, /* LP */ 49 { blk_imp, MAN_XSCOPE }, /* PP */ 50 { blk_imp, MAN_XSCOPE }, /* P */ 51 { blk_imp, MAN_XSCOPE }, /* IP */ 52 { blk_imp, MAN_XSCOPE }, /* HP */ 53 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SM */ 54 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* SB */ 55 { in_line_eoln, 0 }, /* BI */ 56 { in_line_eoln, 0 }, /* IB */ 57 { in_line_eoln, 0 }, /* BR */ 58 { in_line_eoln, 0 }, /* RB */ 59 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* R */ 60 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* B */ 61 { in_line_eoln, MAN_NSCOPED | MAN_ESCOPED | MAN_JOIN }, /* I */ 62 { in_line_eoln, 0 }, /* IR */ 63 { in_line_eoln, 0 }, /* RI */ 64 { blk_close, MAN_XSCOPE }, /* RE */ 65 { blk_exp, MAN_XSCOPE }, /* RS */ 66 { in_line_eoln, 0 }, /* DT */ 67 { in_line_eoln, 0 }, /* UC */ 68 { in_line_eoln, MAN_NSCOPED }, /* PD */ 69 { in_line_eoln, 0 }, /* AT */ 70 { in_line_eoln, MAN_NSCOPED }, /* in */ 71 { blk_imp, MAN_XSCOPE }, /* SY */ 72 { blk_close, MAN_XSCOPE }, /* YS */ 73 { in_line_eoln, 0 }, /* OP */ 74 { in_line_eoln, MAN_XSCOPE }, /* EX */ 75 { in_line_eoln, MAN_XSCOPE }, /* EE */ 76 { blk_exp, MAN_XSCOPE }, /* UR */ 77 { blk_close, MAN_XSCOPE }, /* UE */ 78 { blk_exp, MAN_XSCOPE }, /* MT */ 79 { blk_close, MAN_XSCOPE }, /* ME */ 80 }; 81 82 83 const struct man_macro * 84 man_macro(enum roff_tok tok) 85 { 86 assert(tok >= MAN_TH && tok <= MAN_MAX); 87 return man_macros + (tok - MAN_TH); 88 } 89 90 void 91 man_unscope(struct roff_man *man, const struct roff_node *to) 92 { 93 struct roff_node *n; 94 95 to = to->parent; 96 n = man->last; 97 while (n != to) { 98 99 /* Reached the end of the document? */ 100 101 if (to == NULL && ! (n->flags & NODE_VALID)) { 102 if (man->flags & (MAN_BLINE | MAN_ELINE) && 103 man_macro(n->tok)->flags & 104 (MAN_BSCOPED | MAN_NSCOPED)) { 105 mandoc_msg(MANDOCERR_BLK_LINE, 106 n->line, n->pos, 107 "EOF breaks %s", roff_name[n->tok]); 108 if (man->flags & MAN_ELINE) 109 man->flags &= ~MAN_ELINE; 110 else { 111 assert(n->type == ROFFT_HEAD); 112 n = n->parent; 113 man->flags &= ~MAN_BLINE; 114 } 115 man->last = n; 116 n = n->parent; 117 roff_node_delete(man, man->last); 118 continue; 119 } 120 if (n->type == ROFFT_BLOCK && 121 man_macro(n->tok)->fp == blk_exp) 122 mandoc_msg(MANDOCERR_BLK_NOEND, 123 n->line, n->pos, "%s", 124 roff_name[n->tok]); 125 } 126 127 /* 128 * We might delete the man->last node 129 * in the post-validation phase. 130 * Save a pointer to the parent such that 131 * we know where to continue the iteration. 132 */ 133 134 man->last = n; 135 n = n->parent; 136 man->last->flags |= NODE_VALID; 137 } 138 139 /* 140 * If we ended up at the parent of the node we were 141 * supposed to rewind to, that means the target node 142 * got deleted, so add the next node we parse as a child 143 * of the parent instead of as a sibling of the target. 144 */ 145 146 man->next = (man->last == to) ? 147 ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING; 148 } 149 150 /* 151 * Rewinding entails ascending the parse tree until a coherent point, 152 * for example, the `SH' macro will close out any intervening `SS' 153 * scopes. When a scope is closed, it must be validated and actioned. 154 */ 155 static void 156 rew_scope(struct roff_man *man, enum roff_tok tok) 157 { 158 struct roff_node *n; 159 160 /* Preserve empty paragraphs before RS. */ 161 162 n = man->last; 163 if (tok == MAN_RS && n->child == NULL && 164 (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP)) 165 return; 166 167 for (;;) { 168 if (n->type == ROFFT_ROOT) 169 return; 170 if (n->flags & NODE_VALID) { 171 n = n->parent; 172 continue; 173 } 174 if (n->type != ROFFT_BLOCK) { 175 if (n->parent->type == ROFFT_ROOT) { 176 man_unscope(man, n); 177 return; 178 } else { 179 n = n->parent; 180 continue; 181 } 182 } 183 if (tok != MAN_SH && (n->tok == MAN_SH || 184 (tok != MAN_SS && (n->tok == MAN_SS || 185 man_macro(n->tok)->fp == blk_exp)))) 186 return; 187 man_unscope(man, n); 188 n = man->last; 189 } 190 } 191 192 193 /* 194 * Close out a generic explicit macro. 195 */ 196 void 197 blk_close(MACRO_PROT_ARGS) 198 { 199 enum roff_tok ctok, ntok; 200 const struct roff_node *nn; 201 char *p, *ep; 202 int cline, cpos, la, nrew, target; 203 204 nrew = 1; 205 switch (tok) { 206 case MAN_RE: 207 ntok = MAN_RS; 208 la = *pos; 209 if ( ! man_args(man, line, pos, buf, &p)) 210 break; 211 for (nn = man->last->parent; nn; nn = nn->parent) 212 if (nn->tok == ntok && nn->type == ROFFT_BLOCK) 213 nrew++; 214 target = strtol(p, &ep, 10); 215 if (*ep != '\0') 216 mandoc_msg(MANDOCERR_ARG_EXCESS, line, 217 la + (buf[la] == '"') + (int)(ep - p), 218 "RE ... %s", ep); 219 free(p); 220 if (target == 0) 221 target = 1; 222 nrew -= target; 223 if (nrew < 1) { 224 mandoc_msg(MANDOCERR_RE_NOTOPEN, 225 line, ppos, "RE %d", target); 226 return; 227 } 228 break; 229 case MAN_YS: 230 ntok = MAN_SY; 231 break; 232 case MAN_UE: 233 ntok = MAN_UR; 234 break; 235 case MAN_ME: 236 ntok = MAN_MT; 237 break; 238 default: 239 abort(); 240 } 241 242 for (nn = man->last->parent; nn; nn = nn->parent) 243 if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew) 244 break; 245 246 if (nn == NULL) { 247 mandoc_msg(MANDOCERR_BLK_NOTOPEN, 248 line, ppos, "%s", roff_name[tok]); 249 rew_scope(man, MAN_PP); 250 if (tok == MAN_RE) { 251 roff_elem_alloc(man, line, ppos, ROFF_br); 252 man->last->flags |= NODE_LINE | 253 NODE_VALID | NODE_ENDED; 254 man->next = ROFF_NEXT_SIBLING; 255 } 256 return; 257 } 258 259 cline = man->last->line; 260 cpos = man->last->pos; 261 ctok = man->last->tok; 262 man_unscope(man, nn); 263 264 if (tok == MAN_RE && nn->head->aux > 0) 265 roff_setreg(man->roff, "an-margin", nn->head->aux, '-'); 266 267 /* Trailing text. */ 268 269 if (buf[*pos] != '\0') { 270 roff_word_alloc(man, line, ppos, buf + *pos); 271 man->last->flags |= NODE_DELIMC; 272 if (mandoc_eos(man->last->string, strlen(man->last->string))) 273 man->last->flags |= NODE_EOS; 274 } 275 276 /* Move a trailing paragraph behind the block. */ 277 278 if (ctok == MAN_LP || ctok == MAN_PP || ctok == MAN_P) { 279 *pos = strlen(buf); 280 blk_imp(man, ctok, cline, cpos, pos, buf); 281 } 282 283 /* Synopsis blocks need an explicit end marker for spacing. */ 284 285 if (tok == MAN_YS && man->last == nn) { 286 roff_elem_alloc(man, line, ppos, tok); 287 man_unscope(man, man->last); 288 } 289 } 290 291 void 292 blk_exp(MACRO_PROT_ARGS) 293 { 294 struct roff_node *head; 295 char *p; 296 int la; 297 298 if (tok == MAN_RS) { 299 rew_scope(man, tok); 300 man->flags |= ROFF_NONOFILL; 301 } 302 roff_block_alloc(man, line, ppos, tok); 303 head = roff_head_alloc(man, line, ppos, tok); 304 305 la = *pos; 306 if (man_args(man, line, pos, buf, &p)) { 307 roff_word_alloc(man, line, la, p); 308 if (tok == MAN_RS) { 309 if (roff_getreg(man->roff, "an-margin") == 0) 310 roff_setreg(man->roff, "an-margin", 311 7 * 24, '='); 312 if ((head->aux = strtod(p, NULL) * 24.0) > 0) 313 roff_setreg(man->roff, "an-margin", 314 head->aux, '+'); 315 } 316 free(p); 317 } 318 319 if (buf[*pos] != '\0') 320 mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos, 321 "%s ... %s", roff_name[tok], buf + *pos); 322 323 man_unscope(man, head); 324 roff_body_alloc(man, line, ppos, tok); 325 man->flags &= ~ROFF_NONOFILL; 326 } 327 328 /* 329 * Parse an implicit-block macro. These contain a ROFFT_HEAD and a 330 * ROFFT_BODY contained within a ROFFT_BLOCK. Rules for closing out other 331 * scopes, such as `SH' closing out an `SS', are defined in the rew 332 * routines. 333 */ 334 void 335 blk_imp(MACRO_PROT_ARGS) 336 { 337 int la; 338 char *p; 339 struct roff_node *n; 340 341 rew_scope(man, tok); 342 man->flags |= ROFF_NONOFILL; 343 if (tok == MAN_SH || tok == MAN_SS) 344 man->flags &= ~ROFF_NOFILL; 345 roff_block_alloc(man, line, ppos, tok); 346 n = roff_head_alloc(man, line, ppos, tok); 347 348 /* Add line arguments. */ 349 350 for (;;) { 351 la = *pos; 352 if ( ! man_args(man, line, pos, buf, &p)) 353 break; 354 roff_word_alloc(man, line, la, p); 355 free(p); 356 } 357 358 /* 359 * For macros having optional next-line scope, 360 * keep the head open if there were no arguments. 361 * For `TP' and `TQ', always keep the head open. 362 */ 363 364 if (man_macro(tok)->flags & MAN_BSCOPED && 365 (tok == MAN_TP || tok == MAN_TQ || n == man->last)) { 366 man->flags |= MAN_BLINE; 367 return; 368 } 369 370 /* Close out the head and open the body. */ 371 372 man_unscope(man, n); 373 roff_body_alloc(man, line, ppos, tok); 374 man->flags &= ~ROFF_NONOFILL; 375 } 376 377 void 378 in_line_eoln(MACRO_PROT_ARGS) 379 { 380 int la; 381 char *p; 382 struct roff_node *n; 383 384 roff_elem_alloc(man, line, ppos, tok); 385 n = man->last; 386 387 if (tok == MAN_EX) 388 man->flags |= ROFF_NOFILL; 389 else if (tok == MAN_EE) 390 man->flags &= ~ROFF_NOFILL; 391 392 for (;;) { 393 if (buf[*pos] != '\0' && man->last != n && tok == MAN_PD) { 394 mandoc_msg(MANDOCERR_ARG_EXCESS, line, *pos, 395 "%s ... %s", roff_name[tok], buf + *pos); 396 break; 397 } 398 la = *pos; 399 if ( ! man_args(man, line, pos, buf, &p)) 400 break; 401 if (man_macro(tok)->flags & MAN_JOIN && 402 man->last->type == ROFFT_TEXT) 403 roff_word_append(man, p); 404 else 405 roff_word_alloc(man, line, la, p); 406 free(p); 407 } 408 409 /* 410 * Append NODE_EOS in case the last snipped argument 411 * ends with a dot, e.g. `.IR syslog (3).' 412 */ 413 414 if (n != man->last && 415 mandoc_eos(man->last->string, strlen(man->last->string))) 416 man->last->flags |= NODE_EOS; 417 418 /* 419 * If no arguments are specified and this is MAN_ESCOPED (i.e., 420 * next-line scoped), then set our mode to indicate that we're 421 * waiting for terms to load into our context. 422 */ 423 424 if (n == man->last && man_macro(tok)->flags & MAN_ESCOPED) { 425 man->flags |= MAN_ELINE; 426 return; 427 } 428 429 assert(man->last->type != ROFFT_ROOT); 430 man->next = ROFF_NEXT_SIBLING; 431 432 /* Rewind our element scope. */ 433 434 for ( ; man->last; man->last = man->last->parent) { 435 man->last->flags |= NODE_VALID; 436 if (man->last == n) 437 break; 438 } 439 440 /* Rewind next-line scoped ancestors, if any. */ 441 442 if (man_macro(tok)->flags & MAN_ESCOPED) 443 man_descope(man, line, ppos, NULL); 444 } 445 446 void 447 man_endparse(struct roff_man *man) 448 { 449 man_unscope(man, man->meta.first); 450 } 451 452 static int 453 man_args(struct roff_man *man, int line, int *pos, char *buf, char **v) 454 { 455 char *start; 456 457 assert(*pos); 458 *v = start = buf + *pos; 459 assert(' ' != *start); 460 461 if ('\0' == *start) 462 return 0; 463 464 *v = roff_getarg(man->roff, v, line, pos); 465 return 1; 466 } 467