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