3 * ali.c -- list nmh mail aliases
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/aliasbr.h>
19 * maximum number of names
23 static struct swit switches[] = {
25 { "alias aliasfile", 0 },
49 extern struct aka *akahead;
54 static void print_aka (char *, int, int);
55 static void print_usr (char *, int, int);
59 main (int argc, char **argv)
61 int i, vecp = 0, inverted = 0, list = 0;
62 int noalias = 0, normalize = AD_NHST;
63 char *cp, **ap, **argp, buf[BUFSIZ];
64 char *vec[NVEC], **arguments;
68 setlocale(LC_ALL, "");
70 invo_name = r1bindex (argv[0], '/');
72 /* read user profile/context */
76 arguments = getarguments (invo_name, argc, argv, 1);
79 while ((cp = *argp++)) {
81 switch (smatch (++cp, switches)) {
83 ambigsw (cp, switches);
86 adios (NULL, "-%s unknown", cp);
89 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
91 print_help (buf, switches, 1);
94 print_version (invo_name);
98 if (!(cp = *argp++) || *cp == '-')
99 adios (NULL, "missing argument to %s", argp[-2]);
100 if ((i = alias (cp)) != AK_OK)
101 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
133 /* allow Aliasfile: profile entry */
134 if ((cp = context_find ("Aliasfile"))) {
137 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
138 if ((i = alias (*ap)) != AK_OK)
139 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
147 * If -user is specified
151 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
154 for (i = 0; i < vecp; i++)
155 print_usr (vec[i], list, normalize);
161 /* print specified aliases */
162 for (i = 0; i < vecp; i++)
163 print_aka (akvalue (vec[i]), list, 0);
166 for (ak = akahead; ak; ak = ak->ak_next) {
167 printf ("%s: ", ak->ak_name);
168 pos += strlen (ak->ak_name) + 1;
169 print_aka (akresult (ak), list, pos);
178 print_aka (char *p, int list, int margin)
183 printf ("<empty>\n");
192 printf ("\n%*s", margin, "");
218 print_usr (char *s, int list, int norm)
220 register char *cp, *pp, *vp;
221 register struct aka *ak;
222 register struct mailname *mp, *np;
224 if ((pp = getname (s)) == NULL)
225 adios (NULL, "no address in \"%s\"", s);
226 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
227 adios (NULL, "bad address \"%s\"", s);
232 for (ak = akahead; ak; ak = ak->ak_next) {
234 while ((cp = getname (pp))) {
235 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
237 if (!mh_strcasecmp (mp->m_host, np->m_host)
238 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
239 vp = vp ? add (ak->ak_name, add (",", vp))
240 : getcpy (ak->ak_name);
253 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
255 print_aka (vp ? vp : s, list, 0);