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