X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Fali.c;h=73f716685b6f68002a1c68dff8f35922c4deb15d;hp=bbbf0c1855bfce01bf3b72e7f96a47de76f725bb;hb=32b2354dbaf4bf934936eb5b102a4a3d2fdd209a;hpb=1691e80890e5d8ba258c51c214a3e91880e1db2b diff --git a/uip/ali.c b/uip/ali.c index bbbf0c1..73f7166 100644 --- a/uip/ali.c +++ b/uip/ali.c @@ -1,41 +1,43 @@ - /* - * ali.c -- list nmh mail aliases - * - * $Id$ - */ +** 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 #include #include +#include /* - * maximum number of names - */ -#define NVEC 50 +** 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", 4 }, - { NULL, 0 } +#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 } }; static int pos = 1; @@ -43,211 +45,205 @@ static int pos = 1; extern struct aka *akahead; /* - * prototypes - */ -void print_aka (char *, int, int); -void print_usr (char *, int, int); +** prototypes +*/ +static void print_aka(char *, int, int); +static void print_usr(char *, int, int); int -main (int argc, char **argv) +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; - struct aka *ak; + int i, vecp = 0, inverted = 0, list = 0; + int noalias = 0, normalize = AD_NHST; + char *cp, **ap, **argp, buf[BUFSIZ]; + char *vec[NVEC], **arguments; + struct aka *ak; #ifdef LOCALE - setlocale(LC_ALL, ""); + setlocale(LC_ALL, ""); #endif - invo_name = r1bindex (argv[0], '/'); - - /* read user profile/context */ - context_read(); - - mts_init (invo_name); - arguments = getarguments (invo_name, argc, argv, 1); - argp = arguments; - - while ((cp = *argp++)) { - if (*cp == '-') { - switch (smatch (++cp, switches)) { - case AMBIGSW: - ambigsw (cp, switches); - done (1); - case UNKWNSW: - adios (NULL, "-%s unknown", cp); - - case HELPSW: - snprintf (buf, sizeof(buf), "%s [switches] aliases ...", - invo_name); - print_help (buf, switches, 1); - done (1); - case VERSIONSW: - print_version (invo_name); - done (1); - - case ALIASW: - if (!(cp = *argp++) || *cp == '-') - adios (NULL, "missing argument to %s", argp[-2]); - if ((i = alias (cp)) != AK_OK) - adios (NULL, "aliasing error in %s - %s", cp, akerror (i)); - continue; - case NALIASW: - noalias++; - continue; - - case LISTSW: - list++; - continue; - case NLISTSW: - list = 0; - continue; - - case NORMSW: - normalize = AD_HOST; - continue; - case NNORMSW: - normalize = AD_NHST; - continue; - - case USERSW: - inverted++; - continue; - case NUSERSW: - inverted = 0; - continue; - } + invo_name = mhbasename(argv[0]); + + /* read user profile/context */ + context_read(); + + arguments = getarguments(invo_name, argc, argv, 1); + argp = arguments; + + while ((cp = *argp++)) { + if (*cp == '-') { + switch (smatch(++cp, switches)) { + case AMBIGSW: + ambigsw(cp, switches); + done(1); + case UNKWNSW: + adios(NULL, "-%s unknown", cp); + + case HELPSW: + snprintf(buf, sizeof(buf), "%s [switches] aliases ...", + invo_name); + print_help(buf, switches, 1); + done(1); + case VERSIONSW: + print_version(invo_name); + done(1); + + case ALIASW: + if (!(cp = *argp++) || *cp == '-') + adios(NULL, "missing argument to %s", argp[-2]); + if ((i = alias(cp)) != AK_OK) + adios(NULL, "aliasing error in %s - %s", cp, akerror(i)); + continue; + case NALIASW: + noalias++; + continue; + + case LISTSW: + list++; + continue; + case NLISTSW: + list = 0; + continue; + + case NORMSW: + normalize = AD_HOST; + continue; + case NNORMSW: + normalize = AD_NHST; + continue; + + case USERSW: + inverted++; + continue; + case NUSERSW: + inverted = 0; + continue; + } + } + vec[vecp++] = cp; + } + + if (!noalias) { + /* allow Aliasfile: profile entry */ + if ((cp = context_find("Aliasfile"))) { + char *dp = NULL; + + for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++) + if ((i = alias(etcpath(*ap))) != AK_OK) + adios(NULL, "aliasing error in %s - %s", *ap, akerror(i)); + if (dp) + free(dp); + } } - vec[vecp++] = cp; - } - - if (!noalias) { - /* allow Aliasfile: profile entry */ - if ((cp = context_find ("Aliasfile"))) { - char *dp = NULL; - - for (ap = brkstring(dp = getcpy(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); + + /* + ** If -user is specified + */ + if (inverted) { + if (vecp == 0) + adios(NULL, "usage: %s -user addresses ... (you forgot the addresses)", + invo_name); + + for (i = 0; i < vecp; i++) + print_usr(vec[i], list, normalize); + + done(0); } - alias (AliasFile); - } - - /* - * If -user is specified - */ - if (inverted) { - if (vecp == 0) - adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)", - invo_name); - - 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); + } } - } - done (0); + done(0); + return 1; } -void -print_aka (char *p, int list, int margin) +static void +print_aka(char *p, int list, int margin) { - char c; - - if (p == NULL) { - printf ("\n"); - return; - } - - while ((c = *p++)) { - switch (c) { - case ',': - if (*p) { - if (list) - printf ("\n%*s", margin, ""); - else { - if (pos >= 68) { - printf (",\n "); - pos = 2; - } else { - printf (", "); - pos += 2; + char c; + + if (p == NULL) { + printf("\n"); + return; + } + + while ((c = *p++)) { + switch (c) { + case ',': + if (*p) { + if (list) + printf("\n%*s", margin, ""); + else { + if (pos >= 68) { + printf(",\n "); + pos = 2; + } else { + printf(", "); + pos += 2; + } + } } - } - } - case 0: - break; + case 0: + break; - default: - pos++; - putchar (c); + default: + pos++; + putchar(c); + } } - } - putchar ('\n'); - pos = 1; + putchar('\n'); + pos = 1; } -void -print_usr (char *s, int list, int norm) +static void +print_usr(char *s, int list, int norm) { - register char *cp, *pp, *vp; - register struct aka *ak; - register 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) - adios (NULL, "bad address \"%s\"", s); - while (getname ("")) - continue; - - vp = NULL; - for (ak = akahead; ak; ak = ak->ak_next) { - pp = akresult (ak); - while ((cp = getname (pp))) { - if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL) + register char *cp, *pp, *vp; + register struct aka *ak; + register 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) + adios(NULL, "bad address \"%s\"", s); + while (getname("")) continue; - if (!strcasecmp (mp->m_host, np->m_host) - && !strcasecmp (mp->m_mbox, np->m_mbox)) { - vp = vp ? add (ak->ak_name, add (",", vp)) - : getcpy (ak->ak_name); - mnfree (np); - while (getname ("")) - continue; - break; - } - mnfree (np); + + vp = NULL; + for (ak = akahead; ak; ak = ak->ak_next) { + pp = akresult(ak); + while ((cp = getname(pp))) { + if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL) + continue; + 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); + while (getname("")) + continue; + break; + } + mnfree(np); + } } - } - 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 + mnfree(mp); + + print_aka(vp ? vp : s, list, 0); - if (vp) - free (vp); + if (vp) + free(vp); }