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