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