For whatever reason, I found myself looking at the m4 code. In eval.c,
there are some funny functions. I figure if you have to cast every
instance of a variable to a different type, maybe the problem is
you're starting with the wrong type. Switching to unsigned char *
inputs makes these functions much simpler.

Also simplify and eliminate statics. I can't see any reason frombis 
needs to be static, and in the case of mapvec being static just means
more work to undo writes to it.

(I changed seen to int to make clear it's not used for character data.)

Index: eval.c
===================================================================
RCS file: /cvs/src/usr.bin/m4/eval.c,v
retrieving revision 1.70
diff -u -p -r1.70 eval.c
--- eval.c      12 Apr 2012 17:00:11 -0000      1.70
+++ eval.c      21 Apr 2013 06:26:41 -0000
@@ -67,8 +67,10 @@ static void  dom4wrap(const char *);
 static void    dodiv(int);
 static void    doundiv(const char *[], int);
 static void    dosub(const char *[], int);
-static void    map(char *, const char *, const char *, const char *);
-static const char *handledash(char *, char *, const char *);
+static void    map(unsigned char *, const unsigned char *,
+    const unsigned char *, const unsigned char *);
+static const unsigned char *handledash(unsigned char *, unsigned char *,
+    const unsigned char *);
 static void    expand_builtin(const char *[], int, int);
 static void    expand_macro(const char *[], int);
 static void    dump_one_def(const char *, struct macro_definition *);
@@ -892,34 +894,15 @@ dosub(const char *argv[], int argc)
  * which means that those characters dissapear altogether. 
  */
 static void
-map(char *dest, const char *src, const char *from, const char *to)
+map(unsigned char *dest, const unsigned char *src, const unsigned char *from,
+    const unsigned char *to)
 {
-       const char *tmp;
        unsigned char sch, dch;
-       static char frombis[257];
-       static char tobis[257];
+       unsigned char frombis[257];
+       unsigned char tobis[257];
+       unsigned char mapvec[256];
+       int seen[256];
        int i;
-       char seen[256];
-       static unsigned char mapvec[256] = {
-           0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
-           19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
-           36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
-           53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
-           70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
-           87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102,
-           103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
-           116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128,
-           129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
-           142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
-           155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
-           168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
-           181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193,
-           194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206,
-           207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
-           220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232,
-           233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
-           246, 247, 248, 249, 250, 251, 252, 253, 254, 255
-       };
 
        if (*src) {
                if (mimic_gnu) {
@@ -929,17 +912,17 @@ map(char *dest, const char *src, const c
                        from = handledash(frombis, frombis + 256, from);
                        to = handledash(tobis, tobis + 256, to);
                }
-               tmp = from;
-       /*
-        * create a mapping between "from" and
-        * "to"
-        */
-               for (i = 0; i < 256; i++)
+               /*
+                * create a mapping between "from" and "to"
+                */
+               for (i = 0; i < 256; i++) {
+                       mapvec[i] = i;
                        seen[i] = 0;
+               }
                while (*from) {
-                       if (!seen[(unsigned char)(*from)]) {
-                               mapvec[(unsigned char)(*from)] = (unsigned 
char)(*to);
-                               seen[(unsigned char)(*from)] = 1;
+                       if (!seen[*from]) {
+                               mapvec[*from] = *to;
+                               seen[*from] = 1;
                        }
                        from++;
                        if (*to)
@@ -947,18 +930,11 @@ map(char *dest, const char *src, const c
                }
 
                while (*src) {
-                       sch = (unsigned char)(*src++);
+                       sch = *src++;
                        dch = mapvec[sch];
-                       if ((*dest = (char)dch))
+                       if ((*dest = dch))
                                dest++;
                }
-       /*
-        * restore all the changed characters
-        */
-               while (*tmp) {
-                       mapvec[(unsigned char)(*tmp)] = (unsigned char)(*tmp);
-                       tmp++;
-               }
        }
        *dest = '\0';
 }
@@ -969,18 +945,18 @@ map(char *dest, const char *src, const c
  *  use buffer to copy the src string, expanding character ranges
  * on the way.
  */
-static const char *
-handledash(char *buffer, char *end, const char *src)
+static const unsigned char *
+handledash(unsigned char *buffer, unsigned char *end,
+    const unsigned char *src)
 {
-       char *p;
+       unsigned char *p;
 
        p = buffer;
        while(*src) {
                if (src[1] == '-' && src[2]) {
                        unsigned char i;
-                       if ((unsigned char)src[0] <= (unsigned char)src[2]) {
-                               for (i = (unsigned char)src[0]; 
-                                   i <= (unsigned char)src[2]; i++) {
+                       if (src[0] <= src[2]) {
+                               for (i = src[0]; i <= src[2]; i++) {
                                        *p++ = i;
                                        if (p == end) {
                                                *p = '\0';
@@ -988,8 +964,7 @@ handledash(char *buffer, char *end, cons
                                        }
                                }
                        } else {
-                               for (i = (unsigned char)src[0]; 
-                                   i >= (unsigned char)src[2]; i--) {
+                               for (i = src[0]; i >= src[2]; i--) {
                                        *p++ = i;
                                        if (p == end) {
                                                *p = '\0';

Reply via email to