]> diplodocus.org Git - nmh/blob - uip/ap.c
Removed temporary probes added in commit
[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 #ifdef LOCALE
57 setlocale(LC_ALL, "");
58 #endif
59 invo_name = r1bindex (argv[0], '/');
60
61 /* read user profile/context */
62 context_read();
63
64 mts_init (invo_name);
65 arguments = getarguments (invo_name, argc, argv, 1);
66 argp = arguments;
67
68 while ((cp = *argp++)) {
69 if (*cp == '-') {
70 switch (smatch (++cp, switches)) {
71 case AMBIGSW:
72 ambigsw (cp, switches);
73 done (1);
74
75 case UNKWNSW:
76 adios (NULL, "-%s unknown", cp);
77
78 case HELPSW:
79 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
80 invo_name);
81 print_help (buf, switches, 1);
82 done (0);
83 case VERSIONSW:
84 print_version (invo_name);
85 done (0);
86
87 case FORMSW:
88 if (!(form = *argp++) || *form == '-')
89 adios (NULL, "missing argument to %s", argp[-2]);
90 format = NULL;
91 continue;
92 case FMTSW:
93 if (!(format = *argp++) || *format == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
95 form = NULL;
96 continue;
97
98 case WIDTHSW:
99 if (!(cp = *argp++) || *cp == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
101 width = atoi (cp);
102 continue;
103 }
104 }
105 if (addrp > NADDRS)
106 adios (NULL, "more than %d addresses", NADDRS);
107 else
108 addrs[addrp++] = cp;
109 }
110 addrs[addrp] = NULL;
111
112 if (addrp == 0)
113 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
114
115 /* get new format string */
116 nfs = new_fs (form, format, FORMAT);
117
118 if (width == 0) {
119 if ((width = sc_width ()) < WIDTH / 2)
120 width = WIDTH / 2;
121 width -= 2;
122 }
123 if (width > WBUFSIZ)
124 width = WBUFSIZ;
125 fmt_compile (nfs, &fmt, 1);
126
127 dat[0] = 0;
128 dat[1] = 0;
129 dat[2] = 0;
130 dat[3] = width;
131 dat[4] = 0;
132
133 for (addrp = 0; addrs[addrp]; addrp++)
134 status += process (addrs[addrp], width);
135
136 fmt_free (fmt, 1);
137 done (status);
138 return 1;
139 }
140
141 struct pqpair {
142 char *pq_text;
143 char *pq_error;
144 struct pqpair *pq_next;
145 };
146
147
148 static int
149 process (char *arg, int length)
150 {
151 int status = 0;
152 register char *cp;
153 char buffer[WBUFSIZ + 1], error[BUFSIZ];
154 register struct comp *cptr;
155 register struct pqpair *p, *q;
156 struct pqpair pq;
157 register struct mailname *mp;
158
159 (q = &pq)->pq_next = NULL;
160 while ((cp = getname (arg))) {
161 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
162 adios (NULL, "unable to allocate pqpair memory");
163 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
164 p->pq_text = getcpy (cp);
165 p->pq_error = getcpy (error);
166 status++;
167 }
168 else {
169 p->pq_text = getcpy (mp->m_text);
170 mnfree (mp);
171 }
172 q = (q->pq_next = p);
173 }
174
175 for (p = pq.pq_next; p; p = q) {
176 cptr = fmt_findcomp ("text");
177 if (cptr) {
178 if (cptr->c_text)
179 free(cptr->c_text);
180 cptr->c_text = p->pq_text;
181 p->pq_text = NULL;
182 }
183 cptr = fmt_findcomp ("error");
184 if (cptr) {
185 if (cptr->c_text)
186 free(cptr->c_text);
187 cptr->c_text = p->pq_error;
188 p->pq_error = NULL;
189 }
190
191 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL);
192 fputs (buffer, stdout);
193
194 if (p->pq_text)
195 free (p->pq_text);
196 if (p->pq_error)
197 free (p->pq_error);
198 q = p->pq_next;
199 free ((char *) p);
200 }
201
202 return status;
203 }