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>
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;
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 &&
51 (sP || ct->c_subtype)))
54 for (ap = parts; *ap; ap++) {
56 if (strncmp(*ap, ct->c_partno, len)==0 &&
57 (!ct->c_partno[len] ||
58 ct->c_partno[len] == '.' ))
67 type_ok(CT ct, int sP)
71 CI ci = &ct->c_ctinfo;
73 if (ntype == 0 || (ct->c_type == CT_MULTIPART &&
74 (sP || ct->c_subtype)))
77 snprintf(buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
78 for (ap = types; *ap; ap++)
79 if (!mh_strcasecmp(*ap, ci->ci_type) ||
80 !mh_strcasecmp(*ap, buffer))
96 endian = un.c[0] ? -1 : 1;
98 fprintf(stderr, "%s endian architecture\n",
99 endian > 0 ? "big" : "little");
104 make_intermediates(char *file)
108 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
112 if (stat(file, &st) == NOTOK) {
115 if (errno != ENOENT) {
116 advise(file, "error on directory");
122 ep = concat("Create directory \"", file, "\"? ", NULL);
123 answer = getanswer(ep);
127 goto losing_directory;
128 if (!makedir(file)) {
129 advise(NULL, "unable to create directory %s",
131 goto losing_directory;
143 ** Construct error message for content
147 content_error(char *what, CT ct, char *fmt, ...)
151 char *bp, buffer[BUFSIZ];
155 buflen = sizeof(buffer);
157 if (userrs && invo_name && *invo_name) {
158 snprintf(bp, buflen, "%s: ", invo_name);
164 va_start(arglist, fmt);
166 vsnprintf(bp, buflen, fmt, arglist);
177 snprintf(bp, buflen, " %s: ", what);
183 if ((s = strerror(errno)))
184 snprintf(bp, buflen, "%s", s);
186 snprintf(bp, buflen, "Error %d", errno);
193 i = strlen(invo_name) + 2;
195 /* Now add content type and subtype */
196 snprintf(bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
197 ci->ci_type, ci->ci_subtype);
202 /* Now add the message/part number */
204 snprintf(bp, buflen, " in message %s", ct->c_file);
210 snprintf(bp, buflen, ", part %s", ct->c_partno);
217 snprintf(bp, buflen, ")");
227 errs = add(buffer, errs);
229 advise(NULL, "%s", buffer);
239 fprintf(stderr, "%s", errs);