home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / nn6.4 / part19 / execute.c < prev    next >
C/C++ Source or Header  |  1990-06-07  |  3KB  |  172 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    UNIX command execution.
  5.  */
  6.  
  7. #include <signal.h>
  8. #include <errno.h>
  9. #include "config.h"
  10. #include "term.h"
  11.  
  12. export int shell_restrictions = 0;    /* disable shell escapes */
  13.  
  14. export char *user_shell;
  15. export char *exec_chdir_to = NULL;
  16.  
  17. static shell_check()
  18. {
  19.     if (shell_restrictions) {
  20.     msg("Restricted operation - not allowed");
  21.     return -1;
  22.     }
  23.     return 0;
  24. }
  25.  
  26.  
  27. init_execute()
  28. {
  29.     if ((user_shell = getenv("SHELL")) == NULL)
  30.     user_shell = SHELL;
  31. }
  32.  
  33. execute(path, args, toggle_visual)
  34. char *path, **args;
  35. int toggle_visual;
  36. {
  37.     int was_raw, pid, i, status;
  38.     sig_type  (*quit)(), (*intr)(), (*tstp)();
  39.     extern int errno;
  40.  
  41.     was_raw = toggle_visual ? visual_off() : unset_raw();
  42.  
  43.     while ((pid = fork()) == -1) sleep(1);
  44.  
  45.     if (pid == 0) {
  46.     for (i = 3 ; i < 20 ; i++)
  47.         close(i);
  48.  
  49.     if (exec_chdir_to != NULL) chdir(exec_chdir_to);
  50.  
  51.     execv(path, args);
  52.  
  53.     fprintf(stderr, "%s: not found\n", path);
  54.     nn_exit(20);
  55.     }
  56.     quit = signal(SIGQUIT, SIG_IGN);
  57.     intr = signal(SIGINT,  SIG_IGN);
  58. #ifdef HAVE_JOBCONTROL
  59.     tstp = signal(SIGTSTP, SIG_DFL);
  60. #endif
  61.     while ((i = wait(&status)) != pid && (i != -1 || errno == EINTR));
  62.  
  63.     signal(SIGQUIT, quit);
  64.     signal(SIGINT,  intr);
  65. #ifdef HAVE_JOBCONTROL
  66.     signal(SIGTSTP, tstp);
  67. #endif
  68.     if (toggle_visual) {
  69.     visual_on();
  70.     if (toggle_visual == 2) s_redraw++;
  71.     }
  72.  
  73.     if (was_raw) raw();
  74.  
  75.     return status != 0;
  76. }
  77.  
  78.  
  79. shell_escape()
  80. {
  81.     static char command[FILENAME] = "";
  82.     char *cmd;
  83.     int first = 1;
  84.  
  85.     if (shell_check()) return 0;
  86.  
  87.     for (;;) {
  88.     prompt("\1!\1");
  89.     cmd = get_s(command, NONE, NONE, NULL_FCT);
  90.     if (cmd == NULL) return !first;
  91.  
  92.     if (*cmd == NUL) {
  93.         if (first) run_shell((char *)NULL, 1);
  94.         return 1;
  95.     }
  96.  
  97.     strcpy(command, cmd);
  98.  
  99.     if (!run_shell(command, first)) return !first;
  100.     first = 0;
  101.     prompt_line = -1;
  102.     }
  103. }
  104.  
  105.  
  106. static char *exec_sh_args[] = {
  107.     "nnsh",
  108.     (char *)NULL, /* "-c" or "-i" */
  109.     (char *)NULL, /* cmdstring */
  110.     (char *)NULL
  111. };
  112.  
  113. run_shell(command, clear)
  114. char *command;
  115. int clear; /* -2 => no command output (:!!command) - keep visual,
  116.           output before command: -1 => none, 0 => CR/NL, 1 => clear */
  117. {
  118.     char cmdstring[512];
  119.  
  120.     if (shell_check()) return 0;
  121.  
  122.     if (command != NULL) {
  123.     if (!expand_file_name(cmdstring, command, 1)) return 0;
  124.     exec_sh_args[1] = "-c";
  125.         exec_sh_args[2] = cmdstring;
  126.     } else {
  127.     exec_sh_args[1] = "-i";
  128.         exec_sh_args[2] = NULL;
  129.     }
  130.  
  131.     if (clear > 0) {
  132.     clrdisp();
  133.     fl;
  134.     } else if (clear == 0) {
  135.     putchar(CR);
  136.     putchar(NL);
  137.     }
  138.  
  139.     execute(user_shell, exec_sh_args, clear == -2 ? 0 : 1);
  140.     return 1;
  141. }
  142.  
  143. #ifndef HAVE_JOBCONTROL
  144. static char *exec_suspend_args[] = {
  145.     "nnsh",
  146.     "-i",
  147.     (char *)NULL
  148. };
  149. #endif
  150.  
  151. suspend_nn()
  152. {
  153.     int was_raw;
  154.  
  155.     if (shell_check()) return 0;
  156.  
  157.     gotoxy(0, Lines-1);
  158.     clrline();
  159.  
  160. #ifdef HAVE_JOBCONTROL
  161.     was_raw = visual_off();
  162.     kill(process_id, SIGSTOP);
  163.     visual_on();
  164.     s_redraw++;
  165.     if (was_raw) raw();
  166. #else
  167.     execute(user_shell, exec_suspend_args, 2);
  168. #endif
  169.  
  170.     return 1;
  171. }
  172.