]>
git.cameronkatri.com Git - mandoc.git/blob - action.c
74afb9fa08f7125f1971cd8ae2a080c454ac75a3
1 /* $Id: action.c,v 1.27 2009/02/28 21:50:01 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_sh(struct mdoc
*);
43 static int post_os(struct mdoc
*);
44 static int post_dt(struct mdoc
*);
45 static int post_dd(struct mdoc
*);
46 static int post_nm(struct mdoc
*);
48 static int post_prologue(struct mdoc
*);
50 /* Array of macro action routines. */
52 const struct actions mdoc_actions
[MDOC_MAX
] = {
163 post_nm(struct mdoc
*mdoc
)
167 assert(MDOC_ELEM
== mdoc
->last
->type
);
168 assert(MDOC_Nm
== mdoc
->last
->tok
);
171 * The `Nm' macro sets the document's name when used the first
172 * time with an argument. Subsequent calls without a value will
173 * result in the name value being used.
179 if (xstrlcats(buf
, mdoc
->last
->child
, 64)) {
180 mdoc
->meta
.name
= xstrdup(buf
);
184 return(mdoc_err(mdoc
, "macro parameters too long"));
189 post_sh(struct mdoc
*mdoc
)
195 * We keep track of the current section /and/ the "named"
196 * section, which is one of the conventional ones, in order to
200 if (MDOC_HEAD
!= mdoc
->last
->type
)
202 if (xstrlcats(buf
, mdoc
->last
->child
, 64)) {
203 if (SEC_CUSTOM
!= (sec
= mdoc_atosec(buf
)))
204 mdoc
->lastnamed
= sec
;
207 return(mdoc_err(mdoc
, "parameters too long"));
209 switch (mdoc
->lastsec
) {
210 case (SEC_RETURN_VALUES
):
213 switch (mdoc
->meta
.msec
) {
221 return(mdoc_warn(mdoc
, WARN_COMPAT
,
222 "inappropriate section for "
234 post_dt(struct mdoc
*mdoc
)
241 * Prologue title must be parsed into document meta-data.
244 assert(MDOC_ELEM
== mdoc
->last
->type
);
245 assert(MDOC_Dt
== mdoc
->last
->tok
);
247 assert(NULL
== mdoc
->meta
.title
);
250 for (i
= 0, n
= mdoc
->last
->child
; n
; n
= n
->next
, i
++) {
251 assert(MDOC_TEXT
== n
->type
);
252 p
= n
->data
.text
.string
;
256 mdoc
->meta
.title
= xstrdup(p
);
259 mdoc
->meta
.msec
= mdoc_atomsec(p
);
260 if (MSEC_DEFAULT
!= mdoc
->meta
.msec
)
262 return(mdoc_nerr(mdoc
, n
,
263 "invalid parameter syntax"));
265 mdoc
->meta
.vol
= mdoc_atovol(p
);
266 if (VOL_DEFAULT
!= mdoc
->meta
.vol
)
268 mdoc
->meta
.arch
= mdoc_atoarch(p
);
269 if (ARCH_DEFAULT
!= mdoc
->meta
.arch
)
271 return(mdoc_nerr(mdoc
, n
,
272 "invalid parameter syntax"));
274 return(mdoc_nerr(mdoc
, n
,
275 "too many parameters"));
279 if (NULL
== mdoc
->meta
.title
)
280 mdoc
->meta
.title
= xstrdup("UNTITLED");
282 mdoc_msg(mdoc
, "title: %s", mdoc
->meta
.title
);
284 return(post_prologue(mdoc
));
289 post_os(struct mdoc
*mdoc
)
294 * Prologue operating system must be parsed into document
298 assert(MDOC_ELEM
== mdoc
->last
->type
);
299 assert(MDOC_Os
== mdoc
->last
->tok
);
300 assert(NULL
== mdoc
->meta
.os
);
302 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
303 return(mdoc_err(mdoc
, "macro parameters too long"));
305 mdoc
->meta
.os
= xstrdup(buf
[0] ? buf
: "LOCAL");
306 mdoc
->lastnamed
= SEC_BODY
;
308 return(post_prologue(mdoc
));
313 post_bl_tagwidth(struct mdoc
*mdoc
)
316 struct mdoc_block
*b
;
321 * If -tag has been specified and -width has not been, then try
322 * to intuit our width from the first body element.
325 b
= &mdoc
->last
->data
.block
;
327 if (NULL
== (n
= b
->body
->child
))
329 assert(MDOC_It
== n
->tok
);
332 * Use the text width, if a text node, or the default macro
336 if ((n
= n
->data
.block
.head
->child
)) {
337 if (MDOC_TEXT
!= n
->type
) {
338 if (0 == (sz
= mdoc_macro2len(n
->tok
)))
341 sz
= (int)strlen(n
->data
.text
.string
) + 1;
346 if ( ! mdoc_warn(mdoc
, WARN_SYNTAX
,
347 "cannot determine default %s",
348 mdoc_argnames
[MDOC_Width
]))
353 (void)snprintf(buf
, sizeof(buf
), "%dn", sz
);
356 * We have to dynamically add this to the macro's argument list.
357 * We're guaranteed that a MDOC_Width doesn't already exist.
361 b
->argv
= xrealloc(b
->argv
, b
->argc
* sizeof(struct mdoc_arg
));
363 b
->argv
[b
->argc
- 1].arg
= MDOC_Width
;
364 b
->argv
[b
->argc
- 1].line
= mdoc
->last
->line
;
365 b
->argv
[b
->argc
- 1].pos
= mdoc
->last
->pos
;
366 b
->argv
[b
->argc
- 1].sz
= 1;
367 b
->argv
[b
->argc
- 1].value
= xcalloc(1, sizeof(char *));
368 b
->argv
[b
->argc
- 1].value
[0] = xstrdup(buf
);
370 mdoc_msg(mdoc
, "adding %s argument: %dn",
371 mdoc_argnames
[MDOC_Width
], sz
);
378 post_bl_width(struct mdoc
*mdoc
)
385 for (i
= 0; i
< (int)mdoc
->last
->data
.block
.argc
; i
++)
386 if (MDOC_Width
== mdoc
->last
->data
.block
.argv
[i
].arg
)
389 assert(i
< (int)mdoc
->last
->data
.block
.argc
);
390 assert(1 == mdoc
->last
->data
.block
.argv
[i
].sz
);
391 p
= &mdoc
->last
->data
.block
.argv
[i
].value
[0];
394 * If the value to -width is a macro, then we re-write it to be
395 * the macro's width as set in share/tmac/mdoc/doc-common.
398 if (xstrcmp(*p
, "Ds")) {
399 if ( ! mdoc_warn(mdoc
, WARN_COMPAT
,
400 "%s argument deprecated",
401 mdoc_argnames
[MDOC_Width
]))
404 } else if (MDOC_MAX
== (tok
= mdoc_find(mdoc
, *p
)))
406 else if (0 == (width
= mdoc_macro2len(tok
)))
407 return(mdoc_warn(mdoc
, WARN_SYNTAX
,
408 "%s macro has no length",
409 mdoc_argnames
[MDOC_Width
]));
411 mdoc_msg(mdoc
, "re-writing %s argument: %s -> %zun",
412 mdoc_argnames
[MDOC_Width
], *p
, width
);
414 /* The value already exists: free and reallocate it. */
416 (void)snprintf(buf
, sizeof(buf
), "%zun", width
);
426 post_bl(struct mdoc
*mdoc
)
430 if (MDOC_BLOCK
!= mdoc
->last
->type
)
434 * These are fairly complicated, so we've broken them into two
435 * functions. post_bl_tagwidth() is called when a -tag is
436 * specified, but no -width (it must be guessed). The second
437 * when a -width is specified (macro indicators must be
438 * rewritten into real lengths).
441 for (r
= i
= 0; i
< (int)mdoc
->last
->data
.block
.argc
; i
++) {
442 if (MDOC_Tag
== mdoc
->last
->data
.block
.argv
[i
].arg
)
444 if (MDOC_Width
== mdoc
->last
->data
.block
.argv
[i
].arg
)
448 if (r
& (1 << 0) && ! (r
& (1 << 1))) {
449 if ( ! post_bl_tagwidth(mdoc
))
451 } else if (r
& (1 << 1))
452 if ( ! post_bl_width(mdoc
))
460 post_dd(struct mdoc
*mdoc
)
465 * Prologue date must be parsed into document meta-data. We
466 * accept multiple kinds of dates, described mostly in
470 assert(MDOC_ELEM
== mdoc
->last
->type
);
471 assert(MDOC_Dd
== mdoc
->last
->tok
);
473 assert(0 == mdoc
->meta
.date
);
475 if ( ! xstrlcats(buf
, mdoc
->last
->child
, 64))
476 return(mdoc_err(mdoc
, "macro parameters too long"));
477 if (0 == (mdoc
->meta
.date
= mdoc_atotime(buf
)))
478 return(mdoc_err(mdoc
, "invalid parameter syntax"));
480 mdoc_msg(mdoc
, "date: %u", mdoc
->meta
.date
);
482 return(post_prologue(mdoc
));
487 post_prologue(struct mdoc
*mdoc
)
492 * The end document shouldn't have the prologue macros as part
493 * of the syntax tree (they encompass only meta-data).
496 if (mdoc
->last
->parent
->child
== mdoc
->last
)
497 mdoc
->last
->parent
->child
= mdoc
->last
->prev
;
498 if (mdoc
->last
->prev
)
499 mdoc
->last
->prev
->next
= NULL
;
502 assert(NULL
== mdoc
->last
->next
);
504 if (mdoc
->last
->prev
) {
505 mdoc
->last
= mdoc
->last
->prev
;
506 mdoc
->next
= MDOC_NEXT_SIBLING
;
508 mdoc
->last
= mdoc
->last
->parent
;
509 mdoc
->next
= MDOC_NEXT_CHILD
;
512 mdoc_node_freelist(n
);
518 mdoc_action_post(struct mdoc
*mdoc
)
521 if (MDOC_ACTED
& mdoc
->last
->flags
)
523 mdoc
->last
->flags
|= MDOC_ACTED
;
525 if (MDOC_TEXT
== mdoc
->last
->type
)
527 if (MDOC_ROOT
== mdoc
->last
->type
)
529 if (NULL
== mdoc_actions
[mdoc
->last
->tok
].post
)
531 return((*mdoc_actions
[mdoc
->last
->tok
].post
)(mdoc
));