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