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