]> diplodocus.org Git - nmh/blobdiff - uip/ali.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / ali.c
index e9a63fdacc2a0184554f492b61f09b632d561322..43e9f5a73aa39600aa95db7d364ef6304187d3b5 100644 (file)
--- a/uip/ali.c
+++ b/uip/ali.c
@@ -1,6 +1,4 @@
-
-/*
- * ali.c -- list nmh mail aliases
+/* ali.c -- list nmh mail aliases
  *
  * This code is Copyright (c) 2002, by the authors of nmh.  See the
  * COPYRIGHT file in the root directory of the nmh distribution for
 #include <h/mts.h>
 #include <h/utils.h>
 
-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", 0, NALIASW) \
+    X("list", 0, LISTSW) \
+    X("nolist", 0, NLISTSW) \
+    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;
 
@@ -44,33 +36,28 @@ extern struct aka *akahead;
 /*
  * prototypes
  */
-static void print_aka (char *, int, int);
-static void print_usr (char *, int, int);
+static void print_aka (char *, bool, int);
+static void print_usr (char *, bool);
 
 
 int
 main (int argc, char **argv)
 {
-    int i, vecp = 0, inverted = 0, list = 0;
-    int noalias = 0, normalize = AD_NHST;
+    int i, vecp = 0;
+    bool inverted, list, noalias;
     char *cp, **ap, **argp, buf[BUFSIZ];
     /* 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
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 1)) { return 1; }
 
-    mts_init (invo_name);
+    mts_init ();
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
+    inverted = list = noalias = false;
     while ((cp = *argp++)) {
        if (*cp == '-') {
            switch (smatch (++cp, switches)) {
@@ -96,28 +83,21 @@ main (int argc, char **argv)
                        adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
                    continue;
                case NALIASW: 
-                   noalias++;
+                   noalias = true;
                    continue;
 
                case LISTSW: 
-                   list++;
+                   list = true;
                    continue;
                case NLISTSW: 
-                   list = 0;
-                   continue;
-
-               case NORMSW: 
-                   normalize = AD_HOST;
-                   continue;
-               case NNORMSW: 
-                   normalize = AD_NHST;
+                   list = false;
                    continue;
 
                case USERSW: 
-                   inverted++;
+                   inverted = true;
                    continue;
                case NUSERSW: 
-                   inverted = 0;
+                   inverted = false;
                    continue;
            }
        }
@@ -136,11 +116,10 @@ main (int argc, char **argv)
        if ((cp = context_find ("Aliasfile"))) {
            char *dp = NULL;
 
-           for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
+           for (ap = brkstring(dp = mh_xstrdup(cp), " ", "\n"); ap && *ap; ap++)
                if ((i = alias (*ap)) != AK_OK)
                    adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
-           if (dp)
-               free(dp);
+            free(dp);
        }
        alias (AliasFile);
     }
@@ -154,7 +133,7 @@ main (int argc, char **argv)
                   invo_name);
 
        for (i = 0; i < vecp; i++)
-           print_usr (vec[i], list, normalize);
+           print_usr (vec[i], list);
     } else {
        if (vecp) {
            /* print specified aliases */
@@ -163,9 +142,13 @@ main (int argc, char **argv)
        } else {
            /* print them all */
            for (ak = akahead; ak; ak = ak->ak_next) {
+                char *res;
+
                printf ("%s: ", ak->ak_name);
                pos += strlen (ak->ak_name) + 1;
-               print_aka (akresult (ak), list, pos);
+                res = akresult(ak);
+               print_aka(res, list, pos);
+                free(res);
            }
        }
     }
@@ -176,12 +159,12 @@ main (int argc, char **argv)
 }
 
 static void
-print_aka (char *p, int list, int margin)
+print_aka (char *p, bool list, int margin)
 {
     char c;
 
     if (p == NULL) {
-       printf ("<empty>\n");
+       puts("<empty>");
        return;
     }
 
@@ -216,15 +199,15 @@ print_aka (char *p, int list, int margin)
 }
 
 static void
-print_usr (char *s, int list, int norm)
+print_usr (char *s, bool list)
 {
-    register char *cp, *pp, *vp;
-    register struct aka *ak;
-    register struct mailname *mp, *np;
+    char *cp, *pp, *vp;
+    struct aka *ak;
+    struct mailname *mp, *np;
 
     if ((pp = getname (s)) == NULL)
        adios (NULL, "no address in \"%s\"", s);
-    if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
+    if ((mp = getm (pp, NULL, 0, NULL, 0)) == NULL)
        adios (NULL, "bad address \"%s\"", s);
     while (getname (""))
        continue;
@@ -233,10 +216,12 @@ print_usr (char *s, int list, int norm)
     for (ak = akahead; ak; ak = ak->ak_next) {
        pp = akresult (ak);
        while ((cp = getname (pp))) {
-           if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
+           if ((np = getm (cp, NULL, 0, NULL, 0)) == NULL)
                continue;
-           if (!mh_strcasecmp (mp->m_host, np->m_host)
-                   && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
+           if (!strcasecmp (FENDNULL(mp->m_host),
+                            FENDNULL(np->m_host))  &&
+               !strcasecmp (FENDNULL(mp->m_mbox),
+                            FENDNULL(np->m_mbox))) {
                vp = vp ? add (ak->ak_name, add (",", vp))
                    : getcpy (ak->ak_name);
                mnfree (np);
@@ -249,13 +234,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);
+    free(vp);
 }