+
+/*
+ * Create a string based on a list of output parameters. Assume that this
+ * parameter string will be appended to an existing header, so start out
+ * with the separator (;). Perform RFC 2231 encoding when necessary.
+ */
+
+char *
+output_params(size_t initialwidth, PM params)
+{
+ char *paramout = NULL;
+ char line[CPERLIN + 1], *q;
+ int curlen, index, eightbit;
+ size_t valoff;
+
+ while (params != NULL) {
+ index = 0;
+ valoff = 0;
+ q = line;
+
+ curlen = param_len(params, index, valoff, &eightbit);
+
+ /*
+ * If this won't fit on the line, start a new one. Save room in
+ * case we need a semicolon on the end
+ */
+
+ if (initialwidth + curlen > CPERLIN - 1) {
+ paramout = add(";\n\t", paramout);
+ initialwidth = 8;
+ }
+
+ /*
+ * Loop until we get a parameter that fits within a line.
+ */
+
+ while (initialwidth + curlen > CPERLIN - 1) {
+ int curvallen = strlen(params->pm_value + valoff) -
+ (initialwidth + curlen - (CPERLIN - 1));
+
+ /*
+ * curvallen holds how many characters we take from this
+ * current value. Make sure it's at least 1.
+ */
+
+ if (curvallen < 1)
+ curvallen = 1;
+ }
+ params = params->pm_next;
+ }
+
+ return paramout;
+}
+
+/*
+ * Calculate the size of a parameter. Include any necessary encoding.
+ * Start the length computation from where "offset" is marked.
+ */
+
+static size_t
+param_len(PM pm, int index, size_t valueoff, int *eightbit)
+{
+ char *start = pm->pm_value + valueoff, *p;
+ size_t len = 0;
+
+ *eightbit = 0;
+
+ /*
+ * Add up the length. First, start with the parameter name, and include
+ * the equal sign.
+ */
+
+ len += strlen(pm->pm_name) + 1;
+
+ /*
+ * Scan the parameter value. If we find an 8-bit character, then
+ * we need to compute the locale name for the length.
+ */
+
+ for (p = start; *p != '\0'; p++) {
+ if (! isascii((unsigned char) *p)) {
+ *eightbit = 1;
+ break;
+ }
+ }
+
+ /*
+ * If we've got 8-bit character, put the locale on the front (if we're
+ * doing part 0. Also compute the length of the string based on the
+ * encoding we need to do.
+ */
+
+ if (*eightbit) {
+ len++; /* For the encoding we need to do */
+ if (index == 0) {
+ len += strlen(write_charset_8bit()) + 2; /* Plus extra '' */
+ } else {
+ /*
+ * We know we definitely need to include an index.
+ * This will get the length wrong if we have more than 99
+ * sections. I can live with that.
+ */
+ len += 2; /* *<N> */
+ if (index > 9)
+ len++;
+ }
+ for (p = start; *p != '\0'; p++) {
+ if (isparamencode(*p))
+ len += 3;
+ else
+ len++;
+ }
+ } else {
+ /*
+ * Much simpler! Don't forget opening and closing quotes.
+ */
+ len += strlen(start) + 2;
+ }
+
+ return len;
+}