]> diplodocus.org Git - nmh/blobdiff - uip/ali.c
Support RFC 2231 language tags in RFC 2047 header strings.
[nmh] / uip / ali.c
index 737f68c76b73239b2caf70759999958777d1f5b8..3893a1d8ad16c1455cb6abee3bd1dab18d4d3ac4 100644 (file)
--- a/uip/ali.c
+++ b/uip/ali.c
@@ -2,8 +2,6 @@
 /*
  * ali.c -- list nmh mail aliases
  *
- * $Id$
- *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
  * complete copyright information.
 #include <h/mts.h>
 #include <h/utils.h>
 
-/*
- * maximum number of names
- */
-#define        NVEC 50
-
-static struct swit switches[] = {
-#define        ALIASW                0
-    { "alias aliasfile", 0 },
-#define        NALIASW               1
-    { "noalias", -7 },
-#define        LISTSW                2
-    { "list", 0 },
-#define        NLISTSW               3
-    { "nolist", 0 },
-#define        NORMSW                4
-    { "normalize", 0 },
-#define        NNORMSW               5
-    { "nonormalize", 0 },
-#define        USERSW                6
-    { "user", 0 },
-#define        NUSERSW               7
-    { "nouser", 0 },
-#define VERSIONSW             8
-    { "version", 0 },
-#define        HELPSW                9
-    { "help", 0 },
-    { NULL, 0 }
-};
+#define ALI_SWITCHES \
+    X("alias aliasfile", 0, ALIASW) \
+    X("noalias", -7, NALIASW) \
+    X("list", 0, LISTSW) \
+    X("nolist", 0, NLISTSW) \
+    X("normalize", 0, NORMSW) \
+    X("nonormalize", 0, NNORMSW) \
+    X("user", 0, USERSW) \
+    X("nouser", 0, NUSERSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(ALI);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(ALI, switches);
+#undef X
 
 static int pos = 1;
 
@@ -51,8 +40,8 @@ extern struct aka *akahead;
 /*
  * prototypes
  */
-void print_aka (char *, int, int);
-void print_usr (char *, int, int);
+static void print_aka (char *, int, int);
+static void print_usr (char *, int, int);
 
 
 int
@@ -61,7 +50,9 @@ main (int argc, char **argv)
     int i, vecp = 0, inverted = 0, list = 0;
     int noalias = 0, normalize = AD_NHST;
     char *cp, **ap, **argp, buf[BUFSIZ];
-    char *vec[NVEC], **arguments;
+    /* Really only need to allocate for argc-1, but must allocate at least 1,
+       so go ahead and allocate for argc char pointers. */
+    char **vec = mh_xmalloc (argc * sizeof (char *)), **arguments;
     struct aka *ak;
 
 #ifdef LOCALE
@@ -89,10 +80,10 @@ main (int argc, char **argv)
                    snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
                        invo_name);
                    print_help (buf, switches, 1);
-                   done (1);
+                   done (0);
                case VERSIONSW:
                    print_version (invo_name);
-                   done (1);
+                   done (0);
 
                case ALIASW: 
                    if (!(cp = *argp++) || *cp == '-')
@@ -126,7 +117,14 @@ main (int argc, char **argv)
                    continue;
            }
        }
-       vec[vecp++] = cp;
+
+       if (vecp < argc) {
+           vec[vecp++] = cp;
+       } else {
+           /* Should never happen, but try to protect against code changes
+              that could allow it. */
+           adios (NULL, "too many arguments");
+       }
     }
 
     if (!noalias) {
@@ -153,27 +151,27 @@ main (int argc, char **argv)
 
        for (i = 0; i < vecp; i++)
            print_usr (vec[i], list, normalize);
-
-       done (0);
-    }
-
-    if (vecp) {
-       /* print specified aliases */
-       for (i = 0; i < vecp; i++)
-           print_aka (akvalue (vec[i]), list, 0);
     } else {
-       /* print them all */
-       for (ak = akahead; ak; ak = ak->ak_next) {
-           printf ("%s: ", ak->ak_name);
-           pos += strlen (ak->ak_name) + 1;
-           print_aka (akresult (ak), list, pos);
+       if (vecp) {
+           /* print specified aliases */
+           for (i = 0; i < vecp; i++)
+               print_aka (akvalue (vec[i]), list, 0);
+       } else {
+           /* print them all */
+           for (ak = akahead; ak; ak = ak->ak_next) {
+               printf ("%s: ", ak->ak_name);
+               pos += strlen (ak->ak_name) + 1;
+               print_aka (akresult (ak), list, pos);
+           }
        }
     }
 
-    return done (0);
+    free (vec);
+    done (0);
+    return 1;
 }
 
-void
+static void
 print_aka (char *p, int list, int margin)
 {
     char c;
@@ -213,7 +211,7 @@ print_aka (char *p, int list, int margin)
     pos = 1;
 }
 
-void
+static void
 print_usr (char *s, int list, int norm)
 {
     register char *cp, *pp, *vp;
@@ -233,8 +231,8 @@ print_usr (char *s, int list, int norm)
        while ((cp = getname (pp))) {
            if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
                continue;
-           if (!strcasecmp (mp->m_host, np->m_host)
-                   && !strcasecmp (mp->m_mbox, np->m_mbox)) {
+           if (!mh_strcasecmp (mp->m_host, np->m_host)
+                   && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
                vp = vp ? add (ak->ak_name, add (",", vp))
                    : getcpy (ak->ak_name);
                mnfree (np);
@@ -247,12 +245,7 @@ print_usr (char *s, int list, int norm)
     }
     mnfree (mp);
 
-#if 0
-    printf ("%s: ", s);
-    print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
-#else
     print_aka (vp ? vp : s, list, 0);
-#endif
 
     if (vp)
        free (vp);