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