ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/calc.c
Revision: 1.11
Committed: Sat Jun 7 05:09:45 2025 UTC (32 hours, 45 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +2 -1 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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