File: calc.c

package info (click to toggle)
xymon 4.3.30-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,384 kB
  • sloc: ansic: 69,137; sh: 3,601; makefile: 863; javascript: 452; perl: 48
file content (159 lines) | stat: -rw-r--r-- 4,624 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* Copyright (C) 2003-2011 Henrik Storner <henrik@hswn.dk>                    */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid[] = "$Id: calc.c 6712 2011-07-31 21:01:52Z storner $";

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>

#include "libxymon.h"

long compute(char *expression, int *error)
{
	/*
	 * This routine evaluates an expression.
	 *
	 * Expressions are of the form "expr [operator expr]" or "(expr)"
	 * "operator" is + - / * % & | && || > >= < <= ==
	 *
	 * All operators have equal precedence!
	 *
	 */
	char *exp, *startp, *operator;
	char *inp, *outp;
	char op;
	long xval, yval, result;

	if (*error) return -1;

	/* Copy expression except whitespace */
	exp = (char *) malloc(strlen(expression)+1);
	inp = expression; outp=exp;
	do {
		if (!isspace((int) *inp)) { *outp = *inp; outp++; }
		inp++;
	} while (*inp);
	*outp = '\0';

	/* First find the value of the first sub-expression */
	startp = exp;
	while (isspace((int) *startp)) startp++;
	if (*startp == '(') {
		/* Starts with parentheses:
		 * - find matching end parentheses
		 * - find the operator following the end parentheses
		 * - compute value of expression inside parentheses (recursive call)
		 */
		int pcount = 1;
		char *endp;

		for (endp = startp+1; (*endp && pcount); ) {
			if (*endp == '(') pcount++;
			else if (*endp == ')') pcount--;
			if (pcount) endp++;
		}

		if (*endp == '\0') { *error = 1; return -1; }
		operator = endp+1;
		*endp = '\0';
		xval = compute(startp+1, error);
	}
	else {
		/* No parentheses --> it's a number */
		xval = strtol(startp, &operator, 10);
		if (operator == startp) { *error = 2; return -1; }
	}

	/* Now loop over the following operators and expressions */
	do {
		/* There may not be an operator */
		if (*operator) {

			while (isspace((int) *operator)) operator++;
			op = *operator;
			/* For the && and || operators ... */
			if      ((op == '&') && (*(operator+1) == '&')) { op = 'a'; operator++; }
			else if ((op == '|') && (*(operator+1) == '|')) { op = 'o'; operator++; }
			else if ((op == '>') && (*(operator+1) == '=')) { op = 'g'; operator++; }
			else if ((op == '<') && (*(operator+1) == '=')) { op = 'l'; operator++; }
			else if ((op == '=') && (*(operator+1) == '=')) { op = 'e'; operator++; }

			/* Since there is an operator, there must be a value after the operator */
			startp = operator + 1;
			while (isspace((int) *startp)) startp++;
			if (*startp == '(') {
				int pcount = 1;
				char *endp;

				for (endp = startp+1; (*endp && pcount);) {
					if (*endp == '(') pcount++;
					else if (*endp == ')') pcount--;
					if (pcount) endp++;
				}

				operator = endp+1;
				*endp = '\0';
				yval = compute(startp+1, error);
			}
			else {
				yval = strtol(startp, &operator, 10);
				if (operator == startp) { *error = 3; return -1; }
			}

			switch (op) {
			  case '+': xval = (xval + yval);  break;
			  case '-': xval = (xval - yval);  break;
			  case '*': xval = (xval * yval);  break;
			  case '/': if (yval) xval = (xval / yval); 
				    else { *error = 10; return -1; }
				    break;
			  case '%': if (yval) xval = (xval % yval); 
				    else { *error = 10; return -1; }
				    break;
			  case '&': xval = (xval & yval);  break;
			  case 'a': xval = (xval && yval); break;
			  case '|': xval = (xval | yval);  break;
			  case 'o': xval = (xval || yval); break;
			  case '>': xval = (xval > yval);  break;
			  case 'g': xval = (xval >= yval); break;
			  case '<': xval = (xval < yval);  break;
			  case 'l': xval = (xval <= yval); break;
			  case 'e': xval = (xval == yval); break;
			  default : { *error = 4; return -1; }
			}
		}
		else {
			/* Do nothing - no operator, so result is the xval */
		}

		result = xval;
	} while (*operator);

	xfree(exp);
	return result;
}

#ifdef STANDALONE

int main(int argc, char *argv[])
{
	long result;
	int error = 0;

	result = compute(argv[1], &error);
	printf("%s = %ld\n", argv[1], result);

	return error;
}
#endif