home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume38 / menushell / part02 / functions2.c next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  6.4 KB  |  268 lines

  1. #include "mshell.h"
  2.  
  3.  
  4. extern char     G_homevar      [];
  5. extern char     G_uservar      [];
  6. extern char     G_termvar      [];
  7. extern char     G_mailfile     [WORDLEN];
  8. extern char     G_mail_message [WORDLEN];
  9. extern int      G_mailsize; 
  10. extern struct   stat G_st;
  11.  
  12. /* ======================================================================= */
  13. execute_command (command, args)
  14. /* ======================================================================= */
  15. char *command, *args [];
  16. {
  17.     /*
  18.     char * var_args [2 * MAXARGS]; 
  19.     char * new_var_args [MAXARGS][MAXARGS];
  20.     */
  21.     int pid, i, j, count = 0;
  22.     extern int G_limited;
  23.  
  24.     G_mail_message[0] = EOS;
  25.  
  26.     if (G_limited && invalidcommand(args[0])) {
  27.         printf("Invalid option in restricted menus, sorry.\n");
  28.         printf("See the 'info' menu for how to get a real menu or shell.  (It's free.)\n");
  29.         return;
  30.     }
  31.     if (strcontains(command, "*;|<>&()[]?'\"`~\\")) {
  32.         /* let shell handle these */
  33.         system(command);
  34.         return;
  35.     }
  36.  
  37.     if (strcmp (args[0], CHANGE_DIR) == 0)
  38.         change_directory (args[1]);
  39.     else if (strcmp (args[0], SETENV) == 0)
  40.         /* remove $ sign from environment variable */
  41.         setenv (args[1], args[2]);
  42.  
  43.      /* invoke the command using fork & exec *
  44.       * ==================================== */
  45.      else if ( ( pid = fork () ) == 0 ) {    /*     child process */
  46.         signal (SIGINT,  SIG_DFL); 
  47.         signal (SIGQUIT, SIG_DFL); 
  48.         execvp(args[0], args);
  49.         perror(args[0]);
  50.         exit(1);
  51.     }
  52.     else
  53.         while ( wait (0) !=  -1 )    /*    parent waits for  */
  54.             ;             /*    child            */
  55. }
  56.  
  57. /* ========================================================= */
  58. get_actions (input_string, exec_string, args)
  59. /* ========================================================= */
  60. char *input_string, *exec_string, *args[];
  61. {
  62.     char *p = input_string, word[WORDLEN], *val, *type, *realval;
  63.     char *index(), *prompt(), *ufix(), *strsave(), *strcatsave();
  64.     int    i = 0;
  65.  
  66.     /* Display a helpfile, if appropriate *
  67.      * ================================== */
  68.     extract_action_word    (input_string, HELPVAL, word, 0);
  69.     if (word[0])
  70.         helpfile_display(word);
  71.  
  72.     /* pull out the actual value of args *
  73.      * ==================================*/
  74.  
  75.     i = -1;
  76.     exec_string[0] = EOS;
  77.  
  78.     /* XXX - should check i not out of bounds */
  79.     while ( sscanf(p, "%s", word) == 1 ) {
  80.         while (isspace(*p))    /* skip spaces, then word... */
  81.             p++;
  82.         p += strlen(word);    /* ...for sscanf next time */
  83.  
  84.         if ((val = index(word, '=')) == NULL) {
  85.             /* default: treat like arg= */
  86.             val = word;
  87.             type = "arg";
  88.         }
  89.         else {
  90.             val[0] = EOS;    /* split word into selector and value */
  91.             val++;        /* make val point to value part */
  92.             type = word;
  93.         }
  94.  
  95.         /* build an argv entry */
  96.         if (strcmp(type, "cmd") == 0 || strcmp(type, "arg") == 0)
  97.             args[++i] = realval = ufix(strsave(val));
  98.         else if (strcmp(type, "prompt") == 0)
  99.             args[++i] = realval = strsave(prompt(val));
  100.         else if (strcmp(type, "aarg") == 0)
  101.             args[i] = realval = ufix(strcatsave(args[i], val));
  102.         else if (strcmp(type, "aprompt") == 0)
  103.             args[i] = realval = strcatsave(args[i], prompt(val));
  104.  
  105.         /* build string for 'system' just in case */
  106.         if (strcmp(type, "aarg") != 0 && strcmp(type, "aprompt") != 0)
  107.             strcat(exec_string, " ");
  108.         strcat(exec_string, realval);
  109.     }
  110.  
  111.     args[++i] = NULL;
  112. }
  113.  
  114. /* function to reorder various command line args based on original string *
  115.  ======================================================================== */
  116. assign_parameters (varg_ptr, input_line, command, args, aargs, promptval)
  117. char * varg_ptr [2 * MAXARGS];
  118. char input_line  [DESCLEN];
  119. char command     [WORDLEN];
  120. char args        [MAXARGS][WORDLEN];
  121. char aargs       [MAXARGS][WORDLEN];
  122. char promptval   [MAXARGS][WORDLEN];
  123.  
  124. {
  125.     char target_string [DESCLEN];
  126.     int position, i = 0;
  127.     
  128.     i = 0;
  129.     while ( strcmp (input_line, NULLSTR) != 0 ) {
  130.  
  131.         filter_leading_trailing_blanks_tabs (input_line);
  132.         if ( (position = strsearch (input_line, " ")) < 0 ) 
  133.             position = strlen(input_line) - 1;
  134.  
  135.         target_string[0] = EOS;
  136.         strncpy ( target_string, input_line, position + 1);
  137.         target_string [position+1] = EOS;
  138.         remove_string (input_line, 0, position + 1);
  139.  
  140.         if ( strsearch (target_string, CMDVAL) >= 0)
  141.             varg_ptr [i++] = command;
  142.         else if ( strsearch (target_string, ARGVAL) >= 0) 
  143.             varg_ptr [i++] = (args++);
  144.         else if ( strsearch (target_string, AARGVAL) >= 0)
  145.             varg_ptr [i++] = (aargs++);
  146.         else if ( strsearch (target_string, PROMPTVAL) >= 0)
  147.             varg_ptr [i++] = (promptval++);
  148.         else if (index(target_string, '=') == NULL) /* same as ARGVAL */
  149.             varg_ptr [i++] = (args++);
  150.         
  151.     }    /* while */
  152. }        /* assign_parameters */
  153.  
  154. reorganize (var_args, new_var_args, rcount)
  155. char * var_args [2 * MAXARGS];
  156. char * new_var_args [MAXARGS][MAXARGS];
  157. int  * rcount;
  158.  
  159. {
  160.     int i, j=0, k=0;
  161.     char * temp;
  162.  
  163.     for ( i=0; var_args[i] != NULL; ) {
  164.  
  165.         while (( !index (var_args[i], PIPECHAR)) && (var_args[i] != NULL))
  166.             new_var_args[j][k++] = var_args[i++];
  167.  
  168.         if ( index (var_args[i], PIPECHAR) ) {
  169.             new_var_args[j++][k] = NULL;
  170.             k = 0;
  171.             temp = var_args[i++];
  172.             temp++;
  173.             new_var_args[j]  [k++] = temp;
  174.         }
  175.     }
  176.  
  177.     new_var_args [j][k] = NULL;
  178.     *rcount = j + 1;
  179. }
  180.  
  181. doexec(args)
  182. char *args[];    /* assumes is run in child process */
  183. {
  184.     int fds [2], rpid, i = MAXARGS;
  185.  
  186.     while (--i > 0)
  187.         if (args[i] && args[i][0] == PIPECHAR)
  188.             break;
  189.  
  190.     if (i > 0) {        /* not first command in pipeline */
  191.         args[i]++;    /* skip PIPECHAR */
  192.         pipe(fds);
  193.         if (fork()) {
  194.             dup2(fds[1], 1);
  195.             close(fds[0]);
  196.             close(fds[1]);
  197.         }
  198.         else {    /* child */
  199.             close(0);
  200.             dup(fds[0]);
  201.             close (fds[0]);
  202.             close (fds[1]);
  203.  
  204.             while (i < MAXARGS)
  205.                 args[i++] = NULL;
  206.  
  207.             doexec(args);
  208.         }
  209.     }
  210.  
  211.     execvp(args[i], args+i);
  212.     perror(args[i]);
  213.     exit(0);
  214. }
  215.  
  216. struct command {
  217.     DL_NODE n;
  218.     char *c;
  219. };
  220.  
  221. invalidcommand(cmd)
  222. char *cmd;
  223. {
  224.     static int initialized;
  225.     static DLIST commands;
  226.     int iscmd();
  227.  
  228.     if (!initialized) {
  229.         FILE *fp;
  230.         char line[256], *p;
  231.         struct command *c;
  232.  
  233.         if ((fp = fopen(COMMAND_LIST, "r")) == NULL) {
  234.             printf("Command list missing!\n");
  235.             return TRUE;
  236.         }
  237.  
  238.         if ((commands = dl_create(DL_FREE)) == NULL) {
  239.             printf("Out of memory!\n");
  240.             exit(1);
  241.         }
  242.  
  243.         while (fgets(line, sizeof(line), fp)) {
  244.             if ((p = malloc(strlen(line)+1)) == NULL ||
  245.                 (c = getnode(sizeof(struct command))) == NULL) {
  246.                 printf("Out of memory!\n");
  247.                 exit(1);
  248.             }
  249.             strcpy(p, line);
  250.             p[strlen(p)-1] = '\0';
  251.             c->c = p;
  252.             dl_append(commands, c);
  253.         }
  254.  
  255.         fclose(fp);
  256.         initialized = TRUE;
  257.     }
  258.  
  259.     return(!dl_lsearch(commands, dl_head(commands), NULL, cmd, iscmd));
  260. }
  261.  
  262. iscmd(c, cmd)
  263. struct command *c;
  264. char *cmd;
  265. {
  266.     return(strcmp(cmd, c->c) == 0);
  267. }
  268.