2 ** mhlistsbr.c -- routines to list information about the
3 ** -- contents of MIME messages
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.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
23 void flush_errors(void);
28 void list_all_messages(CT *, int, int, int, int);
29 int list_switch(CT, int, int, int, int);
30 int list_content(CT, int, int, int, int);
35 static void list_single_message(CT, int, int, int);
36 static int list_debug(CT);
37 static int list_multi(CT, int, int, int, int);
38 static int list_partial(CT, int, int, int, int);
39 static int list_external(CT, int, int, int, int);
40 static int list_application(CT, int, int, int, int);
41 static int list_encoding(CT);
45 ** various formats for -list option
47 #define LSTFMT1 "%4s %-5s %-24s %5s %-36s\n"
48 #define LSTFMT2a "%4d "
49 #define LSTFMT2b "%-5s %-24.24s "
50 #define LSTFMT2c1 "%5lu"
51 #define LSTFMT2c2 "%4lu%c"
52 #define LSTFMT2c3 "huge "
54 #define LSTFMT2d1 " %-36.36s"
55 #define LSTFMT2d2 "\t %-65.65s\n"
59 ** Top level entry point to list group of messages
63 list_all_messages(CT *cts, int headers, int realsize, int verbose, int debug)
68 printf(LSTFMT1, "msg", "part", "type/subtype", "size",
71 for (ctp = cts; *ctp; ctp++) {
73 list_single_message(ct, realsize, verbose, debug);
81 ** Entry point to list a single message
85 list_single_message(CT ct, int realsize, int verbose, int debug)
89 list_switch(ct, 1, realsize, verbose, debug);
95 (*ct->c_ceclosefnx) (ct);
101 ** Primary switching routine to list information about a content
105 list_switch(CT ct, int toplevel, int realsize, int verbose, int debug)
107 switch (ct->c_type) {
109 return list_multi(ct, toplevel, realsize, verbose, debug);
113 switch (ct->c_subtype) {
114 case MESSAGE_PARTIAL:
115 return list_partial(ct, toplevel, realsize, verbose,
119 case MESSAGE_EXTERNAL:
120 return list_external(ct, toplevel, realsize, verbose,
126 return list_content(ct, toplevel, realsize, verbose,
136 return list_content(ct, toplevel, realsize, verbose, debug);
140 return list_application(ct, toplevel, realsize, verbose,
145 /* list_debug (ct); */
146 adios(NULL, "unknown content type %d", ct->c_type);
150 return 0; /* NOT REACHED */
154 #define empty(s) ((s) ? (s) : "")
157 ** Method for listing information about a simple/generic content
161 list_content(CT ct, int toplevel, int realsize, int verbose, int debug)
164 char *cp, buffer[BUFSIZ];
165 CI ci = &ct->c_ctinfo;
167 printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
168 atoi(mhbasename(empty(ct->c_file))));
169 snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
170 empty(ci->ci_subtype));
171 printf(LSTFMT2b, empty(ct->c_partno), buffer);
173 if (ct->c_cesizefnx && realsize)
174 size = (*ct->c_cesizefnx) (ct);
176 size = ct->c_end - ct->c_begin;
178 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
179 for (cp = " KMGT"; size > 9999; size >>= 10)
183 /* print size of this body part */
186 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
187 printf(LSTFMT2c1, size);
193 printf(LSTFMT2c2, size, *cp);
200 /* print Content-Description */
204 dp = trimcpy(cp = getcpy(ct->c_descr));
206 printf(LSTFMT2d1, dp);
213 ** If verbose, print any RFC-822 comments in the
214 ** Content-Type line.
216 if (verbose && ci->ci_comment) {
219 dp = trimcpy(cp = getcpy(ci->ci_comment));
221 snprintf(buffer, sizeof(buffer), "(%s)", dp);
223 printf(LSTFMT2d2, buffer);
234 ** Print debugging information about a content
241 CI ci = &ct->c_ctinfo;
244 fprintf(stderr, " partno \"%s\"\n", empty(ct->c_partno));
246 /* print MIME-Version line */
248 fprintf(stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
250 /* print Content-Type line */
252 fprintf(stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
254 /* print parsed elements of content type */
255 fprintf(stderr, " type \"%s\"\n", empty(ci->ci_type));
256 fprintf(stderr, " subtype \"%s\"\n", empty(ci->ci_subtype));
257 fprintf(stderr, " comment \"%s\"\n", empty(ci->ci_comment));
258 fprintf(stderr, " magic \"%s\"\n", empty(ci->ci_magic));
260 /* print parsed parameters attached to content type */
261 fprintf(stderr, " parameters\n");
262 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
263 fprintf(stderr, " %s=\"%s\"\n", *ap, *ep);
265 /* print internal flags for type/subtype */
266 fprintf(stderr, " type 0x%x subtype 0x%x params 0x%x\n",
267 ct->c_type, ct->c_subtype,
268 (unsigned int)(unsigned long) ct->c_ctparams);
270 fprintf(stderr, " showproc \"%s\"\n", empty(ct->c_showproc));
271 fprintf(stderr, " termproc \"%s\"\n", empty(ct->c_termproc));
272 fprintf(stderr, " storeproc \"%s\"\n", empty(ct->c_storeproc));
274 /* print transfer encoding information */
276 fprintf(stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
278 /* print internal flags for transfer encoding */
279 fprintf(stderr, " transfer encoding 0x%x params 0x%x\n",
281 (unsigned int)(unsigned long) ct->c_cefile);
283 /* print Content-ID */
285 fprintf(stderr, " %s:%s", ID_FIELD, ct->c_id);
287 /* print Content-Description */
289 fprintf(stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
291 fprintf(stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
292 (unsigned int)(unsigned long) ct->c_fp,
293 empty(ct->c_file), ct->c_begin, ct->c_end);
295 /* print more information about transfer encoding */
305 ** list content information for type "multipart"
309 list_multi(CT ct, int toplevel, int realsize, int verbose, int debug)
311 struct multipart *m = (struct multipart *) ct->c_ctparams;
314 /* list the content for toplevel of this multipart */
315 list_content(ct, toplevel, realsize, verbose, debug);
317 /* now list for all the subparts */
318 for (part = m->mp_parts; part; part = part->mp_next) {
319 CT p = part->mp_part;
321 if (part_ok(p, 1) && type_ok(p, 1))
322 list_switch(p, 0, realsize, verbose, debug);
330 ** list content information for type "message/partial"
334 list_partial(CT ct, int toplevel, int realsize, int verbose, int debug)
336 struct partial *p = (struct partial *) ct->c_ctparams;
338 list_content(ct, toplevel, realsize, verbose, debug);
340 printf("\t [message %s, part %d",
341 p->pm_partid, p->pm_partno);
343 printf(" of %d", p->pm_maxno);
352 ** list content information for type "message/external"
356 list_external(CT ct, int toplevel, int realsize, int verbose, int debug)
358 struct exbody *e = (struct exbody *) ct->c_ctparams;
361 * First list the information for the
362 * message/external content itself.
364 list_content(ct, toplevel, realsize, verbose, debug);
368 printf("\t name=\"%s\"\n", e->eb_name);
370 printf("\t directory=\"%s\"\n", e->eb_dir);
372 printf("\t site=\"%s\"\n", e->eb_site);
374 printf("\t server=\"%s\"\n", e->eb_server);
376 printf("\t subject=\"%s\"\n", e->eb_subject);
378 /* This must be defined */
379 printf("\t access-type=\"%s\"\n", e->eb_access);
382 printf("\t mode=\"%s\"\n", e->eb_mode);
383 if (e->eb_permission)
384 printf("\t permission=\"%s\"\n", e->eb_permission);
386 if (e->eb_flags == NOTOK)
387 printf("\t [service unavailable]\n");
391 ** Now list the information for the external content
392 ** to which this content points.
394 list_content(e->eb_content, 0, realsize, verbose, debug);
401 ** list content information for type "application"
405 list_application(CT ct, int toplevel, int realsize, int verbose, int debug)
407 list_content(ct, toplevel, realsize, verbose, debug);
410 CI ci = &ct->c_ctinfo;
412 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
413 printf("\t %s=\"%s\"\n", *ap, *ep);
421 ** list information about the Content-Transfer-Encoding
422 ** used by a content.
430 if ((ce = ct->c_cefile))
431 fprintf(stderr, " decoded fp 0x%x file \"%s\"\n",
432 (unsigned int)(unsigned long) ce->ce_fp,
433 ce->ce_file ? ce->ce_file : "");