ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/calc.c
Revision: 1.6
Committed: Tue Dec 9 15:55:46 2003 UTC (20 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R7P2, rad3R7P1, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9
Changes since 1.5: +13 -6 lines
Log Message:
Added ability for calc to read on a continued line

File Contents

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