1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-10 14:13:31 +02:00
Files
coreutils/src/expr.c

758 lines
14 KiB
C
Raw Normal View History

1992-11-01 05:44:29 +00:00
/* expr -- evaluate expressions.
1996-04-24 04:45:54 +00:00
Copyright (C) 86, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
1992-11-01 05:44:29 +00:00
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
1996-03-24 18:33:12 +00:00
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1992-11-01 05:44:29 +00:00
/* 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. */
1993-10-12 14:49:11 +00:00
#include <config.h>
1992-11-01 05:44:29 +00:00
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>
1992-11-01 05:44:29 +00:00
#include "system.h"
1993-10-26 00:11:14 +00:00
#include "long-options.h"
#include "error.h"
1992-11-01 05:44:29 +00:00
#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. */
1992-11-01 06:50:15 +00:00
static char **args;
1992-11-01 05:44:29 +00:00
/* The name this program was run with. */
char *program_name;
char *xstrdup ();
char *strstr ();
char *xmalloc ();
1996-01-06 11:44:05 +00:00
static VALUE *docolon __P ((VALUE *sv, VALUE *pv));
static VALUE *eval __P ((void));
static VALUE *int_value __P ((int i));
static VALUE *str_value __P ((char *s));
static int isstring __P ((VALUE *v));
static int nextarg __P ((char *str));
static int nomoreargs __P ((void));
static int null __P ((VALUE *v));
static int toarith __P ((VALUE *v));
static void freev __P ((VALUE *v));
static void printv __P ((VALUE *v));
static void tostring __P ((VALUE *v));
#ifdef EVAL_TRACE
static void trace ();
#endif
1992-11-01 05:44:29 +00:00
static void
1996-01-06 11:44:05 +00:00
usage (int status)
{
1993-10-17 03:57:04 +00:00
if (status != 0)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
1993-10-17 03:57:04 +00:00
program_name);
else
{
printf (_("\
1993-10-26 00:11:14 +00:00
Usage: %s EXPRESSION\n\
or: %s OPTION\n\
"),
1993-10-26 00:11:14 +00:00
program_name, program_name);
printf (_("\
1993-10-17 03:57:04 +00:00
\n\
--help display this help and exit\n\
--version output version information and exit\n\
\n\
"));
printf (_("\
Print the value of EXPRESSION to standard output. A blank line below\n\
1993-10-17 03:57:04 +00:00
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\
1993-10-24 21:30:08 +00:00
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 (_("\
1993-10-17 03:57:04 +00:00
\n\
Beware that many operators need to be escaped or quoted for shells.\n\
1993-10-17 03:57:04 +00:00
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\
"));
1993-10-17 03:57:04 +00:00
}
exit (status);
}
int
1996-01-06 11:44:05 +00:00
main (int argc, char **argv)
1993-10-04 21:20:37 +00:00
{
VALUE *v;
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
1993-10-04 21:20:37 +00:00
parse_long_options (argc, argv, "expr", PACKAGE_VERSION, usage);
1993-10-04 21:20:37 +00:00
if (argc == 1)
1994-10-21 05:25:03 +00:00
{
error (0, 0, _("too few arguments"));
1994-10-21 05:25:03 +00:00
usage (1);
}
1992-11-01 05:44:29 +00:00
args = argv + 1;
v = eval ();
if (!nomoreargs ())
error (2, 0, _("syntax error"));
1992-11-01 05:44:29 +00:00
printv (v);
exit (null (v));
}
/* Return a VALUE for I. */
static VALUE *
1996-01-06 11:44:05 +00:00
int_value (int i)
1992-11-01 05:44:29 +00:00
{
VALUE *v;
v = NEW (VALUE);
v->type = integer;
v->u.i = i;
return v;
}
/* Return a VALUE for S. */
static VALUE *
1996-01-06 11:44:05 +00:00
str_value (char *s)
1992-11-01 05:44:29 +00:00
{
VALUE *v;
v = NEW (VALUE);
v->type = string;
v->u.s = xstrdup (s);
return v;
}
/* Free VALUE V, including structure components. */
static void
1996-01-06 11:44:05 +00:00
freev (VALUE *v)
1992-11-01 05:44:29 +00:00
{
if (v->type == string)
free (v->u.s);
OLD (v);
}
/* Print VALUE V. */
static void
1996-01-06 11:44:05 +00:00
printv (VALUE *v)
1992-11-01 05:44:29 +00:00
{
switch (v->type)
{
case integer:
printf ("%d\n", v->u.i);
break;
case string:
printf ("%s\n", v->u.s);
break;
default:
abort ();
1992-11-01 05:44:29 +00:00
}
}
/* Return nonzero if V is a null-string or zero-number. */
static int
1996-01-06 11:44:05 +00:00
null (VALUE *v)
1992-11-01 05:44:29 +00:00
{
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 ();
1992-11-01 05:44:29 +00:00
}
}
/* Return nonzero if V is a string value. */
static int
1996-01-06 11:44:05 +00:00
isstring (VALUE *v)
1992-11-01 05:44:29 +00:00
{
return v->type == string;
}
/* Coerce V to a string value (can't fail). */
static void
1996-01-06 11:44:05 +00:00
tostring (VALUE *v)
1992-11-01 05:44:29 +00:00
{
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 ();
1992-11-01 05:44:29 +00:00
}
}
/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
static int
1996-01-06 11:44:05 +00:00
toarith (VALUE *v)
1992-11-01 05:44:29 +00:00
{
int i;
int neg;
char *cp;
switch (v->type)
{
case integer:
return 1;
case string:
i = 0;
cp = v->u.s;
1993-12-23 00:08:23 +00:00
/* Don't interpret the empty string as an integer. */
if (*cp == 0)
return 0;
1992-11-01 05:44:29 +00:00
neg = (*cp == '-');
if (neg)
cp++;
for (; *cp; cp++)
{
if (ISDIGIT (*cp))
1992-11-01 05:44:29 +00:00
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 ();
1992-11-01 05:44:29 +00:00
}
}
/* Return nonzero if the next token matches STR exactly.
STR must not be NULL. */
static int
1996-01-06 11:44:05 +00:00
nextarg (char *str)
1992-11-01 05:44:29 +00:00
{
if (*args == NULL)
return 0;
return strcmp (*args, str) == 0;
}
/* Return nonzero if there no more tokens. */
static int
1996-01-06 11:44:05 +00:00
nomoreargs (void)
1992-11-01 05:44:29 +00:00
{
return *args == 0;
}
/* The comparison operator handling functions. */
#define cmpf(name, rel) \
static \
1992-11-01 05:44:29 +00:00
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, >)
1992-11-01 05:44:29 +00:00
#undef cmpf
/* The arithmetic operator handling functions. */
#define arithf(name, op) \
static \
1992-11-01 05:44:29 +00:00
int name (l, r) VALUE *l; VALUE *r; \
{ \
if (!toarith (l) || !toarith (r)) \
error (2, 0, _("non-numeric argument")); \
1992-11-01 05:44:29 +00:00
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-numeric argument")); \
if (r->u.i == 0) \
error (2, 0, _("division by zero")); \
return l->u.i op r->u.i; \
}
arithf (plus, +)
arithf (minus, -)
arithf (multiply, *)
arithdivf (divide, /)
arithdivf (mod, %)
1992-11-01 05:44:29 +00:00
#undef arithf
#undef arithdivf
1992-11-01 05:44:29 +00:00
#ifdef EVAL_TRACE
/* Print evaluation trace and args remaining. */
static void
1992-11-01 05:44:29 +00:00
trace (fxn)
char *fxn;
{
char **a;
printf ("%s:", fxn);
for (a = args; *a; a++)
printf (" %s", *a);
putchar ('\n');
}
#endif
/* Do the : operator.
SV is the VALUE for the lhs (the string),
PV is the VALUE for the rhs (the pattern). */
static VALUE *
1996-01-06 11:44:05 +00:00
docolon (VALUE *sv, VALUE *pv)
1992-11-01 05:44:29 +00:00
{
VALUE *v;
const char *errmsg;
struct re_pattern_buffer re_buffer;
struct re_registers re_regs;
int len;
tostring (sv);
tostring (pv);
len = strlen (pv->u.s);
memset (&re_buffer, 0, sizeof (re_buffer));
memset (&re_regs, 0, sizeof (re_regs));
1992-11-01 05:44:29 +00:00
re_buffer.allocated = 2 * len;
re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
re_buffer.translate = 0;
re_syntax_options = RE_SYNTAX_POSIX_MINIMAL_BASIC;
1992-11-01 05:44:29 +00:00
errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
if (errmsg)
error (2, 0, "%s", errmsg);
len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
if (len >= 0)
{
/* Were \(...\) used? */
if (re_buffer.re_nsub > 0)/* was (re_regs.start[1] >= 0) */
{
sv->u.s[re_regs.end[1]] = '\0';
v = str_value (sv->u.s + re_regs.start[1]);
}
else
v = int_value (len);
}
else
{
/* Match failed -- return the right kind of null. */
if (strstr (pv->u.s, "\\("))
v = str_value ("");
else
v = int_value (0);
}
free (re_buffer.buffer);
return v;
}
/* Handle bare operands and ( expr ) syntax. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval7 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *v;
#ifdef EVAL_TRACE
trace ("eval7");
#endif
if (nomoreargs ())
error (2, 0, _("syntax error"));
1993-10-12 01:52:24 +00:00
if (nextarg ("("))
1992-11-01 05:44:29 +00:00
{
args++;
v = eval ();
if (!nextarg (")"))
error (2, 0, _("syntax error"));
1992-11-01 05:44:29 +00:00
args++;
return v;
}
1993-10-12 01:52:24 +00:00
if (nextarg (")"))
error (2, 0, _("syntax error"));
1993-10-12 01:52:24 +00:00
return str_value (*args++);
1992-11-01 05:44:29 +00:00
}
/* Handle match, substr, index, and length keywords. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval6 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
VALUE *v;
VALUE *i1;
VALUE *i2;
#ifdef EVAL_TRACE
trace ("eval6");
#endif
if (nextarg ("length"))
{
args++;
r = eval6 ();
tostring (r);
v = int_value (strlen (r->u.s));
freev (r);
return v;
}
else if (nextarg ("match"))
{
args++;
l = eval6 ();
r = eval6 ();
v = docolon (l, r);
freev (l);
freev (r);
return v;
}
else if (nextarg ("index"))
{
args++;
l = eval6 ();
r = eval6 ();
tostring (l);
tostring (r);
v = int_value (strcspn (l->u.s, r->u.s) + 1);
if (v->u.i == (int) strlen (l->u.s) + 1)
1992-11-01 05:44:29 +00:00
v->u.i = 0;
freev (l);
freev (r);
return v;
}
else if (nextarg ("substr"))
{
args++;
l = eval6 ();
i1 = eval6 ();
i2 = eval6 ();
tostring (l);
if (!toarith (i1) || !toarith (i2)
|| i1->u.i > (int) strlen (l->u.s)
1992-11-01 05:44:29 +00:00
|| i1->u.i <= 0 || i2->u.i <= 0)
v = str_value ("");
else
{
v = NEW (VALUE);
v->type = string;
v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1),
l->u.s + i1->u.i - 1, i2->u.i);
v->u.s[i2->u.i] = 0;
1992-11-01 05:44:29 +00:00
}
freev (l);
freev (i1);
freev (i2);
return v;
}
else
return eval7 ();
}
/* Handle : operator (pattern matching).
Calls docolon to do the real work. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval5 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
VALUE *v;
#ifdef EVAL_TRACE
trace ("eval5");
#endif
l = eval6 ();
while (1)
{
if (nextarg (":"))
{
args++;
r = eval6 ();
v = docolon (l, r);
freev (l);
freev (r);
l = v;
}
else
return l;
}
}
/* Handle *, /, % operators. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval4 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
int (*fxn) ();
int val;
#ifdef EVAL_TRACE
trace ("eval4");
#endif
l = eval5 ();
while (1)
{
if (nextarg ("*"))
fxn = multiply;
else if (nextarg ("/"))
fxn = divide;
else if (nextarg ("%"))
fxn = mod;
else
return l;
args++;
r = eval5 ();
val = (*fxn) (l, r);
freev (l);
freev (r);
l = int_value (val);
}
}
/* Handle +, - operators. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval3 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
int (*fxn) ();
int val;
#ifdef EVAL_TRACE
trace ("eval3");
#endif
l = eval4 ();
while (1)
{
if (nextarg ("+"))
fxn = plus;
else if (nextarg ("-"))
fxn = minus;
else
return l;
args++;
r = eval4 ();
val = (*fxn) (l, r);
freev (l);
freev (r);
l = int_value (val);
}
}
/* Handle comparisons. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval2 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
int (*fxn) ();
int val;
#ifdef EVAL_TRACE
trace ("eval2");
#endif
l = eval3 ();
while (1)
{
if (nextarg ("<"))
fxn = less_than;
else if (nextarg ("<="))
fxn = less_equal;
else if (nextarg ("=") || nextarg ("=="))
fxn = equal;
else if (nextarg ("!="))
fxn = not_equal;
else if (nextarg (">="))
fxn = greater_equal;
else if (nextarg (">"))
fxn = greater_than;
else
return l;
args++;
r = eval3 ();
toarith (l);
toarith (r);
val = (*fxn) (l, r);
freev (l);
freev (r);
l = int_value (val);
}
}
/* Handle &. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval1 (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
#ifdef EVAL_TRACE
trace ("eval1");
#endif
l = eval2 ();
while (1)
{
if (nextarg ("&"))
{
args++;
r = eval2 ();
if (null (l) || null (r))
{
freev (l);
freev (r);
l = int_value (0);
}
else
freev (r);
}
else
return l;
}
}
/* Handle |. */
static VALUE *
1996-01-06 11:44:05 +00:00
eval (void)
1992-11-01 05:44:29 +00:00
{
VALUE *l;
VALUE *r;
#ifdef EVAL_TRACE
trace ("eval");
#endif
l = eval1 ();
while (1)
{
if (nextarg ("|"))
{
args++;
r = eval1 ();
if (null (l))
{
freev (l);
l = r;
}
else
freev (r);
}
else
return l;
}
}