Drop register storage class
[mmh] / uip / ali.c
1 /*
2 ** ali.c -- list nmh mail aliases
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/addrsbr.h>
11 #include <h/aliasbr.h>
12 #include <h/utils.h>
13 #include <locale.h>
14 #include <sysexits.h>
15
16 /*
17 ** maximum number of names
18 */
19 #define NVEC 50
20
21 static struct swit switches[] = {
22 #define FILESW     0
23         { "file aliasfile", 0 },
24 #define LISTSW     1
25         { "list", 0 },
26 #define NLISTSW    2
27         { "nolist", 2 },
28 #define NORMSW     3
29         { "normalize", 0 },
30 #define NNORMSW    4
31         { "nonormalize", 2 },
32 #define USERSW     5
33         { "user", 0 },
34 #define NUSERSW    6
35         { "nouser", 2 },
36 #define VERSIONSW  7
37         { "Version", 0 },
38 #define HELPSW     8
39         { "help", 0 },
40         { NULL, 0 }
41 };
42
43 static int pos = 1;
44
45 extern struct aka *akahead;
46
47 /*
48 ** prototypes
49 */
50 static void print_aka(char *, int, int);
51 static void print_usr(char *, int, int);
52
53
54 int
55 main(int argc, char **argv)
56 {
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;
61         struct aka *ak;
62
63         setlocale(LC_ALL, "");
64         invo_name = mhbasename(argv[0]);
65
66         /* read user profile/context */
67         context_read();
68
69         arguments = getarguments(invo_name, argc, argv, 1);
70         argp = arguments;
71
72         while ((cp = *argp++)) {
73                 if (*cp == '-') {
74                         switch (smatch(++cp, switches)) {
75                         case AMBIGSW:
76                                 ambigsw(cp, switches);
77                                 exit(EX_USAGE);
78                         case UNKWNSW:
79                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
80
81                         case HELPSW:
82                                 snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
83                                         invo_name);
84                                 print_help(buf, switches, 1);
85                                 exit(argc == 2 ? EX_OK : EX_USAGE);
86                         case VERSIONSW:
87                                 print_version(invo_name);
88                                 exit(argc == 2 ? EX_OK : EX_USAGE);
89
90                         case FILESW:
91                                 if (!(cp = *argp++) || *cp == '-') {
92                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
93                                 }
94                                 if ((i = alias(cp)) != AK_OK)
95                                         adios(EX_USAGE, NULL, "aliasing error in %s: %s", cp, akerror(i));
96                                 deffiles = 0;
97                                 continue;
98
99                         case LISTSW:
100                                 list++;
101                                 continue;
102                         case NLISTSW:
103                                 list = 0;
104                                 continue;
105
106                         case NORMSW:
107                                 normalize = AD_HOST;
108                                 continue;
109                         case NNORMSW:
110                                 normalize = AD_NHST;
111                                 continue;
112
113                         case USERSW:
114                                 inverted++;
115                                 continue;
116                         case NUSERSW:
117                                 inverted = 0;
118                                 continue;
119                         }
120                 }
121                 vec[vecp++] = cp;
122         }
123
124         /* process default Aliasfile: profile entry */
125         if (deffiles && (cp = context_find("Aliasfile"))) {
126                 char *dp = NULL;
127
128                 for (ap = brkstring(dp=getcpy(cp), " ", "\n");
129                                 ap && *ap; ap++) {
130                         if ((i = alias(etcpath(*ap))) != AK_OK) {
131                                 adios(EX_DATAERR, NULL, "aliasing error in %s: %s",
132                                                 *ap, akerror(i));
133                         }
134                 }
135                 if (dp) {
136                         free(dp);
137                 }
138         }
139
140         /*
141         ** If -user is specified
142         */
143         if (inverted) {
144                 if (vecp == 0)
145                         adios(EX_USAGE, NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
146                                    invo_name);
147
148                 for (i = 0; i < vecp; i++)
149                         print_usr(vec[i], list, normalize);
150
151                 exit(EX_OK);
152         }
153
154         if (vecp) {
155                 /* print specified aliases */
156                 for (i = 0; i < vecp; i++)
157                         print_aka(akvalue(vec[i]), list, 0);
158         } else {
159                 /* print them all */
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);
164                 }
165         }
166
167         return EX_OK;
168 }
169
170 static void
171 print_aka(char *p, int list, int margin)
172 {
173         char c;
174
175         if (p == NULL) {
176                 printf("<empty>\n");
177                 return;
178         }
179
180         while ((c = *p++)) {
181                 switch (c) {
182                 case ',':
183                         if (*p) {
184                                 if (list)
185                                         printf("\n%*s", margin, "");
186                                 else {
187                                         if (pos >= 68) {
188                                                 printf(",\n ");
189                                                 pos = 2;
190                                         } else {
191                                                 printf(", ");
192                                                 pos += 2;
193                                         }
194                                 }
195                         }
196
197                 case 0:
198                         break;
199
200                 default:
201                         pos++;
202                         putchar(c);
203                 }
204         }
205
206         putchar('\n');
207         pos = 1;
208 }
209
210 static void
211 print_usr(char *s, int list, int norm)
212 {
213         char *cp, *pp, *vp;
214         struct aka *ak;
215         struct mailname *mp, *np;
216
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);
221         while (getname(""))
222                 continue;
223
224         vp = NULL;
225         for (ak = akahead; ak; ak = ak->ak_next) {
226                 pp = akresult(ak);
227                 while ((cp = getname(pp))) {
228                         if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
229                                 continue;
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);
234                                 mnfree(np);
235                                 while (getname(""))
236                                         continue;
237                                 break;
238                         }
239                         mnfree(np);
240                 }
241         }
242         mnfree(mp);
243
244         print_aka(vp ? vp : s, list, 0);
245
246         if (vp)
247                 free(vp);
248 }