home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
gnu
/
sh-utils-1.12-src.lha
/
sh-utils-1.12
/
src
/
expr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-11-12
|
14KB
|
766 lines
/* expr -- evaluate expressions.
Copyright (C) 1986, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Author: Mike Parker.
This program evaluates expressions. Each token (operator, operand,
parenthesis) of the expression must be a seperate argument. The
parser used is a reasonably general one, though any incarnation of
it is language-specific. It is especially nice for expressions.
No parse tree is needed; a new node is evaluated immediately.
One function can handle multiple operators all of equal precedence,
provided they all associate ((x op x) op x).
Define EVAL_TRACE to print an evaluation trace. */
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
#include "system.h"
#include "version.h"
#include "long-options.h"
#define NEW(type) ((type *) xmalloc (sizeof (type)))
#define OLD(x) free ((char *) x)
/* The kinds of value we can have. */
enum valtype
{
integer,
string
};
typedef enum valtype TYPE;
/* A value is.... */
struct valinfo
{
TYPE type; /* Which kind. */
union
{ /* The value itself. */
int i;
char *s;
} u;
};
typedef struct valinfo VALUE;
/* The arguments given to the program, minus the program name. */
static char **args;
/* The name this program was run with. */
char *program_name;
void error ();
char *xstrdup ();
char *strstr ();
char *xmalloc ();
static VALUE *docolon ();
static VALUE *eval ();
static VALUE *int_value ();
static VALUE *str_value ();
static int isstring ();
static int nextarg ();
static int nomoreargs ();
static int null ();
static int toarith ();
static void freev ();
static void printv ();
static void tostring ();
#ifdef EVAL_TRACE
static void trace ();
#endif
static void
usage (status)
int status;
{
if (status != 0)
fprintf (stderr, "Try `%s --help' for more information.\n",
program_name);
else
{
printf ("\
Usage: %s EXPRESSION\n\
or: %s OPTION\n\
",
program_name, program_name);
printf ("\
\n\
--help display this help and exit\n\
--version output version information and exit\n\
\n\
");
printf ("\
EXPRESSION value is written on standard output. A white line\n\
separates increasing precedence groups. EXPRESSION may be:\n\
\n\
ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n\
\n\
ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n\
\n\
ARG1 < ARG2 ARG1 is less than ARG2\n\
ARG1 <= ARG2 ARG1 is less than or equal to ARG2\n\
ARG1 = ARG2 ARG1 is equal to ARG2\n\
ARG1 != ARG2 ARG1 is unequal to ARG2\n\
ARG1 >= ARG2 ARG1 is greater than or equal to ARG2\n\
ARG1 > ARG2 ARG1 is greater than ARG2\n\
\n\
ARG1 + ARG2 arithmetic sum of ARG1 and ARG2\n\
ARG1 - ARG2 arithmetic difference of ARG1 and ARG2\n\
\n\
ARG1 * ARG2 arithmetic product of ARG1 and ARG2\n\
ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2\n\
ARG1 %% ARG2 arithmetic remainder of ARG1 divided by ARG2\n\
\n\
STRING : REGEXP anchored pattern match of REGEXP in STRING\n\
\n\
match STRING REGEXP same as STRING : REGEXP\n\
substr STRING POS LENGTH substring of STRING, POS counted from 1\n\
index STRING CHARS index in STRING where any CHARS is found, or 0\n\
length STRING length of STRING\n\
\n\
( EXPRESSION ) value of EXPRESSION\n\
");
printf ("\
\n\
Beware that some operators need to be escaped by backslashes for shells.\n\
Comparisons are arithmetic if both ARGs are numbers, else lexicographical.\n\
Pattern matches return the string matched between \\( and \\) or null; if\n\
\\( and \\) are not used, they return the number of characters matched or 0.\n\
");
}
exit (status);
}
main (argc, argv)
int argc;
char **argv;
{
VALUE *v;
program_name = argv[0];
parse_long_options (argc, argv, "expr", version_string, usage);
if (argc == 1)
{
error (0, 0, "too few arguments");
usage (1);
}
args = argv + 1;
v = eval ();
if (!nomoreargs ())
error (2, 0, "syntax error");
printv (v);
exit (null (v));
}
/* Return a VALUE for I. */
static VALUE *
int_value (i)
int i;
{
VALUE *v;
v = NEW (VALUE);
v->type = integer;
v->u.i = i;
return v;
}
/* Return a VALUE for S. */
static VALUE *
str_value (s)
char *s;
{
VALUE *v;
v = NEW (VALUE);
v->type = string;
v->u.s = xstrdup (s);
return v;
}
/* Free VALUE V, including structure components. */
static void
freev (v)
VALUE *v;
{
if (v->type == string)
free (v->u.s);
OLD (v);
}
/* Print VALUE V. */
static void
printv (v)
VALUE *v;
{
switch (v->type)
{
case integer:
printf ("%d\n", v->u.i);
break;
case string:
printf ("%s\n", v->u.s);
break;
default:
abort ();
}
}
/* Return nonzero if V is a null-string or zero-number. */
static int
null (v)
VALUE *v;
{
switch (v->type)
{
case integer:
return v->u.i == 0;
case string:
return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
default:
abort ();
}
}
/* Return nonzero if V is a string value. */
static int
isstring (v)
VALUE *v;
{
return v->type == string;
}
/* Coerce V to a string value (can't fail). */
static void
tostring (v)
VALUE *v;
{
char *temp;
switch (v->type)
{
case integer:
temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
sprintf (temp, "%d", v->u.i);
v->u.s = temp;
v->type = string;
break;
case string:
break;
default:
abort ();
}
}
/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
static int
toarith (v)
VALUE *v;
{
int i;
int neg;
char *cp;
switch (v->type)
{
case integer:
return 1;
case string:
i = 0;
cp = v->u.s;
/* Don't interpret the empty string as an integer. */
if (*cp == 0)
return 0;
neg = (*cp == '-');
if (neg)
cp++;
for (; *cp; cp++)
{
if (ISDIGIT (*cp))
i = i * 10 + *cp - '0';
else
return 0;
}
free (v->u.s);
v->u.i = i * (neg ? -1 : 1);
v->type = integer;
return 1;
default:
abort ();
}
}
/* Return nonzero if the next token matches STR exactly.
STR must not be NULL. */
static int
nextarg (str)
char *str;
{
if (*args == NULL)
return 0;
return strcmp (*args, str) == 0;
}
/* Return nonzero if there no more tokens. */
static int
nomoreargs ()
{
return *args == 0;
}
/* The comparison operator handling functions. */
#define cmpf(name, rel) \
static \
int name (l, r) VALUE *l; VALUE *r; \
{ \
if (isstring (l) || isstring (r)) \
{ \
tostring (l); \
tostring (r); \
return strcmp (l->u.s, r->u.s) rel 0; \
} \
else \
return l->u.i rel r->u.i; \
}
cmpf (less_than, <)
cmpf (less_equal, <=)
cmpf (equal, ==)
cmpf (not_equal, !=)
cmpf (greater_equal, >=)
cmpf (greater_than, >)
#undef cmpf
/* The arithmetic operator handling functions. */
#define arithf(name, op) \
static \
int name (l, r) VALUE *l; VALUE *r; \
{ \
if (!toarith (l) || !toarith (r)) \
error (2, 0, "non-numeric argument"); \
return l->u.i op r->u.i; \
}
#define arithdivf(name, op) \
int name (l, r) VALUE *l; VALUE *r; \
{ \
if (!toarith (l) || !toarith (r)) \
error (2, 0, "non-numer