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