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