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