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