home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / menushell / part03 / mshell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  4.9 KB  |  210 lines

  1. #include "mshell.h"
  2. #include <setjmp.h>
  3. char * index ();
  4.  
  5.  
  6. extern char     G_homevar      [];
  7. extern char     G_uservar      [];
  8. extern char     G_termvar      [];
  9. extern char     G_mailfile     [WORDLEN];
  10. extern char     G_mail_message [WORDLEN];
  11. extern int      G_mailsize; 
  12. extern int      G_shell_ok; 
  13. extern struct   stat G_st;
  14.  
  15. /* ================== */
  16. M_Shell (m)
  17. /* ================== */
  18.  
  19. char *m;
  20.  
  21. {
  22.     char opt           [WORDLEN],
  23.          opt1          [OPTLEN],
  24.          opt2          [OPTLEN],
  25.          action_string [DESCLEN],
  26.          exec_string   [DESCLEN],
  27.          *args         [MAXARGS],
  28.          *menu_array   [LINES],
  29.          tmpword       [WORDLEN],
  30.          *malloc();
  31.  
  32.     int  i, 
  33.          idx,
  34.          invalid_option = FALSE,
  35.          firsttime = TRUE,
  36.          dontdisplay = FALSE,
  37.          unix_flag,
  38.          menu_flag,
  39.          exit();
  40.  
  41.     static jmp_buf topenv;
  42.     static int topenvset;
  43.  
  44.     menu_flag = FALSE;
  45.     signal (SIGHUP,  exit);
  46.     signal (SIGINT,  SIG_IGN);    /* ignore all ^C interrupts */
  47.     signal (SIGQUIT, SIG_IGN);    /* ignore all ^\ interrupts */
  48.     signal (SIGTSTP, SIG_IGN);    /* ignore all ^Z interrupts */
  49.     signal (SIGPIPE, SIG_IGN);    /* ignore dead pipes */
  50.     log("enter", m);
  51.  
  52.     while TRUE {
  53.  
  54.         if (!topenvset) {
  55.             topenvset = TRUE;
  56.             setjmp(topenv);
  57.         }
  58.  
  59.         unix_flag = FALSE;
  60.  
  61.         /* keep looping until a valid response has been entered *
  62.          * ==================================================== */
  63.         do {
  64.             if (display_menu(m, menu_array, &menu_flag, &idx) == 0)
  65.                 return;
  66.  
  67.             if ( firsttime ) {
  68.                 firsttime = FALSE;
  69.                 search_menu_array (menu_array, idx, "_init",
  70.                     action_string, &invalid_option);
  71.                 if (!invalid_option) {
  72.                     while (substitute(action_string))
  73.                         ;
  74.                     get_actions (action_string,
  75.                         exec_string, args);
  76.                     execute_command (exec_string, args);
  77.                 }
  78.                 else    
  79.                     invalid_option = FALSE;
  80.             }
  81.  
  82.             if ( invalid_option )
  83.                 printf ("No such choice as '%s'.", opt);
  84.  
  85.             if (!moreinput())
  86.                 printf ("\tSelect choice [or help, x, top, bye]: ");
  87.             opt[0] = 0;
  88.             read_input_line (opt);
  89.             log("    ", opt);
  90.             invalid_option = FALSE;
  91.             putchar('\n');
  92.  
  93.             /* if first character of option is an "!"    *
  94.              * character then invoke the remaining line  *
  95.              * using the C system function               *
  96.              * ======================================== */
  97.  
  98.             if ( !opt[0] )
  99.                 continue;
  100.             else if ( opt[0] == EXCLAIM && G_shell_ok ) {
  101.                 system(opt+1);
  102.                 unix_flag = TRUE;
  103.                 wait_for_user ();
  104.             }
  105.             else {
  106.                 search_menu_array (menu_array, idx, opt,
  107.                     action_string, &invalid_option);
  108.             }
  109.         }
  110.         while ( invalid_option );
  111.  
  112.         if ( unix_flag || !opt[0] )
  113.         /* bypass execution of code until end of while(TRUE) loop *
  114.          * ====================================================== */
  115.             continue;
  116.  
  117.  
  118.         /* replace environment variables with values */
  119.         while (substitute(action_string))
  120.             ;
  121.  
  122.     /* remove actual values of commands and arguments from action-string */
  123.     /* ================================================================= */
  124.         get_actions (action_string, exec_string, args);
  125.  
  126.     /* display any prompt and get values if specified in the command line */
  127.     /* ================================================================== */
  128.         opt1[0] = EOS;
  129.         opt2[0] = EOS;
  130.  
  131.         if ( index (opt, BLANK) != NULL )
  132.             sscanf (opt, "%s %s", opt1, opt2);
  133.         else
  134.             strcpy (opt1, opt);
  135.  
  136.         if ( strcmp (opt1, HELP) == 0 ) {
  137.             if ( strcmp (opt2, NULLSTR) == 0 )
  138.                 strcpy(opt2, prompt("Help on which choice"));
  139.  
  140.             search_menu_array (menu_array, idx, opt2,
  141.                    action_string, &invalid_option);
  142.  
  143.             if ( strcmp (action_string, NULLSTR) == 0 ) {
  144.                 invalid_option = TRUE;
  145.                 printf ("\tNo such help option name as: %s\!\!\n", opt2);
  146.             }
  147.             else {
  148.                 tmpword[0] = EOS;
  149.                 extract_action_word (action_string, MAN, tmpword, 0);
  150.                 if ( strcmp (tmpword, NULLSTR) == 0 ) 
  151.                     extract_action_word (action_string, CMDVAL, tmpword, 0);
  152.                 if (strcmp(tmpword, NULLSTR) != 0)
  153.                     do_man(tmpword);
  154.             }
  155.  
  156. /*            if ( invalid_option ) {*/
  157.                 invalid_option = FALSE;
  158.                 wait_for_user ();
  159. /*            }*/
  160.             continue;
  161.         }
  162.  
  163.     /* if the command is to invoke another menu then do not exec    *
  164.      * but call M_Shell recursively                    *
  165.      * =========================================================    */
  166.         if ( all_blanks (opt) ) 
  167.             ;
  168.         else if (strcmp (opt, BYE)  == 0)
  169.             bye(0);
  170.         else if (strcmp (opt, QUIT) == 0) {
  171.             for (i = 0; i < LINES; i++)
  172.                 if (menu_array[i])
  173.                     free(menu_array[i]);
  174.             log("exit", m);
  175.             return;
  176.         }
  177.         else if (strcmp(opt, "T") == 0 || strcmp(opt, "top") == 0) {
  178.             for (i = 0; i < LINES; i++)
  179.                 if (menu_array[i])
  180.                     free(menu_array[i]);
  181.             longjmp(topenv, 1);
  182.         }
  183.         else if ( strcmp(args[0], "menu") == 0 )
  184.             M_Shell (args[1]);
  185.         else {
  186.             execute_command (exec_string, args);
  187.             wait_for_user();
  188.         }
  189.     }
  190. }
  191.  
  192. do_man(entry)
  193. char *entry;
  194. {
  195.     char cmd[WORDLEN], *p, *rindex();
  196.  
  197.     if (p = rindex(entry, '/'))
  198.         entry = p+1;
  199.  
  200.     sprintf(cmd, "man %s", entry);
  201.     system(cmd);
  202. }
  203.  
  204. bye(status)
  205. int status;
  206. {
  207.     printf("\nLogging out -- come back again soon...\n\n");
  208.     exit(status);
  209. }
  210.