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