home *** CD-ROM | disk | FTP | other *** search
- /*
- * chemtab - a periodic table data base (C) 1990 Jim King (pulsar@lsrhs)
- *
- * main.c Main program structure and branch
- */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <curses.h>
- #include <time.h>
- #include <signal.h>
-
- #include "variables.h"
- #include "windows.h"
-
- #define LOCKFILE "/tmp/chemlock"
-
- extern int cleanup(), stop();
- extern WINDOW *hlp;
-
- /*
- * main(ac, av) - specify -r flag for ReGIS
- *
- * purpose: Main program branch & menu & inits
- */
- void main(ac, av)
- int ac;
- char *av[];
- {
- long secs; /* For logfile */
- char c, str[80]; /* input choice */
- int i, rebuild, textonly, closetext;
- struct stat *f1, *f2;
-
- f1 = (struct stat *)malloc(sizeof(struct stat));
- f2 = (struct stat *)malloc(sizeof(struct stat));
-
- gtot = -1;
- #ifdef ReGIS
- regis = 0;
- strcpy(str, getenv("TERM"));
- if ((!strcmp(str, "vt240")) || (!strncmp(str, "vt3", 3))) {
- ++regis;
- printf("chemtab: ReGIS terminal recognized. ReGIS mode enabled.\n");
- sleep(2);
- }
- if (ac > 2) {
- fprintf(stderr, "chemtab: usage: chemtab [-r]\n");
- exit(1);
- } else if ((ac == 2) && (av[1][1] == 'r') && (!regis)) {
- ++regis;
- printf("chemtab: ReGIS mode enabled for this session.\n");
- sleep(2);
- }
-
- #endif ReGIS
-
- if (stat(PERTABLE, f1) == -1) {
- printf("Element data (text) is unavailable.\n");
- exit(1);
- }
- lok: if (stat(BINTABLE, f2) == -1) {
- printf("Binary element table is missing.\n");
- if (fopen(LOCKFILE, "r") != NULL) {
- printf("Someone is already rebuilding the element table.\nPlease wait...\n");
- sleep(5);
- goto lok;
- } else
- rebuild++;
- } else if (f1->st_mtime > f2->st_mtime) {
- printf("Binary element table is outdated.\n");
- rebuild++;
- }
-
- if (rebuild) {
- if (creat(LOCKFILE, 022) == -1) {
- printf("Sorry, cannot create a lockfile.\nProgram will try textfile only.\n");
- textonly++;
- goto next;
- }
- printf("Rebuilding binary element table.\n");
- sleep(1);
- if (readelem() == 888) {
- printf("The computer has run out of memory.\n");
- exit(1);
- }
- if (binwritelem() == 888) {
- printf("Trouble building binary element table.\nUsing text file, please contact chemtab coordinator.\n");
- textonly++;
- } else
- printf("Binary element table rebuilt.\n");
- sleep(1);
- unlink(LOCKFILE);
- }
- next: rebuild = 0; closetext = 0;
-
- initscr(); /* start curses */
- hlp = NULL; /* help screen init */
- btm = newwin(1, 79, 23, 0); /* Bottom line window */
- srt = newwin(5, 79, 0, 0); /* Top sort window */
- graph = newwin(0, 0, 0, 0); /* Whole screen overwrite */
- mn = newwin(17, 79, 5, 0); /* Middle (main) window */
- wrefresh(btm); wrefresh(srt); wrefresh(graph); wrefresh(mn);
-
- #ifdef TRANSCRIPT /* Log time and user in file */
- time(&secs);
- bot("Logging you in..");
- if ((fp = fopen(TRANSCRIPT, "a+")) != NULL) {
- fprintf(fp, "%s logged in at %s", getenv("USER"), ctime(&secs));
- fclose(fp);
- } else {
- bot("Please set the TRANSCRIPT define in tune.h to work on your machine.");
- sleep(5);
- bot("The program will exit so you can do this now.");
- sleep(5);
- cleanup();
- }
- #endif TRANSCRIPT
-
- clear();
- mvaddstr(5, 37, "Chemtab");
- mvaddstr(9, 28, "Periodic Table Data Base");
- mvaddstr(10, 30, "Of the Known Elements");
- standout();
- mvaddstr(15, 21, "Chemtab 2.01 incorporates online help");
- mvaddstr(16, 13, "Answer with a question mark at any question for help.");
- standend();
- mvaddstr(18, 18, "Version 2.01, January 7th, 1991 by Jim King");
- mvaddstr(19, 16, "Developed at Lincoln-Sudbury Regional High School");
- mvaddstr(20, 12, "(and) ReGIS package at Rochester Institute of Technology");
- refresh(); /* Title page */
- spc();
- nxt: mvwaddstr(btm, 0, 0, "Would you like a transcript of graphs and information? ");
- wrefresh(btm); /* transcript flag (trans) */
- c = getchar();
- if (c == '?') { help(13); goto nxt; }
- if (c == 'y') {
- trans = 1;
- wclear(btm);
- l14: mvwaddstr(btm, 0, 0, "Please enter your last name for the transcript: ");
- wrefresh(btm);
- echo(); nocrmode();
- gets(lnm);
- if (lnm[0] == '?') { help(14); goto l14; }
- sprintf(str, "%d.chemtab", getpid());
- fp = fopen(str, "w");
- fprintf(fp, "*** ChemTab - Periodic Table Database - Transcript for >> %s <<", lnm);
- }
- else if (c != 'n') {
- mvwaddstr(btm, 0, 58, "(y, n, or ?) ");
- goto nxt;
- }
- wprintw(btm, "no"); wrefresh(btm);
- wclear(btm);
- wrefresh(btm);
- noecho(); crmode();
- clear();
- refresh();
-
- bot("Reading in elements...");
- if (textonly) {
- if (readelem() == 888) {
- ouch: printf("The computer has run out of memory.\n");
- exit(1);
- }
- } else
- if (binreadelem() == 888) goto ouch;
-
- bot("Reading close values...");
- if (getclose() == 888) {
- printf("\nThe close value file is corrupt.\n");
- exit(1);
- }
-
- signal(SIGINT, cleanup);
- #ifdef USERSHELL
- signal(SIGTSTP, stop);
- #else USERSHELL
- signal(SIGTSTP, SIG_IGN);
- #endif USERSHELL
- signal(SIGQUIT, SIG_IGN);
- signal(SIGTERM, cleanup);
-
- for (i = 0; i < 3; i++) /* initialize sort choices */
- dosort[i].wch = 0;
-
- bot(" "); /* clear the bottom line */
-
- for (;;) { /* main loop */
- wclear(mn);
- wrefresh(srt);
- upsort(); /* Update the sort window */
- for (i = 1; i < 7; i++) {
- wmove(mn, i+1, 0);
- wprintw(mn, "%d] %s", i, mopts[i]);
- } /* give me choices, then branch off */
- menu("Chemtab 2.01 MAIN MENU");
- #ifdef ReGIS
- mvwaddstr(mn, 11, 0, "^R toggles ReGIS mode which is currently ");
- wstandout(mn);
- wprintw(mn, "%s", regis ? "on." : "off");
- wstandend(mn);
- #endif ReGIS
- loop: mvwaddstr(mn, 9, 0, "Your choice? ");
- wclrtoeol(mn);
- wrefresh(mn);
- cur = mn; xp = 14; yp = 9;
- noecho();
- crmode();
- switch(wgetch(mn)) {
- case '?': help(15); goto loop;
- case '1': looke(); break; /* View specific Element. */
- case '2': schar(); esort(); break; /* Select Characteristics */
- case '3': wclear(mn); wclear(srt); vdata(); break; /* View Selected Elements */
- case '4': wclear(mn); wclear(srt); ptabl(); break; /* Print periodic table.. */
- #ifdef ReGIS
- case '5': wclear(mn); wclear(srt); if (regis) doregisgph(); else dogph(); break;
- case '\022': /* ^R */ if (regis) regis = 0; else regis = 1; break;
- #else ReGIS
- case '5': wclear(mn); wclear(srt); dogph(); break; /* Do a graph for us..... */
- #endif ReGIS
- case '6': cleanup(); break; /* Exit */
- case '\026': /* ^V */ version(); break;
- default: goto loop;
- }
- }
- }
-