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