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