]> diplodocus.org Git - nmh/blob - uip/ap.c
a8f9169f9a597494caaf1277453cea698bd134a7
[nmh] / uip / ap.c
1
2 /*
3 * ap.c -- parse addresses 822-style
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/mts.h>
14
15 #define NADDRS 100
16
17 #define WIDTH 78
18 #define WBUFSIZ BUFSIZ
19
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
21
22 #define AP_SWITCHES \
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(AP);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(AP, switches);
35 #undef X
36
37 static struct format *fmt;
38
39 static int dat[5];
40
41 /*
42 * static prototypes
43 */
44 static int process (char *, int);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 int addrp = 0;
51 int width = 0, status = 0;
52 char *cp, *form = NULL, *format = NULL, *nfs;
53 char buf[BUFSIZ], **argp;
54 char **arguments, *addrs[NADDRS];
55
56 setlocale(LC_ALL, "");
57 invo_name = r1bindex (argv[0], '/');
58
59 /* read user profile/context */
60 context_read();
61
62 mts_init (invo_name);
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
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 adios (NULL, "-%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 adios (NULL, "missing argument to %s", argp[-2]);
88 format = NULL;
89 continue;
90 case FMTSW:
91 if (!(format = *argp++) || *format == '-')
92 adios (NULL, "missing argument to %s", argp[-2]);
93 form = NULL;
94 continue;
95
96 case WIDTHSW:
97 if (!(cp = *argp++) || *cp == '-')
98 adios (NULL, "missing argument to %s", argp[-2]);
99 width = atoi (cp);
100 continue;
101 }
102 }
103 if (addrp > NADDRS)
104 adios (NULL, "more than %d addresses", NADDRS);
105 else
106 addrs[addrp++] = cp;
107 }
108 addrs[addrp] = NULL;
109
110 if (addrp == 0)
111 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
112
113 /* get new format string */
114 nfs = new_fs (form, format, FORMAT);
115
116 if (width == 0) {
117 if ((width = sc_width ()) < WIDTH / 2)
118 width = WIDTH / 2;
119 width -= 2;
120 }
121 if (width > WBUFSIZ)
122 width = WBUFSIZ;
123 fmt_compile (nfs, &fmt, 1);
124
125 dat[0] = 0;
126 dat[1] = 0;
127 dat[2] = 0;
128 dat[3] = width;
129 dat[4] = 0;
130
131 for (addrp = 0; addrs[addrp]; addrp++)
132 status += process (addrs[addrp], width);
133
134 fmt_free (fmt, 1);
135 done (status);
136 return 1;
137 }
138
139 struct pqpair {
140 char *pq_text;
141 char *pq_error;
142 struct pqpair *pq_next;
143 };
144
145
146 static int
147 process (char *arg, int length)
148 {
149 int status = 0;
150 register char *cp;
151 char buffer[WBUFSIZ + 1], error[BUFSIZ];
152 register struct comp *cptr;
153 register struct pqpair *p, *q;
154 struct pqpair pq;
155 register struct mailname *mp;
156
157 (q = &pq)->pq_next = NULL;
158 while ((cp = getname (arg))) {
159 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
160 adios (NULL, "unable to allocate pqpair memory");
161 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
162 p->pq_text = getcpy (cp);
163 p->pq_error = getcpy (error);
164 status++;
165 }
166 else {
167 p->pq_text = getcpy (mp->m_text);
168 mnfree (mp);
169 }
170 q = (q->pq_next = p);
171 }
172
173 for (p = pq.pq_next; p; p = q) {
174 cptr = fmt_findcomp ("text");
175 if (cptr) {
176 if (cptr->c_text)
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 if (cptr->c_text)
184 free(cptr->c_text);
185 cptr->c_text = p->pq_error;
186 p->pq_error = NULL;
187 }
188
189 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL);
190 fputs (buffer, stdout);
191
192 if (p->pq_text)
193 free (p->pq_text);
194 if (p->pq_error)
195 free (p->pq_error);
196 q = p->pq_next;
197 free ((char *) p);
198 }
199
200 return status;
201 }