home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / uemacs / part4 / sys / ultrix / spawn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.7 KB  |  106 lines

  1. /*
  2.  * Name:    MicroEMACS
  3.  *        Spawn CLI; stop if C shell.
  4.  * Version:    29
  5.  * Last edit:    10-Feb-86
  6.  * By:        rex::conroy
  7.  *        decvax!decwrl!dec-rhea!dec-rex!conroy
  8.  *
  9.  * Spawn. New version, which
  10.  * interracts with the job control stuff
  11.  * in the 4.X BSD C shell.
  12.  */
  13. #include    "def.h"
  14.  
  15. #include    <sgtty.h>
  16. #include    <signal.h>
  17.  
  18. char    *shellp    = NULL;            /* Saved "SHELL" name.        */
  19.  
  20. extern    struct    sgttyb    oldtty;        /* There really should be a    */
  21. extern    struct    sgttyb    newtty;        /* nicer way of doing this, so    */
  22. extern    struct    sgttyb    oldtchars;    /* spawn does not need to know    */
  23. extern    struct    sgttyb    newtchars;    /* about the insides of the    */
  24. extern    struct    sgttyb    oldltchars;    /* terminal I/O code.        */
  25. extern    struct    sgttyb    newltchars;
  26.  
  27. extern    char    *getenv();
  28.  
  29. /*
  30.  * This code does a one of 2 different
  31.  * things, depending on what version of the shell
  32.  * you are using. If you are using the C shell, which
  33.  * implies that you are using job control, then MicroEMACS
  34.  * moves the cursor to a nice place and sends itself a
  35.  * stop signal. If you are using the Bourne shell it runs
  36.  * a subshell using fork/exec. Bound to "C-C", and used
  37.  * as a subcommand by "C-Z".
  38.  */
  39. spawncli(f, n, k)
  40. {
  41.     register int    pid;
  42.     register int    wpid;
  43.     register int    (*oqsig)();
  44.     register int    (*oisig)();
  45.     int        status;
  46.  
  47.     if (shellp == NULL) {
  48.         shellp = getenv("SHELL");
  49.         if (shellp == NULL)
  50.             shellp = getenv("shell");
  51.         if (shellp == NULL)
  52.             shellp = "/bin/sh";    /* Safer.        */
  53.     }
  54.     ttcolor(CTEXT);
  55.     ttnowindow();
  56.     if (strcmp(shellp, "/bin/csh") == 0) {
  57.         if (epresf != FALSE) {
  58.             ttmove(nrow-1, 0);
  59.             tteeol();
  60.             epresf = FALSE;
  61.         }                /* Csh types a "\n"    */
  62.         ttmove(nrow-2, 0);        /* before "Stopped".    */
  63.     } else {
  64.         ttmove(nrow-1, 0);
  65.         if (epresf != FALSE) {
  66.             tteeol();
  67.             epresf = FALSE;
  68.         }
  69.     }
  70.     ttflush();
  71.     if (ioctl(0, TIOCSLTC, &oldltchars) < 0
  72.     ||  ioctl(0, TIOCSETC, &oldtchars)  < 0
  73.     ||  ioctl(0, TIOCSETP, &oldtty)     < 0) {
  74.         eprintf("IOCTL #1 to terminal failed");
  75.         return (FALSE);
  76.     }
  77.     if (strcmp(shellp, "/bin/csh") == 0)    /* C shell.        */
  78.         kill(0, SIGTSTP);
  79.     else {                    /* Bourne shell.    */
  80.         oqsig = signal(SIGQUIT, SIG_IGN);
  81.         oisig = signal(SIGINT,  SIG_IGN);
  82.         if ((pid=fork()) < 0) {
  83.             signal(SIGQUIT, oqsig);
  84.             signal(SIGINT,  oisig);
  85.             eprintf("Failed to create process");
  86.             return (FALSE);
  87.         }
  88.         if (pid == 0) {
  89.             execl(shellp, "sh", "-i", NULL);
  90.             _exit(0);        /* Should do better!    */
  91.         }
  92.         while ((wpid=wait(&status))>=0 && wpid!=pid)
  93.             ;
  94.         signal(SIGQUIT, oqsig);
  95.         signal(SIGINT,  oisig);
  96.     }
  97.     sgarbf = TRUE;                /* Force repaint.    */
  98.     if (ioctl(0, TIOCSETP, &newtty)     < 0
  99.     ||  ioctl(0, TIOCSETC, &newtchars)  < 0
  100.     ||  ioctl(0, TIOCSLTC, &newltchars) < 0) {
  101.         eprintf("IOCTL #2 to terminal failed");
  102.         return (FALSE);
  103.     }
  104.     return (TRUE);
  105. }
  106.