1N/A/****************************************************************************
1N/A * Copyright (c) 1998,2000,2002 Free Software Foundation, Inc. *
1N/A * *
1N/A * Permission is hereby granted, free of charge, to any person obtaining a *
1N/A * copy of this software and associated documentation files (the *
1N/A * "Software"), to deal in the Software without restriction, including *
1N/A * without limitation the rights to use, copy, modify, merge, publish, *
1N/A * distribute, distribute with modifications, sublicense, and/or sell *
1N/A * copies of the Software, and to permit persons to whom the Software is *
1N/A * furnished to do so, subject to the following conditions: *
1N/A * *
1N/A * The above copyright notice and this permission notice shall be included *
1N/A * in all copies or substantial portions of the Software. *
1N/A * *
1N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
1N/A * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
1N/A * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
1N/A * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
1N/A * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
1N/A * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
1N/A * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
1N/A * *
1N/A * Except as contained in this notice, the name(s) of the above copyright *
1N/A * holders shall not be used in advertising or otherwise to promote the *
1N/A * sale, use or other dealings in this Software without prior written *
1N/A * authorization. *
1N/A ****************************************************************************/
1N/A
1N/A/**********************************************************************
1N/A * This code is a modification of lib_tparm.c found in ncurses-5.2. The
1N/A * modification are for use in grub by replacing all libc function through
1N/A * special grub functions. This also meant to delete all dynamic memory
1N/A * allocation and replace it by a number of fixed buffers.
1N/A *
1N/A * Modifications by Tilmann Bubeck <t.bubeck@reinform.de> 2002
1N/A **********************************************************************/
1N/A
1N/A/****************************************************************************
1N/A * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
1N/A * and: Eric S. Raymond <esr@snark.thyrsus.com> *
1N/A ****************************************************************************/
1N/A
1N/A/*
1N/A * tparm.c
1N/A *
1N/A */
1N/A
1N/A#include "shared.h"
1N/A
1N/A#include "tparm.h"
1N/A
1N/A/*
1N/A * Common/troublesome character definitions
1N/A */
1N/Atypedef char grub_bool;
1N/A#undef isdigit
1N/A#define isdigit(c) ((c) >= '0' && (c) <= '9')
1N/A#ifndef FALSE
1N/A# define FALSE (0)
1N/A#endif
1N/A#ifndef TRUE
1N/A# define TRUE (!FALSE)
1N/A#endif
1N/A#define MAX_FORMAT_LEN 256
1N/A#define max(a,b) ((a) > (b) ? (a) : (b))
1N/A
1N/A//MODULE_ID("$Id: tparm.c,v 1.1.1.1 2003/11/20 02:04:59 fengshuo Exp $")
1N/A
1N/A/*
1N/A * char *
1N/A * tparm(string, ...)
1N/A *
1N/A * Substitute the given parameters into the given string by the following
1N/A * rules (taken from terminfo(5)):
1N/A *
1N/A * Cursor addressing and other strings requiring parame-
1N/A * ters in the terminal are described by a parameterized string
1N/A * capability, with like escapes %x in it. For example, to
1N/A * address the cursor, the cup capability is given, using two
1N/A * parameters: the row and column to address to. (Rows and
1N/A * columns are numbered from zero and refer to the physical
1N/A * screen visible to the user, not to any unseen memory.) If
1N/A * the terminal has memory relative cursor addressing, that can
1N/A * be indicated by
1N/A *
1N/A * The parameter mechanism uses a stack and special %
1N/A * codes to manipulate it. Typically a sequence will push one
1N/A * of the parameters onto the stack and then print it in some
1N/A * format. Often more complex operations are necessary.
1N/A *
1N/A * The % encodings have the following meanings:
1N/A *
1N/A * %% outputs `%'
1N/A * %c print pop() like %c in printf()
1N/A * %s print pop() like %s in printf()
1N/A * %[[:]flags][width[.precision]][doxXs]
1N/A * as in printf, flags are [-+#] and space
1N/A * The ':' is used to avoid making %+ or %-
1N/A * patterns (see below).
1N/A *
1N/A * %p[1-9] push ith parm
1N/A * %P[a-z] set dynamic variable [a-z] to pop()
1N/A * %g[a-z] get dynamic variable [a-z] and push it
1N/A * %P[A-Z] set static variable [A-Z] to pop()
1N/A * %g[A-Z] get static variable [A-Z] and push it
1N/A * %l push strlen(pop)
1N/A * %'c' push char constant c
1N/A * %{nn} push integer constant nn
1N/A *
1N/A * %+ %- %* %/ %m
1N/A * arithmetic (%m is mod): push(pop() op pop())
1N/A * %& %| %^ bit operations: push(pop() op pop())
1N/A * %= %> %< logical operations: push(pop() op pop())
1N/A * %A %O logical and & or operations for conditionals
1N/A * %! %~ unary operations push(op pop())
1N/A * %i add 1 to first two parms (for ANSI terminals)
1N/A *
1N/A * %? expr %t thenpart %e elsepart %;
1N/A * if-then-else, %e elsepart is optional.
1N/A * else-if's are possible ala Algol 68:
1N/A * %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
1N/A *
1N/A * For those of the above operators which are binary and not commutative,
1N/A * the stack works in the usual way, with
1N/A * %gx %gy %m
1N/A * resulting in x mod y, not the reverse.
1N/A */
1N/A
1N/A#define STACKSIZE 20
1N/A
1N/Atypedef struct {
1N/A union {
1N/A unsigned int num;
1N/A char *str;
1N/A } data;
1N/A grub_bool num_type;
1N/A} stack_frame;
1N/A
1N/Astatic stack_frame stack[STACKSIZE];
1N/Astatic int stack_ptr;
1N/A
1N/Astatic char out_buff[256];
1N/Astatic int out_size = 256;
1N/Astatic int out_used;
1N/A
1N/Astatic inline void
1N/Aget_space(int need)
1N/A{
1N/A need += out_used;
1N/A if (need > out_size) {
1N/A // FIX ME! buffer full, what now?
1N/A ;
1N/A }
1N/A}
1N/A
1N/Astatic inline void
1N/Asave_text(const char *fmt, const char *s, int len)
1N/A{
1N/A int s_len = grub_strlen(s);
1N/A if (len > (int) s_len)
1N/A s_len = len;
1N/A
1N/A get_space(s_len + 1);
1N/A
1N/A (void) grub_sprintf(out_buff + out_used, fmt, s);
1N/A out_used += grub_strlen(out_buff + out_used);
1N/A}
1N/A
1N/Astatic inline void
1N/Asave_number(const char *fmt, int number, int len)
1N/A{
1N/A if (len < 30)
1N/A len = 30; /* actually log10(MAX_INT)+1 */
1N/A
1N/A get_space(len + 1);
1N/A
1N/A (void) grub_sprintf(out_buff + out_used, fmt, number);
1N/A out_used += grub_strlen(out_buff + out_used);
1N/A}
1N/A
1N/Astatic inline void
1N/Asave_char(int c)
1N/A{
1N/A if (c == 0)
1N/A c = 0200;
1N/A get_space(1);
1N/A out_buff[out_used++] = c;
1N/A}
1N/A
1N/Astatic inline void
1N/Anpush(int x)
1N/A{
1N/A if (stack_ptr < STACKSIZE) {
1N/A stack[stack_ptr].num_type = TRUE;
1N/A stack[stack_ptr].data.num = x;
1N/A stack_ptr++;
1N/A }
1N/A}
1N/A
1N/Astatic inline int
1N/Anpop(void)
1N/A{
1N/A int result = 0;
1N/A if (stack_ptr > 0) {
1N/A stack_ptr--;
1N/A if (stack[stack_ptr].num_type)
1N/A result = stack[stack_ptr].data.num;
1N/A }
1N/A return result;
1N/A}
1N/A
1N/Astatic inline void
1N/Aspush(char *x)
1N/A{
1N/A if (stack_ptr < STACKSIZE) {
1N/A stack[stack_ptr].num_type = FALSE;
1N/A stack[stack_ptr].data.str = x;
1N/A stack_ptr++;
1N/A }
1N/A}
1N/A
1N/Astatic inline char *
1N/Aspop(void)
1N/A{
1N/A static char dummy[] = ""; /* avoid const-cast */
1N/A char *result = dummy;
1N/A if (stack_ptr > 0) {
1N/A stack_ptr--;
1N/A if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
1N/A result = stack[stack_ptr].data.str;
1N/A }
1N/A return result;
1N/A}
1N/A
1N/Astatic inline const char *
1N/Aparse_format(const char *s, char *format, int *len)
1N/A{
1N/A grub_bool done = FALSE;
1N/A grub_bool allowminus = FALSE;
1N/A grub_bool dot = FALSE;
1N/A grub_bool err = FALSE;
1N/A char *fmt = format;
1N/A int prec = 0;
1N/A int width = 0;
1N/A int value = 0;
1N/A
1N/A *len = 0;
1N/A *format++ = '%';
1N/A while (*s != '\0' && !done) {
1N/A switch (*s) {
1N/A case 'c': /* FALLTHRU */
1N/A case 'd': /* FALLTHRU */
1N/A case 'o': /* FALLTHRU */
1N/A case 'x': /* FALLTHRU */
1N/A case 'X': /* FALLTHRU */
1N/A case 's':
1N/A *format++ = *s;
1N/A done = TRUE;
1N/A break;
1N/A case '.':
1N/A *format++ = *s++;
1N/A if (dot) {
1N/A err = TRUE;
1N/A } else {
1N/A dot = TRUE;
1N/A prec = value;
1N/A }
1N/A value = 0;
1N/A break;
1N/A case '#':
1N/A *format++ = *s++;
1N/A break;
1N/A case ' ':
1N/A *format++ = *s++;
1N/A break;
1N/A case ':':
1N/A s++;
1N/A allowminus = TRUE;
1N/A break;
1N/A case '-':
1N/A if (allowminus) {
1N/A *format++ = *s++;
1N/A } else {
1N/A done = TRUE;
1N/A }
1N/A break;
1N/A default:
1N/A if (isdigit(*s)) {
1N/A value = (value * 10) + (*s - '0');
1N/A if (value > 10000)
1N/A err = TRUE;
1N/A *format++ = *s++;
1N/A } else {
1N/A done = TRUE;
1N/A }
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * If we found an error, ignore (and remove) the flags.
1N/A */
1N/A if (err) {
1N/A prec = width = value = 0;
1N/A format = fmt;
1N/A *format++ = '%';
1N/A *format++ = *s;
1N/A }
1N/A
1N/A if (dot)
1N/A width = value;
1N/A else
1N/A prec = value;
1N/A
1N/A *format = '\0';
1N/A /* return maximum string length in print */
1N/A *len = (prec > width) ? prec : width;
1N/A return s;
1N/A}
1N/A
1N/A#define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
1N/A#define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
1N/A
1N/Astatic inline char *
1N/Atparam_internal(const char *string, int *dataptr)
1N/A{
1N/A#define NUM_VARS 26
1N/A char *p_is_s[9];
1N/A int param[9];
1N/A int lastpop;
1N/A int popcount;
1N/A int number;
1N/A int len;
1N/A int level;
1N/A int x, y;
1N/A int i;
1N/A int len2;
1N/A register const char *cp;
1N/A static int len_fmt = MAX_FORMAT_LEN;
1N/A static char dummy[] = "";
1N/A static char format[MAX_FORMAT_LEN];
1N/A static int dynamic_var[NUM_VARS];
1N/A static int static_vars[NUM_VARS];
1N/A
1N/A out_used = 0;
1N/A if (string == NULL)
1N/A return NULL;
1N/A
1N/A if ((len2 = grub_strlen(string)) > len_fmt) {
1N/A return NULL;
1N/A }
1N/A
1N/A /*
1N/A * Find the highest parameter-number referred to in the format string.
1N/A * Use this value to limit the number of arguments copied from the
1N/A * variable-length argument list.
1N/A */
1N/A
1N/A number = 0;
1N/A lastpop = -1;
1N/A popcount = 0;
1N/A grub_memset(p_is_s, 0, sizeof(p_is_s));
1N/A
1N/A /*
1N/A * Analyze the string to see how many parameters we need from the varargs
1N/A * list, and what their types are. We will only accept string parameters
1N/A * if they appear as a %l or %s format following an explicit parameter
1N/A * reference (e.g., %p2%s). All other parameters are numbers.
1N/A *
1N/A * 'number' counts coarsely the number of pop's we see in the string, and
1N/A * 'popcount' shows the highest parameter number in the string. We would
1N/A * like to simply use the latter count, but if we are reading termcap
1N/A * strings, there may be cases that we cannot see the explicit parameter
1N/A * numbers.
1N/A */
1N/A for (cp = string; (cp - string) < (int) len2;) {
1N/A if (*cp == '%') {
1N/A cp++;
1N/A cp = parse_format(cp, format, &len);
1N/A switch (*cp) {
1N/A default:
1N/A break;
1N/A
1N/A case 'd': /* FALLTHRU */
1N/A case 'o': /* FALLTHRU */
1N/A case 'x': /* FALLTHRU */
1N/A case 'X': /* FALLTHRU */
1N/A case 'c': /* FALLTHRU */
1N/A number++;
1N/A lastpop = -1;
1N/A break;
1N/A
1N/A case 'l':
1N/A case 's':
1N/A if (lastpop > 0)
1N/A p_is_s[lastpop - 1] = dummy;
1N/A ++number;
1N/A break;
1N/A
1N/A case 'p':
1N/A cp++;
1N/A i = (*cp - '0');
1N/A if (i >= 0 && i <= 9) {
1N/A lastpop = i;
1N/A if (lastpop > popcount)
1N/A popcount = lastpop;
1N/A }
1N/A break;
1N/A
1N/A case 'P':
1N/A case 'g':
1N/A cp++;
1N/A break;
1N/A
1N/A case '\'':
1N/A cp += 2;
1N/A lastpop = -1;
1N/A break;
1N/A
1N/A case '{':
1N/A cp++;
1N/A while (*cp >= '0' && *cp <= '9') {
1N/A cp++;
1N/A }
1N/A break;
1N/A
1N/A case '+':
1N/A case '-':
1N/A case '*':
1N/A case '/':
1N/A case 'm':
1N/A case 'A':
1N/A case 'O':
1N/A case '&':
1N/A case '|':
1N/A case '^':
1N/A case '=':
1N/A case '<':
1N/A case '>':
1N/A case '!':
1N/A case '~':
1N/A lastpop = -1;
1N/A number += 2;
1N/A break;
1N/A
1N/A case 'i':
1N/A lastpop = -1;
1N/A if (popcount < 2)
1N/A popcount = 2;
1N/A break;
1N/A }
1N/A }
1N/A if (*cp != '\0')
1N/A cp++;
1N/A }
1N/A
1N/A if (number > 9)
1N/A number = 9;
1N/A for (i = 0; i < max(popcount, number); i++) {
1N/A /*
1N/A * A few caps (such as plab_norm) have string-valued parms.
1N/A * We'll have to assume that the caller knows the difference, since
1N/A * a char* and an int may not be the same size on the stack.
1N/A */
1N/A if (p_is_s[i] != 0) {
1N/A p_is_s[i] = (char *)(*(dataptr++));
1N/A } else {
1N/A param[i] = (int)(*(dataptr++));
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * This is a termcap compatibility hack. If there are no explicit pop
1N/A * operations in the string, load the stack in such a way that
1N/A * successive pops will grab successive parameters. That will make
1N/A * the expansion of (for example) \E[%d;%dH work correctly in termcap
1N/A * style, which means tparam() will expand termcap strings OK.
1N/A */
1N/A stack_ptr = 0;
1N/A if (popcount == 0) {
1N/A popcount = number;
1N/A for (i = number - 1; i >= 0; i--)
1N/A npush(param[i]);
1N/A }
1N/A
1N/A while (*string) {
1N/A /* skip delay timings */
1N/A if (*string == '$' && *(string + 1) == '<') {
1N/A while( *string && *string != '>')
1N/A string++;
1N/A if ( *string == '>' ) string++;
1N/A } else if ( *string == '%') {
1N/A string++;
1N/A string = parse_format(string, format, &len);
1N/A switch (*string) {
1N/A default:
1N/A break;
1N/A case '%':
1N/A save_char('%');
1N/A break;
1N/A
1N/A case 'd': /* FALLTHRU */
1N/A case 'o': /* FALLTHRU */
1N/A case 'x': /* FALLTHRU */
1N/A case 'X': /* FALLTHRU */
1N/A case 'c': /* FALLTHRU */
1N/A save_number(format, npop(), len);
1N/A break;
1N/A
1N/A case 'l':
1N/A save_number("%d", strlen(spop()), 0);
1N/A break;
1N/A
1N/A case 's':
1N/A save_text(format, spop(), len);
1N/A break;
1N/A
1N/A case 'p':
1N/A string++;
1N/A i = (*string - '1');
1N/A if (i >= 0 && i < 9) {
1N/A if (p_is_s[i])
1N/A spush(p_is_s[i]);
1N/A else
1N/A npush(param[i]);
1N/A }
1N/A break;
1N/A
1N/A case 'P':
1N/A string++;
1N/A if (isUPPER(*string)) {
1N/A i = (*string - 'A');
1N/A static_vars[i] = npop();
1N/A } else if (isLOWER(*string)) {
1N/A i = (*string - 'a');
1N/A dynamic_var[i] = npop();
1N/A }
1N/A break;
1N/A
1N/A case 'g':
1N/A string++;
1N/A if (isUPPER(*string)) {
1N/A i = (*string - 'A');
1N/A npush(static_vars[i]);
1N/A } else if (isLOWER(*string)) {
1N/A i = (*string - 'a');
1N/A npush(dynamic_var[i]);
1N/A }
1N/A break;
1N/A
1N/A case '\'':
1N/A string++;
1N/A npush(*string);
1N/A string++;
1N/A break;
1N/A
1N/A case '{':
1N/A number = 0;
1N/A string++;
1N/A while (*string >= '0' && *string <= '9') {
1N/A number = number * 10 + *string - '0';
1N/A string++;
1N/A }
1N/A npush(number);
1N/A break;
1N/A
1N/A case '+':
1N/A npush(npop() + npop());
1N/A break;
1N/A
1N/A case '-':
1N/A y = npop();
1N/A x = npop();
1N/A npush(x - y);
1N/A break;
1N/A
1N/A case '*':
1N/A npush(npop() * npop());
1N/A break;
1N/A
1N/A case '/':
1N/A y = npop();
1N/A x = npop();
1N/A npush(y ? (x / y) : 0);
1N/A break;
1N/A
1N/A case 'm':
1N/A y = npop();
1N/A x = npop();
1N/A npush(y ? (x % y) : 0);
1N/A break;
1N/A
1N/A case 'A':
1N/A npush(npop() && npop());
1N/A break;
1N/A
1N/A case 'O':
1N/A npush(npop() || npop());
1N/A break;
1N/A
1N/A case '&':
1N/A npush(npop() & npop());
1N/A break;
1N/A
1N/A case '|':
1N/A npush(npop() | npop());
1N/A break;
1N/A
1N/A case '^':
1N/A npush(npop() ^ npop());
1N/A break;
1N/A
1N/A case '=':
1N/A y = npop();
1N/A x = npop();
1N/A npush(x == y);
1N/A break;
1N/A
1N/A case '<':
1N/A y = npop();
1N/A x = npop();
1N/A npush(x < y);
1N/A break;
1N/A
1N/A case '>':
1N/A y = npop();
1N/A x = npop();
1N/A npush(x > y);
1N/A break;
1N/A
1N/A case '!':
1N/A npush(!npop());
1N/A break;
1N/A
1N/A case '~':
1N/A npush(~npop());
1N/A break;
1N/A
1N/A case 'i':
1N/A if (p_is_s[0] == 0)
1N/A param[0]++;
1N/A if (p_is_s[1] == 0)
1N/A param[1]++;
1N/A break;
1N/A
1N/A case '?':
1N/A break;
1N/A
1N/A case 't':
1N/A x = npop();
1N/A if (!x) {
1N/A /* scan forward for %e or %; at level zero */
1N/A string++;
1N/A level = 0;
1N/A while (*string) {
1N/A if (*string == '%') {
1N/A string++;
1N/A if (*string == '?')
1N/A level++;
1N/A else if (*string == ';') {
1N/A if (level > 0)
1N/A level--;
1N/A else
1N/A break;
1N/A } else if (*string == 'e' && level == 0)
1N/A break;
1N/A }
1N/A
1N/A if (*string)
1N/A string++;
1N/A }
1N/A }
1N/A break;
1N/A
1N/A case 'e':
1N/A /* scan forward for a %; at level zero */
1N/A string++;
1N/A level = 0;
1N/A while (*string) {
1N/A if (*string == '%') {
1N/A string++;
1N/A if (*string == '?')
1N/A level++;
1N/A else if (*string == ';') {
1N/A if (level > 0)
1N/A level--;
1N/A else
1N/A break;
1N/A }
1N/A }
1N/A
1N/A if (*string)
1N/A string++;
1N/A }
1N/A break;
1N/A
1N/A case ';':
1N/A break;
1N/A
1N/A } /* endswitch (*string) */
1N/A } else { /* endelse (*string == '%') */
1N/A save_char(*string);
1N/A }
1N/A
1N/A if (*string == '\0')
1N/A break;
1N/A
1N/A string++;
1N/A } /* endwhile (*string) */
1N/A
1N/A get_space(1);
1N/A out_buff[out_used] = '\0';
1N/A
1N/A return (out_buff);
1N/A}
1N/A
1N/Achar *
1N/Agrub_tparm(const char *string,...)
1N/A{
1N/A char *result;
1N/A int *dataptr = (int *) &string;
1N/A
1N/A dataptr++;
1N/A
1N/A result = tparam_internal(string, dataptr);
1N/A
1N/A return result;
1N/A}