+
+int
+parse_header_attrs (const char *filename, int len, char **header_attrp, CI ci,
+ int *status) {
+ char **attr = ci->ci_attrs;
+ char *cp = *header_attrp;
+
+ while (*cp == ';') {
+ char *dp, *vp, *up, c;
+
+ /* Relies on knowledge of this declaration:
+ * char *ci_attrs[NPARMS + 2];
+ */
+ if (attr >= ci->ci_attrs + sizeof ci->ci_attrs/sizeof (char *) - 2) {
+ advise (NULL,
+ "too many parameters in message %s's %s: field (%d max)",
+ filename, TYPE_FIELD, NPARMS);
+ *status = NOTOK;
+ return NOTOK;
+ }
+
+ cp++;
+ while (isspace ((unsigned char) *cp))
+ cp++;
+
+ if (*cp == '(' &&
+ get_comment (filename, ci, &cp, 1) == NOTOK) {
+ *status = NOTOK;
+ return NOTOK;
+ }
+
+ if (*cp == 0) {
+ advise (NULL,
+ "extraneous trailing ';' in message %s's %s: "
+ "parameter list",
+ filename, TYPE_FIELD);
+ *status = OK;
+ return NOTOK;
+ }
+
+ /* down case the attribute name */
+ for (dp = cp; istoken ((unsigned char) *dp); dp++)
+ if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
+ *dp = tolower ((unsigned char) *dp);
+
+ for (up = dp; isspace ((unsigned char) *dp);)
+ dp++;
+ if (dp == cp || *dp != '=') {
+ advise (NULL,
+ "invalid parameter in message %s's %s: "
+ "field\n%*.*sparameter %s (error detected at offset %d)",
+ filename, TYPE_FIELD, len, len, "", cp, dp - cp);
+ *status = NOTOK;
+ return NOTOK;
+ }
+
+ vp = (*attr = add (cp, NULL)) + (up - cp);
+ *vp = '\0';
+ for (dp++; isspace ((unsigned char) *dp);)
+ dp++;
+
+ /* Now store the attribute value. */
+ ci->ci_values[attr - ci->ci_attrs] = vp = *attr + (dp - cp);
+
+ if (*dp == '"') {
+ for (cp = ++dp, dp = vp;;) {
+ switch (c = *cp++) {
+ case '\0':
+bad_quote:
+ advise (NULL,
+ "invalid quoted-string in message %s's %s: "
+ "field\n%*.*s(parameter %s)",
+ filename, TYPE_FIELD, len, len, "", *attr);
+ *status = NOTOK;
+ return NOTOK;
+
+ case '\\':
+ *dp++ = c;
+ if ((c = *cp++) == '\0')
+ goto bad_quote;
+ /* else fall... */
+
+ default:
+ *dp++ = c;
+ continue;
+
+ case '"':
+ *dp = '\0';
+ break;
+ }
+ break;
+ }
+ } else {
+ for (cp = dp, dp = vp; istoken (*cp); cp++, dp++)
+ continue;
+ *dp = '\0';
+ }
+ if (!*vp) {
+ advise (NULL,
+ "invalid parameter in message %s's %s: "
+ "field\n%*.*s(parameter %s)",
+ filename, TYPE_FIELD, len, len, "", *attr);
+ *status = NOTOK;
+ return NOTOK;
+ }
+
+ while (isspace ((unsigned char) *cp))
+ cp++;
+
+ if (*cp == '(' &&
+ get_comment (filename, ci, &cp, 1) == NOTOK) {
+ *status = NOTOK;
+ return NOTOK;
+ }
+
+ ++attr;
+ }
+
+ *header_attrp = cp;
+ return OK;
+}
+
+
+char *
+content_charset (CT ct) {
+ const char *const charset = "charset";
+ char *default_charset = NULL;
+ CI ctinfo = &ct->c_ctinfo;
+ char **ap, **vp;
+ char **src_charset = NULL;
+
+ for (ap = ctinfo->ci_attrs, vp = ctinfo->ci_values; *ap; ++ap, ++vp) {
+ if (! strcasecmp (*ap, charset)) {
+ src_charset = vp;
+ break;
+ }
+ }
+
+ /* RFC 2045, Sec. 5.2: default to us-ascii. */
+ if (src_charset == NULL) src_charset = &default_charset;
+ if (*src_charset == NULL) *src_charset = "US-ASCII";
+
+ return *src_charset;
+}
+
+
+/* Change the value of a name=value pair in a header field body.
+ If the name isn't there, append them. In any case, a new
+ string will be allocated and must be free'd by the caller.
+ Trims any trailing newlines. */
+char *
+update_attr (char *body, const char *name, const char *value) {
+ char *bp = nmh_strcasestr (body, name);
+ char *new_body;
+
+ if (bp) {
+ char *other_attrs = strchr (bp, ';');
+
+ *(bp + strlen (name)) = '\0';
+ new_body = concat (body, "\"", value, "\"", NULL);
+
+ if (other_attrs) {
+ char *cp;
+
+ /* Trim any trailing newlines. */
+ for (cp = &other_attrs[strlen (other_attrs) - 1];
+ cp > other_attrs && *cp == '\n';
+ *cp-- = '\0') continue;
+ new_body = add (other_attrs, new_body);
+ }
+ } else {
+ char *cp;
+
+ /* Append name/value pair, after first removing a final newline
+ and (extraneous) semicolon. */
+ if (*(cp = &body[strlen (body) - 1]) == '\n') *cp = '\0';
+ if (*(cp = &body[strlen (body) - 1]) == ';') *cp = '\0';
+ new_body = concat (body, "; ", name, "\"", value, "\"", NULL);
+ }
+
+ return new_body;
+}