]>
diplodocus.org Git - nmh/blob - uip/ap.c
3 * ap.c -- parse addresses 822-style
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.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
18 #define WBUFSIZ BUFSIZ
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("normalize", 0, NORMSW) \
26 X("nonormalize", 0, NNORMSW) \
27 X("width columns", 0, WIDTHSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(AP
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(AP
, switches
);
39 static struct format
*fmt
;
46 static int process (char *, int, int);
50 main (int argc
, char **argv
)
52 int addrp
= 0, normalize
= AD_HOST
;
53 int width
= 0, status
= 0;
54 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
55 char buf
[BUFSIZ
], **argp
;
56 char **arguments
, *addrs
[NADDRS
];
59 setlocale(LC_ALL
, "");
61 invo_name
= r1bindex (argv
[0], '/');
63 /* read user profile/context */
67 arguments
= getarguments (invo_name
, argc
, argv
, 1);
70 while ((cp
= *argp
++)) {
72 switch (smatch (++cp
, switches
)) {
74 ambigsw (cp
, switches
);
78 adios (NULL
, "-%s unknown", cp
);
81 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
83 print_help (buf
, switches
, 1);
86 print_version (invo_name
);
90 if (!(form
= *argp
++) || *form
== '-')
91 adios (NULL
, "missing argument to %s", argp
[-2]);
95 if (!(format
= *argp
++) || *format
== '-')
96 adios (NULL
, "missing argument to %s", argp
[-2]);
101 if (!(cp
= *argp
++) || *cp
== '-')
102 adios (NULL
, "missing argument to %s", argp
[-2]);
115 adios (NULL
, "more than %d addresses", NADDRS
);
122 adios (NULL
, "usage: %s [switches] addrs ...", invo_name
);
124 /* get new format string */
125 nfs
= new_fs (form
, format
, FORMAT
);
128 if ((width
= sc_width ()) < WIDTH
/ 2)
134 fmt_norm
= normalize
;
135 fmt_compile (nfs
, &fmt
, 1);
143 for (addrp
= 0; addrs
[addrp
]; addrp
++)
144 status
+= process (addrs
[addrp
], width
, normalize
);
154 struct pqpair
*pq_next
;
159 process (char *arg
, int length
, int norm
)
163 char buffer
[WBUFSIZ
+ 1], error
[BUFSIZ
];
164 register struct comp
*cptr
;
165 register struct pqpair
*p
, *q
;
167 register struct mailname
*mp
;
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
);
179 p
->pq_text
= getcpy (mp
->m_text
);
182 q
= (q
->pq_next
= p
);
185 for (p
= pq
.pq_next
; p
; p
= q
) {
186 cptr
= fmt_findcomp ("text");
190 cptr
->c_text
= p
->pq_text
;
193 cptr
= fmt_findcomp ("error");
197 cptr
->c_text
= p
->pq_error
;
201 fmt_scan (fmt
, buffer
, sizeof buffer
- 1, length
, dat
, NULL
);
202 fputs (buffer
, stdout
);