]> git.cameronkatri.com Git - mandoc.git/blob - mdoc_macro.c
mdoc_atosec -> mdoc_str2sec (consistent with str2 being different from a2).
[mandoc.git] / mdoc_macro.c
1 /* $Id: mdoc_macro.c,v 1.59 2010/05/09 10:17:02 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
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.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <time.h>
27
28 #include "libmdoc.h"
29
30 enum rew {
31 REWIND_REWIND,
32 REWIND_NOHALT,
33 REWIND_HALT
34 };
35
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(enum mdoct, const char *);
49 static enum mdoct lookup_raw(const char *);
50 static int phrase(struct mdoc *, int, int,
51 char *, enum margserr, int);
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 *);
64
65 const struct mdoc_macro __mdoc_macros[MDOC_MAX] = {
66 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Ap */
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 */
72 { in_line_eoln, 0 }, /* Pp */
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 */
81 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* An */
82 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ar */
83 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Cd */
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 */
93 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ft */
94 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ic */
95 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* In */
96 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Li */
97 { blk_full, 0 }, /* Nd */
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 */
105 { ctx_synopsis, MDOC_CALLABLE | MDOC_PARSED }, /* Vt */
106 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED }, /* Xr */
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 */
137 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Ms */
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 */
143 { in_line_argn, MDOC_CALLABLE | MDOC_PARSED | MDOC_IGNDELIM }, /* Pf */
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 */
173 { in_line_eoln, 0 }, /* Lp */
174 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Lk */
175 { in_line, MDOC_CALLABLE | MDOC_PARSED }, /* Mt */
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 */
184 { in_line_eoln, 0 }, /* br */
185 { in_line_eoln, 0 }, /* sp */
186 { in_line_eoln, 0 }, /* %U */
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))
230 return(mdoc_verr(mdoc, line, pos,
231 "%s scope breaks %s scope of %s",
232 tt, t, n));
233 return(mdoc_vwarn(mdoc, line, pos,
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
245 mdoc_macroend(struct mdoc *m)
246 {
247 struct mdoc_node *n;
248
249 /* Scan for open explicit scopes. */
250
251 n = MDOC_VALID & m->last->flags ? m->last->parent : m->last;
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;
258 return(mdoc_nerr(m, n, EOPEN));
259 }
260
261 /* Rewind to the first. */
262
263 return(rew_last(m, m->first));
264 }
265
266
267 /*
268 * Look up a macro from within a subsequent context.
269 */
270 static enum mdoct
271 lookup(enum mdoct from, const char *p)
272 {
273 /* FIXME: make -diag lists be un-PARSED. */
274
275 if ( ! (MDOC_PARSED & mdoc_macros[from].flags))
276 return(MDOC_MAX);
277 return(lookup_raw(p));
278 }
279
280
281 /*
282 * Lookup a macro following the initial line macro.
283 */
284 static enum mdoct
285 lookup_raw(const char *p)
286 {
287 enum mdoct res;
288
289 if (MDOC_MAX == (res = mdoc_hash_find(p)))
290 return(MDOC_MAX);
291 if (MDOC_CALLABLE & mdoc_macros[res].flags)
292 return(res);
293 return(MDOC_MAX);
294 }
295
296
297 static int
298 rew_last(struct mdoc *mdoc, const struct mdoc_node *to)
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
320 /*
321 * Return the opening macro of a closing one, e.g., `Ec' has `Eo' as its
322 * matching pair.
323 */
324 static enum mdoct
325 rew_alt(enum mdoct tok)
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 */
374 static enum rew
375 rew_dohalt(enum mdoct tok, enum mdoc_type type,
376 const struct mdoc_node *p)
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):
406 /* FALLTHROUGH */
407 case (MDOC_Vt):
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;
423 case (MDOC_Nd):
424 /* FALLTHROUGH */
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
515 rew_dobreak(enum mdoct tok, const struct mdoc_node *p)
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);
529 case (MDOC_Nd):
530 return(MDOC_Nd == p->tok);
531 case (MDOC_Ss):
532 return(MDOC_Ss == p->tok);
533 case (MDOC_Sh):
534 if (MDOC_Nd == p->tok)
535 return(1);
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
561 rew_elem(struct mdoc *mdoc, enum mdoct tok)
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
576 rew_sub(enum mdoc_type t, struct mdoc *m,
577 enum mdoct tok, int line, int ppos)
578 {
579 struct mdoc_node *n;
580 enum rew c;
581
582 /* LINTED */
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 }
592 if (REWIND_REWIND == c)
593 break;
594 else if (rew_dobreak(tok, n))
595 continue;
596 if ( ! swarn(m, t, line, ppos, n))
597 return(0);
598 }
599
600 assert(n);
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);
620 }
621
622
623 static int
624 append_delims(struct mdoc *mdoc, int line, int *pos, char *buf)
625 {
626 int lastarg;
627 enum margserr ac;
628 char *p;
629
630 if (0 == buf[*pos])
631 return(1);
632
633 for (;;) {
634 lastarg = *pos;
635 ac = mdoc_zargs(mdoc, line, pos, buf, ARGS_NOWARN, &p);
636
637 if (ARGS_ERROR == ac)
638 return(0);
639 else if (ARGS_EOLN == ac)
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 {
656 int j, lastarg, maxargs, flushed;
657 enum margserr ac;
658 enum mdoct ntok;
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)) {
671 if (buf[*pos])
672 if ( ! mdoc_pwarn(m, line, ppos, ENOLINE))
673 return(0);
674
675 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
676 return(0);
677 return(rew_sub(MDOC_BLOCK, m, tok, line, ppos));
678 }
679
680 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
681 return(0);
682
683 if (maxargs > 0)
684 if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok)))
685 return(0);
686
687 for (flushed = j = 0; ; j++) {
688 lastarg = *pos;
689
690 if (j == maxargs && ! flushed) {
691 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
692 return(0);
693 flushed = 1;
694 }
695
696 ac = mdoc_args(m, line, pos, buf, tok, &p);
697
698 if (ARGS_ERROR == ac)
699 return(0);
700 if (ARGS_PUNCT == ac)
701 break;
702 if (ARGS_EOLN == ac)
703 break;
704
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))
709 return(0);
710 continue;
711 }
712
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))
719 return(0);
720 break;
721 }
722
723 if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
724 return(0);
725
726 if (ppos > 1)
727 return(1);
728 return(append_delims(m, line, pos, buf));
729 }
730
731
732 static int
733 in_line(MACRO_PROT_ARGS)
734 {
735 int la, lastpunct, cnt, d, nc;
736 enum margverr av;
737 enum mdoct ntok;
738 enum margserr ac;
739 struct mdoc_arg *arg;
740 char *p;
741
742 /*
743 * Whether we allow ignored elements (those without content,
744 * usually because of reserved words) to squeak by.
745 */
746
747 switch (tok) {
748 case (MDOC_An):
749 /* FALLTHROUGH */
750 case (MDOC_Ar):
751 /* FALLTHROUGH */
752 case (MDOC_Fl):
753 /* FALLTHROUGH */
754 case (MDOC_Lk):
755 /* FALLTHROUGH */
756 case (MDOC_Nm):
757 /* FALLTHROUGH */
758 case (MDOC_Pa):
759 nc = 1;
760 break;
761 default:
762 nc = 0;
763 break;
764 }
765
766 for (arg = NULL;; ) {
767 la = *pos;
768 av = mdoc_argv(m, line, tok, &arg, pos, buf);
769
770 if (ARGV_WORD == av) {
771 *pos = la;
772 break;
773 }
774 if (ARGV_EOLN == av)
775 break;
776 if (ARGV_ARG == av)
777 continue;
778
779 mdoc_argv_free(arg);
780 return(0);
781 }
782
783 for (cnt = 0, lastpunct = 1;; ) {
784 la = *pos;
785 ac = mdoc_args(m, line, pos, buf, tok, &p);
786
787 if (ARGS_ERROR == ac)
788 return(0);
789 if (ARGS_EOLN == ac)
790 break;
791 if (ARGS_PUNCT == ac)
792 break;
793
794 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
795
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 */
802
803 if (MDOC_MAX != ntok) {
804 if (0 == lastpunct && ! rew_elem(m, tok))
805 return(0);
806 if (nc && 0 == cnt) {
807 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
808 return(0);
809 if ( ! rew_last(m, m->last))
810 return(0);
811 } else if ( ! nc && 0 == cnt) {
812 mdoc_argv_free(arg);
813 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
814 return(0);
815 }
816 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
817 return(0);
818 if (ppos > 1)
819 return(1);
820 return(append_delims(m, line, pos, buf));
821 }
822
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 */
828
829 d = ARGS_QWORD == ac ? 0 : mdoc_isdelim(p);
830
831 if (ARGS_QWORD != ac && d) {
832 if (0 == lastpunct && ! rew_elem(m, tok))
833 return(0);
834 lastpunct = 1;
835 } else if (lastpunct) {
836 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
837 return(0);
838 lastpunct = 0;
839 }
840
841 if ( ! d)
842 cnt++;
843 if ( ! mdoc_word_alloc(m, line, la, p))
844 return(0);
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 }
856 }
857
858 if (0 == lastpunct && ! rew_elem(m, tok))
859 return(0);
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 */
866
867 if (nc && 0 == cnt) {
868 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
869 return(0);
870 if ( ! rew_last(m, m->last))
871 return(0);
872 } else if ( ! nc && 0 == cnt) {
873 mdoc_argv_free(arg);
874 if ( ! mdoc_pwarn(m, line, ppos, EIGNE))
875 return(0);
876 }
877
878 if (ppos > 1)
879 return(1);
880 return(append_delims(m, line, pos, buf));
881 }
882
883
884 static int
885 blk_full(MACRO_PROT_ARGS)
886 {
887 int la, pcnt;
888 struct mdoc_arg *arg;
889 struct mdoc_node *head; /* save of head macro */
890 struct mdoc_node *body; /* save of body macro */
891 #ifdef UGLY
892 struct mdoc_node *n;
893 #endif
894 enum mdoct ntok;
895 enum margserr ac, lac;
896 enum margverr av;
897 char *p;
898
899 /* Close out prior implicit scope. */
900
901 if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) {
902 if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos))
903 return(0);
904 if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos))
905 return(0);
906 }
907
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
917 for (arg = NULL;; ) {
918 la = *pos;
919 av = mdoc_argv(m, line, tok, &arg, pos, buf);
920
921 if (ARGV_WORD == av) {
922 *pos = la;
923 break;
924 }
925
926 if (ARGV_EOLN == av)
927 break;
928 if (ARGV_ARG == av)
929 continue;
930
931 mdoc_argv_free(arg);
932 return(0);
933 }
934
935 if ( ! mdoc_block_alloc(m, line, ppos, tok, arg))
936 return(0);
937
938 head = body = NULL;
939
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 */
944
945 if (MDOC_Nd == tok) {
946 if ( ! mdoc_head_alloc(m, line, ppos, tok))
947 return(0);
948 head = m->last;
949 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
950 return(0);
951 if ( ! mdoc_body_alloc(m, line, ppos, tok))
952 return(0);
953 body = m->last;
954 }
955
956 ac = ARGS_ERROR;
957
958 for (pcnt = 0; ; ) {
959 la = *pos;
960 lac = ac;
961 ac = mdoc_args(m, line, pos, buf, tok, &p);
962
963 if (ARGS_ERROR == ac)
964 return(0);
965 if (ARGS_EOLN == ac)
966 break;
967
968 /* Don't emit leading punct. for phrases. */
969
970 if (NULL == head &&
971 ARGS_PHRASE != ac &&
972 ARGS_PPHRASE != ac &&
973 ARGS_PEND != ac &&
974 ARGS_QWORD != ac &&
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
983 if (NULL == head ||
984 ARGS_PHRASE == ac ||
985 ARGS_PEND == ac ||
986 ARGS_PPHRASE == ac) {
987 if ( ! mdoc_head_alloc(m, line, ppos, tok))
988 return(0);
989 head = m->last;
990 }
991
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++))
1007 return(0);
1008 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1009 return(0);
1010 continue;
1011 }
1012
1013 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1014
1015 if (MDOC_MAX == ntok) {
1016 if ( ! mdoc_word_alloc(m, line, la, p))
1017 return(0);
1018 continue;
1019 }
1020
1021 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1022 return(0);
1023 break;
1024 }
1025
1026 if (NULL == head) {
1027 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1028 return(0);
1029 head = m->last;
1030 }
1031
1032 if (1 == ppos && ! append_delims(m, line, pos, buf))
1033 return(0);
1034
1035 /* If we've already opened our body, exit now. */
1036
1037 if (NULL != body)
1038 return(1);
1039
1040 #ifdef UGLY
1041 /*
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.
1046 */
1047 for (n = m->last; n && n != head; n = n->parent) {
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));
1052 n->pending = head;
1053 return(1);
1054 }
1055 }
1056 #endif
1057
1058 /* Close out scopes to remain in a consistent state. */
1059
1060 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1061 return(0);
1062 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1063 return(0);
1064
1065 return(1);
1066 }
1067
1068
1069 static int
1070 blk_part_imp(MACRO_PROT_ARGS)
1071 {
1072 int la;
1073 enum mdoct ntok;
1074 enum margserr ac;
1075 char *p;
1076 struct mdoc_node *blk; /* saved block context */
1077 struct mdoc_node *body; /* saved body context */
1078 struct mdoc_node *n;
1079
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 */
1088
1089 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1090 return(0);
1091
1092 blk = m->last;
1093
1094 if ( ! mdoc_head_alloc(m, line, ppos, tok))
1095 return(0);
1096 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1097 return(0);
1098
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 */
1104
1105 for (body = NULL; ; ) {
1106 la = *pos;
1107 ac = mdoc_args(m, line, pos, buf, tok, &p);
1108
1109 if (ARGS_ERROR == ac)
1110 return(0);
1111 if (ARGS_EOLN == ac)
1112 break;
1113 if (ARGS_PUNCT == ac)
1114 break;
1115
1116 if (NULL == body && ARGS_QWORD != ac &&
1117 1 == mdoc_isdelim(p)) {
1118 if ( ! mdoc_word_alloc(m, line, la, p))
1119 return(0);
1120 continue;
1121 }
1122
1123 if (NULL == body) {
1124 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1125 return(0);
1126 body = m->last;
1127 }
1128
1129 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1130
1131 if (MDOC_MAX == ntok) {
1132 if ( ! mdoc_word_alloc(m, line, la, p))
1133 return(0);
1134 continue;
1135 }
1136
1137 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1138 return(0);
1139 break;
1140 }
1141
1142 /* Clean-ups to leave in a consistent state. */
1143
1144 if (NULL == body) {
1145 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1146 return(0);
1147 body = m->last;
1148 }
1149
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
1154 * crufty use of `Op' breakage.
1155 */
1156 for (n = m->last; n; n = n->parent)
1157 if (body == n)
1158 break;
1159
1160 if (NULL == n && ! mdoc_nwarn(m, body, EIMPBRK))
1161 return(0);
1162
1163 if (n && ! rew_last(m, body))
1164 return(0);
1165
1166 /* Standard appending of delimiters. */
1167
1168 if (1 == ppos && ! append_delims(m, line, pos, buf))
1169 return(0);
1170
1171 /* Rewind scope, if applicable. */
1172
1173 if (n && ! rew_last(m, blk))
1174 return(0);
1175
1176 return(1);
1177 }
1178
1179
1180 static int
1181 blk_part_exp(MACRO_PROT_ARGS)
1182 {
1183 int la;
1184 enum margserr ac;
1185 struct mdoc_node *head; /* keep track of head */
1186 struct mdoc_node *body; /* keep track of body */
1187 char *p;
1188 enum mdoct ntok;
1189
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 */
1195
1196 if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL))
1197 return(0);
1198
1199 for (head = body = NULL; ; ) {
1200 la = *pos;
1201 ac = mdoc_args(m, line, pos, buf, tok, &p);
1202
1203 if (ARGS_ERROR == ac)
1204 return(0);
1205 if (ARGS_PUNCT == ac)
1206 break;
1207 if (ARGS_EOLN == ac)
1208 break;
1209
1210 /* Flush out leading punctuation. */
1211
1212 if (NULL == head && ARGS_QWORD != ac &&
1213 1 == mdoc_isdelim(p)) {
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))
1223 return(0);
1224 head = m->last;
1225 }
1226
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
1239 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1240 return(0);
1241 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1242 return(0);
1243 body = m->last;
1244
1245 if (MDOC_Eo == tok)
1246 continue;
1247 }
1248
1249 assert(NULL != head && NULL != body);
1250
1251 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1252
1253 if (MDOC_MAX == ntok) {
1254 if ( ! mdoc_word_alloc(m, line, la, p))
1255 return(0);
1256 continue;
1257 }
1258
1259 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1260 return(0);
1261 break;
1262 }
1263
1264 /* Clean-up to leave in a consistent state. */
1265
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) {
1273 if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos))
1274 return(0);
1275 if ( ! mdoc_body_alloc(m, line, ppos, tok))
1276 return(0);
1277 body = m->last;
1278 }
1279
1280 /* Standard appending of delimiters. */
1281
1282 if (ppos > 1)
1283 return(1);
1284
1285 return(append_delims(m, line, pos, buf));
1286 }
1287
1288
1289 static int
1290 in_line_argn(MACRO_PROT_ARGS)
1291 {
1292 int la, flushed, j, maxargs;
1293 enum margserr ac;
1294 enum margverr av;
1295 struct mdoc_arg *arg;
1296 char *p;
1297 enum mdoct ntok;
1298
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 */
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;
1317 case (MDOC_Xr):
1318 maxargs = 2;
1319 break;
1320 default:
1321 maxargs = 1;
1322 break;
1323 }
1324
1325 for (arg = NULL; ; ) {
1326 la = *pos;
1327 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1328
1329 if (ARGV_WORD == av) {
1330 *pos = la;
1331 break;
1332 }
1333
1334 if (ARGV_EOLN == av)
1335 break;
1336 if (ARGV_ARG == av)
1337 continue;
1338
1339 mdoc_argv_free(arg);
1340 return(0);
1341 }
1342
1343 for (flushed = j = 0; ; ) {
1344 la = *pos;
1345 ac = mdoc_args(m, line, pos, buf, tok, &p);
1346
1347 if (ARGS_ERROR == ac)
1348 return(0);
1349 if (ARGS_PUNCT == ac)
1350 break;
1351 if (ARGS_EOLN == ac)
1352 break;
1353
1354 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1355 ARGS_QWORD != ac &&
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
1370 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1371
1372 if (MDOC_MAX != ntok) {
1373 if ( ! flushed && ! rew_elem(m, tok))
1374 return(0);
1375 flushed = 1;
1376 if ( ! mdoc_macro(m, ntok, line, la, pos, buf))
1377 return(0);
1378 j++;
1379 break;
1380 }
1381
1382 if ( ! (MDOC_IGNDELIM & mdoc_macros[tok].flags) &&
1383 ARGS_QWORD != ac &&
1384 ! flushed && mdoc_isdelim(p)) {
1385 if ( ! rew_elem(m, tok))
1386 return(0);
1387 flushed = 1;
1388 }
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 */
1396
1397 #ifdef __OpenBSD__
1398 if (MDOC_Xr == tok && j == maxargs) {
1399 if ( ! mdoc_elem_alloc(m, line, la, MDOC_Ns, NULL))
1400 return(0);
1401 if ( ! rew_elem(m, MDOC_Ns))
1402 return(0);
1403 }
1404 #endif
1405
1406 if ( ! mdoc_word_alloc(m, line, la, p))
1407 return(0);
1408 j++;
1409 }
1410
1411 if (0 == j && ! mdoc_elem_alloc(m, line, la, tok, arg))
1412 return(0);
1413
1414 /* Close out in a consistent state. */
1415
1416 if ( ! flushed && ! rew_elem(m, tok))
1417 return(0);
1418
1419 if (ppos > 1)
1420 return(1);
1421 return(append_delims(m, line, pos, buf));
1422 }
1423
1424
1425 static int
1426 in_line_eoln(MACRO_PROT_ARGS)
1427 {
1428 int la;
1429 enum margserr ac;
1430 enum margverr av;
1431 struct mdoc_arg *arg;
1432 char *p;
1433 enum mdoct ntok;
1434
1435 assert( ! (MDOC_PARSED & mdoc_macros[tok].flags));
1436
1437 /* Parse macro arguments. */
1438
1439 for (arg = NULL; ; ) {
1440 la = *pos;
1441 av = mdoc_argv(m, line, tok, &arg, pos, buf);
1442
1443 if (ARGV_WORD == av) {
1444 *pos = la;
1445 break;
1446 }
1447 if (ARGV_EOLN == av)
1448 break;
1449 if (ARGV_ARG == av)
1450 continue;
1451
1452 mdoc_argv_free(arg);
1453 return(0);
1454 }
1455
1456 /* Open element scope. */
1457
1458 if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg))
1459 return(0);
1460
1461 /* Parse argument terms. */
1462
1463 for (;;) {
1464 la = *pos;
1465 ac = mdoc_args(m, line, pos, buf, tok, &p);
1466
1467 if (ARGS_ERROR == ac)
1468 return(0);
1469 if (ARGS_EOLN == ac)
1470 break;
1471
1472 ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p);
1473
1474 if (MDOC_MAX == ntok) {
1475 if ( ! mdoc_word_alloc(m, line, la, p))
1476 return(0);
1477 continue;
1478 }
1479
1480 if ( ! rew_elem(m, tok))
1481 return(0);
1482 return(mdoc_macro(m, ntok, line, la, pos, buf));
1483 }
1484
1485 /* Close out (no delimiters). */
1486
1487 return(rew_elem(m, tok));
1488 }
1489
1490
1491 /* ARGSUSED */
1492 static int
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
1516 obsolete(MACRO_PROT_ARGS)
1517 {
1518
1519 return(mdoc_pwarn(m, line, ppos, EOBS));
1520 }
1521
1522
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 */
1528 static int
1529 phrase(struct mdoc *m, int line, int ppos, char *buf,
1530 enum margserr ac, int count)
1531 {
1532 int la, pos;
1533 enum margserr aac;
1534 enum mdoct ntok;
1535 char *p;
1536
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
1544 for (pos = ppos; ; ) {
1545 la = pos;
1546
1547 /* Note: no calling context! */
1548 aac = mdoc_zargs(m, line, &pos, buf, 0, &p);
1549
1550 if (ARGS_ERROR == aac)
1551 return(0);
1552 if (ARGS_EOLN == aac)
1553 break;
1554
1555 ntok = ARGS_QWORD == aac || ARGS_PEND == ac ?
1556 MDOC_MAX : lookup_raw(p);
1557
1558 if (MDOC_MAX == ntok) {
1559 if ( ! mdoc_word_alloc(m, line, la, p))
1560 return(0);
1561 continue;
1562 }
1563
1564 if ( ! mdoc_macro(m, ntok, line, la, &pos, buf))
1565 return(0);
1566 return(append_delims(m, line, &pos, buf));
1567 }
1568
1569 return(1);
1570 }
1571
1572