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