ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/calc.c
Revision: 1.1
Committed: Sat Feb 22 02:07:20 2003 UTC (21 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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