Annotation of mandoc/mdoc_macro.c, Revision 1.55
1.55 ! kristaps 1: /* $Id: mdoc_macro.c,v 1.54 2010/05/07 05:39:35 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.53 kristaps 735: int la, lastpunct, c, cnt, d, nc;
736: enum mdoct ntok;
1.55 ! kristaps 737: enum margserr ac;
1.53 kristaps 738: struct mdoc_arg *arg;
739: char *p;
1.1 kristaps 740:
1.4 kristaps 741: /*
742: * Whether we allow ignored elements (those without content,
743: * usually because of reserved words) to squeak by.
744: */
1.45 kristaps 745:
1.4 kristaps 746: switch (tok) {
1.26 kristaps 747: case (MDOC_An):
748: /* FALLTHROUGH */
749: case (MDOC_Ar):
1.4 kristaps 750: /* FALLTHROUGH */
751: case (MDOC_Fl):
752: /* FALLTHROUGH */
1.12 kristaps 753: case (MDOC_Lk):
754: /* FALLTHROUGH */
1.26 kristaps 755: case (MDOC_Nm):
756: /* FALLTHROUGH */
1.25 kristaps 757: case (MDOC_Pa):
1.4 kristaps 758: nc = 1;
759: break;
760: default:
761: nc = 0;
762: break;
763: }
764:
1.27 kristaps 765: for (arg = NULL;; ) {
1.1 kristaps 766: la = *pos;
1.32 kristaps 767: c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1 kristaps 768:
769: if (ARGV_WORD == c) {
770: *pos = la;
771: break;
772: }
773: if (ARGV_EOLN == c)
774: break;
775: if (ARGV_ARG == c)
776: continue;
777:
778: mdoc_argv_free(arg);
779: return(0);
780: }
781:
1.4 kristaps 782: for (cnt = 0, lastpunct = 1;; ) {
1.1 kristaps 783: la = *pos;
1.53 kristaps 784: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1 kristaps 785:
1.53 kristaps 786: if (ARGS_ERROR == ac)
1.1 kristaps 787: return(0);
1.53 kristaps 788: if (ARGS_EOLN == ac)
1.1 kristaps 789: break;
1.53 kristaps 790: if (ARGS_PUNCT == ac)
1.1 kristaps 791: break;
792:
1.53 kristaps 793: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 794:
1.4 kristaps 795: /*
796: * In this case, we've located a submacro and must
797: * execute it. Close out scope, if open. If no
798: * elements have been generated, either create one (nc)
799: * or raise a warning.
800: */
1.1 kristaps 801:
1.53 kristaps 802: if (MDOC_MAX != ntok) {
1.32 kristaps 803: if (0 == lastpunct && ! rew_elem(m, tok))
1.1 kristaps 804: return(0);
1.4 kristaps 805: if (nc && 0 == cnt) {
1.32 kristaps 806: if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3 kristaps 807: return(0);
1.32 kristaps 808: if ( ! rew_last(m, m->last))
1.12 kristaps 809: return(0);
1.7 kristaps 810: } else if ( ! nc && 0 == cnt) {
811: mdoc_argv_free(arg);
1.32 kristaps 812: if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3 kristaps 813: return(0);
1.7 kristaps 814: }
1.53 kristaps 815: if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1 kristaps 816: return(0);
817: if (ppos > 1)
818: return(1);
1.32 kristaps 819: return(append_delims(m, line, pos, buf));
1.29 kristaps 820: }
1.1 kristaps 821:
1.4 kristaps 822: /*
823: * Non-quote-enclosed punctuation. Set up our scope, if
824: * a word; rewind the scope, if a delimiter; then append
825: * the word.
826: */
1.1 kristaps 827:
1.53 kristaps 828: d = ARGS_QWORD == ac ? 0 : mdoc_isdelim(p);
1.3 kristaps 829:
1.53 kristaps 830: if (ARGS_QWORD != ac && d) {
1.32 kristaps 831: if (0 == lastpunct && ! rew_elem(m, tok))
1.1 kristaps 832: return(0);
833: lastpunct = 1;
834: } else if (lastpunct) {
1.32 kristaps 835: if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1 kristaps 836: return(0);
837: lastpunct = 0;
838: }
839:
1.3 kristaps 840: if ( ! d)
841: cnt++;
1.32 kristaps 842: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 843: return(0);
1.39 kristaps 844:
845: /*
846: * `Fl' macros have their scope re-opened with each new
847: * word so that the `-' can be added to each one without
848: * having to parse out spaces.
849: */
850: if (0 == lastpunct && MDOC_Fl == tok) {
851: if ( ! rew_elem(m, tok))
852: return(0);
853: lastpunct = 1;
854: }
1.1 kristaps 855: }
856:
1.32 kristaps 857: if (0 == lastpunct && ! rew_elem(m, tok))
1.1 kristaps 858: return(0);
1.4 kristaps 859:
860: /*
861: * If no elements have been collected and we're allowed to have
862: * empties (nc), open a scope and close it out. Otherwise,
863: * raise a warning.
864: */
1.45 kristaps 865:
1.4 kristaps 866: if (nc && 0 == cnt) {
1.32 kristaps 867: if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.3 kristaps 868: return(0);
1.32 kristaps 869: if ( ! rew_last(m, m->last))
1.12 kristaps 870: return(0);
1.7 kristaps 871: } else if ( ! nc && 0 == cnt) {
872: mdoc_argv_free(arg);
1.32 kristaps 873: if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
1.3 kristaps 874: return(0);
1.7 kristaps 875: }
1.4 kristaps 876:
1.1 kristaps 877: if (ppos > 1)
878: return(1);
1.32 kristaps 879: return(append_delims(m, line, pos, buf));
1.1 kristaps 880: }
881:
882:
883: static int
884: blk_full(MACRO_PROT_ARGS)
885: {
1.45 kristaps 886: int c, la;
1.1 kristaps 887: struct mdoc_arg *arg;
1.45 kristaps 888: struct mdoc_node *head; /* save of head macro */
1.49 kristaps 889: struct mdoc_node *body; /* save of body macro */
1.48 kristaps 890: #ifdef UGLY
1.47 kristaps 891: struct mdoc_node *n;
1.48 kristaps 892: #endif
1.53 kristaps 893: enum mdoct ntok;
1.55 ! kristaps 894: enum margserr ac;
1.1 kristaps 895: char *p;
896:
1.45 kristaps 897: /* Close out prior implicit scope. */
1.19 kristaps 898:
1.1 kristaps 899: if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
1.32 kristaps 900: if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
1.1 kristaps 901: return(0);
1.32 kristaps 902: if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
1.1 kristaps 903: return(0);
904: }
905:
1.45 kristaps 906: /*
907: * This routine accomodates implicitly- and explicitly-scoped
908: * macro openings. Implicit ones first close out prior scope
909: * (seen above). Delay opening the head until necessary to
910: * allow leading punctuation to print. Special consideration
911: * for `It -column', which has phrase-part syntax instead of
912: * regular child nodes.
913: */
914:
1.1 kristaps 915: for (arg = NULL;; ) {
1.45 kristaps 916: la = *pos;
1.32 kristaps 917: c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1 kristaps 918:
919: if (ARGV_WORD == c) {
1.45 kristaps 920: *pos = la;
1.1 kristaps 921: break;
922: }
923:
924: if (ARGV_EOLN == c)
925: break;
926: if (ARGV_ARG == c)
927: continue;
928:
929: mdoc_argv_free(arg);
930: return(0);
931: }
932:
1.32 kristaps 933: if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
1.1 kristaps 934: return(0);
935:
1.49 kristaps 936: head = body = NULL;
1.1 kristaps 937:
1.45 kristaps 938: /*
939: * The `Nd' macro has all arguments in its body: it's a hybrid
940: * of block partial-explicit and full-implicit. Stupid.
941: */
1.19 kristaps 942:
1.45 kristaps 943: if (MDOC_Nd == tok) {
944: if ( ! mdoc_head_alloc(m, line, ppos, tok))
945: return(0);
946: head = m->last;
1.32 kristaps 947: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.19 kristaps 948: return(0);
1.32 kristaps 949: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.19 kristaps 950: return(0);
1.49 kristaps 951: body = m->last;
1.19 kristaps 952: }
953:
1.45 kristaps 954: for (;;) {
955: la = *pos;
1.53 kristaps 956: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1 kristaps 957:
1.53 kristaps 958: if (ARGS_ERROR == ac)
1.1 kristaps 959: return(0);
1.53 kristaps 960: if (ARGS_EOLN == ac)
1.1 kristaps 961: break;
1.45 kristaps 962:
963: /* Don't emit leading punct. for phrases. */
964:
1.53 kristaps 965: if (NULL == head && ARGS_PHRASE != ac &&
966: ARGS_QWORD != ac &&
1.45 kristaps 967: 1 == mdoc_isdelim(p)) {
968: if ( ! mdoc_word_alloc(m, line, la, p))
969: return(0);
970: continue;
971: }
972:
973: /* Always re-open head for phrases. */
974:
1.53 kristaps 975: if (NULL == head || ARGS_PHRASE == ac) {
1.45 kristaps 976: if ( ! mdoc_head_alloc(m, line, ppos, tok))
977: return(0);
978: head = m->last;
979: }
980:
1.53 kristaps 981: if (ARGS_PHRASE == ac) {
1.45 kristaps 982: if ( ! phrase(m, line, la, buf))
1.1 kristaps 983: return(0);
1.32 kristaps 984: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1 kristaps 985: return(0);
986: continue;
987: }
988:
1.54 kristaps 989: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
990:
991: if (MDOC_MAX == ntok) {
1.53 kristaps 992: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 993: return(0);
1.53 kristaps 994: continue;
1.45 kristaps 995: }
1.53 kristaps 996:
997: if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.45 kristaps 998: return(0);
1.53 kristaps 999: break;
1.45 kristaps 1000: }
1.1 kristaps 1001:
1.45 kristaps 1002: if (NULL == head) {
1003: if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1 kristaps 1004: return(0);
1.45 kristaps 1005: head = m->last;
1.1 kristaps 1006: }
1007:
1.32 kristaps 1008: if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1 kristaps 1009: return(0);
1.19 kristaps 1010:
1.49 kristaps 1011: /* If we've already opened our body, exit now. */
1.45 kristaps 1012:
1.49 kristaps 1013: if (NULL != body)
1.19 kristaps 1014: return(1);
1015:
1.47 kristaps 1016: #ifdef UGLY
1017: /*
1.49 kristaps 1018: * If there is an open (i.e., unvalidated) sub-block requiring
1019: * explicit close-out, postpone switching the current block from
1020: * head to body until the rew_sub() call closing out that
1021: * sub-block.
1.47 kristaps 1022: */
1023: for (n = m->last; n && n != head; n = n->parent) {
1.49 kristaps 1024: if (MDOC_BLOCK == n->type &&
1025: MDOC_EXPLICIT & mdoc_macros[n->tok].flags &&
1026: ! (MDOC_VALID & n->flags)) {
1027: assert( ! (MDOC_ACTED & n->flags));
1.47 kristaps 1028: n->pending = head;
1029: return(1);
1030: }
1031: }
1032: #endif
1033:
1.45 kristaps 1034: /* Close out scopes to remain in a consistent state. */
1035:
1.32 kristaps 1036: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1 kristaps 1037: return(0);
1.32 kristaps 1038: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1 kristaps 1039: return(0);
1040:
1041: return(1);
1042: }
1043:
1044:
1045: static int
1046: blk_part_imp(MACRO_PROT_ARGS)
1047: {
1.53 kristaps 1048: int la;
1049: enum mdoct ntok;
1.55 ! kristaps 1050: enum margserr ac;
1.1 kristaps 1051: char *p;
1.43 kristaps 1052: struct mdoc_node *blk; /* saved block context */
1053: struct mdoc_node *body; /* saved body context */
1054: struct mdoc_node *n;
1.1 kristaps 1055:
1.43 kristaps 1056: /*
1057: * A macro that spans to the end of the line. This is generally
1058: * (but not necessarily) called as the first macro. The block
1059: * has a head as the immediate child, which is always empty,
1060: * followed by zero or more opening punctuation nodes, then the
1061: * body (which may be empty, depending on the macro), then zero
1062: * or more closing punctuation nodes.
1063: */
1.32 kristaps 1064:
1065: if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1066: return(0);
1.43 kristaps 1067:
1.32 kristaps 1068: blk = m->last;
1.43 kristaps 1069:
1.32 kristaps 1070: if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1 kristaps 1071: return(0);
1.32 kristaps 1072: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1 kristaps 1073: return(0);
1074:
1.43 kristaps 1075: /*
1076: * Open the body scope "on-demand", that is, after we've
1077: * processed all our the leading delimiters (open parenthesis,
1078: * etc.).
1079: */
1.1 kristaps 1080:
1.43 kristaps 1081: for (body = NULL; ; ) {
1.32 kristaps 1082: la = *pos;
1.53 kristaps 1083: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.43 kristaps 1084:
1.53 kristaps 1085: assert(ARGS_PHRASE != ac);
1.1 kristaps 1086:
1.53 kristaps 1087: if (ARGS_ERROR == ac)
1.1 kristaps 1088: return(0);
1.53 kristaps 1089: if (ARGS_EOLN == ac)
1.43 kristaps 1090: break;
1.53 kristaps 1091: if (ARGS_PUNCT == ac)
1.1 kristaps 1092: break;
1093:
1.53 kristaps 1094: if (NULL == body && ARGS_QWORD != ac &&
1.50 kristaps 1095: 1 == mdoc_isdelim(p)) {
1.32 kristaps 1096: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 1097: return(0);
1098: continue;
1099: }
1100:
1.43 kristaps 1101: if (NULL == body) {
1102: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1103: return(0);
1104: body = m->last;
1105: }
1106:
1.54 kristaps 1107: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1108:
1109: if (MDOC_MAX == ntok) {
1.53 kristaps 1110: if ( ! mdoc_word_alloc(m, line, la, p))
1.43 kristaps 1111: return(0);
1.53 kristaps 1112: continue;
1113: }
1.43 kristaps 1114:
1.53 kristaps 1115: if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1 kristaps 1116: return(0);
1.53 kristaps 1117: break;
1.1 kristaps 1118: }
1119:
1.43 kristaps 1120: /* Clean-ups to leave in a consistent state. */
1121:
1.44 kristaps 1122: if (NULL == body) {
1123: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1124: return(0);
1125: body = m->last;
1126: }
1.43 kristaps 1127:
1.32 kristaps 1128: /*
1129: * If we can't rewind to our body, then our scope has already
1130: * been closed by another macro (like `Oc' closing `Op'). This
1131: * is ugly behaviour nodding its head to OpenBSD's overwhelming
1.47 kristaps 1132: * crufty use of `Op' breakage.
1.1 kristaps 1133: */
1.32 kristaps 1134: for (n = m->last; n; n = n->parent)
1.1 kristaps 1135: if (body == n)
1136: break;
1.43 kristaps 1137:
1.32 kristaps 1138: if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
1139: return(0);
1.43 kristaps 1140:
1.32 kristaps 1141: if (n && ! rew_last(m, body))
1142: return(0);
1.1 kristaps 1143:
1.32 kristaps 1144: /* Standard appending of delimiters. */
1.1 kristaps 1145:
1.32 kristaps 1146: if (1 == ppos && ! append_delims(m, line, pos, buf))
1.1 kristaps 1147: return(0);
1148:
1.32 kristaps 1149: /* Rewind scope, if applicable. */
1.1 kristaps 1150:
1.32 kristaps 1151: if (n && ! rew_last(m, blk))
1.1 kristaps 1152: return(0);
1153:
1154: return(1);
1155: }
1156:
1157:
1158: static int
1159: blk_part_exp(MACRO_PROT_ARGS)
1160: {
1.53 kristaps 1161: int la;
1.55 ! kristaps 1162: enum margserr ac;
1.43 kristaps 1163: struct mdoc_node *head; /* keep track of head */
1164: struct mdoc_node *body; /* keep track of body */
1.1 kristaps 1165: char *p;
1.53 kristaps 1166: enum mdoct ntok;
1.1 kristaps 1167:
1.43 kristaps 1168: /*
1169: * The opening of an explicit macro having zero or more leading
1170: * punctuation nodes; a head with optional single element (the
1171: * case of `Eo'); and a body that may be empty.
1172: */
1.32 kristaps 1173:
1174: if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1.1 kristaps 1175: return(0);
1.32 kristaps 1176:
1.43 kristaps 1177: for (head = body = NULL; ; ) {
1.32 kristaps 1178: la = *pos;
1.53 kristaps 1179: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1 kristaps 1180:
1.53 kristaps 1181: if (ARGS_ERROR == ac)
1.1 kristaps 1182: return(0);
1.53 kristaps 1183: if (ARGS_PUNCT == ac)
1.1 kristaps 1184: break;
1.53 kristaps 1185: if (ARGS_EOLN == ac)
1.1 kristaps 1186: break;
1187:
1.53 kristaps 1188: assert(ARGS_PHRASE != ac);
1.43 kristaps 1189:
1190: /* Flush out leading punctuation. */
1191:
1.53 kristaps 1192: if (NULL == head && ARGS_QWORD != ac &&
1.50 kristaps 1193: 1 == mdoc_isdelim(p)) {
1.43 kristaps 1194: assert(NULL == body);
1195: if ( ! mdoc_word_alloc(m, line, la, p))
1196: return(0);
1197: continue;
1198: }
1199:
1200: if (NULL == head) {
1201: assert(NULL == body);
1202: if ( ! mdoc_head_alloc(m, line, ppos, tok))
1.1 kristaps 1203: return(0);
1.43 kristaps 1204: head = m->last;
1.1 kristaps 1205: }
1206:
1.43 kristaps 1207: /*
1208: * `Eo' gobbles any data into the head, but most other
1209: * macros just immediately close out and begin the body.
1210: */
1211:
1212: if (NULL == body) {
1213: assert(head);
1214: /* No check whether it's a macro! */
1215: if (MDOC_Eo == tok)
1216: if ( ! mdoc_word_alloc(m, line, la, p))
1217: return(0);
1218:
1.32 kristaps 1219: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1 kristaps 1220: return(0);
1.32 kristaps 1221: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1 kristaps 1222: return(0);
1.43 kristaps 1223: body = m->last;
1224:
1225: if (MDOC_Eo == tok)
1226: continue;
1.1 kristaps 1227: }
1.43 kristaps 1228:
1229: assert(NULL != head && NULL != body);
1230:
1.54 kristaps 1231: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1232:
1233: if (MDOC_MAX == ntok) {
1.53 kristaps 1234: if ( ! mdoc_word_alloc(m, line, la, p))
1.43 kristaps 1235: return(0);
1.53 kristaps 1236: continue;
1.43 kristaps 1237: }
1238:
1.53 kristaps 1239: if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1 kristaps 1240: return(0);
1.53 kristaps 1241: break;
1.1 kristaps 1242: }
1243:
1.43 kristaps 1244: /* Clean-up to leave in a consistent state. */
1.32 kristaps 1245:
1.43 kristaps 1246: if (NULL == head) {
1247: if ( ! mdoc_head_alloc(m, line, ppos, tok))
1248: return(0);
1249: head = m->last;
1250: }
1251:
1252: if (NULL == body) {
1.32 kristaps 1253: if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1.1 kristaps 1254: return(0);
1.32 kristaps 1255: if ( ! mdoc_body_alloc(m, line, ppos, tok))
1.1 kristaps 1256: return(0);
1.43 kristaps 1257: body = m->last;
1.1 kristaps 1258: }
1259:
1.32 kristaps 1260: /* Standard appending of delimiters. */
1261:
1.1 kristaps 1262: if (ppos > 1)
1263: return(1);
1.43 kristaps 1264:
1.32 kristaps 1265: return(append_delims(m, line, pos, buf));
1.1 kristaps 1266: }
1267:
1268:
1269: static int
1270: in_line_argn(MACRO_PROT_ARGS)
1271: {
1.53 kristaps 1272: int la, flushed, j, c, maxargs;
1.55 ! kristaps 1273: enum margserr ac;
1.53 kristaps 1274: struct mdoc_arg *arg;
1275: char *p;
1276: enum mdoct ntok;
1.1 kristaps 1277:
1.46 kristaps 1278: /*
1279: * A line macro that has a fixed number of arguments (maxargs).
1280: * Only open the scope once the first non-leading-punctuation is
1281: * found (unless MDOC_IGNDELIM is noted, like in `Pf'), then
1282: * keep it open until the maximum number of arguments are
1283: * exhausted.
1284: */
1.1 kristaps 1285:
1286: switch (tok) {
1287: case (MDOC_Ap):
1288: /* FALLTHROUGH */
1289: case (MDOC_No):
1290: /* FALLTHROUGH */
1291: case (MDOC_Ns):
1292: /* FALLTHROUGH */
1293: case (MDOC_Ux):
1294: maxargs = 0;
1295: break;
1.42 kristaps 1296: case (MDOC_Xr):
1297: maxargs = 2;
1298: break;
1.1 kristaps 1299: default:
1300: maxargs = 1;
1301: break;
1302: }
1303:
1.46 kristaps 1304: for (arg = NULL; ; ) {
1.32 kristaps 1305: la = *pos;
1306: c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1 kristaps 1307:
1308: if (ARGV_WORD == c) {
1.32 kristaps 1309: *pos = la;
1.1 kristaps 1310: break;
1311: }
1312:
1313: if (ARGV_EOLN == c)
1314: break;
1315: if (ARGV_ARG == c)
1316: continue;
1317:
1318: mdoc_argv_free(arg);
1319: return(0);
1320: }
1321:
1.46 kristaps 1322: for (flushed = j = 0; ; ) {
1.32 kristaps 1323: la = *pos;
1.53 kristaps 1324: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1 kristaps 1325:
1.53 kristaps 1326: if (ARGS_ERROR == ac)
1.1 kristaps 1327: return(0);
1.53 kristaps 1328: if (ARGS_PUNCT == ac)
1.1 kristaps 1329: break;
1.53 kristaps 1330: if (ARGS_EOLN == ac)
1.1 kristaps 1331: break;
1332:
1.46 kristaps 1333: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.53 kristaps 1334: ARGS_QWORD != ac &&
1.46 kristaps 1335: 0 == j && 1 == mdoc_isdelim(p)) {
1336: if ( ! mdoc_word_alloc(m, line, la, p))
1337: return(0);
1338: continue;
1339: } else if (0 == j)
1340: if ( ! mdoc_elem_alloc(m, line, la, tok, arg))
1341: return(0);
1342:
1343: if (j == maxargs && ! flushed) {
1344: if ( ! rew_elem(m, tok))
1345: return(0);
1346: flushed = 1;
1347: }
1348:
1.54 kristaps 1349: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1350:
1351: if (MDOC_MAX != ntok) {
1.32 kristaps 1352: if ( ! flushed && ! rew_elem(m, tok))
1.1 kristaps 1353: return(0);
1354: flushed = 1;
1.53 kristaps 1355: if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1.1 kristaps 1356: return(0);
1.46 kristaps 1357: j++;
1.1 kristaps 1358: break;
1359: }
1360:
1361: if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1.53 kristaps 1362: ARGS_QWORD != ac &&
1.1 kristaps 1363: ! flushed && mdoc_isdelim(p)) {
1.32 kristaps 1364: if ( ! rew_elem(m, tok))
1.1 kristaps 1365: return(0);
1366: flushed = 1;
1367: }
1.42 kristaps 1368:
1369: /*
1370: * XXX: this is a hack to work around groff's ugliness
1371: * as regards `Xr' and extraneous arguments. It should
1372: * ideally be deprecated behaviour, but because this is
1373: * code is no here, it's unlikely to be removed.
1374: */
1.43 kristaps 1375:
1.46 kristaps 1376: #ifdef __OpenBSD__
1.42 kristaps 1377: if (MDOC_Xr == tok && j == maxargs) {
1.46 kristaps 1378: if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1.42 kristaps 1379: return(0);
1380: if ( ! rew_elem(m, MDOC_Ns))
1381: return(0);
1382: }
1.46 kristaps 1383: #endif
1.42 kristaps 1384:
1.32 kristaps 1385: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 1386: return(0);
1.46 kristaps 1387: j++;
1.1 kristaps 1388: }
1389:
1.46 kristaps 1390: if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
1391: return(0);
1392:
1393: /* Close out in a consistent state. */
1.32 kristaps 1394:
1395: if ( ! flushed && ! rew_elem(m, tok))
1.1 kristaps 1396: return(0);
1397:
1398: if (ppos > 1)
1399: return(1);
1.32 kristaps 1400: return(append_delims(m, line, pos, buf));
1.1 kristaps 1401: }
1402:
1403:
1404: static int
1405: in_line_eoln(MACRO_PROT_ARGS)
1406: {
1.53 kristaps 1407: int c, la;
1.55 ! kristaps 1408: enum margserr ac;
1.53 kristaps 1409: struct mdoc_arg *arg;
1410: char *p;
1411: enum mdoct ntok;
1.1 kristaps 1412:
1413: assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1414:
1.32 kristaps 1415: /* Parse macro arguments. */
1.1 kristaps 1416:
1.32 kristaps 1417: for (arg = NULL; ; ) {
1.1 kristaps 1418: la = *pos;
1.32 kristaps 1419: c = mdoc_argv(m, line, tok, &arg, pos, buf);
1.1 kristaps 1420:
1421: if (ARGV_WORD == c) {
1422: *pos = la;
1423: break;
1424: }
1425: if (ARGV_EOLN == c)
1426: break;
1427: if (ARGV_ARG == c)
1428: continue;
1429:
1430: mdoc_argv_free(arg);
1431: return(0);
1432: }
1433:
1.32 kristaps 1434: /* Open element scope. */
1435:
1436: if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1.1 kristaps 1437: return(0);
1438:
1.32 kristaps 1439: /* Parse argument terms. */
1.1 kristaps 1440:
1441: for (;;) {
1442: la = *pos;
1.53 kristaps 1443: ac = mdoc_args(m, line, pos, buf, tok, &p);
1.1 kristaps 1444:
1.53 kristaps 1445: if (ARGS_ERROR == ac)
1.1 kristaps 1446: return(0);
1.53 kristaps 1447: if (ARGS_EOLN == ac)
1.1 kristaps 1448: break;
1449:
1.53 kristaps 1450: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1.1 kristaps 1451:
1.53 kristaps 1452: if (MDOC_MAX == ntok) {
1453: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 1454: return(0);
1.53 kristaps 1455: continue;
1456: }
1.1 kristaps 1457:
1.53 kristaps 1458: if ( ! rew_elem(m, tok))
1.1 kristaps 1459: return(0);
1.53 kristaps 1460: return(mdoc_macro(m, ntok, line, la, pos, buf));
1.1 kristaps 1461: }
1462:
1.32 kristaps 1463: /* Close out (no delimiters). */
1464:
1465: return(rew_elem(m, tok));
1.1 kristaps 1466: }
1467:
1468:
1469: /* ARGSUSED */
1470: static int
1.41 kristaps 1471: ctx_synopsis(MACRO_PROT_ARGS)
1472: {
1473:
1474: /* If we're not in the SYNOPSIS, go straight to in-line. */
1475: if (SEC_SYNOPSIS != m->lastsec)
1476: return(in_line(m, tok, line, ppos, pos, buf));
1477:
1478: /* If we're a nested call, same place. */
1479: if (ppos > 1)
1480: return(in_line(m, tok, line, ppos, pos, buf));
1481:
1482: /*
1483: * XXX: this will open a block scope; however, if later we end
1484: * up formatting the block scope, then child nodes will inherit
1485: * the formatting. Be careful.
1486: */
1487:
1488: return(blk_part_imp(m, tok, line, ppos, pos, buf));
1489: }
1490:
1491:
1492: /* ARGSUSED */
1493: static int
1.1 kristaps 1494: obsolete(MACRO_PROT_ARGS)
1495: {
1496:
1.32 kristaps 1497: return(mdoc_pwarn(m, line, ppos, EOBS));
1.1 kristaps 1498: }
1499:
1500:
1.24 kristaps 1501: /*
1502: * Phrases occur within `Bl -column' entries, separated by `Ta' or tabs.
1503: * They're unusual because they're basically free-form text until a
1504: * macro is encountered.
1505: */
1.1 kristaps 1506: static int
1.32 kristaps 1507: phrase(struct mdoc *m, int line, int ppos, char *buf)
1.1 kristaps 1508: {
1.53 kristaps 1509: int la, pos;
1.55 ! kristaps 1510: enum margserr ac;
1.53 kristaps 1511: enum mdoct ntok;
1512: char *p;
1.1 kristaps 1513:
1.24 kristaps 1514: for (pos = ppos; ; ) {
1515: la = pos;
1.1 kristaps 1516:
1.24 kristaps 1517: /* Note: no calling context! */
1.53 kristaps 1518: ac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1.1 kristaps 1519:
1.53 kristaps 1520: if (ARGS_ERROR == ac)
1.24 kristaps 1521: return(0);
1.53 kristaps 1522: if (ARGS_EOLN == ac)
1.24 kristaps 1523: break;
1.1 kristaps 1524:
1.53 kristaps 1525: ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p);
1.1 kristaps 1526:
1.53 kristaps 1527: if (MDOC_MAX == ntok) {
1528: if ( ! mdoc_word_alloc(m, line, la, p))
1.1 kristaps 1529: return(0);
1.53 kristaps 1530: continue;
1531: }
1.1 kristaps 1532:
1.53 kristaps 1533: if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1.1 kristaps 1534: return(0);
1.53 kristaps 1535: return(append_delims(m, line, &pos, buf));
1.1 kristaps 1536: }
1537:
1538: return(1);
1539: }
1.24 kristaps 1540:
1541:
CVSweb