Relayouted all switch statements: case aligns with switch.
[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/mts.h>
16 #include <h/tws.h>
17 #include <h/mime.h>
18 #include <h/mhparse.h>
19 #include <h/utils.h>
20
21 /* mhmisc.c */
22 int part_ok(CT, int);
23 int type_ok(CT, int);
24 void flush_errors(void);
25
26 /*
27 ** prototypes
28 */
29 void list_all_messages(CT *, int, int, int, int);
30 int list_switch(CT, int, int, int, int);
31 int list_content(CT, int, int, int, int);
32
33 /*
34 ** static prototypes
35 */
36 static void list_single_message(CT, int, int, int);
37 static int list_debug(CT);
38 static int list_multi(CT, int, int, int, int);
39 static int list_partial(CT, int, int, int, int);
40 static int list_external(CT, int, int, int, int);
41 static int list_application(CT, int, int, int, int);
42 static int list_encoding(CT);
43
44
45 /*
46 ** various formats for -list option
47 */
48 #define LSTFMT1    "%4s %-5s %-24s %5s %-36s\n"
49 #define LSTFMT2a   "%4d "
50 #define LSTFMT2b   "%-5s %-24.24s "
51 #define LSTFMT2c1  "%5lu"
52 #define LSTFMT2c2  "%4lu%c"
53 #define LSTFMT2c3  "huge "
54 #define LSTFMT2c4  "     "
55 #define LSTFMT2d1  " %-36.36s"
56 #define LSTFMT2d2  "\t     %-65.65s\n"
57
58
59 /*
60 ** Top level entry point to list group of messages
61 */
62
63 void
64 list_all_messages(CT *cts, int headers, int realsize, int verbose, int debug)
65 {
66         CT ct, *ctp;
67
68         if (headers)
69                 printf(LSTFMT1, "msg", "part", "type/subtype", "size",
70                                 "description");
71
72         for (ctp = cts; *ctp; ctp++) {
73                 ct = *ctp;
74                 list_single_message(ct, realsize, verbose, debug);
75         }
76
77         flush_errors();
78 }
79
80
81 /*
82 ** Entry point to list a single message
83 */
84
85 static void
86 list_single_message(CT ct, int realsize, int verbose, int debug)
87 {
88         if (type_ok(ct, 1)) {
89                 umask(ct->c_umask);
90                 list_switch(ct, 1, realsize, verbose, debug);
91                 if (ct->c_fp) {
92                         fclose(ct->c_fp);
93                         ct->c_fp = NULL;
94                 }
95                 if (ct->c_ceclosefnx)
96                         (*ct->c_ceclosefnx) (ct);
97         }
98 }
99
100
101 /*
102 ** Primary switching routine to list information about a content
103 */
104
105 int
106 list_switch(CT ct, int toplevel, int realsize, int verbose, int debug)
107 {
108         switch (ct->c_type) {
109         case CT_MULTIPART:
110                 return list_multi(ct, toplevel, realsize, verbose, debug);
111                 break;
112
113         case CT_MESSAGE:
114                 switch (ct->c_subtype) {
115                 case MESSAGE_PARTIAL:
116                         return list_partial(ct, toplevel, realsize, verbose,
117                                                 debug);
118                         break;
119
120                 case MESSAGE_EXTERNAL:
121                         return list_external(ct, toplevel, realsize, verbose,
122                                                 debug);
123                         break;
124
125                 case MESSAGE_RFC822:
126                 default:
127                         return list_content(ct, toplevel, realsize, verbose,
128                                                 debug);
129                         break;
130                 }
131                 break;
132
133         case CT_TEXT:
134         case CT_AUDIO:
135         case CT_IMAGE:
136         case CT_VIDEO:
137                 return list_content(ct, toplevel, realsize, verbose, debug);
138                 break;
139
140         case CT_APPLICATION:
141                 return list_application(ct, toplevel, realsize, verbose,
142                                 debug);
143                 break;
144
145         default:
146                 /* list_debug (ct); */
147                 adios(NULL, "unknown content type %d", ct->c_type);
148                 break;
149         }
150
151         return 0;  /* NOT REACHED */
152 }
153
154
155 #define empty(s) ((s) ? (s) : "")
156
157 /*
158 ** Method for listing information about a simple/generic content
159 */
160
161 int
162 list_content(CT ct, int toplevel, int realsize, int verbose, int debug)
163 {
164         unsigned long size;
165         char *cp, buffer[BUFSIZ];
166         CI ci = &ct->c_ctinfo;
167
168         printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : "     ",
169                         atoi(mhbasename(empty(ct->c_file))));
170         snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
171                         empty(ci->ci_subtype));
172         printf(LSTFMT2b, empty(ct->c_partno), buffer);
173
174         if (ct->c_cesizefnx && realsize)
175                 size = (*ct->c_cesizefnx) (ct);
176         else
177                 size = ct->c_end - ct->c_begin;
178
179         /* find correct scale for size (Kilo/Mega/Giga/Tera) */
180         for (cp = " KMGT"; size > 9999; size >>= 10)
181                 if (!*++cp)
182                         break;
183
184         /* print size of this body part */
185         switch (*cp) {
186         case ' ':
187                 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
188                         printf(LSTFMT2c1, size);
189                 else
190                         printf(LSTFMT2c4);
191                 break;
192
193         default:
194                 printf(LSTFMT2c2, size, *cp);
195                 break;
196
197         case '\0':
198                 printf(LSTFMT2c3);
199         }
200
201         /* print Content-Description */
202         if (ct->c_descr) {
203                 char *dp;
204
205                 dp = trimcpy(cp = getcpy(ct->c_descr));
206                 free(cp);
207                 printf(LSTFMT2d1, dp);
208                 free(dp);
209         }
210
211         printf("\n");
212
213         /*
214         ** If verbose, print any RFC-822 comments in the
215         ** Content-Type line.
216         */
217         if (verbose && ci->ci_comment) {
218                 char *dp;
219
220                 dp = trimcpy(cp = getcpy(ci->ci_comment));
221                 free(cp);
222                 snprintf(buffer, sizeof(buffer), "(%s)", dp);
223                 free(dp);
224                 printf(LSTFMT2d2, buffer);
225         }
226
227         if (debug)
228                 list_debug(ct);
229
230         return OK;
231 }
232
233
234 /*
235 ** Print debugging information about a content
236 */
237
238 static int
239 list_debug(CT ct)
240 {
241         char **ap, **ep;
242         CI ci = &ct->c_ctinfo;
243
244         fflush(stdout);
245         fprintf(stderr, "  partno \"%s\"\n", empty(ct->c_partno));
246
247         /* print MIME-Version line */
248         if (ct->c_vrsn)
249                 fprintf(stderr, "  %s:%s\n", VRSN_FIELD, ct->c_vrsn);
250
251         /* print Content-Type line */
252         if (ct->c_ctline)
253                 fprintf(stderr, "  %s:%s\n", TYPE_FIELD, ct->c_ctline);
254
255         /* print parsed elements of content type */
256         fprintf(stderr, "    type    \"%s\"\n", empty(ci->ci_type));
257         fprintf(stderr, "    subtype \"%s\"\n", empty(ci->ci_subtype));
258         fprintf(stderr, "    comment \"%s\"\n", empty(ci->ci_comment));
259         fprintf(stderr, "    magic   \"%s\"\n", empty(ci->ci_magic));
260
261         /* print parsed parameters attached to content type */
262         fprintf(stderr, "    parameters\n");
263         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
264                 fprintf(stderr, "      %s=\"%s\"\n", *ap, *ep);
265
266         /* print internal flags for type/subtype */
267         fprintf(stderr, "    type 0x%x subtype 0x%x params 0x%x\n",
268                          ct->c_type, ct->c_subtype,
269                          (unsigned int)(unsigned long) ct->c_ctparams);
270
271         fprintf(stderr, "    showproc  \"%s\"\n", empty(ct->c_showproc));
272         fprintf(stderr, "    termproc  \"%s\"\n", empty(ct->c_termproc));
273         fprintf(stderr, "    storeproc \"%s\"\n", empty(ct->c_storeproc));
274
275         /* print transfer encoding information */
276         if (ct->c_celine)
277                 fprintf(stderr, "  %s:%s", ENCODING_FIELD, ct->c_celine);
278
279         /* print internal flags for transfer encoding */
280         fprintf(stderr, "    transfer encoding 0x%x params 0x%x\n",
281                         ct->c_encoding,
282                         (unsigned int)(unsigned long) ct->c_cefile);
283
284         /* print Content-ID */
285         if (ct->c_id)
286                 fprintf(stderr, "  %s:%s", ID_FIELD, ct->c_id);
287
288         /* print Content-Description */
289         if (ct->c_descr)
290                 fprintf(stderr, "  %s:%s", DESCR_FIELD, ct->c_descr);
291
292         fprintf(stderr, "    read fp 0x%x file \"%s\" begin %ld end %ld\n",
293                         (unsigned int)(unsigned long) ct->c_fp,
294                         empty(ct->c_file), ct->c_begin, ct->c_end);
295
296         /* print more information about transfer encoding */
297         list_encoding(ct);
298
299         return OK;
300 }
301
302 #undef empty
303
304
305 /*
306 ** list content information for type "multipart"
307 */
308
309 static int
310 list_multi(CT ct, int toplevel, int realsize, int verbose, int debug)
311 {
312         struct multipart *m = (struct multipart *) ct->c_ctparams;
313         struct part *part;
314
315         /* list the content for toplevel of this multipart */
316         list_content(ct, toplevel, realsize, verbose, debug);
317
318         /* now list for all the subparts */
319         for (part = m->mp_parts; part; part = part->mp_next) {
320                 CT p = part->mp_part;
321
322                 if (part_ok(p, 1) && type_ok(p, 1))
323                         list_switch(p, 0, realsize, verbose, debug);
324         }
325
326         return OK;
327 }
328
329
330 /*
331 ** list content information for type "message/partial"
332 */
333
334 static int
335 list_partial(CT ct, int toplevel, int realsize, int verbose, int debug)
336 {
337         struct partial *p = (struct partial *) ct->c_ctparams;
338
339         list_content(ct, toplevel, realsize, verbose, debug);
340         if (verbose) {
341                 printf("\t     [message %s, part %d",
342                                 p->pm_partid, p->pm_partno);
343                 if (p->pm_maxno)
344                         printf(" of %d", p->pm_maxno);
345                 printf("]\n");
346         }
347
348         return OK;
349 }
350
351
352 /*
353 ** list content information for type "message/external"
354 */
355
356 static int
357 list_external(CT ct, int toplevel, int realsize, int verbose, int debug)
358 {
359         struct exbody *e = (struct exbody *) ct->c_ctparams;
360
361         /*
362          * First list the information for the
363          * message/external content itself.
364          */
365         list_content(ct, toplevel, realsize, verbose, debug);
366
367         if (verbose) {
368                 if (e->eb_name)
369                         printf("\t     name=\"%s\"\n", e->eb_name);
370                 if (e->eb_dir)
371                         printf("\t     directory=\"%s\"\n", e->eb_dir);
372                 if (e->eb_site)
373                         printf("\t     site=\"%s\"\n", e->eb_site);
374                 if (e->eb_server)
375                         printf("\t     server=\"%s\"\n", e->eb_server);
376                 if (e->eb_subject)
377                         printf("\t     subject=\"%s\"\n", e->eb_subject);
378
379                 /* This must be defined */
380                 printf("\t     access-type=\"%s\"\n", e->eb_access);
381
382                 if (e->eb_mode)
383                         printf("\t     mode=\"%s\"\n", e->eb_mode);
384                 if (e->eb_permission)
385                         printf("\t     permission=\"%s\"\n", e->eb_permission);
386
387                 if (e->eb_flags == NOTOK)
388                         printf("\t     [service unavailable]\n");
389         }
390
391         /*
392         ** Now list the information for the external content
393         ** to which this content points.
394         */
395         list_content(e->eb_content, 0, realsize, verbose, debug);
396
397         return OK;
398 }
399
400
401 /*
402 ** list content information for type "application"
403 */
404
405 static int
406 list_application(CT ct, int toplevel, int realsize, int verbose, int debug)
407 {
408         list_content(ct, toplevel, realsize, verbose, debug);
409         if (verbose) {
410                 char **ap, **ep;
411                 CI ci = &ct->c_ctinfo;
412
413                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
414                         printf("\t     %s=\"%s\"\n", *ap, *ep);
415         }
416
417         return OK;
418 }
419
420
421 /*
422 ** list information about the Content-Transfer-Encoding
423 ** used by a content.
424 */
425
426 static int
427 list_encoding(CT ct)
428 {
429         CE ce;
430
431         if ((ce = ct->c_cefile))
432                 fprintf(stderr, "    decoded fp 0x%x file \"%s\"\n",
433                         (unsigned int)(unsigned long) ce->ce_fp,
434                         ce->ce_file ? ce->ce_file : "");
435
436         return OK;
437 }