Annotation of mandoc/mdoc_macro.c, Revision 1.144
1.144 ! schwarze 1: /* $Id: mdoc_macro.c,v 1.143 2014/09/07 00:05:28 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.1 kristaps 174: { in_line_eoln, 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.86 schwarze 440: */
1.88 schwarze 441: if (ENDBODY_NOT != p->end || MDOC_Nm == p->tok ||
442: (MDOC_BLOCK == p->type &&
1.86 schwarze 443: ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)))
444: return(REWIND_MORE);
445:
446: /*
1.98 kristaps 447: * By default, closing out full blocks
448: * forces closing of broken explicit blocks,
449: * while closing out partial blocks
450: * allows delayed rewinding by default.
1.86 schwarze 451: */
1.98 kristaps 452: return (&blk_full == mdoc_macros[tok].fp ?
453: REWIND_FORCE : REWIND_LATER);
1.1 kristaps 454: }
455:
456: static int
1.47 kristaps 457: rew_elem(struct mdoc *mdoc, enum mdoct tok)
1.1 kristaps 458: {
459: struct mdoc_node *n;
460:
461: n = mdoc->last;
462: if (MDOC_ELEM != n->type)
463: n = n->parent;
464: assert(MDOC_ELEM == n->type);
465: assert(tok == n->tok);
466:
467: return(rew_last(mdoc, n));
468: }
469:
1.83 schwarze 470: /*
471: * We are trying to close a block identified by tok,
472: * but the child block *broken is still open.
473: * Thus, postpone closing the tok block
474: * until the rew_sub call closing *broken.
475: */
476: static int
477: make_pending(struct mdoc_node *broken, enum mdoct tok,
1.119 schwarze 478: struct mdoc *mdoc, int line, int ppos)
1.83 schwarze 479: {
480: struct mdoc_node *breaker;
481:
482: /*
483: * Iterate backwards, searching for the block matching tok,
484: * that is, the block breaking the *broken block.
485: */
486: for (breaker = broken->parent; breaker; breaker = breaker->parent) {
487:
488: /*
489: * If the *broken block had already been broken before
490: * and we encounter its breaker, make the tok block
491: * pending on the inner breaker.
492: * Graphically, "[A breaker=[B broken=[C->B B] tok=A] C]"
493: * becomes "[A broken=[B [C->B B] tok=A] C]"
494: * and finally "[A [B->A [C->B B] A] C]".
495: */
496: if (breaker == broken->pending) {
497: broken = breaker;
498: continue;
499: }
500:
1.84 schwarze 501: if (REWIND_THIS != rew_dohalt(tok, MDOC_BLOCK, breaker))
1.83 schwarze 502: continue;
503: if (MDOC_BODY == broken->type)
504: broken = broken->parent;
505:
506: /*
507: * Found the breaker.
508: * If another, outer breaker is already pending on
509: * the *broken block, we must not clobber the link
510: * to the outer breaker, but make it pending on the
511: * new, now inner breaker.
512: * Graphically, "[A breaker=[B broken=[C->A A] tok=B] C]"
513: * becomes "[A breaker=[B->A broken=[C A] tok=B] C]"
514: * and finally "[A [B->A [C->B A] B] C]".
515: */
516: if (broken->pending) {
517: struct mdoc_node *taker;
518:
519: /*
520: * If the breaker had also been broken before,
521: * it cannot take on the outer breaker itself,
522: * but must hand it on to its own breakers.
523: * Graphically, this is the following situation:
524: * "[A [B breaker=[C->B B] broken=[D->A A] tok=C] D]"
525: * "[A taker=[B->A breaker=[C->B B] [D->C A] C] D]"
526: */
527: taker = breaker;
528: while (taker->pending)
529: taker = taker->pending;
530: taker->pending = broken->pending;
531: }
532: broken->pending = breaker;
1.137 schwarze 533: mandoc_vmsg(MANDOCERR_BLK_NEST, mdoc->parse, line, ppos,
1.131 schwarze 534: "%s breaks %s", mdoc_macronames[tok],
535: mdoc_macronames[broken->tok]);
1.83 schwarze 536: return(1);
537: }
538:
539: /*
540: * Found no matching block for tok.
541: * Are you trying to close a block that is not open?
542: */
543: return(0);
544: }
545:
1.1 kristaps 546: static int
1.131 schwarze 547: rew_sub(enum mdoc_type t, struct mdoc *mdoc,
1.47 kristaps 548: enum mdoct tok, int line, int ppos)
1.1 kristaps 549: {
550: struct mdoc_node *n;
551:
1.119 schwarze 552: n = mdoc->last;
1.84 schwarze 553: while (n) {
554: switch (rew_dohalt(tok, t, n)) {
1.131 schwarze 555: case REWIND_NONE:
1.84 schwarze 556: return(1);
1.131 schwarze 557: case REWIND_THIS:
1.124 schwarze 558: n->lastline = line -
559: (MDOC_NEWLINE & mdoc->flags &&
560: ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
1.84 schwarze 561: break;
1.131 schwarze 562: case REWIND_FORCE:
1.137 schwarze 563: mandoc_vmsg(MANDOCERR_BLK_BROKEN, mdoc->parse,
1.131 schwarze 564: line, ppos, "%s breaks %s",
565: mdoc_macronames[tok],
566: mdoc_macronames[n->tok]);
1.98 kristaps 567: /* FALLTHROUGH */
1.131 schwarze 568: case REWIND_MORE:
1.124 schwarze 569: n->lastline = line -
570: (MDOC_NEWLINE & mdoc->flags ? 1 : 0);
1.84 schwarze 571: n = n->parent;
572: continue;
1.131 schwarze 573: case REWIND_LATER:
1.119 schwarze 574: if (make_pending(n, tok, mdoc, line, ppos) ||
1.98 kristaps 575: MDOC_BLOCK != t)
576: return(1);
577: /* FALLTHROUGH */
1.131 schwarze 578: case REWIND_ERROR:
1.137 schwarze 579: mandoc_msg(MANDOCERR_BLK_NOTOPEN,
580: mdoc->parse, line, ppos,
581: mdoc_macronames[tok]);
1.98 kristaps 582: return(1);
1.32 kristaps 583: }
1.84 schwarze 584: break;
1.1 kristaps 585: }
586:
587: assert(n);
1.119 schwarze 588: if ( ! rew_last(mdoc, n))
1.47 kristaps 589: return(0);
590:
591: /*
1.83 schwarze 592: * The current block extends an enclosing block.
593: * Now that the current block ends, close the enclosing block, too.
1.47 kristaps 594: */
1.83 schwarze 595: while (NULL != (n = n->pending)) {
1.119 schwarze 596: if ( ! rew_last(mdoc, n))
1.47 kristaps 597: return(0);
1.83 schwarze 598: if (MDOC_HEAD == n->type &&
1.119 schwarze 599: ! mdoc_body_alloc(mdoc, n->line, n->pos, n->tok))
1.47 kristaps 600: return(0);
601: }
602:
603: return(1);
1.1 kristaps 604: }
605:
1.105 kristaps 606: /*
607: * Allocate a word and check whether it's punctuation or not.
608: * Punctuation consists of those tokens found in mdoc_isdelim().
609: */
610: static int
1.123 schwarze 611: dword(struct mdoc *mdoc, int line, int col, const char *p,
612: enum mdelim d, int may_append)
1.105 kristaps 613: {
1.131 schwarze 614:
1.105 kristaps 615: if (DELIM_MAX == d)
616: d = mdoc_isdelim(p);
617:
1.123 schwarze 618: if (may_append &&
619: ! ((MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF) & mdoc->flags) &&
620: DELIM_NONE == d && MDOC_TEXT == mdoc->last->type &&
621: DELIM_NONE == mdoc_isdelim(mdoc->last->string)) {
622: mdoc_word_append(mdoc, p);
623: return(1);
624: }
625:
1.119 schwarze 626: if ( ! mdoc_word_alloc(mdoc, line, col, p))
1.105 kristaps 627: return(0);
628:
1.108 schwarze 629: /*
1.144 ! schwarze 630: * If the word consists of a bare delimiter,
! 631: * flag the new node accordingly,
! 632: * unless doing so was vetoed by the invoking macro.
! 633: * Always clear the veto, it is only valid for one word.
1.108 schwarze 634: */
635:
1.144 ! schwarze 636: if (d == DELIM_OPEN)
! 637: mdoc->last->flags |= MDOC_DELIMO;
! 638: else if (d == DELIM_CLOSE &&
! 639: ! (mdoc->flags & MDOC_NODELIMC) &&
1.131 schwarze 640: mdoc->last->parent->tok != MDOC_Fd)
1.119 schwarze 641: mdoc->last->flags |= MDOC_DELIMC;
1.105 kristaps 642:
1.144 ! schwarze 643: mdoc->flags &= ~MDOC_NODELIMC;
! 644:
1.105 kristaps 645: return(1);
646: }
1.1 kristaps 647:
648: static int
1.119 schwarze 649: append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
1.1 kristaps 650: {
1.66 kristaps 651: int la;
1.55 kristaps 652: enum margserr ac;
1.1 kristaps 653: char *p;
654:
1.66 kristaps 655: if ('\0' == buf[*pos])
1.1 kristaps 656: return(1);
657:
658: for (;;) {
1.66 kristaps 659: la = *pos;
1.119 schwarze 660: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.1 kristaps 661:
1.53 kristaps 662: if (ARGS_ERROR == ac)
1.1 kristaps 663: return(0);
1.53 kristaps 664: else if (ARGS_EOLN == ac)
1.1 kristaps 665: break;
1.66 kristaps 666:
1.123 schwarze 667: dword(mdoc, line, la, p, DELIM_MAX, 1);
1.66 kristaps 668:
669: /*
670: * If we encounter end-of-sentence symbols, then trigger
671: * the double-space.
672: *
1.109 kristaps 673: * XXX: it's easy to allow this to propagate outward to
1.66 kristaps 674: * the last symbol, such that `. )' will cause the
675: * correct double-spacing. However, (1) groff isn't
676: * smart enough to do this and (2) it would require
677: * knowing which symbols break this behaviour, for
1.109 kristaps 678: * example, `. ;' shouldn't propagate the double-space.
1.66 kristaps 679: */
1.127 schwarze 680: if (mandoc_eos(p, strlen(p)))
1.119 schwarze 681: mdoc->last->flags |= MDOC_EOS;
1.1 kristaps 682: }
683:
684: return(1);
685: }
686:
687: /*
1.131 schwarze 688: * Close out block partial/full explicit.
1.1 kristaps 689: */
690: static int
691: blk_exp_close(MACRO_PROT_ARGS)
692: {
1.83 schwarze 693: struct mdoc_node *body; /* Our own body. */
694: struct mdoc_node *later; /* A sub-block starting later. */
695: struct mdoc_node *n; /* For searching backwards. */
696:
1.131 schwarze 697: int j, lastarg, maxargs, flushed, nl;
1.55 kristaps 698: enum margserr ac;
1.83 schwarze 699: enum mdoct atok, ntok;
1.1 kristaps 700: char *p;
701:
1.119 schwarze 702: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 703:
1.1 kristaps 704: switch (tok) {
1.131 schwarze 705: case MDOC_Ec:
1.1 kristaps 706: maxargs = 1;
707: break;
1.131 schwarze 708: case MDOC_Ek:
1.125 schwarze 709: mdoc->flags &= ~MDOC_KEEP;
1.1 kristaps 710: default:
711: maxargs = 0;
712: break;
713: }
714:
1.83 schwarze 715: /*
716: * Search backwards for beginnings of blocks,
717: * both of our own and of pending sub-blocks.
718: */
719: atok = rew_alt(tok);
720: body = later = NULL;
1.119 schwarze 721: for (n = mdoc->last; n; n = n->parent) {
1.83 schwarze 722: if (MDOC_VALID & n->flags)
723: continue;
724:
725: /* Remember the start of our own body. */
726: if (MDOC_BODY == n->type && atok == n->tok) {
1.87 kristaps 727: if (ENDBODY_NOT == n->end)
1.83 schwarze 728: body = n;
729: continue;
730: }
731:
1.88 schwarze 732: if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok)
1.83 schwarze 733: continue;
734: if (atok == n->tok) {
735: assert(body);
736:
737: /*
738: * Found the start of our own block.
739: * When there is no pending sub block,
740: * just proceed to closing out.
741: */
742: if (NULL == later)
743: break;
744:
1.131 schwarze 745: /*
1.83 schwarze 746: * When there is a pending sub block,
747: * postpone closing out the current block
748: * until the rew_sub() closing out the sub-block.
749: */
1.119 schwarze 750: make_pending(later, tok, mdoc, line, ppos);
1.83 schwarze 751:
752: /*
753: * Mark the place where the formatting - but not
754: * the scope - of the current block ends.
755: */
1.119 schwarze 756: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 757: atok, body, ENDBODY_SPACE))
758: return(0);
759: break;
760: }
761:
762: /*
763: * When finding an open sub block, remember the last
764: * open explicit block, or, in case there are only
765: * implicit ones, the first open implicit block.
766: */
767: if (later &&
768: MDOC_EXPLICIT & mdoc_macros[later->tok].flags)
769: continue;
1.118 schwarze 770: if (MDOC_It != n->tok)
1.83 schwarze 771: later = n;
772: }
773:
1.136 schwarze 774: if ( ! (MDOC_PARSED & mdoc_macros[tok].flags)) {
775: if ('\0' != buf[*pos])
776: mandoc_vmsg(MANDOCERR_ARG_SKIP,
777: mdoc->parse, line, ppos,
778: "%s %s", mdoc_macronames[tok],
779: buf + *pos);
1.119 schwarze 780: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.22 kristaps 781: return(0);
1.119 schwarze 782: return(rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos));
1.1 kristaps 783: }
784:
1.119 schwarze 785: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 786: return(0);
787:
1.131 schwarze 788: if (NULL == later && maxargs > 0)
1.119 schwarze 789: if ( ! mdoc_tail_alloc(mdoc, line, ppos, rew_alt(tok)))
1.1 kristaps 790: return(0);
791:
1.27 kristaps 792: for (flushed = j = 0; ; j++) {
1.1 kristaps 793: lastarg = *pos;
794:
795: if (j == maxargs && ! flushed) {
1.119 schwarze 796: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 797: return(0);
798: flushed = 1;
799: }
800:
1.119 schwarze 801: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 802:
1.53 kristaps 803: if (ARGS_ERROR == ac)
1.1 kristaps 804: return(0);
1.53 kristaps 805: if (ARGS_PUNCT == ac)
1.1 kristaps 806: break;
1.53 kristaps 807: if (ARGS_EOLN == ac)
1.1 kristaps 808: break;
809:
1.54 kristaps 810: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
811:
812: if (MDOC_MAX == ntok) {
1.123 schwarze 813: if ( ! dword(mdoc, line, lastarg, p, DELIM_MAX,
814: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 815: return(0);
1.54 kristaps 816: continue;
817: }
1.1 kristaps 818:
1.54 kristaps 819: if ( ! flushed) {
1.119 schwarze 820: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.54 kristaps 821: return(0);
822: flushed = 1;
823: }
1.122 schwarze 824:
825: mdoc->flags &= ~MDOC_NEWLINE;
826:
1.119 schwarze 827: if ( ! mdoc_macro(mdoc, ntok, line, lastarg, pos, buf))
1.1 kristaps 828: return(0);
1.54 kristaps 829: break;
1.1 kristaps 830: }
831:
1.119 schwarze 832: if ( ! flushed && ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 833: return(0);
834:
1.60 kristaps 835: if ( ! nl)
1.1 kristaps 836: return(1);
1.119 schwarze 837: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 838: }
839:
840: static int
841: in_line(MACRO_PROT_ARGS)
842: {
1.144 ! schwarze 843: int la, scope, cnt, firstarg, mayopen, nc, nl;
1.56 kristaps 844: enum margverr av;
1.53 kristaps 845: enum mdoct ntok;
1.55 kristaps 846: enum margserr ac;
1.67 schwarze 847: enum mdelim d;
1.53 kristaps 848: struct mdoc_arg *arg;
849: char *p;
1.1 kristaps 850:
1.119 schwarze 851: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 852:
1.4 kristaps 853: /*
854: * Whether we allow ignored elements (those without content,
855: * usually because of reserved words) to squeak by.
856: */
1.45 kristaps 857:
1.4 kristaps 858: switch (tok) {
1.131 schwarze 859: case MDOC_An:
1.26 kristaps 860: /* FALLTHROUGH */
1.131 schwarze 861: case MDOC_Ar:
1.4 kristaps 862: /* FALLTHROUGH */
1.131 schwarze 863: case MDOC_Fl:
1.4 kristaps 864: /* FALLTHROUGH */
1.131 schwarze 865: case MDOC_Mt:
1.12 kristaps 866: /* FALLTHROUGH */
1.131 schwarze 867: case MDOC_Nm:
1.26 kristaps 868: /* FALLTHROUGH */
1.131 schwarze 869: case MDOC_Pa:
1.4 kristaps 870: nc = 1;
871: break;
872: default:
873: nc = 0;
874: break;
875: }
876:
1.27 kristaps 877: for (arg = NULL;; ) {
1.1 kristaps 878: la = *pos;
1.119 schwarze 879: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 880:
1.56 kristaps 881: if (ARGV_WORD == av) {
1.1 kristaps 882: *pos = la;
883: break;
1.131 schwarze 884: }
1.56 kristaps 885: if (ARGV_EOLN == av)
1.1 kristaps 886: break;
1.56 kristaps 887: if (ARGV_ARG == av)
1.1 kristaps 888: continue;
889:
890: mdoc_argv_free(arg);
891: return(0);
892: }
893:
1.144 ! schwarze 894: d = DELIM_NONE;
! 895: firstarg = 1;
1.142 schwarze 896: mayopen = 1;
1.70 kristaps 897: for (cnt = scope = 0;; ) {
1.1 kristaps 898: la = *pos;
1.119 schwarze 899: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 900:
1.144 ! schwarze 901: if (ac == ARGS_ERROR)
1.1 kristaps 902: return(0);
1.144 ! schwarze 903:
! 904: /*
! 905: * At the end of a macro line,
! 906: * opening delimiters do not suppress spacing.
! 907: */
! 908:
! 909: if (ac == ARGS_EOLN) {
! 910: if (d == DELIM_OPEN)
! 911: mdoc->last->flags &= ~MDOC_DELIMO;
1.1 kristaps 912: break;
1.144 ! schwarze 913: }
! 914:
! 915: /*
! 916: * The rest of the macro line is only punctuation,
! 917: * to be handled by append_delims().
! 918: * If there were no other arguments,
! 919: * do not allow the first one to suppress spacing,
! 920: * even if it turns out to be a closing one.
! 921: */
! 922:
! 923: if (ac == ARGS_PUNCT) {
! 924: if (cnt == 0 && nc == 0)
! 925: mdoc->flags |= MDOC_NODELIMC;
1.1 kristaps 926: break;
1.144 ! schwarze 927: }
1.1 kristaps 928:
1.53 kristaps 929: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 930:
1.131 schwarze 931: /*
1.4 kristaps 932: * In this case, we've located a submacro and must
933: * execute it. Close out scope, if open. If no
934: * elements have been generated, either create one (nc)
935: * or raise a warning.
936: */
1.1 kristaps 937:
1.53 kristaps 938: if (MDOC_MAX != ntok) {
1.119 schwarze 939: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 940: return(0);
1.4 kristaps 941: if (nc && 0 == cnt) {
1.131 schwarze 942: if ( ! mdoc_elem_alloc(mdoc,
943: line, ppos, tok, arg))
1.3 kristaps 944: return(0);
1.119 schwarze 945: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 946: return(0);
1.7 kristaps 947: } else if ( ! nc && 0 == cnt) {
948: mdoc_argv_free(arg);
1.135 schwarze 949: mandoc_msg(MANDOCERR_MACRO_EMPTY,
950: mdoc->parse, line, ppos,
951: mdoc_macronames[tok]);
1.7 kristaps 952: }
1.103 kristaps 953:
1.119 schwarze 954: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 955: return(0);
1.60 kristaps 956: if ( ! nl)
1.1 kristaps 957: return(1);
1.119 schwarze 958: return(append_delims(mdoc, line, pos, buf));
1.131 schwarze 959: }
1.1 kristaps 960:
1.131 schwarze 961: /*
1.4 kristaps 962: * Non-quote-enclosed punctuation. Set up our scope, if
963: * a word; rewind the scope, if a delimiter; then append
1.131 schwarze 964: * the word.
1.4 kristaps 965: */
1.1 kristaps 966:
1.105 kristaps 967: d = ARGS_QWORD == ac ? DELIM_NONE : mdoc_isdelim(p);
1.3 kristaps 968:
1.70 kristaps 969: if (DELIM_NONE != d) {
970: /*
971: * If we encounter closing punctuation, no word
1.144 ! schwarze 972: * has been emitted, no scope is open, and we're
1.70 kristaps 973: * allowed to have an empty element, then start
1.142 schwarze 974: * a new scope.
1.70 kristaps 975: */
1.142 schwarze 976: if ((d == DELIM_CLOSE ||
977: (d == DELIM_MIDDLE && tok == MDOC_Fl)) &&
1.144 ! schwarze 978: !cnt && !scope && nc && mayopen) {
1.131 schwarze 979: if ( ! mdoc_elem_alloc(mdoc,
980: line, ppos, tok, arg))
1.70 kristaps 981: return(0);
982: scope = 1;
1.142 schwarze 983: cnt++;
1.144 ! schwarze 984: if (MDOC_Nm == tok)
1.142 schwarze 985: mayopen = 0;
1.70 kristaps 986: }
987: /*
988: * Close out our scope, if one is open, before
989: * any punctuation.
990: */
1.119 schwarze 991: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 992: return(0);
1.70 kristaps 993: scope = 0;
1.142 schwarze 994: } else if (mayopen && !scope) {
1.119 schwarze 995: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 996: return(0);
1.70 kristaps 997: scope = 1;
1.142 schwarze 998: cnt++;
1.1 kristaps 999: }
1.105 kristaps 1000:
1.123 schwarze 1001: if ( ! dword(mdoc, line, la, p, d,
1002: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1003: return(0);
1.39 kristaps 1004:
1005: /*
1.144 ! schwarze 1006: * If the first argument is a closing delimiter,
! 1007: * do not suppress spacing before it.
! 1008: */
! 1009:
! 1010: if (firstarg && d == DELIM_CLOSE && !nc)
! 1011: mdoc->last->flags &= ~MDOC_DELIMC;
! 1012: firstarg = 0;
! 1013:
! 1014: /*
1.39 kristaps 1015: * `Fl' macros have their scope re-opened with each new
1016: * word so that the `-' can be added to each one without
1017: * having to parse out spaces.
1018: */
1.70 kristaps 1019: if (scope && MDOC_Fl == tok) {
1.119 schwarze 1020: if ( ! rew_elem(mdoc, tok))
1.39 kristaps 1021: return(0);
1.70 kristaps 1022: scope = 0;
1.39 kristaps 1023: }
1.1 kristaps 1024: }
1025:
1.119 schwarze 1026: if (scope && ! rew_elem(mdoc, tok))
1.1 kristaps 1027: return(0);
1.4 kristaps 1028:
1029: /*
1030: * If no elements have been collected and we're allowed to have
1031: * empties (nc), open a scope and close it out. Otherwise,
1032: * raise a warning.
1033: */
1.45 kristaps 1034:
1.4 kristaps 1035: if (nc && 0 == cnt) {
1.119 schwarze 1036: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.3 kristaps 1037: return(0);
1.119 schwarze 1038: if ( ! rew_last(mdoc, mdoc->last))
1.12 kristaps 1039: return(0);
1.7 kristaps 1040: } else if ( ! nc && 0 == cnt) {
1041: mdoc_argv_free(arg);
1.135 schwarze 1042: mandoc_msg(MANDOCERR_MACRO_EMPTY, mdoc->parse,
1043: line, ppos, mdoc_macronames[tok]);
1.7 kristaps 1044: }
1.4 kristaps 1045:
1.60 kristaps 1046: if ( ! nl)
1.1 kristaps 1047: return(1);
1.119 schwarze 1048: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1049: }
1050:
1051: static int
1052: blk_full(MACRO_PROT_ARGS)
1053: {
1.114 kristaps 1054: int la, nl, nparsed;
1.1 kristaps 1055: struct mdoc_arg *arg;
1.45 kristaps 1056: struct mdoc_node *head; /* save of head macro */
1.49 kristaps 1057: struct mdoc_node *body; /* save of body macro */
1.47 kristaps 1058: struct mdoc_node *n;
1.74 kristaps 1059: enum mdoc_type mtt;
1.53 kristaps 1060: enum mdoct ntok;
1.59 kristaps 1061: enum margserr ac, lac;
1.56 kristaps 1062: enum margverr av;
1.1 kristaps 1063: char *p;
1064:
1.119 schwarze 1065: nl = MDOC_NEWLINE & mdoc->flags;
1.138 schwarze 1066:
1067: /* Skip items outside lists. */
1068:
1069: if (tok == MDOC_It) {
1070: for (n = mdoc->last; n; n = n->parent)
1.141 schwarze 1071: if (n->tok == MDOC_Bl &&
1072: ! (n->flags & MDOC_VALID))
1.138 schwarze 1073: break;
1074: if (n == NULL) {
1075: mandoc_vmsg(MANDOCERR_IT_STRAY, mdoc->parse,
1076: line, ppos, "It %s", buf + *pos);
1077: if ( ! mdoc_elem_alloc(mdoc, line, ppos,
1078: MDOC_br, NULL))
1079: return(0);
1080: return(rew_elem(mdoc, MDOC_br));
1081: }
1082: }
1.63 kristaps 1083:
1.45 kristaps 1084: /* Close out prior implicit scope. */
1.19 kristaps 1085:
1.1 kristaps 1086: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.119 schwarze 1087: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.1 kristaps 1088: return(0);
1.119 schwarze 1089: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1090: return(0);
1091: }
1092:
1.45 kristaps 1093: /*
1.109 kristaps 1094: * This routine accommodates implicitly- and explicitly-scoped
1.45 kristaps 1095: * macro openings. Implicit ones first close out prior scope
1096: * (seen above). Delay opening the head until necessary to
1097: * allow leading punctuation to print. Special consideration
1098: * for `It -column', which has phrase-part syntax instead of
1099: * regular child nodes.
1100: */
1101:
1.1 kristaps 1102: for (arg = NULL;; ) {
1.45 kristaps 1103: la = *pos;
1.119 schwarze 1104: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1105:
1.56 kristaps 1106: if (ARGV_WORD == av) {
1.45 kristaps 1107: *pos = la;
1.1 kristaps 1108: break;
1.131 schwarze 1109: }
1.1 kristaps 1110:
1.56 kristaps 1111: if (ARGV_EOLN == av)
1.1 kristaps 1112: break;
1.56 kristaps 1113: if (ARGV_ARG == av)
1.1 kristaps 1114: continue;
1115:
1116: mdoc_argv_free(arg);
1117: return(0);
1118: }
1119:
1.119 schwarze 1120: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1121: return(0);
1122:
1.49 kristaps 1123: head = body = NULL;
1.115 schwarze 1124:
1125: /*
1126: * Exception: Heads of `It' macros in `-diag' lists are not
1127: * parsed, even though `It' macros in general are parsed.
1128: */
1129: nparsed = MDOC_It == tok &&
1.131 schwarze 1130: MDOC_Bl == mdoc->last->parent->tok &&
1131: LIST_diag == mdoc->last->parent->norm->Bl.type;
1.1 kristaps 1132:
1.45 kristaps 1133: /*
1134: * The `Nd' macro has all arguments in its body: it's a hybrid
1135: * of block partial-explicit and full-implicit. Stupid.
1136: */
1.19 kristaps 1137:
1.45 kristaps 1138: if (MDOC_Nd == tok) {
1.119 schwarze 1139: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1140: return(0);
1.119 schwarze 1141: head = mdoc->last;
1142: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.19 kristaps 1143: return(0);
1.119 schwarze 1144: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.19 kristaps 1145: return(0);
1.119 schwarze 1146: body = mdoc->last;
1.123 schwarze 1147: }
1148:
1149: if (MDOC_Bk == tok)
1150: mdoc->flags |= MDOC_KEEP;
1.19 kristaps 1151:
1.59 kristaps 1152: ac = ARGS_ERROR;
1153:
1.62 kristaps 1154: for ( ; ; ) {
1.45 kristaps 1155: la = *pos;
1.75 kristaps 1156: /* Initialise last-phrase-type with ARGS_PEND. */
1157: lac = ARGS_ERROR == ac ? ARGS_PEND : ac;
1.119 schwarze 1158: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.89 schwarze 1159:
1160: if (ARGS_PUNCT == ac)
1161: break;
1.1 kristaps 1162:
1.53 kristaps 1163: if (ARGS_ERROR == ac)
1.1 kristaps 1164: return(0);
1.75 kristaps 1165:
1166: if (ARGS_EOLN == ac) {
1167: if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac)
1168: break;
1169: /*
1170: * This is necessary: if the last token on a
1171: * line is a `Ta' or tab, then we'll get
1172: * ARGS_EOLN, so we must be smart enough to
1173: * reopen our scope if the last parse was a
1174: * phrase or partial phrase.
1175: */
1.119 schwarze 1176: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.75 kristaps 1177: return(0);
1.119 schwarze 1178: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.75 kristaps 1179: return(0);
1.119 schwarze 1180: body = mdoc->last;
1.1 kristaps 1181: break;
1.75 kristaps 1182: }
1.45 kristaps 1183:
1.131 schwarze 1184: /*
1.73 kristaps 1185: * Emit leading punctuation (i.e., punctuation before
1186: * the MDOC_HEAD) for non-phrase types.
1187: */
1.45 kristaps 1188:
1.131 schwarze 1189: if (NULL == head &&
1190: ARGS_PEND != ac &&
1191: ARGS_PHRASE != ac &&
1192: ARGS_PPHRASE != ac &&
1193: ARGS_QWORD != ac &&
1194: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1195: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.45 kristaps 1196: return(0);
1197: continue;
1198: }
1199:
1.74 kristaps 1200: /* Open a head if one hasn't been opened. */
1.45 kristaps 1201:
1.74 kristaps 1202: if (NULL == head) {
1.119 schwarze 1203: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.45 kristaps 1204: return(0);
1.119 schwarze 1205: head = mdoc->last;
1.45 kristaps 1206: }
1207:
1.131 schwarze 1208: if (ARGS_PHRASE == ac ||
1209: ARGS_PEND == ac ||
1210: ARGS_PPHRASE == ac) {
1.73 kristaps 1211: /*
1.74 kristaps 1212: * If we haven't opened a body yet, rewind the
1213: * head; if we have, rewind that instead.
1214: */
1215:
1216: mtt = body ? MDOC_BODY : MDOC_HEAD;
1.119 schwarze 1217: if ( ! rew_sub(mtt, mdoc, tok, line, ppos))
1.74 kristaps 1218: return(0);
1.131 schwarze 1219:
1.74 kristaps 1220: /* Then allocate our body context. */
1221:
1.119 schwarze 1222: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.74 kristaps 1223: return(0);
1.119 schwarze 1224: body = mdoc->last;
1.74 kristaps 1225:
1226: /*
1.73 kristaps 1227: * Process phrases: set whether we're in a
1228: * partial-phrase (this effects line handling)
1229: * then call down into the phrase parser.
1230: */
1.74 kristaps 1231:
1.65 kristaps 1232: if (ARGS_PPHRASE == ac)
1.119 schwarze 1233: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1234: if (ARGS_PEND == ac && ARGS_PPHRASE == lac)
1.119 schwarze 1235: mdoc->flags |= MDOC_PPHRASE;
1.71 kristaps 1236:
1.119 schwarze 1237: if ( ! phrase(mdoc, line, la, buf))
1.1 kristaps 1238: return(0);
1.71 kristaps 1239:
1.119 schwarze 1240: mdoc->flags &= ~MDOC_PPHRASE;
1.1 kristaps 1241: continue;
1242: }
1243:
1.131 schwarze 1244: ntok = nparsed || ARGS_QWORD == ac ?
1245: MDOC_MAX : lookup(tok, p);
1.54 kristaps 1246:
1247: if (MDOC_MAX == ntok) {
1.123 schwarze 1248: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1249: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1250: return(0);
1.53 kristaps 1251: continue;
1.45 kristaps 1252: }
1.53 kristaps 1253:
1.119 schwarze 1254: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.45 kristaps 1255: return(0);
1.53 kristaps 1256: break;
1.45 kristaps 1257: }
1.1 kristaps 1258:
1.45 kristaps 1259: if (NULL == head) {
1.119 schwarze 1260: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1261: return(0);
1.119 schwarze 1262: head = mdoc->last;
1.1 kristaps 1263: }
1.131 schwarze 1264:
1.119 schwarze 1265: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1266: return(0);
1.19 kristaps 1267:
1.49 kristaps 1268: /* If we've already opened our body, exit now. */
1.45 kristaps 1269:
1.49 kristaps 1270: if (NULL != body)
1.77 kristaps 1271: goto out;
1.19 kristaps 1272:
1.47 kristaps 1273: /*
1.49 kristaps 1274: * If there is an open (i.e., unvalidated) sub-block requiring
1275: * explicit close-out, postpone switching the current block from
1276: * head to body until the rew_sub() call closing out that
1277: * sub-block.
1.47 kristaps 1278: */
1.119 schwarze 1279: for (n = mdoc->last; n && n != head; n = n->parent) {
1.131 schwarze 1280: if (MDOC_BLOCK == n->type &&
1281: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1282: ! (MDOC_VALID & n->flags)) {
1.47 kristaps 1283: n->pending = head;
1284: return(1);
1285: }
1286: }
1287:
1.45 kristaps 1288: /* Close out scopes to remain in a consistent state. */
1289:
1.119 schwarze 1290: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1291: return(0);
1.119 schwarze 1292: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1293: return(0);
1294:
1.77 kristaps 1295: out:
1.119 schwarze 1296: if ( ! (MDOC_FREECOL & mdoc->flags))
1.77 kristaps 1297: return(1);
1298:
1.119 schwarze 1299: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.77 kristaps 1300: return(0);
1.119 schwarze 1301: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.77 kristaps 1302: return(0);
1303:
1.119 schwarze 1304: mdoc->flags &= ~MDOC_FREECOL;
1.1 kristaps 1305: return(1);
1306: }
1307:
1308: static int
1309: blk_part_imp(MACRO_PROT_ARGS)
1310: {
1.63 kristaps 1311: int la, nl;
1.53 kristaps 1312: enum mdoct ntok;
1.55 kristaps 1313: enum margserr ac;
1.1 kristaps 1314: char *p;
1.43 kristaps 1315: struct mdoc_node *blk; /* saved block context */
1316: struct mdoc_node *body; /* saved body context */
1317: struct mdoc_node *n;
1.1 kristaps 1318:
1.119 schwarze 1319: nl = MDOC_NEWLINE & mdoc->flags;
1.63 kristaps 1320:
1.43 kristaps 1321: /*
1322: * A macro that spans to the end of the line. This is generally
1323: * (but not necessarily) called as the first macro. The block
1324: * has a head as the immediate child, which is always empty,
1325: * followed by zero or more opening punctuation nodes, then the
1326: * body (which may be empty, depending on the macro), then zero
1327: * or more closing punctuation nodes.
1328: */
1.32 kristaps 1329:
1.119 schwarze 1330: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.32 kristaps 1331: return(0);
1.43 kristaps 1332:
1.119 schwarze 1333: blk = mdoc->last;
1.43 kristaps 1334:
1.119 schwarze 1335: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1336: return(0);
1.119 schwarze 1337: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1338: return(0);
1339:
1.43 kristaps 1340: /*
1341: * Open the body scope "on-demand", that is, after we've
1342: * processed all our the leading delimiters (open parenthesis,
1343: * etc.).
1344: */
1.1 kristaps 1345:
1.43 kristaps 1346: for (body = NULL; ; ) {
1.32 kristaps 1347: la = *pos;
1.119 schwarze 1348: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.43 kristaps 1349:
1.53 kristaps 1350: if (ARGS_ERROR == ac)
1.1 kristaps 1351: return(0);
1.53 kristaps 1352: if (ARGS_EOLN == ac)
1.43 kristaps 1353: break;
1.53 kristaps 1354: if (ARGS_PUNCT == ac)
1.1 kristaps 1355: break;
1356:
1.53 kristaps 1357: if (NULL == body && ARGS_QWORD != ac &&
1.131 schwarze 1358: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1359: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.1 kristaps 1360: return(0);
1361: continue;
1.123 schwarze 1362: }
1.1 kristaps 1363:
1.43 kristaps 1364: if (NULL == body) {
1.119 schwarze 1365: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1366: return(0);
1.119 schwarze 1367: body = mdoc->last;
1.43 kristaps 1368: }
1369:
1.54 kristaps 1370: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1371:
1372: if (MDOC_MAX == ntok) {
1.123 schwarze 1373: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1374: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1375: return(0);
1.53 kristaps 1376: continue;
1377: }
1.43 kristaps 1378:
1.119 schwarze 1379: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1380: return(0);
1.53 kristaps 1381: break;
1.1 kristaps 1382: }
1383:
1.43 kristaps 1384: /* Clean-ups to leave in a consistent state. */
1385:
1.44 kristaps 1386: if (NULL == body) {
1.119 schwarze 1387: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.44 kristaps 1388: return(0);
1.119 schwarze 1389: body = mdoc->last;
1.44 kristaps 1390: }
1.43 kristaps 1391:
1.83 schwarze 1392: /*
1393: * If there is an open sub-block requiring explicit close-out,
1394: * postpone closing out the current block
1395: * until the rew_sub() call closing out the sub-block.
1396: */
1.119 schwarze 1397: for (n = mdoc->last; n && n != body && n != blk->parent;
1.131 schwarze 1398: n = n->parent) {
1.83 schwarze 1399: if (MDOC_BLOCK == n->type &&
1400: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1401: ! (MDOC_VALID & n->flags)) {
1.119 schwarze 1402: make_pending(n, tok, mdoc, line, ppos);
1403: if ( ! mdoc_endbody_alloc(mdoc, line, ppos,
1.83 schwarze 1404: tok, body, ENDBODY_NOSPACE))
1405: return(0);
1406: return(1);
1407: }
1408: }
1.134 schwarze 1409: assert(n == body);
1.83 schwarze 1410:
1.134 schwarze 1411: if ( ! rew_sub(MDOC_BODY, mdoc, tok, line, ppos))
1.32 kristaps 1412: return(0);
1.1 kristaps 1413:
1.32 kristaps 1414: /* Standard appending of delimiters. */
1.1 kristaps 1415:
1.119 schwarze 1416: if (nl && ! append_delims(mdoc, line, pos, buf))
1.1 kristaps 1417: return(0);
1418:
1.32 kristaps 1419: /* Rewind scope, if applicable. */
1.1 kristaps 1420:
1.134 schwarze 1421: if ( ! rew_sub(MDOC_BLOCK, mdoc, tok, line, ppos))
1.1 kristaps 1422: return(0);
1.117 schwarze 1423:
1424: /* Move trailing .Ns out of scope. */
1425:
1426: for (n = body->child; n && n->next; n = n->next)
1427: /* Do nothing. */ ;
1428: if (n && MDOC_Ns == n->tok)
1.119 schwarze 1429: mdoc_node_relink(mdoc, n);
1.1 kristaps 1430:
1431: return(1);
1432: }
1433:
1434: static int
1435: blk_part_exp(MACRO_PROT_ARGS)
1436: {
1.60 kristaps 1437: int la, nl;
1.55 kristaps 1438: enum margserr ac;
1.43 kristaps 1439: struct mdoc_node *head; /* keep track of head */
1440: struct mdoc_node *body; /* keep track of body */
1.1 kristaps 1441: char *p;
1.53 kristaps 1442: enum mdoct ntok;
1.1 kristaps 1443:
1.119 schwarze 1444: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1445:
1.43 kristaps 1446: /*
1447: * The opening of an explicit macro having zero or more leading
1448: * punctuation nodes; a head with optional single element (the
1449: * case of `Eo'); and a body that may be empty.
1450: */
1.32 kristaps 1451:
1.119 schwarze 1452: if ( ! mdoc_block_alloc(mdoc, line, ppos, tok, NULL))
1.131 schwarze 1453: return(0);
1.32 kristaps 1454:
1.43 kristaps 1455: for (head = body = NULL; ; ) {
1.32 kristaps 1456: la = *pos;
1.119 schwarze 1457: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1458:
1.53 kristaps 1459: if (ARGS_ERROR == ac)
1.1 kristaps 1460: return(0);
1.53 kristaps 1461: if (ARGS_PUNCT == ac)
1.1 kristaps 1462: break;
1.53 kristaps 1463: if (ARGS_EOLN == ac)
1.1 kristaps 1464: break;
1.43 kristaps 1465:
1466: /* Flush out leading punctuation. */
1467:
1.53 kristaps 1468: if (NULL == head && ARGS_QWORD != ac &&
1.131 schwarze 1469: DELIM_OPEN == mdoc_isdelim(p)) {
1.43 kristaps 1470: assert(NULL == body);
1.123 schwarze 1471: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.43 kristaps 1472: return(0);
1473: continue;
1.123 schwarze 1474: }
1.43 kristaps 1475:
1476: if (NULL == head) {
1477: assert(NULL == body);
1.119 schwarze 1478: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1479: return(0);
1.119 schwarze 1480: head = mdoc->last;
1.1 kristaps 1481: }
1482:
1.43 kristaps 1483: /*
1484: * `Eo' gobbles any data into the head, but most other
1485: * macros just immediately close out and begin the body.
1486: */
1487:
1488: if (NULL == body) {
1489: assert(head);
1490: /* No check whether it's a macro! */
1491: if (MDOC_Eo == tok)
1.123 schwarze 1492: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 0))
1.43 kristaps 1493: return(0);
1494:
1.119 schwarze 1495: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1496: return(0);
1.119 schwarze 1497: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1498: return(0);
1.119 schwarze 1499: body = mdoc->last;
1.43 kristaps 1500:
1501: if (MDOC_Eo == tok)
1502: continue;
1.1 kristaps 1503: }
1.43 kristaps 1504:
1505: assert(NULL != head && NULL != body);
1506:
1.54 kristaps 1507: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1508:
1509: if (MDOC_MAX == ntok) {
1.123 schwarze 1510: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1511: MDOC_JOIN & mdoc_macros[tok].flags))
1.43 kristaps 1512: return(0);
1.53 kristaps 1513: continue;
1.43 kristaps 1514: }
1515:
1.119 schwarze 1516: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1517: return(0);
1.53 kristaps 1518: break;
1.1 kristaps 1519: }
1520:
1.43 kristaps 1521: /* Clean-up to leave in a consistent state. */
1.32 kristaps 1522:
1.112 schwarze 1523: if (NULL == head)
1.119 schwarze 1524: if ( ! mdoc_head_alloc(mdoc, line, ppos, tok))
1.43 kristaps 1525: return(0);
1526:
1527: if (NULL == body) {
1.119 schwarze 1528: if ( ! rew_sub(MDOC_HEAD, mdoc, tok, line, ppos))
1.1 kristaps 1529: return(0);
1.119 schwarze 1530: if ( ! mdoc_body_alloc(mdoc, line, ppos, tok))
1.1 kristaps 1531: return(0);
1532: }
1533:
1.32 kristaps 1534: /* Standard appending of delimiters. */
1535:
1.60 kristaps 1536: if ( ! nl)
1.1 kristaps 1537: return(1);
1.119 schwarze 1538: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1539: }
1540:
1541: static int
1542: in_line_argn(MACRO_PROT_ARGS)
1543: {
1.60 kristaps 1544: int la, flushed, j, maxargs, nl;
1.55 kristaps 1545: enum margserr ac;
1.56 kristaps 1546: enum margverr av;
1.53 kristaps 1547: struct mdoc_arg *arg;
1548: char *p;
1549: enum mdoct ntok;
1.1 kristaps 1550:
1.119 schwarze 1551: nl = MDOC_NEWLINE & mdoc->flags;
1.60 kristaps 1552:
1.46 kristaps 1553: /*
1554: * A line macro that has a fixed number of arguments (maxargs).
1555: * Only open the scope once the first non-leading-punctuation is
1556: * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1557: * keep it open until the maximum number of arguments are
1558: * exhausted.
1559: */
1.1 kristaps 1560:
1561: switch (tok) {
1.131 schwarze 1562: case MDOC_Ap:
1.1 kristaps 1563: /* FALLTHROUGH */
1.131 schwarze 1564: case MDOC_Ns:
1.1 kristaps 1565: /* FALLTHROUGH */
1.131 schwarze 1566: case MDOC_Ux:
1.1 kristaps 1567: maxargs = 0;
1568: break;
1.131 schwarze 1569: case MDOC_Bx:
1.101 kristaps 1570: /* FALLTHROUGH */
1.132 schwarze 1571: case MDOC_Es:
1572: /* FALLTHROUGH */
1.131 schwarze 1573: case MDOC_Xr:
1.42 kristaps 1574: maxargs = 2;
1575: break;
1.1 kristaps 1576: default:
1577: maxargs = 1;
1578: break;
1579: }
1580:
1.46 kristaps 1581: for (arg = NULL; ; ) {
1.32 kristaps 1582: la = *pos;
1.119 schwarze 1583: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1584:
1.56 kristaps 1585: if (ARGV_WORD == av) {
1.32 kristaps 1586: *pos = la;
1.1 kristaps 1587: break;
1.131 schwarze 1588: }
1.1 kristaps 1589:
1.56 kristaps 1590: if (ARGV_EOLN == av)
1.1 kristaps 1591: break;
1.56 kristaps 1592: if (ARGV_ARG == av)
1.1 kristaps 1593: continue;
1594:
1595: mdoc_argv_free(arg);
1596: return(0);
1597: }
1598:
1.46 kristaps 1599: for (flushed = j = 0; ; ) {
1.32 kristaps 1600: la = *pos;
1.119 schwarze 1601: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1602:
1.53 kristaps 1603: if (ARGS_ERROR == ac)
1.1 kristaps 1604: return(0);
1.53 kristaps 1605: if (ARGS_PUNCT == ac)
1.1 kristaps 1606: break;
1.53 kristaps 1607: if (ARGS_EOLN == ac)
1.1 kristaps 1608: break;
1609:
1.131 schwarze 1610: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1611: ARGS_QWORD != ac && 0 == j &&
1612: DELIM_OPEN == mdoc_isdelim(p)) {
1.123 schwarze 1613: if ( ! dword(mdoc, line, la, p, DELIM_OPEN, 0))
1.46 kristaps 1614: return(0);
1615: continue;
1616: } else if (0 == j)
1.133 schwarze 1617: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.46 kristaps 1618: return(0);
1619:
1620: if (j == maxargs && ! flushed) {
1.119 schwarze 1621: if ( ! rew_elem(mdoc, tok))
1.46 kristaps 1622: return(0);
1623: flushed = 1;
1624: }
1625:
1.54 kristaps 1626: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1627:
1628: if (MDOC_MAX != ntok) {
1.119 schwarze 1629: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1630: return(0);
1631: flushed = 1;
1.119 schwarze 1632: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.1 kristaps 1633: return(0);
1.46 kristaps 1634: j++;
1.1 kristaps 1635: break;
1636: }
1637:
1638: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.131 schwarze 1639: ARGS_QWORD != ac &&
1640: ! flushed &&
1641: DELIM_NONE != mdoc_isdelim(p)) {
1.119 schwarze 1642: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1643: return(0);
1644: flushed = 1;
1645: }
1.42 kristaps 1646:
1.123 schwarze 1647: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1648: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1649: return(0);
1.46 kristaps 1650: j++;
1.1 kristaps 1651: }
1652:
1.133 schwarze 1653: if (0 == j && ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.46 kristaps 1654: return(0);
1655:
1656: /* Close out in a consistent state. */
1.32 kristaps 1657:
1.119 schwarze 1658: if ( ! flushed && ! rew_elem(mdoc, tok))
1.1 kristaps 1659: return(0);
1.60 kristaps 1660: if ( ! nl)
1.1 kristaps 1661: return(1);
1.119 schwarze 1662: return(append_delims(mdoc, line, pos, buf));
1.1 kristaps 1663: }
1664:
1665: static int
1666: in_line_eoln(MACRO_PROT_ARGS)
1667: {
1.56 kristaps 1668: int la;
1.55 kristaps 1669: enum margserr ac;
1.56 kristaps 1670: enum margverr av;
1.53 kristaps 1671: struct mdoc_arg *arg;
1672: char *p;
1673: enum mdoct ntok;
1.1 kristaps 1674:
1675: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1676:
1.90 schwarze 1677: if (tok == MDOC_Pp)
1.119 schwarze 1678: rew_sub(MDOC_BLOCK, mdoc, MDOC_Nm, line, ppos);
1.90 schwarze 1679:
1.32 kristaps 1680: /* Parse macro arguments. */
1.1 kristaps 1681:
1.32 kristaps 1682: for (arg = NULL; ; ) {
1.1 kristaps 1683: la = *pos;
1.119 schwarze 1684: av = mdoc_argv(mdoc, line, tok, &arg, pos, buf);
1.1 kristaps 1685:
1.56 kristaps 1686: if (ARGV_WORD == av) {
1.1 kristaps 1687: *pos = la;
1688: break;
1689: }
1.131 schwarze 1690: if (ARGV_EOLN == av)
1.1 kristaps 1691: break;
1.56 kristaps 1692: if (ARGV_ARG == av)
1.1 kristaps 1693: continue;
1694:
1695: mdoc_argv_free(arg);
1696: return(0);
1697: }
1698:
1.32 kristaps 1699: /* Open element scope. */
1700:
1.119 schwarze 1701: if ( ! mdoc_elem_alloc(mdoc, line, ppos, tok, arg))
1.1 kristaps 1702: return(0);
1703:
1.32 kristaps 1704: /* Parse argument terms. */
1.1 kristaps 1705:
1706: for (;;) {
1707: la = *pos;
1.119 schwarze 1708: ac = mdoc_args(mdoc, line, pos, buf, tok, &p);
1.1 kristaps 1709:
1.53 kristaps 1710: if (ARGS_ERROR == ac)
1.1 kristaps 1711: return(0);
1.53 kristaps 1712: if (ARGS_EOLN == ac)
1.1 kristaps 1713: break;
1714:
1.53 kristaps 1715: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 1716:
1.53 kristaps 1717: if (MDOC_MAX == ntok) {
1.123 schwarze 1718: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1719: MDOC_JOIN & mdoc_macros[tok].flags))
1.1 kristaps 1720: return(0);
1.53 kristaps 1721: continue;
1722: }
1.1 kristaps 1723:
1.119 schwarze 1724: if ( ! rew_elem(mdoc, tok))
1.1 kristaps 1725: return(0);
1.119 schwarze 1726: return(mdoc_macro(mdoc, ntok, line, la, pos, buf));
1.1 kristaps 1727: }
1728:
1.32 kristaps 1729: /* Close out (no delimiters). */
1730:
1.119 schwarze 1731: return(rew_elem(mdoc, tok));
1.1 kristaps 1732: }
1733:
1734: static int
1.41 kristaps 1735: ctx_synopsis(MACRO_PROT_ARGS)
1736: {
1.60 kristaps 1737: int nl;
1738:
1.119 schwarze 1739: nl = MDOC_NEWLINE & mdoc->flags;
1.41 kristaps 1740:
1741: /* If we're not in the SYNOPSIS, go straight to in-line. */
1.119 schwarze 1742: if ( ! (MDOC_SYNOPSIS & mdoc->flags))
1743: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1744:
1745: /* If we're a nested call, same place. */
1.60 kristaps 1746: if ( ! nl)
1.119 schwarze 1747: return(in_line(mdoc, tok, line, ppos, pos, buf));
1.41 kristaps 1748:
1749: /*
1750: * XXX: this will open a block scope; however, if later we end
1751: * up formatting the block scope, then child nodes will inherit
1752: * the formatting. Be careful.
1753: */
1.88 schwarze 1754: if (MDOC_Nm == tok)
1.119 schwarze 1755: return(blk_full(mdoc, tok, line, ppos, pos, buf));
1.88 schwarze 1756: assert(MDOC_Vt == tok);
1.119 schwarze 1757: return(blk_part_imp(mdoc, tok, line, ppos, pos, buf));
1.1 kristaps 1758: }
1759:
1.24 kristaps 1760: /*
1761: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1762: * They're unusual because they're basically free-form text until a
1763: * macro is encountered.
1764: */
1.1 kristaps 1765: static int
1.119 schwarze 1766: phrase(struct mdoc *mdoc, int line, int ppos, char *buf)
1.1 kristaps 1767: {
1.53 kristaps 1768: int la, pos;
1.75 kristaps 1769: enum margserr ac;
1.53 kristaps 1770: enum mdoct ntok;
1771: char *p;
1.1 kristaps 1772:
1.24 kristaps 1773: for (pos = ppos; ; ) {
1774: la = pos;
1.1 kristaps 1775:
1.119 schwarze 1776: ac = mdoc_zargs(mdoc, line, &pos, buf, &p);
1.1 kristaps 1777:
1.75 kristaps 1778: if (ARGS_ERROR == ac)
1.24 kristaps 1779: return(0);
1.75 kristaps 1780: if (ARGS_EOLN == ac)
1.24 kristaps 1781: break;
1.1 kristaps 1782:
1.75 kristaps 1783: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1 kristaps 1784:
1.53 kristaps 1785: if (MDOC_MAX == ntok) {
1.123 schwarze 1786: if ( ! dword(mdoc, line, la, p, DELIM_MAX, 1))
1.1 kristaps 1787: return(0);
1.53 kristaps 1788: continue;
1789: }
1.1 kristaps 1790:
1.119 schwarze 1791: if ( ! mdoc_macro(mdoc, ntok, line, la, &pos, buf))
1.1 kristaps 1792: return(0);
1.119 schwarze 1793: return(append_delims(mdoc, line, &pos, buf));
1.1 kristaps 1794: }
1795:
1796: return(1);
1797: }
1.24 kristaps 1798:
1.75 kristaps 1799: static int
1800: phrase_ta(MACRO_PROT_ARGS)
1801: {
1.121 schwarze 1802: struct mdoc_node *n;
1.75 kristaps 1803: int la;
1804: enum mdoct ntok;
1805: enum margserr ac;
1806: char *p;
1807:
1.121 schwarze 1808: /* Make sure we are in a column list or ignore this macro. */
1809: n = mdoc->last;
1810: while (NULL != n && MDOC_Bl != n->tok)
1811: n = n->parent;
1812: if (NULL == n || LIST_column != n->norm->Bl.type) {
1.137 schwarze 1813: mandoc_msg(MANDOCERR_TA_STRAY, mdoc->parse,
1.139 schwarze 1814: line, ppos, "Ta");
1.121 schwarze 1815: return(1);
1816: }
1.75 kristaps 1817:
1.121 schwarze 1818: /* Advance to the next column. */
1.119 schwarze 1819: if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos))
1.75 kristaps 1820: return(0);
1.119 schwarze 1821: if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It))
1.75 kristaps 1822: return(0);
1823:
1824: for (;;) {
1825: la = *pos;
1.119 schwarze 1826: ac = mdoc_zargs(mdoc, line, pos, buf, &p);
1.75 kristaps 1827:
1828: if (ARGS_ERROR == ac)
1829: return(0);
1830: if (ARGS_EOLN == ac)
1831: break;
1832:
1833: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1834:
1835: if (MDOC_MAX == ntok) {
1.123 schwarze 1836: if ( ! dword(mdoc, line, la, p, DELIM_MAX,
1837: MDOC_JOIN & mdoc_macros[tok].flags))
1.75 kristaps 1838: return(0);
1839: continue;
1840: }
1841:
1.119 schwarze 1842: if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf))
1.75 kristaps 1843: return(0);
1.119 schwarze 1844: return(append_delims(mdoc, line, pos, buf));
1.75 kristaps 1845: }
1846:
1847: return(1);
1848: }
CVSweb