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