Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/mdocml/dist support expanding numbered registers



details:   https://anonhg.NetBSD.org/src/rev/acc583bf7d86
branches:  trunk
changeset: 785592:acc583bf7d86
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 21 21:42:16 2013 +0000

description:
support expanding numbered registers

diffstat:

 external/bsd/mdocml/dist/libmandoc.h |   1 +
 external/bsd/mdocml/dist/read.c      |  16 ++++++++
 external/bsd/mdocml/dist/roff.c      |  71 ++++++++++++++++++++++++++++++++++-
 3 files changed, 85 insertions(+), 3 deletions(-)

diffs (153 lines):

diff -r 0324b46e204d -r acc583bf7d86 external/bsd/mdocml/dist/libmandoc.h
--- a/external/bsd/mdocml/dist/libmandoc.h      Thu Mar 21 21:01:10 2013 +0000
+++ b/external/bsd/mdocml/dist/libmandoc.h      Thu Mar 21 21:42:16 2013 +0000
@@ -73,6 +73,7 @@
 enum rofferr    roff_parseln(struct roff *, int, 
                        char **, size_t *, int, int *);
 void            roff_endparse(struct roff *);
+size_t          roff_expand_nr(struct roff *, const char *, char *, size_t);
 int             roff_regisset(const struct roff *, enum regs);
 unsigned int    roff_regget(const struct roff *, enum regs);
 void            roff_regunset(struct roff *, enum regs);
diff -r 0324b46e204d -r acc583bf7d86 external/bsd/mdocml/dist/read.c
--- a/external/bsd/mdocml/dist/read.c   Thu Mar 21 21:01:10 2013 +0000
+++ b/external/bsd/mdocml/dist/read.c   Thu Mar 21 21:42:16 2013 +0000
@@ -353,6 +353,22 @@
                                continue;
                        }
 
+                       if ('\\' == blk.buf[i] && 'n' == blk.buf[i + 1]) {
+                               int j, k;
+                               i += 2;
+                               if ('(' == blk.buf[i]) /* ) */
+                                       i++;
+                               resize_buf(&ln, 256);
+                               for (j = i, k = pos; i < j + 256 
+                                   && i < (int)blk.sz
+                                   && !isspace((unsigned char)blk.buf[i]);)
+                                   ln.buf[k++] = blk.buf[i++];
+
+                               ln.buf[k] = '\0';
+                               pos += roff_expand_nr(curp->roff,
+                                   ln.buf + pos, ln.buf + pos, 256);
+                       }
+
                        /*
                         * Found escape and at least one other character.
                         * When it's a newline character, skip it.
diff -r 0324b46e204d -r acc583bf7d86 external/bsd/mdocml/dist/roff.c
--- a/external/bsd/mdocml/dist/roff.c   Thu Mar 21 21:01:10 2013 +0000
+++ b/external/bsd/mdocml/dist/roff.c   Thu Mar 21 21:42:16 2013 +0000
@@ -21,8 +21,10 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
 
 #include "mandoc.h"
 #include "libroff.h"
@@ -117,6 +119,7 @@
        struct eqn_node *last_eqn; /* last equation parsed */
        struct eqn_node *first_eqn; /* first equation parsed */
        struct eqn_node *eqn; /* current equation being parsed */
+       struct roff_nr  *nr[64];        /* numbered register set */
 };
 
 struct roffnode {
@@ -1234,21 +1237,70 @@
        r->regs[(int)reg].set = 0;
 }
 
+struct roff_nr {
+       char *str;
+       uint32_t hash;
+       intmax_t val;
+       struct roff_nr *next;
+};
+
+static uint32_t
+hash_str(const char *str)
+{
+       const uint8_t *s = (const uint8_t *)str;
+       uint8_t c;
+       uint32_t hv = 0;
+       while ((c = *s++) != '\0') 
+               hv = hv * 33 + c;           /* "perl": k=33, r=r+r/32 */
+       return hv + (hv >> 5);
+}
+
+static struct roff_nr *
+hash_find(struct roff *r, const char *str, uint32_t *h)
+{
+       struct roff_nr *e;
+       *h = hash_str(str) % __arraycount(r->nr);
+
+       for (e = r->nr[*h]; e; e = e->next)
+               if (e->hash == *h && strcmp(e->str, str) == 0)
+                       return e;
+       return NULL;
+}
+
+static struct roff_nr *
+hash_insert(struct roff *r, const char *str, uint32_t h)
+{
+       struct roff_nr *e;
+
+       e = mandoc_malloc(sizeof(*e));
+       e->str = mandoc_strdup(str);
+       e->hash = h;
+       e->next = r->nr[h];
+       r->nr[h] = e;
+       return e;
+}
+
 /* ARGSUSED */
 static enum rofferr
 roff_nr(ROFF_ARGS)
 {
        const char      *key;
        char            *val;
-       int              iv;
+       uint32_t         hv;
+       struct roff_nr  *h;
 
        val = *bufp + pos;
        key = roff_getname(r, &val, ln, pos);
 
+       if ((h = hash_find(r, key, &hv)) == NULL)
+               h = hash_insert(r, key, hv);
+
+       h->val = mandoc_strntoi(val, strlen(val), 10);
+
        if (0 == strcmp(key, "nS")) {
                r->regs[(int)REG_nS].set = 1;
-               if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
-                       r->regs[(int)REG_nS].u = (unsigned)iv;
+               if (h->val >= 0)
+                       r->regs[(int)REG_nS].u = (unsigned)h->val;
                else
                        r->regs[(int)REG_nS].u = 0u;
        }
@@ -1256,6 +1308,19 @@
        return(ROFF_IGN);
 }
 
+size_t
+roff_expand_nr(struct roff *r, const char *key, char *lp, size_t lpl)
+{
+       uint32_t         hv;
+       struct roff_nr  *h;
+
+       if ((h = hash_find(r, key, &hv)) == NULL)
+               return 0;
+
+       /* XXX: support .af */
+       return snprintf(lp, lpl, "%jd", h->val);
+}
+
 /* ARGSUSED */
 static enum rofferr
 roff_rm(ROFF_ARGS)



Home | Main Index | Thread Index | Old Index