source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Re-constitution of `ds' symbol processing.
@ 2010-07-07 15:04 kristaps
  0 siblings, 0 replies; only message in thread
From: kristaps @ 2010-07-07 15:04 UTC (permalink / raw)
  To: source

Log Message:
-----------
Re-constitution of `ds' symbol processing.  First, push the
roff_getstr() family of functions into roff.c with the "first_string"
directly in struct roff.  Second, pre-process each line for reserved
words in libroff, splicing and re-running a line if it has one (this
allows defined symbols to be macros).  Remove term.c's invocation of the
roff_getstrn() function.  Removed function documentation in roff.3 and
added roff.7 `ds' documentation.

Modified Files:
--------------
    mdocml:
        Makefile
        libman.h
        libmdoc.h
        main.c
        man.3
        man.c
        man_html.c
        man_term.c
        mdoc.3
        mdoc.c
        mdoc_html.c
        mdoc_term.c
        roff.3
        roff.7
        roff.c
        term.c
        tree.c

Removed Files:
-------------
    mdocml:
        regs.h

Revision Data
-------------
Index: roff.3
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/roff.3,v
retrieving revision 1.5
retrieving revision 1.6
diff -Lroff.3 -Lroff.3 -u -p -r1.5 -r1.6
--- roff.3
+++ roff.3
@@ -28,7 +28,6 @@
 .Nd roff macro compiler library
 .Sh SYNOPSIS
 .In mandoc.h
-.In regs.h
 .In roff.h
 .Ft "struct roff *"
 .Fo roff_alloc
@@ -51,15 +50,6 @@
 .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
@@ -155,52 +145,6 @@ Returns 0 on failure, 1 on success.
 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
Index: roff.7
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/roff.7,v
retrieving revision 1.12
retrieving revision 1.13
diff -Lroff.7 -Lroff.7 -u -p -r1.12 -r1.13
--- roff.7
+++ roff.7
@@ -92,11 +92,20 @@ The syntax of this macro is the same as 
 except that a leading argument must be specified.
 It is ignored, as are its children.
 .Ss \&ds
-Define a string.
-This macro is intended to have two arguments,
-the name of the string to define and its content.
-Currently, it is ignored including its arguments,
-and the number of arguments is not checked.
+Define a reserved word.
+Its syntax is as follows:
+.Pp
+.D1 Pf \. Sx \&ds No Cm key val
+.Pp
+The
+.Cm key
+and
+.Cm val
+strings are space-separated.
+The
+.Cm key
+values may be invoked in subsequent text by using \e*(NN for two-letter
+pairs, \e*N for one-letter, and \e*[NNN] for arbitrary-length values.
 .Ss \&de1
 The syntax of this macro is the same as that of
 .Sx \&ig ,
Index: libman.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/libman.h,v
retrieving revision 1.40
retrieving revision 1.41
diff -Llibman.h -Llibman.h -u -p -r1.40 -r1.41
--- libman.h
+++ libman.h
@@ -17,7 +17,6 @@
 #ifndef LIBMAN_H
 #define LIBMAN_H
 
-#include "regs.h"
 #include "man.h"
 
 enum	man_next {
Index: main.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/main.c,v
retrieving revision 1.97
retrieving revision 1.98
diff -Lmain.c -Lmain.c -u -p -r1.97 -r1.98
--- main.c
+++ main.c
@@ -31,7 +31,6 @@
 #include <unistd.h>
 
 #include "mandoc.h"
-#include "regs.h"
 #include "main.h"
 #include "mdoc.h"
 #include "man.h"
Index: Makefile
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/Makefile,v
retrieving revision 1.286
retrieving revision 1.287
diff -LMakefile -LMakefile -u -p -r1.286 -r1.287
--- Makefile
+++ Makefile
@@ -94,7 +94,7 @@ DATAS	   = arch.in att.in lib.in msec.in
 
 HEADS	   = mdoc.h libmdoc.h man.h libman.h term.h \
 	     libmandoc.h html.h chars.h out.h main.h roff.h \
-	     mandoc.h regs.h
+	     mandoc.h
 
 GSGMLS	   = mandoc.1.sgml mdoc.3.sgml mdoc.7.sgml \
 	     mandoc_char.7.sgml man.7.sgml man.3.sgml roff.7.sgml \
Index: man.3
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man.3,v
retrieving revision 1.21
retrieving revision 1.22
diff -Lman.3 -Lman.3 -u -p -r1.21 -r1.22
--- man.3
+++ man.3
@@ -29,7 +29,6 @@
 .Nd man macro compiler library
 .Sh SYNOPSIS
 .In mandoc.h
-.In regs.h
 .In man.h
 .Vt extern const char * const * man_macronames;
 .Ft "struct man *"
Index: term.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/term.c,v
retrieving revision 1.159
retrieving revision 1.160
diff -Lterm.c -Lterm.c -u -p -r1.159 -r1.160
--- term.c
+++ term.c
@@ -31,7 +31,6 @@
 #include "mandoc.h"
 #include "chars.h"
 #include "out.h"
-#include "regs.h"
 #include "term.h"
 #include "main.h"
 
@@ -379,11 +378,6 @@ res(struct termp *p, const char *word, s
 	size_t		 sz;
 
 	rhs = chars_a2res(p->symtab, word, len, &sz);
-	if (NULL == rhs) {
-		rhs = roff_getstrn(word, len);
-		if (rhs)
-			sz = strlen(rhs);
-	}
 	if (rhs)
 		encode(p, rhs, sz);
 }
Index: man_html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man_html.c,v
retrieving revision 1.40
retrieving revision 1.41
diff -Lman_html.c -Lman_html.c -u -p -r1.40 -r1.41
--- man_html.c
+++ man_html.c
@@ -29,7 +29,6 @@
 #include "mandoc.h"
 #include "out.h"
 #include "html.h"
-#include "regs.h"
 #include "man.h"
 #include "main.h"
 
Index: libmdoc.h
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/libmdoc.h,v
retrieving revision 1.59
retrieving revision 1.60
diff -Llibmdoc.h -Llibmdoc.h -u -p -r1.59 -r1.60
--- libmdoc.h
+++ libmdoc.h
@@ -17,7 +17,6 @@
 #ifndef LIBMDOC_H
 #define LIBMDOC_H
 
-#include "regs.h"
 #include "mdoc.h"
 
 enum	mdoc_next {
Index: mdoc_html.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_html.c,v
retrieving revision 1.94
retrieving revision 1.95
diff -Lmdoc_html.c -Lmdoc_html.c -u -p -r1.94 -r1.95
--- mdoc_html.c
+++ mdoc_html.c
@@ -30,7 +30,6 @@
 #include "mandoc.h"
 #include "out.h"
 #include "html.h"
-#include "regs.h"
 #include "mdoc.h"
 #include "main.h"
 
--- regs.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*	$Id: regs.h,v 1.6 2010/07/05 20:10:22 kristaps Exp $ */
-/*
- * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010 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.
- */
-#ifndef REGS_H
-#define REGS_H
-
-__BEGIN_DECLS
-
-char		 *roff_setstr(const char *, const char *);
-char		 *roff_getstr(const char *);
-char		 *roff_getstrn(const char *, size_t);
-void		  roff_freestr(void);
-
-__END_DECLS
-
-#endif /*!REGS_H*/
Index: man.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man.c,v
retrieving revision 1.80
retrieving revision 1.81
diff -Lman.c -Lman.c -u -p -r1.80 -r1.81
--- man.c
+++ man.c
@@ -28,7 +28,6 @@
 #include <string.h>
 
 #include "mandoc.h"
-#include "regs.h"
 #include "libman.h"
 #include "libmandoc.h"
 
Index: mdoc.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc.c,v
retrieving revision 1.157
retrieving revision 1.158
diff -Lmdoc.c -Lmdoc.c -u -p -r1.157 -r1.158
--- mdoc.c
+++ mdoc.c
@@ -30,7 +30,6 @@
 #include <time.h>
 
 #include "mandoc.h"
-#include "regs.h"
 #include "libmdoc.h"
 #include "libmandoc.h"
 
Index: roff.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/roff.c,v
retrieving revision 1.93
retrieving revision 1.94
diff -Lroff.c -Lroff.c -u -p -r1.93 -r1.94
--- roff.c
+++ roff.c
@@ -28,8 +28,8 @@
 #include <stdio.h>
 
 #include "mandoc.h"
-#include "regs.h"
 #include "roff.h"
+#include "libmandoc.h"
 
 #define	RSTACK_MAX	128
 
@@ -69,6 +69,13 @@ enum	roffrule {
 	ROFFRULE_DENY
 };
 
+
+struct	roffstr {
+	char		*name; /* key of symbol */
+	char		*string; /* current value */
+	struct roffstr	*next; /* next in list */
+};
+
 struct	roff {
 	struct roffnode	*last; /* leaf of stack */
 	mandocmsg	 msg; /* err/warn/fatal messages */
@@ -76,6 +83,7 @@ struct	roff {
 	enum roffrule	 rstack[RSTACK_MAX]; /* stack of !`ie' rules */
 	int		 rstackpos; /* position in rstack */
 	struct regset	*regs; /* read/writable registers */
+	struct roffstr	*first_string;
 };
 
 struct	roffnode {
@@ -109,12 +117,6 @@ struct	roffmac {
 	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);
@@ -124,9 +126,16 @@ 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 roffrule	 roff_evalcond(const char *, int *);
+static	void		 roff_freestr(struct roff *);
+static	const char	*roff_getstrn(const struct roff *, 
+				const char *, size_t);
 static	enum rofferr	 roff_line(ROFF_ARGS);
 static	enum rofferr	 roff_nr(ROFF_ARGS);
-static	enum roffrule	 roff_evalcond(const char *, int *);
+static	int		 roff_res(struct roff *, int, 
+				char **, size_t *, int, int *);
+static	void		 roff_setstr(struct roff *,
+				const char *, const char *);
 
 /* See roff_hash_find() */
 
@@ -276,7 +285,7 @@ roff_free1(struct roff *r)
 
 	while (r->last)
 		roffnode_pop(r);
-	roff_freestr();
+	roff_freestr(r);
 }
 
 
@@ -317,6 +326,74 @@ roff_alloc(struct regset *regs, const ma
 }
 
 
+/*
+ * Pre-filter each and every line for reserved words (one beginning with
+ * `\*', e.g., `\*(ab').  These must be handled before the actual line
+ * is processed. 
+ */
+static int
+roff_res(struct roff *r, int ln, char **bufp,
+		size_t *szp, int pos, int *offs)
+{
+	const char	*cp, *cpp, *st, *res;
+	int		 i, maxl;
+	size_t		 nsz;
+	char		*n;
+
+	for (cp = &(*bufp)[pos]; (cpp = strstr(cp, "\\*")); cp++) {
+		cp = cpp + 2;
+		switch (*cp) {
+		case ('('):
+			cp++;
+			maxl = 2;
+			break;
+		case ('['):
+			cp++;
+			maxl = 0;
+			break;
+		default:
+			maxl = 1;
+			break;
+		}
+
+		st = cp;
+
+		for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
+			if ('\0' == *cp)
+				return(1); /* Error. */
+			if (0 == maxl && ']' == *cp)
+				break;
+		}
+
+		res = roff_getstrn(r, st, (size_t)i);
+
+		if (NULL == res) {
+			cp -= maxl ? 1 : 0;
+			continue;
+		}
+
+		ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i, st);
+
+		nsz = *szp + strlen(res) + 1;
+		n = mandoc_malloc(nsz);
+
+		*n = '\0';
+
+		strlcat(n, *bufp, (size_t)(cpp - *bufp + 1));
+		strlcat(n, res, nsz);
+		strlcat(n, cp + (maxl ? 0 : 1), nsz);
+
+		free(*bufp);
+
+		*bufp = n;
+		*szp = nsz;
+		return(0);
+	}
+
+	return(1);
+}
+
+
 enum rofferr
 roff_parseln(struct roff *r, int ln, char **bufp, 
 		size_t *szp, int pos, int *offs)
@@ -325,6 +402,14 @@ roff_parseln(struct roff *r, int ln, cha
 	int		 ppos;
 
 	/*
+	 * Run the reserved-word filter only if we have some reserved
+	 * words to fill in.
+	 */
+
+	if (r->first_string && ! roff_res(r, ln, bufp, szp, pos, offs))
+		return(ROFF_RERUN);
+
+	/*
 	 * First, if a scope is open and we're not a macro, pass the
 	 * text through the macro's filter.  If a scope isn't open and
 	 * we're not a macro, just let it through.
@@ -338,11 +423,8 @@ roff_parseln(struct roff *r, int ln, cha
 		return((*roffs[t].text)
 				(r, t, bufp, szp, 
 				 ln, pos, pos, offs));
-	} else if ( ! ROFF_CTL((*bufp)[pos])) {
-		ROFF_DEBUG("roff: pass non-scoped text: [%s]\n", 
-				&(*bufp)[pos]);
+	} else if ( ! ROFF_CTL((*bufp)[pos]))
 		return(ROFF_CONT);
-	}
 
 	/*
 	 * If a scope is open, go to the child handler for that macro,
@@ -366,11 +448,8 @@ roff_parseln(struct roff *r, int ln, cha
 	 */
 
 	ppos = pos;
-	if (ROFF_MAX == (t = roff_parse(*bufp, &pos))) {
-		ROFF_DEBUG("roff: pass non-scoped non-macro: [%s]\n", 
-				&(*bufp)[pos]);
+	if (ROFF_MAX == (t = roff_parse(*bufp, &pos)))
 		return(ROFF_CONT);
-	}
 
 	ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n", 
 			roffs[t].name, &(*bufp)[pos]);
@@ -914,7 +993,7 @@ roff_ds(ROFF_ARGS)
 			*end = '\0';
 	}
 
-	roff_setstr(name, string);
+	roff_setstr(r, name, string);
 	return(ROFF_IGN);
 }
 
@@ -962,73 +1041,53 @@ roff_nr(ROFF_ARGS)
 }
 
 
-char *
-roff_setstr(const char *name, const char *string)
+static void
+roff_setstr(struct roff *r, const char *name, const char *string)
 {
 	struct roffstr	 *n;
 	char		 *namecopy;
 
-	n = first_string;
+	n = r->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;
+	if (NULL == n) {
+		namecopy = mandoc_strdup(name);
+		n = mandoc_malloc(sizeof(struct roffstr));
+		n->name = namecopy;
+		n->next = r->first_string;
+		r->first_string = n;
+	} else
+		free(n->string);
 
-	n = first_string;
-	while (n && strcmp(name, n->name))
-		n = n->next;
-	if (n)
-		return(n->string);
-	else
-		return(NULL);
+	n->string = string ? strdup(string) : NULL;
 }
 
-char *
-roff_getstrn(const char *name, size_t len)
+
+static const char *
+roff_getstrn(const struct roff *r, const char *name, size_t len)
 {
-	struct roffstr	 *n;
+	const struct roffstr *n;
 
-	n = first_string;
+	n = r->first_string;
 	while (n && (strncmp(name, n->name, len) || '\0' != n->name[len]))
 		n = n->next;
-	if (n)
-		return(n->string);
-	else
-		return(NULL);
+
+	return(n ? n->string : NULL);
 }
 
-void
-roff_freestr(void)
+
+static void
+roff_freestr(struct roff *r)
 {
 	struct roffstr	 *n, *nn;
 
-	for (n = first_string; n; n = nn) {
+	for (n = r->first_string; n; n = nn) {
 		free(n->name);
 		free(n->string);
 		nn = n->next;
 		free(n);
 	}
-	first_string = NULL;
+
+	r->first_string = NULL;
 }
Index: man_term.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/man_term.c,v
retrieving revision 1.78
retrieving revision 1.79
diff -Lman_term.c -Lman_term.c -u -p -r1.78 -r1.79
--- man_term.c
+++ man_term.c
@@ -28,7 +28,6 @@
 
 #include "mandoc.h"
 #include "out.h"
-#include "regs.h"
 #include "man.h"
 #include "term.h"
 #include "chars.h"
Index: mdoc.3
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc.3,v
retrieving revision 1.47
retrieving revision 1.48
diff -Lmdoc.3 -Lmdoc.3 -u -p -r1.47 -r1.48
--- mdoc.3
+++ mdoc.3
@@ -30,7 +30,6 @@
 .Nd mdoc macro compiler library
 .Sh SYNOPSIS
 .In mandoc.h
-.In regs.h
 .In mdoc.h
 .Vt extern const char * const * mdoc_macronames;
 .Vt extern const char * const * mdoc_argnames;
Index: mdoc_term.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mdoc_term.c,v
retrieving revision 1.172
retrieving revision 1.173
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.172 -r1.173
--- mdoc_term.c
+++ mdoc_term.c
@@ -31,7 +31,6 @@
 #include "mandoc.h"
 #include "out.h"
 #include "term.h"
-#include "regs.h"
 #include "mdoc.h"
 #include "chars.h"
 #include "main.h"
Index: tree.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/tree.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -Ltree.c -Ltree.c -u -p -r1.23 -r1.24
--- tree.c
+++ tree.c
@@ -24,7 +24,6 @@
 #include <time.h>
 
 #include "mandoc.h"
-#include "regs.h"
 #include "mdoc.h"
 #include "man.h"
 #include "main.h"
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-07-07 15:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-07 15:04 mdocml: Re-constitution of `ds' symbol processing kristaps

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).