]>
diplodocus.org Git - nmh/blob - uip/ap.c
2671375fcf2bc83b6877d6b948a4f2beecb3869c
2 * ap.c -- parse addresses 822-style
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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
19 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
22 X("form formatfile", 0, FORMSW) \
23 X("format string", 5, FMTSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(AP
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(AP
, switches
);
36 static struct format
*fmt
;
43 static int process (char *, int);
47 main (int argc
, char **argv
)
50 int width
= -1, status
= 0;
51 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
52 char buf
[BUFSIZ
], **argp
;
53 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
55 if (nmh_init(argv
[0], 2)) { return 1; }
59 argp
= getarguments (invo_name
, argc
, argv
, 1);
60 while ((cp
= *argp
++)) {
62 switch (smatch (++cp
, switches
)) {
64 ambigsw (cp
, switches
);
68 adios (NULL
, "-%s unknown", cp
);
71 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
73 print_help (buf
, switches
, 1);
76 print_version (invo_name
);
80 if (!(form
= *argp
++) || *form
== '-')
81 adios (NULL
, "missing argument to %s", argp
[-2]);
85 if (!(format
= *argp
++) || *format
== '-')
86 adios (NULL
, "missing argument to %s", argp
[-2]);
91 if (!(cp
= *argp
++) || *cp
== '-')
92 adios (NULL
, "missing argument to %s", argp
[-2]);
98 adios (NULL
, "more than %d addresses", NADDRS
);
105 adios (NULL
, "usage: %s [switches] addrs ...", invo_name
);
107 /* get new format string */
108 nfs
= new_fs (form
, format
, FORMAT
);
111 if ((width
= sc_width ()) < WIDTH
/ 2) {
112 /* Default: width of the terminal, but at least WIDTH/2. */
116 } else if (width
== 0) {
117 /* Unlimited width. */
120 fmt_compile (nfs
, &fmt
, 1);
128 for (addrp
= 0; addrs
[addrp
]; addrp
++)
129 status
+= process (addrs
[addrp
], width
);
139 struct pqpair
*pq_next
;
144 process (char *arg
, int length
)
150 struct pqpair
*p
, *q
;
154 (q
= &pq
)->pq_next
= NULL
;
155 while ((cp
= getname (arg
))) {
157 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
158 p
->pq_text
= mh_xstrdup(cp
);
159 p
->pq_error
= mh_xstrdup(error
);
163 p
->pq_text
= getcpy (mp
->m_text
);
166 q
= (q
->pq_next
= p
);
169 for (p
= pq
.pq_next
; p
; p
= q
) {
171 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
173 cptr
= fmt_findcomp ("text");
175 mh_xfree(cptr
->c_text
);
176 cptr
->c_text
= p
->pq_text
;
179 cptr
= fmt_findcomp ("error");
181 mh_xfree(cptr
->c_text
);
182 cptr
->c_text
= p
->pq_error
;
186 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
187 fputs (charstring_buffer (scanl
), stdout
);
188 charstring_free (scanl
);
190 mh_xfree(p
->pq_text
);
191 mh_xfree(p
->pq_error
);