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