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