]> diplodocus.org Git - nmh/blob - uip/ap.c
Makefile.am: Add test/inc/test-eom-align to XFAIL_TESTS.
[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
14 #define NADDRS 100
15
16 #define WIDTH 78
17
18 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
19
20 #define AP_SWITCHES \
21 X("form formatfile", 0, FORMSW) \
22 X("format string", 5, FMTSW) \
23 X("width columns", 0, WIDTHSW) \
24 X("version", 0, VERSIONSW) \
25 X("help", 0, HELPSW) \
26
27 #define X(sw, minchars, id) id,
28 DEFINE_SWITCH_ENUM(AP);
29 #undef X
30
31 #define X(sw, minchars, id) { sw, minchars, id },
32 DEFINE_SWITCH_ARRAY(AP, switches);
33 #undef X
34
35 static struct format *fmt;
36
37 static int dat[5];
38
39 /*
40 * static prototypes
41 */
42 static int process (char *, int);
43
44
45 int
46 main (int argc, char **argv)
47 {
48 int addrp = 0;
49 int width = -1, status = 0;
50 char *cp, *form = NULL, *format = NULL, *nfs;
51 char buf[BUFSIZ], **argp;
52 char *addrs[NADDRS + 1]; /* Includes terminating NULL. */
53
54 if (nmh_init(argv[0], 2)) { return 1; }
55
56 mts_init ();
57
58 argp = getarguments (invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
60 if (*cp == '-') {
61 switch (smatch (++cp, switches)) {
62 case AMBIGSW:
63 ambigsw (cp, switches);
64 done (1);
65
66 case UNKWNSW:
67 adios (NULL, "-%s unknown", cp);
68
69 case HELPSW:
70 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
71 invo_name);
72 print_help (buf, switches, 1);
73 done (0);
74 case VERSIONSW:
75 print_version (invo_name);
76 done (0);
77
78 case FORMSW:
79 if (!(form = *argp++) || *form == '-')
80 adios (NULL, "missing argument to %s", argp[-2]);
81 format = NULL;
82 continue;
83 case FMTSW:
84 if (!(format = *argp++) || *format == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
86 form = NULL;
87 continue;
88
89 case WIDTHSW:
90 if (!(cp = *argp++) || *cp == '-')
91 adios (NULL, "missing argument to %s", argp[-2]);
92 width = atoi (cp);
93 continue;
94 }
95 }
96 if (addrp == NADDRS)
97 adios (NULL, "more than %d addresses", NADDRS);
98 else
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 mh_xfree(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 mh_xfree(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 mh_xfree(p->pq_text);
190 mh_xfree(p->pq_error);
191 q = p->pq_next;
192 free(p);
193 }
194
195 return status;
196 }