1 /* expr -- evaluate expressions.
2 Copyright (C) 86, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Author: Mike Parker.
20 This program evaluates expressions. Each token (operator, operand,
21 parenthesis) of the expression must be a seperate argument. The
22 parser used is a reasonably general one, though any incarnation of
23 it is language-specific. It is especially nice for expressions.
25 No parse tree is needed; a new node is evaluated immediately.
26 One function can handle multiple operators all of equal precedence,
27 provided they all associate ((x op x) op x).
29 Define EVAL_TRACE to print an evaluation trace. */
33 #include <sys/types.h>
38 #include "long-options.h"
41 #define NEW(type) ((type *) xmalloc (sizeof (type)))
42 #define OLD(x) free ((char *) x)
44 /* The kinds of value we can have. */
50 typedef enum valtype TYPE
;
55 TYPE type
; /* Which kind. */
57 { /* The value itself. */
62 typedef struct valinfo VALUE
;
64 /* The arguments given to the program, minus the program name. */
67 /* The name this program was run with. */
74 static VALUE
*docolon ();
75 static VALUE
*eval ();
76 static VALUE
*int_value ();
77 static VALUE
*str_value ();
78 static int isstring ();
79 static int nextarg ();
80 static int nomoreargs ();
82 static int toarith ();
84 static void printv ();
85 static void tostring ();
96 fprintf (stderr
, "Try `%s --help' for more information.\n",
101 Usage: %s EXPRESSION\n\
104 program_name
, program_name
);
107 --help display this help and exit\n\
108 --version output version information and exit\n\
112 Print the value of EXPRESSION to standard output. A blank line below\n\
113 separates increasing precedence groups. EXPRESSION may be:\n\
115 ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n\
117 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n\
119 ARG1 < ARG2 ARG1 is less than ARG2\n\
120 ARG1 <= ARG2 ARG1 is less than or equal to ARG2\n\
121 ARG1 = ARG2 ARG1 is equal to ARG2\n\
122 ARG1 != ARG2 ARG1 is unequal to ARG2\n\
123 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2\n\
124 ARG1 > ARG2 ARG1 is greater than ARG2\n\
126 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2\n\
127 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2\n\
129 ARG1 * ARG2 arithmetic product of ARG1 and ARG2\n\
130 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2\n\
131 ARG1 %% ARG2 arithmetic remainder of ARG1 divided by ARG2\n\
133 STRING : REGEXP anchored pattern match of REGEXP in STRING\n\
135 match STRING REGEXP same as STRING : REGEXP\n\
136 substr STRING POS LENGTH substring of STRING, POS counted from 1\n\
137 index STRING CHARS index in STRING where any CHARS is found, or 0\n\
138 length STRING length of STRING\n\
140 ( EXPRESSION ) value of EXPRESSION\n\
144 Beware that many operators need to be escaped or quoted for shells.\n\
145 Comparisons are arithmetic if both ARGs are numbers, else lexicographical.\n\
146 Pattern matches return the string matched between \\( and \\) or null; if\n\
147 \\( and \\) are not used, they return the number of characters matched or 0.\n\
160 program_name
= argv
[0];
162 parse_long_options (argc
, argv
, "expr", version_string
, usage
);
166 error (0, 0, "too few arguments");
174 error (2, 0, "syntax error");
180 /* Return a VALUE for I. */
194 /* Return a VALUE for S. */
204 v
->u
.s
= xstrdup (s
);
208 /* Free VALUE V, including structure components. */
214 if (v
->type
== string
)
228 printf ("%d\n", v
->u
.i
);
231 printf ("%s\n", v
->u
.s
);
238 /* Return nonzero if V is a null-string or zero-number. */
249 return v
->u
.s
[0] == '\0' || strcmp(v
->u
.s
, "0") == 0;
255 /* Return nonzero if V is a string value. */
261 return v
->type
== string
;
264 /* Coerce V to a string value (can't fail). */
275 temp
= xmalloc (4 * (sizeof (int) / sizeof (char)));
276 sprintf (temp
, "%d", v
->u
.i
);
287 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
304 /* Don't interpret the empty string as an integer. */
313 i
= i
* 10 + *cp
- '0';
318 v
->u
.i
= i
* (neg
? -1 : 1);
326 /* Return nonzero if the next token matches STR exactly.
327 STR must not be NULL. */
335 return strcmp (*args
, str
) == 0;
338 /* Return nonzero if there no more tokens. */
346 /* The comparison operator handling functions. */
348 #define cmpf(name, rel) \
350 int name (l, r) VALUE *l; VALUE *r; \
352 if (isstring (l) || isstring (r)) \
356 return strcmp (l->u.s, r->u.s) rel 0; \
359 return l->u.i rel r->u.i; \
362 cmpf (less_equal
, <=)
365 cmpf (greater_equal
, >=)
366 cmpf (greater_than
, >)
370 /* The arithmetic operator handling functions. */
372 #define arithf(name, op) \
374 int name (l, r) VALUE *l; VALUE *r; \
376 if (!toarith (l) || !toarith (r)) \
377 error (2, 0, "non-numeric argument"); \
378 return l->u.i op r->u.i; \
381 #define arithdivf(name, op) \
382 int name (l, r) VALUE *l; VALUE *r; \
384 if (!toarith (l) || !toarith (r)) \
385 error (2, 0, "non-numeric argument"); \
387 error (2, 0, "division by zero"); \
388 return l->u.i op r->u.i; \
394 arithdivf (divide
, /)
401 /* Print evaluation trace and args remaining. */
410 for (a
= args
; *a
; a
++)
416 /* Do the : operator.
417 SV is the VALUE for the lhs (the string),
418 PV is the VALUE for the rhs (the pattern). */
427 struct re_pattern_buffer re_buffer
;
428 struct re_registers re_regs
;
434 len
= strlen (pv
->u
.s
);
435 memset (&re_buffer
, 0, sizeof (re_buffer
));
436 memset (&re_regs
, 0, sizeof (re_regs
));
437 re_buffer
.allocated
= 2 * len
;
438 re_buffer
.buffer
= (unsigned char *) xmalloc (re_buffer
.allocated
);
439 re_buffer
.translate
= 0;
440 errmsg
= re_compile_pattern (pv
->u
.s
, len
, &re_buffer
);
442 error (2, 0, "%s", errmsg
);
444 len
= re_match (&re_buffer
, sv
->u
.s
, strlen (sv
->u
.s
), 0, &re_regs
);
447 /* Were \(...\) used? */
448 if (re_buffer
.re_nsub
> 0)/* was (re_regs.start[1] >= 0) */
450 sv
->u
.s
[re_regs
.end
[1]] = '\0';
451 v
= str_value (sv
->u
.s
+ re_regs
.start
[1]);
458 /* Match failed -- return the right kind of null. */
459 if (strstr (pv
->u
.s
, "\\("))
464 free (re_buffer
.buffer
);
468 /* Handle bare operands and ( expr ) syntax. */
479 error (2, 0, "syntax error");
486 error (2, 0, "syntax error");
492 error (2, 0, "syntax error");
494 return str_value (*args
++);
497 /* Handle match, substr, index, and length keywords. */
511 if (nextarg ("length"))
516 v
= int_value (strlen (r
->u
.s
));
520 else if (nextarg ("match"))
530 else if (nextarg ("index"))
537 v
= int_value (strcspn (l
->u
.s
, r
->u
.s
) + 1);
538 if (v
->u
.i
== strlen (l
->u
.s
) + 1)
544 else if (nextarg ("substr"))
551 if (!toarith (i1
) || !toarith (i2
)
552 || i1
->u
.i
> strlen (l
->u
.s
)
553 || i1
->u
.i
<= 0 || i2
->u
.i
<= 0)
559 v
->u
.s
= strncpy ((char *) xmalloc (i2
->u
.i
+ 1),
560 l
->u
.s
+ i1
->u
.i
- 1, i2
->u
.i
);
572 /* Handle : operator (pattern matching).
573 Calls docolon to do the real work. */
602 /* Handle *, /, % operators. */
620 else if (nextarg ("/"))
622 else if (nextarg ("%"))
635 /* Handle +, - operators. */
653 else if (nextarg ("-"))
666 /* Handle comparisons. */
684 else if (nextarg ("<="))
686 else if (nextarg ("=") || nextarg ("=="))
688 else if (nextarg ("!="))
690 else if (nextarg (">="))
692 else if (nextarg (">"))
725 if (null (l
) || null (r
))