version 1.2, 2009/03/23 14:31:58 |
version 1.17, 2009/06/18 10:53:58 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the |
* purpose with or without fee is hereby granted, provided that the above |
* above copyright notice and this permission notice appear in all |
* copyright notice and this permission notice appear in all copies. |
* copies. |
|
* |
* |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
|
*/ |
*/ |
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <stdio.h> |
|
#include <string.h> |
#include <string.h> |
|
|
#include "libman.h" |
#include "libman.h" |
|
|
static int in_line_eoln(MACRO_PROT_ARGS); |
#define FL_NLINE (1 << 0) |
|
#define FL_TLINE (1 << 1) |
|
|
const struct man_macro __man_macros[MAN_MAX] = { |
static int man_args(struct man *, int, |
{ in_line_eoln, 0 }, /* MAN___ */ |
int *, char *, char **); |
{ in_line_eoln, 0 }, /* MAN_TH */ |
|
{ in_line_eoln, 0 }, /* MAN_SH */ |
static int man_flags[MAN_MAX] = { |
{ in_line_eoln, 0 }, /* MAN_SS */ |
0, /* br */ |
{ in_line_eoln, 0 }, /* MAN_TP */ |
0, /* TH */ |
{ in_line_eoln, 0 }, /* MAN_LP */ |
0, /* SH */ |
{ in_line_eoln, 0 }, /* MAN_PP */ |
0, /* SS */ |
{ in_line_eoln, 0 }, /* MAN_P */ |
FL_TLINE, /* TP */ |
{ in_line_eoln, 0 }, /* MAN_IP */ |
0, /* LP */ |
{ in_line_eoln, 0 }, /* MAN_HP */ |
0, /* PP */ |
{ in_line_eoln, 0 }, /* MAN_SM */ |
0, /* P */ |
{ in_line_eoln, 0 }, /* MAN_SB */ |
0, /* IP */ |
{ in_line_eoln, 0 }, /* MAN_BI */ |
0, /* HP */ |
{ in_line_eoln, 0 }, /* MAN_IB */ |
FL_NLINE, /* SM */ |
{ in_line_eoln, 0 }, /* MAN_BR */ |
FL_NLINE, /* SB */ |
{ in_line_eoln, 0 }, /* MAN_RB */ |
FL_NLINE, /* BI */ |
{ in_line_eoln, 0 }, /* MAN_R */ |
FL_NLINE, /* IB */ |
{ in_line_eoln, 0 }, /* MAN_B */ |
FL_NLINE, /* BR */ |
{ in_line_eoln, 0 }, /* MAN_I */ |
FL_NLINE, /* RB */ |
|
FL_NLINE, /* R */ |
|
FL_NLINE, /* B */ |
|
FL_NLINE, /* I */ |
|
FL_NLINE, /* IR */ |
|
FL_NLINE, /* RI */ |
|
0, /* na */ |
|
FL_NLINE, /* i */ |
}; |
}; |
|
|
const struct man_macro * const man_macros = __man_macros; |
int |
|
man_macro(struct man *man, int tok, int line, |
|
int ppos, int *pos, char *buf) |
/* |
|
* In-line macro that spans an entire line. May be callable, but has no |
|
* subsequent parsed arguments. |
|
*/ |
|
static int |
|
in_line_eoln(MACRO_PROT_ARGS) |
|
{ |
{ |
#if 0 |
int w, la; |
int c, w, la; |
char *p; |
char *p; |
struct man_node *n; |
|
|
if ( ! man_elem_alloc(man, line, ppos, tok, arg)) |
if ( ! man_elem_alloc(man, line, ppos, tok)) |
return(0); |
return(0); |
man->next = MDOC_NEXT_SIBLING; |
n = man->last; |
|
man->next = MAN_NEXT_CHILD; |
|
|
for (;;) { |
for (;;) { |
la = *pos; |
la = *pos; |
w = man_args(man, line, pos, buf, tok, &p); |
w = man_args(man, line, pos, buf, &p); |
|
|
if (ARGS_ERROR == w) |
if (-1 == w) |
return(0); |
return(0); |
if (ARGS_EOLN == w) |
if (0 == w) |
break; |
break; |
|
|
c = ARGS_QWORD == w ? MAN_MAX : |
if ( ! man_word_alloc(man, line, la, p)) |
lookup(man, line, la, tok, p); |
return(0); |
|
man->next = MAN_NEXT_SIBLING; |
|
} |
|
|
if (MDOC_MAX != c && -1 != c) { |
if (n == man->last && (FL_NLINE & man_flags[tok])) { |
if ( ! rew_elem(mdoc, tok)) |
if (MAN_NLINE & man->flags) |
return(0); |
return(man_perr(man, line, ppos, WLNSCOPE)); |
return(mdoc_macro(mdoc, c, line, la, pos, buf)); |
man->flags |= MAN_NLINE; |
} else if (-1 == c) |
return(1); |
|
} |
|
|
|
if (FL_TLINE & man_flags[tok]) { |
|
if (MAN_NLINE & man->flags) |
|
return(man_perr(man, line, ppos, WLNSCOPE)); |
|
man->flags |= MAN_NLINE; |
|
return(1); |
|
} |
|
|
|
/* |
|
* Note that when TH is pruned, we'll be back at the root, so |
|
* make sure that we don't clobber as its sibling. |
|
*/ |
|
|
|
for ( ; man->last; man->last = man->last->parent) { |
|
if (man->last == n) |
|
break; |
|
if (man->last->type == MAN_ROOT) |
|
break; |
|
if ( ! man_valid_post(man)) |
return(0); |
return(0); |
|
if ( ! man_action_post(man)) |
|
return(0); |
|
} |
|
|
if ( ! mdoc_word_alloc(mdoc, line, la, p)) |
assert(man->last); |
|
|
|
/* |
|
* Same here regarding whether we're back at the root. |
|
*/ |
|
|
|
if (man->last->type != MAN_ROOT && ! man_valid_post(man)) |
|
return(0); |
|
if (man->last->type != MAN_ROOT && ! man_action_post(man)) |
|
return(0); |
|
if (man->last->type != MAN_ROOT) |
|
man->next = MAN_NEXT_SIBLING; |
|
|
|
return(1); |
|
} |
|
|
|
|
|
int |
|
man_macroend(struct man *m) |
|
{ |
|
|
|
for ( ; m->last && m->last != m->first; |
|
m->last = m->last->parent) { |
|
if ( ! man_valid_post(m)) |
return(0); |
return(0); |
|
if ( ! man_action_post(m)) |
|
return(0); |
} |
} |
|
assert(m->last == m->first); |
|
|
return(rew_elem(mdoc, tok)); |
if ( ! man_valid_post(m)) |
#endif |
return(0); |
|
if ( ! man_action_post(m)) |
|
return(0); |
|
|
return(1); |
return(1); |
} |
} |
|
|
|
|
|
/* ARGSUSED */ |
|
static int |
|
man_args(struct man *m, int line, |
|
int *pos, char *buf, char **v) |
|
{ |
|
|
|
if (0 == buf[*pos]) |
|
return(0); |
|
|
|
/* First parse non-quoted strings. */ |
|
|
|
if ('\"' != buf[*pos]) { |
|
*v = &buf[*pos]; |
|
|
|
while (buf[*pos]) { |
|
if (' ' == buf[*pos]) |
|
if ('\\' != buf[*pos - 1]) |
|
break; |
|
(*pos)++; |
|
} |
|
|
|
if (0 == buf[*pos]) |
|
return(1); |
|
|
|
buf[(*pos)++] = 0; |
|
|
|
if (0 == buf[*pos]) |
|
return(1); |
|
|
|
while (buf[*pos] && ' ' == buf[*pos]) |
|
(*pos)++; |
|
|
|
if (buf[*pos]) |
|
return(1); |
|
|
|
if ( ! man_pwarn(m, line, *pos, WTSPACE)) |
|
return(-1); |
|
|
|
return(1); |
|
} |
|
|
|
/* |
|
* If we're a quoted string (and quoted strings are allowed), |
|
* then parse ahead to the next quote. If none's found, it's an |
|
* error. After, parse to the next word. |
|
*/ |
|
|
|
*v = &buf[++(*pos)]; |
|
|
|
while (buf[*pos] && '\"' != buf[*pos]) |
|
(*pos)++; |
|
|
|
if (0 == buf[*pos]) { |
|
if ( ! man_pwarn(m, line, *pos, WTQUOTE)) |
|
return(-1); |
|
return(1); |
|
} |
|
|
|
buf[(*pos)++] = 0; |
|
if (0 == buf[*pos]) |
|
return(1); |
|
|
|
while (buf[*pos] && ' ' == buf[*pos]) |
|
(*pos)++; |
|
|
|
if (buf[*pos]) |
|
return(1); |
|
|
|
if ( ! man_pwarn(m, line, *pos, WTSPACE)) |
|
return(-1); |
|
return(1); |
|
} |