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>
16 static struct swit switches[] = {
18 { "alias aliasfile", 0 },
42 extern struct aka *akahead;
47 static void print_aka (char *, int, int);
48 static void print_usr (char *, int, int);
52 main (int argc, char **argv)
54 int i, vecp = 0, inverted = 0, list = 0;
55 int noalias = 0, normalize = AD_NHST;
56 char *cp, **ap, **argp, buf[BUFSIZ];
57 /* Really only need to allocate for argc-1, but must allocate at least 1,
58 so go ahead and allocate for argc char pointers. */
59 char **vec = mh_xmalloc (argc * sizeof (char *)), **arguments;
63 setlocale(LC_ALL, "");
65 invo_name = r1bindex (argv[0], '/');
67 /* read user profile/context */
71 arguments = getarguments (invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch (++cp, switches)) {
78 ambigsw (cp, switches);
81 adios (NULL, "-%s unknown", cp);
84 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
86 print_help (buf, switches, 1);
89 print_version (invo_name);
93 if (!(cp = *argp++) || *cp == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
95 if ((i = alias (cp)) != AK_OK)
96 adios (NULL, "aliasing error in %s - %s", cp, akerror (i));
128 /* Should never happen, but try to protect against code changes
129 that could allow it. */
130 adios (NULL, "too many arguments");
135 /* allow Aliasfile: profile entry */
136 if ((cp = context_find ("Aliasfile"))) {
139 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
140 if ((i = alias (*ap)) != AK_OK)
141 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
149 * If -user is specified
153 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
156 for (i = 0; i < vecp; i++)
157 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);
179 print_aka (char *p, int list, int margin)
184 printf ("<empty>\n");
193 printf ("\n%*s", margin, "");
219 print_usr (char *s, int list, int norm)
221 register char *cp, *pp, *vp;
222 register struct aka *ak;
223 register struct mailname *mp, *np;
225 if ((pp = getname (s)) == NULL)
226 adios (NULL, "no address in \"%s\"", s);
227 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
228 adios (NULL, "bad address \"%s\"", s);
233 for (ak = akahead; ak; ak = ak->ak_next) {
235 while ((cp = getname (pp))) {
236 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
238 if (!mh_strcasecmp (mp->m_host, np->m_host)
239 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
240 vp = vp ? add (ak->ak_name, add (",", vp))
241 : getcpy (ak->ak_name);
254 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
256 print_aka (vp ? vp : s, list, 0);