2 * ali.c -- list nmh mail aliases
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/addrsbr.h>
11 #include <h/aliasbr.h>
16 * maximum number of names
20 static struct swit switches[] = {
22 { "alias aliasfile", 0 },
46 extern struct aka *akahead;
51 static void print_aka (char *, int, int);
52 static void print_usr (char *, int, int);
56 main (int argc, char **argv)
58 int i, vecp = 0, inverted = 0, list = 0;
59 int noalias = 0, normalize = AD_NHST;
60 char *cp, **ap, **argp, buf[BUFSIZ];
61 char *vec[NVEC], **arguments;
65 setlocale(LC_ALL, "");
67 invo_name = r1bindex (argv[0], '/');
69 /* read user profile/context */
73 arguments = getarguments (invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch (++cp, switches)) {
80 ambigsw (cp, switches);
83 adios (NULL, "-%s unknown", cp);
86 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
88 print_help (buf, switches, 1);
91 print_version (invo_name);
95 if (!(cp = *argp++) || *cp == '-')
96 adios (NULL, "missing argument to %s", argp[-2]);
97 if ((i = alias (cp)) != AK_OK)
98 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
130 /* allow Aliasfile: profile entry */
131 if ((cp = context_find ("Aliasfile"))) {
134 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
135 if ((i = alias (*ap)) != AK_OK)
136 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
144 * If -user is specified
148 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
151 for (i = 0; i < vecp; i++)
152 print_usr (vec[i], list, normalize);
158 /* print specified aliases */
159 for (i = 0; i < vecp; i++)
160 print_aka (akvalue (vec[i]), list, 0);
163 for (ak = akahead; ak; ak = ak->ak_next) {
164 printf ("%s: ", ak->ak_name);
165 pos += strlen (ak->ak_name) + 1;
166 print_aka (akresult (ak), list, pos);
175 print_aka (char *p, int list, int margin)
180 printf ("<empty>\n");
189 printf ("\n%*s", margin, "");
215 print_usr (char *s, int list, int norm)
217 register char *cp, *pp, *vp;
218 register struct aka *ak;
219 register struct mailname *mp, *np;
221 if ((pp = getname (s)) == NULL)
222 adios (NULL, "no address in \"%s\"", s);
223 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
224 adios (NULL, "bad address \"%s\"", s);
229 for (ak = akahead; ak; ak = ak->ak_next) {
231 while ((cp = getname (pp))) {
232 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
234 if (!mh_strcasecmp (mp->m_host, np->m_host)
235 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
236 vp = vp ? add (ak->ak_name, add (",", vp))
237 : getcpy (ak->ak_name);
250 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
252 print_aka (vp ? vp : s, list, 0);