-.\" $Id: roff.3,v 1.3 2010/06/27 15:52:41 kristaps Exp $
+.\" $Id: roff.3,v 1.4 2010/07/03 16:02:12 schwarze Exp $
.\"
.\" Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
.\"
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
-.Dd $Mdocdate: June 27 2010 $
+.Dd $Mdocdate: July 3 2010 $
.Dt ROFF 3
.Os
.Sh NAME
.Fc
.Ft void
.Fn roff_reset "struct roff *roff"
+.In regs.h
+.Ft "char *"
+.Fn roff_setstr "const char *name" "const char *string"
+.Ft "char *"
+.Fn roff_getstr "const char *name"
+.Ft "char *"
+.Fn roff_getstrn "const char *name" "size_t len"
+.Ft void
+.Fn roff_freestr void
.Sh DESCRIPTION
The
.Nm
Signals that the parse is complete.
Returns 0 on failure, 1 on success.
.El
+.Sh USER-DEFINED STRINGS
+Strings defined by the
+.Xr roff 7
+.Sx \&ds
+instruction are saved using the
+.Fn roff_setstr
+function and retrieved using the
+.Fn roff_getstr
+and
+.Fn roff_getstrn
+functions.
+.Pp
+These functions take the name of the string to be accessed
+as their first argument.
+While
+.Fn roff_getstr
+requires the name to be null-terminated,
+.Fn roff_getstrn
+accepts non-terminated strings, but requires the length of the name
+to be specified.
+.Pp
+The second argument to
+.Fn roff_setstr
+is the new value of the string.
+It will be copied to internal storage, so both pointers to constant
+strings and pointers to volatile storage are acceptable.
+.Pp
+All of these functions return a pointer to the new value of the string
+in internal storage, which should be considered read-only, so use
+.Xr strdup 3
+on it as appropriate.
+The read functions return NULL when a string of the specified name
+is not available or empty, and
+.Fn roff_setstr
+returns NULL when memory allocation fails.
+In the latter case, the string will remain unset.
+.Pp
+The function
+.Fn roff_freestr
+clears all user-defined strings.
+It always succeeds.
+Both
+.Fn roff_reset
+and
+.Fn roff_free
+call it.
.Sh EXAMPLES
See
.Pa main.c
.Nm
library was written by
.An Kristaps Dzonsons Aq kristaps@bsd.lv .
+.Sh BUGS
+The implementation of user-defined strings needs improvement:
+.Bl -dash
+.It
+String values are taken literally and are not interpreted.
+.It
+Parsing of quoted strings is incomplete.
+.It
+The stings are stored internally using a singly linked list,
+which is fine for small numbers of strings,
+but ineffient when handling many strings.
+.El
-/* $Id: roff.c,v 1.91 2010/06/27 16:36:22 kristaps Exp $ */
+/* $Id: roff.c,v 1.92 2010/07/03 16:02:12 schwarze Exp $ */
/*
* Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
*
struct roffmac *next;
};
+struct roffstr {
+ char *name;
+ char *string;
+ struct roffstr *next;
+} *first_string;
+
static enum rofferr roff_block(ROFF_ARGS);
static enum rofferr roff_block_text(ROFF_ARGS);
static enum rofferr roff_block_sub(ROFF_ARGS);
static enum rofferr roff_cond(ROFF_ARGS);
static enum rofferr roff_cond_text(ROFF_ARGS);
static enum rofferr roff_cond_sub(ROFF_ARGS);
+static enum rofferr roff_ds(ROFF_ARGS);
static enum rofferr roff_line(ROFF_ARGS);
static enum rofferr roff_nr(ROFF_ARGS);
static enum roffrule roff_evalcond(const char *, int *);
{ "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
{ "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
{ "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
- { "ds", roff_line, NULL, NULL, 0, NULL },
+ { "ds", roff_ds, NULL, NULL, 0, NULL },
{ "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
{ "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
{ "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
while (r->last)
roffnode_pop(r);
+ roff_freestr();
}
}
+/* ARGSUSED */
+static enum rofferr
+roff_ds(ROFF_ARGS)
+{
+ char *name, *string, *end;
+
+ name = *bufp + pos;
+ if ('\0' == *name)
+ return(ROFF_IGN);
+
+ string = name;
+ while (*string && ' ' != *string)
+ string++;
+ if (*string)
+ *(string++) = NULL;
+ if (*string && '"' == *string)
+ string++;
+ while (*string && ' ' == *string)
+ string++;
+ end = string;
+ while (*end)
+ end++;
+ if (string < end) {
+ end--;
+ if (*end == '"')
+ *end = '\0';
+ }
+
+ roff_setstr(name, string);
+ return(ROFF_IGN);
+}
+
+
/* ARGSUSED */
static enum rofferr
roff_nr(ROFF_ARGS)
return(ROFF_IGN);
}
+
+
+char *
+roff_setstr(const char *name, const char *string)
+{
+ struct roffstr *n;
+ char *namecopy;
+
+ n = first_string;
+ while (n && strcmp(name, n->name))
+ n = n->next;
+ if (n) {
+ free(n->string);
+ } else {
+ if (NULL == (namecopy = strdup(name)))
+ return(NULL);
+ if (NULL == (n = malloc(sizeof(struct roffstr)))) {
+ free(n);
+ return(NULL);
+ }
+ n->name = namecopy;
+ n->next = first_string;
+ first_string = n;
+ }
+ if (string)
+ n->string = strdup(string);
+ else
+ n->string = NULL;
+ return(n->string);
+}
+
+char *
+roff_getstr(const char *name)
+{
+ struct roffstr *n;
+
+ n = first_string;
+ while (n && strcmp(name, n->name))
+ n = n->next;
+ if (n)
+ return(n->string);
+ else
+ return(NULL);
+}
+
+char *
+roff_getstrn(const char *name, size_t len)
+{
+ struct roffstr *n;
+
+ n = first_string;
+ while (n && (strncmp(name, n->name, len) || '\0' != n->name[len]))
+ n = n->next;
+ if (n)
+ return(n->string);
+ else
+ return(NULL);
+}
+
+void
+roff_freestr(void)
+{
+ struct roffstr *n, *nn;
+
+ for (n = first_string; n; n = nn) {
+ free(n->name);
+ free(n->string);
+ nn = n->next;
+ free(n);
+ }
+ first_string = NULL;
+}