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>
18 * maximum number of names
22 static struct swit switches[] = {
24 { "alias aliasfile", 0 },
48 extern struct aka *akahead;
53 void print_aka (char *, int, int);
54 void print_usr (char *, int, int);
58 main (int argc, char **argv)
60 int i, vecp = 0, inverted = 0, list = 0;
61 int noalias = 0, normalize = AD_NHST;
62 char *cp, **ap, **argp, buf[BUFSIZ];
63 char *vec[NVEC], **arguments;
67 setlocale(LC_ALL, "");
69 invo_name = r1bindex (argv[0], '/');
71 /* read user profile/context */
75 arguments = getarguments (invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch (++cp, switches)) {
82 ambigsw (cp, switches);
85 adios (NULL, "-%s unknown", cp);
88 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
90 print_help (buf, switches, 1);
93 print_version (invo_name);
97 if (!(cp = *argp++) || *cp == '-')
98 adios (NULL, "missing argument to %s", argp[-2]);
99 if ((i = alias (cp)) != AK_OK)
100 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
132 /* allow Aliasfile: profile entry */
133 if ((cp = context_find ("Aliasfile"))) {
136 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
137 if ((i = alias (*ap)) != AK_OK)
138 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
146 * If -user is specified
150 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
153 for (i = 0; i < vecp; i++)
154 print_usr (vec[i], list, normalize);
160 /* print specified aliases */
161 for (i = 0; i < vecp; i++)
162 print_aka (akvalue (vec[i]), list, 0);
165 for (ak = akahead; ak; ak = ak->ak_next) {
166 printf ("%s: ", ak->ak_name);
167 pos += strlen (ak->ak_name) + 1;
168 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 (!strcasecmp (mp->m_host, np->m_host)
236 && !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);