]> diplodocus.org Git - nmh/blob - uip/ali.c
Document argsplit changes in mh-profile man page.
[nmh] / uip / ali.c
1
2 /*
3 * ali.c -- list nmh mail aliases
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/aliasbr.h>
13 #include <h/mts.h>
14 #include <h/utils.h>
15
16 #define ALI_SWITCHES \
17 X("alias aliasfile", 0, ALIASW) \
18 X("noalias", -7, NALIASW) \
19 X("list", 0, LISTSW) \
20 X("nolist", 0, NLISTSW) \
21 X("normalize", 0, NORMSW) \
22 X("nonormalize", 0, NNORMSW) \
23 X("user", 0, USERSW) \
24 X("nouser", 0, NUSERSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(ALI);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(ALI, switches);
34 #undef X
35
36 static int pos = 1;
37
38 extern struct aka *akahead;
39
40 /*
41 * prototypes
42 */
43 static void print_aka (char *, int, int);
44 static void print_usr (char *, int, int);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 int i, vecp = 0, inverted = 0, list = 0;
51 int noalias = 0, normalize = AD_NHST;
52 char *cp, **ap, **argp, buf[BUFSIZ];
53 /* Really only need to allocate for argc-1, but must allocate at least 1,
54 so go ahead and allocate for argc char pointers. */
55 char **vec = mh_xmalloc (argc * sizeof (char *)), **arguments;
56 struct aka *ak;
57
58 #ifdef LOCALE
59 setlocale(LC_ALL, "");
60 #endif
61 invo_name = r1bindex (argv[0], '/');
62
63 /* read user profile/context */
64 context_read();
65
66 mts_init (invo_name);
67 arguments = getarguments (invo_name, argc, argv, 1);
68 argp = arguments;
69
70 while ((cp = *argp++)) {
71 if (*cp == '-') {
72 switch (smatch (++cp, switches)) {
73 case AMBIGSW:
74 ambigsw (cp, switches);
75 done (1);
76 case UNKWNSW:
77 adios (NULL, "-%s unknown", cp);
78
79 case HELPSW:
80 snprintf (buf, sizeof(buf), "%s [switches] aliases ...",
81 invo_name);
82 print_help (buf, switches, 1);
83 done (0);
84 case VERSIONSW:
85 print_version (invo_name);
86 done (0);
87
88 case ALIASW:
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));
93 continue;
94 case NALIASW:
95 noalias++;
96 continue;
97
98 case LISTSW:
99 list++;
100 continue;
101 case NLISTSW:
102 list = 0;
103 continue;
104
105 case NORMSW:
106 normalize = AD_HOST;
107 continue;
108 case NNORMSW:
109 normalize = AD_NHST;
110 continue;
111
112 case USERSW:
113 inverted++;
114 continue;
115 case NUSERSW:
116 inverted = 0;
117 continue;
118 }
119 }
120
121 if (vecp < argc) {
122 vec[vecp++] = cp;
123 } else {
124 /* Should never happen, but try to protect against code changes
125 that could allow it. */
126 adios (NULL, "too many arguments");
127 }
128 }
129
130 if (!noalias) {
131 /* allow Aliasfile: profile entry */
132 if ((cp = context_find ("Aliasfile"))) {
133 char *dp = NULL;
134
135 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
136 if ((i = alias (*ap)) != AK_OK)
137 adios (NULL, "aliasing error in %s - %s", *ap, akerror (i));
138 if (dp)
139 free(dp);
140 }
141 alias (AliasFile);
142 }
143
144 /*
145 * If -user is specified
146 */
147 if (inverted) {
148 if (vecp == 0)
149 adios (NULL, "usage: %s -user addresses ... (you forgot the addresses)",
150 invo_name);
151
152 for (i = 0; i < vecp; i++)
153 print_usr (vec[i], list, normalize);
154 } else {
155 if (vecp) {
156 /* print specified aliases */
157 for (i = 0; i < vecp; i++)
158 print_aka (akvalue (vec[i]), list, 0);
159 } else {
160 /* print them all */
161 for (ak = akahead; ak; ak = ak->ak_next) {
162 printf ("%s: ", ak->ak_name);
163 pos += strlen (ak->ak_name) + 1;
164 print_aka (akresult (ak), list, pos);
165 }
166 }
167 }
168
169 free (vec);
170 done (0);
171 return 1;
172 }
173
174 static void
175 print_aka (char *p, int list, int margin)
176 {
177 char c;
178
179 if (p == NULL) {
180 printf ("<empty>\n");
181 return;
182 }
183
184 while ((c = *p++)) {
185 switch (c) {
186 case ',':
187 if (*p) {
188 if (list)
189 printf ("\n%*s", margin, "");
190 else {
191 if (pos >= 68) {
192 printf (",\n ");
193 pos = 2;
194 } else {
195 printf (", ");
196 pos += 2;
197 }
198 }
199 }
200
201 case 0:
202 break;
203
204 default:
205 pos++;
206 putchar (c);
207 }
208 }
209
210 putchar ('\n');
211 pos = 1;
212 }
213
214 static void
215 print_usr (char *s, int list, int norm)
216 {
217 register char *cp, *pp, *vp;
218 register struct aka *ak;
219 register struct mailname *mp, *np;
220
221 if ((pp = getname (s)) == NULL)
222 adios (NULL, "no address in \"%s\"", s);
223 if ((mp = getm (pp, NULL, 0, norm, NULL)) == NULL)
224 adios (NULL, "bad address \"%s\"", s);
225 while (getname (""))
226 continue;
227
228 vp = NULL;
229 for (ak = akahead; ak; ak = ak->ak_next) {
230 pp = akresult (ak);
231 while ((cp = getname (pp))) {
232 if ((np = getm (cp, NULL, 0, norm, NULL)) == NULL)
233 continue;
234 if (!mh_strcasecmp (mp->m_host, np->m_host)
235 && !mh_strcasecmp (mp->m_mbox, np->m_mbox)) {
236 vp = vp ? add (ak->ak_name, add (",", vp))
237 : getcpy (ak->ak_name);
238 mnfree (np);
239 while (getname (""))
240 continue;
241 break;
242 }
243 mnfree (np);
244 }
245 }
246 mnfree (mp);
247
248 print_aka (vp ? vp : s, list, 0);
249
250 if (vp)
251 free (vp);
252 }