mdoc_argv.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
/* $Id: mdoc_argv.c,v 1.100 2015/02/04 18:59:45 schwarze Exp $ */
/*
* Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mdoc.h"
#include "mandoc.h"
#include "mandoc_aux.h"
#include "libmdoc.h"
#include "libmandoc.h"
enum argsflag {
ARGSFL_NONE = 0,
ARGSFL_DELIM, /* handle delimiters of [[::delim::][ ]+]+ */
ARGSFL_TABSEP /* handle tab/`Ta' separated phrases */
};
enum argvflag {
ARGV_NONE, /* no args to flag (e.g., -split) */
ARGV_SINGLE, /* one arg to flag (e.g., -file xxx) */
ARGV_MULTI /* multiple args (e.g., -column xxx yyy) */
};
struct mdocarg {
};
char *, enum argsflag, char **);
static int args_checkpunct(const char *, int);
static void argv_multi(struct mdoc *, int,
struct mdoc_argv *, int *, char *);
static void argv_single(struct mdoc *, int,
struct mdoc_argv *, int *, char *);
ARGV_NONE, /* MDOC_Split */
ARGV_NONE, /* MDOC_Nosplit */
ARGV_NONE, /* MDOC_Ragged */
ARGV_NONE, /* MDOC_Unfilled */
ARGV_NONE, /* MDOC_Literal */
ARGV_SINGLE, /* MDOC_File */
ARGV_SINGLE, /* MDOC_Offset */
ARGV_NONE, /* MDOC_Bullet */
ARGV_NONE, /* MDOC_Dash */
ARGV_NONE, /* MDOC_Hyphen */
ARGV_NONE, /* MDOC_Item */
ARGV_NONE, /* MDOC_Enum */
ARGV_NONE, /* MDOC_Tag */
ARGV_NONE, /* MDOC_Diag */
ARGV_NONE, /* MDOC_Hang */
ARGV_NONE, /* MDOC_Ohang */
ARGV_NONE, /* MDOC_Inset */
ARGV_MULTI, /* MDOC_Column */
ARGV_SINGLE, /* MDOC_Width */
ARGV_NONE, /* MDOC_Compact */
ARGV_NONE, /* MDOC_Std */
ARGV_NONE, /* MDOC_Filled */
ARGV_NONE, /* MDOC_Words */
ARGV_NONE, /* MDOC_Emphasis */
ARGV_NONE, /* MDOC_Symbolic */
ARGV_NONE /* MDOC_Symbolic */
};
};
};
};
};
};
};
};
/*
* Parse flags and their arguments from the input line.
* These come in the form -flag [argument ...].
* Some flags take no argument, some one, some multiple.
*/
void
{
char *argname;
char savechar;
/* Which flags does this macro support? */
return;
/* Loop over the flags on the input line. */
/* Seek to the first unescaped space. */
break;
/*
* We want to nil-terminate the word to look it up.
* But we may not have a flag, in which case we need
* to restore the line as-is. So keep around the
* stray byte, which we'll reset upon exiting.
*/
/*
* Now look up the word as a flag. Use temporary
* storage that we'll copy into the node's flags.
*/
break;
/* If it isn't a flag, restore the saved byte. */
if (savechar != '\0')
break;
}
/* Read to the next word (the first argument). */
ipos++;
/* Parse the arguments of the flag. */
case ARGV_SINGLE:
break;
case ARGV_MULTI:
break;
case ARGV_NONE:
break;
}
/* Append to the return values. */
/* Prepare for parsing the next flag. */
}
}
void
mdoc_argv_free(struct mdoc_arg *p)
{
int i;
if (NULL == p)
return;
if (p->refcnt) {
--(p->refcnt);
if (p->refcnt)
return;
}
for (i = (int)p->argc - 1; i >= 0; i--)
argn_free(p, i);
free(p);
}
static void
{
int j;
}
}
enum margserr
{
struct mdoc_node *n;
char *v_local;
if (v == NULL)
v = &v_local;
/*
* We know that we're in an `It', so it's reasonable to expect
* us to be sitting in a `Bl'. Someday this may not be the case
* (if we allow random `It's sitting out there), so provide a
* safe fall-back into the default behaviour.
*/
fl = ARGSFL_TABSEP;
break;
}
}
static enum margserr
{
char *p, *pp;
int pairs;
return(ARGS_EOLN);
/*
* If we're not in a partial phrase and the flag for
* being a phrase literal is still set, the punctuation
* is unterminated.
*/
return(ARGS_EOLN);
}
if (ARGSFL_DELIM == fl)
return(ARGS_PUNCT);
/*
* First handle TABSEP items, restricted to `Bl -column'. This
* ignores conventional token parsing and instead uses tabs or
* `Ta' macros to separate phrases. Phrases are parsed again
* for arguments at a later phase.
*/
if (ARGSFL_TABSEP == fl) {
/* Scan ahead to tab (can't be escaped). */
p = strchr(*v, '\t');
/* Scan ahead to unescaped `Ta'. */
break;
continue;
break;
}
/* By default, assume a phrase. */
rc = ARGS_PHRASE;
/*
* Adjust new-buffer position to be beyond delimiter
* mark (e.g., Ta -> end + 2).
*/
if (p && pp) {
} else if (p && ! pp) {
rc = ARGS_PPHRASE;
*pos += 1;
} else if (pp && ! p) {
p = pp;
*pos += 2;
} else {
p = strchr(*v, 0);
}
/* Whitespace check for eoln case... */
if ('\0' == *p && ' ' == *(p - 1))
*pos += (int)(p - *v);
/* Strip delimiter's preceding whitespace. */
pp = p - 1;
break;
pp--;
}
*(pp + 1) = 0;
/* Strip delimiter's proceeding whitespace. */
/* Skip ahead. */ ;
return(rc);
}
/*
* Process a quoted literal. A quote begins with a double-quote
* and ends with a double-quote NOT preceded by a double-quote.
* NUL-terminate the literal in place.
* Collapse pairs of quotes inside quoted literals.
* Whitespace is NOT involved in literal termination.
*/
pairs = 0;
/* Move following text left after quoted quotes. */
if (pairs)
continue;
/* Unquoted quotes end quoted args. */
break;
/* Quoted quotes collapse. */
pairs++;
(*pos)++;
}
if (pairs)
return(ARGS_QWORD);
return(ARGS_QWORD);
}
return(ARGS_QWORD);
(*pos)++;
return(ARGS_QWORD);
}
return(ARGS_WORD);
}
/*
* Check if the string consists only of space-separated closing
* delimiters. This is a bit of a dance: the first must be a close
* delimiter, but it may be followed by middle delimiters. Arbitrary
* whitespace may separate these tokens.
*/
static int
args_checkpunct(const char *buf, int i)
{
int j;
enum mdelim d;
/* First token must be a close-delimiter. */
if (DELIMSZ == j)
return(0);
dbuf[j] = '\0';
return(0);
while (' ' == buf[i])
i++;
while (buf[i]) {
j = 0;
if (DELIMSZ == j)
return(0);
dbuf[j] = '\0';
d = mdoc_isdelim(dbuf);
if (DELIM_NONE == d || DELIM_OPEN == d)
return(0);
while (' ' == buf[i])
i++;
}
return('\0' == buf[i]);
}
static void
{
char *p;
break;
break;
if (v->sz % MULTI_STEP == 0)
v->sz + MULTI_STEP, sizeof(char *));
}
}
static void
{
char *p;
return;
v->sz = 1;
v->value = mandoc_malloc(sizeof(char *));
v->value[0] = mandoc_strdup(p);
}