]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_action.c
1 /* $Id: mdoc_action.c,v 1.1 2009/03/25 15:36:05 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/utsname.h>
31 * Actions are executed on macros after they've been post-validated: in
32 * other words, a macro will not be "acted upon" until all of its
33 * children have been filled in (post-fix order).
42 #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
43 #define POST_ARGS struct mdoc *m
47 int (*post
)(POST_ARGS
);
50 static int pwarn(struct mdoc
*, int, int, enum mwarn
);
52 static int post_ar(POST_ARGS
);
53 static int post_bl(POST_ARGS
);
54 static int post_bl_width(POST_ARGS
);
55 static int post_bl_tagwidth(POST_ARGS
);
56 static int post_dd(POST_ARGS
);
57 static int post_display(POST_ARGS
);
58 static int post_dt(POST_ARGS
);
59 static int post_nm(POST_ARGS
);
60 static int post_os(POST_ARGS
);
61 static int post_prol(POST_ARGS
);
62 static int post_sh(POST_ARGS
);
63 static int post_std(POST_ARGS
);
65 static int pre_bd(PRE_ARGS
);
66 static int pre_dl(PRE_ARGS
);
68 #define mwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
70 const struct actions mdoc_actions
[MDOC_MAX
] = {
71 { NULL
, NULL
}, /* \" */
72 { NULL
, post_dd
}, /* Dd */
73 { NULL
, post_dt
}, /* Dt */
74 { NULL
, post_os
}, /* Os */
75 { NULL
, post_sh
}, /* Sh */
76 { NULL
, NULL
}, /* Ss */
77 { NULL
, NULL
}, /* Pp */
78 { NULL
, NULL
}, /* D1 */
79 { pre_dl
, post_display
}, /* Dl */
80 { pre_bd
, post_display
}, /* Bd */
81 { NULL
, NULL
}, /* Ed */
82 { NULL
, post_bl
}, /* Bl */
83 { NULL
, NULL
}, /* El */
84 { NULL
, NULL
}, /* It */
85 { NULL
, NULL
}, /* Ad */
86 { NULL
, NULL
}, /* An */
87 { NULL
, post_ar
}, /* Ar */
88 { NULL
, NULL
}, /* Cd */
89 { NULL
, NULL
}, /* Cm */
90 { NULL
, NULL
}, /* Dv */
91 { NULL
, NULL
}, /* Er */
92 { NULL
, NULL
}, /* Ev */
93 { NULL
, post_std
}, /* Ex */
94 { NULL
, NULL
}, /* Fa */
95 { NULL
, NULL
}, /* Fd */
96 { NULL
, NULL
}, /* Fl */
97 { NULL
, NULL
}, /* Fn */
98 { NULL
, NULL
}, /* Ft */
99 { NULL
, NULL
}, /* Ic */
100 { NULL
, NULL
}, /* In */
101 { NULL
, NULL
}, /* Li */
102 { NULL
, NULL
}, /* Nd */
103 { NULL
, post_nm
}, /* Nm */
104 { NULL
, NULL
}, /* Op */
105 { NULL
, NULL
}, /* Ot */
106 { NULL
, NULL
}, /* Pa */
107 { NULL
, post_std
}, /* Rv */
108 { NULL
, NULL
}, /* St */
109 { NULL
, NULL
}, /* Va */
110 { NULL
, NULL
}, /* Vt */
111 { NULL
, NULL
}, /* Xr */
112 { NULL
, NULL
}, /* %A */
113 { NULL
, NULL
}, /* %B */
114 { NULL
, NULL
}, /* %D */
115 { NULL
, NULL
}, /* %I */
116 { NULL
, NULL
}, /* %J */
117 { NULL
, NULL
}, /* %N */
118 { NULL
, NULL
}, /* %O */
119 { NULL
, NULL
}, /* %P */
120 { NULL
, NULL
}, /* %R */
121 { NULL
, NULL
}, /* %T */
122 { NULL
, NULL
}, /* %V */
123 { NULL
, NULL
}, /* Ac */
124 { NULL
, NULL
}, /* Ao */
125 { NULL
, NULL
}, /* Aq */
126 { NULL
, NULL
}, /* At */
127 { NULL
, NULL
}, /* Bc */
128 { NULL
, NULL
}, /* Bf */
129 { NULL
, NULL
}, /* Bo */
130 { NULL
, NULL
}, /* Bq */
131 { NULL
, NULL
}, /* Bsx */
132 { NULL
, NULL
}, /* Bx */
133 { NULL
, NULL
}, /* Db */
134 { NULL
, NULL
}, /* Dc */
135 { NULL
, NULL
}, /* Do */
136 { NULL
, NULL
}, /* Dq */
137 { NULL
, NULL
}, /* Ec */
138 { NULL
, NULL
}, /* Ef */
139 { NULL
, NULL
}, /* Em */
140 { NULL
, NULL
}, /* Eo */
141 { NULL
, NULL
}, /* Fx */
142 { NULL
, NULL
}, /* Ms */
143 { NULL
, NULL
}, /* No */
144 { NULL
, NULL
}, /* Ns */
145 { NULL
, NULL
}, /* Nx */
146 { NULL
, NULL
}, /* Ox */
147 { NULL
, NULL
}, /* Pc */
148 { NULL
, NULL
}, /* Pf */
149 { NULL
, NULL
}, /* Po */
150 { NULL
, NULL
}, /* Pq */
151 { NULL
, NULL
}, /* Qc */
152 { NULL
, NULL
}, /* Ql */
153 { NULL
, NULL
}, /* Qo */
154 { NULL
, NULL
}, /* Qq */
155 { NULL
, NULL
}, /* Re */
156 { NULL
, NULL
}, /* Rs */
157 { NULL
, NULL
}, /* Sc */
158 { NULL
, NULL
}, /* So */
159 { NULL
, NULL
}, /* Sq */
160 { NULL
, NULL
}, /* Sm */
161 { NULL
, NULL
}, /* Sx */
162 { NULL
, NULL
}, /* Sy */
163 { NULL
, NULL
}, /* Tn */
164 { NULL
, NULL
}, /* Ux */
165 { NULL
, NULL
}, /* Xc */
166 { NULL
, NULL
}, /* Xo */
167 { NULL
, NULL
}, /* Fo */
168 { NULL
, NULL
}, /* Fc */
169 { NULL
, NULL
}, /* Oo */
170 { NULL
, NULL
}, /* Oc */
171 { NULL
, NULL
}, /* Bk */
172 { NULL
, NULL
}, /* Ek */
173 { NULL
, NULL
}, /* Bt */
174 { NULL
, NULL
}, /* Hf */
175 { NULL
, NULL
}, /* Fr */
176 { NULL
, NULL
}, /* Ud */
177 { NULL
, NULL
}, /* Lb */
178 { NULL
, NULL
}, /* Ap */
179 { NULL
, NULL
}, /* Lp */
180 { NULL
, NULL
}, /* Lk */
181 { NULL
, NULL
}, /* Mt */
182 { NULL
, NULL
}, /* Brq */
183 { NULL
, NULL
}, /* Bro */
184 { NULL
, NULL
}, /* Brc */
185 { NULL
, NULL
}, /* %C */
186 { NULL
, NULL
}, /* Es */
187 { NULL
, NULL
}, /* En */
188 { NULL
, NULL
}, /* Dx */
189 { NULL
, NULL
}, /* %Q */
194 mdoc_action_pre(struct mdoc
*m
, const struct mdoc_node
*n
)
203 if (NULL
== mdoc_actions
[m
->last
->tok
].pre
)
205 return((*mdoc_actions
[m
->last
->tok
].pre
)(m
, n
));
212 mdoc_action_post(struct mdoc
*m
)
215 if (MDOC_ACTED
& m
->last
->flags
)
217 m
->last
->flags
|= MDOC_ACTED
;
219 switch (m
->last
->type
) {
225 if (NULL
== mdoc_actions
[m
->last
->tok
].post
)
227 return((*mdoc_actions
[m
->last
->tok
].post
)(m
));
234 pwarn(struct mdoc
*m
, int line
, int pos
, enum mwarn type
)
244 p
= "inappropriate document section in manual section";
248 p
= "cannot determine default width";
251 p
= "malformed date syntax";
256 return(mdoc_pwarn(m
, line
, pos
, c
, p
));
265 * If '-std' is invoked without an argument, fill it in with our
266 * name (if it's been set).
269 if (NULL
== m
->last
->args
)
271 if (m
->last
->args
->argv
[0].sz
)
274 assert(m
->meta
.name
);
276 m
->last
->args
->argv
[0].value
= calloc(1, sizeof(char *));
277 if (NULL
== m
->last
->args
->argv
[0].value
)
280 m
->last
->args
->argv
[0].sz
= 1;
281 m
->last
->args
->argv
[0].value
[0] = xstrdup(m
->meta
.name
);
294 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
295 m
->meta
.name
= xstrdup(buf
);
308 * We keep track of the current section /and/ the "named"
309 * section, which is one of the conventional ones, in order to
313 if (MDOC_HEAD
!= m
->last
->type
)
316 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
317 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
320 switch ((m
->lastsec
= sec
)) {
321 case (SEC_RETURN_VALUES
):
324 switch (m
->meta
.msec
) {
332 return(mwarn(m
, WBADSEC
));
357 m
->meta
.title
= m
->meta
.vol
= m
->meta
.arch
= NULL
;
361 * --> title = unknown, volume = local, msec = 0, arch = NULL
364 if (NULL
== (n
= m
->last
->child
)) {
365 m
->meta
.title
= xstrdup("unknown");
366 m
->meta
.vol
= xstrdup("local");
367 return(post_prol(m
));
370 /* Handles: `.Dt TITLE'
371 * --> title = TITLE, volume = local, msec = 0, arch = NULL
374 m
->meta
.title
= xstrdup(n
->string
);
376 if (NULL
== (n
= n
->next
)) {
377 m
->meta
.vol
= xstrdup("local");
378 return(post_prol(m
));
381 /* Handles: `.Dt TITLE SEC'
382 * --> title = TITLE, volume = SEC is msec ?
383 * format(msec) : SEC,
384 * msec = SEC is msec ? atoi(msec) : 0,
388 cp
= mdoc_a2msec(n
->string
);
390 m
->meta
.vol
= xstrdup(cp
);
392 lval
= strtol(n
->string
, &ep
, 10);
393 if (n
->string
[0] != '\0' && *ep
== '\0')
394 m
->meta
.msec
= (int)lval
;
396 m
->meta
.vol
= xstrdup(n
->string
);
398 if (NULL
== (n
= n
->next
))
399 return(post_prol(m
));
401 /* Handles: `.Dt TITLE SEC VOL'
402 * --> title = TITLE, volume = VOL is vol ?
404 * VOL is arch ? format(arch) :
408 cp
= mdoc_a2vol(n
->string
);
411 m
->meta
.vol
= xstrdup(cp
);
414 cp
= mdoc_a2arch(n
->string
);
417 m
->meta
.vol
= xstrdup(n
->string
);
419 m
->meta
.arch
= xstrdup(cp
);
422 /* Ignore any subsequent parameters... */
424 return(post_prol(m
));
432 struct utsname utsname
;
437 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
440 if (-1 == uname(&utsname
))
441 return(mdoc_err(m
, "utsname"));
442 (void)xstrlcpy(buf
, utsname
.sysname
, sizeof(buf
));
443 (void)xstrlcat(buf
, " ", sizeof(buf
));
444 (void)xstrlcat(buf
, utsname
.release
, sizeof(buf
));
447 m
->meta
.os
= xstrdup(buf
);
448 m
->lastnamed
= m
->lastsec
= SEC_BODY
;
450 return(post_prol(m
));
455 * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
456 * Uses the first head macro.
459 post_bl_tagwidth(struct mdoc
*m
)
466 * Use the text width, if a text node, or the default macro
470 if ((n
= m
->last
->body
->child
)) {
471 assert(MDOC_BLOCK
== n
->type
);
472 assert(MDOC_It
== n
->tok
);
476 sz
= 10; /* Default size. */
479 if (MDOC_TEXT
!= n
->type
) {
480 if (0 == (sz
= (int)mdoc_macro2len(n
->tok
)))
481 if ( ! mwarn(m
, WNOWIDTH
))
484 sz
= (int)strlen(n
->string
) + 1;
487 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
490 * We have to dynamically add this to the macro's argument list.
491 * We're guaranteed that a MDOC_Width doesn't already exist.
497 sz
= (int)(n
->args
->argc
)++;
498 n
->args
->argv
= xrealloc(n
->args
->argv
,
499 n
->args
->argc
* sizeof(struct mdoc_argv
));
501 n
->args
->argv
[sz
].arg
= MDOC_Width
;
502 n
->args
->argv
[sz
].line
= m
->last
->line
;
503 n
->args
->argv
[sz
].pos
= m
->last
->pos
;
504 n
->args
->argv
[sz
].sz
= 1;
505 n
->args
->argv
[sz
].value
= calloc(1, sizeof(char *));
506 if (NULL
== n
->args
->argv
[sz
].value
)
508 n
->args
->argv
[sz
].value
[0] = xstrdup(buf
);
515 post_bl_width(struct mdoc
*m
)
522 if (NULL
== m
->last
->args
)
525 for (i
= 0; i
< (int)m
->last
->args
->argc
; i
++)
526 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
529 if (i
== (int)m
->last
->args
->argc
)
532 p
= m
->last
->args
->argv
[i
].value
[0];
535 * If the value to -width is a macro, then we re-write it to be
536 * the macro's width as set in share/tmac/mdoc/doc-common.
539 if (xstrcmp(p
, "Ds"))
541 else if (MDOC_MAX
== (tok
= mdoc_tokhash_find(m
->htab
, p
)))
543 else if (0 == (width
= mdoc_macro2len(tok
)))
544 return(mwarn(m
, WNOWIDTH
));
546 /* The value already exists: free and reallocate it. */
548 (void)snprintf(buf
, sizeof(buf
), "%zun", width
);
550 free(m
->last
->args
->argv
[i
].value
[0]);
551 m
->last
->args
->argv
[i
].value
[0] = xstrdup(buf
);
562 if (MDOC_BLOCK
!= m
->last
->type
)
566 * These are fairly complicated, so we've broken them into two
567 * functions. post_bl_tagwidth() is called when a -tag is
568 * specified, but no -width (it must be guessed). The second
569 * when a -width is specified (macro indicators must be
570 * rewritten into real lengths).
573 len
= (int)(m
->last
->args
? m
->last
->args
->argc
: 0);
575 for (r
= i
= 0; i
< len
; i
++) {
576 if (MDOC_Tag
== m
->last
->args
->argv
[i
].arg
)
578 if (MDOC_Width
== m
->last
->args
->argv
[i
].arg
)
582 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
583 if ( ! post_bl_tagwidth(m
))
585 } else if (r
& (1 << 1))
586 if ( ! post_bl_width(m
))
602 m
->next
= MDOC_NEXT_CHILD
;
603 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
604 m
->last
->pos
, "file"))
606 m
->next
= MDOC_NEXT_SIBLING
;
607 if ( ! mdoc_word_alloc(m
, m
->last
->line
,
608 m
->last
->pos
, "..."))
612 m
->next
= MDOC_NEXT_SIBLING
;
622 (void)xstrlcpys(buf
, m
->last
->child
, sizeof(buf
));
624 if (0 == (m
->meta
.date
= mdoc_atotime(buf
))) {
625 if ( ! mwarn(m
, WBADDATE
))
627 m
->meta
.date
= time(NULL
);
630 return(post_prol(m
));
640 * The end document shouldn't have the prologue macros as part
641 * of the syntax tree (they encompass only meta-data).
644 if (m
->last
->parent
->child
== m
->last
)
645 m
->last
->parent
->child
= m
->last
->prev
;
647 m
->last
->prev
->next
= NULL
;
650 assert(NULL
== m
->last
->next
);
653 m
->last
= m
->last
->prev
;
654 m
->next
= MDOC_NEXT_SIBLING
;
656 m
->last
= m
->last
->parent
;
657 m
->next
= MDOC_NEXT_CHILD
;
660 mdoc_node_freelist(n
);
669 if (MDOC_BODY
!= n
->type
)
671 m
->flags
|= MDOC_LITERAL
;
681 if (MDOC_BODY
!= n
->type
)
685 * We ONLY enter a literal context if `Bd -literal' or `Bd
691 for (i
= 0; i
< (int)n
->args
->argc
; i
++)
692 if (MDOC_Literal
== n
->args
->argv
[i
].arg
)
694 else if (MDOC_Unfilled
== n
->args
->argv
[i
].arg
)
697 if (i
< (int)n
->args
->argc
)
698 m
->flags
|= MDOC_LITERAL
;
705 post_display(POST_ARGS
)
708 if (MDOC_BODY
== m
->last
->type
)
709 m
->flags
&= ~MDOC_LITERAL
;