home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / execute.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  3KB  |  188 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 "keymap.h"
  11. #include "nn_term.h"
  12.  
  13.  
  14. /* execute.c */
  15.  
  16. static shell_check __APROTO((void));
  17.  
  18.  
  19. export int shell_restrictions = 0;    /* disable shell escapes */
  20.  
  21. export char *init_shell = SHELL;
  22. export char *user_shell;
  23. export char *exec_chdir_to = NULL;
  24.  
  25. extern int errno;
  26.  
  27.  
  28. static int
  29. shell_check()
  30. {
  31.     if (shell_restrictions) {
  32.     msg("Restricted operation - not allowed");
  33.     return -1;
  34.     }
  35.     return 0;
  36. }
  37.  
  38. void
  39. init_execute()
  40. {
  41.     if ((user_shell = getenv("SHELL")) == NULL)
  42.     user_shell = SHELL;
  43. }
  44.  
  45. int
  46. execute(path, args, toggle_visual)
  47. char *path, **args;
  48. int toggle_visual;
  49. {
  50.     int was_raw, pid, i, status;
  51.     sig_type  (*quit)(), (*intr)(), (*tstp)();
  52.  
  53.     was_raw = toggle_visual ? visual_off() : unset_raw();
  54.  
  55.     while ((pid = fork()) == -1) sleep(1);
  56.  
  57.     if (pid == 0) {
  58.     for (i = 3 ; i < 20 ; i++)
  59.         close(i);
  60.  
  61.     if (exec_chdir_to != NULL) chdir(exec_chdir_to);
  62.  
  63.     execv(path, args);
  64.  
  65.     fprintf(stderr, "%s: not found\n", path);
  66.     nn_exit(20);
  67.     }
  68.     quit = signal(SIGQUIT, SIG_IGN);
  69.     intr = signal(SIGINT,  SIG_IGN);
  70. #ifdef HAVE_JOBCONTROL
  71.     tstp = signal(SIGTSTP, SIG_DFL);
  72. #endif
  73.     while ((i = wait(&status)) != pid && (i != -1 || errno == EINTR));
  74.  
  75.     signal(SIGQUIT, quit);
  76.     signal(SIGINT,  intr);
  77. #ifdef HAVE_JOBCONTROL
  78.     signal(SIGTSTP, tstp);
  79. #endif
  80.     if (toggle_visual) {
  81.     visual_on();
  82.     if (toggle_visual == 2) s_redraw++;
  83.     }
  84.  
  85.     if (was_raw) nn_raw();
  86.  
  87.     return (status & 0xff) ? 0x100 : (status >> 8);
  88. }
  89.  
  90.  
  91. int
  92. shell_escape()
  93. {
  94.     static char command[FILENAME] = "";
  95.     char *cmd;
  96.     int first = 1;
  97.  
  98.     if (shell_check()) return 0;
  99.  
  100.     for (;;) {
  101.     prompt("\1!\1");
  102.     cmd = get_s(command, NONE, NONE, NULL_FCT);
  103.     if (cmd == NULL) return !first;
  104.  
  105.     if (*cmd == NUL) {
  106.         if (first) run_shell((char *)NULL, 1, 0);
  107.         return 1;
  108.     }
  109.  
  110.     strcpy(command, cmd);
  111.  
  112.     if (run_shell(command, first, 0) < 0) return !first;
  113.     first = 0;
  114.     prompt_line = -1;
  115.     }
  116. }
  117.  
  118.  
  119. static char *exec_sh_args[] = {
  120.     "nnsh",
  121.     (char *)NULL, /* "-c" or "-i" */
  122.     (char *)NULL, /* cmdstring */
  123.     (char *)NULL
  124. };
  125.  
  126. int
  127. run_shell(command, clear, init_sh)
  128. char *command;
  129. int clear; /* -2 => no command output (:!!command) - keep visual,
  130.           output before command: -1 => none, 0 => CR/NL, 1 => clear */
  131. int init_sh;    /* 0 => use user_shell, else use init_shell */
  132. {
  133.     char cmdstring[512];
  134.  
  135.     if (shell_check()) return -1;
  136.  
  137.     if (command != NULL) {
  138.     if (!expand_file_name(cmdstring, command, 1)) return -1;
  139.     exec_sh_args[1] = "-c";
  140.         exec_sh_args[2] = cmdstring;
  141.     } else {
  142.     exec_sh_args[1] = "-i";
  143.         exec_sh_args[2] = NULL;
  144.     }
  145.  
  146.     if (clear > 0) {
  147.     clrdisp();
  148.     fl;
  149.     } else if (clear == 0) {
  150.     tputc(CR);
  151.     tputc(NL);
  152.     }
  153.  
  154.     return execute(init_sh ? init_shell : user_shell,
  155.            exec_sh_args, clear == -2 ? 0 : 1);
  156. }
  157.  
  158. #ifndef HAVE_JOBCONTROL
  159. static char *exec_suspend_args[] = {
  160.     "nnsh",
  161.     "-i",
  162.     (char *)NULL
  163. };
  164. #endif
  165.  
  166. int
  167. suspend_nn()
  168. {
  169.     int was_raw;
  170.  
  171.     if (shell_check()) return 0;
  172.  
  173.     gotoxy(0, Lines-1);
  174.     clrline();
  175.  
  176. #ifdef HAVE_JOBCONTROL
  177.     was_raw = visual_off();
  178.     kill(process_id, SIGSTOP);
  179.     visual_on();
  180.     s_redraw++;
  181.     if (was_raw) nn_raw();
  182. #else
  183.     execute(user_shell, exec_suspend_args, 2);
  184. #endif
  185.  
  186.     return 1;
  187. }
  188.