0
|
1 /* ap.c - parse addresses 822-style */
|
|
2 #ifndef lint
|
12
|
3 static char ident[] = "@(#)$Id: ap.c,v 1.1.1.1 2005/04/18 14:46:07 kono Exp $";
|
0
|
4 #endif /* lint */
|
|
5
|
|
6 #include "../h/mh.h"
|
|
7 #include "../h/addrsbr.h"
|
|
8 #include "../h/formatsbr.h"
|
|
9 #include <stdio.h>
|
|
10 #ifdef LOCALE
|
|
11 #include <locale.h>
|
|
12 #endif
|
|
13
|
|
14
|
|
15 #define NADDRS 100
|
|
16
|
|
17 #define WIDTH 78
|
|
18 #define WBUFSIZ BUFSIZ
|
|
19
|
|
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
|
|
21
|
|
22 /* */
|
|
23
|
|
24 static struct swit switches[] = {
|
|
25 #define FORMSW 0
|
|
26 "form formatfile", 0,
|
|
27 #define FMTSW 1
|
|
28 "format string", 5,
|
|
29
|
|
30 #define NORMSW 2
|
|
31 "normalize", 0,
|
|
32 #define NNORMSW 3
|
|
33 "nonormalize", 0,
|
|
34
|
|
35 #define WIDSW 4
|
|
36 "width columns", 0,
|
|
37
|
|
38 #define HELPSW 5
|
|
39 "help", 4,
|
|
40
|
|
41 NULL, 0
|
|
42 };
|
|
43
|
|
44 /* */
|
|
45
|
|
46 static struct format *fmt;
|
|
47
|
|
48 static int dat[5];
|
|
49
|
|
50 static int process();
|
|
51 /* */
|
|
52
|
|
53 /* ARGSUSED */
|
|
54
|
|
55 main (argc, argv)
|
|
56 int argc;
|
|
57 char **argv;
|
|
58 {
|
|
59 int addrp = 0,
|
|
60 normalize = AD_HOST,
|
|
61 width = 0,
|
|
62 status = 0;
|
|
63 char *cp,
|
|
64 *form = NULL,
|
|
65 *format = NULL,
|
|
66 *nfs,
|
|
67 buf[80],
|
|
68 **ap,
|
|
69 **argp,
|
|
70 *arguments[MAXARGS],
|
|
71 *addrs[NADDRS];
|
|
72
|
|
73 #ifdef LOCALE
|
|
74 setlocale(LC_ALL, "");
|
|
75 #endif
|
|
76 #ifdef JAPAN
|
|
77 ml_init();
|
|
78 #endif /* JAPAN */
|
|
79 invo_name = r1bindex (argv[0], '/');
|
|
80 mts_init (invo_name);
|
|
81 if ((cp = m_find (invo_name)) != NULL) {
|
|
82 ap = brkstring (cp = getcpy (cp), " ", "\n");
|
|
83 ap = copyip (ap, arguments);
|
|
84 }
|
|
85 else
|
|
86 ap = arguments;
|
|
87 (void) copyip (argv + 1, ap);
|
|
88 argp = arguments;
|
|
89
|
|
90 /* */
|
|
91
|
|
92 while (cp = *argp++) {
|
|
93 if (*cp == '-')
|
|
94 switch (smatch (++cp, switches)) {
|
|
95 case AMBIGSW:
|
|
96 ambigsw (cp, switches);
|
|
97 done (1);
|
|
98
|
|
99 case UNKWNSW:
|
|
100 adios (NULLCP, "-%s unknown", cp);
|
|
101
|
|
102 case HELPSW:
|
|
103 (void) sprintf (buf, "%s [switches] addrs ...", invo_name);
|
|
104 help (buf, switches);
|
|
105 done (1);
|
|
106
|
|
107 case FORMSW:
|
|
108 if (!(form = *argp++) || *form == '-')
|
|
109 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
110 format = NULL;
|
|
111 continue;
|
|
112 case FMTSW:
|
|
113 if (!(format = *argp++) || *format == '-')
|
|
114 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
115 form = NULL;
|
|
116 continue;
|
|
117
|
|
118 case WIDSW:
|
|
119 if (!(cp = *argp++) || *cp == '-')
|
|
120 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
121 width = atoi (cp);
|
|
122 continue;
|
|
123
|
|
124 case NORMSW:
|
|
125 normalize = AD_HOST;
|
|
126 continue;
|
|
127 case NNORMSW:
|
|
128 normalize = AD_NHST;
|
|
129 continue;
|
|
130 }
|
|
131 if (addrp > NADDRS)
|
|
132 adios (NULLCP, "more than %d addresses", NADDRS);
|
|
133 else
|
|
134 addrs[addrp++] = cp;
|
|
135 }
|
|
136 addrs[addrp] = NULL;
|
|
137
|
|
138 /* */
|
|
139
|
|
140 if (addrp == 0)
|
|
141 adios (NULLCP, "usage: %s [switches] addrs ...", invo_name);
|
|
142
|
|
143 nfs = new_fs (form, format, FORMAT);
|
|
144 if (width == 0) {
|
|
145 if ((width = sc_width ()) < WIDTH / 2)
|
|
146 width = WIDTH / 2;
|
|
147 width -= 2;
|
|
148 }
|
|
149 if (width > WBUFSIZ)
|
|
150 width = WBUFSIZ;
|
|
151 fmt_norm = normalize;
|
|
152 (void) fmt_compile (nfs, &fmt);
|
|
153 dat[0] = dat[1] = dat[2] = dat[4] = 0;
|
|
154 dat[3] = width;
|
|
155
|
|
156 for (addrp = 0; addrs[addrp]; addrp++)
|
|
157 status += process (addrs[addrp], width, normalize);
|
|
158
|
|
159 done (status);
|
|
160 }
|
|
161
|
|
162 /* */
|
|
163
|
|
164 struct pqpair {
|
|
165 char *pq_text;
|
|
166 char *pq_error;
|
|
167 struct pqpair *pq_next;
|
|
168 };
|
|
169
|
|
170
|
|
171 static int process (arg, length, norm)
|
|
172 register char *arg;
|
|
173 int length,
|
|
174 norm;
|
|
175 {
|
|
176 int status = 0;
|
|
177 register char *cp;
|
|
178 char buffer[WBUFSIZ + 1],
|
|
179 error[BUFSIZ];
|
|
180 register struct comp *cptr;
|
|
181 register struct pqpair *p,
|
|
182 *q;
|
|
183 struct pqpair pq;
|
|
184 register struct mailname *mp;
|
|
185
|
|
186 (q = &pq) -> pq_next = NULL;
|
|
187 while (cp = getname (arg)) {
|
|
188 if ((p = (struct pqpair *) calloc ((unsigned) 1, sizeof *p)) == NULL)
|
|
189 adios (NULLCP, "unable to allocate pqpair memory");
|
|
190 if ((mp = getm (cp, NULLCP, 0, norm, error)) == NULL) {
|
|
191 p -> pq_text = getcpy (cp);
|
|
192 p -> pq_error = getcpy (error);
|
|
193 status++;
|
|
194 }
|
|
195 else {
|
|
196 p -> pq_text = getcpy (mp -> m_text);
|
|
197 mnfree (mp);
|
|
198 }
|
|
199 q = (q -> pq_next = p);
|
|
200 }
|
|
201
|
|
202 for (p = pq.pq_next; p; p = q) {
|
|
203 FINDCOMP (cptr, "text");
|
|
204 if (cptr)
|
|
205 cptr -> c_text = p -> pq_text;
|
|
206 FINDCOMP (cptr, "error");
|
|
207 if (cptr)
|
|
208 cptr -> c_text = p -> pq_error;
|
|
209
|
|
210 (void) fmtscan (fmt, buffer, length, dat);
|
|
211 #ifdef JAPAN
|
|
212 ml_fputs (buffer, stdout);
|
|
213 #else /* JAPAN */
|
|
214 (void) fputs (buffer, stdout);
|
|
215 #endif /* JAPAN */
|
|
216
|
|
217 free (p -> pq_text);
|
|
218 if (p -> pq_error)
|
|
219 free (p -> pq_error);
|
|
220 q = p -> pq_next;
|
|
221 free ((char *) p);
|
|
222 }
|
|
223
|
|
224 return status;
|
|
225 }
|