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