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