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