]>
diplodocus.org Git - nmh/blob - uip/ap.c
3 * ap.c -- parse addresses 822-style
10 #include <h/fmt_scan.h>
15 #define WBUFSIZ BUFSIZ
17 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
19 static struct swit switches
[] = {
21 { "form formatfile", 0 },
23 { "format string", 5 },
29 { "width columns", 0 },
37 static struct format
*fmt
;
44 int sc_width (void); /* from termsbr.c */
49 static int process (char *, int, int);
53 main (int argc
, char **argv
)
55 int addrp
= 0, normalize
= AD_HOST
;
56 int width
= 0, status
= 0;
57 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
58 char buf
[BUFSIZ
], **argp
;
59 char **arguments
, *addrs
[NADDRS
];
62 setlocale(LC_ALL
, "");
64 invo_name
= r1bindex (argv
[0], '/');
66 /* read user profile/context */
70 arguments
= getarguments (invo_name
, argc
, argv
, 1);
73 while ((cp
= *argp
++)) {
75 switch (smatch (++cp
, switches
)) {
77 ambigsw (cp
, switches
);
81 adios (NULL
, "-%s unknown", cp
);
84 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
86 print_help (buf
, switches
, 1);
89 print_version (invo_name
);
93 if (!(form
= *argp
++) || *form
== '-')
94 adios (NULL
, "missing argument to %s", argp
[-2]);
98 if (!(format
= *argp
++) || *format
== '-')
99 adios (NULL
, "missing argument to %s", argp
[-2]);
104 if (!(cp
= *argp
++) || *cp
== '-')
105 adios (NULL
, "missing argument to %s", argp
[-2]);
118 adios (NULL
, "more than %d addresses", NADDRS
);
125 adios (NULL
, "usage: %s [switches] addrs ...", invo_name
);
127 /* get new format string */
128 nfs
= new_fs (form
, format
, FORMAT
);
131 if ((width
= sc_width ()) < WIDTH
/ 2)
137 fmt_norm
= normalize
;
138 fmt_compile (nfs
, &fmt
);
146 for (addrp
= 0; addrs
[addrp
]; addrp
++)
147 status
+= process (addrs
[addrp
], width
, normalize
);
155 struct pqpair
*pq_next
;
160 process (char *arg
, int length
, int norm
)
164 char buffer
[WBUFSIZ
+ 1], error
[BUFSIZ
];
165 register struct comp
*cptr
;
166 register struct pqpair
*p
, *q
;
168 register struct mailname
*mp
;
170 (q
= &pq
)->pq_next
= NULL
;
171 while ((cp
= getname (arg
))) {
172 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
173 adios (NULL
, "unable to allocate pqpair memory");
174 if ((mp
= getm (cp
, NULL
, 0, norm
, error
)) == NULL
) {
175 p
->pq_text
= getcpy (cp
);
176 p
->pq_error
= getcpy (error
);
180 p
->pq_text
= getcpy (mp
->m_text
);
183 q
= (q
->pq_next
= p
);
186 for (p
= pq
.pq_next
; p
; p
= q
) {
187 FINDCOMP (cptr
, "text");
189 cptr
->c_text
= p
->pq_text
;
190 FINDCOMP (cptr
, "error");
192 cptr
->c_text
= p
->pq_error
;
194 fmt_scan (fmt
, buffer
, length
, dat
);
195 fputs (buffer
, stdout
);