3 * mhparse.c -- misc routines to process MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
15 #include <h/mhparse.h>
20 * limit actions to specified parts or content types
24 char *parts[NPARTS + 1];
25 char *types[NTYPES + 1];
27 int endian = 0; /* little or big endian */
30 static char *errs = NULL;
36 int part_ok (CT, int);
37 int type_ok (CT, int);
38 void set_endian (void);
39 int make_intermediates (char *);
40 void content_error (char *, CT, char *, ...);
41 void flush_errors (void);
45 part_ok (CT ct, int sP)
50 if (npart == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
53 for (ap = parts; *ap; ap++) {
55 if (!strncmp (*ap, ct->c_partno, len) &&
56 (!ct->c_partno[len] || ct->c_partno[len] == '.' ))
65 type_ok (CT ct, int sP)
69 CI ci = &ct->c_ctinfo;
71 if (ntype == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
74 snprintf (buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
75 for (ap = types; *ap; ap++)
76 if (!strcasecmp (*ap, ci->ci_type) || !strcasecmp (*ap, buffer))
92 endian = un.c[0] ? -1 : 1;
94 fprintf (stderr, "%s endian architecture\n",
95 endian > 0 ? "big" : "little");
100 make_intermediates (char *file)
104 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
108 if (stat (file, &st) == NOTOK) {
111 if (errno != ENOENT) {
112 advise (file, "error on directory");
118 ep = concat ("Create directory \"", file, "\"? ", NULL);
119 answer = getanswer (ep);
123 goto losing_directory;
124 if (!makedir (file)) {
125 advise (NULL, "unable to create directory %s", file);
126 goto losing_directory;
138 * Construct error message for content
142 content_error (char *what, CT ct, char *fmt, ...)
146 char *bp, buffer[BUFSIZ];
150 buflen = sizeof(buffer);
152 if (userrs && invo_name && *invo_name) {
153 snprintf (bp, buflen, "%s: ", invo_name);
159 va_start (arglist, fmt);
161 vsnprintf (bp, buflen, fmt, arglist);
172 snprintf (bp, buflen, " %s: ", what);
178 if ((s = strerror (errno)))
179 snprintf (bp, buflen, "%s", s);
181 snprintf (bp, buflen, "Error %d", errno);
188 i = strlen (invo_name) + 2;
190 /* Now add content type and subtype */
191 snprintf (bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
192 ci->ci_type, ci->ci_subtype);
197 /* Now add the message/part number */
199 snprintf (bp, buflen, " in message %s", ct->c_file);
205 snprintf (bp, buflen, ", part %s", ct->c_partno);
212 snprintf (bp, buflen, ")");
222 errs = add (buffer, errs);
224 advise (NULL, "%s", buffer);
234 fprintf (stderr, "%s", errs);