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