version 1.76, 2011/01/01 17:38:11 |
version 1.77, 2011/01/04 22:28:17 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
|
|
#include "libmdoc.h" |
#include "libmdoc.h" |
#include "libmandoc.h" |
#include "libmandoc.h" |
|
|
#include "out.h" |
|
#include "term.h" |
|
#include "tbl.h" |
|
|
|
const char *const __mdoc_macronames[MDOC_MAX] = { |
const char *const __mdoc_macronames[MDOC_MAX] = { |
"Ap", "Dd", "Dt", "Os", |
"Ap", "Dd", "Dt", "Os", |
"Sh", "Ss", "Pp", "D1", |
"Sh", "Ss", "Pp", "D1", |
|
|
/* LINTED */ |
/* LINTED */ |
"Dx", "%Q", "br", "sp", |
"Dx", "%Q", "br", "sp", |
/* LINTED */ |
/* LINTED */ |
"%U", "Ta", "TS", "TE" |
"%U", "Ta" |
}; |
}; |
|
|
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
const char *const __mdoc_argnames[MDOC_ARG_MAX] = { |
|
|
struct mdoc_node *); |
struct mdoc_node *); |
static int mdoc_ptext(struct mdoc *, int, char *, int); |
static int mdoc_ptext(struct mdoc *, int, char *, int); |
static int mdoc_pmacro(struct mdoc *, int, char *, int); |
static int mdoc_pmacro(struct mdoc *, int, char *, int); |
|
static int mdoc_span_alloc(struct mdoc *, |
|
const struct tbl_span *); |
|
|
|
|
const struct mdoc_node * |
const struct mdoc_node * |
|
|
return(0); |
return(0); |
} |
} |
|
|
|
int |
|
mdoc_addspan(struct mdoc *m, const struct tbl_span *sp) |
|
{ |
|
|
|
if (MDOC_HALT & m->flags) |
|
return(0); |
|
|
|
/* No text before an initial macro. */ |
|
|
|
if (SEC_NONE == m->lastnamed) { |
|
/* FIXME: grab from span. */ |
|
mdoc_pmsg(m, 0, 0, MANDOCERR_NOTEXT); |
|
return(1); |
|
} |
|
|
|
return(mdoc_span_alloc(m, sp)); |
|
} |
|
|
|
|
/* |
/* |
* Main parse routine. Parses a single line -- really just hands off to |
* Main parse routine. Parses a single line -- really just hands off to |
* the macro (mdoc_pmacro()) or text parser (mdoc_ptext()). |
* the macro (mdoc_pmacro()) or text parser (mdoc_ptext()). |
|
|
int |
int |
mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs) |
mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs) |
{ |
{ |
struct mdoc_node *n; |
|
|
|
if (MDOC_HALT & m->flags) |
if (MDOC_HALT & m->flags) |
return(0); |
return(0); |
|
|
m->flags |= MDOC_NEWLINE; |
m->flags |= MDOC_NEWLINE; |
|
|
n = m->last; |
|
|
|
if (n && MDOC_TS == n->tok && MDOC_BODY == n->type && |
|
strncmp(buf+offs, ".TE", 3)) { |
|
n = n->parent; |
|
if ( ! tbl_read(n->norm->TS, "mdoc tbl parser", |
|
ln, buf+offs, strlen(buf+offs))) |
|
mdoc_nmsg(m, n, MANDOCERR_TBL); |
|
return(1); |
|
} |
|
|
|
/* |
/* |
* Let the roff nS register switch SYNOPSIS mode early, |
* Let the roff nS register switch SYNOPSIS mode early, |
* such that the parser knows at all times |
* such that the parser knows at all times |
|
|
mdoc->last = p; |
mdoc->last = p; |
|
|
switch (p->type) { |
switch (p->type) { |
|
case (MDOC_TBL): |
|
/* FALLTHROUGH */ |
case (MDOC_TEXT): |
case (MDOC_TEXT): |
if ( ! mdoc_valid_post(mdoc)) |
if ( ! mdoc_valid_post(mdoc)) |
return(0); |
return(0); |
|
|
case (MDOC_Bl): |
case (MDOC_Bl): |
/* FALLTHROUGH */ |
/* FALLTHROUGH */ |
case (MDOC_Rs): |
case (MDOC_Rs): |
/* FALLTHROUGH */ |
|
case (MDOC_TS): |
|
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
p->norm = mandoc_calloc(1, sizeof(union mdoc_data)); |
break; |
break; |
default: |
default: |
|
|
return(1); |
return(1); |
} |
} |
|
|
|
static int |
|
mdoc_span_alloc(struct mdoc *m, const struct tbl_span *sp) |
|
{ |
|
struct mdoc_node *n; |
|
|
|
/* FIXME: grab from tbl_span. */ |
|
n = node_alloc(m, 0, 0, MDOC_MAX, MDOC_TBL); |
|
n->span = sp; |
|
|
|
if ( ! node_append(m, n)) |
|
return(0); |
|
|
|
m->next = MDOC_NEXT_SIBLING; |
|
return(1); |
|
} |
|
|
|
|
int |
int |
mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p) |
mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p) |
{ |
{ |
|
|
static void |
static void |
mdoc_node_free(struct mdoc_node *p) |
mdoc_node_free(struct mdoc_node *p) |
{ |
{ |
|
|
if (MDOC_TS == p->tok && MDOC_BLOCK == p->type) |
|
if (p->norm->TS) |
|
tbl_free(p->norm->TS); |
|
|
|
if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type) |
if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type) |
free(p->norm); |
free(p->norm); |