1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: calc.c,v 1.8 2013/12/19 16:56:31 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* calc.c - simple algebraic desk calculator program. |
6 |
* |
7 |
* 4/1/86 |
8 |
*/ |
9 |
|
10 |
#include <stdlib.h> |
11 |
#include <setjmp.h> |
12 |
#include <ctype.h> |
13 |
|
14 |
#include "rtio.h" |
15 |
#include "rterror.h" |
16 |
#include "calcomp.h" |
17 |
|
18 |
#define MAXRES 100 |
19 |
|
20 |
double result[MAXRES]; |
21 |
int nres = 0; |
22 |
|
23 |
jmp_buf env; |
24 |
int recover = 0; |
25 |
|
26 |
|
27 |
int |
28 |
main(int argc, char *argv[]) |
29 |
{ |
30 |
char expr[2048]; |
31 |
char *epos; |
32 |
FILE *fp; |
33 |
int i; |
34 |
char *cp; |
35 |
|
36 |
esupport |= E_VARIABLE|E_INCHAN|E_FUNCTION; |
37 |
esupport &= ~(E_REDEFW|E_RCONST|E_OUTCHAN); |
38 |
#ifdef BIGGERLIB |
39 |
biggerlib(); |
40 |
#endif |
41 |
varset("PI", ':', 3.14159265358979323846); |
42 |
|
43 |
for (i = 1; i < argc; i++) { |
44 |
cp = getpath(argv[i], getrlibpath(), 0); |
45 |
if (cp == NULL) { |
46 |
eputs(argv[0]); |
47 |
eputs(": cannot find file '"); |
48 |
eputs(argv[i]); |
49 |
eputs("'\n"); |
50 |
quit(1); |
51 |
} |
52 |
fcompile(cp); |
53 |
} |
54 |
setjmp(env); |
55 |
recover = 1; |
56 |
eclock++; |
57 |
|
58 |
epos = expr; |
59 |
while (fgets(epos, sizeof(expr)-(epos-expr), stdin) != NULL) { |
60 |
while (*epos && *epos != '\n') |
61 |
epos++; |
62 |
if (*epos && epos > expr && epos[-1] == '\\') { |
63 |
epos[-1] = ' '; |
64 |
continue; /* escaped newline */ |
65 |
} |
66 |
*epos = '\0'; |
67 |
epos = expr; |
68 |
switch (expr[0]) { |
69 |
case '\0': |
70 |
continue; |
71 |
case '?': |
72 |
for (cp = expr+1; isspace(*cp); cp++) |
73 |
; |
74 |
if (*cp) |
75 |
dprint(cp, stdout); |
76 |
else |
77 |
dprint(NULL, stdout); |
78 |
continue; |
79 |
case '>': |
80 |
for (cp = expr+1; isspace(*cp); cp++) |
81 |
; |
82 |
if (!*cp) { |
83 |
eputs("file name required\n"); |
84 |
continue; |
85 |
} |
86 |
if ((fp = fopen(cp, "w")) == NULL) { |
87 |
eputs(cp); |
88 |
eputs(": cannot open\n"); |
89 |
continue; |
90 |
} |
91 |
dprint(NULL, fp); |
92 |
fclose(fp); |
93 |
continue; |
94 |
case '<': |
95 |
for (cp = expr+1; isspace(*cp); cp++) |
96 |
; |
97 |
if (!*cp) { |
98 |
eputs("file name required\n"); |
99 |
continue; |
100 |
} |
101 |
cp = getpath(cp, getrlibpath(), 0); |
102 |
if (cp == NULL) { |
103 |
eputs("cannot find file\n"); |
104 |
continue; |
105 |
} |
106 |
fcompile(cp); |
107 |
eclock++; |
108 |
continue; |
109 |
} |
110 |
if ((cp = strchr(expr, '=')) != NULL || |
111 |
(cp = strchr(expr, ':')) != NULL) { |
112 |
if (cp[1]) |
113 |
scompile(expr, NULL, 0); |
114 |
else if (*cp == '=') { |
115 |
*cp = '\0'; |
116 |
if (!strcmp(expr, "*")) |
117 |
dcleanup(1); |
118 |
else |
119 |
dclear(expr); |
120 |
} else { |
121 |
*cp = '\0'; |
122 |
if (!strcmp(expr, "*")) |
123 |
dcleanup(2); |
124 |
else |
125 |
dremove(expr); |
126 |
} |
127 |
eclock++; |
128 |
} else { |
129 |
printf("$%d=%.9g\n", nres+1, |
130 |
result[nres%MAXRES] = eval(expr)); |
131 |
nres++; |
132 |
} |
133 |
} |
134 |
|
135 |
recover = 0; |
136 |
quit(0); |
137 |
return 0; /* pro forma exit */ |
138 |
} |
139 |
|
140 |
|
141 |
double |
142 |
chanvalue(n) /* return channel value */ |
143 |
int n; |
144 |
{ |
145 |
if (n == 0) |
146 |
n = nres; |
147 |
else if (n > nres || nres-n >= MAXRES) { |
148 |
fprintf(stderr, "$%d: illegal result\n", n); |
149 |
return(0.0); |
150 |
} |
151 |
return(result[(n-1)%MAXRES]); |
152 |
} |
153 |
|
154 |
|
155 |
void |
156 |
eputs(const char *msg) |
157 |
{ |
158 |
fputs(msg, stderr); |
159 |
} |
160 |
|
161 |
|
162 |
void |
163 |
wputs(const char *msg) |
164 |
{ |
165 |
eputs(msg); |
166 |
} |
167 |
|
168 |
|
169 |
void |
170 |
quit(int code) |
171 |
{ |
172 |
if (recover) /* a cavalier approach */ |
173 |
longjmp(env, 1); |
174 |
exit(code); |
175 |
} |