Added argument to fmt_scan() to specify the buffer size.
[mmh] / 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 static struct swit switches[] = {
23 #define FORMSW  0
24     { "form formatfile", 0 },
25 #define FMTSW   1
26     { "format string", 5 },
27 #define NORMSW  2
28     { "normalize", 0 },
29 #define NNORMSW 3
30     { "nonormalize", 0 },
31 #define WIDTHSW 4
32     { "width columns", 0 },
33 #define VERSIONSW 5
34     { "version", 0 },
35 #define HELPSW  6
36     { "help", 0 },
37     { NULL, 0 }
38 };
39
40 static struct format *fmt;
41
42 static int dat[5];
43
44 /*
45  * static prototypes
46  */
47 static int process (char *, int, int);
48
49
50 int
51 main (int argc, char **argv)
52 {
53     int addrp = 0, normalize = AD_HOST;
54     int width = 0, status = 0;
55     char *cp, *form = NULL, *format = NULL, *nfs;
56     char buf[BUFSIZ], **argp;
57     char **arguments, *addrs[NADDRS];
58
59 #ifdef LOCALE
60     setlocale(LC_ALL, "");
61 #endif
62     invo_name = r1bindex (argv[0], '/');
63
64     /* read user profile/context */
65     context_read();
66
67     mts_init (invo_name);
68     arguments = getarguments (invo_name, argc, argv, 1);
69     argp = arguments;
70
71     while ((cp = *argp++)) {
72         if (*cp == '-') {
73             switch (smatch (++cp, switches)) {
74                 case AMBIGSW: 
75                     ambigsw (cp, switches);
76                     done (1);
77
78                 case UNKWNSW: 
79                     adios (NULL, "-%s unknown", cp);
80
81                 case HELPSW: 
82                     snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
83                         invo_name);
84                     print_help (buf, switches, 1);
85                     done (0);
86                 case VERSIONSW:
87                     print_version (invo_name);
88                     done (0);
89
90                 case FORMSW: 
91                     if (!(form = *argp++) || *form == '-')
92                         adios (NULL, "missing argument to %s", argp[-2]);
93                     format = NULL;
94                     continue;
95                 case FMTSW: 
96                     if (!(format = *argp++) || *format == '-')
97                         adios (NULL, "missing argument to %s", argp[-2]);
98                     form = NULL;
99                     continue;
100
101                 case WIDTHSW: 
102                     if (!(cp = *argp++) || *cp == '-')
103                         adios (NULL, "missing argument to %s", argp[-2]);
104                     width = atoi (cp);
105                     continue;
106
107                 case NORMSW: 
108                     normalize = AD_HOST;
109                     continue;
110                 case NNORMSW: 
111                     normalize = AD_NHST;
112                     continue;
113             }
114         }
115         if (addrp > NADDRS)
116             adios (NULL, "more than %d addresses", NADDRS);
117         else
118             addrs[addrp++] = cp;
119     }
120     addrs[addrp] = NULL;
121
122     if (addrp == 0)
123         adios (NULL, "usage: %s [switches] addrs ...", invo_name);
124
125     /* get new format string */
126     nfs = new_fs (form, format, FORMAT);
127
128     if (width == 0) {
129         if ((width = sc_width ()) < WIDTH / 2)
130             width = WIDTH / 2;
131         width -= 2;
132     }
133     if (width > WBUFSIZ)
134         width = WBUFSIZ;
135     fmt_norm = normalize;
136     fmt_compile (nfs, &fmt);
137
138     dat[0] = 0;
139     dat[1] = 0;
140     dat[2] = 0;
141     dat[3] = width;
142     dat[4] = 0;
143
144     for (addrp = 0; addrs[addrp]; addrp++)
145         status += process (addrs[addrp], width, normalize);
146
147     done (status);
148     return 1;
149 }
150
151 struct pqpair {
152     char *pq_text;
153     char *pq_error;
154     struct pqpair *pq_next;
155 };
156
157
158 static int
159 process (char *arg, int length, int norm)
160 {
161     int status = 0;
162     register char *cp;
163     char buffer[WBUFSIZ + 1], error[BUFSIZ];
164     register struct comp *cptr;
165     register struct pqpair *p, *q;
166     struct pqpair pq;
167     register struct mailname *mp;
168
169     (q = &pq)->pq_next = NULL;
170     while ((cp = getname (arg))) {
171         if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
172             adios (NULL, "unable to allocate pqpair memory");
173         if ((mp = getm (cp, NULL, 0, norm, error)) == NULL) {
174             p->pq_text = getcpy (cp);
175             p->pq_error = getcpy (error);
176             status++;
177         }
178         else {
179             p->pq_text = getcpy (mp->m_text);
180             mnfree (mp);
181         }
182         q = (q->pq_next = p);
183     }
184
185     for (p = pq.pq_next; p; p = q) {
186         FINDCOMP (cptr, "text");
187         if (cptr)
188             cptr->c_text = p->pq_text;
189         FINDCOMP (cptr, "error");
190         if (cptr)
191             cptr->c_text = p->pq_error;
192
193         fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat);
194         fputs (buffer, stdout);
195
196         free (p->pq_text);
197         if (p->pq_error)
198             free (p->pq_error);
199         q = p->pq_next;
200         free ((char *) p);
201     }
202
203     return status;
204 }