2017-05-04 12:42:23 +03:00
|
|
|
/* $NetBSD: tparm.c,v 1.17 2017/05/04 09:42:23 roy Exp $ */
|
2010-02-03 18:16:32 +03:00
|
|
|
|
|
|
|
/*
|
2013-01-24 14:41:28 +04:00
|
|
|
* Copyright (c) 2009, 2011, 2013 The NetBSD Foundation, Inc.
|
2010-02-03 18:16:32 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Roy Marples.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2017-05-04 12:42:23 +03:00
|
|
|
__RCSID("$NetBSD: tparm.c,v 1.17 2017/05/04 09:42:23 roy Exp $");
|
2012-06-02 23:10:33 +04:00
|
|
|
#include <sys/param.h>
|
2010-02-03 18:16:32 +03:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <term_private.h>
|
|
|
|
#include <term.h>
|
|
|
|
|
2013-01-24 14:28:28 +04:00
|
|
|
#define LONG_STR_MAX ((CHAR_BIT * sizeof(long)) / 3)
|
2013-01-24 14:41:28 +04:00
|
|
|
#define BUFINC 128 /* Size to increament the terminal buffer by */
|
2013-01-24 14:28:28 +04:00
|
|
|
|
2013-06-07 17:16:18 +04:00
|
|
|
#define VA_LONG_LONG 1
|
2013-01-25 21:28:50 +04:00
|
|
|
#define VA_CHAR_INT 2
|
2013-06-07 17:16:18 +04:00
|
|
|
//#define VA_CHAR_LONG 3 /* No need for this yet */
|
2013-01-25 21:28:50 +04:00
|
|
|
|
2010-02-03 18:16:32 +03:00
|
|
|
static TERMINAL *dumbterm; /* For non thread safe functions */
|
|
|
|
|
2010-09-22 10:10:51 +04:00
|
|
|
typedef struct {
|
2013-01-24 14:28:28 +04:00
|
|
|
long nums[20];
|
2010-02-03 18:16:32 +03:00
|
|
|
char *strings[20];
|
|
|
|
size_t offset;
|
|
|
|
} TPSTACK;
|
|
|
|
|
2010-09-22 10:10:51 +04:00
|
|
|
typedef struct {
|
2013-01-24 14:28:28 +04:00
|
|
|
long num;
|
2010-02-03 18:16:32 +03:00
|
|
|
char *string;
|
|
|
|
} TPVAR;
|
|
|
|
|
|
|
|
static int
|
2013-01-24 14:28:28 +04:00
|
|
|
push(long num, char *string, TPSTACK *stack)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
2012-11-30 14:14:18 +04:00
|
|
|
if (stack->offset >= sizeof(stack->nums)) {
|
2010-02-03 18:16:32 +03:00
|
|
|
errno = E2BIG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stack->nums[stack->offset] = num;
|
|
|
|
stack->strings[stack->offset] = string;
|
|
|
|
stack->offset++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-01-24 14:28:28 +04:00
|
|
|
pop(long *num, char **string, TPSTACK *stack)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
|
|
|
if (stack->offset == 0) {
|
2011-03-10 16:39:26 +03:00
|
|
|
if (num)
|
|
|
|
*num = 0;
|
|
|
|
if (string)
|
|
|
|
*string = NULL;
|
2010-02-03 18:16:32 +03:00
|
|
|
errno = E2BIG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
stack->offset--;
|
|
|
|
if (num)
|
|
|
|
*num = stack->nums[stack->offset];
|
|
|
|
if (string)
|
|
|
|
*string = stack->strings[stack->offset];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
checkbuf(TERMINAL *term, size_t len)
|
|
|
|
{
|
|
|
|
char *buf;
|
2013-06-07 17:16:18 +04:00
|
|
|
|
2010-02-03 18:16:32 +03:00
|
|
|
if (term->_bufpos + len >= term->_buflen) {
|
2013-01-24 14:28:28 +04:00
|
|
|
len = term->_buflen + MAX(len, BUFINC);
|
2010-02-03 18:16:32 +03:00
|
|
|
buf = realloc(term->_buf, len);
|
|
|
|
if (buf == NULL)
|
2012-06-02 23:10:33 +04:00
|
|
|
return NULL;
|
2010-02-03 18:16:32 +03:00
|
|
|
term->_buf = buf;
|
|
|
|
term->_buflen = len;
|
|
|
|
}
|
|
|
|
return term->_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
ochar(TERMINAL *term, int c)
|
|
|
|
{
|
|
|
|
if (c == 0)
|
|
|
|
c = 0200;
|
|
|
|
/* Check we have space and a terminator */
|
|
|
|
if (checkbuf(term, 2) == NULL)
|
|
|
|
return 0;
|
|
|
|
term->_buf[term->_bufpos++] = (char)c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-05-04 12:42:23 +03:00
|
|
|
onum(TERMINAL *term, const char *fmt, int num, size_t len)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
2017-05-04 12:42:23 +03:00
|
|
|
int l;
|
|
|
|
size_t r;
|
2010-02-03 18:16:32 +03:00
|
|
|
|
2013-01-24 14:28:28 +04:00
|
|
|
if (len < LONG_STR_MAX)
|
|
|
|
len = LONG_STR_MAX;
|
|
|
|
if (checkbuf(term, len + 2) == NULL)
|
2010-02-03 18:16:32 +03:00
|
|
|
return 0;
|
2015-11-25 21:46:59 +03:00
|
|
|
l = snprintf(term->_buf + term->_bufpos, len + 2, fmt, num);
|
2017-05-04 12:42:23 +03:00
|
|
|
if (l == -1)
|
|
|
|
return 0;
|
|
|
|
r = (size_t)l;
|
|
|
|
term->_bufpos += r;
|
|
|
|
return r;
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
|
2013-01-24 14:41:28 +04:00
|
|
|
/*
|
|
|
|
Make a pass through the string so we can work out
|
|
|
|
which parameters are ints and which are char *.
|
|
|
|
Basically we only use char * if %p[1-9] is followed by %l or %s.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_ti_parm_analyse(const char *str, int *piss, int piss_len)
|
|
|
|
{
|
|
|
|
int nparm, lpop;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
nparm = 0;
|
|
|
|
lpop = -1;
|
|
|
|
while ((c = *str++) != '\0') {
|
|
|
|
if (c != '%')
|
|
|
|
continue;
|
|
|
|
c = *str++;
|
|
|
|
switch (c) {
|
|
|
|
case 'l': /* FALLTHROUGH */
|
|
|
|
case 's':
|
|
|
|
if (lpop > 0) {
|
|
|
|
if (lpop <= piss_len)
|
|
|
|
piss[lpop - 1] = 1;
|
|
|
|
else if (piss)
|
|
|
|
errno = E2BIG;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
c = *str++;
|
|
|
|
if (c < '1' || c > '9') {
|
|
|
|
errno = EINVAL;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
lpop = c - '0';
|
|
|
|
if (lpop > nparm)
|
|
|
|
nparm = lpop;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lpop = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nparm;
|
|
|
|
}
|
|
|
|
|
2010-02-03 18:16:32 +03:00
|
|
|
static char *
|
2013-01-25 21:28:50 +04:00
|
|
|
_ti_tiparm(TERMINAL *term, const char *str, int va_type, va_list parms)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
|
|
|
char c, fmt[64], *fp, *ostr;
|
2013-01-24 14:28:28 +04:00
|
|
|
long val, val2;
|
|
|
|
long dnums[26]; /* dynamic variables a-z, not preserved */
|
2017-05-04 12:42:23 +03:00
|
|
|
size_t l, max, width, precision, olen;
|
2010-02-03 18:16:32 +03:00
|
|
|
TPSTACK stack;
|
2013-01-24 14:41:28 +04:00
|
|
|
TPVAR params[TPARM_MAX];
|
2017-05-04 12:42:23 +03:00
|
|
|
unsigned int done, dot, minus;
|
2013-01-24 14:41:28 +04:00
|
|
|
int piss[TPARM_MAX]; /* Parameter IS String - piss ;) */
|
2010-02-03 18:16:32 +03:00
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
If not passed a terminal, malloc a dummy one.
|
|
|
|
This means we can preserve buffers and variables per terminal and
|
|
|
|
still work with non thread safe functions (which sadly are still the
|
|
|
|
norm and standard).
|
|
|
|
*/
|
|
|
|
if (term == NULL) {
|
|
|
|
if (dumbterm == NULL) {
|
|
|
|
dumbterm = malloc(sizeof(*dumbterm));
|
|
|
|
if (dumbterm == NULL)
|
|
|
|
return NULL;
|
|
|
|
dumbterm->_buflen = 0;
|
|
|
|
}
|
|
|
|
term = dumbterm;
|
|
|
|
}
|
|
|
|
|
|
|
|
term->_bufpos = 0;
|
|
|
|
/* Ensure we have an initial buffer */
|
|
|
|
if (term->_buflen == 0) {
|
2013-01-24 14:28:28 +04:00
|
|
|
term->_buf = malloc(BUFINC);
|
2010-02-03 18:16:32 +03:00
|
|
|
if (term->_buf == NULL)
|
|
|
|
return NULL;
|
2013-01-24 14:28:28 +04:00
|
|
|
term->_buflen = BUFINC;
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&piss, 0, sizeof(piss));
|
2017-05-04 12:42:23 +03:00
|
|
|
max = (size_t)_ti_parm_analyse(str, piss, TPARM_MAX);
|
2010-02-03 18:16:32 +03:00
|
|
|
|
|
|
|
/* Put our parameters into variables */
|
|
|
|
memset(¶ms, 0, sizeof(params));
|
|
|
|
for (l = 0; l < max; l++) {
|
2013-01-25 16:30:05 +04:00
|
|
|
if (piss[l]) {
|
2013-01-25 21:28:50 +04:00
|
|
|
if (va_type == VA_LONG_LONG) {
|
2013-01-25 16:30:05 +04:00
|
|
|
/* This only works if char * fits into a long
|
|
|
|
* on this platform. */
|
|
|
|
if (sizeof(char *) <= sizeof(long)/*CONSTCOND*/)
|
|
|
|
params[l].string =
|
|
|
|
(char *)va_arg(parms, long);
|
|
|
|
else {
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
params[l].string = va_arg(parms, char *);
|
|
|
|
} else {
|
2013-01-25 21:28:50 +04:00
|
|
|
if (va_type == VA_CHAR_INT)
|
2013-01-25 16:30:05 +04:00
|
|
|
params[l].num = (long)va_arg(parms, int);
|
2013-01-25 21:28:50 +04:00
|
|
|
else
|
|
|
|
params[l].num = va_arg(parms, long);
|
2013-01-25 16:30:05 +04:00
|
|
|
}
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(&stack, 0, sizeof(stack));
|
|
|
|
while ((c = *str++) != '\0') {
|
|
|
|
if (c != '%' || (c = *str++) == '%') {
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
if (ochar(term, c) == 0)
|
|
|
|
return NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle formatting. */
|
|
|
|
fp = fmt;
|
|
|
|
*fp++ = '%';
|
|
|
|
done = dot = minus = width = precision = 0;
|
|
|
|
val = 0;
|
|
|
|
while (done == 0 && (size_t)(fp - fmt) < sizeof(fmt)) {
|
|
|
|
switch (c) {
|
|
|
|
case 'c': /* FALLTHROUGH */
|
2013-01-24 14:28:28 +04:00
|
|
|
case 's':
|
|
|
|
*fp++ = c;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2010-02-03 18:16:32 +03:00
|
|
|
case 'd': /* FALLTHROUGH */
|
|
|
|
case 'o': /* FALLTHROUGH */
|
|
|
|
case 'x': /* FALLTHROUGH */
|
|
|
|
case 'X': /* FALLTHROUGH */
|
2013-01-24 14:28:28 +04:00
|
|
|
*fp++ = 'l';
|
2010-02-03 18:16:32 +03:00
|
|
|
*fp++ = c;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case '#': /* FALLTHROUGH */
|
|
|
|
case ' ':
|
|
|
|
*fp++ = c;
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
*fp++ = c;
|
|
|
|
if (dot == 0) {
|
|
|
|
dot = 1;
|
2017-05-04 12:42:23 +03:00
|
|
|
width = (size_t)val;
|
2010-02-03 18:16:32 +03:00
|
|
|
} else
|
|
|
|
done = 2;
|
|
|
|
val = 0;
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
minus = 1;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (minus)
|
|
|
|
*fp++ = c;
|
|
|
|
else
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isdigit((unsigned char)c)) {
|
|
|
|
val = (val * 10) + (c - '0');
|
|
|
|
if (val > 10000)
|
|
|
|
done = 2;
|
|
|
|
else
|
|
|
|
*fp++ = c;
|
|
|
|
} else
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
if (done == 0)
|
|
|
|
c = *str++;
|
|
|
|
}
|
|
|
|
if (done == 2) {
|
|
|
|
/* Found an error in the format */
|
|
|
|
fp = fmt + 1;
|
|
|
|
*fp = *str;
|
|
|
|
olen = 0;
|
|
|
|
} else {
|
|
|
|
if (dot == 0)
|
2017-05-04 12:42:23 +03:00
|
|
|
width = (size_t)val;
|
2010-02-03 18:16:32 +03:00
|
|
|
else
|
2017-05-04 12:42:23 +03:00
|
|
|
precision = (size_t)val;
|
2013-01-24 14:28:28 +04:00
|
|
|
olen = MAX(width, precision);
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
*fp++ = '\0';
|
|
|
|
|
|
|
|
/* Handle commands */
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
2010-02-03 18:16:32 +03:00
|
|
|
if (ochar(term, (unsigned char)val) == 0)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case 's':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(NULL, &ostr, &stack);
|
2010-02-03 18:16:32 +03:00
|
|
|
if (ostr != NULL) {
|
2017-05-04 12:42:23 +03:00
|
|
|
int r;
|
|
|
|
|
2010-02-03 18:16:32 +03:00
|
|
|
l = strlen(ostr);
|
|
|
|
if (l < (size_t)olen)
|
|
|
|
l = olen;
|
|
|
|
if (checkbuf(term, (size_t)(l + 1)) == NULL)
|
|
|
|
return NULL;
|
2017-05-04 12:42:23 +03:00
|
|
|
r = snprintf(term->_buf + term->_bufpos, l + 1,
|
2010-02-03 18:16:32 +03:00
|
|
|
fmt, ostr);
|
2017-05-04 12:42:23 +03:00
|
|
|
if (r != -1)
|
|
|
|
term->_bufpos += (size_t)r;
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'l':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(NULL, &ostr, &stack);
|
2010-02-03 18:16:32 +03:00
|
|
|
if (ostr == NULL)
|
|
|
|
l = 0;
|
|
|
|
else
|
|
|
|
l = strlen(ostr);
|
2013-01-24 14:28:28 +04:00
|
|
|
#ifdef NCURSES_COMPAT_57
|
|
|
|
if (onum(term, "%ld", (long)l, 0) == 0)
|
2010-02-03 18:16:32 +03:00
|
|
|
return NULL;
|
2013-01-24 14:28:28 +04:00
|
|
|
#else
|
|
|
|
push((long)l, NULL, &stack);
|
|
|
|
#endif
|
2010-02-03 18:16:32 +03:00
|
|
|
break;
|
|
|
|
case 'd': /* FALLTHROUGH */
|
|
|
|
case 'o': /* FALLTHROUGH */
|
|
|
|
case 'x': /* FALLTHROUGH */
|
|
|
|
case 'X':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
2013-01-24 14:28:28 +04:00
|
|
|
if (onum(term, fmt, (int)val, olen) == 0)
|
2010-02-03 18:16:32 +03:00
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case 'p':
|
2011-03-10 16:39:26 +03:00
|
|
|
if (*str < '1' || *str > '9')
|
|
|
|
break;
|
2017-05-04 12:42:23 +03:00
|
|
|
l = (size_t)(*str++ - '1');
|
2010-02-03 18:16:32 +03:00
|
|
|
if (push(params[l].num, params[l].string, &stack))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case 'P':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
2010-02-03 18:16:32 +03:00
|
|
|
if (*str >= 'a' && *str <= 'z')
|
|
|
|
dnums[*str - 'a'] = val;
|
|
|
|
else if (*str >= 'A' && *str <= 'Z')
|
|
|
|
term->_snums[*str - 'A'] = val;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
if (*str >= 'a' && *str <= 'z') {
|
|
|
|
if (push(dnums[*str - 'a'], NULL, &stack))
|
|
|
|
return NULL;
|
|
|
|
} else if (*str >= 'A' && *str <= 'Z') {
|
|
|
|
if (push(term->_snums[*str - 'A'],
|
|
|
|
NULL, &stack))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
if (piss[0] == 0)
|
|
|
|
params[0].num++;
|
|
|
|
if (piss[1] == 0)
|
|
|
|
params[1].num++;
|
|
|
|
break;
|
|
|
|
case '\'':
|
2013-01-24 14:28:28 +04:00
|
|
|
if (push((long)(unsigned char)*str++, NULL, &stack))
|
2010-02-03 18:16:32 +03:00
|
|
|
return NULL;
|
|
|
|
while (*str != '\0' && *str != '\'')
|
|
|
|
str++;
|
|
|
|
if (*str == '\'')
|
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
case '{':
|
|
|
|
val = 0;
|
2011-03-10 12:45:32 +03:00
|
|
|
for (; isdigit((unsigned char)*str); str++)
|
2010-02-03 18:16:32 +03:00
|
|
|
val = (val * 10) + (*str - '0');
|
|
|
|
if (push(val, NULL, &stack))
|
|
|
|
return NULL;
|
|
|
|
while (*str != '\0' && *str != '}')
|
|
|
|
str++;
|
|
|
|
if (*str == '}')
|
|
|
|
str++;
|
|
|
|
break;
|
|
|
|
case '+': /* FALLTHROUGH */
|
|
|
|
case '-': /* FALLTHROUGH */
|
|
|
|
case '*': /* FALLTHROUGH */
|
|
|
|
case '/': /* FALLTHROUGH */
|
|
|
|
case 'm': /* FALLTHROUGH */
|
|
|
|
case 'A': /* FALLTHROUGH */
|
|
|
|
case 'O': /* FALLTHROUGH */
|
|
|
|
case '&': /* FALLTHROUGH */
|
|
|
|
case '|': /* FALLTHROUGH */
|
|
|
|
case '^': /* FALLTHROUGH */
|
|
|
|
case '=': /* FALLTHROUGH */
|
|
|
|
case '<': /* FALLTHROUGH */
|
|
|
|
case '>':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
|
|
|
pop(&val2, NULL, &stack);
|
2010-02-03 18:16:32 +03:00
|
|
|
switch (c) {
|
|
|
|
case '+':
|
|
|
|
val = val + val2;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
val = val2 - val;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
val = val * val2;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
val = val ? val2 / val : 0;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
val = val ? val2 % val : 0;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
val = val && val2;
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
val = val || val2;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
val = val & val2;
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
val = val | val2;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
val = val ^ val2;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
val = val == val2;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
val = val2 < val;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
val = val2 > val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (push(val, NULL, &stack))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
case '~':
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
2013-01-24 14:28:28 +04:00
|
|
|
switch (c) {
|
2010-02-03 18:16:32 +03:00
|
|
|
case '!':
|
|
|
|
val = !val;
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
val = ~val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (push(val, NULL, &stack))
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
case '?': /* if */
|
|
|
|
break;
|
|
|
|
case 't': /* then */
|
2011-03-10 16:39:26 +03:00
|
|
|
pop(&val, NULL, &stack);
|
2013-01-23 17:06:18 +04:00
|
|
|
if (val == 0) {
|
2010-02-03 18:16:32 +03:00
|
|
|
l = 0;
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
if (*str != '%')
|
|
|
|
continue;
|
|
|
|
str++;
|
|
|
|
if (*str == '?')
|
|
|
|
l++;
|
|
|
|
else if (*str == ';') {
|
|
|
|
if (l > 0)
|
|
|
|
l--;
|
2013-01-23 17:06:18 +04:00
|
|
|
else {
|
|
|
|
str++;
|
2010-02-03 18:16:32 +03:00
|
|
|
break;
|
2013-01-23 17:06:18 +04:00
|
|
|
}
|
|
|
|
} else if (*str == 'e' && l == 0) {
|
|
|
|
str++;
|
2010-02-03 18:16:32 +03:00
|
|
|
break;
|
2013-01-23 17:06:18 +04:00
|
|
|
}
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e': /* else */
|
|
|
|
l = 0;
|
|
|
|
for (; *str != '\0'; str++) {
|
|
|
|
if (*str != '%')
|
|
|
|
continue;
|
|
|
|
str++;
|
|
|
|
if (*str == '?')
|
|
|
|
l++;
|
|
|
|
else if (*str == ';') {
|
|
|
|
if (l > 0)
|
|
|
|
l--;
|
2013-01-23 17:06:18 +04:00
|
|
|
else {
|
|
|
|
str++;
|
2010-02-03 18:16:32 +03:00
|
|
|
break;
|
2013-01-23 17:06:18 +04:00
|
|
|
}
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ';': /* fi */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
term->_buf[term->_bufpos] = '\0';
|
|
|
|
return term->_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2011-10-03 16:31:51 +04:00
|
|
|
ti_tiparm(TERMINAL *term, const char *str, ...)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
_DIAGASSERT(term != NULL);
|
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
|
|
|
va_start(va, str);
|
2013-01-25 21:28:50 +04:00
|
|
|
ret = _ti_tiparm(term, str, VA_CHAR_INT, va);
|
2010-02-03 18:16:32 +03:00
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2011-10-03 16:31:51 +04:00
|
|
|
tiparm(const char *str, ...)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char *ret;
|
2013-06-07 17:16:18 +04:00
|
|
|
|
2010-02-03 18:16:32 +03:00
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
|
|
|
va_start(va, str);
|
2013-01-25 21:28:50 +04:00
|
|
|
ret = _ti_tiparm(NULL, str, VA_CHAR_INT, va);
|
2013-01-24 14:28:28 +04:00
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-25 21:28:50 +04:00
|
|
|
#ifdef VA_CHAR_LONG
|
|
|
|
char *
|
|
|
|
ti_tlparm(TERMINAL *term, const char *str, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
_DIAGASSERT(term != NULL);
|
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
|
|
|
va_start(va, str);
|
|
|
|
ret = _ti_tiparm(term, str, VA_CHAR_LONG, va);
|
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2013-01-24 14:28:28 +04:00
|
|
|
tlparm(const char *str, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char *ret;
|
2013-06-07 17:16:18 +04:00
|
|
|
|
2013-01-24 14:28:28 +04:00
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
|
|
|
va_start(va, str);
|
2013-01-25 21:28:50 +04:00
|
|
|
ret = _ti_tiparm(NULL, str, VA_CHAR_LONG, va);
|
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *
|
|
|
|
_tparm(const char *str, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
char *ret;
|
2013-06-07 17:16:18 +04:00
|
|
|
|
2013-01-25 21:28:50 +04:00
|
|
|
_DIAGASSERT(str != NULL);
|
|
|
|
|
|
|
|
va_start(va, str);
|
|
|
|
ret = _ti_tiparm(NULL, str, VA_LONG_LONG, va);
|
2010-02-03 18:16:32 +03:00
|
|
|
va_end(va);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
tparm(const char *str,
|
2013-01-24 14:28:28 +04:00
|
|
|
long p1, long p2, long p3, long p4, long p5,
|
|
|
|
long p6, long p7, long p8, long p9)
|
2010-02-03 18:16:32 +03:00
|
|
|
{
|
2011-10-04 00:13:48 +04:00
|
|
|
|
2013-01-25 21:28:50 +04:00
|
|
|
return _tparm(str, p1, p2, p3, p4, p5, p6, p7, p8, p9);
|
2010-02-03 18:16:32 +03:00
|
|
|
}
|