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