Add test to check for correct handling of long lines
[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 char *version=VERSION;
44
45 static int pos = 1;
46
47 extern struct aka *akahead;
48
49 /*
50 ** prototypes
51 */
52 static void print_aka(char *, int, int);
53 static void print_usr(char *, int, int);
54
55
56 int
57 main(int argc, char **argv)
58 {
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;
63         struct aka *ak;
64
65         setlocale(LC_ALL, "");
66         invo_name = mhbasename(argv[0]);
67
68         /* read user profile/context */
69         context_read();
70
71         arguments = getarguments(invo_name, argc, argv, 1);
72         argp = arguments;
73
74         while ((cp = *argp++)) {
75                 if (*cp == '-') {
76                         switch (smatch(++cp, switches)) {
77                         case AMBIGSW:
78                                 ambigsw(cp, switches);
79                                 exit(EX_USAGE);
80                         case UNKWNSW:
81                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
82
83                         case HELPSW:
84                                 snprintf(buf, sizeof(buf), "%s [switches] aliases ...",
85                                         invo_name);
86                                 print_help(buf, switches, 1);
87                                 exit(argc == 2 ? EX_OK : EX_USAGE);
88                         case VERSIONSW:
89                                 print_version(invo_name);
90                                 exit(argc == 2 ? EX_OK : EX_USAGE);
91
92                         case FILESW:
93                                 if (!(cp = *argp++) || *cp == '-') {
94                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
95                                 }
96                                 if ((i = alias(cp)) != AK_OK)
97                                         adios(EX_USAGE, NULL, "aliasing error in %s: %s", cp, akerror(i));
98                                 deffiles = 0;
99                                 continue;
100
101                         case LISTSW:
102                                 list++;
103                                 continue;
104                         case NLISTSW:
105                                 list = 0;
106                                 continue;
107
108                         case NORMSW:
109                                 normalize = AD_HOST;
110                                 continue;
111                         case NNORMSW:
112                                 normalize = AD_NHST;
113                                 continue;
114
115                         case USERSW:
116                                 inverted++;
117                                 continue;
118                         case NUSERSW:
119                                 inverted = 0;
120                                 continue;
121                         }
122                 }
123                 vec[vecp++] = cp;
124         }
125
126         /* process default Aliasfile: profile entry */
127         if (deffiles && (cp = context_find("Aliasfile"))) {
128                 char *dp = NULL;
129
130                 for (ap = brkstring(dp=mh_xstrdup(cp), " ", "\n");
131                                 ap && *ap; ap++) {
132                         if ((i = alias(etcpath(*ap))) != AK_OK) {
133                                 adios(EX_DATAERR, NULL, "aliasing error in %s: %s",
134                                                 *ap, akerror(i));
135                         }
136                 }
137                 if (dp) {
138                         mh_free0(&dp);
139                 }
140         }
141
142         /*
143         ** If -user is specified
144         */
145         if (inverted) {
146                 if (vecp == 0)
147                         adios(EX_USAGE, NULL, "usage: %s -user addresses ...  (you forgot the addresses)",
148                                    invo_name);
149
150                 for (i = 0; i < vecp; i++)
151                         print_usr(vec[i], list, normalize);
152
153                 exit(EX_OK);
154         }
155
156         if (vecp) {
157                 /* print specified aliases */
158                 for (i = 0; i < vecp; i++)
159                         print_aka(akvalue(vec[i]), list, 0);
160         } else {
161                 /* print them all */
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);
166                 }
167         }
168
169         return EX_OK;
170 }
171
172 static void
173 print_aka(char *p, int list, int margin)
174 {
175         char c;
176
177         if (p == NULL) {
178                 printf("<empty>\n");
179                 return;
180         }
181
182         while ((c = *p++)) {
183                 switch (c) {
184                 case ',':
185                         if (*p) {
186                                 if (list)
187                                         printf("\n%*s", margin, "");
188                                 else {
189                                         if (pos >= 68) {
190                                                 printf(",\n ");
191                                                 pos = 2;
192                                         } else {
193                                                 printf(", ");
194                                                 pos += 2;
195                                         }
196                                 }
197                         }
198
199                 case 0:
200                         break;
201
202                 default:
203                         pos++;
204                         putchar(c);
205                 }
206         }
207
208         putchar('\n');
209         pos = 1;
210 }
211
212 static void
213 print_usr(char *s, int list, int norm)
214 {
215         char *cp, *pp, *vp;
216         struct aka *ak;
217         struct mailname *mp, *np;
218
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);
223         while (getname(""))
224                 continue;
225
226         vp = NULL;
227         for (ak = akahead; ak; ak = ak->ak_next) {
228                 pp = akresult(ak);
229                 while ((cp = getname(pp))) {
230                         if ((np = getm(cp, NULL, 0, norm, NULL)) == NULL)
231                                 continue;
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);
236                                 mnfree(np);
237                                 while (getname(""))
238                                         continue;
239                                 break;
240                         }
241                         mnfree(np);
242                 }
243         }
244         mnfree(mp);
245
246         print_aka(vp ? vp : s, list, 0);
247
248         if (vp)
249                 mh_free0(&vp);
250 }