3 * mhlistsbr.c -- routines to list information about the
4 * -- contents of MIME messages
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
13 #include <h/signals.h>
19 #include <h/mhparse.h>
23 int part_ok (CT, int);
24 int type_ok (CT, int);
25 void flush_errors (void);
30 void list_all_messages (CT *, int, int, int, int);
31 int list_switch (CT, int, int, int, int);
32 int list_content (CT, int, int, int, int);
37 static void list_single_message (CT, int, int, int);
38 static int list_debug (CT);
39 static int list_multi (CT, int, int, int, int);
40 static int list_partial (CT, int, int, int, int);
41 static int list_external (CT, int, int, int, int);
42 static int list_application (CT, int, int, int, int);
43 static int list_encoding (CT);
47 * various formats for -list option
49 #define LSTFMT1 "%4s %-5s %-24s %5s %-36s\n"
50 #define LSTFMT2a "%4d "
51 #define LSTFMT2b "%-5s %-24.24s "
52 #define LSTFMT2c1 "%5lu"
53 #define LSTFMT2c2 "%4lu%c"
54 #define LSTFMT2c3 "huge "
56 #define LSTFMT2d1 " %-36.36s"
57 #define LSTFMT2d2 "\t %-65.65s\n"
61 * Top level entry point to list group of messages
65 list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug)
70 printf (LSTFMT1, "msg", "part", "type/subtype", "size", "description");
72 for (ctp = cts; *ctp; ctp++) {
74 list_single_message (ct, realsize, verbose, debug);
82 * Entry point to list a single message
86 list_single_message (CT ct, int realsize, int verbose, int debug)
88 if (type_ok (ct, 1)) {
90 list_switch (ct, 1, realsize, verbose, debug);
96 (*ct->c_ceclosefnx) (ct);
102 * Primary switching routine to list information about a content
106 list_switch (CT ct, int toplevel, int realsize, int verbose, int debug)
108 switch (ct->c_type) {
110 return list_multi (ct, toplevel, realsize, verbose, debug);
114 switch (ct->c_subtype) {
115 case MESSAGE_PARTIAL:
116 return list_partial (ct, toplevel, realsize, verbose, debug);
119 case MESSAGE_EXTERNAL:
120 return list_external (ct, toplevel, realsize, verbose, debug);
125 return list_content (ct, toplevel, realsize, verbose, debug);
134 return list_content (ct, toplevel, realsize, verbose, debug);
138 return list_application (ct, toplevel, realsize, verbose, debug);
142 /* list_debug (ct); */
143 adios (NULL, "unknown content type %d", ct->c_type);
147 return 0; /* NOT REACHED */
151 #define empty(s) ((s) ? (s) : "")
154 * Method for listing information about a simple/generic content
158 list_content (CT ct, int toplevel, int realsize, int verbose, int debug)
161 char *cp, buffer[BUFSIZ];
162 CI ci = &ct->c_ctinfo;
164 printf (toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
165 atoi (r1bindex (empty (ct->c_file), '/')));
166 snprintf (buffer, sizeof(buffer), "%s/%s", empty (ci->ci_type),
167 empty (ci->ci_subtype));
168 printf (LSTFMT2b, empty (ct->c_partno), buffer);
170 if (ct->c_cesizefnx && realsize)
171 size = (*ct->c_cesizefnx) (ct);
173 size = ct->c_end - ct->c_begin;
175 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
176 for (cp = " KMGT"; size > 9999; size >>= 10)
180 /* print size of this body part */
183 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
184 printf (LSTFMT2c1, size);
190 printf (LSTFMT2c2, size, *cp);
197 /* print Content-Description */
201 dp = trimcpy (cp = add (ct->c_descr, NULL));
203 printf (LSTFMT2d1, dp);
210 * If verbose, print any RFC-822 comments in the
213 if (verbose && ci->ci_comment) {
216 dp = trimcpy (cp = add (ci->ci_comment, NULL));
218 snprintf (buffer, sizeof(buffer), "(%s)", dp);
220 printf (LSTFMT2d2, buffer);
231 * Print debugging information about a content
238 CI ci = &ct->c_ctinfo;
241 fprintf (stderr, " partno \"%s\"\n", empty (ct->c_partno));
243 /* print MIME-Version line */
245 fprintf (stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
247 /* print Content-Type line */
249 fprintf (stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
251 /* print parsed elements of content type */
252 fprintf (stderr, " type \"%s\"\n", empty (ci->ci_type));
253 fprintf (stderr, " subtype \"%s\"\n", empty (ci->ci_subtype));
254 fprintf (stderr, " comment \"%s\"\n", empty (ci->ci_comment));
255 fprintf (stderr, " magic \"%s\"\n", empty (ci->ci_magic));
257 /* print parsed parameters attached to content type */
258 fprintf (stderr, " parameters\n");
259 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
260 fprintf (stderr, " %s=\"%s\"\n", *ap, *ep);
262 /* print internal flags for type/subtype */
263 fprintf (stderr, " type 0x%x subtype 0x%x params 0x%x\n",
264 ct->c_type, ct->c_subtype,
265 (unsigned int)(unsigned long) ct->c_ctparams);
267 fprintf (stderr, " showproc \"%s\"\n", empty (ct->c_showproc));
268 fprintf (stderr, " termproc \"%s\"\n", empty (ct->c_termproc));
269 fprintf (stderr, " storeproc \"%s\"\n", empty (ct->c_storeproc));
271 /* print transfer encoding information */
273 fprintf (stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
275 /* print internal flags for transfer encoding */
276 fprintf (stderr, " transfer encoding 0x%x params 0x%x\n",
277 ct->c_encoding, (unsigned int)(unsigned long) ct->c_cefile);
279 /* print Content-ID */
281 fprintf (stderr, " %s:%s", ID_FIELD, ct->c_id);
283 /* print Content-Description */
285 fprintf (stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
287 fprintf (stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
288 (unsigned int)(unsigned long) ct->c_fp, empty (ct->c_file),
289 ct->c_begin, ct->c_end);
291 /* print more information about transfer encoding */
301 * list content information for type "multipart"
305 list_multi (CT ct, int toplevel, int realsize, int verbose, int debug)
307 struct multipart *m = (struct multipart *) ct->c_ctparams;
310 /* list the content for toplevel of this multipart */
311 list_content (ct, toplevel, realsize, verbose, debug);
313 /* now list for all the subparts */
314 for (part = m->mp_parts; part; part = part->mp_next) {
315 CT p = part->mp_part;
317 if (part_ok (p, 1) && type_ok (p, 1))
318 list_switch (p, 0, realsize, verbose, debug);
326 * list content information for type "message/partial"
330 list_partial (CT ct, int toplevel, int realsize, int verbose, int debug)
332 struct partial *p = (struct partial *) ct->c_ctparams;
334 list_content (ct, toplevel, realsize, verbose, debug);
336 printf ("\t [message %s, part %d", p->pm_partid, p->pm_partno);
338 printf (" of %d", p->pm_maxno);
347 * list content information for type "message/external"
351 list_external (CT ct, int toplevel, int realsize, int verbose, int debug)
353 struct exbody *e = (struct exbody *) ct->c_ctparams;
356 * First list the information for the
357 * message/external content itself.
359 list_content (ct, toplevel, realsize, verbose, debug);
363 printf ("\t name=\"%s\"\n", e->eb_name);
365 printf ("\t directory=\"%s\"\n", e->eb_dir);
367 printf ("\t site=\"%s\"\n", e->eb_site);
369 printf ("\t server=\"%s\"\n", e->eb_server);
371 printf ("\t subject=\"%s\"\n", e->eb_subject);
373 /* This must be defined */
374 printf ("\t access-type=\"%s\"\n", e->eb_access);
377 printf ("\t mode=\"%s\"\n", e->eb_mode);
378 if (e->eb_permission)
379 printf ("\t permission=\"%s\"\n", e->eb_permission);
381 if (e->eb_flags == NOTOK)
382 printf ("\t [service unavailable]\n");
386 * Now list the information for the external content
387 * to which this content points.
389 list_content (e->eb_content, 0, realsize, verbose, debug);
396 * list content information for type "application"
400 list_application (CT ct, int toplevel, int realsize, int verbose, int debug)
402 list_content (ct, toplevel, realsize, verbose, debug);
405 CI ci = &ct->c_ctinfo;
407 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
408 printf ("\t %s=\"%s\"\n", *ap, *ep);
416 * list information about the Content-Transfer-Encoding
421 list_encoding (CT ct)
425 if ((ce = ct->c_cefile))
426 fprintf (stderr, " decoded fp 0x%x file \"%s\"\n",
427 (unsigned int)(unsigned long) ce->ce_fp,
428 ce->ce_file ? ce->ce_file : "");