home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume24 / chemtab / part01 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-12  |  6.2 KB  |  229 lines

  1. /*
  2.  * chemtab - a periodic table data base (C) 1990 Jim King (pulsar@lsrhs)
  3.  *
  4.  * main.c    Main program structure and branch
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <curses.h>
  11. #include <time.h>
  12. #include <signal.h>
  13.  
  14. #include "variables.h"
  15. #include "windows.h"
  16.  
  17. #define    LOCKFILE    "/tmp/chemlock"
  18.  
  19. extern int    cleanup(), stop();
  20. extern WINDOW    *hlp;
  21.  
  22. /*
  23.  * main(ac, av) - specify -r flag for ReGIS
  24.  *
  25.  * purpose:    Main program branch & menu & inits
  26.  */
  27. void main(ac, av)
  28. int    ac;
  29. char    *av[];
  30. {
  31.     long    secs;        /* For logfile */
  32.     char    c, str[80];    /* input choice */
  33.     int    i, rebuild, textonly, closetext;
  34.     struct    stat    *f1, *f2;
  35.     
  36.     f1 = (struct stat *)malloc(sizeof(struct stat));
  37.     f2 = (struct stat *)malloc(sizeof(struct stat));
  38.  
  39.     gtot = -1;
  40. #ifdef ReGIS
  41.     regis = 0;
  42.     strcpy(str, getenv("TERM"));
  43.     if ((!strcmp(str, "vt240")) || (!strncmp(str, "vt3", 3))) {
  44.         ++regis;
  45.         printf("chemtab: ReGIS terminal recognized.  ReGIS mode enabled.\n");
  46.         sleep(2);
  47.     }
  48.     if (ac > 2) {
  49.         fprintf(stderr, "chemtab: usage: chemtab [-r]\n");
  50.         exit(1);
  51.     } else if ((ac == 2) && (av[1][1] == 'r') && (!regis)) {
  52.         ++regis;
  53.         printf("chemtab: ReGIS mode enabled for this session.\n");
  54.         sleep(2);
  55.     }
  56.  
  57. #endif ReGIS
  58.  
  59.     if (stat(PERTABLE, f1) == -1) {
  60.         printf("Element data (text) is unavailable.\n");
  61.         exit(1);
  62.     }
  63. lok:    if (stat(BINTABLE, f2) == -1) {
  64.         printf("Binary element table is missing.\n");
  65.         if (fopen(LOCKFILE, "r") != NULL) {
  66.             printf("Someone is already rebuilding the element table.\nPlease wait...\n");
  67.             sleep(5);
  68.             goto lok;
  69.         } else
  70.             rebuild++;
  71.     } else if (f1->st_mtime > f2->st_mtime) {
  72.         printf("Binary element table is outdated.\n");
  73.         rebuild++;
  74.     }
  75.     
  76.     if (rebuild) {
  77.         if (creat(LOCKFILE, 022) == -1) {
  78.             printf("Sorry, cannot create a lockfile.\nProgram will try textfile only.\n");
  79.             textonly++;
  80.             goto next;
  81.         }
  82.         printf("Rebuilding binary element table.\n");
  83.         sleep(1);
  84.         if (readelem() == 888) {
  85.             printf("The computer has run out of memory.\n");
  86.             exit(1);
  87.         }
  88.         if (binwritelem() == 888) {
  89.             printf("Trouble building binary element table.\nUsing text file, please contact chemtab coordinator.\n");
  90.             textonly++;
  91.         } else
  92.             printf("Binary element table rebuilt.\n");
  93.         sleep(1);
  94.         unlink(LOCKFILE);
  95.     }
  96. next:    rebuild = 0;  closetext = 0;
  97.  
  98.     initscr();            /* start curses */
  99.     hlp = NULL;            /* help screen init */
  100.     btm = newwin(1, 79, 23, 0);    /* Bottom line window */
  101.     srt = newwin(5, 79, 0, 0);    /* Top sort window */
  102.     graph = newwin(0, 0, 0, 0);    /* Whole screen overwrite */
  103.     mn = newwin(17, 79, 5, 0);    /* Middle (main) window */
  104.     wrefresh(btm); wrefresh(srt); wrefresh(graph); wrefresh(mn);
  105.     
  106. #ifdef    TRANSCRIPT            /* Log time and user in file */
  107.     time(&secs);
  108.     bot("Logging you in..");
  109.     if ((fp = fopen(TRANSCRIPT, "a+")) != NULL) {
  110.         fprintf(fp, "%s logged in at %s", getenv("USER"), ctime(&secs));
  111.         fclose(fp);
  112.     } else {
  113.         bot("Please set the TRANSCRIPT define in tune.h to work on your machine.");
  114.         sleep(5);
  115.         bot("The program will exit so you can do this now.");
  116.         sleep(5);
  117.         cleanup();
  118.     }
  119. #endif    TRANSCRIPT
  120.  
  121.     clear();
  122.     mvaddstr(5, 37, "Chemtab");
  123.     mvaddstr(9, 28, "Periodic Table Data Base");
  124.     mvaddstr(10, 30, "Of the Known Elements");
  125.     standout();
  126.     mvaddstr(15, 21, "Chemtab 2.01 incorporates online help");
  127.     mvaddstr(16, 13, "Answer with a question mark at any question for help.");
  128.     standend();
  129.     mvaddstr(18, 18, "Version 2.01, January 7th, 1991 by Jim King");
  130.     mvaddstr(19, 16, "Developed at Lincoln-Sudbury Regional High School");
  131.     mvaddstr(20, 12, "(and) ReGIS package at Rochester Institute of Technology");
  132.     refresh();                /* Title page */
  133.     spc();
  134. nxt:    mvwaddstr(btm, 0, 0, "Would you like a transcript of graphs and information? ");
  135.     wrefresh(btm);    /* transcript flag (trans) */
  136.     c = getchar();
  137.     if (c == '?') { help(13); goto nxt; }
  138.     if (c == 'y') {
  139.         trans = 1;
  140.         wclear(btm);
  141. l14:        mvwaddstr(btm, 0, 0, "Please enter your last name for the transcript: ");
  142.         wrefresh(btm);
  143.         echo(); nocrmode();
  144.         gets(lnm);
  145.         if (lnm[0] == '?') { help(14); goto l14; }
  146.         sprintf(str, "%d.chemtab", getpid());
  147.         fp = fopen(str, "w");
  148.         fprintf(fp, "*** ChemTab - Periodic Table Database - Transcript for >> %s <<", lnm);
  149.     }
  150.     else if (c != 'n') {
  151.         mvwaddstr(btm, 0, 58, "(y, n, or ?) ");
  152.         goto nxt;
  153.     }
  154.     wprintw(btm, "no"); wrefresh(btm);
  155.     wclear(btm);
  156.     wrefresh(btm);
  157.     noecho(); crmode();
  158.     clear();
  159.     refresh();
  160.  
  161.     bot("Reading in elements...");
  162.     if (textonly) {
  163.         if (readelem() == 888) {
  164. ouch:            printf("The computer has run out of memory.\n");
  165.             exit(1);
  166.         }
  167.     } else
  168.         if (binreadelem() == 888) goto ouch;
  169.  
  170.     bot("Reading close values...");
  171.     if (getclose() == 888) {
  172.         printf("\nThe close value file is corrupt.\n");
  173.         exit(1);
  174.     }
  175.  
  176.     signal(SIGINT, cleanup);
  177. #ifdef    USERSHELL
  178.     signal(SIGTSTP, stop);
  179. #else    USERSHELL
  180.     signal(SIGTSTP, SIG_IGN);
  181. #endif    USERSHELL
  182.     signal(SIGQUIT, SIG_IGN);
  183.     signal(SIGTERM, cleanup);
  184.     
  185.     for (i = 0; i < 3; i++)            /* initialize sort choices */
  186.         dosort[i].wch = 0;
  187.  
  188.     bot(" ");                /* clear the bottom line */
  189.  
  190.     for (;;) {                /* main loop */
  191.         wclear(mn);
  192.         wrefresh(srt);
  193.         upsort();            /* Update the sort window */
  194.         for (i = 1; i < 7; i++) {
  195.             wmove(mn, i+1, 0);
  196.             wprintw(mn, "%d] %s", i, mopts[i]);
  197.         }    /* give me choices, then branch off */
  198.         menu("Chemtab 2.01 MAIN MENU");
  199. #ifdef    ReGIS
  200.         mvwaddstr(mn, 11, 0, "^R toggles ReGIS mode which is currently ");
  201.         wstandout(mn);
  202.         wprintw(mn, "%s", regis ? "on." : "off");
  203.         wstandend(mn);
  204. #endif    ReGIS
  205. loop:        mvwaddstr(mn, 9, 0, "Your choice? ");
  206.         wclrtoeol(mn);
  207.         wrefresh(mn);
  208.         cur = mn; xp = 14; yp = 9;
  209.         noecho();
  210.         crmode();
  211.         switch(wgetch(mn)) {
  212.             case '?': help(15); goto loop;
  213.             case '1': looke(); break; /* View specific Element. */
  214.             case '2': schar(); esort(); break; /* Select Characteristics */
  215.             case '3': wclear(mn); wclear(srt); vdata(); break; /* View Selected Elements */
  216.             case '4': wclear(mn); wclear(srt); ptabl(); break; /* Print periodic table.. */
  217. #ifdef ReGIS
  218.             case '5': wclear(mn); wclear(srt); if (regis) doregisgph(); else dogph(); break;
  219.             case '\022': /* ^R */ if (regis) regis = 0; else regis = 1; break;
  220. #else ReGIS
  221.             case '5': wclear(mn); wclear(srt); dogph(); break; /* Do a graph for us..... */
  222. #endif ReGIS
  223.             case '6': cleanup(); break; /* Exit */
  224.             case '\026': /* ^V */ version(); break;
  225.             default: goto loop;
  226.         }
  227.     }
  228. }
  229.