]> diplodocus.org Git - nmh/blob - uip/ap.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / ap.c
1 /* ap.c -- parse addresses 822-style
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/addrsbr.h>
10 #include <h/fmt_scan.h>
11 #include <h/mts.h>
12 #include "h/done.h"
13 #include <h/utils.h>
14 #include "sbr/terminal.h"
15
16 #define NADDRS 100
17
18 #define WIDTH 78
19
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
21
22 #define AP_SWITCHES \
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(AP);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(AP, switches);
35 #undef X
36
37 static struct format *fmt;
38
39 static int dat[5];
40
41 /*
42 * static prototypes
43 */
44 static int process (char *, int);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 int addrp = 0;
51 int width = -1, status = 0;
52 char *cp, *form = NULL, *format = NULL, *nfs;
53 char buf[BUFSIZ], **argp;
54 char *addrs[NADDRS + 1]; /* Includes terminating NULL. */
55
56 if (nmh_init(argv[0], true, false)) { return 1; }
57
58 mts_init ();
59
60 argp = getarguments (invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
62 if (*cp == '-') {
63 switch (smatch (++cp, switches)) {
64 case AMBIGSW:
65 ambigsw (cp, switches);
66 done (1);
67
68 case UNKWNSW:
69 die("-%s unknown", cp);
70
71 case HELPSW:
72 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
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 FORMSW:
81 if (!(form = *argp++) || *form == '-')
82 die("missing argument to %s", argp[-2]);
83 format = NULL;
84 continue;
85 case FMTSW:
86 if (!(format = *argp++) || *format == '-')
87 die("missing argument to %s", argp[-2]);
88 form = NULL;
89 continue;
90
91 case WIDTHSW:
92 if (!(cp = *argp++) || *cp == '-')
93 die("missing argument to %s", argp[-2]);
94 width = atoi (cp);
95 continue;
96 }
97 }
98 if (addrp == NADDRS)
99 die("more than %d addresses", NADDRS);
100 addrs[addrp++] = cp;
101 }
102 addrs[addrp] = NULL;
103
104 if (addrp == 0)
105 die("usage: %s [switches] addrs ...", invo_name);
106
107 /* get new format string */
108 nfs = new_fs (form, format, FORMAT);
109
110 if (width == -1) {
111 if ((width = sc_width ()) < WIDTH / 2) {
112 /* Default: width of the terminal, but at least WIDTH/2. */
113 width = WIDTH / 2;
114 }
115 width -= 2;
116 } else if (width == 0) {
117 /* Unlimited width. */
118 width = INT_MAX;
119 }
120 fmt_compile (nfs, &fmt, 1);
121
122 dat[0] = 0;
123 dat[1] = 0;
124 dat[2] = 0;
125 dat[3] = width;
126 dat[4] = 0;
127
128 for (addrp = 0; addrs[addrp]; addrp++)
129 status += process (addrs[addrp], width);
130
131 fmt_free (fmt, 1);
132 done(!!status);
133 return 1;
134 }
135
136 struct pqpair {
137 char *pq_text;
138 char *pq_error;
139 struct pqpair *pq_next;
140 };
141
142
143 static int
144 process (char *arg, int length)
145 {
146 int status = 0;
147 char *cp;
148 char error[BUFSIZ];
149 struct comp *cptr;
150 struct pqpair *p, *q;
151 struct pqpair pq;
152 struct mailname *mp;
153
154 (q = &pq)->pq_next = NULL;
155 while ((cp = getname (arg))) {
156 NEW0(p);
157 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
158 p->pq_text = mh_xstrdup(cp);
159 p->pq_error = mh_xstrdup(error);
160 status++;
161 }
162 else {
163 p->pq_text = getcpy (mp->m_text);
164 mnfree (mp);
165 }
166 q = (q->pq_next = p);
167 }
168
169 for (p = pq.pq_next; p; p = q) {
170 charstring_t scanl =
171 charstring_create (length < NMH_BUFSIZ ? length : NMH_BUFSIZ);
172
173 cptr = fmt_findcomp ("text");
174 if (cptr) {
175 free(cptr->c_text);
176 cptr->c_text = p->pq_text;
177 p->pq_text = NULL;
178 }
179 cptr = fmt_findcomp ("error");
180 if (cptr) {
181 free(cptr->c_text);
182 cptr->c_text = p->pq_error;
183 p->pq_error = NULL;
184 }
185
186 fmt_scan (fmt, scanl, length, dat, NULL);
187 fputs (charstring_buffer (scanl), stdout);
188 charstring_free (scanl);
189
190 free(p->pq_text);
191 free(p->pq_error);
192 q = p->pq_next;
193 free(p);
194 }
195
196 return status;
197 }