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