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