2 * mhparse.c -- misc routines to process MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
12 #include <h/mhparse.h>
18 * limit actions to specified parts or content types
22 char *parts[NPARTS + 1];
23 char *types[NTYPES + 1];
25 int endian = 0; /* little or big endian */
28 static char *errs = NULL;
34 int part_ok (CT, int);
35 int type_ok (CT, int);
36 void set_endian (void);
37 int make_intermediates (char *);
38 void content_error (char *, CT, char *, ...);
39 void flush_errors (void);
43 part_ok (CT ct, int sP)
48 if (npart == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
51 for (ap = parts; *ap; ap++) {
53 if (!strncmp (*ap, ct->c_partno, len) &&
54 (!ct->c_partno[len] || ct->c_partno[len] == '.' ))
63 type_ok (CT ct, int sP)
67 CI ci = &ct->c_ctinfo;
69 if (ntype == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
72 snprintf (buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
73 for (ap = types; *ap; ap++)
74 if (!mh_strcasecmp (*ap, ci->ci_type) || !mh_strcasecmp (*ap, buffer))
90 endian = un.c[0] ? -1 : 1;
92 fprintf (stderr, "%s endian architecture\n",
93 endian > 0 ? "big" : "little");
98 make_intermediates (char *file)
102 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
106 if (stat (file, &st) == NOTOK) {
109 if (errno != ENOENT) {
110 advise (file, "error on directory");
116 ep = concat ("Create directory \"", file, "\"? ", NULL);
117 answer = getanswer (ep);
121 goto losing_directory;
122 if (!makedir (file)) {
123 advise (NULL, "unable to create directory %s", file);
124 goto losing_directory;
136 * Construct error message for content
140 content_error (char *what, CT ct, char *fmt, ...)
144 char *bp, buffer[BUFSIZ];
148 buflen = sizeof(buffer);
150 if (userrs && invo_name && *invo_name) {
151 snprintf (bp, buflen, "%s: ", invo_name);
157 va_start (arglist, fmt);
159 vsnprintf (bp, buflen, fmt, arglist);
170 snprintf (bp, buflen, " %s: ", what);
176 if ((s = strerror (errno)))
177 snprintf (bp, buflen, "%s", s);
179 snprintf (bp, buflen, "Error %d", errno);
186 i = strlen (invo_name) + 2;
188 /* Now add content type and subtype */
189 snprintf (bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
190 ci->ci_type, ci->ci_subtype);
195 /* Now add the message/part number */
197 snprintf (bp, buflen, " in message %s", ct->c_file);
203 snprintf (bp, buflen, ", part %s", ct->c_partno);
210 snprintf (bp, buflen, ")");
220 errs = add (buffer, errs);
222 advise (NULL, "%s", buffer);
232 fprintf (stderr, "%s", errs);