Back to index

radiance  4R0+20100331
Defines | Functions | Variables
calc.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <ctype.h>
#include "rterror.h"
#include "calcomp.h"

Go to the source code of this file.

Defines

#define MAXRES   100

Functions

int main (int argc, argv)
double chanvalue (int n)
void eputs (char *msg)
void wputs (char *msg)
void quit (int code)

Variables

static const char RCSid [] = "$Id: calc.c,v 1.6 2003/12/09 15:55:46 greg Exp $"
double result [MAXRES]
int nres = 0
jmp_buf env
int recover = 0

Define Documentation

#define MAXRES   100

Definition at line 19 of file calc.c.


Function Documentation

double chanvalue ( int  n)

Definition at line 132 of file calc.c.

{
       if (n == 0)
              n = nres;
       else if (n > nres || nres-n >= MAXRES) {
              fprintf(stderr, "$%d: illegal result\n", n);
              return(0.0);
       }
       return(result[(n-1)%MAXRES]);
}

Here is the caller graph for this function:

void eputs ( char *  msg)

Definition at line 146 of file calc.c.

{
       fputs(msg, stderr);
}
int main ( int  argc,
argv   
)

Definition at line 29 of file calc.c.

{
       char  expr[2048];
       char  *epos;
       FILE  *fp;
       int  i;
       register char  *cp;

       esupport |= E_VARIABLE|E_INCHAN|E_FUNCTION;
       esupport &= ~(E_REDEFW|E_RCONST|E_OUTCHAN);
#ifdef  BIGGERLIB
       biggerlib();
#endif
       varset("PI", ':', 3.14159265358979323846);

       for (i = 1; i < argc; i++)
              fcompile(argv[i]);

       setjmp(env);
       recover = 1;
       eclock++;

       epos = expr;
       while (fgets(epos, sizeof(expr)-(epos-expr), stdin) != NULL) {
              while (*epos && *epos != '\n')
                     epos++;
              if (*epos && epos > expr && epos[-1] == '\\') {
                     epos[-1] = ' ';
                     continue;            /* escaped newline */
              }
              *epos = '\0';
              epos = expr;
              switch (expr[0]) {
              case '\0':
                     continue;
              case '?':
                     for (cp = expr+1; isspace(*cp); cp++)
                            ;
                     if (*cp)
                            dprint(cp, stdout);
                     else
                            dprint(NULL, stdout);
                     continue;
              case '>':
                     for (cp = expr+1; isspace(*cp); cp++)
                            ;
                     if (!*cp) {
                            eputs("file name required\n");
                            continue;
                     }
                     if ((fp = fopen(cp, "w")) == NULL) {
                            eputs(cp);
                            eputs(": cannot open\n");
                            continue;
                     }
                     dprint(NULL, fp);
                     fclose(fp);
                     continue;
              case '<':
                     for (cp = expr+1; isspace(*cp); cp++)
                            ;
                     if (!*cp) {
                            eputs("file name required\n");
                            continue;
                     }
                     fcompile(cp);
                     eclock++;
                     continue;
              }
              if ((cp = strchr(expr, '=')) != NULL ||
                            (cp = strchr(expr, ':')) != NULL) {
                     if (cp[1])
                            scompile(expr, NULL, 0);
                     else if (*cp == '=') {
                            *cp = '\0';
                            if (!strcmp(expr, "*"))
                                   dcleanup(1);
                            else
                                   dclear(expr);
                     } else {
                            *cp = '\0';
                            if (!strcmp(expr, "*"))
                                   dcleanup(2);
                            else
                                   dremove(expr);
                     }
                     eclock++;
              } else {
                     printf("$%d=%.9g\n", nres+1,
                                   result[nres%MAXRES] = eval(expr));
                     nres++;
              }
       }

       recover = 0;
       quit(0);
       return 0; /* pro forma exit */
}

Here is the call graph for this function:

void quit ( int  code)

Definition at line 162 of file calc.c.

{
       if (recover)                /* a cavalier approach */
              longjmp(env, 1);
       exit(code);
}
void wputs ( char *  msg)

Definition at line 154 of file calc.c.

{
       eputs(msg);
}

Here is the caller graph for this function:


Variable Documentation

jmp_buf env

Definition at line 24 of file calc.c.

int nres = 0

Definition at line 22 of file calc.c.

const char RCSid[] = "$Id: calc.c,v 1.6 2003/12/09 15:55:46 greg Exp $" [static]

Definition at line 2 of file calc.c.

static int recover = 0

Definition at line 25 of file calc.c.

double result[MAXRES]

Definition at line 21 of file calc.c.