2009-06-08 22:13:07 +02:00
|
|
|
/* $OpenBSD: expr.c,v 1.21 2009/06/01 19:00:57 deraadt Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009
|
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2009-09-06 19:55:55 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.34 2009/09/06 17:55:54 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* The order of these enums is constrained by the order of opinfo[] */
|
|
|
|
enum token {
|
|
|
|
/* some (long) unary operators */
|
|
|
|
O_PLUSPLUS = 0, O_MINUSMINUS,
|
|
|
|
/* binary operators */
|
|
|
|
O_EQ, O_NE,
|
|
|
|
/* assignments are assumed to be in range O_ASN .. O_BORASN */
|
|
|
|
O_ASN, O_TIMESASN, O_DIVASN, O_MODASN, O_PLUSASN, O_MINUSASN,
|
|
|
|
O_LSHIFTASN, O_RSHIFTASN, O_BANDASN, O_BXORASN, O_BORASN,
|
|
|
|
O_LSHIFT, O_RSHIFT,
|
|
|
|
O_LE, O_GE, O_LT, O_GT,
|
|
|
|
O_LAND,
|
|
|
|
O_LOR,
|
|
|
|
O_TIMES, O_DIV, O_MOD,
|
|
|
|
O_PLUS, O_MINUS,
|
|
|
|
O_BAND,
|
|
|
|
O_BXOR,
|
|
|
|
O_BOR,
|
|
|
|
O_TERN,
|
|
|
|
O_COMMA,
|
|
|
|
/* things after this aren't used as binary operators */
|
|
|
|
/* unary that are not also binaries */
|
|
|
|
O_BNOT, O_LNOT,
|
|
|
|
/* misc */
|
|
|
|
OPEN_PAREN, CLOSE_PAREN, CTERN,
|
|
|
|
/* things that don't appear in the opinfo[] table */
|
|
|
|
VAR, LIT, END, BAD
|
|
|
|
};
|
|
|
|
#define IS_BINOP(op) (((int)op) >= (int)O_EQ && ((int)op) <= (int)O_COMMA)
|
|
|
|
#define IS_ASSIGNOP(op) ((int)(op) >= (int)O_ASN && (int)(op) <= (int)O_BORASN)
|
|
|
|
|
2008-03-28 14:33:37 +01:00
|
|
|
/* precisions; used to be enum prec but we do arithmetics on it */
|
|
|
|
#define P_PRIMARY 0 /* VAR, LIT, (), ~ ! - + */
|
|
|
|
#define P_MULT 1 /* * / % */
|
|
|
|
#define P_ADD 2 /* + - */
|
|
|
|
#define P_SHIFT 3 /* << >> */
|
|
|
|
#define P_RELATION 4 /* < <= > >= */
|
|
|
|
#define P_EQUALITY 5 /* == != */
|
|
|
|
#define P_BAND 6 /* & */
|
|
|
|
#define P_BXOR 7 /* ^ */
|
|
|
|
#define P_BOR 8 /* | */
|
|
|
|
#define P_LAND 9 /* && */
|
|
|
|
#define P_LOR 10 /* || */
|
|
|
|
#define P_TERN 11 /* ?: */
|
|
|
|
#define P_ASSIGN 12 /* = *= /= %= += -= <<= >>= &= ^= |= */
|
|
|
|
#define P_COMMA 13 /* , */
|
2005-05-23 05:06:10 +02:00
|
|
|
#define MAX_PREC P_COMMA
|
|
|
|
|
|
|
|
struct opinfo {
|
|
|
|
char name[4];
|
|
|
|
int len; /* name length */
|
2008-03-28 14:33:37 +01:00
|
|
|
int prec; /* precedence: lower is higher */
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Tokens in this table must be ordered so the longest are first
|
2009-06-10 20:12:51 +02:00
|
|
|
* (eg, += before +). If you change something, change the order
|
2005-05-23 05:06:10 +02:00
|
|
|
* of enum token too.
|
|
|
|
*/
|
|
|
|
static const struct opinfo opinfo[] = {
|
|
|
|
{ "++", 2, P_PRIMARY }, /* before + */
|
|
|
|
{ "--", 2, P_PRIMARY }, /* before - */
|
|
|
|
{ "==", 2, P_EQUALITY }, /* before = */
|
|
|
|
{ "!=", 2, P_EQUALITY }, /* before ! */
|
|
|
|
{ "=", 1, P_ASSIGN }, /* keep assigns in a block */
|
|
|
|
{ "*=", 2, P_ASSIGN },
|
|
|
|
{ "/=", 2, P_ASSIGN },
|
|
|
|
{ "%=", 2, P_ASSIGN },
|
|
|
|
{ "+=", 2, P_ASSIGN },
|
|
|
|
{ "-=", 2, P_ASSIGN },
|
|
|
|
{ "<<=", 3, P_ASSIGN },
|
|
|
|
{ ">>=", 3, P_ASSIGN },
|
|
|
|
{ "&=", 2, P_ASSIGN },
|
|
|
|
{ "^=", 2, P_ASSIGN },
|
|
|
|
{ "|=", 2, P_ASSIGN },
|
|
|
|
{ "<<", 2, P_SHIFT },
|
|
|
|
{ ">>", 2, P_SHIFT },
|
|
|
|
{ "<=", 2, P_RELATION },
|
|
|
|
{ ">=", 2, P_RELATION },
|
|
|
|
{ "<", 1, P_RELATION },
|
|
|
|
{ ">", 1, P_RELATION },
|
|
|
|
{ "&&", 2, P_LAND },
|
|
|
|
{ "||", 2, P_LOR },
|
|
|
|
{ "*", 1, P_MULT },
|
|
|
|
{ "/", 1, P_MULT },
|
|
|
|
{ "%", 1, P_MULT },
|
|
|
|
{ "+", 1, P_ADD },
|
|
|
|
{ "-", 1, P_ADD },
|
|
|
|
{ "&", 1, P_BAND },
|
|
|
|
{ "^", 1, P_BXOR },
|
|
|
|
{ "|", 1, P_BOR },
|
|
|
|
{ "?", 1, P_TERN },
|
|
|
|
{ ",", 1, P_COMMA },
|
|
|
|
{ "~", 1, P_PRIMARY },
|
|
|
|
{ "!", 1, P_PRIMARY },
|
|
|
|
{ "(", 1, P_PRIMARY },
|
|
|
|
{ ")", 1, P_PRIMARY },
|
|
|
|
{ ":", 1, P_PRIMARY },
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
{ "", 0, P_PRIMARY }
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct expr_state Expr_state;
|
|
|
|
struct expr_state {
|
|
|
|
const char *expression; /* expression being evaluated */
|
|
|
|
const char *tokp; /* lexical position */
|
|
|
|
struct tbl *val; /* value from token() */
|
|
|
|
struct tbl *evaling; /* variable that is being recursively
|
2007-10-25 17:19:16 +02:00
|
|
|
* expanded (EXPRINEVAL flag set) */
|
2009-04-07 20:41:37 +02:00
|
|
|
int noassign; /* don't do assigns (for ?:,&&,||) */
|
|
|
|
enum token tok; /* token from token() */
|
2007-10-25 17:19:16 +02:00
|
|
|
bool arith; /* evaluating an $(()) expression? */
|
2008-12-17 20:39:23 +01:00
|
|
|
bool natural; /* unsigned arithmetic calculation */
|
2005-05-23 05:06:10 +02:00
|
|
|
};
|
|
|
|
|
2009-03-14 19:12:55 +01:00
|
|
|
#define bivui(x, op, y) (es->natural ? \
|
2009-06-10 20:12:51 +02:00
|
|
|
(mksh_ari_t)((x)->val.u op (y)->val.u) : \
|
|
|
|
(mksh_ari_t)((x)->val.i op (y)->val.i) \
|
|
|
|
)
|
2008-12-17 20:39:23 +01:00
|
|
|
#define chvui(x, op) do { \
|
|
|
|
if (es->natural) \
|
|
|
|
(x)->val.u = op (x)->val.u; \
|
|
|
|
else \
|
|
|
|
(x)->val.i = op (x)->val.i; \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
#define stvui(x, n) do { \
|
|
|
|
if (es->natural) \
|
|
|
|
(x)->val.u = (n); \
|
|
|
|
else \
|
|
|
|
(x)->val.i = (n); \
|
|
|
|
} while (/* CONSTCOND */ 0)
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
enum error_type {
|
|
|
|
ET_UNEXPECTED, ET_BADLIT, ET_RECURSIVE,
|
|
|
|
ET_LVALUE, ET_RDONLY, ET_STR
|
|
|
|
};
|
|
|
|
|
2007-01-17 23:51:47 +01:00
|
|
|
static void evalerr(Expr_state *, enum error_type, const char *)
|
|
|
|
__attribute__((noreturn));
|
2008-03-28 14:33:37 +01:00
|
|
|
static struct tbl *evalexpr(Expr_state *, int);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void exprtoken(Expr_state *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct tbl *do_ppmm(Expr_state *, enum token, struct tbl *, bool);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void assign_check(Expr_state *, enum token, struct tbl *);
|
2005-05-23 05:06:10 +02:00
|
|
|
static struct tbl *tempvar(void);
|
|
|
|
static struct tbl *intvar(Expr_state *, struct tbl *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse and evaluate expression
|
|
|
|
*/
|
|
|
|
int
|
2009-03-14 19:12:55 +01:00
|
|
|
evaluate(const char *expr, mksh_ari_t *rval, int error_ok, bool arith)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl v;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
v.flag = DEFINED|INTEGER;
|
|
|
|
v.type = 0;
|
|
|
|
ret = v_evaluate(&v, expr, error_ok, arith);
|
|
|
|
*rval = v.val.i;
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (ret);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse and evaluate expression, storing result in vp.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
v_evaluate(struct tbl *vp, const char *expr, volatile int error_ok,
|
|
|
|
bool arith)
|
|
|
|
{
|
|
|
|
struct tbl *v;
|
|
|
|
Expr_state curstate;
|
|
|
|
Expr_state * const es = &curstate;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* save state to allow recursive calls */
|
|
|
|
curstate.expression = curstate.tokp = expr;
|
|
|
|
curstate.noassign = 0;
|
|
|
|
curstate.arith = arith;
|
|
|
|
curstate.evaling = NULL;
|
2008-12-17 20:39:23 +01:00
|
|
|
curstate.natural = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
newenv(E_ERRH);
|
|
|
|
i = sigsetjmp(e->jbuf, 0);
|
|
|
|
if (i) {
|
|
|
|
/* Clear EXPRINEVAL in of any variables we were playing with */
|
|
|
|
if (curstate.evaling)
|
|
|
|
curstate.evaling->flag &= ~EXPRINEVAL;
|
|
|
|
quitenv(NULL);
|
|
|
|
if (i == LAEXPR) {
|
|
|
|
if (error_ok == KSH_RETURN_ERROR)
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (0);
|
2007-07-22 16:01:50 +02:00
|
|
|
errorfz();
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
unwind(i);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (es->tok == END) {
|
|
|
|
es->tok = LIT;
|
|
|
|
es->val = tempvar();
|
|
|
|
}
|
|
|
|
v = intvar(es, evalexpr(es, MAX_PREC));
|
|
|
|
|
|
|
|
if (es->tok != END)
|
|
|
|
evalerr(es, ET_UNEXPECTED, NULL);
|
|
|
|
|
2008-12-17 20:39:23 +01:00
|
|
|
if (es->arith && es->natural)
|
|
|
|
vp->flag |= INT_U;
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vp->flag & INTEGER)
|
|
|
|
setint_v(vp, v, es->arith);
|
|
|
|
else
|
|
|
|
/* can fail if readonly */
|
|
|
|
setstr(vp, str_val(v), error_ok);
|
|
|
|
|
|
|
|
quitenv(NULL);
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (1);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
evalerr(Expr_state *es, enum error_type type, const char *str)
|
|
|
|
{
|
|
|
|
char tbuf[2];
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
es->arith = false;
|
|
|
|
switch (type) {
|
|
|
|
case ET_UNEXPECTED:
|
|
|
|
switch (es->tok) {
|
|
|
|
case VAR:
|
|
|
|
s = es->val->name;
|
|
|
|
break;
|
|
|
|
case LIT:
|
|
|
|
s = str_val(es->val);
|
|
|
|
break;
|
|
|
|
case END:
|
|
|
|
s = "end of expression";
|
|
|
|
break;
|
|
|
|
case BAD:
|
|
|
|
tbuf[0] = *es->tokp;
|
|
|
|
tbuf[1] = '\0';
|
|
|
|
s = tbuf;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s = opinfo[(int)es->tok].name;
|
|
|
|
}
|
|
|
|
warningf(true, "%s: unexpected '%s'", es->expression, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_BADLIT:
|
|
|
|
warningf(true, "%s: bad number '%s'", es->expression, str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_RECURSIVE:
|
|
|
|
warningf(true, "%s: expression recurses on parameter '%s'",
|
|
|
|
es->expression, str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_LVALUE:
|
|
|
|
warningf(true, "%s: %s requires lvalue",
|
|
|
|
es->expression, str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ET_RDONLY:
|
|
|
|
warningf(true, "%s: %s applied to read only variable",
|
|
|
|
es->expression, str);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* keep gcc happy */
|
|
|
|
case ET_STR:
|
|
|
|
warningf(true, "%s: %s", es->expression, str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
unwind(LAEXPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct tbl *
|
2008-03-28 14:33:37 +01:00
|
|
|
evalexpr(Expr_state *es, int prec)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct tbl *vl, *vr = NULL, *vasn;
|
|
|
|
enum token op;
|
2009-03-14 19:12:55 +01:00
|
|
|
mksh_ari_t res = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (prec == P_PRIMARY) {
|
|
|
|
op = es->tok;
|
|
|
|
if (op == O_BNOT || op == O_LNOT || op == O_MINUS ||
|
|
|
|
op == O_PLUS) {
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = intvar(es, evalexpr(es, P_PRIMARY));
|
|
|
|
if (op == O_BNOT)
|
2008-12-17 20:39:23 +01:00
|
|
|
chvui(vl, ~);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (op == O_LNOT)
|
2008-12-17 20:39:23 +01:00
|
|
|
chvui(vl, !);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (op == O_MINUS)
|
2008-12-17 20:39:23 +01:00
|
|
|
chvui(vl, -);
|
2005-05-23 05:06:10 +02:00
|
|
|
/* op == O_PLUS is a no-op */
|
|
|
|
} else if (op == OPEN_PAREN) {
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = evalexpr(es, MAX_PREC);
|
|
|
|
if (es->tok != CLOSE_PAREN)
|
|
|
|
evalerr(es, ET_STR, "missing )");
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (op == O_PLUSPLUS || op == O_MINUSMINUS) {
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vl = do_ppmm(es, op, es->val, true);
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (op == VAR || op == LIT) {
|
|
|
|
vl = es->val;
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
evalerr(es, ET_UNEXPECTED, NULL);
|
2006-05-10 20:54:13 +02:00
|
|
|
/* NOTREACHED */
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
if (es->tok == O_PLUSPLUS || es->tok == O_MINUSMINUS) {
|
|
|
|
vl = do_ppmm(es, es->tok, vl, false);
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vl);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2008-03-28 14:33:37 +01:00
|
|
|
vl = evalexpr(es, prec - 1);
|
2008-12-17 20:39:23 +01:00
|
|
|
for (op = es->tok; IS_BINOP(op) && opinfo[(int)op].prec == prec;
|
2005-05-23 05:06:10 +02:00
|
|
|
op = es->tok) {
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
vasn = vl;
|
|
|
|
if (op != O_ASN) /* vl may not have a value yet */
|
|
|
|
vl = intvar(es, vl);
|
|
|
|
if (IS_ASSIGNOP(op)) {
|
|
|
|
assign_check(es, op, vasn);
|
|
|
|
vr = intvar(es, evalexpr(es, P_ASSIGN));
|
|
|
|
} else if (op != O_TERN && op != O_LAND && op != O_LOR)
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2005-05-23 05:06:10 +02:00
|
|
|
if ((op == O_DIV || op == O_MOD || op == O_DIVASN ||
|
|
|
|
op == O_MODASN) && vr->val.i == 0) {
|
|
|
|
if (es->noassign)
|
|
|
|
vr->val.i = 1;
|
|
|
|
else
|
|
|
|
evalerr(es, ET_STR, "zero divisor");
|
|
|
|
}
|
2008-12-17 20:39:23 +01:00
|
|
|
switch ((int)op) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case O_TIMES:
|
|
|
|
case O_TIMESASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, *, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_DIV:
|
|
|
|
case O_DIVASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, /, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_MOD:
|
|
|
|
case O_MODASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, %, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_PLUS:
|
|
|
|
case O_PLUSASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, +, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_MINUS:
|
|
|
|
case O_MINUSASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, -, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LSHIFT:
|
|
|
|
case O_LSHIFTASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, <<, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_RSHIFT:
|
|
|
|
case O_RSHIFTASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, >>, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LT:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, <, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LE:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, <=, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_GT:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, >, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_GE:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, >=, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_EQ:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, ==, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_NE:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, !=, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BAND:
|
|
|
|
case O_BANDASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, &, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BXOR:
|
|
|
|
case O_BXORASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, ^, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_BOR:
|
|
|
|
case O_BORASN:
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, |, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case O_LAND:
|
|
|
|
if (!vl->val.i)
|
|
|
|
es->noassign++;
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, &&, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (!vl->val.i)
|
|
|
|
es->noassign--;
|
|
|
|
break;
|
|
|
|
case O_LOR:
|
|
|
|
if (vl->val.i)
|
|
|
|
es->noassign++;
|
2008-03-28 14:33:37 +01:00
|
|
|
vr = intvar(es, evalexpr(es, prec - 1));
|
2008-12-17 20:39:23 +01:00
|
|
|
res = bivui(vl, ||, vr);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vl->val.i)
|
|
|
|
es->noassign--;
|
|
|
|
break;
|
|
|
|
case O_TERN:
|
|
|
|
{
|
2008-12-17 20:39:23 +01:00
|
|
|
bool ev = vl->val.i != 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (!ev)
|
|
|
|
es->noassign++;
|
|
|
|
vl = evalexpr(es, MAX_PREC);
|
|
|
|
if (!ev)
|
|
|
|
es->noassign--;
|
|
|
|
if (es->tok != CTERN)
|
|
|
|
evalerr(es, ET_STR, "missing :");
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(es);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (ev)
|
|
|
|
es->noassign++;
|
|
|
|
vr = evalexpr(es, P_TERN);
|
|
|
|
if (ev)
|
|
|
|
es->noassign--;
|
|
|
|
vl = ev ? vl : vr;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case O_ASN:
|
|
|
|
res = vr->val.i;
|
|
|
|
break;
|
|
|
|
case O_COMMA:
|
|
|
|
res = vr->val.i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (IS_ASSIGNOP(op)) {
|
2008-12-17 20:39:23 +01:00
|
|
|
stvui(vr, res);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vasn->flag & INTEGER)
|
|
|
|
setint_v(vasn, vr, es->arith);
|
|
|
|
else
|
|
|
|
setint(vasn, res);
|
|
|
|
vl = vr;
|
|
|
|
} else if (op != O_TERN)
|
2008-12-17 20:39:23 +01:00
|
|
|
stvui(vl, res);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vl);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-03-03 22:12:51 +01:00
|
|
|
exprtoken(Expr_state *es)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2008-12-17 20:39:23 +01:00
|
|
|
const char *cp = es->tokp;
|
2005-05-23 05:06:10 +02:00
|
|
|
int c;
|
|
|
|
char *tvar;
|
|
|
|
|
|
|
|
/* skip white space */
|
2008-12-17 20:39:23 +01:00
|
|
|
skip_spaces:
|
|
|
|
while ((c = *cp), ksh_isspace(c))
|
|
|
|
++cp;
|
|
|
|
if (es->tokp == es->expression && c == '#') {
|
|
|
|
/* expression begins with # */
|
|
|
|
es->natural = true; /* switch to unsigned */
|
|
|
|
++cp;
|
|
|
|
goto skip_spaces;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
es->tokp = cp;
|
|
|
|
|
|
|
|
if (c == '\0')
|
|
|
|
es->tok = END;
|
2006-11-10 08:52:04 +01:00
|
|
|
else if (ksh_isalphx(c)) {
|
|
|
|
for (; ksh_isalnux(c); c = *cp)
|
2005-05-23 05:06:10 +02:00
|
|
|
cp++;
|
|
|
|
if (c == '[') {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = array_ref_len(cp);
|
|
|
|
if (len == 0)
|
|
|
|
evalerr(es, ET_STR, "missing ]");
|
|
|
|
cp += len;
|
|
|
|
} else if (c == '(' /*)*/ ) {
|
|
|
|
/* todo: add math functions (all take single argument):
|
|
|
|
* abs acos asin atan cos cosh exp int log sin sinh sqrt
|
|
|
|
* tan tanh
|
|
|
|
*/
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (es->noassign) {
|
|
|
|
es->val = tempvar();
|
|
|
|
es->val->flag |= EXPRLVALUE;
|
|
|
|
} else {
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
es->val = global(tvar);
|
|
|
|
afree(tvar, ATEMP);
|
|
|
|
}
|
|
|
|
es->tok = VAR;
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
} else if (c == '1' && cp[1] == '#') {
|
2008-10-28 15:32:43 +01:00
|
|
|
cp += 2;
|
2009-05-16 17:09:07 +02:00
|
|
|
cp += utf_ptradj(cp);
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, cp - es->tokp, ATEMP);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
goto process_tvar;
|
2009-09-06 19:55:55 +02:00
|
|
|
} else if (c == '\'') {
|
|
|
|
++cp;
|
|
|
|
cp += utf_ptradj(cp);
|
|
|
|
if (*cp++ != '\'')
|
|
|
|
evalerr(es, ET_STR,
|
|
|
|
"multi-character character constant");
|
|
|
|
/* 'x' -> 1#x (x = one multibyte character) */
|
|
|
|
c = cp - es->tokp;
|
|
|
|
tvar = alloc(c + /* NUL */ 1, ATEMP);
|
|
|
|
tvar[0] = '1';
|
|
|
|
tvar[1] = '#';
|
|
|
|
memcpy(tvar + 2, es->tokp + 1, c - 2);
|
|
|
|
tvar[c] = '\0';
|
|
|
|
goto process_tvar;
|
2006-11-10 08:52:04 +01:00
|
|
|
} else if (ksh_isdigit(c)) {
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
while (c != '_' && (ksh_isalnux(c) || c == '#'))
|
|
|
|
c = *cp++;
|
2008-10-28 15:32:43 +01:00
|
|
|
strndupx(tvar, es->tokp, --cp - es->tokp, ATEMP);
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
process_tvar:
|
2005-05-23 05:06:10 +02:00
|
|
|
es->val = tempvar();
|
|
|
|
es->val->flag &= ~INTEGER;
|
|
|
|
es->val->type = 0;
|
|
|
|
es->val->val.s = tvar;
|
|
|
|
if (setint_v(es->val, es->val, es->arith) == NULL)
|
|
|
|
evalerr(es, ET_BADLIT, tvar);
|
|
|
|
afree(tvar, ATEMP);
|
|
|
|
es->tok = LIT;
|
|
|
|
} else {
|
|
|
|
int i, n0;
|
|
|
|
|
|
|
|
for (i = 0; (n0 = opinfo[i].name[0]); i++)
|
2009-08-08 15:08:53 +02:00
|
|
|
if (c == n0 && strncmp(cp, opinfo[i].name,
|
|
|
|
(size_t)opinfo[i].len) == 0) {
|
2008-12-17 20:39:23 +01:00
|
|
|
es->tok = (enum token)i;
|
2005-05-23 05:06:10 +02:00
|
|
|
cp += opinfo[i].len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!n0)
|
|
|
|
es->tok = BAD;
|
|
|
|
}
|
|
|
|
es->tokp = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do a ++ or -- operation */
|
|
|
|
static struct tbl *
|
|
|
|
do_ppmm(Expr_state *es, enum token op, struct tbl *vasn, bool is_prefix)
|
|
|
|
{
|
|
|
|
struct tbl *vl;
|
2009-03-14 19:12:55 +01:00
|
|
|
mksh_ari_t oval;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
assign_check(es, op, vasn);
|
|
|
|
|
|
|
|
vl = intvar(es, vasn);
|
2008-12-17 20:39:23 +01:00
|
|
|
oval = vl->val.i;
|
|
|
|
if (op == O_PLUSPLUS) {
|
|
|
|
if (es->natural)
|
|
|
|
++vl->val.u;
|
|
|
|
else
|
|
|
|
++vl->val.i;
|
|
|
|
} else {
|
|
|
|
if (es->natural)
|
|
|
|
--vl->val.u;
|
|
|
|
else
|
|
|
|
--vl->val.i;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
if (vasn->flag & INTEGER)
|
|
|
|
setint_v(vasn, vl, es->arith);
|
|
|
|
else
|
|
|
|
setint(vasn, vl->val.i);
|
|
|
|
if (!is_prefix) /* undo the inc/dec */
|
|
|
|
vl->val.i = oval;
|
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vl);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
assign_check(Expr_state *es, enum token op, struct tbl *vasn)
|
|
|
|
{
|
2009-06-08 22:13:07 +02:00
|
|
|
if (es->tok == END ||
|
|
|
|
(vasn->name[0] == '\0' && !(vasn->flag & EXPRLVALUE)))
|
2008-12-17 20:39:23 +01:00
|
|
|
evalerr(es, ET_LVALUE, opinfo[(int)op].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
else if (vasn->flag & RDONLY)
|
2008-12-17 20:39:23 +01:00
|
|
|
evalerr(es, ET_RDONLY, opinfo[(int)op].name);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct tbl *
|
|
|
|
tempvar(void)
|
|
|
|
{
|
|
|
|
struct tbl *vp;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
vp = alloc(sizeof(struct tbl), ATEMP);
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->flag = ISSET|INTEGER;
|
|
|
|
vp->type = 0;
|
|
|
|
vp->areap = ATEMP;
|
2009-08-28 22:38:43 +02:00
|
|
|
#ifdef notyet_ktremove
|
2009-08-28 22:30:59 +02:00
|
|
|
vp->tablep = NULL;
|
2009-08-28 22:38:43 +02:00
|
|
|
#endif
|
2009-08-28 23:01:27 +02:00
|
|
|
vp->ua.hval = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
vp->val.i = 0;
|
|
|
|
vp->name[0] = '\0';
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vp);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cast (string) variable to temporary integer variable */
|
|
|
|
static struct tbl *
|
|
|
|
intvar(Expr_state *es, struct tbl *vp)
|
|
|
|
{
|
|
|
|
struct tbl *vq;
|
|
|
|
|
|
|
|
/* try to avoid replacing a temp var with another temp var */
|
|
|
|
if (vp->name[0] == '\0' &&
|
|
|
|
(vp->flag & (ISSET|INTEGER|EXPRLVALUE)) == (ISSET|INTEGER))
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vp);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
vq = tempvar();
|
|
|
|
if (setint_v(vq, vp, es->arith) == NULL) {
|
|
|
|
if (vp->flag & EXPRINEVAL)
|
|
|
|
evalerr(es, ET_RECURSIVE, vp->name);
|
|
|
|
es->evaling = vp;
|
|
|
|
vp->flag |= EXPRINEVAL;
|
|
|
|
v_evaluate(vq, str_val(vp), KSH_UNWIND_ERROR, es->arith);
|
|
|
|
vp->flag &= ~EXPRINEVAL;
|
|
|
|
es->evaling = NULL;
|
|
|
|
}
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
return (vq);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|