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