-int param[9];
-int popcount;
-int number;
-int len;
-int level;
-int x, y;
-int i;
-register const char *cp;
-static size_t len_fmt;
-static char *format;
-static int dynamic_var[NUM_VARS];
-static int static_vars[NUM_VARS];
-
- out_used = 0;
- if (string == NULL)
- return NULL;
-
- /*
- * Find the highest parameter-number referred to in the format string.
- * Use this value to limit the number of arguments copied from the
- * variable-length argument list.
- */
- for (cp = string, popcount = number = 0; *cp != '\0'; cp++) {
- if (cp[0] == '%' && cp[1] != '\0') {
- switch (cp[1]) {
- case '%':
- cp++;
- break;
- case 'i':
- if (popcount < 2)
- popcount = 2;
- break;
- case 'p':
- cp++;
- if (cp[1] >= '1' && cp[1] <= '9') {
- int c = cp[1] - '0';
- if (c > popcount)
- popcount = c;
- }
- break;
- case '0': case '1': case '2': case '3': case '4':
- case '5': case '6': case '7': case '8': case '9':
- case 'd': case 'c': case 's':
- ++number;
- break;
- }
+ char *p_is_s[9];
+ long param[9];
+ int lastpop;
+ int popcount;
+ int number;
+ int len;
+ int level;
+ int x, y;
+ int i;
+ size_t len2;
+ register const char *cp;
+ static size_t len_fmt;
+ static char dummy[] = "";
+ static char *format;
+ static int dynamic_var[NUM_VARS];
+ static int static_vars[NUM_VARS];
+
+ out_used = 0;
+ if (string == NULL)
+ return NULL;
+
+ if ((len2 = strlen(string)) > len_fmt) {
+ len_fmt = len2 + len_fmt + 2;
+ if ((format = typeRealloc(char, len_fmt, format)) == 0)
+ return 0;
+ }
+
+ /*
+ * Find the highest parameter-number referred to in the format string.
+ * Use this value to limit the number of arguments copied from the
+ * variable-length argument list.
+ */
+
+ number = 0;
+ lastpop = -1;
+ popcount = 0;
+ memset(p_is_s, 0, sizeof(p_is_s));
+
+ /*
+ * Analyze the string to see how many parameters we need from the varargs
+ * list, and what their types are. We will only accept string parameters
+ * if they appear as a %l or %s format following an explicit parameter
+ * reference (e.g., %p2%s). All other parameters are numbers.
+ *
+ * 'number' counts coarsely the number of pop's we see in the string, and
+ * 'popcount' shows the highest parameter number in the string. We would
+ * like to simply use the latter count, but if we are reading termcap
+ * strings, there may be cases that we cannot see the explicit parameter
+ * numbers.
+ */
+ for (cp = string; (cp - string) < (int) len2;) {
+ if (*cp == '%') {
+ cp++;
+ cp = parse_format(cp, format, &len);
+ switch (*cp) {
+ default:
+ break;
+
+ case 'd': /* FALLTHRU */
+ case 'o': /* FALLTHRU */
+ case 'x': /* FALLTHRU */
+ case 'X': /* FALLTHRU */
+ case 'c': /* FALLTHRU */
+ number++;
+ lastpop = -1;
+ break;
+
+ case 'l':
+ case 's':
+ if (lastpop > 0)
+ p_is_s[lastpop - 1] = dummy;
+ ++number;
+ break;
+
+ case 'p':
+ cp++;
+ i = (*cp - '0');
+ if (i >= 0 && i <= 9) {
+ lastpop = i;
+ if (lastpop > popcount)
+ popcount = lastpop;