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