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 = mhbasename(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(etcpath(*ap))) != AK_OK)
136 adios(NULL, "aliasing error in %s - %s", *ap, akerror(i));
143 ** If -user is specified
147 adios(NULL, "usage: %s -user addresses ... (you forgot the addresses)",
150 for (i = 0; i < vecp; i++)
151 print_usr(vec[i], list, normalize);
157 /* print specified aliases */
158 for (i = 0; i < vecp; i++)
159 print_aka(akvalue(vec[i]), list, 0);
162 for (ak = akahead; ak; ak = ak->ak_next) {
163 printf("%s: ", ak->ak_name);
164 pos += strlen(ak->ak_name) + 1;
165 print_aka(akresult(ak), list, pos);
174 print_aka(char *p, int list, int margin)
188 printf("\n%*s", margin, "");
214 print_usr(char *s, int list, int norm)
216 register char *cp, *pp, *vp;
217 register struct aka *ak;
218 register struct mailname *mp, *np;
220 if ((pp = getname(s)) == NULL)
221 adios(NULL, "no address in \"%s\"", s);
222 if ((mp = getm(pp, NULL, 0, norm, NULL)) == NULL)
223 adios(NULL, "bad address \"%s\"", s);
228 for (ak = akahead; ak; ak = ak->ak_next) {
230 while ((cp = getname(pp))) {
231 if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
233 if (!mh_strcasecmp(mp->m_host, np->m_host)
234 && !mh_strcasecmp(mp->m_mbox, np->m_mbox)) {
235 vp = vp ? add(ak->ak_name, add(",", vp))
236 : getcpy(ak->ak_name);
247 print_aka(vp ? vp : s, list, 0);