3 * ali.c -- list nmh mail aliases
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
17 * maximum number of names
21 static struct swit switches[] = {
23 { "alias aliasfile", 0 },
47 extern struct aka *akahead;
52 static void print_aka (char *, int, int);
53 static void print_usr (char *, int, int);
57 main (int argc, char **argv)
59 int i, vecp = 0, inverted = 0, list = 0;
60 int noalias = 0, normalize = AD_NHST;
61 char *cp, **ap, **argp, buf[BUFSIZ];
62 char *vec[NVEC], **arguments;
66 setlocale(LC_ALL, "");
68 invo_name = r1bindex (argv[0], '/');
70 /* read user profile/context */
74 arguments = getarguments (invo_name, argc, argv, 1);
77 while ((cp = *argp++)) {
79 switch (smatch (++cp, switches)) {
81 ambigsw (cp, switches);
84 adios (NULL, "-%s unknown", cp);
87 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
89 print_help (buf, switches, 1);
92 print_version (invo_name);
96 if (!(cp = *argp++) || *cp == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
98 if ((i = alias (cp)) != AK_OK)
99 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
131 /* allow Aliasfile: profile entry */
132 if ((cp = context_find ("Aliasfile"))) {
135 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
136 if ((i = alias (*ap)) != AK_OK)
137 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
145 * If -user is specified
149 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
152 for (i = 0; i < vecp; i++)
153 print_usr (vec[i], list, normalize);
159 /* print specified aliases */
160 for (i = 0; i < vecp; i++)
161 print_aka (akvalue (vec[i]), list, 0);
164 for (ak = akahead; ak; ak = ak->ak_next) {
165 printf ("%s: ", ak->ak_name);
166 pos += strlen (ak->ak_name) + 1;
167 print_aka (akresult (ak), list, pos);
176 print_aka (char *p, int list, int margin)
181 printf ("<empty>\n");
190 printf ("\n%*s", margin, "");
216 print_usr (char *s, int list, int norm)
218 register char *cp, *pp, *vp;
219 register struct aka *ak;
220 register struct mailname *mp, *np;
222 if ((pp = getname (s)) == NULL)
223 adios (NULL, "no address in \"%s\"", s);
224 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
225 adios (NULL, "bad address \"%s\"", s);
230 for (ak = akahead; ak; ak = ak->ak_next) {
232 while ((cp = getname (pp))) {
233 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
235 if (!mh_strcasecmp (mp->m_host, np->m_host)
236 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
237 vp = vp ? add (ak->ak_name, add (",", vp))
238 : getcpy (ak->ak_name);
251 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
253 print_aka (vp ? vp : s, list, 0);