Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.22
1.22 ! schwarze 1: /* $Id: mdoc_action.c,v 1.21 2009/09/21 21:11: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 <sys/utsname.h>
18:
19: #include <assert.h>
20: #include <errno.h>
21: #include <stdio.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "libmdoc.h"
26:
1.20 schwarze 27: #define POST_ARGS struct mdoc *m, struct mdoc_node *n
1.1 kristaps 28: #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
29:
30: struct actions {
31: int (*pre)(PRE_ARGS);
32: int (*post)(POST_ARGS);
33: };
34:
35: static int post_ar(POST_ARGS);
1.15 schwarze 36: static int post_at(POST_ARGS);
1.1 kristaps 37: static int post_bl(POST_ARGS);
1.6 schwarze 38: static int post_bl_head(POST_ARGS);
1.15 schwarze 39: static int post_bl_tagwidth(POST_ARGS);
1.1 kristaps 40: static int post_bl_width(POST_ARGS);
41: static int post_dd(POST_ARGS);
42: static int post_display(POST_ARGS);
43: static int post_dt(POST_ARGS);
1.15 schwarze 44: static int post_lb(POST_ARGS);
1.1 kristaps 45: static int post_nm(POST_ARGS);
46: static int post_os(POST_ARGS);
47: static int post_prol(POST_ARGS);
48: static int post_sh(POST_ARGS);
1.15 schwarze 49: static int post_st(POST_ARGS);
1.1 kristaps 50: static int post_std(POST_ARGS);
1.17 schwarze 51: static int post_tilde(POST_ARGS);
1.1 kristaps 52:
53: static int pre_bd(PRE_ARGS);
1.22 ! schwarze 54: static int pre_bl(PRE_ARGS);
1.1 kristaps 55: static int pre_dl(PRE_ARGS);
1.22 ! schwarze 56: static int pre_offset(PRE_ARGS);
1.1 kristaps 57:
1.21 schwarze 58: static const struct actions mdoc_actions[MDOC_MAX] = {
1.4 schwarze 59: { NULL, NULL }, /* Ap */
1.1 kristaps 60: { NULL, post_dd }, /* Dd */
61: { NULL, post_dt }, /* Dt */
62: { NULL, post_os }, /* Os */
63: { NULL, post_sh }, /* Sh */
64: { NULL, NULL }, /* Ss */
65: { NULL, NULL }, /* Pp */
66: { NULL, NULL }, /* D1 */
67: { pre_dl, post_display }, /* Dl */
68: { pre_bd, post_display }, /* Bd */
69: { NULL, NULL }, /* Ed */
1.22 ! schwarze 70: { pre_bl, post_bl }, /* Bl */
1.1 kristaps 71: { NULL, NULL }, /* El */
72: { NULL, NULL }, /* It */
73: { NULL, NULL }, /* Ad */
74: { NULL, NULL }, /* An */
75: { NULL, post_ar }, /* Ar */
1.20 schwarze 76: { NULL, NULL }, /* Cd */
1.1 kristaps 77: { NULL, NULL }, /* Cm */
78: { NULL, NULL }, /* Dv */
79: { NULL, NULL }, /* Er */
80: { NULL, NULL }, /* Ev */
81: { NULL, post_std }, /* Ex */
82: { NULL, NULL }, /* Fa */
83: { NULL, NULL }, /* Fd */
84: { NULL, NULL }, /* Fl */
85: { NULL, NULL }, /* Fn */
86: { NULL, NULL }, /* Ft */
87: { NULL, NULL }, /* Ic */
88: { NULL, NULL }, /* In */
89: { NULL, NULL }, /* Li */
90: { NULL, NULL }, /* Nd */
91: { NULL, post_nm }, /* Nm */
92: { NULL, NULL }, /* Op */
93: { NULL, NULL }, /* Ot */
1.17 schwarze 94: { NULL, post_tilde }, /* Pa */
1.1 kristaps 95: { NULL, post_std }, /* Rv */
1.15 schwarze 96: { NULL, post_st }, /* St */
1.1 kristaps 97: { NULL, NULL }, /* Va */
98: { NULL, NULL }, /* Vt */
99: { NULL, NULL }, /* Xr */
100: { NULL, NULL }, /* %A */
101: { NULL, NULL }, /* %B */
102: { NULL, NULL }, /* %D */
103: { NULL, NULL }, /* %I */
104: { NULL, NULL }, /* %J */
105: { NULL, NULL }, /* %N */
106: { NULL, NULL }, /* %O */
107: { NULL, NULL }, /* %P */
108: { NULL, NULL }, /* %R */
109: { NULL, NULL }, /* %T */
110: { NULL, NULL }, /* %V */
111: { NULL, NULL }, /* Ac */
112: { NULL, NULL }, /* Ao */
113: { NULL, NULL }, /* Aq */
1.15 schwarze 114: { NULL, post_at }, /* At */
1.1 kristaps 115: { NULL, NULL }, /* Bc */
116: { NULL, NULL }, /* Bf */
117: { NULL, NULL }, /* Bo */
118: { NULL, NULL }, /* Bq */
119: { NULL, NULL }, /* Bsx */
120: { NULL, NULL }, /* Bx */
121: { NULL, NULL }, /* Db */
122: { NULL, NULL }, /* Dc */
123: { NULL, NULL }, /* Do */
124: { NULL, NULL }, /* Dq */
125: { NULL, NULL }, /* Ec */
126: { NULL, NULL }, /* Ef */
127: { NULL, NULL }, /* Em */
128: { NULL, NULL }, /* Eo */
129: { NULL, NULL }, /* Fx */
130: { NULL, NULL }, /* Ms */
131: { NULL, NULL }, /* No */
132: { NULL, NULL }, /* Ns */
133: { NULL, NULL }, /* Nx */
134: { NULL, NULL }, /* Ox */
135: { NULL, NULL }, /* Pc */
136: { NULL, NULL }, /* Pf */
137: { NULL, NULL }, /* Po */
138: { NULL, NULL }, /* Pq */
139: { NULL, NULL }, /* Qc */
140: { NULL, NULL }, /* Ql */
141: { NULL, NULL }, /* Qo */
142: { NULL, NULL }, /* Qq */
143: { NULL, NULL }, /* Re */
144: { NULL, NULL }, /* Rs */
145: { NULL, NULL }, /* Sc */
146: { NULL, NULL }, /* So */
147: { NULL, NULL }, /* Sq */
148: { NULL, NULL }, /* Sm */
149: { NULL, NULL }, /* Sx */
150: { NULL, NULL }, /* Sy */
151: { NULL, NULL }, /* Tn */
152: { NULL, NULL }, /* Ux */
153: { NULL, NULL }, /* Xc */
154: { NULL, NULL }, /* Xo */
155: { NULL, NULL }, /* Fo */
156: { NULL, NULL }, /* Fc */
157: { NULL, NULL }, /* Oo */
158: { NULL, NULL }, /* Oc */
159: { NULL, NULL }, /* Bk */
160: { NULL, NULL }, /* Ek */
161: { NULL, NULL }, /* Bt */
162: { NULL, NULL }, /* Hf */
163: { NULL, NULL }, /* Fr */
164: { NULL, NULL }, /* Ud */
1.15 schwarze 165: { NULL, post_lb }, /* Lb */
1.1 kristaps 166: { NULL, NULL }, /* Lp */
1.17 schwarze 167: { NULL, post_tilde }, /* Lk */
1.1 kristaps 168: { NULL, NULL }, /* Mt */
169: { NULL, NULL }, /* Brq */
170: { NULL, NULL }, /* Bro */
171: { NULL, NULL }, /* Brc */
172: { NULL, NULL }, /* %C */
173: { NULL, NULL }, /* Es */
174: { NULL, NULL }, /* En */
175: { NULL, NULL }, /* Dx */
176: { NULL, NULL }, /* %Q */
1.16 schwarze 177: { NULL, NULL }, /* br */
178: { NULL, NULL }, /* sp */
1.1 kristaps 179: };
180:
1.15 schwarze 181: static int concat(struct mdoc *, const struct mdoc_node *,
182: char *, size_t);
183:
1.1 kristaps 184:
185: int
186: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
187: {
188:
189: switch (n->type) {
190: case (MDOC_ROOT):
191: /* FALLTHROUGH */
192: case (MDOC_TEXT):
193: return(1);
194: default:
195: break;
196: }
197:
198: if (NULL == mdoc_actions[n->tok].pre)
199: return(1);
200: return((*mdoc_actions[n->tok].pre)(m, n));
201: }
202:
203:
204: int
205: mdoc_action_post(struct mdoc *m)
206: {
207:
208: if (MDOC_ACTED & m->last->flags)
209: return(1);
210: m->last->flags |= MDOC_ACTED;
211:
212: switch (m->last->type) {
213: case (MDOC_TEXT):
214: /* FALLTHROUGH */
215: case (MDOC_ROOT):
216: return(1);
217: default:
218: break;
219: }
220:
221: if (NULL == mdoc_actions[m->last->tok].post)
222: return(1);
1.20 schwarze 223: return((*mdoc_actions[m->last->tok].post)(m, m->last));
1.1 kristaps 224: }
225:
226:
227: static int
228: concat(struct mdoc *m, const struct mdoc_node *n,
229: char *buf, size_t sz)
230: {
231:
232: for ( ; n; n = n->next) {
233: assert(MDOC_TEXT == n->type);
234: if (strlcat(buf, n->string, sz) >= sz)
1.12 schwarze 235: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 236: if (NULL == n->next)
237: continue;
238: if (strlcat(buf, " ", sz) >= sz)
1.12 schwarze 239: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 240: }
241:
242: return(1);
243: }
244:
245:
246: static int
247: post_std(POST_ARGS)
248: {
1.20 schwarze 249: struct mdoc_node *nn;
1.1 kristaps 250:
1.20 schwarze 251: if (n->child)
1.1 kristaps 252: return(1);
1.20 schwarze 253:
254: nn = n;
255: m->next = MDOC_NEXT_CHILD;
1.1 kristaps 256: assert(m->meta.name);
1.20 schwarze 257: if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name))
258: return(0);
259: m->last = nn;
1.1 kristaps 260:
261: return(1);
262: }
263:
264:
265: static int
266: post_nm(POST_ARGS)
267: {
268: char buf[64];
269:
270: if (m->meta.name)
271: return(1);
272:
273: buf[0] = 0;
1.20 schwarze 274: if ( ! concat(m, n->child, buf, sizeof(buf)))
1.1 kristaps 275: return(0);
276: if (NULL == (m->meta.name = strdup(buf)))
1.20 schwarze 277: return(mdoc_nerr(m, n, EMALLOC));
1.2 schwarze 278:
1.1 kristaps 279: return(1);
280: }
281:
282:
283: static int
1.15 schwarze 284: post_lb(POST_ARGS)
285: {
286: const char *p;
287: char *buf;
288: size_t sz;
289:
1.20 schwarze 290: assert(MDOC_TEXT == n->child->type);
291: p = mdoc_a2lib(n->child->string);
1.15 schwarze 292: if (NULL == p) {
1.20 schwarze 293: sz = strlen(n->child->string) +
1.15 schwarze 294: 2 + strlen("\\(lqlibrary\\(rq");
295: buf = malloc(sz);
296: if (NULL == buf)
1.20 schwarze 297: return(mdoc_nerr(m, n, EMALLOC));
1.15 schwarze 298: (void)snprintf(buf, sz, "library \\(lq%s\\(rq",
1.20 schwarze 299: n->child->string);
300: free(n->child->string);
301: n->child->string = buf;
1.15 schwarze 302: return(1);
303: }
304:
1.20 schwarze 305: free(n->child->string);
306: n->child->string = strdup(p);
307: if (NULL == n->child->string)
308: return(mdoc_nerr(m, n, EMALLOC));
309:
1.15 schwarze 310: return(1);
311: }
312:
313:
314: static int
315: post_st(POST_ARGS)
316: {
317: const char *p;
318:
1.20 schwarze 319: assert(MDOC_TEXT == n->child->type);
320: p = mdoc_a2st(n->child->string);
1.15 schwarze 321: assert(p);
1.20 schwarze 322: free(n->child->string);
323: n->child->string = strdup(p);
324: if (NULL == n->child->string)
325: return(mdoc_nerr(m, n, EMALLOC));
326:
1.15 schwarze 327: return(1);
328: }
329:
330:
331: static int
332: post_at(POST_ARGS)
333: {
1.20 schwarze 334: struct mdoc_node *nn;
335: const char *p;
1.15 schwarze 336:
1.20 schwarze 337: if (n->child) {
338: assert(MDOC_TEXT == n->child->type);
339: p = mdoc_a2att(n->child->string);
1.15 schwarze 340: assert(p);
1.20 schwarze 341: free(n->child->string);
342: n->child->string = strdup(p);
343: if (NULL == n->child->string)
344: return(mdoc_nerr(m, n, EMALLOC));
1.15 schwarze 345: return(1);
346: }
347:
1.20 schwarze 348: nn = n;
1.15 schwarze 349: m->next = MDOC_NEXT_CHILD;
350:
1.20 schwarze 351: if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX"))
1.15 schwarze 352: return(0);
1.20 schwarze 353: m->last = nn;
1.15 schwarze 354:
355: return(1);
356: }
357:
358:
359: static int
1.1 kristaps 360: post_sh(POST_ARGS)
361: {
362: enum mdoc_sec sec;
363: char buf[64];
364:
365: /*
366: * We keep track of the current section /and/ the "named"
367: * section, which is one of the conventional ones, in order to
368: * check ordering.
369: */
370:
1.20 schwarze 371: if (MDOC_HEAD != n->type)
1.1 kristaps 372: return(1);
373:
374: buf[0] = 0;
1.20 schwarze 375: if ( ! concat(m, n->child, buf, sizeof(buf)))
1.1 kristaps 376: return(0);
377: if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
378: m->lastnamed = sec;
379:
380: switch ((m->lastsec = sec)) {
381: case (SEC_RETURN_VALUES):
382: /* FALLTHROUGH */
383: case (SEC_ERRORS):
384: switch (m->meta.msec) {
385: case (2):
386: /* FALLTHROUGH */
387: case (3):
388: /* FALLTHROUGH */
389: case (9):
390: break;
391: default:
1.20 schwarze 392: return(mdoc_nwarn(m, n, EBADSEC));
1.1 kristaps 393: }
394: break;
395: default:
396: break;
397: }
398: return(1);
399: }
400:
401:
402: static int
403: post_dt(POST_ARGS)
404: {
1.20 schwarze 405: struct mdoc_node *nn;
1.1 kristaps 406: const char *cp;
407: char *ep;
408: long lval;
409:
410: if (m->meta.title)
411: free(m->meta.title);
412: if (m->meta.vol)
413: free(m->meta.vol);
414: if (m->meta.arch)
415: free(m->meta.arch);
416:
417: m->meta.title = m->meta.vol = m->meta.arch = NULL;
418: m->meta.msec = 0;
419:
420: /* Handles: `.Dt'
421: * --> title = unknown, volume = local, msec = 0, arch = NULL
422: */
423:
1.20 schwarze 424: if (NULL == (nn = n->child)) {
1.1 kristaps 425: if (NULL == (m->meta.title = strdup("unknown")))
1.20 schwarze 426: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 427: if (NULL == (m->meta.vol = strdup("local")))
1.20 schwarze 428: return(mdoc_nerr(m, n, EMALLOC));
429: return(post_prol(m, n));
1.1 kristaps 430: }
431:
432: /* Handles: `.Dt TITLE'
433: * --> title = TITLE, volume = local, msec = 0, arch = NULL
434: */
435:
1.20 schwarze 436: if (NULL == (m->meta.title = strdup(nn->string)))
437: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 438:
1.20 schwarze 439: if (NULL == (nn = nn->next)) {
1.1 kristaps 440: if (NULL == (m->meta.vol = strdup("local")))
1.20 schwarze 441: return(mdoc_nerr(m, n, EMALLOC));
442: return(post_prol(m, n));
1.1 kristaps 443: }
444:
445: /* Handles: `.Dt TITLE SEC'
446: * --> title = TITLE, volume = SEC is msec ?
447: * format(msec) : SEC,
448: * msec = SEC is msec ? atoi(msec) : 0,
449: * arch = NULL
450: */
451:
1.20 schwarze 452: cp = mdoc_a2msec(nn->string);
1.1 kristaps 453: if (cp) {
454: if (NULL == (m->meta.vol = strdup(cp)))
1.20 schwarze 455: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 456: errno = 0;
1.20 schwarze 457: lval = strtol(nn->string, &ep, 10);
458: if (nn->string[0] != '\0' && *ep == '\0')
1.1 kristaps 459: m->meta.msec = (int)lval;
1.20 schwarze 460: } else if (NULL == (m->meta.vol = strdup(nn->string)))
461: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 462:
1.20 schwarze 463: if (NULL == (nn = nn->next))
464: return(post_prol(m, n));
1.1 kristaps 465:
466: /* Handles: `.Dt TITLE SEC VOL'
467: * --> title = TITLE, volume = VOL is vol ?
468: * format(VOL) :
469: * VOL is arch ? format(arch) :
470: * VOL
471: */
472:
1.20 schwarze 473: cp = mdoc_a2vol(nn->string);
1.1 kristaps 474: if (cp) {
475: free(m->meta.vol);
476: if (NULL == (m->meta.vol = strdup(cp)))
1.20 schwarze 477: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 478: } else {
1.20 schwarze 479: cp = mdoc_a2arch(nn->string);
1.1 kristaps 480: if (NULL == cp) {
481: free(m->meta.vol);
1.20 schwarze 482: if (NULL == (m->meta.vol = strdup(nn->string)))
483: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 484: } else if (NULL == (m->meta.arch = strdup(cp)))
1.20 schwarze 485: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 486: }
487:
488: /* Ignore any subsequent parameters... */
489:
1.20 schwarze 490: return(post_prol(m, n));
1.1 kristaps 491: }
492:
493:
494: static int
495: post_os(POST_ARGS)
496: {
497: char buf[64];
498: struct utsname utsname;
499:
500: if (m->meta.os)
501: free(m->meta.os);
502:
503: buf[0] = 0;
1.20 schwarze 504: if ( ! concat(m, n->child, buf, sizeof(buf)))
1.1 kristaps 505: return(0);
506:
507: if (0 == buf[0]) {
508: if (-1 == uname(&utsname))
1.20 schwarze 509: return(mdoc_nerr(m, n, EUTSNAME));
1.1 kristaps 510: if (strlcat(buf, utsname.sysname, 64) >= 64)
1.20 schwarze 511: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 512: if (strlcat(buf, " ", 64) >= 64)
1.20 schwarze 513: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 514: if (strlcat(buf, utsname.release, 64) >= 64)
1.20 schwarze 515: return(mdoc_nerr(m, n, ETOOLONG));
1.1 kristaps 516: }
517:
518: if (NULL == (m->meta.os = strdup(buf)))
1.20 schwarze 519: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 520:
1.20 schwarze 521: return(post_prol(m, n));
1.1 kristaps 522: }
523:
524:
525: /*
526: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
527: * Uses the first head macro.
528: */
529: static int
1.20 schwarze 530: post_bl_tagwidth(POST_ARGS)
1.1 kristaps 531: {
1.20 schwarze 532: struct mdoc_node *nn;
1.1 kristaps 533: int sz;
534: char buf[32];
535:
536: /*
537: * Use the text width, if a text node, or the default macro
538: * width if a macro.
539: */
540:
1.20 schwarze 541: nn = n->body->child;
542: if (nn) {
543: assert(MDOC_BLOCK == nn->type);
544: assert(MDOC_It == nn->tok);
545: nn = nn->head->child;
1.1 kristaps 546: }
547:
548: sz = 10; /* Default size. */
549:
1.20 schwarze 550: if (nn) {
551: if (MDOC_TEXT != nn->type) {
552: if (0 == (sz = (int)mdoc_macro2len(nn->tok)))
553: if ( ! mdoc_nwarn(m, n, ENOWIDTH))
1.1 kristaps 554: return(0);
555: } else
1.20 schwarze 556: sz = (int)strlen(nn->string) + 1;
1.1 kristaps 557: }
558:
559: if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
1.20 schwarze 560: return(mdoc_nerr(m, n, ENUMFMT));
1.1 kristaps 561:
562: /*
563: * We have to dynamically add this to the macro's argument list.
564: * We're guaranteed that a MDOC_Width doesn't already exist.
565: */
566:
1.20 schwarze 567: nn = n;
568: assert(nn->args);
569: sz = (int)(nn->args->argc)++;
1.1 kristaps 570:
1.20 schwarze 571: nn->args->argv = realloc(nn->args->argv,
572: nn->args->argc * sizeof(struct mdoc_argv));
1.1 kristaps 573:
1.20 schwarze 574: if (NULL == nn->args->argv)
575: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 576:
1.20 schwarze 577: nn->args->argv[sz].arg = MDOC_Width;
578: nn->args->argv[sz].line = n->line;
579: nn->args->argv[sz].pos = n->pos;
580: nn->args->argv[sz].sz = 1;
581: nn->args->argv[sz].value = calloc(1, sizeof(char *));
1.1 kristaps 582:
1.20 schwarze 583: if (NULL == nn->args->argv[sz].value)
584: return(mdoc_nerr(m, n, EMALLOC));
585: if (NULL == (nn->args->argv[sz].value[0] = strdup(buf)))
586: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 587:
588: return(1);
589: }
590:
591:
592: static int
1.20 schwarze 593: post_bl_width(POST_ARGS)
1.1 kristaps 594: {
595: size_t width;
596: int i, tok;
597: char buf[32];
598: char *p;
599:
1.20 schwarze 600: if (NULL == n->args)
1.1 kristaps 601: return(1);
602:
1.20 schwarze 603: for (i = 0; i < (int)n->args->argc; i++)
604: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 605: break;
606:
1.20 schwarze 607: if (i == (int)n->args->argc)
1.1 kristaps 608: return(1);
1.20 schwarze 609: p = n->args->argv[i].value[0];
1.1 kristaps 610:
611: /*
612: * If the value to -width is a macro, then we re-write it to be
613: * the macro's width as set in share/tmac/mdoc/doc-common.
614: */
615:
616: if (0 == strcmp(p, "Ds"))
1.9 schwarze 617: width = 6;
1.21 schwarze 618: else if (MDOC_MAX == (tok = mdoc_hash_find(p)))
1.1 kristaps 619: return(1);
620: else if (0 == (width = mdoc_macro2len(tok)))
1.20 schwarze 621: return(mdoc_nwarn(m, n, ENOWIDTH));
1.1 kristaps 622:
623: /* The value already exists: free and reallocate it. */
624:
625: if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
1.20 schwarze 626: return(mdoc_nerr(m, n, ENUMFMT));
1.1 kristaps 627:
1.20 schwarze 628: free(n->args->argv[i].value[0]);
629: n->args->argv[i].value[0] = strdup(buf);
630: if (NULL == n->args->argv[i].value[0])
631: return(mdoc_nerr(m, n, EMALLOC));
1.1 kristaps 632:
633: return(1);
634: }
635:
636:
1.20 schwarze 637: /* ARGSUSED */
1.1 kristaps 638: static int
1.6 schwarze 639: post_bl_head(POST_ARGS)
640: {
641: int i, c;
1.20 schwarze 642: struct mdoc_node *np, *nn, *nnp;
1.6 schwarze 643:
1.20 schwarze 644: if (NULL == n->child)
1.6 schwarze 645: return(1);
646:
1.20 schwarze 647: np = n->parent;
648: assert(np->args);
1.6 schwarze 649:
1.20 schwarze 650: for (c = 0; c < (int)np->args->argc; c++)
651: if (MDOC_Column == np->args->argv[c].arg)
1.6 schwarze 652: break;
653:
654: /* Only process -column. */
655:
1.20 schwarze 656: if (c == (int)np->args->argc)
1.6 schwarze 657: return(1);
658:
1.20 schwarze 659: assert(0 == np->args->argv[c].sz);
1.6 schwarze 660:
661: /*
662: * Accomodate for new-style groff column syntax. Shuffle the
663: * child nodes, all of which must be TEXT, as arguments for the
664: * column field. Then, delete the head children.
665: */
666:
1.20 schwarze 667: np->args->argv[c].sz = (size_t)n->nchild;
668: np->args->argv[c].value = malloc
669: ((size_t)n->nchild * sizeof(char *));
1.6 schwarze 670:
1.20 schwarze 671: for (i = 0, nn = n->child; nn; i++) {
672: np->args->argv[c].value[i] = nn->string;
1.6 schwarze 673: nn->string = NULL;
674: nnp = nn;
675: nn = nn->next;
676: mdoc_node_free(nnp);
677: }
678:
1.20 schwarze 679: n->nchild = 0;
680: n->child = NULL;
1.8 schwarze 681:
1.6 schwarze 682: return(1);
683: }
684:
685:
686: static int
1.1 kristaps 687: post_bl(POST_ARGS)
688: {
689: int i, r, len;
690:
1.20 schwarze 691: if (MDOC_HEAD == n->type)
692: return(post_bl_head(m, n));
693: if (MDOC_BLOCK != n->type)
1.1 kristaps 694: return(1);
695:
696: /*
697: * These are fairly complicated, so we've broken them into two
698: * functions. post_bl_tagwidth() is called when a -tag is
699: * specified, but no -width (it must be guessed). The second
700: * when a -width is specified (macro indicators must be
701: * rewritten into real lengths).
702: */
703:
1.20 schwarze 704: len = (int)(n->args ? n->args->argc : 0);
1.1 kristaps 705:
706: for (r = i = 0; i < len; i++) {
1.20 schwarze 707: if (MDOC_Tag == n->args->argv[i].arg)
1.1 kristaps 708: r |= 1 << 0;
1.20 schwarze 709: if (MDOC_Width == n->args->argv[i].arg)
1.1 kristaps 710: r |= 1 << 1;
711: }
712:
713: if (r & (1 << 0) && ! (r & (1 << 1))) {
1.20 schwarze 714: if ( ! post_bl_tagwidth(m, n))
1.1 kristaps 715: return(0);
716: } else if (r & (1 << 1))
1.20 schwarze 717: if ( ! post_bl_width(m, n))
1.1 kristaps 718: return(0);
719:
1.3 schwarze 720: return(1);
721: }
722:
723:
724: static int
1.17 schwarze 725: post_tilde(POST_ARGS)
1.3 schwarze 726: {
1.20 schwarze 727: struct mdoc_node *np;
1.3 schwarze 728:
1.20 schwarze 729: if (n->child)
1.3 schwarze 730: return(1);
731:
1.20 schwarze 732: np = n;
1.3 schwarze 733: m->next = MDOC_NEXT_CHILD;
1.15 schwarze 734:
1.17 schwarze 735: /* XXX: not documented for `Lk'. */
1.20 schwarze 736: if ( ! mdoc_word_alloc(m, n->line, n->pos, "~"))
1.3 schwarze 737: return(0);
1.20 schwarze 738: m->last = np;
1.3 schwarze 739:
1.1 kristaps 740: return(1);
741: }
742:
743:
744: static int
745: post_ar(POST_ARGS)
746: {
1.20 schwarze 747: struct mdoc_node *np;
1.1 kristaps 748:
1.20 schwarze 749: if (n->child)
1.1 kristaps 750: return(1);
751:
1.20 schwarze 752: np = n;
1.1 kristaps 753: m->next = MDOC_NEXT_CHILD;
1.20 schwarze 754: if ( ! mdoc_word_alloc(m, n->line, n->pos, "file"))
1.1 kristaps 755: return(0);
1.20 schwarze 756: if ( ! mdoc_word_alloc(m, n->line, n->pos, "..."))
1.1 kristaps 757: return(0);
1.20 schwarze 758: m->last = np;
1.1 kristaps 759:
760: return(1);
761: }
762:
763:
764: static int
765: post_dd(POST_ARGS)
766: {
767: char buf[64];
768:
769: buf[0] = 0;
1.20 schwarze 770: if ( ! concat(m, n->child, buf, sizeof(buf)))
1.1 kristaps 771: return(0);
772:
773: if (0 == (m->meta.date = mdoc_atotime(buf))) {
1.20 schwarze 774: if ( ! mdoc_nwarn(m, n, EBADDATE))
1.1 kristaps 775: return(0);
776: m->meta.date = time(NULL);
777: }
778:
1.20 schwarze 779: return(post_prol(m, n));
1.1 kristaps 780: }
781:
782:
783: static int
784: post_prol(POST_ARGS)
785: {
1.20 schwarze 786: struct mdoc_node *np;
1.1 kristaps 787:
1.20 schwarze 788: /* Remove prologue macros from AST. */
1.1 kristaps 789:
1.20 schwarze 790: if (n->parent->child == n)
791: n->parent->child = n->prev;
792: if (n->prev)
793: n->prev->next = NULL;
1.1 kristaps 794:
1.20 schwarze 795: np = n;
796: assert(NULL == n->next);
1.1 kristaps 797:
1.20 schwarze 798: if (n->prev) {
799: m->last = n->prev;
1.1 kristaps 800: m->next = MDOC_NEXT_SIBLING;
801: } else {
1.20 schwarze 802: m->last = n->parent;
1.1 kristaps 803: m->next = MDOC_NEXT_CHILD;
804: }
805:
1.20 schwarze 806: mdoc_node_freelist(np);
1.18 schwarze 807:
808: if (m->meta.title && m->meta.date && m->meta.os)
809: m->flags |= MDOC_PBODY;
1.20 schwarze 810:
1.1 kristaps 811: return(1);
812: }
813:
814:
815: static int
816: pre_dl(PRE_ARGS)
817: {
818:
1.7 schwarze 819: if (MDOC_BODY == n->type)
820: m->flags |= MDOC_LITERAL;
1.1 kristaps 821: return(1);
822: }
823:
824:
825: static int
1.22 ! schwarze 826: pre_offset(PRE_ARGS)
! 827: {
! 828: int i;
! 829:
! 830: /*
! 831: * Make sure that an empty offset produces an 8n length space as
! 832: * stipulated by mdoc.samples.
! 833: */
! 834:
! 835: assert(n->args);
! 836: for (i = 0; i < (int)n->args->argc; i++) {
! 837: if (MDOC_Offset != n->args->argv[i].arg)
! 838: continue;
! 839: if (n->args->argv[i].sz)
! 840: break;
! 841: assert(1 == n->args->refcnt);
! 842: /* If no value set, length of <string>. */
! 843: n->args->argv[i].value =
! 844: calloc(1, sizeof(char *));
! 845: if (NULL == n->args->argv[i].value)
! 846: return(mdoc_nerr(m, n, EMALLOC));
! 847: n->args->argv[i].sz++;
! 848: n->args->argv[i].value[0] = strdup("8n");
! 849: if (NULL == n->args->argv[i].value[0])
! 850: return(mdoc_nerr(m, n, EMALLOC));
! 851: break;
! 852: }
! 853:
! 854: return(1);
! 855: }
! 856:
! 857:
! 858: static int
! 859: pre_bl(PRE_ARGS)
! 860: {
! 861:
! 862: return(MDOC_BLOCK == n->type ? pre_offset(m, n) : 1);
! 863: }
! 864:
! 865:
! 866: static int
1.1 kristaps 867: pre_bd(PRE_ARGS)
868: {
869: int i;
870:
1.22 ! schwarze 871: if (MDOC_BLOCK == n->type)
! 872: return(pre_offset(m, n));
1.1 kristaps 873: if (MDOC_BODY != n->type)
874: return(1);
875:
1.19 schwarze 876: /* Enter literal context if `Bd -literal' or `-unfilled'. */
877:
1.1 kristaps 878: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
879: if (MDOC_Literal == n->args->argv[i].arg)
1.22 ! schwarze 880: m->flags |= MDOC_LITERAL;
1.1 kristaps 881: else if (MDOC_Unfilled == n->args->argv[i].arg)
1.22 ! schwarze 882: m->flags |= MDOC_LITERAL;
1.1 kristaps 883:
884: return(1);
885: }
886:
887:
888: static int
889: post_display(POST_ARGS)
890: {
891:
1.20 schwarze 892: if (MDOC_BODY == n->type)
1.1 kristaps 893: m->flags &= ~MDOC_LITERAL;
894: return(1);
895: }
896:
897: