17c478bdstevel@tonic-gate/****************************************************************************
27c478bdstevel@tonic-gate * Copyright (c) 1998,2000,2002 Free Software Foundation, Inc.              *
37c478bdstevel@tonic-gate *                                                                          *
47c478bdstevel@tonic-gate * Permission is hereby granted, free of charge, to any person obtaining a  *
57c478bdstevel@tonic-gate * copy of this software and associated documentation files (the            *
67c478bdstevel@tonic-gate * "Software"), to deal in the Software without restriction, including      *
77c478bdstevel@tonic-gate * without limitation the rights to use, copy, modify, merge, publish,      *
87c478bdstevel@tonic-gate * distribute, distribute with modifications, sublicense, and/or sell       *
97c478bdstevel@tonic-gate * copies of the Software, and to permit persons to whom the Software is    *
107c478bdstevel@tonic-gate * furnished to do so, subject to the following conditions:                 *
117c478bdstevel@tonic-gate *                                                                          *
127c478bdstevel@tonic-gate * The above copyright notice and this permission notice shall be included  *
137c478bdstevel@tonic-gate * in all copies or substantial portions of the Software.                   *
147c478bdstevel@tonic-gate *                                                                          *
157c478bdstevel@tonic-gate * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
167c478bdstevel@tonic-gate * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
177c478bdstevel@tonic-gate * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
187c478bdstevel@tonic-gate * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
197c478bdstevel@tonic-gate * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
207c478bdstevel@tonic-gate * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
217c478bdstevel@tonic-gate * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
227c478bdstevel@tonic-gate *                                                                          *
237c478bdstevel@tonic-gate * Except as contained in this notice, the name(s) of the above copyright   *
247c478bdstevel@tonic-gate * holders shall not be used in advertising or otherwise to promote the     *
257c478bdstevel@tonic-gate * sale, use or other dealings in this Software without prior written       *
267c478bdstevel@tonic-gate * authorization.                                                           *
277c478bdstevel@tonic-gate ****************************************************************************/
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/**********************************************************************
307c478bdstevel@tonic-gate * This code is a modification of lib_tparm.c found in ncurses-5.2. The
317c478bdstevel@tonic-gate * modification are for use in grub by replacing all libc function through
327c478bdstevel@tonic-gate * special grub functions. This also meant to delete all dynamic memory
337c478bdstevel@tonic-gate * allocation and replace it by a number of fixed buffers.
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate * Modifications by Tilmann Bubeck <t.bubeck@reinform.de> 2002
367c478bdstevel@tonic-gate **********************************************************************/
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate/****************************************************************************
397c478bdstevel@tonic-gate *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
407c478bdstevel@tonic-gate *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
417c478bdstevel@tonic-gate ****************************************************************************/
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/*
447c478bdstevel@tonic-gate *	tparm.c
457c478bdstevel@tonic-gate *
467c478bdstevel@tonic-gate */
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate#include "shared.h"
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate#include "tparm.h"
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate/*
537c478bdstevel@tonic-gate * Common/troublesome character definitions
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gatetypedef char grub_bool;
567c478bdstevel@tonic-gate#undef isdigit
577c478bdstevel@tonic-gate#define isdigit(c) ((c) >= '0' && (c) <= '9')
587c478bdstevel@tonic-gate#ifndef FALSE
597c478bdstevel@tonic-gate# define FALSE (0)
607c478bdstevel@tonic-gate#endif
617c478bdstevel@tonic-gate#ifndef TRUE
627c478bdstevel@tonic-gate# define TRUE (!FALSE)
637c478bdstevel@tonic-gate#endif
647c478bdstevel@tonic-gate#define MAX_FORMAT_LEN 256
657c478bdstevel@tonic-gate#define max(a,b) ((a) > (b) ? (a) : (b))
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate//MODULE_ID("$Id: tparm.c,v 1.1.1.1 2003/11/20 02:04:59 fengshuo Exp $")
687c478bdstevel@tonic-gate
697c478bdstevel@tonic-gate/*
707c478bdstevel@tonic-gate *	char *
717c478bdstevel@tonic-gate *	tparm(string, ...)
727c478bdstevel@tonic-gate *
737c478bdstevel@tonic-gate *	Substitute the given parameters into the given string by the following
747c478bdstevel@tonic-gate *	rules (taken from terminfo(5)):
757c478bdstevel@tonic-gate *
767c478bdstevel@tonic-gate *	     Cursor addressing and other strings  requiring  parame-
777c478bdstevel@tonic-gate *	ters in the terminal are described by a parameterized string
787c478bdstevel@tonic-gate *	capability, with like escapes %x in  it.   For  example,  to
797c478bdstevel@tonic-gate *	address  the  cursor, the cup capability is given, using two
807c478bdstevel@tonic-gate *	parameters: the row and column to  address  to.   (Rows  and
817c478bdstevel@tonic-gate *	columns  are  numbered  from  zero and refer to the physical
827c478bdstevel@tonic-gate *	screen visible to the user, not to any  unseen  memory.)  If
837c478bdstevel@tonic-gate *	the terminal has memory relative cursor addressing, that can
847c478bdstevel@tonic-gate *	be indicated by
857c478bdstevel@tonic-gate *
867c478bdstevel@tonic-gate *	     The parameter mechanism uses  a  stack  and  special  %
877c478bdstevel@tonic-gate *	codes  to manipulate it.  Typically a sequence will push one
887c478bdstevel@tonic-gate *	of the parameters onto the stack and then print it  in  some
897c478bdstevel@tonic-gate *	format.  Often more complex operations are necessary.
907c478bdstevel@tonic-gate *
917c478bdstevel@tonic-gate *	     The % encodings have the following meanings:
927c478bdstevel@tonic-gate *
937c478bdstevel@tonic-gate *	     %%        outputs `%'
947c478bdstevel@tonic-gate *	     %c        print pop() like %c in printf()
957c478bdstevel@tonic-gate *	     %s        print pop() like %s in printf()
967c478bdstevel@tonic-gate *           %[[:]flags][width[.precision]][doxXs]
977c478bdstevel@tonic-gate *                     as in printf, flags are [-+#] and space
987c478bdstevel@tonic-gate *                     The ':' is used to avoid making %+ or %-
997c478bdstevel@tonic-gate *                     patterns (see below).
1007c478bdstevel@tonic-gate *
1017c478bdstevel@tonic-gate *	     %p[1-9]   push ith parm
1027c478bdstevel@tonic-gate *	     %P[a-z]   set dynamic variable [a-z] to pop()
1037c478bdstevel@tonic-gate *	     %g[a-z]   get dynamic variable [a-z] and push it
1047c478bdstevel@tonic-gate *	     %P[A-Z]   set static variable [A-Z] to pop()
1057c478bdstevel@tonic-gate *	     %g[A-Z]   get static variable [A-Z] and push it
1067c478bdstevel@tonic-gate *	     %l        push strlen(pop)
1077c478bdstevel@tonic-gate *	     %'c'      push char constant c
1087c478bdstevel@tonic-gate *	     %{nn}     push integer constant nn
1097c478bdstevel@tonic-gate *
1107c478bdstevel@tonic-gate *	     %+ %- %* %/ %m
1117c478bdstevel@tonic-gate *	               arithmetic (%m is mod): push(pop() op pop())
1127c478bdstevel@tonic-gate *	     %& %| %^  bit operations: push(pop() op pop())
1137c478bdstevel@tonic-gate *	     %= %> %<  logical operations: push(pop() op pop())
1147c478bdstevel@tonic-gate *	     %A %O     logical and & or operations for conditionals
1157c478bdstevel@tonic-gate *	     %! %~     unary operations push(op pop())
1167c478bdstevel@tonic-gate *	     %i        add 1 to first two parms (for ANSI terminals)
1177c478bdstevel@tonic-gate *
1187c478bdstevel@tonic-gate *	     %? expr %t thenpart %e elsepart %;
1197c478bdstevel@tonic-gate *	               if-then-else, %e elsepart is optional.
1207c478bdstevel@tonic-gate *	               else-if's are possible ala Algol 68:
1217c478bdstevel@tonic-gate *	               %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
1227c478bdstevel@tonic-gate *
1237c478bdstevel@tonic-gate *	For those of the above operators which are binary and not commutative,
1247c478bdstevel@tonic-gate *	the stack works in the usual way, with
1257c478bdstevel@tonic-gate *			%gx %gy %m
1267c478bdstevel@tonic-gate *	resulting in x mod y, not the reverse.
1277c478bdstevel@tonic-gate */
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate#define STACKSIZE	20
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gatetypedef struct {
1327c478bdstevel@tonic-gate    union {
1337c478bdstevel@tonic-gate	unsigned int num;
1347c478bdstevel@tonic-gate	char *str;
1357c478bdstevel@tonic-gate    } data;
1367c478bdstevel@tonic-gate    grub_bool num_type;
1377c478bdstevel@tonic-gate} stack_frame;
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gatestatic stack_frame stack[STACKSIZE];
1407c478bdstevel@tonic-gatestatic int stack_ptr;
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gatestatic char out_buff[256];
1437c478bdstevel@tonic-gatestatic int out_size = 256;
1447c478bdstevel@tonic-gatestatic int out_used;
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gatestatic inline void
1477c478bdstevel@tonic-gateget_space(int need)
1487c478bdstevel@tonic-gate{
1497c478bdstevel@tonic-gate    need += out_used;
1507c478bdstevel@tonic-gate    if (need > out_size) {
1517c478bdstevel@tonic-gate	// FIX ME! buffer full, what now?
1527c478bdstevel@tonic-gate	;
1537c478bdstevel@tonic-gate    }
1547c478bdstevel@tonic-gate}
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gatestatic inline void
1577c478bdstevel@tonic-gatesave_text(const char *fmt, const char *s, int len)
1587c478bdstevel@tonic-gate{
1597c478bdstevel@tonic-gate    int s_len = grub_strlen(s);
1607c478bdstevel@tonic-gate    if (len > (int) s_len)
1617c478bdstevel@tonic-gate	s_len = len;
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate    get_space(s_len + 1);
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate    (void) grub_sprintf(out_buff + out_used, fmt, s);
1667c478bdstevel@tonic-gate    out_used += grub_strlen(out_buff + out_used);
1677c478bdstevel@tonic-gate}
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gatestatic inline void
1707c478bdstevel@tonic-gatesave_number(const char *fmt, int number, int len)
1717c478bdstevel@tonic-gate{
1727c478bdstevel@tonic-gate    if (len < 30)
1737c478bdstevel@tonic-gate	len = 30;		/* actually log10(MAX_INT)+1 */
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate    get_space(len + 1);
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate    (void) grub_sprintf(out_buff + out_used, fmt, number);
1787c478bdstevel@tonic-gate    out_used += grub_strlen(out_buff + out_used);
1797c478bdstevel@tonic-gate}
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gatestatic inline void
1827c478bdstevel@tonic-gatesave_char(int c)
1837c478bdstevel@tonic-gate{
1847c478bdstevel@tonic-gate    if (c == 0)
1857c478bdstevel@tonic-gate	c = 0200;
1867c478bdstevel@tonic-gate    get_space(1);
1877c478bdstevel@tonic-gate    out_buff[out_used++] = c;
1887c478bdstevel@tonic-gate}
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gatestatic inline void
1917c478bdstevel@tonic-gatenpush(int x)
1927c478bdstevel@tonic-gate{
1937c478bdstevel@tonic-gate    if (stack_ptr < STACKSIZE) {
1947c478bdstevel@tonic-gate	stack[stack_ptr].num_type = TRUE;
1957c478bdstevel@tonic-gate	stack[stack_ptr].data.num = x;
1967c478bdstevel@tonic-gate	stack_ptr++;
1977c478bdstevel@tonic-gate    }
1987c478bdstevel@tonic-gate}
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gatestatic inline int
2017c478bdstevel@tonic-gatenpop(void)
2027c478bdstevel@tonic-gate{
2037c478bdstevel@tonic-gate    int result = 0;
2047c478bdstevel@tonic-gate    if (stack_ptr > 0) {
2057c478bdstevel@tonic-gate	stack_ptr--;
2067c478bdstevel@tonic-gate	if (stack[stack_ptr].num_type)
2077c478bdstevel@tonic-gate	    result = stack[stack_ptr].data.num;
2087c478bdstevel@tonic-gate    }
2097c478bdstevel@tonic-gate    return result;
2107c478bdstevel@tonic-gate}
2117c478bdstevel@tonic-gate
2127c478bdstevel@tonic-gatestatic inline void
2137c478bdstevel@tonic-gatespush(char *x)
2147c478bdstevel@tonic-gate{
2157c478bdstevel@tonic-gate    if (stack_ptr < STACKSIZE) {
2167c478bdstevel@tonic-gate	stack[stack_ptr].num_type = FALSE;
2177c478bdstevel@tonic-gate	stack[stack_ptr].data.str = x;
2187c478bdstevel@tonic-gate	stack_ptr++;
2197c478bdstevel@tonic-gate    }
2207c478bdstevel@tonic-gate}
2217c478bdstevel@tonic-gate
2227c478bdstevel@tonic-gatestatic inline char *
2237c478bdstevel@tonic-gatespop(void)
2247c478bdstevel@tonic-gate{
2257c478bdstevel@tonic-gate    static char dummy[] = "";	/* avoid const-cast */
2267c478bdstevel@tonic-gate    char *result = dummy;
2277c478bdstevel@tonic-gate    if (stack_ptr > 0) {
2287c478bdstevel@tonic-gate	stack_ptr--;
2297c478bdstevel@tonic-gate	if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
2307c478bdstevel@tonic-gate	    result = stack[stack_ptr].data.str;
2317c478bdstevel@tonic-gate    }
2327c478bdstevel@tonic-gate    return result;
2337c478bdstevel@tonic-gate}
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gatestatic inline const char *
2367c478bdstevel@tonic-gateparse_format(const char *s, char *format, int *len)
2377c478bdstevel@tonic-gate{
2387c478bdstevel@tonic-gate    grub_bool done = FALSE;
2397c478bdstevel@tonic-gate    grub_bool allowminus = FALSE;
2407c478bdstevel@tonic-gate    grub_bool dot = FALSE;
2417c478bdstevel@tonic-gate    grub_bool err = FALSE;
2427c478bdstevel@tonic-gate    char *fmt = format;
2437c478bdstevel@tonic-gate    int prec = 0;
2447c478bdstevel@tonic-gate    int width = 0;
2457c478bdstevel@tonic-gate    int value = 0;
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate    *len = 0;
2487c478bdstevel@tonic-gate    *format++ = '%';
2497c478bdstevel@tonic-gate    while (*s != '\0' && !done) {
2507c478bdstevel@tonic-gate	switch (*s) {
2517c478bdstevel@tonic-gate	case 'c':		/* FALLTHRU */
2527c478bdstevel@tonic-gate	case 'd':		/* FALLTHRU */
2537c478bdstevel@tonic-gate	case 'o':		/* FALLTHRU */
2547c478bdstevel@tonic-gate	case 'x':		/* FALLTHRU */
2557c478bdstevel@tonic-gate	case 'X':		/* FALLTHRU */
2567c478bdstevel@tonic-gate	case 's':
2577c478bdstevel@tonic-gate	    *format++ = *s;
2587c478bdstevel@tonic-gate	    done = TRUE;
2597c478bdstevel@tonic-gate	    break;
2607c478bdstevel@tonic-gate	case '.':
2617c478bdstevel@tonic-gate	    *format++ = *s++;
2627c478bdstevel@tonic-gate	    if (dot) {
2637c478bdstevel@tonic-gate		err = TRUE;
2647c478bdstevel@tonic-gate	    } else {
2657c478bdstevel@tonic-gate		dot = TRUE;
2667c478bdstevel@tonic-gate		prec = value;
2677c478bdstevel@tonic-gate	    }
2687c478bdstevel@tonic-gate	    value = 0;
2697c478bdstevel@tonic-gate	    break;
2707c478bdstevel@tonic-gate	case '#':
2717c478bdstevel@tonic-gate	    *format++ = *s++;
2727c478bdstevel@tonic-gate	    break;
2737c478bdstevel@tonic-gate	case ' ':
2747c478bdstevel@tonic-gate	    *format++ = *s++;
2757c478bdstevel@tonic-gate	    break;
2767c478bdstevel@tonic-gate	case ':':
2777c478bdstevel@tonic-gate	    s++;
2787c478bdstevel@tonic-gate	    allowminus = TRUE;
2797c478bdstevel@tonic-gate	    break;
2807c478bdstevel@tonic-gate	case '-':
2817c478bdstevel@tonic-gate	    if (allowminus) {
2827c478bdstevel@tonic-gate		*format++ = *s++;
2837c478bdstevel@tonic-gate	    } else {
2847c478bdstevel@tonic-gate		done = TRUE;
2857c478bdstevel@tonic-gate	    }
2867c478bdstevel@tonic-gate	    break;
2877c478bdstevel@tonic-gate	default:
2887c478bdstevel@tonic-gate	    if (isdigit(*s)) {
2897c478bdstevel@tonic-gate		value = (value * 10) + (*s - '0');
2907c478bdstevel@tonic-gate		if (value > 10000)
2917c478bdstevel@tonic-gate		    err = TRUE;
2927c478bdstevel@tonic-gate		*format++ = *s++;
2937c478bdstevel@tonic-gate	    } else {
2947c478bdstevel@tonic-gate		done = TRUE;
2957c478bdstevel@tonic-gate	    }
2967c478bdstevel@tonic-gate	}
2977c478bdstevel@tonic-gate    }
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate    /*
3007c478bdstevel@tonic-gate     * If we found an error, ignore (and remove) the flags.
3017c478bdstevel@tonic-gate     */
3027c478bdstevel@tonic-gate    if (err) {
3037c478bdstevel@tonic-gate	prec = width = value = 0;
3047c478bdstevel@tonic-gate	format = fmt;
3057c478bdstevel@tonic-gate	*format++ = '%';
3067c478bdstevel@tonic-gate	*format++ = *s;
3077c478bdstevel@tonic-gate    }
3087c478bdstevel@tonic-gate
3097c478bdstevel@tonic-gate    if (dot)
3107c478bdstevel@tonic-gate	width = value;
3117c478bdstevel@tonic-gate    else
3127c478bdstevel@tonic-gate	prec = value;
3137c478bdstevel@tonic-gate
3147c478bdstevel@tonic-gate    *format = '\0';
3157c478bdstevel@tonic-gate    /* return maximum string length in print */
3167c478bdstevel@tonic-gate    *len = (prec > width) ? prec : width;
3177c478bdstevel@tonic-gate    return s;
3187c478bdstevel@tonic-gate}
3197c478bdstevel@tonic-gate
3207c478bdstevel@tonic-gate#define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
3217c478bdstevel@tonic-gate#define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
3227c478bdstevel@tonic-gate
3237c478bdstevel@tonic-gatestatic inline char *
3247c478bdstevel@tonic-gatetparam_internal(const char *string, int *dataptr)
3257c478bdstevel@tonic-gate{
3267c478bdstevel@tonic-gate#define NUM_VARS 26
3277c478bdstevel@tonic-gate    char *p_is_s[9];
3287c478bdstevel@tonic-gate    int param[9];
3297c478bdstevel@tonic-gate    int lastpop;
3307c478bdstevel@tonic-gate    int popcount;
3317c478bdstevel@tonic-gate    int number;
3327c478bdstevel@tonic-gate    int len;
3337c478bdstevel@tonic-gate    int level;
3347c478bdstevel@tonic-gate    int x, y;
3357c478bdstevel@tonic-gate    int i;
3367c478bdstevel@tonic-gate    int len2;
3377c478bdstevel@tonic-gate    register const char *cp;
3387c478bdstevel@tonic-gate    static int len_fmt = MAX_FORMAT_LEN;
3397c478bdstevel@tonic-gate    static char dummy[] = "";
3407c478bdstevel@tonic-gate    static char format[MAX_FORMAT_LEN];
3417c478bdstevel@tonic-gate    static int dynamic_var[NUM_VARS];
3427c478bdstevel@tonic-gate    static int static_vars[NUM_VARS];
3437c478bdstevel@tonic-gate
3447c478bdstevel@tonic-gate    out_used = 0;
3457c478bdstevel@tonic-gate    if (string == NULL)
3467c478bdstevel@tonic-gate	return NULL;
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate    if ((len2 = grub_strlen(string)) > len_fmt) {
3497c478bdstevel@tonic-gate	return NULL;
3507c478bdstevel@tonic-gate    }
3517c478bdstevel@tonic-gate
3527c478bdstevel@tonic-gate    /*
3537c478bdstevel@tonic-gate     * Find the highest parameter-number referred to in the format string.
3547c478bdstevel@tonic-gate     * Use this value to limit the number of arguments copied from the
3557c478bdstevel@tonic-gate     * variable-length argument list.
3567c478bdstevel@tonic-gate     */
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate    number = 0;
3597c478bdstevel@tonic-gate    lastpop = -1;
3607c478bdstevel@tonic-gate    popcount = 0;
3617c478bdstevel@tonic-gate    grub_memset(p_is_s, 0, sizeof(p_is_s));
3627c478bdstevel@tonic-gate
3637c478bdstevel@tonic-gate    /*
3647c478bdstevel@tonic-gate     * Analyze the string to see how many parameters we need from the varargs
3657c478bdstevel@tonic-gate     * list, and what their types are.  We will only accept string parameters
3667c478bdstevel@tonic-gate     * if they appear as a %l or %s format following an explicit parameter
3677c478bdstevel@tonic-gate     * reference (e.g., %p2%s).  All other parameters are numbers.
3687c478bdstevel@tonic-gate     *
3697c478bdstevel@tonic-gate     * 'number' counts coarsely the number of pop's we see in the string, and
3707c478bdstevel@tonic-gate     * 'popcount' shows the highest parameter number in the string.  We would
3717c478bdstevel@tonic-gate     * like to simply use the latter count, but if we are reading termcap
3727c478bdstevel@tonic-gate     * strings, there may be cases that we cannot see the explicit parameter
3737c478bdstevel@tonic-gate     * numbers.
3747c478bdstevel@tonic-gate     */
3757c478bdstevel@tonic-gate    for (cp = string; (cp - string) < (int) len2;) {
3767c478bdstevel@tonic-gate	if (*cp == '%') {
3777c478bdstevel@tonic-gate	    cp++;
3787c478bdstevel@tonic-gate	    cp = parse_format(cp, format, &len);
3797c478bdstevel@tonic-gate	    switch (*cp) {
3807c478bdstevel@tonic-gate	    default:
3817c478bdstevel@tonic-gate		break;
3827c478bdstevel@tonic-gate
3837c478bdstevel@tonic-gate	    case 'd':		/* FALLTHRU */
3847c478bdstevel@tonic-gate	    case 'o':		/* FALLTHRU */
3857c478bdstevel@tonic-gate	    case 'x':		/* FALLTHRU */
3867c478bdstevel@tonic-gate	    case 'X':		/* FALLTHRU */
3877c478bdstevel@tonic-gate	    case 'c':		/* FALLTHRU */
3887c478bdstevel@tonic-gate		number++;
3897c478bdstevel@tonic-gate		lastpop = -1;
3907c478bdstevel@tonic-gate		break;
3917c478bdstevel@tonic-gate
3927c478bdstevel@tonic-gate	    case 'l':
3937c478bdstevel@tonic-gate	    case 's':
3947c478bdstevel@tonic-gate		if (lastpop > 0)
3957c478bdstevel@tonic-gate		    p_is_s[lastpop - 1] = dummy;
3967c478bdstevel@tonic-gate		++number;
3977c478bdstevel@tonic-gate		break;
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gate	    case 'p':
4007c478bdstevel@tonic-gate		cp++;
4017c478bdstevel@tonic-gate		i = (*cp - '0');
4027c478bdstevel@tonic-gate		if (i >= 0 && i <= 9) {
4037c478bdstevel@tonic-gate		    lastpop = i;
4047c478bdstevel@tonic-gate		    if (lastpop > popcount)
4057c478bdstevel@tonic-gate			popcount = lastpop;
4067c478bdstevel@tonic-gate		}
4077c478bdstevel@tonic-gate		break;
4087c478bdstevel@tonic-gate
4097c478bdstevel@tonic-gate	    case 'P':
4107c478bdstevel@tonic-gate	    case 'g':
4117c478bdstevel@tonic-gate		cp++;
4127c478bdstevel@tonic-gate		break;
4137c478bdstevel@tonic-gate
4147c478bdstevel@tonic-gate	    case '\'':
4157c478bdstevel@tonic-gate		cp += 2;
4167c478bdstevel@tonic-gate		lastpop = -1;
4177c478bdstevel@tonic-gate		break;
4187c478bdstevel@tonic-gate
4197c478bdstevel@tonic-gate	    case '{':
4207c478bdstevel@tonic-gate		cp++;
4217c478bdstevel@tonic-gate		while (*cp >= '0' && *cp <= '9') {
4227c478bdstevel@tonic-gate		    cp++;
4237c478bdstevel@tonic-gate		}
4247c478bdstevel@tonic-gate		break;
4257c478bdstevel@tonic-gate
4267c478bdstevel@tonic-gate	    case '+':
4277c478bdstevel@tonic-gate	    case '-':
4287c478bdstevel@tonic-gate	    case '*':
4297c478bdstevel@tonic-gate	    case '/':
4307c478bdstevel@tonic-gate	    case 'm':
4317c478bdstevel@tonic-gate	    case 'A':
4327c478bdstevel@tonic-gate	    case 'O':
4337c478bdstevel@tonic-gate	    case '&':
4347c478bdstevel@tonic-gate	    case '|':
4357c478bdstevel@tonic-gate	    case '^':
4367c478bdstevel@tonic-gate	    case '=':
4377c478bdstevel@tonic-gate	    case '<':
4387c478bdstevel@tonic-gate	    case '>':
4397c478bdstevel@tonic-gate	    case '!':
4407c478bdstevel@tonic-gate	    case '~':
4417c478bdstevel@tonic-gate		lastpop = -1;
4427c478bdstevel@tonic-gate		number += 2;
4437c478bdstevel@tonic-gate		break;
4447c478bdstevel@tonic-gate
4457c478bdstevel@tonic-gate	    case 'i':
4467c478bdstevel@tonic-gate		lastpop = -1;
4477c478bdstevel@tonic-gate		if (popcount < 2)
4487c478bdstevel@tonic-gate		    popcount = 2;
4497c478bdstevel@tonic-gate		break;
4507c478bdstevel@tonic-gate	    }
4517c478bdstevel@tonic-gate	}
4527c478bdstevel@tonic-gate	if (*cp != '\0')
4537c478bdstevel@tonic-gate	    cp++;
4547c478bdstevel@tonic-gate    }
4557c478bdstevel@tonic-gate
4567c478bdstevel@tonic-gate    if (number > 9)
4577c478bdstevel@tonic-gate	number = 9;
4587c478bdstevel@tonic-gate    for (i = 0; i < max(popcount, number); i++) {
4597c478bdstevel@tonic-gate	/*
4607c478bdstevel@tonic-gate	 * A few caps (such as plab_norm) have string-valued parms.
4617c478bdstevel@tonic-gate	 * We'll have to assume that the caller knows the difference, since
4627c478bdstevel@tonic-gate	 * a char* and an int may not be the same size on the stack.
4637c478bdstevel@tonic-gate	 */
4647c478bdstevel@tonic-gate	if (p_is_s[i] != 0) {
4657c478bdstevel@tonic-gate	  p_is_s[i] = (char *)(*(dataptr++));
4667c478bdstevel@tonic-gate	} else {
4677c478bdstevel@tonic-gate	  param[i] = (int)(*(dataptr++));
4687c478bdstevel@tonic-gate	}
4697c478bdstevel@tonic-gate    }
4707c478bdstevel@tonic-gate
4717c478bdstevel@tonic-gate    /*
4727c478bdstevel@tonic-gate     * This is a termcap compatibility hack.  If there are no explicit pop
4737c478bdstevel@tonic-gate     * operations in the string, load the stack in such a way that
4747c478bdstevel@tonic-gate     * successive pops will grab successive parameters.  That will make
4757c478bdstevel@tonic-gate     * the expansion of (for example) \E[%d;%dH work correctly in termcap
4767c478bdstevel@tonic-gate     * style, which means tparam() will expand termcap strings OK.
4777c478bdstevel@tonic-gate     */
4787c478bdstevel@tonic-gate    stack_ptr = 0;
4797c478bdstevel@tonic-gate    if (popcount == 0) {
4807c478bdstevel@tonic-gate	popcount = number;
4817c478bdstevel@tonic-gate	for (i = number - 1; i >= 0; i--)
4827c478bdstevel@tonic-gate	    npush(param[i]);
4837c478bdstevel@tonic-gate    }
4847c478bdstevel@tonic-gate
4857c478bdstevel@tonic-gate    while (*string) {
4867c478bdstevel@tonic-gate        /* skip delay timings */
4877c478bdstevel@tonic-gate	if (*string == '$' && *(string + 1) == '<') {
4887c478bdstevel@tonic-gate	    while( *string && *string != '>')
4897c478bdstevel@tonic-gate	        string++;
4907c478bdstevel@tonic-gate	    if ( *string == '>' ) string++;
4917c478bdstevel@tonic-gate	} else if ( *string == '%') {
4927c478bdstevel@tonic-gate	    string++;
4937c478bdstevel@tonic-gate	    string = parse_format(string, format, &len);
4947c478bdstevel@tonic-gate	    switch (*string) {
4957c478bdstevel@tonic-gate	    default:
4967c478bdstevel@tonic-gate		break;
4977c478bdstevel@tonic-gate	    case '%':
4987c478bdstevel@tonic-gate		save_char('%');
4997c478bdstevel@tonic-gate		break;
5007c478bdstevel@tonic-gate
5017c478bdstevel@tonic-gate	    case 'd':		/* FALLTHRU */
5027c478bdstevel@tonic-gate	    case 'o':		/* FALLTHRU */
5037c478bdstevel@tonic-gate	    case 'x':		/* FALLTHRU */
5047c478bdstevel@tonic-gate	    case 'X':		/* FALLTHRU */
5057c478bdstevel@tonic-gate	    case 'c':		/* FALLTHRU */
5067c478bdstevel@tonic-gate		save_number(format, npop(), len);
5077c478bdstevel@tonic-gate		break;
5087c478bdstevel@tonic-gate
5097c478bdstevel@tonic-gate	    case 'l':
5107c478bdstevel@tonic-gate		save_number("%d", strlen(spop()), 0);
5117c478bdstevel@tonic-gate		break;
5127c478bdstevel@tonic-gate
5137c478bdstevel@tonic-gate	    case 's':
5147c478bdstevel@tonic-gate		save_text(format, spop(), len);
5157c478bdstevel@tonic-gate		break;
5167c478bdstevel@tonic-gate
5177c478bdstevel@tonic-gate	    case 'p':
5187c478bdstevel@tonic-gate		string++;
5197c478bdstevel@tonic-gate		i = (*string - '1');
5207c478bdstevel@tonic-gate		if (i >= 0 && i < 9) {
5217c478bdstevel@tonic-gate		    if (p_is_s[i])
5227c478bdstevel@tonic-gate			spush(p_is_s[i]);
5237c478bdstevel@tonic-gate		    else
5247c478bdstevel@tonic-gate			npush(param[i]);
5257c478bdstevel@tonic-gate		}
5267c478bdstevel@tonic-gate		break;
5277c478bdstevel@tonic-gate
5287c478bdstevel@tonic-gate	    case 'P':
5297c478bdstevel@tonic-gate		string++;
5307c478bdstevel@tonic-gate		if (isUPPER(*string)) {
5317c478bdstevel@tonic-gate		    i = (*string - 'A');
5327c478bdstevel@tonic-gate		    static_vars[i] = npop();
5337c478bdstevel@tonic-gate		} else if (isLOWER(*string)) {
5347c478bdstevel@tonic-gate		    i = (*string - 'a');
5357c478bdstevel@tonic-gate		    dynamic_var[i] = npop();
5367c478bdstevel@tonic-gate		}
5377c478bdstevel@tonic-gate		break;
5387c478bdstevel@tonic-gate
5397c478bdstevel@tonic-gate	    case 'g':
5407c478bdstevel@tonic-gate		string++;
5417c478bdstevel@tonic-gate		if (isUPPER(*string)) {
5427c478bdstevel@tonic-gate		    i = (*string - 'A');
5437c478bdstevel@tonic-gate		    npush(static_vars[i]);
5447c478bdstevel@tonic-gate		} else if (isLOWER(*string)) {
5457c478bdstevel@tonic-gate		    i = (*string - 'a');
5467c478bdstevel@tonic-gate		    npush(dynamic_var[i]);
5477c478bdstevel@tonic-gate		}
5487c478bdstevel@tonic-gate		break;
5497c478bdstevel@tonic-gate
5507c478bdstevel@tonic-gate	    case '\'':
5517c478bdstevel@tonic-gate		string++;
5527c478bdstevel@tonic-gate		npush(*string);
5537c478bdstevel@tonic-gate		string++;
5547c478bdstevel@tonic-gate		break;
5557c478bdstevel@tonic-gate
5567c478bdstevel@tonic-gate	    case '{':
5577c478bdstevel@tonic-gate		number = 0;
5587c478bdstevel@tonic-gate		string++;
5597c478bdstevel@tonic-gate		while (*string >= '0' && *string <= '9') {
5607c478bdstevel@tonic-gate		    number = number * 10 + *string - '0';
5617c478bdstevel@tonic-gate		    string++;
5627c478bdstevel@tonic-gate		}
5637c478bdstevel@tonic-gate		npush(number);
5647c478bdstevel@tonic-gate		break;
5657c478bdstevel@tonic-gate
5667c478bdstevel@tonic-gate	    case '+':
5677c478bdstevel@tonic-gate		npush(npop() + npop());
5687c478bdstevel@tonic-gate		break;
5697c478bdstevel@tonic-gate
5707c478bdstevel@tonic-gate	    case '-':
5717c478bdstevel@tonic-gate		y = npop();
5727c478bdstevel@tonic-gate		x = npop();
5737c478bdstevel@tonic-gate		npush(x - y);
5747c478bdstevel@tonic-gate		break;
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate	    case '*':
5777c478bdstevel@tonic-gate		npush(npop() * npop());
5787c478bdstevel@tonic-gate		break;
5797c478bdstevel@tonic-gate
5807c478bdstevel@tonic-gate	    case '/':
5817c478bdstevel@tonic-gate		y = npop();
5827c478bdstevel@tonic-gate		x = npop();
5837c478bdstevel@tonic-gate		npush(y ? (x / y) : 0);
5847c478bdstevel@tonic-gate		break;
5857c478bdstevel@tonic-gate
5867c478bdstevel@tonic-gate	    case 'm':
5877c478bdstevel@tonic-gate		y = npop();
5887c478bdstevel@tonic-gate		x = npop();
5897c478bdstevel@tonic-gate		npush(y ? (x % y) : 0);
5907c478bdstevel@tonic-gate		break;
5917c478bdstevel@tonic-gate
5927c478bdstevel@tonic-gate	    case 'A':
5937c478bdstevel@tonic-gate		npush(npop() && npop());
5947c478bdstevel@tonic-gate		break;
5957c478bdstevel@tonic-gate
5967c478bdstevel@tonic-gate	    case 'O':
5977c478bdstevel@tonic-gate		npush(npop() || npop());
5987c478bdstevel@tonic-gate		break;
5997c478bdstevel@tonic-gate
6007c478bdstevel@tonic-gate	    case '&':
6017c478bdstevel@tonic-gate		npush(npop() & npop());
6027c478bdstevel@tonic-gate		break;
6037c478bdstevel@tonic-gate
6047c478bdstevel@tonic-gate	    case '|':
6057c478bdstevel@tonic-gate		npush(npop() | npop());
6067c478bdstevel@tonic-gate		break;
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate	    case '^':
6097c478bdstevel@tonic-gate		npush(npop() ^ npop());
6107c478bdstevel@tonic-gate		break;
6117c478bdstevel@tonic-gate
6127c478bdstevel@tonic-gate	    case '=':
6137c478bdstevel@tonic-gate		y = npop();
6147c478bdstevel@tonic-gate		x = npop();
6157c478bdstevel@tonic-gate		npush(x == y);
6167c478bdstevel@tonic-gate		break;
6177c478bdstevel@tonic-gate
6187c478bdstevel@tonic-gate	    case '<':
6197c478bdstevel@tonic-gate		y = npop();
6207c478bdstevel@tonic-gate		x = npop();
6217c478bdstevel@tonic-gate		npush(x < y);
6227c478bdstevel@tonic-gate		break;
6237c478bdstevel@tonic-gate
6247c478bdstevel@tonic-gate	    case '>':
6257c478bdstevel@tonic-gate		y = npop();
6267c478bdstevel@tonic-gate		x = npop();
6277c478bdstevel@tonic-gate		npush(x > y);
6287c478bdstevel@tonic-gate		break;
6297c478bdstevel@tonic-gate
6307c478bdstevel@tonic-gate	    case '!':
6317c478bdstevel@tonic-gate		npush(!npop());
6327c478bdstevel@tonic-gate		break;
6337c478bdstevel@tonic-gate
6347c478bdstevel@tonic-gate	    case '~':
6357c478bdstevel@tonic-gate		npush(~npop());
6367c478bdstevel@tonic-gate		break;
6377c478bdstevel@tonic-gate
6387c478bdstevel@tonic-gate	    case 'i':
6397c478bdstevel@tonic-gate		if (p_is_s[0] == 0)
6407c478bdstevel@tonic-gate		    param[0]++;
6417c478bdstevel@tonic-gate		if (p_is_s[1] == 0)
6427c478bdstevel@tonic-gate		    param[1]++;
6437c478bdstevel@tonic-gate		break;
6447c478bdstevel@tonic-gate
6457c478bdstevel@tonic-gate	    case '?':
6467c478bdstevel@tonic-gate		break;
6477c478bdstevel@tonic-gate
6487c478bdstevel@tonic-gate	    case 't':
6497c478bdstevel@tonic-gate		x = npop();
6507c478bdstevel@tonic-gate		if (!x) {
6517c478bdstevel@tonic-gate		    /* scan forward for %e or %; at level zero */
6527c478bdstevel@tonic-gate		    string++;
6537c478bdstevel@tonic-gate		    level = 0;
6547c478bdstevel@tonic-gate		    while (*string) {
6557c478bdstevel@tonic-gate			if (*string == '%') {
6567c478bdstevel@tonic-gate			    string++;
6577c478bdstevel@tonic-gate			    if (*string == '?')
6587c478bdstevel@tonic-gate				level++;
6597c478bdstevel@tonic-gate			    else if (*string == ';') {
6607c478bdstevel@tonic-gate				if (level > 0)
6617c478bdstevel@tonic-gate				    level--;
6627c478bdstevel@tonic-gate				else
6637c478bdstevel@tonic-gate				    break;
6647c478bdstevel@tonic-gate			    } else if (*string == 'e' && level == 0)
6657c478bdstevel@tonic-gate				break;
6667c478bdstevel@tonic-gate			}
6677c478bdstevel@tonic-gate
6687c478bdstevel@tonic-gate			if (*string)
6697c478bdstevel@tonic-gate			    string++;
6707c478bdstevel@tonic-gate		    }
6717c478bdstevel@tonic-gate		}
6727c478bdstevel@tonic-gate		break;
6737c478bdstevel@tonic-gate
6747c478bdstevel@tonic-gate	    case 'e':
6757c478bdstevel@tonic-gate		/* scan forward for a %; at level zero */
6767c478bdstevel@tonic-gate		string++;
6777c478bdstevel@tonic-gate		level = 0;
6787c478bdstevel@tonic-gate		while (*string) {
6797c478bdstevel@tonic-gate		    if (*string == '%') {
6807c478bdstevel@tonic-gate			string++;
6817c478bdstevel@tonic-gate			if (*string == '?')
6827c478bdstevel@tonic-gate			    level++;
6837c478bdstevel@tonic-gate			else if (*string == ';') {
6847c478bdstevel@tonic-gate			    if (level > 0)
6857c478bdstevel@tonic-gate				level--;
6867c478bdstevel@tonic-gate			    else
6877c478bdstevel@tonic-gate				break;
6887c478bdstevel@tonic-gate			}
6897c478bdstevel@tonic-gate		    }
6907c478bdstevel@tonic-gate
6917c478bdstevel@tonic-gate		    if (*string)
6927c478bdstevel@tonic-gate			string++;
6937c478bdstevel@tonic-gate		}
6947c478bdstevel@tonic-gate		break;
6957c478bdstevel@tonic-gate
6967c478bdstevel@tonic-gate	    case ';':
6977c478bdstevel@tonic-gate		break;
6987c478bdstevel@tonic-gate
6997c478bdstevel@tonic-gate	    }			/* endswitch (*string) */
7007c478bdstevel@tonic-gate	} else {             	/* endelse (*string == '%') */
7017c478bdstevel@tonic-gate	    save_char(*string);
7027c478bdstevel@tonic-gate	}
7037c478bdstevel@tonic-gate
7047c478bdstevel@tonic-gate	if (*string == '\0')
7057c478bdstevel@tonic-gate	    break;
7067c478bdstevel@tonic-gate
7077c478bdstevel@tonic-gate	string++;
7087c478bdstevel@tonic-gate    }				/* endwhile (*string) */
7097c478bdstevel@tonic-gate
7107c478bdstevel@tonic-gate    get_space(1);
7117c478bdstevel@tonic-gate    out_buff[out_used] = '\0';
7127c478bdstevel@tonic-gate
7137c478bdstevel@tonic-gate    return (out_buff);
7147c478bdstevel@tonic-gate}
7157c478bdstevel@tonic-gate
7167c478bdstevel@tonic-gatechar *
7177c478bdstevel@tonic-gategrub_tparm(const char *string,...)
7187c478bdstevel@tonic-gate{
7197c478bdstevel@tonic-gate    char *result;
7207c478bdstevel@tonic-gate    int *dataptr = (int *) &string;
7217c478bdstevel@tonic-gate
7227c478bdstevel@tonic-gate    dataptr++;
7237c478bdstevel@tonic-gate
7247c478bdstevel@tonic-gate    result = tparam_internal(string, dataptr);
7257c478bdstevel@tonic-gate
7267c478bdstevel@tonic-gate    return result;
7277c478bdstevel@tonic-gate}
728