Annotation of mandoc/macro.c, Revision 1.3
1.3 ! kristaps 1: /* $Id: macro.c,v 1.2 2008/12/15 01:54:58 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
1.2 kristaps 19: #include <assert.h>
20: #include <ctype.h>
1.1 kristaps 21: #include <stdlib.h>
1.2 kristaps 22: #include <stdio.h>
23:
24: #include "private.h"
25:
1.3 ! kristaps 26: #define _CC(p) ((const char **)p)
1.2 kristaps 27:
1.3 ! kristaps 28: static int append_text(struct mdoc *, int,
! 29: int, int, char *[]);
! 30: static int append_scoped(struct mdoc *, int,
! 31: int, int, char *[]);
1.2 kristaps 32: static int isdelim(const char *);
1.3 ! kristaps 33: static int args_next(struct mdoc *, int,
! 34: int *, char *, char **);
1.1 kristaps 35:
36:
37: static int
1.2 kristaps 38: isdelim(const char *p)
39: {
40:
41: if (0 == *p)
42: return(0);
43: if (0 != *(p + 1))
44: return(0);
45:
46: switch (*p) {
47: case('{'):
48: /* FALLTHROUGH */
49: case('.'):
50: /* FALLTHROUGH */
51: case(','):
52: /* FALLTHROUGH */
53: case(';'):
54: /* FALLTHROUGH */
55: case(':'):
56: /* FALLTHROUGH */
57: case('?'):
58: /* FALLTHROUGH */
59: case('!'):
60: /* FALLTHROUGH */
61: case('('):
62: /* FALLTHROUGH */
63: case(')'):
64: /* FALLTHROUGH */
65: case('['):
66: /* FALLTHROUGH */
67: case(']'):
68: /* FALLTHROUGH */
69: case('}'):
70: return(1);
71: default:
72: break;
73: }
74:
75: return(0);
76: }
77:
78:
79: static int
80: args_next(struct mdoc *mdoc, int tok,
81: int *pos, char *buf, char **v)
1.1 kristaps 82: {
83:
84: if (0 == buf[*pos])
85: return(0);
86:
1.2 kristaps 87: assert( ! isspace(buf[*pos]));
88:
1.1 kristaps 89: if ('\"' == buf[*pos]) {
1.2 kristaps 90: (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_QUOTE);
1.1 kristaps 91: return(-1);
92: }
93:
94: *v = &buf[*pos];
95:
1.2 kristaps 96: /* Scan ahead to end of token. */
97:
1.1 kristaps 98: while (buf[*pos] && ! isspace(buf[*pos]))
99: (*pos)++;
100:
1.2 kristaps 101: if (buf[*pos] && buf[*pos + 1] && '\\' == buf[*pos]) {
102: (void)mdoc_err(mdoc, tok, *pos, ERR_SYNTAX_WS);
1.1 kristaps 103: return(-1);
104: }
105:
1.2 kristaps 106: if (0 == buf[*pos])
107: return(1);
108:
109: /* Scan ahead over trailing whitespace. */
110:
111: buf[(*pos)++] = 0;
112: while (buf[*pos] && isspace(buf[*pos]))
113: (*pos)++;
114:
115: if (0 == buf[*pos])
116: if ( ! mdoc_warn(mdoc, tok, *pos, WARN_SYNTAX_WS_EOLN))
117: return(-1);
118:
1.1 kristaps 119: return(1);
120: }
121:
1.2 kristaps 122:
123: static int
1.3 ! kristaps 124: append_scoped(struct mdoc *mdoc, int tok,
! 125: int pos, int sz, char *args[])
1.2 kristaps 126: {
127:
1.3 ! kristaps 128: assert(sz >= 0);
1.2 kristaps 129: args[sz] = NULL;
130: mdoc_block_alloc(mdoc, pos, tok, 0, NULL);
1.3 ! kristaps 131: mdoc_head_alloc(mdoc, pos, tok, (size_t)sz, _CC(args));
1.2 kristaps 132: mdoc_body_alloc(mdoc, pos, tok);
133: return(1);
134: }
135:
136:
1.1 kristaps 137: static int
1.3 ! kristaps 138: append_text(struct mdoc *mdoc, int tok,
! 139: int pos, int sz, char *args[])
1.1 kristaps 140: {
141:
1.3 ! kristaps 142: assert(sz >= 0);
1.2 kristaps 143: args[sz] = NULL;
144:
145: switch (tok) {
146: /* ======= ADD MORE MACRO ARGUMENT-LIMITS BELOW. ======= */
1.1 kristaps 147:
1.2 kristaps 148: case (MDOC_Ft):
149: /* FALLTHROUGH */
150: case (MDOC_Li):
151: /* FALLTHROUGH */
152: case (MDOC_Ms):
153: /* FALLTHROUGH */
154: case (MDOC_Pa):
155: /* FALLTHROUGH */
156: case (MDOC_Tn):
157: if (0 == sz && ! mdoc_warn(mdoc, tok, pos, WARN_ARGS_GE1))
1.1 kristaps 158: return(0);
1.3 ! kristaps 159: mdoc_elem_alloc(mdoc, pos, tok, 0,
! 160: NULL, (size_t)sz, _CC(args));
1.2 kristaps 161: return(1);
162:
163: case (MDOC_Ar):
164: /* FALLTHROUGH */
165: case (MDOC_Cm):
166: /* FALLTHROUGH */
167: case (MDOC_Fl):
1.3 ! kristaps 168: mdoc_elem_alloc(mdoc, pos, tok, 0,
! 169: NULL, (size_t)sz, _CC(args));
1.2 kristaps 170: return(1);
171:
172: case (MDOC_Ad):
173: /* FALLTHROUGH */
174: case (MDOC_Em):
175: /* FALLTHROUGH */
176: case (MDOC_Er):
177: /* FALLTHROUGH */
178: case (MDOC_Ev):
179: /* FALLTHROUGH */
180: case (MDOC_Fa):
181: /* FALLTHROUGH */
182: case (MDOC_Dv):
183: /* FALLTHROUGH */
184: case (MDOC_Ic):
185: /* FALLTHROUGH */
186: case (MDOC_Va):
187: /* FALLTHROUGH */
188: case (MDOC_Vt):
189: if (0 == sz)
190: return(mdoc_err(mdoc, tok, pos, ERR_ARGS_GE1));
1.3 ! kristaps 191: mdoc_elem_alloc(mdoc, pos, tok, 0,
! 192: NULL, (size_t)sz, _CC(args));
1.2 kristaps 193: return(1);
194:
195: /* ======= ADD MORE MACRO ARGUMENT-LIMITS ABOVE. ======= */
196: default:
197: break;
198: }
199:
200: abort();
201: /* NOTREACHED */
202: }
203:
1.1 kristaps 204:
1.2 kristaps 205: int
206: macro_text(struct mdoc *mdoc, int tok, int ppos, int *pos, char *buf)
207: {
1.3 ! kristaps 208: int lastarg, c, lasttok, lastpunct, j;
1.2 kristaps 209: char *args[MDOC_LINEARG_MAX], *p;
1.1 kristaps 210:
1.2 kristaps 211: lasttok = ppos;
212: lastpunct = 0;
213: j = 0;
214:
215: again:
216:
217: lastarg = *pos;
218: c = args_next(mdoc, tok, pos, buf, &args[j]);
219:
220: if (-1 == c)
221: return(0);
222: if (0 == c && ! lastpunct)
223: return(append_text(mdoc, tok, lasttok, j, args));
224: else if (0 == c)
225: return(1);
226:
227: /* Command found. */
228:
229: if (MDOC_MAX != (c = mdoc_find(mdoc, args[j]))) {
230: if ( ! lastpunct)
231: if ( ! append_text(mdoc, tok, lasttok, j, args))
1.1 kristaps 232: return(0);
1.2 kristaps 233: return(mdoc_macro(mdoc, c, lastarg, pos, buf));
234: }
235:
236: /* Word found. */
237:
238: if ( ! isdelim(args[j])) {
239: j++;
240: goto again;
241: }
242:
243: /* Punctuation found. */
244:
245: p = args[j]; /* Save argument (NULL-ified in append). */
246:
247: if ( ! lastpunct)
248: if ( ! append_text(mdoc, tok, lasttok, j, args))
249: return(0);
250:
251: args[j] = p;
252:
253: mdoc_word_alloc(mdoc, lastarg, args[j]);
254: lastpunct = 1;
255: j = 0;
256:
257: goto again;
258:
259: /* NOTREACHED */
260: }
1.1 kristaps 261:
262:
1.2 kristaps 263: int
264: macro_scoped_implicit(struct mdoc *mdoc,
265: int tok, int ppos, int *pos, char *buf)
266: {
1.3 ! kristaps 267: int t, c, lastarg, j;
1.2 kristaps 268: char *args[MDOC_LINEARG_MAX];
269: struct mdoc_node *n;
270:
271: /*
272: * Look for an implicit parent.
273: */
1.1 kristaps 274:
1.2 kristaps 275: assert( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags));
1.1 kristaps 276:
1.3 ! kristaps 277: /* LINTED */
1.2 kristaps 278: for (n = mdoc->last; n; n = n->parent) {
279: if (MDOC_BLOCK != n->type)
280: continue;
281: if (tok == (t = n->data.block.tok))
1.1 kristaps 282: break;
1.2 kristaps 283: if ( ! (MDOC_EXPLICIT & mdoc_macros[t].flags))
284: continue;
285: return(mdoc_err(mdoc, tok, ppos, ERR_SCOPE_BREAK));
286: }
287:
288: if (n) {
289: mdoc->last = n;
290: mdoc_msg(mdoc, ppos, "scope: rewound `%s'",
291: mdoc_macronames[tok]);
292: } else
293: mdoc_msg(mdoc, ppos, "scope: new `%s'",
294: mdoc_macronames[tok]);
295:
296: j = 0;
297:
298: again:
299:
300: lastarg = *pos;
301: c = args_next(mdoc, tok, pos, buf, &args[j]);
302:
303: if (-1 == c)
304: return(0);
305: if (0 == c)
306: return(append_scoped(mdoc, tok, ppos, j, args));
1.1 kristaps 307:
1.2 kristaps 308: /* Command found. */
1.1 kristaps 309:
1.2 kristaps 310: if (MDOC_MAX != (c = mdoc_find(mdoc, args[j])))
1.3 ! kristaps 311: if ( ! mdoc_warn(mdoc, tok, lastarg, WARN_SYNTAX_MACLIKE))
1.1 kristaps 312: return(0);
313:
1.2 kristaps 314: /* Word found. */
315:
316: j++;
317: goto again;
1.1 kristaps 318:
1.2 kristaps 319: /* NOTREACHED */
1.1 kristaps 320: }
CVSweb