0
|
1 /* ali.c - the new ali */
|
|
2 #ifndef lint
|
|
3 static char ident[] = "@(#)$Id$";
|
|
4 #endif /* lint */
|
|
5
|
|
6 #include "../h/mh.h"
|
|
7 #include "../h/addrsbr.h"
|
|
8 #include "../h/aliasbr.h"
|
|
9 #include <stdio.h>
|
|
10 #ifdef LOCALE
|
|
11 #include <locale.h>
|
|
12 #endif
|
|
13
|
|
14
|
|
15 #define NVEC 50 /* maximum number of names */
|
|
16
|
|
17 /* */
|
|
18
|
|
19 static struct swit switches[] = {
|
|
20 #define ALIASW 0
|
|
21 "alias aliasfile", 0,
|
|
22 #define NALIASW 1
|
|
23 "noalias", -7,
|
|
24
|
|
25 #define LISTSW 2
|
|
26 "list", 0,
|
|
27 #define NLISTSW 3
|
|
28 "nolist", 0,
|
|
29
|
|
30 #define NORMSW 4
|
|
31 "normalize", 0,
|
|
32 #define NNORMSW 5
|
|
33 "nonormalize", 0,
|
|
34
|
|
35 #define USERSW 6
|
|
36 "user", 0,
|
|
37 #define NUSERSW 7
|
|
38 "nouser", 0,
|
|
39
|
|
40 #define HELPSW 8
|
|
41 "help", 4,
|
|
42
|
|
43 NULL, 0
|
|
44 };
|
|
45
|
|
46 /* */
|
|
47
|
|
48 static int pos = 1;
|
|
49
|
|
50 extern struct aka *akahead;
|
|
51
|
|
52 /* */
|
|
53
|
|
54 /* ARGSUSED */
|
|
55
|
|
56 main (argc, argv)
|
|
57 int argc;
|
|
58 char *argv[];
|
|
59 {
|
|
60 int i,
|
|
61 vecp = 0,
|
|
62 inverted = 0,
|
|
63 list = 0,
|
|
64 noalias = 0,
|
|
65 normalize = AD_NHST;
|
|
66 char *cp,
|
|
67 **ap,
|
|
68 **argp,
|
|
69 buf[100],
|
|
70 *vec[NVEC],
|
|
71 *arguments[MAXARGS];
|
|
72 struct aka *ak;
|
|
73
|
|
74 #ifdef LOCALE
|
|
75 setlocale(LC_ALL, "");
|
|
76 #endif
|
|
77 #ifdef JAPAN
|
|
78 ml_init();
|
|
79 #endif /* JAPAN */
|
|
80 invo_name = r1bindex (argv[0], '/');
|
|
81 mts_init (invo_name);
|
|
82 if ((cp = m_find (invo_name)) != NULL) {
|
|
83 ap = brkstring (cp = getcpy (cp), " ", "\n");
|
|
84 ap = copyip (ap, arguments);
|
|
85 }
|
|
86 else
|
|
87 ap = arguments;
|
|
88 (void) copyip (argv + 1, ap);
|
|
89 argp = arguments;
|
|
90
|
|
91 /* */
|
|
92
|
|
93 while (cp = *argp++) {
|
|
94 if (*cp == '-')
|
|
95 switch (smatch (++cp, switches)) {
|
|
96 case AMBIGSW:
|
|
97 ambigsw (cp, switches);
|
|
98 done (1);
|
|
99 case UNKWNSW:
|
|
100 adios (NULLCP, "-%s unknown", cp);
|
|
101 case HELPSW:
|
|
102 (void) sprintf (buf, "%s [switches] aliases ...",
|
|
103 invo_name);
|
|
104 help (buf, switches);
|
|
105 done (1);
|
|
106
|
|
107 case ALIASW:
|
|
108 if (!(cp = *argp++) || *cp == '-')
|
|
109 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
110 if ((i = alias (cp)) != AK_OK)
|
|
111 adios (NULLCP, "aliasing error in %s - %s",
|
|
112 cp, akerror (i));
|
|
113 continue;
|
|
114 case NALIASW:
|
|
115 noalias++;
|
|
116 continue;
|
|
117
|
|
118 case LISTSW:
|
|
119 list++;
|
|
120 continue;
|
|
121 case NLISTSW:
|
|
122 list = 0;
|
|
123 continue;
|
|
124
|
|
125 case NORMSW:
|
|
126 normalize = AD_HOST;
|
|
127 continue;
|
|
128 case NNORMSW:
|
|
129 normalize = AD_NHST;
|
|
130 continue;
|
|
131
|
|
132 case USERSW:
|
|
133 inverted++;
|
|
134 continue;
|
|
135 case NUSERSW:
|
|
136 inverted = 0;
|
|
137 continue;
|
|
138 }
|
|
139 vec[vecp++] = cp;
|
|
140 }
|
|
141
|
|
142 if (!noalias) {
|
|
143 if (cp = m_find ("Aliasfile")) { /* allow Aliasfile: profile entry */
|
|
144 char *dp = NULL;
|
|
145
|
|
146 for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
|
|
147 if ((i = alias (*ap)) != AK_OK)
|
|
148 adios (NULLCP,
|
|
149 "aliasing error in %s - %s", *ap, akerror (i));
|
|
150 if (dp)
|
|
151 free(dp);
|
|
152 }
|
|
153 (void) alias (AliasFile);
|
|
154 }
|
|
155
|
|
156
|
|
157 /* */
|
|
158
|
|
159 if (vecp)
|
|
160 for (i = 0; i < vecp; i++)
|
|
161 if (inverted)
|
|
162 print_usr (vec[i], list, normalize);
|
|
163 else
|
|
164 print_aka (akvalue (vec[i]), list, 0);
|
|
165 else {
|
|
166 if (inverted)
|
|
167 adios (NULLCP,
|
|
168 "usage: %s -user addresses ... (you forgot the addresses)",
|
|
169 invo_name);
|
|
170
|
|
171 for (ak = akahead; ak; ak = ak -> ak_next) {
|
|
172 printf ("%s: ", ak -> ak_name);
|
|
173 pos += strlen (ak -> ak_name) + 1;
|
|
174 print_aka (akresult (ak), list, pos);
|
|
175 }
|
|
176 }
|
|
177
|
|
178 done (0);
|
|
179 }
|
|
180
|
|
181 /* */
|
|
182
|
|
183 print_aka (p, list, margin)
|
|
184 register char *p;
|
|
185 int list,
|
|
186 margin;
|
|
187 {
|
|
188 register char c;
|
|
189 #ifdef JAPAN
|
|
190 char *pp;
|
|
191 #endif
|
|
192
|
|
193 if (p == NULL) {
|
|
194 printf ("<empty>\n");
|
|
195 return;
|
|
196 }
|
|
197
|
|
198 while (c = *p++)
|
|
199 switch (c) {
|
|
200 case ',':
|
|
201 if (*p)
|
|
202 if (list)
|
|
203 printf ("\n%*s", margin, "");
|
|
204 else
|
|
205 if (pos >= 68) {
|
|
206 printf (",\n ");
|
|
207 pos = 2;
|
|
208 }
|
|
209 else {
|
|
210 printf (", ");
|
|
211 pos += 2;
|
|
212 }
|
|
213
|
|
214 case 0:
|
|
215 break;
|
|
216
|
|
217 default:
|
|
218 #ifdef JAPAN
|
|
219 if ((pp = index(p, ','))) {
|
|
220 *pp = '\0';
|
|
221 ml_fputs(p-1, stdout);
|
|
222 *pp = ',';
|
|
223 pos += pp - p + 1;
|
|
224 p = pp;
|
|
225 } else {
|
|
226 ml_fputs(p-1, stdout);
|
|
227 pos += strlen(p) + 1;
|
|
228 p += strlen(p);
|
|
229 }
|
|
230 #else
|
|
231 pos++;
|
|
232 (void) putchar (c);
|
|
233 #endif
|
|
234 }
|
|
235
|
|
236 (void) putchar ('\n');
|
|
237 pos = 1;
|
|
238 }
|
|
239
|
|
240 /* */
|
|
241
|
|
242 print_usr (s, list, norm)
|
|
243 register char *s;
|
|
244 int list,
|
|
245 norm;
|
|
246 {
|
|
247 register char *cp,
|
|
248 *pp,
|
|
249 *vp;
|
|
250 register struct aka *ak;
|
|
251 register struct mailname *mp,
|
|
252 *np;
|
|
253
|
|
254 if ((pp = getname (s)) == NULL)
|
|
255 adios (NULLCP, "no address in \"%s\"", s);
|
|
256 if ((mp = getm (pp, NULLCP, 0, norm, NULLCP)) == NULL)
|
|
257 adios (NULLCP, "bad address \"%s\"", s);
|
|
258 while (getname (""))
|
|
259 continue;
|
|
260
|
|
261 vp = NULL;
|
|
262 for (ak = akahead; ak; ak = ak -> ak_next) {
|
|
263 pp = akresult (ak);
|
|
264 while (cp = getname (pp)) {
|
|
265 if ((np = getm (cp, NULLCP, 0, norm, NULLCP)) == NULL)
|
|
266 continue;
|
|
267 if (uleq (mp -> m_host, np -> m_host)
|
|
268 && uleq (mp -> m_mbox, np -> m_mbox)) {
|
|
269 vp = vp ? add (ak -> ak_name, add (",", vp))
|
|
270 : getcpy (ak -> ak_name);
|
|
271 mnfree (np);
|
|
272 while (getname (""))
|
|
273 continue;
|
|
274 break;
|
|
275 }
|
|
276 mnfree (np);
|
|
277 }
|
|
278 }
|
|
279 mnfree (mp);
|
|
280
|
|
281 #ifdef notdef
|
|
282 printf ("%s: ", s);
|
|
283 print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
|
|
284 #else
|
|
285 print_aka (vp ? vp : s, list, 0);
|
|
286 #endif
|
|
287 if (vp)
|
|
288 free (vp);
|
|
289 }
|