]> diplodocus.org Git - nmh/blob - uip/ap.c
2671375fcf2bc83b6877d6b948a4f2beecb3869c
[nmh] / uip / ap.c
1 /*
2 * ap.c -- parse addresses 822-style
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <h/mts.h>
13 #include <h/utils.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 else
100 addrs[addrp++] = cp;
101 }
102 addrs[addrp] = NULL;
103
104 if (addrp == 0)
105 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
106
107 /* get new format string */
108 nfs = new_fs (form, format, FORMAT);
109
110 if (width == -1) {
111 if ((width = sc_width ()) < WIDTH / 2) {
112 /* Default: width of the terminal, but at least WIDTH/2. */
113 width = WIDTH / 2;
114 }
115 width -= 2;
116 } else if (width == 0) {
117 /* Unlimited width. */
118 width = INT_MAX;
119 }
120 fmt_compile (nfs, &fmt, 1);
121
122 dat[0] = 0;
123 dat[1] = 0;
124 dat[2] = 0;
125 dat[3] = width;
126 dat[4] = 0;
127
128 for (addrp = 0; addrs[addrp]; addrp++)
129 status += process (addrs[addrp], width);
130
131 fmt_free (fmt, 1);
132 done (status);
133 return 1;
134 }
135
136 struct pqpair {
137 char *pq_text;
138 char *pq_error;
139 struct pqpair *pq_next;
140 };
141
142
143 static int
144 process (char *arg, int length)
145 {
146 int status = 0;
147 char *cp;
148 char error[BUFSIZ];
149 struct comp *cptr;
150 struct pqpair *p, *q;
151 struct pqpair pq;
152 struct mailname *mp;
153
154 (q = &pq)->pq_next = NULL;
155 while ((cp = getname (arg))) {
156 NEW0(p);
157 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
158 p->pq_text = mh_xstrdup(cp);
159 p->pq_error = mh_xstrdup(error);
160 status++;
161 }
162 else {
163 p->pq_text = getcpy (mp->m_text);
164 mnfree (mp);
165 }
166 q = (q->pq_next = p);
167 }
168
169 for (p = pq.pq_next; p; p = q) {
170 charstring_t scanl =
171 charstring_create (length < NMH_BUFSIZ ? length : NMH_BUFSIZ);
172
173 cptr = fmt_findcomp ("text");
174 if (cptr) {
175 mh_xfree(cptr->c_text);
176 cptr->c_text = p->pq_text;
177 p->pq_text = NULL;
178 }
179 cptr = fmt_findcomp ("error");
180 if (cptr) {
181 mh_xfree(cptr->c_text);
182 cptr->c_text = p->pq_error;
183 p->pq_error = NULL;
184 }
185
186 fmt_scan (fmt, scanl, length, dat, NULL);
187 fputs (charstring_buffer (scanl), stdout);
188 charstring_free (scanl);
189
190 mh_xfree(p->pq_text);
191 mh_xfree(p->pq_error);
192 q = p->pq_next;
193 free(p);
194 }
195
196 return status;
197 }