]> diplodocus.org Git - nmh/blob - uip/ap.c
Started revising m_getfld() code to replace direct buffer
[nmh] / uip / ap.c
1
2 /*
3 * ap.c -- parse addresses 822-style
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/fmt_scan.h>
13 #include <h/mts.h>
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 static struct swit switches[] = {
23 #define FORMSW 0
24 { "form formatfile", 0 },
25 #define FMTSW 1
26 { "format string", 5 },
27 #define NORMSW 2
28 { "normalize", 0 },
29 #define NNORMSW 3
30 { "nonormalize", 0 },
31 #define WIDTHSW 4
32 { "width columns", 0 },
33 #define VERSIONSW 5
34 { "version", 0 },
35 #define HELPSW 6
36 { "help", 0 },
37 { NULL, 0 }
38 };
39
40 static struct format *fmt;
41
42 static int dat[5];
43
44 /*
45 * static prototypes
46 */
47 static int process (char *, int, int);
48
49
50 int
51 main (int argc, char **argv)
52 {
53 int addrp = 0, normalize = AD_HOST;
54 int width = 0, status = 0;
55 char *cp, *form = NULL, *format = NULL, *nfs;
56 char buf[BUFSIZ], **argp;
57 char **arguments, *addrs[NADDRS];
58
59 #ifdef LOCALE
60 setlocale(LC_ALL, "");
61 #endif
62 invo_name = r1bindex (argv[0], '/');
63
64 /* read user profile/context */
65 context_read();
66
67 mts_init (invo_name);
68 arguments = getarguments (invo_name, argc, argv, 1);
69 argp = arguments;
70
71 while ((cp = *argp++)) {
72 if (*cp == '-') {
73 switch (smatch (++cp, switches)) {
74 case AMBIGSW:
75 ambigsw (cp, switches);
76 done (1);
77
78 case UNKWNSW:
79 adios (NULL, "-%s unknown", cp);
80
81 case HELPSW:
82 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
83 invo_name);
84 print_help (buf, switches, 1);
85 done (0);
86 case VERSIONSW:
87 print_version (invo_name);
88 done (0);
89
90 case FORMSW:
91 if (!(form = *argp++) || *form == '-')
92 adios (NULL, "missing argument to %s", argp[-2]);
93 format = NULL;
94 continue;
95 case FMTSW:
96 if (!(format = *argp++) || *format == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
98 form = NULL;
99 continue;
100
101 case WIDTHSW:
102 if (!(cp = *argp++) || *cp == '-')
103 adios (NULL, "missing argument to %s", argp[-2]);
104 width = atoi (cp);
105 continue;
106
107 case NORMSW:
108 normalize = AD_HOST;
109 continue;
110 case NNORMSW:
111 normalize = AD_NHST;
112 continue;
113 }
114 }
115 if (addrp > NADDRS)
116 adios (NULL, "more than %d addresses", NADDRS);
117 else
118 addrs[addrp++] = cp;
119 }
120 addrs[addrp] = NULL;
121
122 if (addrp == 0)
123 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
124
125 /* get new format string */
126 nfs = new_fs (form, format, FORMAT);
127
128 if (width == 0) {
129 if ((width = sc_width ()) < WIDTH / 2)
130 width = WIDTH / 2;
131 width -= 2;
132 }
133 if (width > WBUFSIZ)
134 width = WBUFSIZ;
135 fmt_norm = normalize;
136 fmt_compile (nfs, &fmt, 1);
137
138 dat[0] = 0;
139 dat[1] = 0;
140 dat[2] = 0;
141 dat[3] = width;
142 dat[4] = 0;
143
144 for (addrp = 0; addrs[addrp]; addrp++)
145 status += process (addrs[addrp], width, normalize);
146
147 fmt_free (fmt, 1);
148 done (status);
149 return 1;
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 cptr = fmt_findcomp ("text");
188 if (cptr) {
189 if (cptr->c_text)
190 free(cptr->c_text);
191 cptr->c_text = p->pq_text;
192 p->pq_text = NULL;
193 }
194 cptr = fmt_findcomp ("error");
195 if (cptr) {
196 if (cptr->c_text)
197 free(cptr->c_text);
198 cptr->c_text = p->pq_error;
199 p->pq_error = NULL;
200 }
201
202 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat);
203 fputs (buffer, stdout);
204
205 if (p->pq_text)
206 free (p->pq_text);
207 if (p->pq_error)
208 free (p->pq_error);
209 q = p->pq_next;
210 free ((char *) p);
211 }
212
213 return status;
214 }