version 1.1, 2009/01/05 14:14:04 |
version 1.8, 2009/01/16 11:50:54 |
|
|
*/ |
*/ |
#include <assert.h> |
#include <assert.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <time.h> |
|
|
#include "private.h" |
#include "private.h" |
|
|
typedef int (*a_act)(struct mdoc *, int, int, |
typedef int (*a_pre)(struct mdoc *, struct mdoc_node *); |
int, const char *[], |
typedef int (*a_post)(struct mdoc *); |
int, const struct mdoc_arg *); |
|
|
|
|
|
struct actions { |
struct actions { |
a_act action; |
a_pre pre; |
|
a_post post; |
}; |
}; |
|
|
|
|
|
static int post_sh(struct mdoc *); |
|
static int post_os(struct mdoc *); |
|
static int post_dt(struct mdoc *); |
|
static int post_dd(struct mdoc *); |
|
|
|
|
const struct actions mdoc_actions[MDOC_MAX] = { |
const struct actions mdoc_actions[MDOC_MAX] = { |
{ NULL }, /* \" */ |
{ NULL, NULL }, /* \" */ |
{ NULL }, /* Dd */ |
{ NULL, post_dd }, /* Dd */ |
{ NULL }, /* Dt */ |
{ NULL, post_dt }, /* Dt */ |
{ NULL }, /* Os */ |
{ NULL, post_os }, /* Os */ |
{ NULL }, /* Sh */ |
{ NULL, post_sh }, /* Sh */ |
{ NULL }, /* Ss */ |
{ NULL, NULL }, /* Ss */ |
{ NULL }, /* Pp */ |
{ NULL, NULL }, /* Pp */ |
{ NULL }, /* D1 */ |
{ NULL, NULL }, /* D1 */ |
{ NULL }, /* Dl */ |
{ NULL, NULL }, /* Dl */ |
{ NULL }, /* Bd */ |
{ NULL, NULL }, /* Bd */ |
{ NULL }, /* Ed */ |
{ NULL, NULL }, /* Ed */ |
{ NULL }, /* Bl */ |
{ NULL, NULL }, /* Bl */ |
{ NULL }, /* El */ |
{ NULL, NULL }, /* El */ |
{ NULL }, /* It */ |
{ NULL, NULL }, /* It */ |
{ NULL }, /* Ad */ |
{ NULL, NULL }, /* Ad */ |
{ NULL }, /* An */ |
{ NULL, NULL }, /* An */ |
{ NULL }, /* Ar */ |
{ NULL, NULL }, /* Ar */ |
{ NULL }, /* Cd */ |
{ NULL, NULL }, /* Cd */ |
{ NULL }, /* Cm */ |
{ NULL, NULL }, /* Cm */ |
{ NULL }, /* Dv */ |
{ NULL, NULL }, /* Dv */ |
{ NULL }, /* Er */ |
{ NULL, NULL }, /* Er */ |
{ NULL }, /* Ev */ |
{ NULL, NULL }, /* Ev */ |
{ NULL }, /* Ex */ |
{ NULL, NULL }, /* Ex */ |
{ NULL }, /* Fa */ |
{ NULL, NULL }, /* Fa */ |
{ NULL }, /* Fd */ |
{ NULL, NULL }, /* Fd */ |
{ NULL }, /* Fl */ |
{ NULL, NULL }, /* Fl */ |
{ NULL }, /* Fn */ |
{ NULL, NULL }, /* Fn */ |
{ NULL }, /* Ft */ |
{ NULL, NULL }, /* Ft */ |
{ NULL }, /* Ic */ |
{ NULL, NULL }, /* Ic */ |
{ NULL }, /* In */ |
{ NULL, NULL }, /* In */ |
{ NULL }, /* Li */ |
{ NULL, NULL }, /* Li */ |
{ NULL }, /* Nd */ |
{ NULL, NULL }, /* Nd */ |
{ NULL }, /* Nm */ |
{ NULL, NULL }, /* Nm */ |
{ NULL }, /* Op */ |
{ NULL, NULL }, /* Op */ |
{ NULL }, /* Ot */ |
{ NULL, NULL }, /* Ot */ |
{ NULL }, /* Pa */ |
{ NULL, NULL }, /* Pa */ |
{ NULL }, /* Rv */ |
{ NULL, NULL }, /* Rv */ |
{ NULL }, /* St */ |
{ NULL, NULL }, /* St */ |
{ NULL }, /* Va */ |
{ NULL, NULL }, /* Va */ |
{ NULL }, /* Vt */ |
{ NULL, NULL }, /* Vt */ |
{ NULL }, /* Xr */ |
{ NULL, NULL }, /* Xr */ |
{ NULL }, /* %A */ |
{ NULL, NULL }, /* %A */ |
{ NULL }, /* %B */ |
{ NULL, NULL }, /* %B */ |
{ NULL }, /* %D */ |
{ NULL, NULL }, /* %D */ |
{ NULL }, /* %I */ |
{ NULL, NULL }, /* %I */ |
{ NULL }, /* %J */ |
{ NULL, NULL }, /* %J */ |
{ NULL }, /* %N */ |
{ NULL, NULL }, /* %N */ |
{ NULL }, /* %O */ |
{ NULL, NULL }, /* %O */ |
{ NULL }, /* %P */ |
{ NULL, NULL }, /* %P */ |
{ NULL }, /* %R */ |
{ NULL, NULL }, /* %R */ |
{ NULL }, /* %T */ |
{ NULL, NULL }, /* %T */ |
{ NULL }, /* %V */ |
{ NULL, NULL }, /* %V */ |
{ NULL }, /* Ac */ |
{ NULL, NULL }, /* Ac */ |
{ NULL }, /* Ao */ |
{ NULL, NULL }, /* Ao */ |
{ NULL }, /* Aq */ |
{ NULL, NULL }, /* Aq */ |
{ NULL }, /* At */ |
{ NULL, NULL }, /* At */ |
{ NULL }, /* Bc */ |
{ NULL, NULL }, /* Bc */ |
{ NULL }, /* Bf */ |
{ NULL, NULL }, /* Bf */ |
{ NULL }, /* Bo */ |
{ NULL, NULL }, /* Bo */ |
{ NULL }, /* Bq */ |
{ NULL, NULL }, /* Bq */ |
{ NULL }, /* Bsx */ |
{ NULL, NULL }, /* Bsx */ |
{ NULL }, /* Bx */ |
{ NULL, NULL }, /* Bx */ |
{ NULL }, /* Db */ |
{ NULL, NULL }, /* Db */ |
{ NULL }, /* Dc */ |
{ NULL, NULL }, /* Dc */ |
{ NULL }, /* Do */ |
{ NULL, NULL }, /* Do */ |
{ NULL }, /* Dq */ |
{ NULL, NULL }, /* Dq */ |
{ NULL }, /* Ec */ |
{ NULL, NULL }, /* Ec */ |
{ NULL }, /* Ef */ |
{ NULL, NULL }, /* Ef */ |
{ NULL }, /* Em */ |
{ NULL, NULL }, /* Em */ |
{ NULL }, /* Eo */ |
{ NULL, NULL }, /* Eo */ |
{ NULL }, /* Fx */ |
{ NULL, NULL }, /* Fx */ |
{ NULL }, /* Ms */ |
{ NULL, NULL }, /* Ms */ |
{ NULL }, /* No */ |
{ NULL, NULL }, /* No */ |
{ NULL }, /* Ns */ |
{ NULL, NULL }, /* Ns */ |
{ NULL }, /* Nx */ |
{ NULL, NULL }, /* Nx */ |
{ NULL }, /* Ox */ |
{ NULL, NULL }, /* Ox */ |
{ NULL }, /* Pc */ |
{ NULL, NULL }, /* Pc */ |
{ NULL }, /* Pf */ |
{ NULL, NULL }, /* Pf */ |
{ NULL }, /* Po */ |
{ NULL, NULL }, /* Po */ |
{ NULL }, /* Pq */ |
{ NULL, NULL }, /* Pq */ |
{ NULL }, /* Qc */ |
{ NULL, NULL }, /* Qc */ |
{ NULL }, /* Ql */ |
{ NULL, NULL }, /* Ql */ |
{ NULL }, /* Qo */ |
{ NULL, NULL }, /* Qo */ |
{ NULL }, /* Qq */ |
{ NULL, NULL }, /* Qq */ |
{ NULL }, /* Re */ |
{ NULL, NULL }, /* Re */ |
{ NULL }, /* Rs */ |
{ NULL, NULL }, /* Rs */ |
{ NULL }, /* Sc */ |
{ NULL, NULL }, /* Sc */ |
{ NULL }, /* So */ |
{ NULL, NULL }, /* So */ |
{ NULL }, /* Sq */ |
{ NULL, NULL }, /* Sq */ |
{ NULL }, /* Sm */ |
{ NULL, NULL }, /* Sm */ |
{ NULL }, /* Sx */ |
{ NULL, NULL }, /* Sx */ |
{ NULL }, /* Sy */ |
{ NULL, NULL }, /* Sy */ |
{ NULL }, /* Tn */ |
{ NULL, NULL }, /* Tn */ |
{ NULL }, /* Ux */ |
{ NULL, NULL }, /* Ux */ |
{ NULL }, /* Xc */ |
{ NULL, NULL }, /* Xc */ |
{ NULL }, /* Xo */ |
{ NULL, NULL }, /* Xo */ |
{ NULL }, /* Fo */ |
{ NULL, NULL }, /* Fo */ |
{ NULL }, /* Fc */ |
{ NULL, NULL }, /* Fc */ |
{ NULL }, /* Oo */ |
{ NULL, NULL }, /* Oo */ |
{ NULL }, /* Oc */ |
{ NULL, NULL }, /* Oc */ |
{ NULL }, /* Bk */ |
{ NULL, NULL }, /* Bk */ |
{ NULL }, /* Ek */ |
{ NULL, NULL }, /* Ek */ |
{ NULL }, /* Bt */ |
{ NULL, NULL }, /* Bt */ |
{ NULL }, /* Hf */ |
{ NULL, NULL }, /* Hf */ |
{ NULL }, /* Fr */ |
{ NULL, NULL }, /* Fr */ |
{ NULL }, /* Ud */ |
{ NULL, NULL }, /* Ud */ |
}; |
}; |
|
|
|
|
int |
static int |
mdoc_action(struct mdoc *mdoc, int tok, int pos) |
post_sh(struct mdoc *mdoc) |
{ |
{ |
|
enum mdoc_sec sec; |
|
int i; |
|
struct mdoc_node *n; |
|
char *args[MDOC_LINEARG_MAX]; |
|
|
|
if (MDOC_HEAD != mdoc->last->type) |
|
return(1); |
|
|
|
assert(MDOC_Sh == mdoc->last->tok); |
|
|
|
n = mdoc->last->child; |
|
assert(n); |
|
|
|
for (i = 0; n && i < MDOC_LINEARG_MAX; n = n->next, i++) { |
|
assert(MDOC_TEXT == n->type); |
|
assert(NULL == n->child); |
|
assert(n->data.text.string); |
|
args[i] = n->data.text.string; |
|
} |
|
|
|
sec = mdoc_atosec((size_t)i, (const char **)args); |
|
if (SEC_CUSTOM != sec) |
|
mdoc->sec_lastn = sec; |
|
mdoc->sec_last = sec; |
|
|
return(1); |
return(1); |
} |
} |
|
|
#if 0 |
|
/* Post-processing. */ |
static int |
switch (tok) { |
post_dt(struct mdoc *mdoc) |
case (MDOC_Sh): |
{ |
sec = mdoc_atosec((size_t)sz, _CC(args)); |
int i; |
if (SEC_CUSTOM != sec) |
char *p; |
mdoc->sec_lastn = sec; |
size_t sz; |
mdoc->sec_last = sec; |
struct mdoc_node *n; |
break; |
|
default: |
assert(MDOC_ELEM == mdoc->last->type); |
break; |
assert(MDOC_Dt == mdoc->last->tok); |
|
assert(0 == mdoc->meta.title[0]); |
|
|
|
sz = META_TITLE_SZ; |
|
(void)xstrlcpy(mdoc->meta.title, "UNTITLED", sz); |
|
|
|
for (i = 0, n = mdoc->last->child; n; n = n->next, i++) { |
|
assert(MDOC_TEXT == n->type); |
|
p = n->data.text.string; |
|
|
|
switch (i) { |
|
case (0): |
|
if (xstrlcpy(mdoc->meta.title, p, sz)) |
|
break; |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual title parameter")); |
|
case (1): |
|
mdoc->meta.msec = mdoc_atomsec(p); |
|
if (MSEC_DEFAULT != mdoc->meta.msec) |
|
break; |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual section parameter")); |
|
case (2): |
|
mdoc->meta.vol = mdoc_atovol(p); |
|
if (VOL_DEFAULT != mdoc->meta.vol) |
|
break; |
|
mdoc->meta.arch = mdoc_atoarch(p); |
|
if (ARCH_DEFAULT != mdoc->meta.arch) |
|
break; |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual volume parameter")); |
|
default: |
|
return(mdoc_nerr(mdoc, n, "too many parameters")); |
|
} |
} |
} |
#endif |
|
|
|
|
mdoc_msg(mdoc, "parsed title: %s", mdoc->meta.title); |
|
/* TODO: print vol2a functions. */ |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
post_os(struct mdoc *mdoc) |
|
{ |
|
char *p; |
|
size_t sz; |
|
struct mdoc_node *n; |
|
|
|
assert(MDOC_ELEM == mdoc->last->type); |
|
assert(MDOC_Os == mdoc->last->tok); |
|
assert(0 == mdoc->meta.os[0]); |
|
|
|
sz = META_OS_SZ; |
|
|
|
for (n = mdoc->last->child; n; n = n->next) { |
|
assert(MDOC_TEXT == n->type); |
|
p = n->data.text.string; |
|
|
|
if ( ! xstrlcat(mdoc->meta.os, p, sz)) |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual system parameter")); |
|
if ( ! xstrlcat(mdoc->meta.os, " ", sz)) |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual system parameter")); |
|
} |
|
|
|
if (0 == mdoc->meta.os[0]) |
|
(void)xstrlcpy(mdoc->meta.os, "LOCAL", sz); |
|
|
|
mdoc_msg(mdoc, "parsed operating system: %s", mdoc->meta.os); |
|
mdoc->sec_lastn = mdoc->sec_last = SEC_BODY; |
|
return(1); |
|
} |
|
|
|
|
|
static int |
|
post_dd(struct mdoc *mdoc) |
|
{ |
|
char date[64]; |
|
size_t sz; |
|
char *p; |
|
struct mdoc_node *n; |
|
|
|
assert(MDOC_ELEM == mdoc->last->type); |
|
assert(MDOC_Dd == mdoc->last->tok); |
|
|
|
n = mdoc->last->child; |
|
assert(0 == mdoc->meta.date); |
|
date[0] = 0; |
|
|
|
sz = 64; |
|
|
|
for ( ; 0 == mdoc->meta.date && n; n = n->next) { |
|
assert(MDOC_TEXT == n->type); |
|
p = n->data.text.string; |
|
|
|
if (xstrcmp(p, "$Mdocdate$")) { |
|
mdoc->meta.date = time(NULL); |
|
continue; |
|
} else if (xstrcmp(p, "$")) { |
|
mdoc->meta.date = mdoc_atotime(date); |
|
continue; |
|
} else if (xstrcmp(p, "$Mdocdate:")) |
|
continue; |
|
|
|
if ( ! xstrlcat(date, n->data.text.string, sz)) |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual date parameter")); |
|
if ( ! xstrlcat(date, " ", sz)) |
|
return(mdoc_nerr(mdoc, n, "badly-formed manual date parameter")); |
|
} |
|
|
|
if (mdoc->meta.date && NULL == n) { |
|
mdoc_msg(mdoc, "parsed time: %u since epoch", |
|
mdoc->meta.date); |
|
return(1); |
|
} |
|
|
|
return(mdoc_err(mdoc, "badly-formed manual date")); |
|
} |
|
|
|
|
|
int |
|
mdoc_action_pre(struct mdoc *mdoc, struct mdoc_node *node) |
|
{ |
|
|
|
return(1); |
|
} |
|
|
|
|
|
int |
|
mdoc_action_post(struct mdoc *mdoc) |
|
{ |
|
|
|
if (MDOC_TEXT == mdoc->last->type) |
|
return(1); |
|
if (MDOC_ROOT == mdoc->last->type) |
|
return(1); |
|
|
|
if (NULL == mdoc_actions[mdoc->last->tok].post) |
|
return(1); |
|
/* TODO: MDOC_Nm... ? */ |
|
return((*mdoc_actions[mdoc->last->tok].post)(mdoc)); |
|
} |