]>
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>
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(AP
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(AP
, switches
);
37 static struct format
*fmt
;
44 static int process (char *, int);
48 main (int argc
, char **argv
)
51 int width
= -1, status
= 0;
52 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
53 char buf
[BUFSIZ
], **argp
;
54 char **arguments
, *addrs
[NADDRS
];
56 if (nmh_init(argv
[0], 1)) { return 1; }
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
70 adios (NULL
, "-%s unknown", cp
);
73 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
75 print_help (buf
, switches
, 1);
78 print_version (invo_name
);
82 if (!(form
= *argp
++) || *form
== '-')
83 adios (NULL
, "missing argument to %s", argp
[-2]);
87 if (!(format
= *argp
++) || *format
== '-')
88 adios (NULL
, "missing argument to %s", argp
[-2]);
93 if (!(cp
= *argp
++) || *cp
== '-')
94 adios (NULL
, "missing argument to %s", argp
[-2]);
100 adios (NULL
, "more than %d addresses", NADDRS
);
107 adios (NULL
, "usage: %s [switches] addrs ...", invo_name
);
109 /* get new format string */
110 nfs
= new_fs (form
, format
, FORMAT
);
113 if ((width
= sc_width ()) < WIDTH
/ 2) {
114 /* Default: width of the terminal, but at least WIDTH/2. */
118 } else if (width
== 0) {
119 /* Unlimited width. */
122 fmt_compile (nfs
, &fmt
, 1);
130 for (addrp
= 0; addrs
[addrp
]; addrp
++)
131 status
+= process (addrs
[addrp
], width
);
141 struct pqpair
*pq_next
;
146 process (char *arg
, int length
)
151 register struct comp
*cptr
;
152 register struct pqpair
*p
, *q
;
154 register struct mailname
*mp
;
156 (q
= &pq
)->pq_next
= NULL
;
157 while ((cp
= getname (arg
))) {
158 if ((p
= (struct pqpair
*) mh_xcalloc ((size_t) 1, sizeof(*p
))) == NULL
)
159 adios (NULL
, "unable to allocate pqpair memory");
160 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
161 p
->pq_text
= getcpy (cp
);
162 p
->pq_error
= getcpy (error
);
166 p
->pq_text
= getcpy (mp
->m_text
);
169 q
= (q
->pq_next
= p
);
172 for (p
= pq
.pq_next
; p
; p
= q
) {
174 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
176 cptr
= fmt_findcomp ("text");
180 cptr
->c_text
= p
->pq_text
;
183 cptr
= fmt_findcomp ("error");
187 cptr
->c_text
= p
->pq_error
;
191 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
192 fputs (charstring_buffer (scanl
), stdout
);
193 charstring_free (scanl
);