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 },
45 extern struct aka *akahead;
50 static void print_aka(char *, int, int);
51 static void print_usr(char *, int, int);
55 main(int argc, char **argv)
57 int i, vecp = 0, inverted = 0, list = 0;
58 int deffiles = 1, normalize = AD_NHST;
59 char *cp, **ap, **argp, buf[BUFSIZ];
60 char *vec[NVEC], **arguments;
63 setlocale(LC_ALL, "");
64 invo_name = mhbasename(argv[0]);
66 /* read user profile/context */
69 arguments = getarguments(invo_name, argc, argv, 1);
72 while ((cp = *argp++)) {
74 switch (smatch(++cp, switches)) {
76 ambigsw(cp, switches);
79 adios(EX_USAGE, NULL, "-%s unknown", cp);
82 snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
84 print_help(buf, switches, 1);
85 exit(argc == 2 ? EX_OK : EX_USAGE);
87 print_version(invo_name);
88 exit(argc == 2 ? EX_OK : EX_USAGE);
91 if (!(cp = *argp++) || *cp == '-') {
92 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
94 if ((i = alias(cp)) != AK_OK)
95 adios(EX_USAGE, NULL, "aliasing error in %s: %s", cp, akerror(i));
124 /* process default Aliasfile: profile entry */
125 if (deffiles && (cp = context_find("Aliasfile"))) {
128 for (ap = brkstring(dp=getcpy(cp), " ", "\n");
130 if ((i = alias(etcpath(*ap))) != AK_OK) {
131 adios(EX_DATAERR, NULL, "aliasing error in %s: %s",
141 ** If -user is specified
145 adios(EX_USAGE, NULL, "usage: %s -user addresses ... (you forgot the addresses)",
148 for (i = 0; i < vecp; i++)
149 print_usr(vec[i], list, normalize);
155 /* print specified aliases */
156 for (i = 0; i < vecp; i++)
157 print_aka(akvalue(vec[i]), list, 0);
160 for (ak = akahead; ak; ak = ak->ak_next) {
161 printf("%s: ", ak->ak_name);
162 pos += strlen(ak->ak_name) + 1;
163 print_aka(akresult(ak), list, pos);
171 print_aka(char *p, int list, int margin)
185 printf("\n%*s", margin, "");
211 print_usr(char *s, int list, int norm)
215 struct mailname *mp, *np;
217 if ((pp = getname(s)) == NULL)
218 adios(EX_DATAERR, NULL, "no address in \"%s\"", s);
219 if ((mp = getm(pp, NULL, 0, norm, NULL)) == NULL)
220 adios(EX_DATAERR, NULL, "bad address \"%s\"", s);
225 for (ak = akahead; ak; ak = ak->ak_next) {
227 while ((cp = getname(pp))) {
228 if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
230 if (!mh_strcasecmp(mp->m_host, np->m_host)
231 && !mh_strcasecmp(mp->m_mbox, np->m_mbox)) {
232 vp = vp ? add(ak->ak_name, add(",", vp))
233 : getcpy(ak->ak_name);
244 print_aka(vp ? vp : s, list, 0);