Annotation of mandoc/mdoc_macro.c, Revision 1.131
1.131 ! schwarze 1: /* $Id: mdoc_macro.c,v 1.130 2014/03/30 19:47:48 schwarze Exp $ */
1.1 kristaps 2: /*
1.116 schwarze 3: * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.123 schwarze 4: * Copyright (c) 2010, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.8 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.8 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
1.1 kristaps 17: */
1.40 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
23: #include <ctype.h>
24: #include <stdlib.h>
25: #include <stdio.h>
26: #include <string.h>
1.38 kristaps 27: #include <time.h>
1.1 kristaps 28:
1.106 kristaps 29: #include "mdoc.h"
1.68 kristaps 30: #include "mandoc.h"
1.1 kristaps 31: #include "libmdoc.h"
1.64 kristaps 32: #include "libmandoc.h"
1.1 kristaps 33:
1.84 schwarze 34: enum rew { /* see rew_dohalt() */
35: REWIND_NONE,
36: REWIND_THIS,
37: REWIND_MORE,
1.98 kristaps 38: REWIND_FORCE,
1.84 schwarze 39: REWIND_LATER,
1.91 kristaps 40: REWIND_ERROR
1.51 kristaps 41: };
1.1 kristaps 42:
1.131 ! schwarze 43: static int blk_full(MACRO_PROT_ARGS);
! 44: static int blk_exp_close(MACRO_PROT_ARGS);
! 45: static int blk_part_exp(MACRO_PROT_ARGS);
! 46: static int blk_part_imp(MACRO_PROT_ARGS);
! 47: static int ctx_synopsis(MACRO_PROT_ARGS);
! 48: static int in_line_eoln(MACRO_PROT_ARGS);
! 49: static int in_line_argn(MACRO_PROT_ARGS);
! 50: static int in_line(MACRO_PROT_ARGS);
! 51: static int obsolete(MACRO_PROT_ARGS);
! 52: static int phrase_ta(MACRO_PROT_ARGS);
1.53 kristaps 53:
1.123 schwarze 54: static int dword(struct mdoc *, int, int, const char *,
55: enum mdelim, int);
1.131 ! schwarze 56: static int append_delims(struct mdoc *,
1.53 kristaps 57: int, int *, char *);
1.58 kristaps 58: static enum mdoct lookup(enum mdoct, const char *);
1.53 kristaps 59: static enum mdoct lookup_raw(const char *);
1.85 schwarze 60: static int make_pending(struct mdoc_node *, enum mdoct,
1.83 schwarze 61: struct mdoc *, int, int);
1.131 ! schwarze 62: static int phrase(struct mdoc *, int, int, char *);
! 63: static enum mdoct rew_alt(enum mdoct);
! 64: static enum rew rew_dohalt(enum mdoct, enum mdoc_type,
1.53 kristaps 65: const struct mdoc_node *);
1.131 ! schwarze 66: static int rew_elem(struct mdoc *, enum mdoct);
! 67: static int rew_last(struct mdoc *,
1.53 kristaps 68: const struct mdoc_node *);
1.131 ! schwarze 69: static int rew_sub(enum mdoc_type, struct mdoc *,
1.53 kristaps 70: enum mdoct, int, int);
1.1 kristaps 71:
72: const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.123 schwarze 73: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */
1.1 kristaps 74: { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
75: { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
76: { in_line_eoln, MDOC_PROLOGUE }, /* Os */
1.123 schwarze 77: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
78: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
79: { in_line_eoln, 0 }, /* Pp */
80: { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
81: { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
1.1 kristaps 82: { blk_full, MDOC_EXPLICIT }, /* Bd */
1.123 schwarze 83: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
1.1 kristaps 84: { blk_full, MDOC_EXPLICIT }, /* Bl */
1.123 schwarze 85: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
86: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
87: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
88: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
1.1 kristaps 89: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.129 schwarze 90: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
1.1 kristaps 91: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
1.123 schwarze 92: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
93: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
94: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.1 kristaps 95: { in_line_eoln, 0 }, /* Ex */
1.123 schwarze 96: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
97: { in_line_eoln, 0 }, /* Fd */
1.1 kristaps 98: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
1.123 schwarze 99: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
100: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.128 schwarze 101: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
1.18 kristaps 102: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.123 schwarze 103: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
104: { blk_full, MDOC_JOIN }, /* Nd */
105: { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1 kristaps 106: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
107: { obsolete, 0 }, /* Ot */
108: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
109: { in_line_eoln, 0 }, /* Rv */
1.123 schwarze 110: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
1.1 kristaps 111: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.123 schwarze 112: { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
1.42 kristaps 113: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.123 schwarze 114: { in_line_eoln, MDOC_JOIN }, /* %A */
115: { in_line_eoln, MDOC_JOIN }, /* %B */
116: { in_line_eoln, MDOC_JOIN }, /* %D */
117: { in_line_eoln, MDOC_JOIN }, /* %I */
118: { in_line_eoln, MDOC_JOIN }, /* %J */
1.1 kristaps 119: { in_line_eoln, 0 }, /* %N */
1.123 schwarze 120: { in_line_eoln, MDOC_JOIN }, /* %O */
1.1 kristaps 121: { in_line_eoln, 0 }, /* %P */
1.123 schwarze 122: { in_line_eoln, MDOC_JOIN }, /* %R */
123: { in_line_eoln, MDOC_JOIN }, /* %T */
1.1 kristaps 124: { in_line_eoln, 0 }, /* %V */
1.123 schwarze 125: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
126: MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
127: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
128: MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
129: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
1.1 kristaps 130: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
1.123 schwarze 131: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
132: MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
133: { blk_full, MDOC_EXPLICIT }, /* Bf */
134: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
135: MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
136: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
1.1 kristaps 137: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
138: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
139: { in_line_eoln, 0 }, /* Db */
1.123 schwarze 140: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
141: MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
142: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
143: MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
144: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
145: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
146: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
147: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
1.1 kristaps 148: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
149: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11 kristaps 150: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.123 schwarze 151: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
152: MDOC_IGNDELIM | MDOC_JOIN }, /* No */
153: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
154: MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
1.1 kristaps 155: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
156: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
1.123 schwarze 157: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
158: MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
1.52 kristaps 159: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
1.123 schwarze 160: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
161: MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
162: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
163: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
164: MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
165: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
166: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
167: MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
168: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
169: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
1.1 kristaps 170: { blk_full, MDOC_EXPLICIT }, /* Rs */
1.123 schwarze 171: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
172: MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
173: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
174: MDOC_EXPLICIT | MDOC_JOIN }, /* So */
175: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
1.1 kristaps 176: { in_line_eoln, 0 }, /* Sm */
1.123 schwarze 177: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
178: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
1.1 kristaps 179: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
1.123 schwarze 180: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
1.1 kristaps 181: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
182: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.123 schwarze 183: { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
184: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
185: MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
186: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
187: MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
188: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
189: MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
1.1 kristaps 190: { blk_full, MDOC_EXPLICIT }, /* Bk */
1.123 schwarze 191: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
1.1 kristaps 192: { in_line_eoln, 0 }, /* Bt */
193: { in_line_eoln, 0 }, /* Hf */
194: { obsolete, 0 }, /* Fr */
195: { in_line_eoln, 0 }, /* Ud */
1.69 kristaps 196: { in_line, 0 }, /* Lb */
1.123 schwarze 197: { in_line_eoln, 0 }, /* Lp */
198: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
199: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
200: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
201: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
202: MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
203: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
204: MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
205: { in_line_eoln, MDOC_JOIN }, /* %C */
1.1 kristaps 206: { obsolete, 0 }, /* Es */
207: { obsolete, 0 }, /* En */
208: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
1.123 schwarze 209: { in_line_eoln, MDOC_JOIN }, /* %Q */
1.20 kristaps 210: { in_line_eoln, 0 }, /* br */
211: { in_line_eoln, 0 }, /* sp */
1.37 kristaps 212: { in_line_eoln, 0 }, /* %U */
1.123 schwarze 213: { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
1.130 schwarze 214: { in_line_eoln, 0 }, /* ll */
1.1 kristaps 215: };
216:
217: const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
218:
219:
220: /*
221: * This is called at the end of parsing. It must traverse up the tree,
222: * closing out open [implicit] scopes. Obviously, open explicit scopes
223: * are errors.
224: */
225: int
1.119 schwarze 226: mdoc_macroend(struct mdoc *mdoc)
1.1 kristaps 227: {
228: struct mdoc_node *n;
229:
230: /* Scan for open explicit scopes. */
231:
1.119 schwarze 232: n = MDOC_VALID & mdoc->last->flags ?
1.131 ! schwarze 233: mdoc->last->parent : mdoc->last;
1.1 kristaps 234:
1.94 schwarze 235: for ( ; n; n = n->parent)
236: if (MDOC_BLOCK == n->type &&
237: MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
1.119 schwarze 238: mdoc_nmsg(mdoc, n, MANDOCERR_SCOPEEXIT);
1.1 kristaps 239:
1.32 kristaps 240: /* Rewind to the first. */
241:
1.119 schwarze 242: return(rew_last(mdoc, mdoc->first));
1.1 kristaps 243: }
244:
1.32 kristaps 245: /*
246: * Look up a macro from within a subsequent context.
247: */
1.53 kristaps 248: static enum mdoct
1.58 kristaps 249: lookup(enum mdoct from, const char *p)
1.28 kristaps 250: {
251:
252: if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
253: return(MDOC_MAX);
1.35 kristaps 254: return(lookup_raw(p));
1.28 kristaps 255: }
256:
1.32 kristaps 257: /*
258: * Lookup a macro following the initial line macro.
259: */
1.53 kristaps 260: static enum mdoct
1.35 kristaps 261: lookup_raw(const char *p)
1.1 kristaps 262: {
1.58 kristaps 263: enum mdoct res;
1.1 kristaps 264:
1.34 kristaps 265: if (MDOC_MAX == (res = mdoc_hash_find(p)))
1.28 kristaps 266: return(MDOC_MAX);
267: if (MDOC_CALLABLE & mdoc_macros[res].flags)
1.1 kristaps 268: return(res);
269: return(MDOC_MAX);
270: }
271:
272: static int
1.32 kristaps 273: rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
1.1 kristaps 274: {
1.100 kristaps 275: struct mdoc_node *n, *np;
1.1 kristaps 276:
277: assert(to);
278: mdoc->next = MDOC_NEXT_SIBLING;
279:
1.131 ! schwarze 280:
1.1 kristaps 281: while (mdoc->last != to) {
1.100 kristaps 282: /*
283: * Save the parent here, because we may delete the
1.119 schwarze 284: * mdoc->last node in the post-validation phase and reset
285: * it to mdoc->last->parent, causing a step in the closing
1.100 kristaps 286: * out to be lost.
287: */
288: np = mdoc->last->parent;
1.1 kristaps 289: if ( ! mdoc_valid_post(mdoc))
290: return(0);
1.99 kristaps 291: n = mdoc->last;
1.100 kristaps 292: mdoc->last = np;
1.1 kristaps 293: assert(mdoc->last);
1.99 kristaps 294: mdoc->last->last = n;
1.1 kristaps 295: }
296:
1.97 kristaps 297: return(mdoc_valid_post(mdoc));
1.1 kristaps 298: }
299:
1.32 kristaps 300: /*
1.84 schwarze 301: * For a block closing macro, return the corresponding opening one.
302: * Otherwise, return the macro itself.
1.32 kristaps 303: */
1.47 kristaps 304: static enum mdoct
305: rew_alt(enum mdoct tok)
1.1 kristaps 306: {
307: switch (tok) {
1.131 ! schwarze 308: case MDOC_Ac:
1.1 kristaps 309: return(MDOC_Ao);
1.131 ! schwarze 310: case MDOC_Bc:
1.1 kristaps 311: return(MDOC_Bo);
1.131 ! schwarze 312: case MDOC_Brc:
1.1 kristaps 313: return(MDOC_Bro);
1.131 ! schwarze 314: case MDOC_Dc:
1.1 kristaps 315: return(MDOC_Do);
1.131 ! schwarze 316: case MDOC_Ec:
1.1 kristaps 317: return(MDOC_Eo);
1.131 ! schwarze 318: case MDOC_Ed:
1.1 kristaps 319: return(MDOC_Bd);
1.131 ! schwarze 320: case MDOC_Ef:
1.1 kristaps 321: return(MDOC_Bf);
1.131 ! schwarze 322: case MDOC_Ek:
1.1 kristaps 323: return(MDOC_Bk);
1.131 ! schwarze 324: case MDOC_El:
1.1 kristaps 325: return(MDOC_Bl);
1.131 ! schwarze 326: case MDOC_Fc:
1.1 kristaps 327: return(MDOC_Fo);
1.131 ! schwarze 328: case MDOC_Oc:
1.1 kristaps 329: return(MDOC_Oo);
1.131 ! schwarze 330: case MDOC_Pc:
1.1 kristaps 331: return(MDOC_Po);
1.131 ! schwarze 332: case MDOC_Qc:
1.1 kristaps 333: return(MDOC_Qo);
1.131 ! schwarze 334: case MDOC_Re:
1.1 kristaps 335: return(MDOC_Rs);
1.131 ! schwarze 336: case MDOC_Sc:
1.1 kristaps 337: return(MDOC_So);
1.131 ! schwarze 338: case MDOC_Xc:
1.1 kristaps 339: return(MDOC_Xo);
340: default:
1.84 schwarze 341: return(tok);
1.1 kristaps 342: }
343: /* NOTREACHED */
344: }
345:
1.84 schwarze 346: /*
347: * Rewinding to tok, how do we have to handle *p?
348: * REWIND_NONE: *p would delimit tok, but no tok scope is open
349: * inside *p, so there is no need to rewind anything at all.
350: * REWIND_THIS: *p matches tok, so rewind *p and nothing else.
351: * REWIND_MORE: *p is implicit, rewind it and keep searching for tok.
1.98 kristaps 352: * REWIND_FORCE: *p is explicit, but tok is full, force rewinding *p.
1.84 schwarze 353: * REWIND_LATER: *p is explicit and still open, postpone rewinding.
354: * REWIND_ERROR: No tok block is open at all.
1.1 kristaps 355: */
1.51 kristaps 356: static enum rew
1.131 ! schwarze 357: rew_dohalt(enum mdoct tok, enum mdoc_type type,
1.47 kristaps 358: const struct mdoc_node *p)
1.1 kristaps 359: {
360:
1.86 schwarze 361: /*
362: * No matching token, no delimiting block, no broken block.
363: * This can happen when full implicit macros are called for
364: * the first time but try to rewind their previous
365: * instance anyway.
366: */
1.1 kristaps 367: if (MDOC_ROOT == p->type)
1.84 schwarze 368: return(MDOC_BLOCK == type &&
369: MDOC_EXPLICIT & mdoc_macros[tok].flags ?
370: REWIND_ERROR : REWIND_NONE);
1.86 schwarze 371:
372: /*
1.131 ! schwarze 373: * When starting to rewind, skip plain text
1.86 schwarze 374: * and nodes that have already been rewound.
375: */
1.84 schwarze 376: if (MDOC_TEXT == p->type || MDOC_VALID & p->flags)
377: return(REWIND_MORE);
378:
1.86 schwarze 379: /*
380: * The easiest case: Found a matching token.
381: * This applies to both blocks and elements.
382: */
1.84 schwarze 383: tok = rew_alt(tok);
384: if (tok == p->tok)
385: return(p->end ? REWIND_NONE :
386: type == p->type ? REWIND_THIS : REWIND_MORE);
387:
1.86 schwarze 388: /*
389: * While elements do require rewinding for themselves,
390: * they never affect rewinding of other nodes.
391: */
1.84 schwarze 392: if (MDOC_ELEM == p->type)
393: return(REWIND_MORE);
1.1 kristaps 394:
1.86 schwarze 395: /*
396: * Blocks delimited by our target token get REWIND_MORE.
1.131 ! schwarze 397: * Blocks delimiting our target token get REWIND_NONE.
1.86 schwarze 398: */
1.1 kristaps 399: switch (tok) {
1.131 ! schwarze 400: case MDOC_Bl:
1.84 schwarze 401: if (MDOC_It == p->tok)
402: return(REWIND_MORE);
1.1 kristaps 403: break;
1.131 ! schwarze 404: case MDOC_It:
1.1 kristaps 405: if (MDOC_BODY == p->type && MDOC_Bl == p->tok)
1.84 schwarze 406: return(REWIND_NONE);
1.1 kristaps 407: break;
1.84 schwarze 408: /*
409: * XXX Badly nested block handling still fails badly
410: * when one block is breaking two blocks of the same type.
411: * This is an incomplete and extremely ugly workaround,
412: * required to let the OpenBSD tree build.
413: */
1.131 ! schwarze 414: case MDOC_Oo:
1.84 schwarze 415: if (MDOC_Op == p->tok)
416: return(REWIND_MORE);
1.1 kristaps 417: break;
1.131 ! schwarze 418: case MDOC_Nm:
1.88 schwarze 419: return(REWIND_NONE);
1.131 ! schwarze 420: case MDOC_Nd:
1.19 kristaps 421: /* FALLTHROUGH */
1.131 ! schwarze 422: case MDOC_Ss:
1.1 kristaps 423: if (MDOC_BODY == p->type && MDOC_Sh == p->tok)
1.84 schwarze 424: return(REWIND_NONE);
1.1 kristaps 425: /* FALLTHROUGH */
1.131 ! schwarze 426: case MDOC_Sh:
1.84 schwarze 427: if (MDOC_Nd == p->tok || MDOC_Ss == p->tok ||
428: MDOC_Sh == p->tok)
429: return(REWIND_MORE);
1.1 kristaps 430: break;
431: default:
432: break;
433: }
434:
1.86 schwarze 435: /*
436: * Default block rewinding rules.
1.88 schwarze 437: * In particular, always skip block end markers,
438: * and let all blocks rewind Nm children.
1.86 schwarze 439: */
1.88 schwarze 440: if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
441: (MDOC_BLOCK == p->type &&
1.86 schwarze 442: ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
443: return(REWIND_MORE);
444:
445: /*
1.98 kristaps 446: * By default, closing out full blocks
447: * forces closing of broken explicit blocks,
448: * while closing out partial blocks
449: * allows delayed rewinding by default.
1.86 schwarze 450: */
1.98 kristaps 451: return (&blk_full == mdoc_macros[tok].fp ?
452: REWIND_FORCE : REWIND_LATER);
1.1 kristaps 453: }
454:
455: static int
1.47 kristaps 456: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1 kristaps 457: {
458: struct mdoc_node *n;
459:
460: n = mdoc->last;
461: if (MDOC_ELEM != n->type)
462: n = n->parent;
463: assert(MDOC_ELEM == n->type);
464: assert(tok == n->tok);
465:
466: return(rew_last(mdoc, n));
467: }
468:
1.83 schwarze 469: /*
470: * We are trying to close a block identified by tok,
471: * but the child block *broken is still open.
472: * Thus, postpone closing the tok block
473: * until the rew_sub call closing *broken.
474: */
475: static int
476: make_pending(struct mdoc_node *broken, enum mdoct tok,
1.119 schwarze 477: struct mdoc *mdoc, int line, int ppos)
1.83 schwarze 478: {
479: struct mdoc_node *breaker;
480:
481: /*
482: * Iterate backwards, searching for the block matching tok,
483: * that is, the block breaking the *broken block.
484: */
485: for (breaker = broken->parent; breaker; breaker = breaker->parent) {
486:
487: /*
488: * If the *broken block had already been broken before
489: * and we encounter its breaker, make the tok block
490: * pending on the inner breaker.
491: * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
492: * becomes "[A broken=[B [C->B B] tok=A] C]"
493: * and finally "[A [B->A [C->B B] A] C]".
494: */
495: if (breaker == broken->pending) {
496: broken = breaker;
497: continue;
498: }
499:
1.84 schwarze 500: if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
1.83 schwarze 501: continue;
502: if (MDOC_BODY == broken->type)
503: broken = broken->parent;
504:
505: /*
506: * Found the breaker.
507: * If another, outer breaker is already pending on
508: * the *broken block, we must not clobber the link
509: * to the outer breaker, but make it pending on the
510: * new, now inner breaker.
511: * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
512: * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
513: * and finally "[A [B->A [C->B A] B] C]".
514: */
515: if (broken->pending) {
516: struct mdoc_node *taker;
517:
518: /*
519: * If the breaker had also been broken before,
520: * it cannot take on the outer breaker itself,
521: * but must hand it on to its own breakers.
522: * Graphically, this is the following situation:
523: * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
524: * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
525: */
526: taker = breaker;
527: while (taker->pending)
528: taker = taker->pending;
529: taker->pending = broken->pending;
530: }
531: broken->pending = breaker;
1.119 schwarze 532: mandoc_vmsg(MANDOCERR_SCOPENEST, mdoc->parse, line, ppos,
1.131 ! schwarze 533: "%s breaks %s", mdoc_macronames[tok],
! 534: mdoc_macronames[broken->tok]);
1.83 schwarze 535: return(1);
536: }
537:
538: /*
539: * Found no matching block for tok.
540: * Are you trying to close a block that is not open?
541: */
542: return(0);
543: }
544:
1.1 kristaps 545: static int
1.131 ! schwarze 546: rew_sub(enum mdoc_type t, struct mdoc *mdoc,
1.47 kristaps 547: enum mdoct tok, int line, int ppos)
1.1 kristaps 548: {
549: struct mdoc_node *n;
550:
1.119 schwarze 551: n = mdoc->last;
1.84 schwarze 552: while (n) {
553: switch (rew_dohalt(tok, t, n)) {
1.131 ! schwarze 554: case REWIND_NONE:
1.84 schwarze 555: return(1);
1.131 ! schwarze 556: case REWIND_THIS:
1.124 schwarze 557: n->lastline = line -
558: (MDOC_NEWLINE & mdoc->flags &&
559: ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
1.84 schwarze 560: break;
1.131 ! schwarze 561: case REWIND_FORCE:
! 562: mandoc_vmsg(MANDOCERR_SCOPEBROKEN, mdoc->parse,
! 563: line, ppos, "%s breaks %s",
! 564: mdoc_macronames[tok],
! 565: mdoc_macronames[n->tok]);
1.98 kristaps 566: /* FALLTHROUGH */
1.131 ! schwarze 567: case REWIND_MORE:
1.124 schwarze 568: n->lastline = line -
569: (MDOC_NEWLINE & mdoc->flags ? 1 : 0);
1.84 schwarze 570: n = n->parent;
571: continue;
1.131 ! schwarze 572: case REWIND_LATER:
1.119 schwarze 573: if (make_pending(n, tok, mdoc, line, ppos) ||
1.98 kristaps 574: MDOC_BLOCK != t)
575: return(1);
576: /* FALLTHROUGH */
1.131 ! schwarze 577: case REWIND_ERROR:
1.119 schwarze 578: mdoc_pmsg(mdoc, line, ppos, MANDOCERR_NOSCOPE);
1.98 kristaps 579: return(1);
1.32 kristaps 580: }
1.84 schwarze 581: break;
1.1 kristaps 582: }
583:
584: assert(n);
1.119 schwarze 585: if ( ! rew_last(mdoc, n))
1.47 kristaps 586: return(0);
587:
588: /*
1.83 schwarze 589: * The current block extends an enclosing block.
590: * Now that the current block ends, close the enclosing block, too.
1.47 kristaps 591: */
1.83 schwarze 592: while (NULL != (n = n->pending)) {
1.119 schwarze 593: if ( ! rew_last(mdoc, n))
1.47 kristaps 594: return(0);
1.83 schwarze 595: if (MDOC_HEAD == n->type &&
1.119 schwarze 596: ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
1.47 kristaps 597: return(0);
598: }
599:
600: return(1);
1.1 kristaps 601: }
602:
1.105 kristaps 603: /*
604: * Allocate a word and check whether it's punctuation or not.
605: * Punctuation consists of those tokens found in mdoc_isdelim().
606: */
607: static int
1.123 schwarze 608: dword(struct mdoc *mdoc, int line, int col, const char *p,
609: enum mdelim d, int may_append)
1.105 kristaps 610: {
1.131 ! schwarze 611:
1.105 kristaps 612: if (DELIM_MAX == d)
613: d = mdoc_isdelim(p);
614:
1.123 schwarze 615: if (may_append &&
616: ! ((MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF) & mdoc->flags) &&
617: DELIM_NONE == d && MDOC_TEXT == mdoc->last->type &&
618: DELIM_NONE == mdoc_isdelim(mdoc->last->string)) {
619: mdoc_word_append(mdoc, p);
620: return(1);
621: }
622:
1.119 schwarze 623: if ( ! mdoc_word_alloc(mdoc, line, col, p))
1.105 kristaps 624: return(0);
625:
626: if (DELIM_OPEN == d)
1.119 schwarze 627: mdoc->last->flags |= MDOC_DELIMO;
1.108 schwarze 628:
629: /*
630: * Closing delimiters only suppress the preceding space
631: * when they follow something, not when they start a new
632: * block or element, and not when they follow `No'.
633: *
634: * XXX Explicitly special-casing MDOC_No here feels
635: * like a layering violation. Find a better way
636: * and solve this in the code related to `No'!
637: */
638:
1.119 schwarze 639: else if (DELIM_CLOSE == d && mdoc->last->prev &&
1.131 ! schwarze 640: mdoc->last->prev->tok != MDOC_No &&
! 641: mdoc->last->parent->tok != MDOC_Fd)
1.119 schwarze 642: mdoc->last->flags |= MDOC_DELIMC;
1.105 kristaps 643:
644: return(1);
645: }
1.1 kristaps 646:
647: static int
1.119 schwarze 648: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
1.1 kristaps 649: {
1.66 kristaps 650: int la;
1.55 kristaps 651: enum margserr ac;
1.1 kristaps 652: char *p;
653:
1.66 kristaps 654: if ('\0' == buf[*pos])
1.1 kristaps 655: return(1);
656:
657: for (;;) {
1.66 kristaps 658: la = *pos;
1.119 schwarze 659: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.1 kristaps 660:
1.53 kristaps 661: if (ARGS_ERROR == ac)
1.1 kristaps 662: return(0);
1.53 kristaps 663: else if (ARGS_EOLN == ac)
1.1 kristaps 664: break;
1.66 kristaps 665:
1.123 schwarze 666: dword(mdoc, line, la, p, DELIM_MAX, 1);
1.66 kristaps 667:
668: /*
669: * If we encounter end-of-sentence symbols, then trigger
670: * the double-space.
671: *
1.109 kristaps 672: * XXX: it's easy to allow this to propagate outward to
1.66 kristaps 673: * the last symbol, such that `. )' will cause the
674: * correct double-spacing. However, (1) groff isn't
675: * smart enough to do this and (2) it would require
676: * knowing which symbols break this behaviour, for
1.109 kristaps 677: * example, `. ;' shouldn't propagate the double-space.
1.66 kristaps 678: */
1.127 schwarze 679: if (mandoc_eos(p, strlen(p)))
1.119 schwarze 680: mdoc->last->flags |= MDOC_EOS;
1.1 kristaps 681: }
682:
683: return(1);
684: }
685:
686: /*
1.131 ! schwarze 687: * Close out block partial/full explicit.
1.1 kristaps 688: */
689: static int
690: blk_exp_close(MACRO_PROT_ARGS)
691: {
1.83 schwarze 692: struct mdoc_node *body; /* Our own body. */
693: struct mdoc_node *later; /* A sub-block starting later. */
694: struct mdoc_node *n; /* For searching backwards. */
695:
1.131 ! schwarze 696: int j, lastarg, maxargs, flushed, nl;
1.55 kristaps 697: enum margserr ac;
1.83 schwarze 698: enum mdoct atok, ntok;
1.1 kristaps 699: char *p;
700:
1.119 schwarze 701: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 702:
1.1 kristaps 703: switch (tok) {
1.131 ! schwarze 704: case MDOC_Ec:
1.1 kristaps 705: maxargs = 1;
706: break;
1.131 ! schwarze 707: case MDOC_Ek:
1.125 schwarze 708: mdoc->flags &= ~MDOC_KEEP;
1.1 kristaps 709: default:
710: maxargs = 0;
711: break;
712: }
713:
1.83 schwarze 714: /*
715: * Search backwards for beginnings of blocks,
716: * both of our own and of pending sub-blocks.
717: */
718: atok = rew_alt(tok);
719: body = later = NULL;
1.119 schwarze 720: for (n = mdoc->last; n; n = n->parent) {
1.83 schwarze 721: if (MDOC_VALID & n->flags)
722: continue;
723:
724: /* Remember the start of our own body. */
725: if (MDOC_BODY == n->type && atok == n->tok) {
1.87 kristaps 726: if (ENDBODY_NOT == n->end)
1.83 schwarze 727: body = n;
728: continue;
729: }
730:
1.88 schwarze 731: if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.83 schwarze 732: continue;
733: if (atok == n->tok) {
734: assert(body);
735:
736: /*
737: * Found the start of our own block.
738: * When there is no pending sub block,
739: * just proceed to closing out.
740: */
741: if (NULL == later)
742: break;
743:
1.131 ! schwarze 744: /*
1.83 schwarze 745: * When there is a pending sub block,
746: * postpone closing out the current block
747: * until the rew_sub() closing out the sub-block.
748: */
1.119 schwarze 749: make_pending(later, tok, mdoc, line, ppos);
1.83 schwarze 750:
751: /*
752: * Mark the place where the formatting - but not
753: * the scope - of the current block ends.
754: */
1.119 schwarze 755: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 756: atok, body, ENDBODY_SPACE))
757: return(0);
758: break;
759: }
760:
761: /*
762: * When finding an open sub block, remember the last
763: * open explicit block, or, in case there are only
764: * implicit ones, the first open implicit block.
765: */
766: if (later &&
767: MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
768: continue;
1.118 schwarze 769: if (MDOC_It != n->tok)
1.83 schwarze 770: later = n;
771: }
772:
1.1 kristaps 773: if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) {
1.68 kristaps 774: /* FIXME: do this in validate */
1.131 ! schwarze 775: if (buf[*pos])
1.119 schwarze 776: mdoc_pmsg(mdoc, line, ppos, MANDOCERR_ARGSLOST);
1.22 kristaps 777:
1.119 schwarze 778: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.22 kristaps 779: return(0);
1.119 schwarze 780: return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
1.1 kristaps 781: }
782:
1.119 schwarze 783: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 784: return(0);
785:
1.131 ! schwarze 786: if (NULL == later && maxargs > 0)
1.119 schwarze 787: if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
1.1 kristaps 788: return(0);
789:
1.27 kristaps 790: for (flushed = j = 0; ; j++) {
1.1 kristaps 791: lastarg = *pos;
792:
793: if (j == maxargs && ! flushed) {
1.119 schwarze 794: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 795: return(0);
796: flushed = 1;
797: }
798:
1.119 schwarze 799: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 800:
1.53 kristaps 801: if (ARGS_ERROR == ac)
1.1 kristaps 802: return(0);
1.53 kristaps 803: if (ARGS_PUNCT == ac)
1.1 kristaps 804: break;
1.53 kristaps 805: if (ARGS_EOLN == ac)
1.1 kristaps 806: break;
807:
1.54 kristaps 808: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
809:
810: if (MDOC_MAX == ntok) {
1.123 schwarze 811: if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX,
812: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 813: return(0);
1.54 kristaps 814: continue;
815: }
1.1 kristaps 816:
1.54 kristaps 817: if ( ! flushed) {
1.119 schwarze 818: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.54 kristaps 819: return(0);
820: flushed = 1;
821: }
1.122 schwarze 822:
823: mdoc->flags &= ~MDOC_NEWLINE;
824:
1.119 schwarze 825: if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
1.1 kristaps 826: return(0);
1.54 kristaps 827: break;
1.1 kristaps 828: }
829:
1.119 schwarze 830: if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 831: return(0);
832:
1.60 kristaps 833: if ( ! nl)
1.1 kristaps 834: return(1);
1.119 schwarze 835: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 836: }
837:
838: static int
839: in_line(MACRO_PROT_ARGS)
840: {
1.70 kristaps 841: int la, scope, cnt, nc, nl;
1.56 kristaps 842: enum margverr av;
1.53 kristaps 843: enum mdoct ntok;
1.55 kristaps 844: enum margserr ac;
1.67 schwarze 845: enum mdelim d;
1.53 kristaps 846: struct mdoc_arg *arg;
847: char *p;
1.1 kristaps 848:
1.119 schwarze 849: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 850:
1.4 kristaps 851: /*
852: * Whether we allow ignored elements (those without content,
853: * usually because of reserved words) to squeak by.
854: */
1.45 kristaps 855:
1.4 kristaps 856: switch (tok) {
1.131 ! schwarze 857: case MDOC_An:
1.26 kristaps 858: /* FALLTHROUGH */
1.131 ! schwarze 859: case MDOC_Ar:
1.4 kristaps 860: /* FALLTHROUGH */
1.131 ! schwarze 861: case MDOC_Fl:
1.4 kristaps 862: /* FALLTHROUGH */
1.131 ! schwarze 863: case MDOC_Mt:
1.12 kristaps 864: /* FALLTHROUGH */
1.131 ! schwarze 865: case MDOC_Nm:
1.26 kristaps 866: /* FALLTHROUGH */
1.131 ! schwarze 867: case MDOC_Pa:
1.4 kristaps 868: nc = 1;
869: break;
870: default:
871: nc = 0;
872: break;
873: }
874:
1.27 kristaps 875: for (arg = NULL;; ) {
1.1 kristaps 876: la = *pos;
1.119 schwarze 877: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 878:
1.56 kristaps 879: if (ARGV_WORD == av) {
1.1 kristaps 880: *pos = la;
881: break;
1.131 ! schwarze 882: }
1.56 kristaps 883: if (ARGV_EOLN == av)
1.1 kristaps 884: break;
1.56 kristaps 885: if (ARGV_ARG == av)
1.1 kristaps 886: continue;
887:
888: mdoc_argv_free(arg);
889: return(0);
890: }
891:
1.70 kristaps 892: for (cnt = scope = 0;; ) {
1.1 kristaps 893: la = *pos;
1.119 schwarze 894: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 895:
1.53 kristaps 896: if (ARGS_ERROR == ac)
1.1 kristaps 897: return(0);
1.53 kristaps 898: if (ARGS_EOLN == ac)
1.1 kristaps 899: break;
1.53 kristaps 900: if (ARGS_PUNCT == ac)
1.1 kristaps 901: break;
902:
1.53 kristaps 903: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 904:
1.131 ! schwarze 905: /*
1.4 kristaps 906: * In this case, we've located a submacro and must
907: * execute it. Close out scope, if open. If no
908: * elements have been generated, either create one (nc)
909: * or raise a warning.
910: */
1.1 kristaps 911:
1.53 kristaps 912: if (MDOC_MAX != ntok) {
1.119 schwarze 913: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 914: return(0);
1.4 kristaps 915: if (nc && 0 == cnt) {
1.131 ! schwarze 916: if ( ! mdoc_elem_alloc(mdoc,
! 917: line, ppos, tok, arg))
1.3 kristaps 918: return(0);
1.119 schwarze 919: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 920: return(0);
1.7 kristaps 921: } else if ( ! nc && 0 == cnt) {
922: mdoc_argv_free(arg);
1.119 schwarze 923: mdoc_pmsg(mdoc, line, ppos,
1.131 ! schwarze 924: MANDOCERR_MACROEMPTY);
1.7 kristaps 925: }
1.103 kristaps 926:
1.119 schwarze 927: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 928: return(0);
1.60 kristaps 929: if ( ! nl)
1.1 kristaps 930: return(1);
1.119 schwarze 931: return(append_delims(mdoc, line, pos, buf));
1.131 ! schwarze 932: }
1.1 kristaps 933:
1.131 ! schwarze 934: /*
1.4 kristaps 935: * Non-quote-enclosed punctuation. Set up our scope, if
936: * a word; rewind the scope, if a delimiter; then append
1.131 ! schwarze 937: * the word.
1.4 kristaps 938: */
1.1 kristaps 939:
1.105 kristaps 940: d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.3 kristaps 941:
1.70 kristaps 942: if (DELIM_NONE != d) {
943: /*
944: * If we encounter closing punctuation, no word
945: * has been omitted, no scope is open, and we're
946: * allowed to have an empty element, then start
947: * a new scope. `Ar', `Fl', and `Li', only do
948: * this once per invocation. There may be more
949: * of these (all of them?).
950: */
1.131 ! schwarze 951: if (0 == cnt && (nc || MDOC_Li == tok) &&
! 952: DELIM_CLOSE == d && ! scope) {
! 953: if ( ! mdoc_elem_alloc(mdoc,
! 954: line, ppos, tok, arg))
1.70 kristaps 955: return(0);
1.131 ! schwarze 956: if (MDOC_Ar == tok || MDOC_Li == tok ||
! 957: MDOC_Fl == tok)
1.70 kristaps 958: cnt++;
959: scope = 1;
960: }
961: /*
962: * Close out our scope, if one is open, before
963: * any punctuation.
964: */
1.119 schwarze 965: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 966: return(0);
1.70 kristaps 967: scope = 0;
968: } else if ( ! scope) {
1.119 schwarze 969: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 970: return(0);
1.70 kristaps 971: scope = 1;
1.1 kristaps 972: }
973:
1.67 schwarze 974: if (DELIM_NONE == d)
1.3 kristaps 975: cnt++;
1.105 kristaps 976:
1.123 schwarze 977: if ( ! dword(mdoc, line, la, p, d,
978: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 979: return(0);
1.39 kristaps 980:
981: /*
982: * `Fl' macros have their scope re-opened with each new
983: * word so that the `-' can be added to each one without
984: * having to parse out spaces.
985: */
1.70 kristaps 986: if (scope && MDOC_Fl == tok) {
1.119 schwarze 987: if ( ! rew_elem(mdoc, tok))
1.39 kristaps 988: return(0);
1.70 kristaps 989: scope = 0;
1.39 kristaps 990: }
1.1 kristaps 991: }
992:
1.119 schwarze 993: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 994: return(0);
1.4 kristaps 995:
996: /*
997: * If no elements have been collected and we're allowed to have
998: * empties (nc), open a scope and close it out. Otherwise,
999: * raise a warning.
1000: */
1.45 kristaps 1001:
1.4 kristaps 1002: if (nc && 0 == cnt) {
1.119 schwarze 1003: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.3 kristaps 1004: return(0);
1.119 schwarze 1005: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 1006: return(0);
1.7 kristaps 1007: } else if ( ! nc && 0 == cnt) {
1008: mdoc_argv_free(arg);
1.119 schwarze 1009: mdoc_pmsg(mdoc, line, ppos, MANDOCERR_MACROEMPTY);
1.7 kristaps 1010: }
1.4 kristaps 1011:
1.60 kristaps 1012: if ( ! nl)
1.1 kristaps 1013: return(1);
1.119 schwarze 1014: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1015: }
1016:
1017: static int
1018: blk_full(MACRO_PROT_ARGS)
1019: {
1.114 kristaps 1020: int la, nl, nparsed;
1.1 kristaps 1021: struct mdoc_arg *arg;
1.45 kristaps 1022: struct mdoc_node *head; /* save of head macro */
1.49 kristaps 1023: struct mdoc_node *body; /* save of body macro */
1.47 kristaps 1024: struct mdoc_node *n;
1.74 kristaps 1025: enum mdoc_type mtt;
1.53 kristaps 1026: enum mdoct ntok;
1.59 kristaps 1027: enum margserr ac, lac;
1.56 kristaps 1028: enum margverr av;
1.1 kristaps 1029: char *p;
1030:
1.119 schwarze 1031: nl = MDOC_NEWLINE & mdoc->flags;
1.63 kristaps 1032:
1.45 kristaps 1033: /* Close out prior implicit scope. */
1.19 kristaps 1034:
1.1 kristaps 1035: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.119 schwarze 1036: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 1037: return(0);
1.119 schwarze 1038: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1039: return(0);
1040: }
1041:
1.45 kristaps 1042: /*
1.109 kristaps 1043: * This routine accommodates implicitly- and explicitly-scoped
1.45 kristaps 1044: * macro openings. Implicit ones first close out prior scope
1045: * (seen above). Delay opening the head until necessary to
1046: * allow leading punctuation to print. Special consideration
1047: * for `It -column', which has phrase-part syntax instead of
1048: * regular child nodes.
1049: */
1050:
1.1 kristaps 1051: for (arg = NULL;; ) {
1.45 kristaps 1052: la = *pos;
1.119 schwarze 1053: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1054:
1.56 kristaps 1055: if (ARGV_WORD == av) {
1.45 kristaps 1056: *pos = la;
1.1 kristaps 1057: break;
1.131 ! schwarze 1058: }
1.1 kristaps 1059:
1.56 kristaps 1060: if (ARGV_EOLN == av)
1.1 kristaps 1061: break;
1.56 kristaps 1062: if (ARGV_ARG == av)
1.1 kristaps 1063: continue;
1064:
1065: mdoc_argv_free(arg);
1066: return(0);
1067: }
1068:
1.119 schwarze 1069: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1070: return(0);
1071:
1.49 kristaps 1072: head = body = NULL;
1.115 schwarze 1073:
1074: /*
1075: * Exception: Heads of `It' macros in `-diag' lists are not
1076: * parsed, even though `It' macros in general are parsed.
1077: */
1078: nparsed = MDOC_It == tok &&
1.131 ! schwarze 1079: MDOC_Bl == mdoc->last->parent->tok &&
! 1080: LIST_diag == mdoc->last->parent->norm->Bl.type;
1.1 kristaps 1081:
1.45 kristaps 1082: /*
1083: * The `Nd' macro has all arguments in its body: it's a hybrid
1084: * of block partial-explicit and full-implicit. Stupid.
1085: */
1.19 kristaps 1086:
1.45 kristaps 1087: if (MDOC_Nd == tok) {
1.119 schwarze 1088: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1089: return(0);
1.119 schwarze 1090: head = mdoc->last;
1091: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.19 kristaps 1092: return(0);
1.119 schwarze 1093: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.19 kristaps 1094: return(0);
1.119 schwarze 1095: body = mdoc->last;
1.123 schwarze 1096: }
1097:
1098: if (MDOC_Bk == tok)
1099: mdoc->flags |= MDOC_KEEP;
1.19 kristaps 1100:
1.59 kristaps 1101: ac = ARGS_ERROR;
1102:
1.62 kristaps 1103: for ( ; ; ) {
1.45 kristaps 1104: la = *pos;
1.75 kristaps 1105: /* Initialise last-phrase-type with ARGS_PEND. */
1106: lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.119 schwarze 1107: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.89 schwarze 1108:
1109: if (ARGS_PUNCT == ac)
1110: break;
1.1 kristaps 1111:
1.53 kristaps 1112: if (ARGS_ERROR == ac)
1.1 kristaps 1113: return(0);
1.75 kristaps 1114:
1115: if (ARGS_EOLN == ac) {
1116: if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1117: break;
1118: /*
1119: * This is necessary: if the last token on a
1120: * line is a `Ta' or tab, then we'll get
1121: * ARGS_EOLN, so we must be smart enough to
1122: * reopen our scope if the last parse was a
1123: * phrase or partial phrase.
1124: */
1.119 schwarze 1125: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.75 kristaps 1126: return(0);
1.119 schwarze 1127: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.75 kristaps 1128: return(0);
1.119 schwarze 1129: body = mdoc->last;
1.1 kristaps 1130: break;
1.75 kristaps 1131: }
1.45 kristaps 1132:
1.131 ! schwarze 1133: /*
1.73 kristaps 1134: * Emit leading punctuation (i.e., punctuation before
1135: * the MDOC_HEAD) for non-phrase types.
1136: */
1.45 kristaps 1137:
1.131 ! schwarze 1138: if (NULL == head &&
! 1139: ARGS_PEND != ac &&
! 1140: ARGS_PHRASE != ac &&
! 1141: ARGS_PPHRASE != ac &&
! 1142: ARGS_QWORD != ac &&
! 1143: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1144: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.45 kristaps 1145: return(0);
1146: continue;
1147: }
1148:
1.74 kristaps 1149: /* Open a head if one hasn't been opened. */
1.45 kristaps 1150:
1.74 kristaps 1151: if (NULL == head) {
1.119 schwarze 1152: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1153: return(0);
1.119 schwarze 1154: head = mdoc->last;
1.45 kristaps 1155: }
1156:
1.131 ! schwarze 1157: if (ARGS_PHRASE == ac ||
! 1158: ARGS_PEND == ac ||
! 1159: ARGS_PPHRASE == ac) {
1.73 kristaps 1160: /*
1.74 kristaps 1161: * If we haven't opened a body yet, rewind the
1162: * head; if we have, rewind that instead.
1163: */
1164:
1165: mtt = body ? MDOC_BODY : MDOC_HEAD;
1.119 schwarze 1166: if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
1.74 kristaps 1167: return(0);
1.131 ! schwarze 1168:
1.74 kristaps 1169: /* Then allocate our body context. */
1170:
1.119 schwarze 1171: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.74 kristaps 1172: return(0);
1.119 schwarze 1173: body = mdoc->last;
1.74 kristaps 1174:
1175: /*
1.73 kristaps 1176: * Process phrases: set whether we're in a
1177: * partial-phrase (this effects line handling)
1178: * then call down into the phrase parser.
1179: */
1.74 kristaps 1180:
1.65 kristaps 1181: if (ARGS_PPHRASE == ac)
1.119 schwarze 1182: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1183: if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1.119 schwarze 1184: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1185:
1.119 schwarze 1186: if ( ! phrase(mdoc, line, la, buf))
1.1 kristaps 1187: return(0);
1.71 kristaps 1188:
1.119 schwarze 1189: mdoc->flags &= ~MDOC_PPHRASE;
1.1 kristaps 1190: continue;
1191: }
1192:
1.131 ! schwarze 1193: ntok = nparsed || ARGS_QWORD == ac ?
! 1194: MDOC_MAX : lookup(tok, p);
1.54 kristaps 1195:
1196: if (MDOC_MAX == ntok) {
1.123 schwarze 1197: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1198: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1199: return(0);
1.53 kristaps 1200: continue;
1.45 kristaps 1201: }
1.53 kristaps 1202:
1.119 schwarze 1203: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.45 kristaps 1204: return(0);
1.53 kristaps 1205: break;
1.45 kristaps 1206: }
1.1 kristaps 1207:
1.45 kristaps 1208: if (NULL == head) {
1.119 schwarze 1209: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1210: return(0);
1.119 schwarze 1211: head = mdoc->last;
1.1 kristaps 1212: }
1.131 ! schwarze 1213:
1.119 schwarze 1214: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1215: return(0);
1.19 kristaps 1216:
1.49 kristaps 1217: /* If we've already opened our body, exit now. */
1.45 kristaps 1218:
1.49 kristaps 1219: if (NULL != body)
1.77 kristaps 1220: goto out;
1.19 kristaps 1221:
1.47 kristaps 1222: /*
1.49 kristaps 1223: * If there is an open (i.e., unvalidated) sub-block requiring
1224: * explicit close-out, postpone switching the current block from
1225: * head to body until the rew_sub() call closing out that
1226: * sub-block.
1.47 kristaps 1227: */
1.119 schwarze 1228: for (n = mdoc->last; n && n != head; n = n->parent) {
1.131 ! schwarze 1229: if (MDOC_BLOCK == n->type &&
! 1230: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
! 1231: ! (MDOC_VALID & n->flags)) {
1.47 kristaps 1232: n->pending = head;
1233: return(1);
1234: }
1235: }
1236:
1.45 kristaps 1237: /* Close out scopes to remain in a consistent state. */
1238:
1.119 schwarze 1239: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1240: return(0);
1.119 schwarze 1241: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1242: return(0);
1243:
1.77 kristaps 1244: out:
1.119 schwarze 1245: if ( ! (MDOC_FREECOL & mdoc->flags))
1.77 kristaps 1246: return(1);
1247:
1.119 schwarze 1248: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.77 kristaps 1249: return(0);
1.119 schwarze 1250: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.77 kristaps 1251: return(0);
1252:
1.119 schwarze 1253: mdoc->flags &= ~MDOC_FREECOL;
1.1 kristaps 1254: return(1);
1255: }
1256:
1257: static int
1258: blk_part_imp(MACRO_PROT_ARGS)
1259: {
1.63 kristaps 1260: int la, nl;
1.53 kristaps 1261: enum mdoct ntok;
1.55 kristaps 1262: enum margserr ac;
1.1 kristaps 1263: char *p;
1.43 kristaps 1264: struct mdoc_node *blk; /* saved block context */
1265: struct mdoc_node *body; /* saved body context */
1266: struct mdoc_node *n;
1.1 kristaps 1267:
1.119 schwarze 1268: nl = MDOC_NEWLINE & mdoc->flags;
1.63 kristaps 1269:
1.43 kristaps 1270: /*
1271: * A macro that spans to the end of the line. This is generally
1272: * (but not necessarily) called as the first macro. The block
1273: * has a head as the immediate child, which is always empty,
1274: * followed by zero or more opening punctuation nodes, then the
1275: * body (which may be empty, depending on the macro), then zero
1276: * or more closing punctuation nodes.
1277: */
1.32 kristaps 1278:
1.119 schwarze 1279: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.32 kristaps 1280: return(0);
1.43 kristaps 1281:
1.119 schwarze 1282: blk = mdoc->last;
1.43 kristaps 1283:
1.119 schwarze 1284: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1285: return(0);
1.119 schwarze 1286: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1287: return(0);
1288:
1.43 kristaps 1289: /*
1290: * Open the body scope "on-demand", that is, after we've
1291: * processed all our the leading delimiters (open parenthesis,
1292: * etc.).
1293: */
1.1 kristaps 1294:
1.43 kristaps 1295: for (body = NULL; ; ) {
1.32 kristaps 1296: la = *pos;
1.119 schwarze 1297: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.43 kristaps 1298:
1.53 kristaps 1299: if (ARGS_ERROR == ac)
1.1 kristaps 1300: return(0);
1.53 kristaps 1301: if (ARGS_EOLN == ac)
1.43 kristaps 1302: break;
1.53 kristaps 1303: if (ARGS_PUNCT == ac)
1.1 kristaps 1304: break;
1305:
1.53 kristaps 1306: if (NULL == body && ARGS_QWORD != ac &&
1.131 ! schwarze 1307: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1308: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.1 kristaps 1309: return(0);
1310: continue;
1.123 schwarze 1311: }
1.1 kristaps 1312:
1.43 kristaps 1313: if (NULL == body) {
1.119 schwarze 1314: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1315: return(0);
1.119 schwarze 1316: body = mdoc->last;
1.43 kristaps 1317: }
1318:
1.54 kristaps 1319: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1320:
1321: if (MDOC_MAX == ntok) {
1.123 schwarze 1322: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1323: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1324: return(0);
1.53 kristaps 1325: continue;
1326: }
1.43 kristaps 1327:
1.119 schwarze 1328: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1329: return(0);
1.53 kristaps 1330: break;
1.1 kristaps 1331: }
1332:
1.43 kristaps 1333: /* Clean-ups to leave in a consistent state. */
1334:
1.44 kristaps 1335: if (NULL == body) {
1.119 schwarze 1336: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.44 kristaps 1337: return(0);
1.119 schwarze 1338: body = mdoc->last;
1.44 kristaps 1339: }
1.43 kristaps 1340:
1.83 schwarze 1341: /*
1342: * If there is an open sub-block requiring explicit close-out,
1343: * postpone closing out the current block
1344: * until the rew_sub() call closing out the sub-block.
1345: */
1.119 schwarze 1346: for (n = mdoc->last; n && n != body && n != blk->parent;
1.131 ! schwarze 1347: n = n->parent) {
1.83 schwarze 1348: if (MDOC_BLOCK == n->type &&
1349: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1350: ! (MDOC_VALID & n->flags)) {
1.119 schwarze 1351: make_pending(n, tok, mdoc, line, ppos);
1352: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 1353: tok, body, ENDBODY_NOSPACE))
1354: return(0);
1355: return(1);
1356: }
1357: }
1358:
1.131 ! schwarze 1359: /*
1.32 kristaps 1360: * If we can't rewind to our body, then our scope has already
1361: * been closed by another macro (like `Oc' closing `Op'). This
1362: * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.47 kristaps 1363: * crufty use of `Op' breakage.
1.1 kristaps 1364: */
1.103 kristaps 1365: if (n != body)
1.131 ! schwarze 1366: mandoc_vmsg(MANDOCERR_SCOPENEST, mdoc->parse, line,
! 1367: ppos, "%s broken", mdoc_macronames[tok]);
1.43 kristaps 1368:
1.119 schwarze 1369: if (n && ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.32 kristaps 1370: return(0);
1.1 kristaps 1371:
1.32 kristaps 1372: /* Standard appending of delimiters. */
1.1 kristaps 1373:
1.119 schwarze 1374: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1375: return(0);
1376:
1.32 kristaps 1377: /* Rewind scope, if applicable. */
1.1 kristaps 1378:
1.119 schwarze 1379: if (n && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1380: return(0);
1.117 schwarze 1381:
1382: /* Move trailing .Ns out of scope. */
1383:
1384: for (n = body->child; n && n->next; n = n->next)
1385: /* Do nothing. */ ;
1386: if (n && MDOC_Ns == n->tok)
1.119 schwarze 1387: mdoc_node_relink(mdoc, n);
1.1 kristaps 1388:
1389: return(1);
1390: }
1391:
1392: static int
1393: blk_part_exp(MACRO_PROT_ARGS)
1394: {
1.60 kristaps 1395: int la, nl;
1.55 kristaps 1396: enum margserr ac;
1.43 kristaps 1397: struct mdoc_node *head; /* keep track of head */
1398: struct mdoc_node *body; /* keep track of body */
1.1 kristaps 1399: char *p;
1.53 kristaps 1400: enum mdoct ntok;
1.1 kristaps 1401:
1.119 schwarze 1402: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1403:
1.43 kristaps 1404: /*
1405: * The opening of an explicit macro having zero or more leading
1406: * punctuation nodes; a head with optional single element (the
1407: * case of `Eo'); and a body that may be empty.
1408: */
1.32 kristaps 1409:
1.119 schwarze 1410: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.131 ! schwarze 1411: return(0);
1.32 kristaps 1412:
1.43 kristaps 1413: for (head = body = NULL; ; ) {
1.32 kristaps 1414: la = *pos;
1.119 schwarze 1415: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1416:
1.53 kristaps 1417: if (ARGS_ERROR == ac)
1.1 kristaps 1418: return(0);
1.53 kristaps 1419: if (ARGS_PUNCT == ac)
1.1 kristaps 1420: break;
1.53 kristaps 1421: if (ARGS_EOLN == ac)
1.1 kristaps 1422: break;
1.43 kristaps 1423:
1424: /* Flush out leading punctuation. */
1425:
1.53 kristaps 1426: if (NULL == head && ARGS_QWORD != ac &&
1.131 ! schwarze 1427: DELIM_OPEN == mdoc_isdelim(p)) {
1.43 kristaps 1428: assert(NULL == body);
1.123 schwarze 1429: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.43 kristaps 1430: return(0);
1431: continue;
1.123 schwarze 1432: }
1.43 kristaps 1433:
1434: if (NULL == head) {
1435: assert(NULL == body);
1.119 schwarze 1436: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1437: return(0);
1.119 schwarze 1438: head = mdoc->last;
1.1 kristaps 1439: }
1440:
1.43 kristaps 1441: /*
1442: * `Eo' gobbles any data into the head, but most other
1443: * macros just immediately close out and begin the body.
1444: */
1445:
1446: if (NULL == body) {
1447: assert(head);
1448: /* No check whether it's a macro! */
1449: if (MDOC_Eo == tok)
1.123 schwarze 1450: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 0))
1.43 kristaps 1451: return(0);
1452:
1.119 schwarze 1453: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1454: return(0);
1.119 schwarze 1455: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1456: return(0);
1.119 schwarze 1457: body = mdoc->last;
1.43 kristaps 1458:
1459: if (MDOC_Eo == tok)
1460: continue;
1.1 kristaps 1461: }
1.43 kristaps 1462:
1463: assert(NULL != head && NULL != body);
1464:
1.54 kristaps 1465: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1466:
1467: if (MDOC_MAX == ntok) {
1.123 schwarze 1468: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1469: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1470: return(0);
1.53 kristaps 1471: continue;
1.43 kristaps 1472: }
1473:
1.119 schwarze 1474: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1475: return(0);
1.53 kristaps 1476: break;
1.1 kristaps 1477: }
1478:
1.43 kristaps 1479: /* Clean-up to leave in a consistent state. */
1.32 kristaps 1480:
1.112 schwarze 1481: if (NULL == head)
1.119 schwarze 1482: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1483: return(0);
1484:
1485: if (NULL == body) {
1.119 schwarze 1486: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1487: return(0);
1.119 schwarze 1488: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1489: return(0);
1490: }
1491:
1.32 kristaps 1492: /* Standard appending of delimiters. */
1493:
1.60 kristaps 1494: if ( ! nl)
1.1 kristaps 1495: return(1);
1.119 schwarze 1496: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1497: }
1498:
1499: static int
1500: in_line_argn(MACRO_PROT_ARGS)
1501: {
1.60 kristaps 1502: int la, flushed, j, maxargs, nl;
1.55 kristaps 1503: enum margserr ac;
1.56 kristaps 1504: enum margverr av;
1.53 kristaps 1505: struct mdoc_arg *arg;
1506: char *p;
1507: enum mdoct ntok;
1.1 kristaps 1508:
1.119 schwarze 1509: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1510:
1.46 kristaps 1511: /*
1512: * A line macro that has a fixed number of arguments (maxargs).
1513: * Only open the scope once the first non-leading-punctuation is
1514: * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1515: * keep it open until the maximum number of arguments are
1516: * exhausted.
1517: */
1.1 kristaps 1518:
1519: switch (tok) {
1.131 ! schwarze 1520: case MDOC_Ap:
1.1 kristaps 1521: /* FALLTHROUGH */
1.131 ! schwarze 1522: case MDOC_No:
1.1 kristaps 1523: /* FALLTHROUGH */
1.131 ! schwarze 1524: case MDOC_Ns:
1.1 kristaps 1525: /* FALLTHROUGH */
1.131 ! schwarze 1526: case MDOC_Ux:
1.1 kristaps 1527: maxargs = 0;
1528: break;
1.131 ! schwarze 1529: case MDOC_Bx:
1.101 kristaps 1530: /* FALLTHROUGH */
1.131 ! schwarze 1531: case MDOC_Xr:
1.42 kristaps 1532: maxargs = 2;
1533: break;
1.1 kristaps 1534: default:
1535: maxargs = 1;
1536: break;
1537: }
1538:
1.46 kristaps 1539: for (arg = NULL; ; ) {
1.32 kristaps 1540: la = *pos;
1.119 schwarze 1541: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1542:
1.56 kristaps 1543: if (ARGV_WORD == av) {
1.32 kristaps 1544: *pos = la;
1.1 kristaps 1545: break;
1.131 ! schwarze 1546: }
1.1 kristaps 1547:
1.56 kristaps 1548: if (ARGV_EOLN == av)
1.1 kristaps 1549: break;
1.56 kristaps 1550: if (ARGV_ARG == av)
1.1 kristaps 1551: continue;
1552:
1553: mdoc_argv_free(arg);
1554: return(0);
1555: }
1556:
1.46 kristaps 1557: for (flushed = j = 0; ; ) {
1.32 kristaps 1558: la = *pos;
1.119 schwarze 1559: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1560:
1.53 kristaps 1561: if (ARGS_ERROR == ac)
1.1 kristaps 1562: return(0);
1.53 kristaps 1563: if (ARGS_PUNCT == ac)
1.1 kristaps 1564: break;
1.53 kristaps 1565: if (ARGS_EOLN == ac)
1.1 kristaps 1566: break;
1567:
1.131 ! schwarze 1568: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
! 1569: ARGS_QWORD != ac && 0 == j &&
! 1570: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1571: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.46 kristaps 1572: return(0);
1573: continue;
1574: } else if (0 == j)
1.119 schwarze 1575: if ( ! mdoc_elem_alloc(mdoc, line, la, tok, arg))
1.46 kristaps 1576: return(0);
1577:
1578: if (j == maxargs && ! flushed) {
1.119 schwarze 1579: if ( ! rew_elem(mdoc, tok))
1.46 kristaps 1580: return(0);
1581: flushed = 1;
1582: }
1583:
1.54 kristaps 1584: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1585:
1586: if (MDOC_MAX != ntok) {
1.119 schwarze 1587: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1588: return(0);
1589: flushed = 1;
1.119 schwarze 1590: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1591: return(0);
1.46 kristaps 1592: j++;
1.1 kristaps 1593: break;
1594: }
1595:
1596: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.131 ! schwarze 1597: ARGS_QWORD != ac &&
! 1598: ! flushed &&
! 1599: DELIM_NONE != mdoc_isdelim(p)) {
1.119 schwarze 1600: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1601: return(0);
1602: flushed = 1;
1603: }
1.42 kristaps 1604:
1.123 schwarze 1605: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1606: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1607: return(0);
1.46 kristaps 1608: j++;
1.1 kristaps 1609: }
1610:
1.119 schwarze 1611: if (0 == j && ! mdoc_elem_alloc(mdoc, line, la, tok, arg))
1.46 kristaps 1612: return(0);
1613:
1614: /* Close out in a consistent state. */
1.32 kristaps 1615:
1.119 schwarze 1616: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1617: return(0);
1.60 kristaps 1618: if ( ! nl)
1.1 kristaps 1619: return(1);
1.119 schwarze 1620: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1621: }
1622:
1623: static int
1624: in_line_eoln(MACRO_PROT_ARGS)
1625: {
1.56 kristaps 1626: int la;
1.55 kristaps 1627: enum margserr ac;
1.56 kristaps 1628: enum margverr av;
1.53 kristaps 1629: struct mdoc_arg *arg;
1630: char *p;
1631: enum mdoct ntok;
1.1 kristaps 1632:
1633: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1634:
1.90 schwarze 1635: if (tok == MDOC_Pp)
1.119 schwarze 1636: rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1.90 schwarze 1637:
1.32 kristaps 1638: /* Parse macro arguments. */
1.1 kristaps 1639:
1.32 kristaps 1640: for (arg = NULL; ; ) {
1.1 kristaps 1641: la = *pos;
1.119 schwarze 1642: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1643:
1.56 kristaps 1644: if (ARGV_WORD == av) {
1.1 kristaps 1645: *pos = la;
1646: break;
1647: }
1.131 ! schwarze 1648: if (ARGV_EOLN == av)
1.1 kristaps 1649: break;
1.56 kristaps 1650: if (ARGV_ARG == av)
1.1 kristaps 1651: continue;
1652:
1653: mdoc_argv_free(arg);
1654: return(0);
1655: }
1656:
1.32 kristaps 1657: /* Open element scope. */
1658:
1.119 schwarze 1659: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1660: return(0);
1661:
1.32 kristaps 1662: /* Parse argument terms. */
1.1 kristaps 1663:
1664: for (;;) {
1665: la = *pos;
1.119 schwarze 1666: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1667:
1.53 kristaps 1668: if (ARGS_ERROR == ac)
1.1 kristaps 1669: return(0);
1.53 kristaps 1670: if (ARGS_EOLN == ac)
1.1 kristaps 1671: break;
1672:
1.53 kristaps 1673: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 1674:
1.53 kristaps 1675: if (MDOC_MAX == ntok) {
1.123 schwarze 1676: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1677: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1678: return(0);
1.53 kristaps 1679: continue;
1680: }
1.1 kristaps 1681:
1.119 schwarze 1682: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1683: return(0);
1.119 schwarze 1684: return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
1.1 kristaps 1685: }
1686:
1.32 kristaps 1687: /* Close out (no delimiters). */
1688:
1.119 schwarze 1689: return(rew_elem(mdoc, tok));
1.1 kristaps 1690: }
1691:
1692: static int
1.41 kristaps 1693: ctx_synopsis(MACRO_PROT_ARGS)
1694: {
1.60 kristaps 1695: int nl;
1696:
1.119 schwarze 1697: nl = MDOC_NEWLINE & mdoc->flags;
1.41 kristaps 1698:
1699: /* If we're not in the SYNOPSIS, go straight to in-line. */
1.119 schwarze 1700: if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1701: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1702:
1703: /* If we're a nested call, same place. */
1.60 kristaps 1704: if ( ! nl)
1.119 schwarze 1705: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1706:
1707: /*
1708: * XXX: this will open a block scope; however, if later we end
1709: * up formatting the block scope, then child nodes will inherit
1710: * the formatting. Be careful.
1711: */
1.88 schwarze 1712: if (MDOC_Nm == tok)
1.119 schwarze 1713: return(blk_full(mdoc, tok, line, ppos, pos, buf));
1.88 schwarze 1714: assert(MDOC_Vt == tok);
1.119 schwarze 1715: return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1716: }
1717:
1718: static int
1.1 kristaps 1719: obsolete(MACRO_PROT_ARGS)
1720: {
1721:
1.119 schwarze 1722: mdoc_pmsg(mdoc, line, ppos, MANDOCERR_MACROOBS);
1.103 kristaps 1723: return(1);
1.1 kristaps 1724: }
1725:
1.24 kristaps 1726: /*
1727: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1728: * They're unusual because they're basically free-form text until a
1729: * macro is encountered.
1730: */
1.1 kristaps 1731: static int
1.119 schwarze 1732: phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1.1 kristaps 1733: {
1.53 kristaps 1734: int la, pos;
1.75 kristaps 1735: enum margserr ac;
1.53 kristaps 1736: enum mdoct ntok;
1737: char *p;
1.1 kristaps 1738:
1.24 kristaps 1739: for (pos = ppos; ; ) {
1740: la = pos;
1.1 kristaps 1741:
1.119 schwarze 1742: ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
1.1 kristaps 1743:
1.75 kristaps 1744: if (ARGS_ERROR == ac)
1.24 kristaps 1745: return(0);
1.75 kristaps 1746: if (ARGS_EOLN == ac)
1.24 kristaps 1747: break;
1.1 kristaps 1748:
1.75 kristaps 1749: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1 kristaps 1750:
1.53 kristaps 1751: if (MDOC_MAX == ntok) {
1.123 schwarze 1752: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 1))
1.1 kristaps 1753: return(0);
1.53 kristaps 1754: continue;
1755: }
1.1 kristaps 1756:
1.119 schwarze 1757: if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
1.1 kristaps 1758: return(0);
1.119 schwarze 1759: return(append_delims(mdoc, line, &pos, buf));
1.1 kristaps 1760: }
1761:
1762: return(1);
1763: }
1.24 kristaps 1764:
1.75 kristaps 1765: static int
1766: phrase_ta(MACRO_PROT_ARGS)
1767: {
1.121 schwarze 1768: struct mdoc_node *n;
1.75 kristaps 1769: int la;
1770: enum mdoct ntok;
1771: enum margserr ac;
1772: char *p;
1773:
1.121 schwarze 1774: /* Make sure we are in a column list or ignore this macro. */
1775: n = mdoc->last;
1776: while (NULL != n && MDOC_Bl != n->tok)
1777: n = n->parent;
1778: if (NULL == n || LIST_column != n->norm->Bl.type) {
1779: mdoc_pmsg(mdoc, line, ppos, MANDOCERR_STRAYTA);
1780: return(1);
1781: }
1.75 kristaps 1782:
1.121 schwarze 1783: /* Advance to the next column. */
1.119 schwarze 1784: if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
1.75 kristaps 1785: return(0);
1.119 schwarze 1786: if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
1.75 kristaps 1787: return(0);
1788:
1789: for (;;) {
1790: la = *pos;
1.119 schwarze 1791: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.75 kristaps 1792:
1793: if (ARGS_ERROR == ac)
1794: return(0);
1795: if (ARGS_EOLN == ac)
1796: break;
1797:
1798: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1799:
1800: if (MDOC_MAX == ntok) {
1.123 schwarze 1801: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1802: MDOC_JOIN & mdoc_macros[tok].flags))
1.75 kristaps 1803: return(0);
1804: continue;
1805: }
1806:
1.119 schwarze 1807: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.75 kristaps 1808: return(0);
1.119 schwarze 1809: return(append_delims(mdoc, line, pos, buf));
1.75 kristaps 1810: }
1811:
1812: return(1);
1813: }
CVSweb