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>
15 ** maximum number of names
19 static struct swit switches[] = {
21 { "file aliasfile", 0 },
43 extern struct aka *akahead;
48 static void print_aka(char *, int, int);
49 static void print_usr(char *, int, int);
53 main(int argc, char **argv)
55 int i, vecp = 0, inverted = 0, list = 0;
56 int deffiles = 1, normalize = AD_NHST;
57 char *cp, **ap, **argp, buf[BUFSIZ];
58 char *vec[NVEC], **arguments;
61 setlocale(LC_ALL, "");
62 invo_name = mhbasename(argv[0]);
64 /* read user profile/context */
67 arguments = getarguments(invo_name, argc, argv, 1);
70 while ((cp = *argp++)) {
72 switch (smatch(++cp, switches)) {
74 ambigsw(cp, switches);
77 adios(NULL, "-%s unknown", cp);
80 snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
82 print_help(buf, switches, 1);
85 print_version(invo_name);
89 if (!(cp = *argp++) || *cp == '-')
90 adios(NULL, "missing argument to %s", argp[-2]);
91 if ((i = alias(cp)) != AK_OK)
92 adios(NULL, "aliasing error in %s: %s", cp, akerror(i));
121 /* process default Aliasfile: profile entry */
122 if (deffiles && (cp = context_find("Aliasfile"))) {
125 for (ap = brkstring(dp=getcpy(cp), " ", "\n");
127 if ((i = alias(etcpath(*ap))) != AK_OK) {
128 adios(NULL, "aliasing error in %s: %s",
138 ** If -user is specified
142 adios(NULL, "usage: %s -user addresses ... (you forgot the addresses)",
145 for (i = 0; i < vecp; i++)
146 print_usr(vec[i], list, normalize);
152 /* print specified aliases */
153 for (i = 0; i < vecp; i++)
154 print_aka(akvalue(vec[i]), list, 0);
157 for (ak = akahead; ak; ak = ak->ak_next) {
158 printf("%s: ", ak->ak_name);
159 pos += strlen(ak->ak_name) + 1;
160 print_aka(akresult(ak), list, pos);
169 print_aka(char *p, int list, int margin)
183 printf("\n%*s", margin, "");
209 print_usr(char *s, int list, int norm)
211 register char *cp, *pp, *vp;
212 register struct aka *ak;
213 register struct mailname *mp, *np;
215 if ((pp = getname(s)) == NULL)
216 adios(NULL, "no address in \"%s\"", s);
217 if ((mp = getm(pp, NULL, 0, norm, NULL)) == NULL)
218 adios(NULL, "bad address \"%s\"", s);
223 for (ak = akahead; ak; ak = ak->ak_next) {
225 while ((cp = getname(pp))) {
226 if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
228 if (!mh_strcasecmp(mp->m_host, np->m_host)
229 && !mh_strcasecmp(mp->m_mbox, np->m_mbox)) {
230 vp = vp ? add(ak->ak_name, add(",", vp))
231 : getcpy(ak->ak_name);
242 print_aka(vp ? vp : s, list, 0);