Annotation of src/usr.bin/mandoc/man_macro.c, Revision 1.7
1.7 ! schwarze 1: /* $Id: man_macro.c,v 1.6 2009/08/22 20:14:37 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 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.2 schwarze 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.2 schwarze 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: */
17: #include <assert.h>
18: #include <ctype.h>
19: #include <stdlib.h>
20: #include <string.h>
21:
22: #include "libman.h"
23:
1.6 schwarze 24: #define REW_REWIND (0) /* See rew_scope(). */
25: #define REW_NOHALT (1) /* See rew_scope(). */
26: #define REW_HALT (2) /* See rew_scope(). */
27:
28: static int in_line_eoln(MACRO_PROT_ARGS);
29: static int blk_imp(MACRO_PROT_ARGS);
1.7 ! schwarze 30: static int blk_close(MACRO_PROT_ARGS);
1.6 schwarze 31:
32: static int rew_scope(enum man_type, struct man *, int);
33: static int rew_dohalt(int, enum man_type,
34: const struct man_node *);
1.7 ! schwarze 35: static int rew_block(int, enum man_type,
! 36: const struct man_node *);
1.6 schwarze 37:
38: const struct man_macro __man_macros[MAN_MAX] = {
39: { in_line_eoln, 0 }, /* br */
40: { in_line_eoln, 0 }, /* TH */
1.7 ! schwarze 41: { blk_imp, MAN_SCOPED }, /* SH */
! 42: { blk_imp, MAN_SCOPED }, /* SS */
! 43: { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.6 schwarze 44: { blk_imp, 0 }, /* LP */
45: { blk_imp, 0 }, /* PP */
46: { blk_imp, 0 }, /* P */
47: { blk_imp, 0 }, /* IP */
48: { blk_imp, 0 }, /* HP */
49: { in_line_eoln, MAN_SCOPED }, /* SM */
50: { in_line_eoln, MAN_SCOPED }, /* SB */
51: { in_line_eoln, 0 }, /* BI */
52: { in_line_eoln, 0 }, /* IB */
53: { in_line_eoln, 0 }, /* BR */
54: { in_line_eoln, 0 }, /* RB */
55: { in_line_eoln, MAN_SCOPED }, /* R */
56: { in_line_eoln, MAN_SCOPED }, /* B */
57: { in_line_eoln, MAN_SCOPED }, /* I */
58: { in_line_eoln, 0 }, /* IR */
59: { in_line_eoln, 0 }, /* RI */
60: { in_line_eoln, 0 }, /* na */
61: { in_line_eoln, 0 }, /* i */
62: { in_line_eoln, 0 }, /* sp */
63: { in_line_eoln, 0 }, /* nf */
64: { in_line_eoln, 0 }, /* fi */
65: { in_line_eoln, 0 }, /* r */
1.7 ! schwarze 66: { blk_close, 0 }, /* RE */
! 67: { blk_imp, MAN_EXPLICIT }, /* RS */
! 68: { in_line_eoln, 0 }, /* DT */
1.6 schwarze 69: };
1.1 kristaps 70:
1.6 schwarze 71: const struct man_macro * const man_macros = __man_macros;
1.1 kristaps 72:
73:
74: int
1.6 schwarze 75: man_unscope(struct man *m, const struct man_node *n)
1.1 kristaps 76: {
77:
1.6 schwarze 78: assert(n);
79: m->next = MAN_NEXT_SIBLING;
80:
81: /* LINTED */
82: while (m->last != n) {
83: if ( ! man_valid_post(m))
84: return(0);
85: if ( ! man_action_post(m))
86: return(0);
87: m->last = m->last->parent;
88: assert(m->last);
89: }
90:
91: if ( ! man_valid_post(m))
1.1 kristaps 92: return(0);
1.6 schwarze 93: return(man_action_post(m));
94: }
1.1 kristaps 95:
96:
1.7 ! schwarze 97: static int
! 98: rew_block(int ntok, enum man_type type, const struct man_node *n)
! 99: {
! 100:
! 101: if (MAN_BLOCK == type && ntok == n->parent->tok &&
! 102: MAN_BODY == n->parent->type)
! 103: return(REW_REWIND);
! 104: return(ntok == n->tok ? REW_HALT : REW_NOHALT);
! 105: }
! 106:
! 107:
1.6 schwarze 108: /*
109: * There are three scope levels: scoped to the root (all), scoped to the
110: * section (all less sections), and scoped to subsections (all less
111: * sections and subsections).
112: */
113: static int
114: rew_dohalt(int tok, enum man_type type, const struct man_node *n)
115: {
1.7 ! schwarze 116: int c;
1.1 kristaps 117:
1.6 schwarze 118: if (MAN_ROOT == n->type)
119: return(REW_HALT);
120: assert(n->parent);
121: if (MAN_ROOT == n->parent->type)
122: return(REW_REWIND);
123: if (MAN_VALID & n->flags)
124: return(REW_NOHALT);
125:
1.7 ! schwarze 126: /* Rewind to ourselves, first. */
! 127: if (type == n->type && tok == n->tok)
! 128: return(REW_REWIND);
! 129:
1.6 schwarze 130: switch (tok) {
131: case (MAN_SH):
132: break;
133: case (MAN_SS):
134: /* Rewind to a section, if a block. */
1.7 ! schwarze 135: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
! 136: return(c);
! 137: break;
! 138: case (MAN_RS):
! 139: /* Rewind to a subsection, if a block. */
! 140: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
! 141: return(c);
! 142: /* Rewind to a section, if a block. */
! 143: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
! 144: return(c);
1.6 schwarze 145: break;
146: default:
1.7 ! schwarze 147: /* Rewind to an offsetter, if a block. */
! 148: if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
! 149: return(c);
1.6 schwarze 150: /* Rewind to a subsection, if a block. */
1.7 ! schwarze 151: if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
! 152: return(c);
1.6 schwarze 153: /* Rewind to a section, if a block. */
1.7 ! schwarze 154: if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
! 155: return(c);
1.6 schwarze 156: break;
1.1 kristaps 157: }
158:
1.6 schwarze 159: return(REW_NOHALT);
160: }
1.1 kristaps 161:
162:
1.6 schwarze 163: /*
164: * Rewinding entails ascending the parse tree until a coherent point,
165: * for example, the `SH' macro will close out any intervening `SS'
166: * scopes. When a scope is closed, it must be validated and actioned.
167: */
168: static int
169: rew_scope(enum man_type type, struct man *m, int tok)
170: {
171: struct man_node *n;
172: int c;
1.1 kristaps 173:
1.6 schwarze 174: /* LINTED */
175: for (n = m->last; n; n = n->parent) {
176: /*
177: * Whether we should stop immediately (REW_HALT), stop
178: * and rewind until this point (REW_REWIND), or keep
179: * rewinding (REW_NOHALT).
180: */
181: c = rew_dohalt(tok, type, n);
182: if (REW_HALT == c)
183: return(1);
184: if (REW_REWIND == c)
1.1 kristaps 185: break;
186: }
187:
1.6 schwarze 188: /* Rewind until the current point. */
189:
190: assert(n);
191: return(man_unscope(m, n));
192: }
193:
1.1 kristaps 194:
1.7 ! schwarze 195: /* ARGSUSED */
! 196: int
! 197: blk_close(MACRO_PROT_ARGS)
! 198: {
! 199: int ntok;
! 200: const struct man_node *nn;
! 201:
! 202: switch (tok) {
! 203: case (MAN_RE):
! 204: ntok = MAN_RS;
! 205: break;
! 206: default:
! 207: abort();
! 208: /* NOTREACHED */
! 209: }
! 210:
! 211: for (nn = m->last->parent; nn; nn = nn->parent)
! 212: if (ntok == nn->tok)
! 213: break;
! 214:
! 215: if (NULL == nn)
! 216: if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
! 217: return(0);
! 218:
! 219: if ( ! rew_scope(MAN_BODY, m, ntok))
! 220: return(0);
! 221: if ( ! rew_scope(MAN_BLOCK, m, ntok))
! 222: return(0);
! 223: m->next = MAN_NEXT_SIBLING;
! 224: return(1);
! 225: }
! 226:
! 227:
1.6 schwarze 228: /*
229: * Parse an implicit-block macro. These contain a MAN_HEAD and a
230: * MAN_BODY contained within a MAN_BLOCK. Rules for closing out other
231: * scopes, such as `SH' closing out an `SS', are defined in the rew
232: * routines.
233: */
234: int
235: blk_imp(MACRO_PROT_ARGS)
236: {
237: int w, la;
238: char *p;
1.7 ! schwarze 239: struct man_node *n;
1.6 schwarze 240:
241: /* Close out prior scopes. */
1.1 kristaps 242:
1.6 schwarze 243: if ( ! rew_scope(MAN_BODY, m, tok))
1.1 kristaps 244: return(0);
1.6 schwarze 245: if ( ! rew_scope(MAN_BLOCK, m, tok))
1.1 kristaps 246: return(0);
247:
1.6 schwarze 248: /* Allocate new block & head scope. */
249:
250: if ( ! man_block_alloc(m, line, ppos, tok))
251: return(0);
252: if ( ! man_head_alloc(m, line, ppos, tok))
253: return(0);
1.1 kristaps 254:
1.7 ! schwarze 255: n = m->last;
! 256:
1.6 schwarze 257: /* Add line arguments. */
1.1 kristaps 258:
1.6 schwarze 259: for (;;) {
260: la = *pos;
261: w = man_args(m, line, pos, buf, &p);
1.1 kristaps 262:
1.6 schwarze 263: if (-1 == w)
1.1 kristaps 264: return(0);
1.6 schwarze 265: if (0 == w)
266: break;
267:
268: if ( ! man_word_alloc(m, line, la, p))
1.1 kristaps 269: return(0);
270: }
271:
1.6 schwarze 272: /* Close out head and open body (unless MAN_SCOPE). */
273:
274: if (MAN_SCOPED & man_macros[tok].flags) {
1.7 ! schwarze 275: /* If we're forcing scope (`TP'), keep it open. */
! 276: if (MAN_FSCOPED & man_macros[tok].flags) {
! 277: m->flags |= MAN_BLINE;
! 278: return(1);
! 279: } else if (n == m->last) {
! 280: m->flags |= MAN_BLINE;
! 281: return(1);
! 282: }
! 283: }
! 284:
! 285: if ( ! rew_scope(MAN_HEAD, m, tok))
1.1 kristaps 286: return(0);
287:
1.6 schwarze 288: return(man_body_alloc(m, line, ppos, tok));
1.1 kristaps 289: }
290:
291:
1.6 schwarze 292: int
293: in_line_eoln(MACRO_PROT_ARGS)
1.1 kristaps 294: {
1.6 schwarze 295: int w, la;
296: char *p;
297: struct man_node *n;
1.1 kristaps 298:
1.6 schwarze 299: if ( ! man_elem_alloc(m, line, ppos, tok))
1.1 kristaps 300: return(0);
301:
1.6 schwarze 302: n = m->last;
1.1 kristaps 303:
1.6 schwarze 304: for (;;) {
305: la = *pos;
306: w = man_args(m, line, pos, buf, &p);
1.1 kristaps 307:
1.6 schwarze 308: if (-1 == w)
309: return(0);
310: if (0 == w)
311: break;
1.1 kristaps 312:
1.6 schwarze 313: if ( ! man_word_alloc(m, line, la, p))
314: return(0);
315: }
1.1 kristaps 316:
1.7 ! schwarze 317: if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.6 schwarze 318: m->flags |= MAN_ELINE;
319: return(1);
320: }
1.1 kristaps 321:
1.6 schwarze 322: /*
323: * Note that when TH is pruned, we'll be back at the root, so
324: * make sure that we don't clobber as its sibling.
325: */
1.1 kristaps 326:
1.6 schwarze 327: for ( ; m->last; m->last = m->last->parent) {
328: if (m->last == n)
329: break;
330: if (m->last->type == MAN_ROOT)
331: break;
332: if ( ! man_valid_post(m))
333: return(0);
334: if ( ! man_action_post(m))
335: return(0);
336: }
1.1 kristaps 337:
1.6 schwarze 338: assert(m->last);
1.1 kristaps 339:
340: /*
1.6 schwarze 341: * Same here regarding whether we're back at the root.
1.1 kristaps 342: */
343:
1.6 schwarze 344: if (m->last->type != MAN_ROOT && ! man_valid_post(m))
345: return(0);
346: if (m->last->type != MAN_ROOT && ! man_action_post(m))
347: return(0);
348: if (m->last->type != MAN_ROOT)
349: m->next = MAN_NEXT_SIBLING;
1.1 kristaps 350:
1.6 schwarze 351: return(1);
352: }
1.1 kristaps 353:
354:
1.6 schwarze 355: int
356: man_macroend(struct man *m)
357: {
1.7 ! schwarze 358: struct man_node *n;
! 359:
! 360: n = MAN_VALID & m->last->flags ?
! 361: m->last->parent : m->last;
! 362:
! 363: for ( ; n; n = n->parent) {
! 364: if (MAN_BLOCK != n->type)
! 365: continue;
! 366: if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
! 367: continue;
! 368: if ( ! man_nwarn(m, n, WEXITSCOPE))
! 369: return(0);
! 370: }
1.1 kristaps 371:
1.6 schwarze 372: return(man_unscope(m, m->first));
373: }
1.1 kristaps 374: