mhlist: Always display the column header (removed -headers).
[mmh] / uip / mhlistsbr.c
1 /*
2 ** mhlistsbr.c -- routines to list information about the
3 **             -- contents of MIME messages
4 **
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/utils.h>
19
20 /* mhmisc.c */
21 int part_ok(CT, int);
22 int type_ok(CT, int);
23 void flush_errors(void);
24
25 /*
26 ** prototypes
27 */
28 void list_all_messages(CT *, int, int);
29 int list_switch(CT, int, int, int);
30 int list_content(CT, int, int, int);
31
32 /*
33 ** static prototypes
34 */
35 static void list_single_message(CT, int, int);
36 static int list_debug(CT);
37 static int list_multi(CT, int, int, int);
38 static int list_partial(CT, int, int, int);
39 static int list_encoding(CT);
40
41
42 /*
43 ** various formats for -list option
44 */
45 #define LSTFMT1    "%4s %-5s %-24s %5s %-36s\n"
46 #define LSTFMT2a   "%4d "
47 #define LSTFMT2b   "%-5s %-24.24s "
48 #define LSTFMT2c1  "%5lu"
49 #define LSTFMT2c2  "%4lu%c"
50 #define LSTFMT2c3  "huge "
51 #define LSTFMT2c4  "     "
52 #define LSTFMT2d1  " %-36.36s"
53 #define LSTFMT2d2  "\t     %-65.65s\n"
54
55
56 /*
57 ** Top level entry point to list group of messages
58 */
59 void
60 list_all_messages(CT *cts, int verbose, int debug)
61 {
62         CT ct, *ctp;
63
64         printf(LSTFMT1, "msg", "part", "type/subtype", "size", "description");
65         for (ctp = cts; *ctp; ctp++) {
66                 ct = *ctp;
67                 list_single_message(ct, verbose, debug);
68         }
69         flush_errors();
70 }
71
72
73 /*
74 ** Entry point to list a single message
75 */
76 static void
77 list_single_message(CT ct, int verbose, int debug)
78 {
79         if (type_ok(ct, 1)) {
80                 umask(ct->c_umask);
81                 list_switch(ct, 1, verbose, debug);
82                 if (ct->c_fp) {
83                         fclose(ct->c_fp);
84                         ct->c_fp = NULL;
85                 }
86                 if (ct->c_ceclosefnx)
87                         (*ct->c_ceclosefnx) (ct);
88         }
89 }
90
91
92 /*
93 ** Primary switching routine to list information about a content
94 */
95 int
96 list_switch(CT ct, int toplevel, int verbose, int debug)
97 {
98         switch (ct->c_type) {
99         case CT_MULTIPART:
100                 return list_multi(ct, toplevel, verbose, debug);
101                 break;
102
103         case CT_MESSAGE:
104                 if (ct->c_subtype == MESSAGE_PARTIAL) {
105                         return list_partial(ct, toplevel, verbose, debug);
106                 } else {
107                         return list_content(ct, toplevel, verbose, debug);
108                 }
109                 break;
110
111         case CT_TEXT:
112         case CT_AUDIO:
113         case CT_IMAGE:
114         case CT_VIDEO:
115         case CT_APPLICATION:
116                 return list_content(ct, toplevel, verbose, debug);
117                 break;
118
119         default:
120                 /* list_debug (ct); */
121                 adios(NULL, "unknown content type %d", ct->c_type);
122                 break;
123         }
124
125         return 0;  /* NOT REACHED */
126 }
127
128
129 #define empty(s) ((s) ? (s) : "")
130
131 /*
132 ** Method for listing information about a simple/generic content
133 */
134 int
135 list_content(CT ct, int toplevel, int verbose, int debug)
136 {
137         unsigned long size;
138         char *cp, buffer[BUFSIZ];
139         CI ci = &ct->c_ctinfo;
140
141         printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : "     ",
142                         atoi(mhbasename(empty(ct->c_file))));
143         snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
144                         empty(ci->ci_subtype));
145         printf(LSTFMT2b, empty(ct->c_partno), buffer);
146
147         if (ct->c_cesizefnx)
148                 size = (*ct->c_cesizefnx) (ct);
149         else
150                 size = ct->c_end - ct->c_begin;
151
152         /* find correct scale for size (Kilo/Mega/Giga/Tera) */
153         for (cp = " KMGT"; size > 9999; size >>= 10)
154                 if (!*++cp)
155                         break;
156
157         /* print size of this body part */
158         switch (*cp) {
159         case ' ':
160                 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
161                         printf(LSTFMT2c1, size);
162                 else
163                         printf(LSTFMT2c4);
164                 break;
165
166         default:
167                 printf(LSTFMT2c2, size, *cp);
168                 break;
169
170         case '\0':
171                 printf(LSTFMT2c3);
172         }
173
174         /* print Content-Description */
175         if (ct->c_descr) {
176                 char *dp;
177
178                 dp = trimcpy(cp = getcpy(ct->c_descr));
179                 free(cp);
180                 printf(LSTFMT2d1, dp);
181                 free(dp);
182         }
183
184         printf("\n");
185
186         if (verbose) {
187                 char **ap, **ep;
188                 CI ci = &ct->c_ctinfo;
189
190                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
191                         printf("\t\t%s=\"%s\"\n", *ap, *ep);
192                 }
193
194                 /*
195                 ** If verbose, print any RFC-822 comments in the
196                 ** Content-Type line.
197                 */
198                 if (ci->ci_comment) {
199                         char *dp;
200
201                         dp = trimcpy(cp = add(ci->ci_comment, NULL));
202                         free (cp);
203                         snprintf(buffer, sizeof(buffer), "(%s)", dp);
204                         free(dp);
205                         printf(LSTFMT2d2, buffer);
206                 }
207         }
208
209         if (debug)
210                 list_debug(ct);
211
212         return OK;
213 }
214
215
216 /*
217 ** Print debugging information about a content
218 */
219 static int
220 list_debug(CT ct)
221 {
222         char **ap, **ep;
223         CI ci = &ct->c_ctinfo;
224
225         fflush(stdout);
226         fprintf(stderr, "  partno \"%s\"\n", empty(ct->c_partno));
227
228         /* print MIME-Version line */
229         if (ct->c_vrsn)
230                 fprintf(stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
231
232         /* print Content-Type line */
233         if (ct->c_ctline)
234                 fprintf(stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
235
236         /* print parsed elements of content type */
237         fprintf(stderr, "    type    \"%s\"\n", empty(ci->ci_type));
238         fprintf(stderr, "    subtype \"%s\"\n", empty(ci->ci_subtype));
239         fprintf(stderr, "    comment \"%s\"\n", empty(ci->ci_comment));
240         fprintf(stderr, "    magic   \"%s\"\n", empty(ci->ci_magic));
241
242         /* print parsed parameters attached to content type */
243         fprintf(stderr, "    parameters\n");
244         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
245                 fprintf(stderr, "      %s=\"%s\"\n", *ap, *ep);
246
247         /* print internal flags for type/subtype */
248         fprintf(stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
249                          ct->c_type, ct->c_subtype,
250                          (unsigned int)(unsigned long) ct->c_ctparams);
251
252         fprintf(stderr, "    charset  \"%s\"\n", empty(ct->c_charset));
253         fprintf(stderr, "    showproc  \"%s\"\n", empty(ct->c_showproc));
254         fprintf(stderr, "    storeproc \"%s\"\n", empty(ct->c_storeproc));
255
256         /* print transfer encoding information */
257         if (ct->c_celine)
258                 fprintf(stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
259
260         /* print internal flags for transfer encoding */
261         fprintf(stderr, "    transfer encoding 0x%x params 0x%x\n",
262                         ct->c_encoding,
263                         (unsigned int)(unsigned long) ct->c_cefile);
264
265         /* print Content-ID */
266         if (ct->c_id)
267                 fprintf(stderr, "  %s:%s", ID_FIELD, ct->c_id);
268
269         /* print Content-Description */
270         if (ct->c_descr)
271                 fprintf(stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
272
273         fprintf(stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
274                         (unsigned int)(unsigned long) ct->c_fp,
275                         empty(ct->c_file), ct->c_begin, ct->c_end);
276
277         /* print more information about transfer encoding */
278         list_encoding(ct);
279
280         return OK;
281 }
282
283 #undef empty
284
285
286 /*
287 ** list content information for type "multipart"
288 */
289 static int
290 list_multi(CT ct, int toplevel, int verbose, int debug)
291 {
292         struct multipart *m = (struct multipart *) ct->c_ctparams;
293         struct part *part;
294
295         /* list the content for toplevel of this multipart */
296         list_content(ct, toplevel, verbose, debug);
297
298         /* now list for all the subparts */
299         for (part = m->mp_parts; part; part = part->mp_next) {
300                 CT p = part->mp_part;
301
302                 if (part_ok(p, 1) && type_ok(p, 1))
303                         list_switch(p, 0, verbose, debug);
304         }
305
306         return OK;
307 }
308
309
310 /*
311 ** list content information for type "message/partial"
312 */
313 static int
314 list_partial(CT ct, int toplevel, int verbose, int debug)
315 {
316         struct partial *p = (struct partial *) ct->c_ctparams;
317
318         list_content(ct, toplevel, verbose, debug);
319         if (verbose) {
320                 printf("\t     [message %s, part %d",
321                                 p->pm_partid, p->pm_partno);
322                 if (p->pm_maxno)
323                         printf(" of %d", p->pm_maxno);
324                 printf("]\n");
325         }
326
327         return OK;
328 }
329
330
331 /*
332 ** list information about the Content-Transfer-Encoding
333 ** used by a content.
334 */
335 static int
336 list_encoding(CT ct)
337 {
338         CE ce;
339
340         if ((ce = ct->c_cefile))
341                 fprintf(stderr, "    decoded fp 0x%x file \"%s\"\n",
342                         (unsigned int)(unsigned long) ce->ce_fp,
343                         ce->ce_file ? ce->ce_file : "");
344
345         return OK;
346 }