ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/calc.c
Revision: 1.3
Committed: Mon Jun 30 14:59:10 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 1.2: +3 -7 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 1.3 static const char RCSid[] = "$Id: calc.c,v 1.2 2003/06/08 12:03:09 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     #include "calcomp.h"
17    
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     main(argc, argv)
29     int argc;
30     char *argv[];
31     {
32     char expr[512];
33     FILE *fp;
34     int i;
35     register char *cp;
36    
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     for (i = 1; i < argc; i++)
45     fcompile(argv[i]);
46    
47     setjmp(env);
48     recover = 1;
49     eclock++;
50    
51     while (fgets(expr, sizeof(expr), stdin) != NULL) {
52     for (cp = expr; *cp && *cp != '\n'; cp++)
53     ;
54     *cp = '\0';
55     switch (expr[0]) {
56     case '\0':
57     continue;
58     case '?':
59     for (cp = expr+1; isspace(*cp); cp++)
60     ;
61     if (*cp)
62     dprint(cp, stdout);
63     else
64     dprint(NULL, stdout);
65     continue;
66     case '>':
67     for (cp = expr+1; isspace(*cp); cp++)
68     ;
69     if (!*cp) {
70     eputs("file name required\n");
71     continue;
72     }
73     if ((fp = fopen(cp, "w")) == NULL) {
74     eputs(cp);
75     eputs(": cannot open\n");
76     continue;
77     }
78     dprint(NULL, fp);
79     fclose(fp);
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     fcompile(cp);
89     eclock++;
90     continue;
91     }
92 schorsch 1.3 if ((cp = strchr(expr, '=')) != NULL ||
93     (cp = strchr(expr, ':')) != NULL) {
94 greg 1.1 if (cp[1])
95     scompile(expr, NULL, 0);
96     else if (*cp == '=') {
97     *cp = '\0';
98     if (!strcmp(expr, "*"))
99     dcleanup(1);
100     else
101     dclear(expr);
102     } else {
103     *cp = '\0';
104     if (!strcmp(expr, "*"))
105     dcleanup(2);
106     else
107     dremove(expr);
108     }
109     eclock++;
110     } else {
111     printf("$%d=%.9g\n", nres+1,
112     result[nres%MAXRES] = eval(expr));
113     nres++;
114     }
115     }
116    
117     recover = 0;
118     quit(0);
119     }
120    
121    
122     double
123     chanvalue(n) /* return channel value */
124     int n;
125     {
126     if (n == 0)
127     n = nres;
128     else if (n > nres || nres-n >= MAXRES) {
129     fprintf(stderr, "$%d: illegal result\n", n);
130     return(0.0);
131     }
132     return(result[(n-1)%MAXRES]);
133     }
134    
135    
136     void
137     eputs(msg)
138     char *msg;
139     {
140     fputs(msg, stderr);
141     }
142    
143    
144     void
145     wputs(msg)
146     char *msg;
147     {
148     eputs(msg);
149     }
150    
151    
152     void
153     quit(code)
154     int code;
155     {
156     if (recover) /* a cavalier approach */
157     longjmp(env, 1);
158     exit(code);
159     }