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