Annotation of mandoc/mdoc_macro.c, Revision 1.137
1.137 ! schwarze 1: /* $Id: mdoc_macro.c,v 1.136 2014/07/04 16:12:08 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 phrase_ta(MACRO_PROT_ARGS);
1.53 kristaps 52:
1.123 schwarze 53: static int dword(struct mdoc *, int, int, const char *,
54: enum mdelim, int);
1.131 schwarze 55: static int append_delims(struct mdoc *,
1.53 kristaps 56: int, int *, char *);
1.58 kristaps 57: static enum mdoct lookup(enum mdoct, const char *);
1.53 kristaps 58: static enum mdoct lookup_raw(const char *);
1.85 schwarze 59: static int make_pending(struct mdoc_node *, enum mdoct,
1.83 schwarze 60: struct mdoc *, int, int);
1.131 schwarze 61: static int phrase(struct mdoc *, int, int, char *);
62: static enum mdoct rew_alt(enum mdoct);
63: static enum rew rew_dohalt(enum mdoct, enum mdoc_type,
1.53 kristaps 64: const struct mdoc_node *);
1.131 schwarze 65: static int rew_elem(struct mdoc *, enum mdoct);
66: static int rew_last(struct mdoc *,
1.53 kristaps 67: const struct mdoc_node *);
1.131 schwarze 68: static int rew_sub(enum mdoc_type, struct mdoc *,
1.53 kristaps 69: enum mdoct, int, int);
1.1 kristaps 70:
71: const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
1.123 schwarze 72: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ap */
1.1 kristaps 73: { in_line_eoln, MDOC_PROLOGUE }, /* Dd */
74: { in_line_eoln, MDOC_PROLOGUE }, /* Dt */
75: { in_line_eoln, MDOC_PROLOGUE }, /* Os */
1.123 schwarze 76: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Sh */
77: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* Ss */
78: { in_line_eoln, 0 }, /* Pp */
79: { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* D1 */
80: { blk_part_imp, MDOC_PARSED | MDOC_JOIN }, /* Dl */
1.1 kristaps 81: { blk_full, MDOC_EXPLICIT }, /* Bd */
1.123 schwarze 82: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ed */
1.1 kristaps 83: { blk_full, MDOC_EXPLICIT }, /* Bl */
1.123 schwarze 84: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* El */
85: { blk_full, MDOC_PARSED | MDOC_JOIN }, /* It */
86: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ad */
87: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* An */
1.1 kristaps 88: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
1.129 schwarze 89: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Cd */
1.1 kristaps 90: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cm */
1.123 schwarze 91: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Dv */
92: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Er */
93: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ev */
1.1 kristaps 94: { in_line_eoln, 0 }, /* Ex */
1.123 schwarze 95: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fa */
96: { in_line_eoln, 0 }, /* Fd */
1.1 kristaps 97: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fl */
1.123 schwarze 98: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fn */
99: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
1.128 schwarze 100: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ic */
1.18 kristaps 101: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
1.123 schwarze 102: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Li */
103: { blk_full, MDOC_JOIN }, /* Nd */
104: { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Nm */
1.1 kristaps 105: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED }, /* Op */
1.132 schwarze 106: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ot */
1.1 kristaps 107: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Pa */
108: { in_line_eoln, 0 }, /* Rv */
1.123 schwarze 109: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* St */
1.1 kristaps 110: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Va */
1.123 schwarze 111: { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
1.42 kristaps 112: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
1.123 schwarze 113: { in_line_eoln, MDOC_JOIN }, /* %A */
114: { in_line_eoln, MDOC_JOIN }, /* %B */
115: { in_line_eoln, MDOC_JOIN }, /* %D */
116: { in_line_eoln, MDOC_JOIN }, /* %I */
117: { in_line_eoln, MDOC_JOIN }, /* %J */
1.1 kristaps 118: { in_line_eoln, 0 }, /* %N */
1.123 schwarze 119: { in_line_eoln, MDOC_JOIN }, /* %O */
1.1 kristaps 120: { in_line_eoln, 0 }, /* %P */
1.123 schwarze 121: { in_line_eoln, MDOC_JOIN }, /* %R */
122: { in_line_eoln, MDOC_JOIN }, /* %T */
1.1 kristaps 123: { in_line_eoln, 0 }, /* %V */
1.123 schwarze 124: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
125: MDOC_EXPLICIT | MDOC_JOIN }, /* Ac */
126: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
127: MDOC_EXPLICIT | MDOC_JOIN }, /* Ao */
128: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Aq */
1.1 kristaps 129: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* At */
1.123 schwarze 130: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
131: MDOC_EXPLICIT | MDOC_JOIN }, /* Bc */
132: { blk_full, MDOC_EXPLICIT }, /* Bf */
133: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
134: MDOC_EXPLICIT | MDOC_JOIN }, /* Bo */
135: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Bq */
1.1 kristaps 136: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bsx */
137: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Bx */
138: { in_line_eoln, 0 }, /* Db */
1.123 schwarze 139: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
140: MDOC_EXPLICIT | MDOC_JOIN }, /* Dc */
141: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
142: MDOC_EXPLICIT | MDOC_JOIN }, /* Do */
143: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Dq */
144: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Ec */
145: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ef */
146: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Em */
1.1 kristaps 147: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Eo */
148: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Fx */
1.11 kristaps 149: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
1.123 schwarze 150: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
151: MDOC_IGNDELIM | MDOC_JOIN }, /* No */
152: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED |
153: MDOC_IGNDELIM | MDOC_JOIN }, /* Ns */
1.1 kristaps 154: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Nx */
155: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ox */
1.123 schwarze 156: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
157: MDOC_EXPLICIT | MDOC_JOIN }, /* Pc */
1.52 kristaps 158: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
1.123 schwarze 159: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
160: MDOC_EXPLICIT | MDOC_JOIN }, /* Po */
161: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Pq */
162: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
163: MDOC_EXPLICIT | MDOC_JOIN }, /* Qc */
164: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ql */
165: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
166: MDOC_EXPLICIT | MDOC_JOIN }, /* Qo */
167: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Qq */
168: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Re */
1.1 kristaps 169: { blk_full, MDOC_EXPLICIT }, /* Rs */
1.123 schwarze 170: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
171: MDOC_EXPLICIT | MDOC_JOIN }, /* Sc */
172: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
173: MDOC_EXPLICIT | MDOC_JOIN }, /* So */
174: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sq */
1.1 kristaps 175: { in_line_eoln, 0 }, /* Sm */
1.123 schwarze 176: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sx */
177: { in_line, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Sy */
1.1 kristaps 178: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Tn */
1.123 schwarze 179: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ux */
1.1 kristaps 180: { blk_exp_close, MDOC_EXPLICIT | MDOC_CALLABLE | MDOC_PARSED }, /* Xc */
181: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED | MDOC_EXPLICIT }, /* Xo */
1.123 schwarze 182: { blk_full, MDOC_EXPLICIT | MDOC_CALLABLE }, /* Fo */
183: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
184: MDOC_EXPLICIT | MDOC_JOIN }, /* Fc */
185: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
186: MDOC_EXPLICIT | MDOC_JOIN }, /* Oo */
187: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
188: MDOC_EXPLICIT | MDOC_JOIN }, /* Oc */
1.1 kristaps 189: { blk_full, MDOC_EXPLICIT }, /* Bk */
1.123 schwarze 190: { blk_exp_close, MDOC_EXPLICIT | MDOC_JOIN }, /* Ek */
1.1 kristaps 191: { in_line_eoln, 0 }, /* Bt */
192: { in_line_eoln, 0 }, /* Hf */
1.132 schwarze 193: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Fr */
1.1 kristaps 194: { in_line_eoln, 0 }, /* Ud */
1.69 kristaps 195: { in_line, 0 }, /* Lb */
1.123 schwarze 196: { in_line_eoln, 0 }, /* Lp */
197: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
198: { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
199: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Brq */
200: { blk_part_exp, MDOC_CALLABLE | MDOC_PARSED |
201: MDOC_EXPLICIT | MDOC_JOIN }, /* Bro */
202: { blk_exp_close, MDOC_CALLABLE | MDOC_PARSED |
203: MDOC_EXPLICIT | MDOC_JOIN }, /* Brc */
204: { in_line_eoln, MDOC_JOIN }, /* %C */
1.132 schwarze 205: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Es */
206: { blk_part_imp, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* En */
1.1 kristaps 207: { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Dx */
1.123 schwarze 208: { in_line_eoln, MDOC_JOIN }, /* %Q */
1.20 kristaps 209: { in_line_eoln, 0 }, /* br */
210: { in_line_eoln, 0 }, /* sp */
1.37 kristaps 211: { in_line_eoln, 0 }, /* %U */
1.123 schwarze 212: { phrase_ta, MDOC_CALLABLE | MDOC_PARSED | MDOC_JOIN }, /* Ta */
1.130 schwarze 213: { in_line_eoln, 0 }, /* ll */
1.1 kristaps 214: };
215:
216: const struct mdoc_macro * const mdoc_macros = __mdoc_macros;
217:
218:
219: /*
220: * This is called at the end of parsing. It must traverse up the tree,
221: * closing out open [implicit] scopes. Obviously, open explicit scopes
222: * are errors.
223: */
224: int
1.119 schwarze 225: mdoc_macroend(struct mdoc *mdoc)
1.1 kristaps 226: {
227: struct mdoc_node *n;
228:
229: /* Scan for open explicit scopes. */
230:
1.119 schwarze 231: n = MDOC_VALID & mdoc->last->flags ?
1.131 schwarze 232: mdoc->last->parent : mdoc->last;
1.1 kristaps 233:
1.94 schwarze 234: for ( ; n; n = n->parent)
235: if (MDOC_BLOCK == n->type &&
236: MDOC_EXPLICIT & mdoc_macros[n->tok].flags)
1.137 ! schwarze 237: mandoc_msg(MANDOCERR_BLK_NOEND, mdoc->parse,
! 238: n->line, n->pos, mdoc_macronames[n->tok]);
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.137 ! schwarze 532: mandoc_vmsg(MANDOCERR_BLK_NEST, 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:
1.137 ! schwarze 562: mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
1.131 schwarze 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.137 ! schwarze 578: mandoc_msg(MANDOCERR_BLK_NOTOPEN,
! 579: mdoc->parse, line, ppos,
! 580: mdoc_macronames[tok]);
1.98 kristaps 581: return(1);
1.32 kristaps 582: }
1.84 schwarze 583: break;
1.1 kristaps 584: }
585:
586: assert(n);
1.119 schwarze 587: if ( ! rew_last(mdoc, n))
1.47 kristaps 588: return(0);
589:
590: /*
1.83 schwarze 591: * The current block extends an enclosing block.
592: * Now that the current block ends, close the enclosing block, too.
1.47 kristaps 593: */
1.83 schwarze 594: while (NULL != (n = n->pending)) {
1.119 schwarze 595: if ( ! rew_last(mdoc, n))
1.47 kristaps 596: return(0);
1.83 schwarze 597: if (MDOC_HEAD == n->type &&
1.119 schwarze 598: ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
1.47 kristaps 599: return(0);
600: }
601:
602: return(1);
1.1 kristaps 603: }
604:
1.105 kristaps 605: /*
606: * Allocate a word and check whether it's punctuation or not.
607: * Punctuation consists of those tokens found in mdoc_isdelim().
608: */
609: static int
1.123 schwarze 610: dword(struct mdoc *mdoc, int line, int col, const char *p,
611: enum mdelim d, int may_append)
1.105 kristaps 612: {
1.131 schwarze 613:
1.105 kristaps 614: if (DELIM_MAX == d)
615: d = mdoc_isdelim(p);
616:
1.123 schwarze 617: if (may_append &&
618: ! ((MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF) & mdoc->flags) &&
619: DELIM_NONE == d && MDOC_TEXT == mdoc->last->type &&
620: DELIM_NONE == mdoc_isdelim(mdoc->last->string)) {
621: mdoc_word_append(mdoc, p);
622: return(1);
623: }
624:
1.119 schwarze 625: if ( ! mdoc_word_alloc(mdoc, line, col, p))
1.105 kristaps 626: return(0);
627:
628: if (DELIM_OPEN == d)
1.119 schwarze 629: mdoc->last->flags |= MDOC_DELIMO;
1.108 schwarze 630:
631: /*
632: * Closing delimiters only suppress the preceding space
633: * when they follow something, not when they start a new
634: * block or element, and not when they follow `No'.
635: *
636: * XXX Explicitly special-casing MDOC_No here feels
637: * like a layering violation. Find a better way
638: * and solve this in the code related to `No'!
639: */
640:
1.119 schwarze 641: else if (DELIM_CLOSE == d && mdoc->last->prev &&
1.131 schwarze 642: mdoc->last->prev->tok != MDOC_No &&
643: mdoc->last->parent->tok != MDOC_Fd)
1.119 schwarze 644: mdoc->last->flags |= MDOC_DELIMC;
1.105 kristaps 645:
646: return(1);
647: }
1.1 kristaps 648:
649: static int
1.119 schwarze 650: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
1.1 kristaps 651: {
1.66 kristaps 652: int la;
1.55 kristaps 653: enum margserr ac;
1.1 kristaps 654: char *p;
655:
1.66 kristaps 656: if ('\0' == buf[*pos])
1.1 kristaps 657: return(1);
658:
659: for (;;) {
1.66 kristaps 660: la = *pos;
1.119 schwarze 661: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.1 kristaps 662:
1.53 kristaps 663: if (ARGS_ERROR == ac)
1.1 kristaps 664: return(0);
1.53 kristaps 665: else if (ARGS_EOLN == ac)
1.1 kristaps 666: break;
1.66 kristaps 667:
1.123 schwarze 668: dword(mdoc, line, la, p, DELIM_MAX, 1);
1.66 kristaps 669:
670: /*
671: * If we encounter end-of-sentence symbols, then trigger
672: * the double-space.
673: *
1.109 kristaps 674: * XXX: it's easy to allow this to propagate outward to
1.66 kristaps 675: * the last symbol, such that `. )' will cause the
676: * correct double-spacing. However, (1) groff isn't
677: * smart enough to do this and (2) it would require
678: * knowing which symbols break this behaviour, for
1.109 kristaps 679: * example, `. ;' shouldn't propagate the double-space.
1.66 kristaps 680: */
1.127 schwarze 681: if (mandoc_eos(p, strlen(p)))
1.119 schwarze 682: mdoc->last->flags |= MDOC_EOS;
1.1 kristaps 683: }
684:
685: return(1);
686: }
687:
688: /*
1.131 schwarze 689: * Close out block partial/full explicit.
1.1 kristaps 690: */
691: static int
692: blk_exp_close(MACRO_PROT_ARGS)
693: {
1.83 schwarze 694: struct mdoc_node *body; /* Our own body. */
695: struct mdoc_node *later; /* A sub-block starting later. */
696: struct mdoc_node *n; /* For searching backwards. */
697:
1.131 schwarze 698: int j, lastarg, maxargs, flushed, nl;
1.55 kristaps 699: enum margserr ac;
1.83 schwarze 700: enum mdoct atok, ntok;
1.1 kristaps 701: char *p;
702:
1.119 schwarze 703: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 704:
1.1 kristaps 705: switch (tok) {
1.131 schwarze 706: case MDOC_Ec:
1.1 kristaps 707: maxargs = 1;
708: break;
1.131 schwarze 709: case MDOC_Ek:
1.125 schwarze 710: mdoc->flags &= ~MDOC_KEEP;
1.1 kristaps 711: default:
712: maxargs = 0;
713: break;
714: }
715:
1.83 schwarze 716: /*
717: * Search backwards for beginnings of blocks,
718: * both of our own and of pending sub-blocks.
719: */
720: atok = rew_alt(tok);
721: body = later = NULL;
1.119 schwarze 722: for (n = mdoc->last; n; n = n->parent) {
1.83 schwarze 723: if (MDOC_VALID & n->flags)
724: continue;
725:
726: /* Remember the start of our own body. */
727: if (MDOC_BODY == n->type && atok == n->tok) {
1.87 kristaps 728: if (ENDBODY_NOT == n->end)
1.83 schwarze 729: body = n;
730: continue;
731: }
732:
1.88 schwarze 733: if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.83 schwarze 734: continue;
735: if (atok == n->tok) {
736: assert(body);
737:
738: /*
739: * Found the start of our own block.
740: * When there is no pending sub block,
741: * just proceed to closing out.
742: */
743: if (NULL == later)
744: break;
745:
1.131 schwarze 746: /*
1.83 schwarze 747: * When there is a pending sub block,
748: * postpone closing out the current block
749: * until the rew_sub() closing out the sub-block.
750: */
1.119 schwarze 751: make_pending(later, tok, mdoc, line, ppos);
1.83 schwarze 752:
753: /*
754: * Mark the place where the formatting - but not
755: * the scope - of the current block ends.
756: */
1.119 schwarze 757: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 758: atok, body, ENDBODY_SPACE))
759: return(0);
760: break;
761: }
762:
763: /*
764: * When finding an open sub block, remember the last
765: * open explicit block, or, in case there are only
766: * implicit ones, the first open implicit block.
767: */
768: if (later &&
769: MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
770: continue;
1.118 schwarze 771: if (MDOC_It != n->tok)
1.83 schwarze 772: later = n;
773: }
774:
1.136 schwarze 775: if ( ! (MDOC_PARSED & mdoc_macros[tok].flags)) {
776: if ('\0' != buf[*pos])
777: mandoc_vmsg(MANDOCERR_ARG_SKIP,
778: mdoc->parse, line, ppos,
779: "%s %s", mdoc_macronames[tok],
780: buf + *pos);
1.119 schwarze 781: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.22 kristaps 782: return(0);
1.119 schwarze 783: return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
1.1 kristaps 784: }
785:
1.119 schwarze 786: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 787: return(0);
788:
1.131 schwarze 789: if (NULL == later && maxargs > 0)
1.119 schwarze 790: if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
1.1 kristaps 791: return(0);
792:
1.27 kristaps 793: for (flushed = j = 0; ; j++) {
1.1 kristaps 794: lastarg = *pos;
795:
796: if (j == maxargs && ! flushed) {
1.119 schwarze 797: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 798: return(0);
799: flushed = 1;
800: }
801:
1.119 schwarze 802: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 803:
1.53 kristaps 804: if (ARGS_ERROR == ac)
1.1 kristaps 805: return(0);
1.53 kristaps 806: if (ARGS_PUNCT == ac)
1.1 kristaps 807: break;
1.53 kristaps 808: if (ARGS_EOLN == ac)
1.1 kristaps 809: break;
810:
1.54 kristaps 811: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
812:
813: if (MDOC_MAX == ntok) {
1.123 schwarze 814: if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX,
815: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 816: return(0);
1.54 kristaps 817: continue;
818: }
1.1 kristaps 819:
1.54 kristaps 820: if ( ! flushed) {
1.119 schwarze 821: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.54 kristaps 822: return(0);
823: flushed = 1;
824: }
1.122 schwarze 825:
826: mdoc->flags &= ~MDOC_NEWLINE;
827:
1.119 schwarze 828: if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
1.1 kristaps 829: return(0);
1.54 kristaps 830: break;
1.1 kristaps 831: }
832:
1.119 schwarze 833: if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 834: return(0);
835:
1.60 kristaps 836: if ( ! nl)
1.1 kristaps 837: return(1);
1.119 schwarze 838: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 839: }
840:
841: static int
842: in_line(MACRO_PROT_ARGS)
843: {
1.70 kristaps 844: int la, scope, cnt, nc, nl;
1.56 kristaps 845: enum margverr av;
1.53 kristaps 846: enum mdoct ntok;
1.55 kristaps 847: enum margserr ac;
1.67 schwarze 848: enum mdelim d;
1.53 kristaps 849: struct mdoc_arg *arg;
850: char *p;
1.1 kristaps 851:
1.119 schwarze 852: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 853:
1.4 kristaps 854: /*
855: * Whether we allow ignored elements (those without content,
856: * usually because of reserved words) to squeak by.
857: */
1.45 kristaps 858:
1.4 kristaps 859: switch (tok) {
1.131 schwarze 860: case MDOC_An:
1.26 kristaps 861: /* FALLTHROUGH */
1.131 schwarze 862: case MDOC_Ar:
1.4 kristaps 863: /* FALLTHROUGH */
1.131 schwarze 864: case MDOC_Fl:
1.4 kristaps 865: /* FALLTHROUGH */
1.131 schwarze 866: case MDOC_Mt:
1.12 kristaps 867: /* FALLTHROUGH */
1.131 schwarze 868: case MDOC_Nm:
1.26 kristaps 869: /* FALLTHROUGH */
1.131 schwarze 870: case MDOC_Pa:
1.4 kristaps 871: nc = 1;
872: break;
873: default:
874: nc = 0;
875: break;
876: }
877:
1.27 kristaps 878: for (arg = NULL;; ) {
1.1 kristaps 879: la = *pos;
1.119 schwarze 880: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 881:
1.56 kristaps 882: if (ARGV_WORD == av) {
1.1 kristaps 883: *pos = la;
884: break;
1.131 schwarze 885: }
1.56 kristaps 886: if (ARGV_EOLN == av)
1.1 kristaps 887: break;
1.56 kristaps 888: if (ARGV_ARG == av)
1.1 kristaps 889: continue;
890:
891: mdoc_argv_free(arg);
892: return(0);
893: }
894:
1.70 kristaps 895: for (cnt = scope = 0;; ) {
1.1 kristaps 896: la = *pos;
1.119 schwarze 897: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 898:
1.53 kristaps 899: if (ARGS_ERROR == ac)
1.1 kristaps 900: return(0);
1.53 kristaps 901: if (ARGS_EOLN == ac)
1.1 kristaps 902: break;
1.53 kristaps 903: if (ARGS_PUNCT == ac)
1.1 kristaps 904: break;
905:
1.53 kristaps 906: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 907:
1.131 schwarze 908: /*
1.4 kristaps 909: * In this case, we've located a submacro and must
910: * execute it. Close out scope, if open. If no
911: * elements have been generated, either create one (nc)
912: * or raise a warning.
913: */
1.1 kristaps 914:
1.53 kristaps 915: if (MDOC_MAX != ntok) {
1.119 schwarze 916: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 917: return(0);
1.4 kristaps 918: if (nc && 0 == cnt) {
1.131 schwarze 919: if ( ! mdoc_elem_alloc(mdoc,
920: line, ppos, tok, arg))
1.3 kristaps 921: return(0);
1.119 schwarze 922: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 923: return(0);
1.7 kristaps 924: } else if ( ! nc && 0 == cnt) {
925: mdoc_argv_free(arg);
1.135 schwarze 926: mandoc_msg(MANDOCERR_MACRO_EMPTY,
927: mdoc->parse, line, ppos,
928: mdoc_macronames[tok]);
1.7 kristaps 929: }
1.103 kristaps 930:
1.119 schwarze 931: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 932: return(0);
1.60 kristaps 933: if ( ! nl)
1.1 kristaps 934: return(1);
1.119 schwarze 935: return(append_delims(mdoc, line, pos, buf));
1.131 schwarze 936: }
1.1 kristaps 937:
1.131 schwarze 938: /*
1.4 kristaps 939: * Non-quote-enclosed punctuation. Set up our scope, if
940: * a word; rewind the scope, if a delimiter; then append
1.131 schwarze 941: * the word.
1.4 kristaps 942: */
1.1 kristaps 943:
1.105 kristaps 944: d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.3 kristaps 945:
1.70 kristaps 946: if (DELIM_NONE != d) {
947: /*
948: * If we encounter closing punctuation, no word
949: * has been omitted, no scope is open, and we're
950: * allowed to have an empty element, then start
951: * a new scope. `Ar', `Fl', and `Li', only do
952: * this once per invocation. There may be more
953: * of these (all of them?).
954: */
1.131 schwarze 955: if (0 == cnt && (nc || MDOC_Li == tok) &&
956: DELIM_CLOSE == d && ! scope) {
957: if ( ! mdoc_elem_alloc(mdoc,
958: line, ppos, tok, arg))
1.70 kristaps 959: return(0);
1.131 schwarze 960: if (MDOC_Ar == tok || MDOC_Li == tok ||
961: MDOC_Fl == tok)
1.70 kristaps 962: cnt++;
963: scope = 1;
964: }
965: /*
966: * Close out our scope, if one is open, before
967: * any punctuation.
968: */
1.119 schwarze 969: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 970: return(0);
1.70 kristaps 971: scope = 0;
972: } else if ( ! scope) {
1.119 schwarze 973: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 974: return(0);
1.70 kristaps 975: scope = 1;
1.1 kristaps 976: }
977:
1.67 schwarze 978: if (DELIM_NONE == d)
1.3 kristaps 979: cnt++;
1.105 kristaps 980:
1.123 schwarze 981: if ( ! dword(mdoc, line, la, p, d,
982: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 983: return(0);
1.39 kristaps 984:
985: /*
986: * `Fl' macros have their scope re-opened with each new
987: * word so that the `-' can be added to each one without
988: * having to parse out spaces.
989: */
1.70 kristaps 990: if (scope && MDOC_Fl == tok) {
1.119 schwarze 991: if ( ! rew_elem(mdoc, tok))
1.39 kristaps 992: return(0);
1.70 kristaps 993: scope = 0;
1.39 kristaps 994: }
1.1 kristaps 995: }
996:
1.119 schwarze 997: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 998: return(0);
1.4 kristaps 999:
1000: /*
1001: * If no elements have been collected and we're allowed to have
1002: * empties (nc), open a scope and close it out. Otherwise,
1003: * raise a warning.
1004: */
1.45 kristaps 1005:
1.4 kristaps 1006: if (nc && 0 == cnt) {
1.119 schwarze 1007: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.3 kristaps 1008: return(0);
1.119 schwarze 1009: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 1010: return(0);
1.7 kristaps 1011: } else if ( ! nc && 0 == cnt) {
1012: mdoc_argv_free(arg);
1.135 schwarze 1013: mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1014: line, ppos, mdoc_macronames[tok]);
1.7 kristaps 1015: }
1.4 kristaps 1016:
1.60 kristaps 1017: if ( ! nl)
1.1 kristaps 1018: return(1);
1.119 schwarze 1019: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1020: }
1021:
1022: static int
1023: blk_full(MACRO_PROT_ARGS)
1024: {
1.114 kristaps 1025: int la, nl, nparsed;
1.1 kristaps 1026: struct mdoc_arg *arg;
1.45 kristaps 1027: struct mdoc_node *head; /* save of head macro */
1.49 kristaps 1028: struct mdoc_node *body; /* save of body macro */
1.47 kristaps 1029: struct mdoc_node *n;
1.74 kristaps 1030: enum mdoc_type mtt;
1.53 kristaps 1031: enum mdoct ntok;
1.59 kristaps 1032: enum margserr ac, lac;
1.56 kristaps 1033: enum margverr av;
1.1 kristaps 1034: char *p;
1035:
1.119 schwarze 1036: nl = MDOC_NEWLINE & mdoc->flags;
1.63 kristaps 1037:
1.45 kristaps 1038: /* Close out prior implicit scope. */
1.19 kristaps 1039:
1.1 kristaps 1040: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.119 schwarze 1041: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 1042: return(0);
1.119 schwarze 1043: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1044: return(0);
1045: }
1046:
1.45 kristaps 1047: /*
1.109 kristaps 1048: * This routine accommodates implicitly- and explicitly-scoped
1.45 kristaps 1049: * macro openings. Implicit ones first close out prior scope
1050: * (seen above). Delay opening the head until necessary to
1051: * allow leading punctuation to print. Special consideration
1052: * for `It -column', which has phrase-part syntax instead of
1053: * regular child nodes.
1054: */
1055:
1.1 kristaps 1056: for (arg = NULL;; ) {
1.45 kristaps 1057: la = *pos;
1.119 schwarze 1058: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1059:
1.56 kristaps 1060: if (ARGV_WORD == av) {
1.45 kristaps 1061: *pos = la;
1.1 kristaps 1062: break;
1.131 schwarze 1063: }
1.1 kristaps 1064:
1.56 kristaps 1065: if (ARGV_EOLN == av)
1.1 kristaps 1066: break;
1.56 kristaps 1067: if (ARGV_ARG == av)
1.1 kristaps 1068: continue;
1069:
1070: mdoc_argv_free(arg);
1071: return(0);
1072: }
1073:
1.119 schwarze 1074: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1075: return(0);
1076:
1.49 kristaps 1077: head = body = NULL;
1.115 schwarze 1078:
1079: /*
1080: * Exception: Heads of `It' macros in `-diag' lists are not
1081: * parsed, even though `It' macros in general are parsed.
1082: */
1083: nparsed = MDOC_It == tok &&
1.131 schwarze 1084: MDOC_Bl == mdoc->last->parent->tok &&
1085: LIST_diag == mdoc->last->parent->norm->Bl.type;
1.1 kristaps 1086:
1.45 kristaps 1087: /*
1088: * The `Nd' macro has all arguments in its body: it's a hybrid
1089: * of block partial-explicit and full-implicit. Stupid.
1090: */
1.19 kristaps 1091:
1.45 kristaps 1092: if (MDOC_Nd == tok) {
1.119 schwarze 1093: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1094: return(0);
1.119 schwarze 1095: head = mdoc->last;
1096: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.19 kristaps 1097: return(0);
1.119 schwarze 1098: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.19 kristaps 1099: return(0);
1.119 schwarze 1100: body = mdoc->last;
1.123 schwarze 1101: }
1102:
1103: if (MDOC_Bk == tok)
1104: mdoc->flags |= MDOC_KEEP;
1.19 kristaps 1105:
1.59 kristaps 1106: ac = ARGS_ERROR;
1107:
1.62 kristaps 1108: for ( ; ; ) {
1.45 kristaps 1109: la = *pos;
1.75 kristaps 1110: /* Initialise last-phrase-type with ARGS_PEND. */
1111: lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.119 schwarze 1112: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.89 schwarze 1113:
1114: if (ARGS_PUNCT == ac)
1115: break;
1.1 kristaps 1116:
1.53 kristaps 1117: if (ARGS_ERROR == ac)
1.1 kristaps 1118: return(0);
1.75 kristaps 1119:
1120: if (ARGS_EOLN == ac) {
1121: if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1122: break;
1123: /*
1124: * This is necessary: if the last token on a
1125: * line is a `Ta' or tab, then we'll get
1126: * ARGS_EOLN, so we must be smart enough to
1127: * reopen our scope if the last parse was a
1128: * phrase or partial phrase.
1129: */
1.119 schwarze 1130: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.75 kristaps 1131: return(0);
1.119 schwarze 1132: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.75 kristaps 1133: return(0);
1.119 schwarze 1134: body = mdoc->last;
1.1 kristaps 1135: break;
1.75 kristaps 1136: }
1.45 kristaps 1137:
1.131 schwarze 1138: /*
1.73 kristaps 1139: * Emit leading punctuation (i.e., punctuation before
1140: * the MDOC_HEAD) for non-phrase types.
1141: */
1.45 kristaps 1142:
1.131 schwarze 1143: if (NULL == head &&
1144: ARGS_PEND != ac &&
1145: ARGS_PHRASE != ac &&
1146: ARGS_PPHRASE != ac &&
1147: ARGS_QWORD != ac &&
1148: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1149: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.45 kristaps 1150: return(0);
1151: continue;
1152: }
1153:
1.74 kristaps 1154: /* Open a head if one hasn't been opened. */
1.45 kristaps 1155:
1.74 kristaps 1156: if (NULL == head) {
1.119 schwarze 1157: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1158: return(0);
1.119 schwarze 1159: head = mdoc->last;
1.45 kristaps 1160: }
1161:
1.131 schwarze 1162: if (ARGS_PHRASE == ac ||
1163: ARGS_PEND == ac ||
1164: ARGS_PPHRASE == ac) {
1.73 kristaps 1165: /*
1.74 kristaps 1166: * If we haven't opened a body yet, rewind the
1167: * head; if we have, rewind that instead.
1168: */
1169:
1170: mtt = body ? MDOC_BODY : MDOC_HEAD;
1.119 schwarze 1171: if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
1.74 kristaps 1172: return(0);
1.131 schwarze 1173:
1.74 kristaps 1174: /* Then allocate our body context. */
1175:
1.119 schwarze 1176: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.74 kristaps 1177: return(0);
1.119 schwarze 1178: body = mdoc->last;
1.74 kristaps 1179:
1180: /*
1.73 kristaps 1181: * Process phrases: set whether we're in a
1182: * partial-phrase (this effects line handling)
1183: * then call down into the phrase parser.
1184: */
1.74 kristaps 1185:
1.65 kristaps 1186: if (ARGS_PPHRASE == ac)
1.119 schwarze 1187: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1188: if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1.119 schwarze 1189: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1190:
1.119 schwarze 1191: if ( ! phrase(mdoc, line, la, buf))
1.1 kristaps 1192: return(0);
1.71 kristaps 1193:
1.119 schwarze 1194: mdoc->flags &= ~MDOC_PPHRASE;
1.1 kristaps 1195: continue;
1196: }
1197:
1.131 schwarze 1198: ntok = nparsed || ARGS_QWORD == ac ?
1199: MDOC_MAX : lookup(tok, p);
1.54 kristaps 1200:
1201: if (MDOC_MAX == ntok) {
1.123 schwarze 1202: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1203: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1204: return(0);
1.53 kristaps 1205: continue;
1.45 kristaps 1206: }
1.53 kristaps 1207:
1.119 schwarze 1208: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.45 kristaps 1209: return(0);
1.53 kristaps 1210: break;
1.45 kristaps 1211: }
1.1 kristaps 1212:
1.45 kristaps 1213: if (NULL == head) {
1.119 schwarze 1214: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1215: return(0);
1.119 schwarze 1216: head = mdoc->last;
1.1 kristaps 1217: }
1.131 schwarze 1218:
1.119 schwarze 1219: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1220: return(0);
1.19 kristaps 1221:
1.49 kristaps 1222: /* If we've already opened our body, exit now. */
1.45 kristaps 1223:
1.49 kristaps 1224: if (NULL != body)
1.77 kristaps 1225: goto out;
1.19 kristaps 1226:
1.47 kristaps 1227: /*
1.49 kristaps 1228: * If there is an open (i.e., unvalidated) sub-block requiring
1229: * explicit close-out, postpone switching the current block from
1230: * head to body until the rew_sub() call closing out that
1231: * sub-block.
1.47 kristaps 1232: */
1.119 schwarze 1233: for (n = mdoc->last; n && n != head; n = n->parent) {
1.131 schwarze 1234: if (MDOC_BLOCK == n->type &&
1235: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1236: ! (MDOC_VALID & n->flags)) {
1.47 kristaps 1237: n->pending = head;
1238: return(1);
1239: }
1240: }
1241:
1.45 kristaps 1242: /* Close out scopes to remain in a consistent state. */
1243:
1.119 schwarze 1244: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1245: return(0);
1.119 schwarze 1246: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1247: return(0);
1248:
1.77 kristaps 1249: out:
1.119 schwarze 1250: if ( ! (MDOC_FREECOL & mdoc->flags))
1.77 kristaps 1251: return(1);
1252:
1.119 schwarze 1253: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.77 kristaps 1254: return(0);
1.119 schwarze 1255: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.77 kristaps 1256: return(0);
1257:
1.119 schwarze 1258: mdoc->flags &= ~MDOC_FREECOL;
1.1 kristaps 1259: return(1);
1260: }
1261:
1262: static int
1263: blk_part_imp(MACRO_PROT_ARGS)
1264: {
1.63 kristaps 1265: int la, nl;
1.53 kristaps 1266: enum mdoct ntok;
1.55 kristaps 1267: enum margserr ac;
1.1 kristaps 1268: char *p;
1.43 kristaps 1269: struct mdoc_node *blk; /* saved block context */
1270: struct mdoc_node *body; /* saved body context */
1271: struct mdoc_node *n;
1.1 kristaps 1272:
1.119 schwarze 1273: nl = MDOC_NEWLINE & mdoc->flags;
1.63 kristaps 1274:
1.43 kristaps 1275: /*
1276: * A macro that spans to the end of the line. This is generally
1277: * (but not necessarily) called as the first macro. The block
1278: * has a head as the immediate child, which is always empty,
1279: * followed by zero or more opening punctuation nodes, then the
1280: * body (which may be empty, depending on the macro), then zero
1281: * or more closing punctuation nodes.
1282: */
1.32 kristaps 1283:
1.119 schwarze 1284: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.32 kristaps 1285: return(0);
1.43 kristaps 1286:
1.119 schwarze 1287: blk = mdoc->last;
1.43 kristaps 1288:
1.119 schwarze 1289: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1290: return(0);
1.119 schwarze 1291: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1292: return(0);
1293:
1.43 kristaps 1294: /*
1295: * Open the body scope "on-demand", that is, after we've
1296: * processed all our the leading delimiters (open parenthesis,
1297: * etc.).
1298: */
1.1 kristaps 1299:
1.43 kristaps 1300: for (body = NULL; ; ) {
1.32 kristaps 1301: la = *pos;
1.119 schwarze 1302: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.43 kristaps 1303:
1.53 kristaps 1304: if (ARGS_ERROR == ac)
1.1 kristaps 1305: return(0);
1.53 kristaps 1306: if (ARGS_EOLN == ac)
1.43 kristaps 1307: break;
1.53 kristaps 1308: if (ARGS_PUNCT == ac)
1.1 kristaps 1309: break;
1310:
1.53 kristaps 1311: if (NULL == body && ARGS_QWORD != ac &&
1.131 schwarze 1312: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1313: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.1 kristaps 1314: return(0);
1315: continue;
1.123 schwarze 1316: }
1.1 kristaps 1317:
1.43 kristaps 1318: if (NULL == body) {
1.119 schwarze 1319: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1320: return(0);
1.119 schwarze 1321: body = mdoc->last;
1.43 kristaps 1322: }
1323:
1.54 kristaps 1324: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1325:
1326: if (MDOC_MAX == ntok) {
1.123 schwarze 1327: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1328: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1329: return(0);
1.53 kristaps 1330: continue;
1331: }
1.43 kristaps 1332:
1.119 schwarze 1333: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1334: return(0);
1.53 kristaps 1335: break;
1.1 kristaps 1336: }
1337:
1.43 kristaps 1338: /* Clean-ups to leave in a consistent state. */
1339:
1.44 kristaps 1340: if (NULL == body) {
1.119 schwarze 1341: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.44 kristaps 1342: return(0);
1.119 schwarze 1343: body = mdoc->last;
1.44 kristaps 1344: }
1.43 kristaps 1345:
1.83 schwarze 1346: /*
1347: * If there is an open sub-block requiring explicit close-out,
1348: * postpone closing out the current block
1349: * until the rew_sub() call closing out the sub-block.
1350: */
1.119 schwarze 1351: for (n = mdoc->last; n && n != body && n != blk->parent;
1.131 schwarze 1352: n = n->parent) {
1.83 schwarze 1353: if (MDOC_BLOCK == n->type &&
1354: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1355: ! (MDOC_VALID & n->flags)) {
1.119 schwarze 1356: make_pending(n, tok, mdoc, line, ppos);
1357: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 1358: tok, body, ENDBODY_NOSPACE))
1359: return(0);
1360: return(1);
1361: }
1362: }
1.134 schwarze 1363: assert(n == body);
1.83 schwarze 1364:
1.134 schwarze 1365: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.32 kristaps 1366: return(0);
1.1 kristaps 1367:
1.32 kristaps 1368: /* Standard appending of delimiters. */
1.1 kristaps 1369:
1.119 schwarze 1370: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1371: return(0);
1372:
1.32 kristaps 1373: /* Rewind scope, if applicable. */
1.1 kristaps 1374:
1.134 schwarze 1375: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1376: return(0);
1.117 schwarze 1377:
1378: /* Move trailing .Ns out of scope. */
1379:
1380: for (n = body->child; n && n->next; n = n->next)
1381: /* Do nothing. */ ;
1382: if (n && MDOC_Ns == n->tok)
1.119 schwarze 1383: mdoc_node_relink(mdoc, n);
1.1 kristaps 1384:
1385: return(1);
1386: }
1387:
1388: static int
1389: blk_part_exp(MACRO_PROT_ARGS)
1390: {
1.60 kristaps 1391: int la, nl;
1.55 kristaps 1392: enum margserr ac;
1.43 kristaps 1393: struct mdoc_node *head; /* keep track of head */
1394: struct mdoc_node *body; /* keep track of body */
1.1 kristaps 1395: char *p;
1.53 kristaps 1396: enum mdoct ntok;
1.1 kristaps 1397:
1.119 schwarze 1398: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1399:
1.43 kristaps 1400: /*
1401: * The opening of an explicit macro having zero or more leading
1402: * punctuation nodes; a head with optional single element (the
1403: * case of `Eo'); and a body that may be empty.
1404: */
1.32 kristaps 1405:
1.119 schwarze 1406: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.131 schwarze 1407: return(0);
1.32 kristaps 1408:
1.43 kristaps 1409: for (head = body = NULL; ; ) {
1.32 kristaps 1410: la = *pos;
1.119 schwarze 1411: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1412:
1.53 kristaps 1413: if (ARGS_ERROR == ac)
1.1 kristaps 1414: return(0);
1.53 kristaps 1415: if (ARGS_PUNCT == ac)
1.1 kristaps 1416: break;
1.53 kristaps 1417: if (ARGS_EOLN == ac)
1.1 kristaps 1418: break;
1.43 kristaps 1419:
1420: /* Flush out leading punctuation. */
1421:
1.53 kristaps 1422: if (NULL == head && ARGS_QWORD != ac &&
1.131 schwarze 1423: DELIM_OPEN == mdoc_isdelim(p)) {
1.43 kristaps 1424: assert(NULL == body);
1.123 schwarze 1425: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.43 kristaps 1426: return(0);
1427: continue;
1.123 schwarze 1428: }
1.43 kristaps 1429:
1430: if (NULL == head) {
1431: assert(NULL == body);
1.119 schwarze 1432: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1433: return(0);
1.119 schwarze 1434: head = mdoc->last;
1.1 kristaps 1435: }
1436:
1.43 kristaps 1437: /*
1438: * `Eo' gobbles any data into the head, but most other
1439: * macros just immediately close out and begin the body.
1440: */
1441:
1442: if (NULL == body) {
1443: assert(head);
1444: /* No check whether it's a macro! */
1445: if (MDOC_Eo == tok)
1.123 schwarze 1446: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 0))
1.43 kristaps 1447: return(0);
1448:
1.119 schwarze 1449: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1450: return(0);
1.119 schwarze 1451: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1452: return(0);
1.119 schwarze 1453: body = mdoc->last;
1.43 kristaps 1454:
1455: if (MDOC_Eo == tok)
1456: continue;
1.1 kristaps 1457: }
1.43 kristaps 1458:
1459: assert(NULL != head && NULL != body);
1460:
1.54 kristaps 1461: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1462:
1463: if (MDOC_MAX == ntok) {
1.123 schwarze 1464: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1465: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1466: return(0);
1.53 kristaps 1467: continue;
1.43 kristaps 1468: }
1469:
1.119 schwarze 1470: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1471: return(0);
1.53 kristaps 1472: break;
1.1 kristaps 1473: }
1474:
1.43 kristaps 1475: /* Clean-up to leave in a consistent state. */
1.32 kristaps 1476:
1.112 schwarze 1477: if (NULL == head)
1.119 schwarze 1478: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1479: return(0);
1480:
1481: if (NULL == body) {
1.119 schwarze 1482: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1483: return(0);
1.119 schwarze 1484: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1485: return(0);
1486: }
1487:
1.32 kristaps 1488: /* Standard appending of delimiters. */
1489:
1.60 kristaps 1490: if ( ! nl)
1.1 kristaps 1491: return(1);
1.119 schwarze 1492: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1493: }
1494:
1495: static int
1496: in_line_argn(MACRO_PROT_ARGS)
1497: {
1.60 kristaps 1498: int la, flushed, j, maxargs, nl;
1.55 kristaps 1499: enum margserr ac;
1.56 kristaps 1500: enum margverr av;
1.53 kristaps 1501: struct mdoc_arg *arg;
1502: char *p;
1503: enum mdoct ntok;
1.1 kristaps 1504:
1.119 schwarze 1505: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1506:
1.46 kristaps 1507: /*
1508: * A line macro that has a fixed number of arguments (maxargs).
1509: * Only open the scope once the first non-leading-punctuation is
1510: * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1511: * keep it open until the maximum number of arguments are
1512: * exhausted.
1513: */
1.1 kristaps 1514:
1515: switch (tok) {
1.131 schwarze 1516: case MDOC_Ap:
1.1 kristaps 1517: /* FALLTHROUGH */
1.131 schwarze 1518: case MDOC_No:
1.1 kristaps 1519: /* FALLTHROUGH */
1.131 schwarze 1520: case MDOC_Ns:
1.1 kristaps 1521: /* FALLTHROUGH */
1.131 schwarze 1522: case MDOC_Ux:
1.1 kristaps 1523: maxargs = 0;
1524: break;
1.131 schwarze 1525: case MDOC_Bx:
1.101 kristaps 1526: /* FALLTHROUGH */
1.132 schwarze 1527: case MDOC_Es:
1528: /* FALLTHROUGH */
1.131 schwarze 1529: case MDOC_Xr:
1.42 kristaps 1530: maxargs = 2;
1531: break;
1.1 kristaps 1532: default:
1533: maxargs = 1;
1534: break;
1535: }
1536:
1.46 kristaps 1537: for (arg = NULL; ; ) {
1.32 kristaps 1538: la = *pos;
1.119 schwarze 1539: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1540:
1.56 kristaps 1541: if (ARGV_WORD == av) {
1.32 kristaps 1542: *pos = la;
1.1 kristaps 1543: break;
1.131 schwarze 1544: }
1.1 kristaps 1545:
1.56 kristaps 1546: if (ARGV_EOLN == av)
1.1 kristaps 1547: break;
1.56 kristaps 1548: if (ARGV_ARG == av)
1.1 kristaps 1549: continue;
1550:
1551: mdoc_argv_free(arg);
1552: return(0);
1553: }
1554:
1.46 kristaps 1555: for (flushed = j = 0; ; ) {
1.32 kristaps 1556: la = *pos;
1.119 schwarze 1557: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1558:
1.53 kristaps 1559: if (ARGS_ERROR == ac)
1.1 kristaps 1560: return(0);
1.53 kristaps 1561: if (ARGS_PUNCT == ac)
1.1 kristaps 1562: break;
1.53 kristaps 1563: if (ARGS_EOLN == ac)
1.1 kristaps 1564: break;
1565:
1.131 schwarze 1566: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1567: ARGS_QWORD != ac && 0 == j &&
1568: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1569: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.46 kristaps 1570: return(0);
1571: continue;
1572: } else if (0 == j)
1.133 schwarze 1573: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.46 kristaps 1574: return(0);
1575:
1576: if (j == maxargs && ! flushed) {
1.119 schwarze 1577: if ( ! rew_elem(mdoc, tok))
1.46 kristaps 1578: return(0);
1579: flushed = 1;
1580: }
1581:
1.54 kristaps 1582: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1583:
1584: if (MDOC_MAX != ntok) {
1.119 schwarze 1585: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1586: return(0);
1587: flushed = 1;
1.119 schwarze 1588: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1589: return(0);
1.46 kristaps 1590: j++;
1.1 kristaps 1591: break;
1592: }
1593:
1594: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.131 schwarze 1595: ARGS_QWORD != ac &&
1596: ! flushed &&
1597: DELIM_NONE != mdoc_isdelim(p)) {
1.119 schwarze 1598: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1599: return(0);
1600: flushed = 1;
1601: }
1.42 kristaps 1602:
1.123 schwarze 1603: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1604: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1605: return(0);
1.46 kristaps 1606: j++;
1.1 kristaps 1607: }
1608:
1.133 schwarze 1609: if (0 == j && ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.46 kristaps 1610: return(0);
1611:
1612: /* Close out in a consistent state. */
1.32 kristaps 1613:
1.119 schwarze 1614: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1615: return(0);
1.60 kristaps 1616: if ( ! nl)
1.1 kristaps 1617: return(1);
1.119 schwarze 1618: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1619: }
1620:
1621: static int
1622: in_line_eoln(MACRO_PROT_ARGS)
1623: {
1.56 kristaps 1624: int la;
1.55 kristaps 1625: enum margserr ac;
1.56 kristaps 1626: enum margverr av;
1.53 kristaps 1627: struct mdoc_arg *arg;
1628: char *p;
1629: enum mdoct ntok;
1.1 kristaps 1630:
1631: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1632:
1.90 schwarze 1633: if (tok == MDOC_Pp)
1.119 schwarze 1634: rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1.90 schwarze 1635:
1.32 kristaps 1636: /* Parse macro arguments. */
1.1 kristaps 1637:
1.32 kristaps 1638: for (arg = NULL; ; ) {
1.1 kristaps 1639: la = *pos;
1.119 schwarze 1640: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1641:
1.56 kristaps 1642: if (ARGV_WORD == av) {
1.1 kristaps 1643: *pos = la;
1644: break;
1645: }
1.131 schwarze 1646: if (ARGV_EOLN == av)
1.1 kristaps 1647: break;
1.56 kristaps 1648: if (ARGV_ARG == av)
1.1 kristaps 1649: continue;
1650:
1651: mdoc_argv_free(arg);
1652: return(0);
1653: }
1654:
1.32 kristaps 1655: /* Open element scope. */
1656:
1.119 schwarze 1657: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1658: return(0);
1659:
1.32 kristaps 1660: /* Parse argument terms. */
1.1 kristaps 1661:
1662: for (;;) {
1663: la = *pos;
1.119 schwarze 1664: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1665:
1.53 kristaps 1666: if (ARGS_ERROR == ac)
1.1 kristaps 1667: return(0);
1.53 kristaps 1668: if (ARGS_EOLN == ac)
1.1 kristaps 1669: break;
1670:
1.53 kristaps 1671: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 1672:
1.53 kristaps 1673: if (MDOC_MAX == ntok) {
1.123 schwarze 1674: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1675: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1676: return(0);
1.53 kristaps 1677: continue;
1678: }
1.1 kristaps 1679:
1.119 schwarze 1680: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1681: return(0);
1.119 schwarze 1682: return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
1.1 kristaps 1683: }
1684:
1.32 kristaps 1685: /* Close out (no delimiters). */
1686:
1.119 schwarze 1687: return(rew_elem(mdoc, tok));
1.1 kristaps 1688: }
1689:
1690: static int
1.41 kristaps 1691: ctx_synopsis(MACRO_PROT_ARGS)
1692: {
1.60 kristaps 1693: int nl;
1694:
1.119 schwarze 1695: nl = MDOC_NEWLINE & mdoc->flags;
1.41 kristaps 1696:
1697: /* If we're not in the SYNOPSIS, go straight to in-line. */
1.119 schwarze 1698: if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1699: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1700:
1701: /* If we're a nested call, same place. */
1.60 kristaps 1702: if ( ! nl)
1.119 schwarze 1703: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1704:
1705: /*
1706: * XXX: this will open a block scope; however, if later we end
1707: * up formatting the block scope, then child nodes will inherit
1708: * the formatting. Be careful.
1709: */
1.88 schwarze 1710: if (MDOC_Nm == tok)
1.119 schwarze 1711: return(blk_full(mdoc, tok, line, ppos, pos, buf));
1.88 schwarze 1712: assert(MDOC_Vt == tok);
1.119 schwarze 1713: return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
1.1 kristaps 1714: }
1715:
1.24 kristaps 1716: /*
1717: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1718: * They're unusual because they're basically free-form text until a
1719: * macro is encountered.
1720: */
1.1 kristaps 1721: static int
1.119 schwarze 1722: phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1.1 kristaps 1723: {
1.53 kristaps 1724: int la, pos;
1.75 kristaps 1725: enum margserr ac;
1.53 kristaps 1726: enum mdoct ntok;
1727: char *p;
1.1 kristaps 1728:
1.24 kristaps 1729: for (pos = ppos; ; ) {
1730: la = pos;
1.1 kristaps 1731:
1.119 schwarze 1732: ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
1.1 kristaps 1733:
1.75 kristaps 1734: if (ARGS_ERROR == ac)
1.24 kristaps 1735: return(0);
1.75 kristaps 1736: if (ARGS_EOLN == ac)
1.24 kristaps 1737: break;
1.1 kristaps 1738:
1.75 kristaps 1739: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1 kristaps 1740:
1.53 kristaps 1741: if (MDOC_MAX == ntok) {
1.123 schwarze 1742: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 1))
1.1 kristaps 1743: return(0);
1.53 kristaps 1744: continue;
1745: }
1.1 kristaps 1746:
1.119 schwarze 1747: if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
1.1 kristaps 1748: return(0);
1.119 schwarze 1749: return(append_delims(mdoc, line, &pos, buf));
1.1 kristaps 1750: }
1751:
1752: return(1);
1753: }
1.24 kristaps 1754:
1.75 kristaps 1755: static int
1756: phrase_ta(MACRO_PROT_ARGS)
1757: {
1.121 schwarze 1758: struct mdoc_node *n;
1.75 kristaps 1759: int la;
1760: enum mdoct ntok;
1761: enum margserr ac;
1762: char *p;
1763:
1.121 schwarze 1764: /* Make sure we are in a column list or ignore this macro. */
1765: n = mdoc->last;
1766: while (NULL != n && MDOC_Bl != n->tok)
1767: n = n->parent;
1768: if (NULL == n || LIST_column != n->norm->Bl.type) {
1.137 ! schwarze 1769: mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
! 1770: line, ppos, NULL);
1.121 schwarze 1771: return(1);
1772: }
1.75 kristaps 1773:
1.121 schwarze 1774: /* Advance to the next column. */
1.119 schwarze 1775: if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
1.75 kristaps 1776: return(0);
1.119 schwarze 1777: if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
1.75 kristaps 1778: return(0);
1779:
1780: for (;;) {
1781: la = *pos;
1.119 schwarze 1782: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.75 kristaps 1783:
1784: if (ARGS_ERROR == ac)
1785: return(0);
1786: if (ARGS_EOLN == ac)
1787: break;
1788:
1789: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1790:
1791: if (MDOC_MAX == ntok) {
1.123 schwarze 1792: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1793: MDOC_JOIN & mdoc_macros[tok].flags))
1.75 kristaps 1794: return(0);
1795: continue;
1796: }
1797:
1.119 schwarze 1798: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.75 kristaps 1799: return(0);
1.119 schwarze 1800: return(append_delims(mdoc, line, pos, buf));
1.75 kristaps 1801: }
1802:
1803: return(1);
1804: }
CVSweb