Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / ali.c
1 /* ali.c - the new ali */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: ali.c,v 1.9 1993/09/02 00:05:15 jromine Exp $";
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 /* \f */
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 /* \f */
47
48 static  int     pos = 1;
49
50 extern struct aka  *akahead;
51
52 /* \f */
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     invo_name = r1bindex (argv[0], '/');
78     mts_init (invo_name);
79     if ((cp = m_find (invo_name)) != NULL) {
80         ap = brkstring (cp = getcpy (cp), " ", "\n");
81         ap = copyip (ap, arguments);
82     }
83     else
84         ap = arguments;
85     (void) copyip (argv + 1, ap);
86     argp = arguments;
87
88 /* \f */
89
90     while (cp = *argp++) {
91         if (*cp == '-')
92             switch (smatch (++cp, switches)) {
93                 case AMBIGSW: 
94                     ambigsw (cp, switches);
95                     done (1);
96                 case UNKWNSW: 
97                     adios (NULLCP, "-%s unknown", cp);
98                 case HELPSW: 
99                     (void) sprintf (buf, "%s [switches] aliases ...",
100                             invo_name);
101                     help (buf, switches);
102                     done (1);
103
104                 case ALIASW: 
105                     if (!(cp = *argp++) || *cp == '-')
106                         adios (NULLCP, "missing argument to %s", argp[-2]);
107                     if ((i = alias (cp)) != AK_OK)
108                         adios (NULLCP, "aliasing error in %s - %s",
109                                 cp, akerror (i));
110                     continue;
111                 case NALIASW: 
112                     noalias++;
113                     continue;
114
115                 case LISTSW: 
116                     list++;
117                     continue;
118                 case NLISTSW: 
119                     list = 0;
120                     continue;
121
122                 case NORMSW: 
123                     normalize = AD_HOST;
124                     continue;
125                 case NNORMSW: 
126                     normalize = AD_NHST;
127                     continue;
128
129                 case USERSW: 
130                     inverted++;
131                     continue;
132                 case NUSERSW: 
133                     inverted = 0;
134                     continue;
135             }
136         vec[vecp++] = cp;
137     }
138
139     if (!noalias) {
140         if (cp = m_find ("Aliasfile")) { /* allow Aliasfile: profile entry */
141             char *dp = NULL;
142
143             for (ap = brkstring(dp = getcpy(cp), " ", "\n"); ap && *ap; ap++)
144                 if ((i = alias (*ap)) != AK_OK)
145                     adios (NULLCP,
146                             "aliasing error in %s - %s", *ap, akerror (i));
147             if (dp)
148                 free(dp);
149         }
150         (void) alias (AliasFile);
151     }
152         
153
154 /* \f */
155
156     if (vecp)
157         for (i = 0; i < vecp; i++)
158             if (inverted)
159                 print_usr (vec[i], list, normalize);
160             else
161                 print_aka (akvalue (vec[i]), list, 0);
162     else {
163         if (inverted)
164             adios (NULLCP,
165                     "usage: %s -user addresses ...  (you forgot the addresses)",
166                 invo_name);
167
168         for (ak = akahead; ak; ak = ak -> ak_next) {
169             printf ("%s: ", ak -> ak_name);
170             pos += strlen (ak -> ak_name) + 1;
171             print_aka (akresult (ak), list, pos);
172         }
173     }
174
175     done (0);
176 }
177
178 /* \f */
179
180 print_aka (p, list, margin)
181 register char  *p;
182 int     list,
183         margin;
184 {
185     register char   c;
186
187     if (p == NULL) {
188         printf ("<empty>\n");
189         return;
190     }
191
192     while (c = *p++)
193         switch (c) {
194             case ',': 
195                 if (*p)
196                     if (list)
197                         printf ("\n%*s", margin, "");
198                     else
199                         if (pos >= 68) {
200                             printf (",\n ");
201                             pos = 2;
202                         }
203                         else {
204                             printf (", ");
205                             pos += 2;
206                         }
207
208             case 0: 
209                 break;
210
211             default: 
212                 pos++;
213                 (void) putchar (c);
214         }
215
216     (void) putchar ('\n');
217     pos = 1;
218 }
219
220 /* \f */
221
222 print_usr (s, list, norm)
223 register char  *s;
224 int     list,
225         norm;
226 {
227     register char  *cp,
228                    *pp,
229                    *vp;
230     register struct aka *ak;
231     register struct mailname   *mp,
232                                *np;
233
234     if ((pp = getname (s)) == NULL)
235         adios (NULLCP, "no address in \"%s\"", s);
236     if ((mp = getm (pp, NULLCP, 0, norm, NULLCP)) == NULL)
237         adios (NULLCP, "bad address \"%s\"", s);
238     while (getname (""))
239         continue;
240
241     vp = NULL;
242     for (ak = akahead; ak; ak = ak -> ak_next) {
243         pp = akresult (ak);
244         while (cp = getname (pp)) {
245             if ((np = getm (cp, NULLCP, 0, norm, NULLCP)) == NULL)
246                 continue;
247             if (uleq (mp -> m_host, np -> m_host)
248                     && uleq (mp -> m_mbox, np -> m_mbox)) {
249                 vp = vp ? add (ak -> ak_name, add (",", vp))
250                     : getcpy (ak -> ak_name);
251                 mnfree (np);
252                 while (getname (""))
253                     continue;
254                 break;
255             }
256             mnfree (np);
257         }
258     }
259     mnfree (mp);
260
261 #ifdef  notdef
262     printf ("%s: ", s);
263     print_aka (vp ? vp : s, list, pos += strlen (s) + 1);
264 #else
265     print_aka (vp ? vp : s, list, 0);
266 #endif
267     if (vp)
268         free (vp);
269 }