Remove unused code
[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 <errno.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/utils.h>
17 #include <sys/stat.h>
18 #include <sysexits.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(EX_DATAERR, 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                 dp = trimcpy(cp = mh_xstrdup(ct->c_descr));
178                 mh_free0(&cp);
179                 printf(LSTFMT2d1, dp);
180                 mh_free0(&dp);
181         }
182
183         printf("\n");
184
185         if (verbose) {
186                 char **ap, **ep;
187                 CI ci = &ct->c_ctinfo;
188
189                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
190                         printf("\t\t%s=\"%s\"\n", *ap, *ep);
191                 }
192
193                 /*
194                 ** If verbose, print any RFC-822 comments in the
195                 ** Content-Type line.
196                 */
197                 if (ci->ci_comment) {
198                         char *dp;
199
200                         dp = trimcpy(cp = add(ci->ci_comment, NULL));
201                         free (cp);
202                         snprintf(buffer, sizeof(buffer), "(%s)", dp);
203                         mh_free0(&dp);
204                         printf(LSTFMT2d2, buffer);
205                 }
206         }
207
208         if (debug)
209                 list_debug(ct);
210
211         return OK;
212 }
213
214
215 /*
216 ** Print debugging information about a content
217 */
218 static int
219 list_debug(CT ct)
220 {
221         char **ap, **ep;
222         CI ci = &ct->c_ctinfo;
223
224         fflush(stdout);
225         fprintf(stderr, "  partno \"%s\"\n", empty(ct->c_partno));
226
227         /* print MIME-Version line */
228         if (ct->c_vrsn)
229                 fprintf(stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
230
231         /* print Content-Type line */
232         if (ct->c_ctline)
233                 fprintf(stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
234
235         /* print parsed elements of content type */
236         fprintf(stderr, "    type    \"%s\"\n", empty(ci->ci_type));
237         fprintf(stderr, "    subtype \"%s\"\n", empty(ci->ci_subtype));
238         fprintf(stderr, "    comment \"%s\"\n", empty(ci->ci_comment));
239         fprintf(stderr, "    magic   \"%s\"\n", empty(ci->ci_magic));
240
241         /* print parsed parameters attached to content type */
242         fprintf(stderr, "    parameters\n");
243         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
244                 fprintf(stderr, "      %s=\"%s\"\n", *ap, *ep);
245
246         /* print internal flags for type/subtype */
247         fprintf(stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
248                          ct->c_type, ct->c_subtype,
249                          (unsigned int)(unsigned long) ct->c_ctparams);
250
251         fprintf(stderr, "    charset  \"%s\"\n", empty(ct->c_charset));
252         fprintf(stderr, "    showproc  \"%s\"\n", empty(ct->c_showproc));
253         fprintf(stderr, "    storeproc \"%s\"\n", empty(ct->c_storeproc));
254
255         /* print transfer encoding information */
256         if (ct->c_celine)
257                 fprintf(stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
258
259         /* print internal flags for transfer encoding */
260         fprintf(stderr, "    transfer encoding 0x%x params 0x%x\n",
261                         ct->c_encoding,
262                         (unsigned int)(unsigned long) ct->c_cefile);
263
264         /* print Content-ID */
265         if (ct->c_id)
266                 fprintf(stderr, "  %s:%s", ID_FIELD, ct->c_id);
267
268         /* print Content-Description */
269         if (ct->c_descr)
270                 fprintf(stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
271
272         fprintf(stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
273                         (unsigned int)(unsigned long) ct->c_fp,
274                         empty(ct->c_file), ct->c_begin, ct->c_end);
275
276         /* print more information about transfer encoding */
277         list_encoding(ct);
278
279         return OK;
280 }
281
282 #undef empty
283
284
285 /*
286 ** list content information for type "multipart"
287 */
288 static int
289 list_multi(CT ct, int toplevel, int verbose, int debug)
290 {
291         struct multipart *m = (struct multipart *) ct->c_ctparams;
292         struct part *part;
293
294         /* list the content for toplevel of this multipart */
295         list_content(ct, toplevel, verbose, debug);
296
297         /* now list for all the subparts */
298         for (part = m->mp_parts; part; part = part->mp_next) {
299                 CT p = part->mp_part;
300
301                 if (part_ok(p, 1) && type_ok(p, 1))
302                         list_switch(p, 0, verbose, debug);
303         }
304
305         return OK;
306 }
307
308
309 /*
310 ** list content information for type "message/partial"
311 */
312 static int
313 list_partial(CT ct, int toplevel, int verbose, int debug)
314 {
315         struct partial *p = (struct partial *) ct->c_ctparams;
316
317         list_content(ct, toplevel, verbose, debug);
318         if (verbose) {
319                 printf("\t     [message %s, part %d",
320                                 p->pm_partid, p->pm_partno);
321                 if (p->pm_maxno)
322                         printf(" of %d", p->pm_maxno);
323                 printf("]\n");
324         }
325
326         return OK;
327 }
328
329
330 /*
331 ** list information about the Content-Transfer-Encoding
332 ** used by a content.
333 */
334 static int
335 list_encoding(CT ct)
336 {
337         CE ce;
338
339         if ((ce = ct->c_cefile))
340                 fprintf(stderr, "    decoded fp 0x%x file \"%s\"\n",
341                         (unsigned int)(unsigned long) ce->ce_fp,
342                         ce->ce_file ? ce->ce_file : "");
343
344         return OK;
345 }