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