home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / SHELL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-08  |  1.9 KB  |  58 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. extern char    *_base;
  6.  
  7. shell()
  8. /*
  9.  *    Invoke a command line interface shell.  If the "SHELL" environment
  10.  *    variable is not defined, a prompt showing the current working
  11.  *    directory will be given.  Each line entered will then be passed
  12.  *    to the system() function for execution until the command "exit"
  13.  *    is entered to terminate the shell.  If "SHELL" is defined, and
  14.  *    the "_shell_p" variable is valid, the value of "SHELL" will be
  15.  *    passed to the program pointed to by "_shell_p" in order to allow
  16.  *    the shell to invoke a command line interaction of its own.  If
  17.  *    the "_shell_p" variable is not valid, the program defined by
  18.  *    "SHELL" will be searched for along the "PATH", and executed with
  19.  *    no arguments.  If the "SHELL" can't be found, the internal command
  20.  *    line described above will be used.
  21.  */
  22. {
  23.     register char *p, *q;
  24.     register int rv = 0;
  25.     register int (*shell)();
  26.     char command[128];
  27.     char *getenv(), *strtok(), *pfindfile(), *fullpath();
  28.     int getch(), putch();
  29.  
  30.     /* SHELL= variable? */
  31.     if((p = getenv("SHELL")) && (*p)) {    /* SHELL is valid? */
  32.         q = Super(0L);
  33.         shell = *((long *) 0x4F6L);    /* get _shell_p value */
  34.         Super(q);
  35.         if((shell) &&                /* shell available */
  36.            (((long) shell) < ((long) _base)) &&    /* reasonable value */
  37.            (strncmp(shell, "PATH", 4))) {    /* not corrupted */
  38.             rv = (*shell)(p);        /* call shell */
  39.             return;
  40.         }
  41.         if(p = pfindfile(p, "prg\0ttp\0tos\0")) {
  42.             spawn(p, "");            /* execute shell */
  43.             return;
  44.         }
  45.     }
  46.     /* internal command line interface */
  47.     for(;;) {
  48.         cprintf("%s> ", fullpath(NULL, ""));    /* cwd as prompt */
  49.         getln(NULL, getch, putch, command, 128);/* editable field */
  50.         cputs("\r\n");
  51.         if(command[0] == '\0')            /* ignore blank lines */
  52.             continue;
  53.         if(!stricmp(command, "exit"))        /* leave the shell */
  54.             return;
  55.         system(command);            /* execute a command */
  56.     }
  57. }
  58.