]>
git.cameronkatri.com Git - mandoc.git/blob - action.c
1 /* $Id: action.c,v 1.29 2009/03/05 12:08:53 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
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.
28 * Actions are executed on macros after they've been post-validated: in
29 * other words, a macro will not be "acted upon" until all of its
30 * children have been filled in (post-fix order).
34 int (*post
)(struct mdoc
*);
37 /* Per-macro action routines. */
39 static int post_bl(struct mdoc
*);
40 static int post_bl_width(struct mdoc
*);
41 static int post_bl_tagwidth(struct mdoc
*);
42 static int post_dd(struct mdoc
*);
43 static int post_dt(struct mdoc
*);
44 static int post_ex(struct mdoc
*);
45 static int post_nm(struct mdoc
*);
46 static int post_os(struct mdoc
*);
47 static int post_sh(struct mdoc
*);
49 static int post_prologue(struct mdoc
*);
51 /* Array of macro action routines. */
53 const struct actions mdoc_actions
[MDOC_MAX
] = {
164 post_ex(struct mdoc
*mdoc
)
168 * If `.Ex -std' is invoked without an argument, fill it in with
169 * our name (if it's been set).
172 if (0 == mdoc
->last
->data
.elem
.argc
)
175 assert(1 == mdoc
->last
->data
.elem
.argc
);
176 if (1 == mdoc
->last
->data
.elem
.argv
[0].sz
)
178 assert(0 == mdoc
->last
->data
.elem
.argv
[0].sz
);
180 if (NULL
== mdoc
->meta
.name
)
181 return(mdoc_err(mdoc
, "default name not yet set"));
183 mdoc_msg(mdoc
, "writing %s argument: %s",
184 mdoc_argnames
[MDOC_Std
], mdoc
->meta
.name
);
186 mdoc
->last
->data
.elem
.argv
[0].sz
= 1;
187 mdoc
->last
->data
.elem
.argv
[0].value
= xcalloc(1, sizeof(char *));
188 mdoc
->last
->data
.elem
.argv
[0].value
[0] = xstrdup(mdoc
->meta
.name
);
194 post_nm(struct mdoc
*mdoc
)
198 assert(MDOC_ELEM
== mdoc
->last
->type
);
199 assert(MDOC_Nm
== mdoc
->last
->tok
);
202 * The `Nm' macro sets the document's name when used the first
203 * time with an argument. Subsequent calls without a value will
204 * result in the name value being used.
210 if (xstrlcats(buf
, mdoc
->last
->child
, 64)) {
211 mdoc
->meta
.name
= xstrdup(buf
);
215 return(mdoc_err(mdoc
, "macro parameters too long"));
220 post_sh(struct mdoc
*mdoc
)
226 * We keep track of the current section /and/ the "named"
227 * section, which is one of the conventional ones, in order to
231 if (MDOC_HEAD
!= mdoc
->last
->type
)
233 if (xstrlcats(buf
, mdoc
->last
->child
, 64)) {
234 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
235 mdoc
->lastnamed
= sec
;
238 return(mdoc_err(mdoc
, "parameters too long"));
240 switch (mdoc
->lastsec
) {
241 case (SEC_RETURN_VALUES
):
244 switch (mdoc
->meta
.msec
) {
252 return(mdoc_warn(mdoc
, WARN_COMPAT
,
253 "inappropriate section for "
265 post_dt(struct mdoc
*mdoc
)
272 * Prologue title must be parsed into document meta-data.
275 assert(MDOC_ELEM
== mdoc
->last
->type
);
276 assert(MDOC_Dt
== mdoc
->last
->tok
);
278 assert(NULL
== mdoc
->meta
.title
);
281 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++) {
282 assert(MDOC_TEXT
== n
->type
);
283 p
= n
->data
.text
.string
;
287 mdoc
->meta
.title
= xstrdup(p
);
290 mdoc
->meta
.msec
= mdoc_atomsec(p
);
291 if (MSEC_DEFAULT
!= mdoc
->meta
.msec
)
293 return(mdoc_nerr(mdoc
, n
,
294 "invalid parameter syntax"));
296 mdoc
->meta
.vol
= mdoc_atovol(p
);
297 if (VOL_DEFAULT
!= mdoc
->meta
.vol
)
299 mdoc
->meta
.arch
= mdoc_atoarch(p
);
300 if (ARCH_DEFAULT
!= mdoc
->meta
.arch
)
302 return(mdoc_nerr(mdoc
, n
,
303 "invalid parameter syntax"));
305 return(mdoc_nerr(mdoc
, n
,
306 "too many parameters"));
310 if (NULL
== mdoc
->meta
.title
)
311 mdoc
->meta
.title
= xstrdup("UNTITLED");
313 mdoc_msg(mdoc
, "title: %s", mdoc
->meta
.title
);
315 return(post_prologue(mdoc
));
320 post_os(struct mdoc
*mdoc
)
325 * Prologue operating system must be parsed into document
329 assert(MDOC_ELEM
== mdoc
->last
->type
);
330 assert(MDOC_Os
== mdoc
->last
->tok
);
331 assert(NULL
== mdoc
->meta
.os
);
333 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
334 return(mdoc_err(mdoc
, "macro parameters too long"));
336 mdoc
->meta
.os
= xstrdup(buf
[0] ? buf
: "LOCAL");
337 mdoc
->lastnamed
= SEC_BODY
;
339 return(post_prologue(mdoc
));
344 post_bl_tagwidth(struct mdoc
*mdoc
)
347 struct mdoc_block
*b
;
352 * If -tag has been specified and -width has not been, then try
353 * to intuit our width from the first body element.
356 b
= &mdoc
->last
->data
.block
;
358 if (NULL
== (n
= b
->body
->child
))
360 assert(MDOC_It
== n
->tok
);
363 * Use the text width, if a text node, or the default macro
367 if ((n
= n
->data
.block
.head
->child
)) {
368 if (MDOC_TEXT
!= n
->type
) {
369 if (0 == (sz
= mdoc_macro2len(n
->tok
)))
372 sz
= (int)strlen(n
->data
.text
.string
) + 1;
377 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
378 "cannot determine default %s",
379 mdoc_argnames
[MDOC_Width
]))
384 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
387 * We have to dynamically add this to the macro's argument list.
388 * We're guaranteed that a MDOC_Width doesn't already exist.
392 b
->argv
= xrealloc(b
->argv
, b
->argc
* sizeof(struct mdoc_arg
));
394 b
->argv
[b
->argc
- 1].arg
= MDOC_Width
;
395 b
->argv
[b
->argc
- 1].line
= mdoc
->last
->line
;
396 b
->argv
[b
->argc
- 1].pos
= mdoc
->last
->pos
;
397 b
->argv
[b
->argc
- 1].sz
= 1;
398 b
->argv
[b
->argc
- 1].value
= xcalloc(1, sizeof(char *));
399 b
->argv
[b
->argc
- 1].value
[0] = xstrdup(buf
);
401 mdoc_msg(mdoc
, "adding %s argument: %dn",
402 mdoc_argnames
[MDOC_Width
], sz
);
409 post_bl_width(struct mdoc
*mdoc
)
416 for (i
= 0; i
< (int)mdoc
->last
->data
.block
.argc
; i
++)
417 if (MDOC_Width
== mdoc
->last
->data
.block
.argv
[i
].arg
)
420 assert(i
< (int)mdoc
->last
->data
.block
.argc
);
421 assert(1 == mdoc
->last
->data
.block
.argv
[i
].sz
);
422 p
= &mdoc
->last
->data
.block
.argv
[i
].value
[0];
425 * If the value to -width is a macro, then we re-write it to be
426 * the macro's width as set in share/tmac/mdoc/doc-common.
429 if (xstrcmp(*p
, "Ds"))
431 else if (MDOC_MAX
== (tok
= mdoc_find(mdoc
, *p
)))
433 else if (0 == (width
= mdoc_macro2len(tok
)))
434 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
435 "%s macro has no length",
436 mdoc_argnames
[MDOC_Width
]));
438 mdoc_msg(mdoc
, "re-writing %s argument: %s -> %zun",
439 mdoc_argnames
[MDOC_Width
], *p
, width
);
441 /* The value already exists: free and reallocate it. */
443 (void)snprintf(buf
, sizeof(buf
), "%zun", width
);
453 post_bl(struct mdoc
*mdoc
)
457 if (MDOC_BLOCK
!= mdoc
->last
->type
)
461 * These are fairly complicated, so we've broken them into two
462 * functions. post_bl_tagwidth() is called when a -tag is
463 * specified, but no -width (it must be guessed). The second
464 * when a -width is specified (macro indicators must be
465 * rewritten into real lengths).
468 for (r
= i
= 0; i
< (int)mdoc
->last
->data
.block
.argc
; i
++) {
469 if (MDOC_Tag
== mdoc
->last
->data
.block
.argv
[i
].arg
)
471 if (MDOC_Width
== mdoc
->last
->data
.block
.argv
[i
].arg
)
475 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
476 if ( ! post_bl_tagwidth(mdoc
))
478 } else if (r
& (1 << 1))
479 if ( ! post_bl_width(mdoc
))
487 post_dd(struct mdoc
*mdoc
)
492 * Prologue date must be parsed into document meta-data. We
493 * accept multiple kinds of dates, described mostly in
497 assert(MDOC_ELEM
== mdoc
->last
->type
);
498 assert(MDOC_Dd
== mdoc
->last
->tok
);
500 assert(0 == mdoc
->meta
.date
);
502 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
503 return(mdoc_err(mdoc
, "macro parameters too long"));
504 if (0 == (mdoc
->meta
.date
= mdoc_atotime(buf
)))
505 return(mdoc_err(mdoc
, "invalid parameter syntax"));
507 mdoc_msg(mdoc
, "date: %u", mdoc
->meta
.date
);
509 return(post_prologue(mdoc
));
514 post_prologue(struct mdoc
*mdoc
)
519 * The end document shouldn't have the prologue macros as part
520 * of the syntax tree (they encompass only meta-data).
523 if (mdoc
->last
->parent
->child
== mdoc
->last
)
524 mdoc
->last
->parent
->child
= mdoc
->last
->prev
;
525 if (mdoc
->last
->prev
)
526 mdoc
->last
->prev
->next
= NULL
;
529 assert(NULL
== mdoc
->last
->next
);
531 if (mdoc
->last
->prev
) {
532 mdoc
->last
= mdoc
->last
->prev
;
533 mdoc
->next
= MDOC_NEXT_SIBLING
;
535 mdoc
->last
= mdoc
->last
->parent
;
536 mdoc
->next
= MDOC_NEXT_CHILD
;
539 mdoc_node_freelist(n
);
545 mdoc_action_post(struct mdoc
*mdoc
)
548 if (MDOC_ACTED
& mdoc
->last
->flags
)
550 mdoc
->last
->flags
|= MDOC_ACTED
;
552 if (MDOC_TEXT
== mdoc
->last
->type
)
554 if (MDOC_ROOT
== mdoc
->last
->type
)
556 if (NULL
== mdoc_actions
[mdoc
->last
->tok
].post
)
558 return((*mdoc_actions
[mdoc
->last
->tok
].post
)(mdoc
));