]> diplodocus.org Git - nmh/blobdiff - uip/ali.c
getpass.c: Move interface to own file.
[nmh] / uip / ali.c
index db536214f71788dfefe552a1147dfd2bb81dd068..229831b954d6f9932c9be9617d69d2033e1d2c01 100644 (file)
--- a/uip/ali.c
+++ b/uip/ali.c
@@ -1,17 +1,23 @@
-
-/*
- * 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
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/addrsbr.h>
-#include <h/aliasbr.h>
-#include <h/mts.h>
-#include <h/utils.h>
+#include "h/mh.h"
+#include "sbr/getcpy.h"
+#include "sbr/context_find.h"
+#include "sbr/brkstring.h"
+#include "sbr/ambigsw.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/error.h"
+#include "h/addrsbr.h"
+#include "h/aliasbr.h"
+#include "h/mts.h"
+#include "h/done.h"
+#include "h/utils.h"
 
 #define ALI_SWITCHES \
     X("alias aliasfile", 0, ALIASW) \
@@ -38,27 +44,28 @@ extern struct aka *akahead;
 /*
  * prototypes
  */
-static void print_aka (char *, int, int);
-static void print_usr (char *, 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;
+    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;
 
-    if (nmh_init(argv[0], 1)) { return 1; }
+    if (nmh_init(argv[0], true, true)) { return 1; }
 
     mts_init ();
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
 
+    inverted = list = noalias = false;
     while ((cp = *argp++)) {
        if (*cp == '-') {
            switch (smatch (++cp, switches)) {
@@ -66,7 +73,7 @@ main (int argc, char **argv)
                    ambigsw (cp, switches);
                    done (1);
                case UNKWNSW: 
-                   adios (NULL, "-%s unknown", cp);
+                   die("-%s unknown", cp);
 
                case HELPSW: 
                    snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
@@ -79,26 +86,26 @@ main (int argc, char **argv)
 
                case ALIASW: 
                    if (!(cp = *argp++) || *cp == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
+                       die("missing argument to %s", argp[-2]);
                    if ((i = alias (cp)) != AK_OK)
-                       adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
+                       die("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;
+                   list = false;
                    continue;
 
                case USERSW: 
-                   inverted++;
+                   inverted = true;
                    continue;
                case NUSERSW: 
-                   inverted = 0;
+                   inverted = false;
                    continue;
            }
        }
@@ -108,7 +115,7 @@ main (int argc, char **argv)
        } else {
            /* Should never happen, but try to protect against code changes
               that could allow it. */
-           adios (NULL, "too many arguments");
+           die("too many arguments");
        }
     }
 
@@ -117,11 +124,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);
+                   die("aliasing error in %s - %s", *ap, akerror (i));
+            free(dp);
        }
        alias (AliasFile);
     }
@@ -131,7 +137,7 @@ main (int argc, char **argv)
      */
     if (inverted) {
        if (vecp == 0)
-           adios (NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
+           die("usage: %s -user addresses ...  (you forgot the addresses)",
                   invo_name);
 
        for (i = 0; i < vecp; i++)
@@ -144,9 +150,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);
            }
        }
     }
@@ -157,12 +167,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;
     }
 
@@ -174,10 +184,10 @@ print_aka (char *p, int list, int margin)
                        printf ("\n%*s", margin, "");
                    else {
                        if (pos >= 68) {
-                           printf (",\n ");
+                           fputs(",\n ", stdout);
                            pos = 2;
                        } else {
-                           printf (", ");
+                           fputs(", ", stdout);
                            pos += 2;
                        }
                    }
@@ -197,16 +207,16 @@ print_aka (char *p, int list, int margin)
 }
 
 static void
-print_usr (char *s, int list)
+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);
+       die("no address in \"%s\"", s);
     if ((mp = getm (pp, NULL, 0, NULL, 0)) == NULL)
-       adios (NULL, "bad address \"%s\"", s);
+       die("bad address \"%s\"", s);
     while (getname (""))
        continue;
 
@@ -216,10 +226,10 @@ print_usr (char *s, int list)
        while ((cp = getname (pp))) {
            if ((np = getm (cp, NULL, 0, NULL, 0)) == NULL)
                continue;
-           if (!strcasecmp (mp->m_host ? mp->m_host : "",
-                            np->m_host ? np->m_host : "")  &&
-               !strcasecmp (mp->m_mbox ? mp->m_mbox : "",
-                            np->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);
@@ -234,6 +244,5 @@ print_usr (char *s, int list)
 
     print_aka (vp ? vp : s, list, 0);
 
-    if (vp)
-       free (vp);
+    free(vp);
 }