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