home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume26 / calc / part02 / calc.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  5KB  |  224 lines

  1. /*
  2.  * Copyright (c) 1992 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Arbitrary precision calculator.
  7.  */
  8.  
  9. #include <signal.h>
  10. #include <pwd.h>
  11. #include <sys/types.h>
  12.  
  13. #include "calc.h"
  14. #include "func.h"
  15. #include "opcodes.h"
  16. #include "config.h"
  17. #include "token.h"
  18. #include "symbol.h"
  19.  
  20. /*
  21.  * Common definitions
  22.  */
  23. long maxprint;        /* number of elements to print */
  24. int abortlevel;        /* current level of aborts */
  25. BOOL inputwait;        /* TRUE if in a terminal input wait */
  26. jmp_buf jmpbuf;        /* for errors */
  27.  
  28. static int q_flag = FALSE;    /* TRUE => don't execute rc files */
  29.  
  30. char *calcpath;        /* $CALCPATH or default */
  31. char *calcrc;        /* $CALCRC or default */
  32. char *home;        /* $HOME or default */
  33. static char *pager;    /* $PAGER or default */
  34. char *shell;        /* $SHELL or default */
  35.  
  36. static void intint();    /* interrupt routine */
  37. void givehelp();
  38. static void initenv();    /* initialize/default special environment vars */
  39.  
  40. extern struct passwd *getpwuid();
  41. extern char *getenv();
  42. extern uid_t geteuid();
  43.  
  44. /*
  45.  * Top level calculator routine.
  46.  */
  47. main(argc, argv)
  48.     char **argv;
  49. {
  50.     char *str;        /* current option string or expression */
  51.     char cmdbuf[MAXCMD+1];    /* command line expression */
  52.  
  53.     initenv();
  54.     argc--;
  55.     argv++;
  56.     while ((argc > 0) && (**argv == '-')) {
  57.         for (str = &argv[0][1]; *str; str++) switch (*str) {
  58.             case 'h':
  59.                 givehelp(DEFAULTCALCHELP);
  60.                 exit(0);
  61.                 break;
  62.             case 'q':
  63.                 q_flag = TRUE;
  64.                 break;
  65.             default:
  66.                 printf("Unknown option\n");
  67.                 exit(1);
  68.         }
  69.         argc--;
  70.         argv++;
  71.     }
  72.     str = cmdbuf;
  73.     *str = '\0';
  74.     while (--argc >= 0) {
  75.         *str++ = ' ';
  76.         strcpy(str, *argv++);
  77.         str += strlen(str);
  78.         str[0] = '\n';
  79.         str[1] = '\0';
  80.     }
  81.     str = cmdbuf;
  82.     if (*str == '\0') {
  83.         str = NULL;
  84.         printf("C-style arbitrary precision calculator.\n");
  85.         version(stdout);
  86.         printf("[Type \"exit\" to exit, or \"help\" for help.]\n\n");
  87.     }
  88.     if (setjmp(jmpbuf) == 0) {
  89.         initmasks();
  90.         inittokens();
  91.         initglobals();
  92.         initfunctions();
  93.         initstack();
  94.         resetinput();
  95.         cleardiversions();
  96.         setfp(stdout);
  97.         setmode(MODE_INITIAL);
  98.         setdigits(DISPLAY_DEFAULT);
  99.         maxprint = MAXPRINT_DEFAULT;
  100.         _epsilon_ = atoq(EPSILON_DEFAULT);
  101.         _epsilonprec_ = qprecision(_epsilon_);
  102.         if (str) {
  103.             if (q_flag == FALSE) {
  104.                 runrcfiles();
  105.                 q_flag = TRUE;
  106.             }
  107.             (void) openstring(str);
  108.             getcommands();
  109.             exit(0);
  110.         }
  111.     }
  112.     if (str)
  113.         exit(1);
  114.     abortlevel = 0;
  115.     _math_abort_ = FALSE;
  116.     inputwait = FALSE;
  117.     (void) signal(SIGINT, intint);
  118.     cleardiversions();
  119.     setfp(stdout);
  120.     resetinput();
  121.     if (q_flag == FALSE) {
  122.         runrcfiles();
  123.         q_flag = TRUE;
  124.     }
  125.     (void) openterminal();
  126.     getcommands();
  127.     exit(0);
  128.     /*NOTREACHED*/
  129. }
  130.  
  131.  
  132. /*
  133.  * initenv - obtain $CALCPATH, $CALCRC, $HOME, $PAGER and $SHELL values
  134.  *
  135.  * If $CALCPATH, $CALCRC, $PAGER or $SHELL do not exist, use the default
  136.  * values.  If $PAGER or $SHELL is an empty string, also use a default value.
  137.  * If $HOME does not exist, or is empty, use the home directory
  138.  * information from the password file.
  139.  */
  140. static void
  141. initenv()
  142. {
  143.     struct passwd *ent;        /* our password entry */
  144.  
  145.     /* determine the $CALCPATH value */
  146.     calcpath = getenv(CALCPATH);
  147.     if (calcpath == NULL)
  148.         calcpath = DEFAULTCALCPATH;
  149.  
  150.     /* determine the $CALCRC value */
  151.     calcrc = getenv(CALCRC);
  152.     if (calcrc == NULL) {
  153.         calcrc = DEFAULTCALCRC;
  154.     }
  155.     
  156.     /* determine the $HOME value */
  157.     home = getenv(HOME);
  158.     if (home == NULL || home[0] == '\0') {
  159.         ent = getpwuid(geteuid());
  160.         if (ent == NULL) {
  161.             /* just assume . is home if all else fails */
  162.             home = ".";
  163.         }
  164.         home = (char *)malloc(strlen(ent->pw_dir)+1);
  165.         strcpy(home, ent->pw_dir);
  166.     }
  167.  
  168.     /* determine the $PAGER value */
  169.     pager = getenv(PAGER);
  170.     if (pager == NULL || *pager == '\0') {
  171.         pager = DEFAULTCALCPAGER;
  172.     }
  173.  
  174.     /* determine the $SHELL value */
  175.     shell = getenv(SHELL);
  176.     if (shell == NULL)
  177.         shell = DEFAULTSHELL;
  178. }
  179.  
  180. void
  181. givehelp(type)
  182.     char *type;        /* the type of help to give, NULL => index */
  183. {
  184.     char *helpcmd;        /* what to execute to print help */
  185.  
  186.     /* catch the case where we just print the index */
  187.     if (type == NULL) {
  188.         type = DEFAULTCALCHELP;        /* the help index file */
  189.     }
  190.  
  191.     /* form the help command name */
  192.     helpcmd = (char *)malloc(
  193.         sizeof("if [ ! -d \"")+sizeof(HELPDIR)+1+strlen(type)+
  194.         sizeof("\" ];then ")+
  195.         strlen(pager)+1+1+sizeof(HELPDIR)+1+strlen(type)+1+1+
  196.         sizeof(";else echo no such help;fi"));
  197.     sprintf(helpcmd, 
  198.         "if [ -r \"%s/%s\" ];then %s \"%s/%s\";else echo no such help;fi", 
  199.         HELPDIR, type, pager, HELPDIR, type);
  200.  
  201.     /* execute the help command */
  202.     system(helpcmd);
  203.     free(helpcmd);
  204. }
  205.  
  206.  
  207. /*
  208.  * Interrupt routine.
  209.  */
  210. /*ARGSUSED*/
  211. static void
  212. intint(arg)
  213.     int arg;    /* to keep ANSI C happy */
  214. {
  215.     (void) signal(SIGINT, intint);
  216.     if (inputwait || (++abortlevel >= ABORT_NOW))
  217.         error("\nABORT");
  218.     if (abortlevel >= ABORT_MATH)
  219.         _math_abort_ = TRUE;
  220.     printf("\n[Abort level %d]\n", abortlevel);
  221. }
  222.  
  223. /* END CODE */
  224.