home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / cbw / part02 / start.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  4.7 KB  |  258 lines

  1. /*
  2.  * Start up module for the crypt breaking editor.
  3.  * Much of the global behavior of the program is set here.
  4.  *
  5.  * Robert W. Baldwin, December 1984.
  6.  *
  7.  * Bob Baldwin, 10/86:
  8.  *  Added cmd to jump to user window.
  9.  *  Handle C-Z to suspend CBW and abort commands.
  10.  */
  11.  
  12.  
  13. #include    <stdio.h>
  14. #include    <signal.h>
  15. #include    <setjmp.h>
  16. #include    "window.h"
  17. #include    "terminal.h"
  18. #include    "layout.h"
  19. #include    "specs.h"
  20.  
  21.  
  22. /* Shell variable names. */
  23. #define    LETTERSTATS    "LETTERSTATS"
  24. #define    BIGRAMSTATS    "BIGRAMSTATS"
  25. #define    TRIGRAMSTATS    "TRIGRAMSTATS"
  26.  
  27. #define    QUITMSG    "Permutations not saved.  Type 'y' if you want to quit."
  28.  
  29.  
  30. /* Keystroke behavior that is the same in all windows.
  31.  */
  32. extern        alldraw(), jumpcmd();
  33. extern    char    *getenv();
  34.  
  35. keyer    topktab[] = {
  36.         {CREFRESH, alldraw},
  37.         {CJUMPCMD, jumpcmd},
  38.         {0, NULL},
  39.         };
  40.  
  41.  
  42. /* Table of all top-level windows.  Terminated by NULL.
  43.  */
  44. gwindow        *wtable[WINDNUM+1];
  45. char        cfilebuf[100];
  46. char        pfilebuf[100];
  47.  
  48. /* Saved stack state for suspending the program.
  49.  */
  50. jmp_buf        saved_stack;
  51.  
  52.  
  53. main(argc, argv)
  54. int        argc;
  55. char    *argv[];
  56. {
  57.     extern    stop_handler();
  58.     extern    kill_handler();
  59.     char    *q, *pp, *pc;
  60.  
  61.     if (argc < 2)  {
  62.         printf("Usage: %s FileNameRoot\n", argv[0]);
  63.         printf("\tThe extensions .cipher and .perm will be used.");
  64.         printf("\n\tThe shell variables");
  65.         printf(" %s, %s, %s,", LETTERSTATS, TRIGRAMSTATS, BIGRAMSTATS);
  66.         printf("\n\tand TERM must be defined.");
  67.         printf("\n\tThe shell variables");
  68.         printf(" %s and %s", GRAPHICSVAR, KEYMAPVAR);
  69.         printf(" may be defined.");
  70.         printf("\n");
  71.         exit(0);
  72.         }
  73.     q = argv[1];
  74.     pc = cipherfile = cfilebuf;
  75.     pp = permfile = pfilebuf;
  76.     while (*pp++ = *pc++ = *q++);
  77.     pp--;
  78.     pc--;
  79.     q = ".cipher";
  80.     while (*pc++ = *q++);
  81.     q = ".perm";
  82.     while (*pp++ = *q++); 
  83.  
  84.     load_tables();
  85.  
  86.     set_term();
  87.     signal(SIGTSTP, stop_handler);
  88.     signal(SIGINT, kill_handler);
  89.  
  90.     initwindows();
  91.  
  92.     /* Control returns here when program restarted after C-Z. */
  93.      setjmp(saved_stack);
  94.  
  95.     alldraw();
  96.     usrstatus(&user, "Ready.");
  97.     usrfirst(&user, 1, 1);        /* Start in user window. */
  98.     
  99.     wl_driver(wtable);
  100.     done(0);            /* Fell off windows, we're done. */
  101. }
  102.  
  103.  
  104. /* Handle C-Z signal (suspend program).
  105.  * Restore screen and exit.
  106.  * On restart, setup screen, redraw screen, and abort to top loop.
  107.  */
  108. stop_handler()
  109. {
  110.     setcursor(MAXHEIGHT, 1);
  111.     fflush(stdout);
  112.     unset_term();
  113.     
  114.     kill(getpid(), SIGSTOP);
  115.  
  116.     /* Return here when/if program restarted. */
  117.     /* Note that the signal mask is restored by longjmp. */
  118.     set_term();
  119.     longjmp(saved_stack, 0);
  120. }
  121.  
  122.  
  123. /* Handle C-C signal (kill program).
  124.  * Restore screen and exit.
  125.  */
  126. kill_handler()
  127. {
  128.     setcursor(MAXHEIGHT, 1);
  129.     printf("\n");
  130.     fflush(stdout);
  131.     unset_term();
  132.     
  133.     kill(getpid(), SIGKILL);
  134. }
  135.  
  136.  
  137. /* Load stat tables.
  138.  */
  139. load_tables()
  140. {
  141.     printf("\n\nLoading letter statistics ...");
  142.     fflush(stdout);
  143.     if ((letterstats = getenv(LETTERSTATS)) == NULL)  {
  144.         printf("The shell variable %s is not defined.\n", LETTERSTATS);
  145.         exit(0);
  146.         }
  147.     load_1stats_from(letterstats);
  148.     printf(" done.\n");
  149.  
  150.     printf("\n\nLoading bigram statistics ...");
  151.     fflush(stdout);
  152.     if ((bigramstats = getenv(BIGRAMSTATS)) == NULL)  {
  153.         printf("The shell variable %s is not defined.\n", BIGRAMSTATS);
  154.         exit(0);
  155.         }
  156.     load_2stats_from(bigramstats);
  157.     printf(" done.\n");
  158.  
  159.     printf("\n\nLoading trigram statistics ...");
  160.     fflush(stdout);
  161.     if ((trigramstats = getenv(TRIGRAMSTATS)) == NULL)  {
  162.         printf("The shell variable %s is not defined.\n",TRIGRAMSTATS);
  163.         exit(0);
  164.         }
  165.     load_tri_from(trigramstats);
  166.     printf(" done.\n");
  167.     
  168.     permchgflg = FALSE;
  169. }
  170.  
  171.  
  172. /* Quit command
  173.  * This is the prefered way to leave the program.
  174.  */
  175. char *quitcmd(arg)
  176. char    *arg;
  177. {
  178.     char    c;
  179.  
  180.     if (permchgflg)  {
  181.         usrstatus(&user, QUITMSG);
  182.         c = getchar();
  183.         if (!(c == 'y'  ||  c == 'Y'))
  184.               return(NULL);
  185.         }
  186.     done(0);
  187. }
  188.  
  189.  
  190. /* Exit the program after cleaning up the terminal.
  191.  */
  192. done(status)
  193. int    status;
  194. {
  195.     unset_term();
  196.     printf("\n");
  197.     exit(status);
  198. }
  199.  
  200.  
  201. /* (re)Draw all the windows.
  202.  */
  203. alldraw()
  204. {
  205.     wl_refresh(wtable);
  206. }
  207.  
  208.  
  209. /* Jump to the command window.
  210.  * Tell the current window, that it is losing the cursor, and
  211.  * then tell the user window that it's got the cursor.
  212.  */
  213. jumpcmd(w)
  214. gwindow    *w;
  215. {
  216.     (*(w->wlast))(w);
  217.     usrfirst(&user, 1, 1);        /* Given row, col ignored. */
  218. }
  219.  
  220.  
  221. /* Fill in the window table.
  222.  */
  223. initwindows()
  224. {
  225.     int    i;
  226.  
  227.     i = 0;
  228.     wtable[i++] = iuser();
  229.     wtable[i++] = ibanner();
  230.     wtable[i++] = idblabel();
  231.     wtable[i++] = idbstore();
  232.     wtable[i++] = igblabel();
  233.     wtable[i++] = igbstore();
  234.     wtable[i++] = iwebster();
  235.  
  236.     if (i != WINDNUM)  {
  237.         disperr("inittables: WINDNUM value is wrong");
  238.         setcursor(2,1);
  239.         exit(0);
  240.         }
  241.  
  242.     wtable[i] = ((gwindow *) NULL);
  243. }
  244.  
  245.  
  246. /* Get keystroke routine.
  247.  * Responsible for clearing the status area before every keystroke.
  248.  */
  249. key    u_getkey()
  250. {
  251.     key    k;
  252.  
  253.     k = getcmd();
  254.     usrstatus(&user, "");
  255.  
  256.     return(k);
  257. }
  258.