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>
17 ** maximum number of names
21 static struct swit switches[] = {
23 { "file aliasfile", 0 },
43 char *version=VERSION;
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 deffiles = 1, normalize = AD_NHST;
61 char *cp, **ap, **argp, buf[BUFSIZ];
62 char *vec[NVEC], **arguments;
65 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
68 /* 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(EX_USAGE, NULL, "-%s unknown", cp);
84 snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
86 print_help(buf, switches, 1);
87 exit(argc == 2 ? EX_OK : EX_USAGE);
89 print_version(invo_name);
90 exit(argc == 2 ? EX_OK : EX_USAGE);
93 if (!(cp = *argp++) || *cp == '-') {
94 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
96 if ((i = alias(cp)) != AK_OK)
97 adios(EX_USAGE, NULL, "aliasing error in %s: %s", cp, akerror(i));
126 /* process default Aliasfile: profile entry */
127 if (deffiles && (cp = context_find("Aliasfile"))) {
130 for (ap = brkstring(dp=mh_xstrdup(cp), " ", "\n");
132 if ((i = alias(etcpath(*ap))) != AK_OK) {
133 adios(EX_DATAERR, NULL, "aliasing error in %s: %s",
143 ** If -user is specified
147 adios(EX_USAGE, 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);
173 print_aka(char *p, int list, int margin)
187 printf("\n%*s", margin, "");
213 print_usr(char *s, int list, int norm)
217 struct mailname *mp, *np;
219 if ((pp = getname(s)) == NULL)
220 adios(EX_DATAERR, NULL, "no address in \"%s\"", s);
221 if ((mp = getm(pp, NULL, 0, norm, NULL)) == NULL)
222 adios(EX_DATAERR, NULL, "bad address \"%s\"", s);
227 for (ak = akahead; ak; ak = ak->ak_next) {
229 while ((cp = getname(pp))) {
230 if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
232 if (!mh_strcasecmp(mp->m_host, np->m_host)
233 && !mh_strcasecmp(mp->m_mbox, np->m_mbox)) {
234 vp = vp ? add(ak->ak_name, add(",", vp))
235 : mh_xstrdup(ak->ak_name);
246 print_aka(vp ? vp : s, list, 0);