home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / newemacs / spawn.c < prev    next >
C/C++ Source or Header  |  1986-02-06  |  5KB  |  301 lines

  1. /*
  2.  * The routines in this file
  3.  * are called to create a subjob running
  4.  * a command interpreter.
  5.  */
  6. #include    <stdio.h>
  7. #include    "ed.h"
  8.  
  9. #define    PRIVATE    static
  10. #define    PUBLIC    extern
  11. #define    NULL    0
  12. #define    VOID    int
  13. #define    BOOLEAN    int
  14.  
  15. /*
  16.  * Create a subjob with a copy
  17.  * of the command intrepreter in it. When the
  18.  * command interpreter exits, mark the screen as
  19.  * garbage so that you do a full repaint. Bound
  20.  * to "C-C".
  21.  */
  22. spawncli(f, n)
  23. {
  24.     mlwrite("\nWhen done, enter the command 'EXIT' to return to EMACS...\n") ;
  25.     movecursor(NROW, 0);        /* Seek to last line.    */
  26.     sys("COMMAND.COM", "");        /* Run CLI.        */
  27.     sgarbf = TRUE;
  28.     return(TRUE);
  29. }
  30.  
  31. /*
  32.  * Run a one-liner in a subjob.
  33.  * When the command returns, wait for a single
  34.  * character to be typed, then mark the screen as
  35.  * garbage so a full repaint is done.
  36.  * Bound to "C-X !".
  37.  */
  38. spawn(f, n)
  39. {
  40.     register int    s;
  41.     char        line[NLINE];
  42.     if ((s=mlreply("MS-DOS command: ", line, NLINE)) != TRUE)
  43.         return (s);
  44.     system(line);
  45.     mlwrite("\nStrike any key to return to EMACS...") ;
  46.     while (scr_ci() != '\r')    /* Pause.        */
  47.         ;
  48.     sgarbf = TRUE;
  49.     return (TRUE);
  50. }
  51.  
  52. system(cmd)
  53. char    *cmd;
  54. {
  55.     char buffer[100] ;
  56.  
  57.     strcpy(buffer, "/C ") ;
  58.     strcat(buffer, cmd) ;
  59.     return(sys("COMMAND.COM", buffer));
  60. }
  61.  
  62. sys(cmd, tail)
  63. char    *cmd;
  64. char    *tail;
  65. {
  66.     PRIVATE int Execute() ;
  67.  
  68.     return(Execute(cmd, tail));
  69. }
  70.  
  71. PRIVATE int Execute(program, args)
  72. char *program, *args ;
  73.     {
  74.     char *path ;
  75.     PRIVATE char *Full_Path_Name() ;
  76.  
  77.     if (path = Full_Path_Name(program))
  78.         {
  79.         return (exec(path, args)) ;
  80.         }
  81.     return 255 ;
  82.     }
  83.  
  84. PRIVATE char *Full_Path_Name(file)
  85. char *file ;
  86.     {
  87.     char cur_path[200], sav_dir[80], *dir, *ptr, program[80] ;
  88.     static char path[80] ;
  89.     int sav_drv ;
  90.     PRIVATE char *Strip_Drive() ;
  91.     PRIVATE VOID Get_Cur_Path(), Get_Cur_Dir() ;
  92.     PRIVATE VOID Set_Cur_Drv(), Set_Cur_Dir() ;
  93.     PRIVATE int Get_Cur_Drv() ;
  94.     PRIVATE BOOLEAN Found() ;
  95.  
  96.     /* convert program name to upper case */
  97.     strcpy(program, file) ;
  98.     for (ptr = program; *ptr; ptr++)
  99.         {
  100.         *ptr = toupper(*ptr) ;
  101.         }
  102.  
  103.     /* look in current directory first */
  104.     if (Found(program))
  105.         {
  106.         strcpy(path, program) ;
  107.         return path ;
  108.         }
  109.  
  110.     /* save context */
  111.     Get_Cur_Dir(sav_dir) ;
  112.     sav_drv = Get_Cur_Drv() ;
  113.  
  114.     /* look for COMSPEC in environment */
  115.     if (!strcmp(program, "COMMAND.COM") && (dir = Get_Env_Str("COMSPEC")))
  116.         {
  117.         if (Found(dir))
  118.             {
  119.             strcpy(path, dir) ;
  120.             Set_Cur_Drv(sav_drv) ;
  121.             Set_Cur_Dir(sav_dir) ;
  122.             return path ;
  123.             }
  124.         }
  125.  
  126.     Get_Cur_Path(cur_path) ;
  127.     if (!*cur_path)
  128.         {
  129.         return 0 ;
  130.         }
  131.  
  132.     dir = cur_path ;
  133.     do
  134.         {
  135.         if (ptr = index(dir, ';'))
  136.             {
  137.             *ptr++ = NULL ;
  138.             }
  139.         dir = Strip_Drive(dir, sav_drv) ;
  140.  
  141.         if (*dir == '\\')
  142.             {
  143.             Set_Cur_Dir("\\") ;
  144.             dir++ ;
  145.             }
  146.         else
  147.             {
  148.             Set_Cur_Dir(sav_dir) ;
  149.             }
  150.         Set_Cur_Dir(dir) ;
  151.  
  152.         if (Found(program))
  153.             {
  154.             path[0] = 'A' + Get_Cur_Drv() ;
  155.             path[1] = ':' ;
  156.             Get_Cur_Dir(&path[2]) ;
  157.             if (path[3])
  158.                 {
  159.                 strcat(path, "\\") ;
  160.                 }
  161.             strcat(path, program) ;
  162.             Set_Cur_Drv(sav_drv) ;
  163.             Set_Cur_Dir(sav_dir) ;
  164.             return path ;
  165.             }
  166.         dir = ptr ;
  167.         }
  168.     while (*ptr) ;
  169.  
  170.     /* restore context */
  171.     Set_Cur_Drv(sav_drv) ;
  172.     Set_Cur_Dir(sav_dir) ;
  173.  
  174.     return 0 ;
  175.     }
  176.  
  177. PRIVATE VOID Get_Cur_Path(cur_path)
  178. char *cur_path ;
  179.     {
  180.     PRIVATE char *Get_Env_Str() ;
  181.     char *ptr ;
  182.  
  183.     *cur_path = NULL ;
  184.     if (ptr = Get_Env_Str("PATH"))
  185.         {
  186.         strcpy(cur_path,ptr) ;
  187.         }
  188.     }
  189.  
  190. PRIVATE char *Get_Env_Str(var)
  191. char *var ;
  192.     {
  193.     PUBLIC VOID chain() ;
  194.     unsigned offset = 0, env_seg, *psp_ptr ;
  195.     char *ptr ;
  196.     static char bfr[200] ;
  197.  
  198. /* The following cryptic line of code was gleaned from debugging 
  199.    the file EXEC.O from the DeSmet C Compiler package...
  200. */
  201.     psp_ptr = (_peek(chain + 0x11, _showcs()) << 8) |
  202.           _peek(chain + 0x10, _showcs()) ;
  203.  
  204.     env_seg = (_peek(0x2D, *psp_ptr) << 8) | _peek(0x2C, *psp_ptr) ;
  205.  
  206.     while (_peek(offset, env_seg))
  207.         {
  208.         ptr = bfr ;
  209.         do /* copy string */
  210.             {
  211.             } while (*ptr++ = toupper(_peek(offset++, env_seg))) ;
  212.         *(ptr = index(bfr, '=')) = NULL ;
  213.         if (!strcmp(bfr, var))
  214.             {
  215.             return ptr + 1 ;
  216.             }
  217.         }
  218.     return 0 ;
  219.     }
  220.  
  221. PRIVATE VOID Get_Cur_Dir(bfr)
  222. char *bfr ;
  223.     {
  224.     PUBLIC int _rax, _rdx, _rds, _rsi ;
  225.  
  226.     *bfr = '\\' ;
  227.     _rax = 0x4700 ;
  228.     _rsi = bfr + 1 ;
  229.     _rds = _showds() ;
  230.     _rdx = 0 ;
  231.     _doint(0x21) ;
  232.     }
  233.  
  234. PRIVATE VOID Set_Cur_Dir(dir)
  235. char *dir ;
  236.     {
  237.     PUBLIC int _rax, _rdx, _rds ;
  238.  
  239.     _rax = 0x3B00 ;
  240.     _rdx = dir ;
  241.     _rds = _showds() ;
  242.     _doint(0x21) ;
  243.     }
  244.  
  245. PRIVATE int Get_Cur_Drv()
  246.     {
  247.     PUBLIC int _rax ;
  248.  
  249.     _rax = 0x1900 ;
  250.     _doint(0x21) ;
  251.     return _rax & 0xFF ;
  252.     }
  253.  
  254. PRIVATE VOID Set_Cur_Drv(drive)
  255. unsigned drive ;
  256.     {
  257.     PUBLIC int _rax, _rdx ;
  258.  
  259.     _rax = 0x0E00 ;
  260.     _rdx = drive ;
  261.     _doint(0x21) ;
  262.     }
  263.  
  264. PRIVATE BOOLEAN Found(file)
  265. char *file ;
  266.     {
  267.     PUBLIC int _rax, _rdx, _rcx, _rds, _carryf ;
  268.     char bfr[64] ;
  269.  
  270.     _rax = 0x1A00 ;
  271.     _rds = _showds() ;
  272.     _rdx = bfr ;
  273.     _doint(0x21) ;
  274.  
  275.     _rax = 0x4E00 ;
  276.     _rds = _showds() ;
  277.     _rdx = file ;
  278.     _rcx = 0 ;
  279.     _doint(0x21) ;
  280.  
  281.     return ! _carryf ;
  282.     }
  283.  
  284. PRIVATE char *Strip_Drive(dir, drv)
  285. char *dir ;
  286. int drv ;
  287.     {
  288.     PRIVATE Set_Cur_Drv() ;
  289.  
  290.     if (dir[1] == ':')
  291.         {
  292.         Set_Cur_Drv(toupper(*dir) - 'A') ;
  293.         dir += 2 ;
  294.         }
  295.     else
  296.         {
  297.         Set_Cur_Drv(drv) ;
  298.         }
  299.     return dir ;
  300.     }
  301.