home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / MAWK113.ZIP / mawk113 / msdos / dosexec.c < prev    next >
C/C++ Source or Header  |  1992-12-05  |  4KB  |  201 lines

  1.  
  2. /********************************************
  3. dosexec.h
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log: dosexec.c,v $
  14.  * Revision 1.4  1992/12/05  22:29:43  mike
  15.  * dos patch 112d:
  16.  * don't use string_buff
  17.  * check COMSPEC
  18.  *
  19.  * Revision 1.3  1992/07/10  16:21:57  brennan
  20.  * store exit code of input pipes
  21.  *
  22.  * Revision 1.2  1991/11/16  10:27:18  brennan
  23.  * BINMODE
  24.  *
  25.  * Revision 1.1  91/10/29  09:45:56  brennan
  26.  * Initial revision
  27.  * 
  28. */
  29.  
  30. /* system() and pipes() for MSDOS */
  31.  
  32. #include "mawk.h"
  33.  
  34. #if MSDOS
  35. #include "memory.h"
  36. #include "files.h"
  37. #include "fin.h"
  38.  
  39. #include <process.h>
  40.  
  41. static void PROTO(get_shell, (void)) ;
  42.  
  43. static char *shell ;     /* e.g.   "c:\\sys\\command.com"  */
  44. static char *command_opt ;  /*  " /c"  */
  45.  
  46. static void get_shell()
  47. { char *s , *p ;
  48.   int len ;
  49.  
  50.   if ( s = getenv("MAWKSHELL") )
  51.   {
  52.     /* break into shell part and option part */
  53.     p = s ;
  54.     while ( *p != ' ' && *p != '\t' ) p++ ;
  55.     len = p - s ;
  56.     shell = (char *) zmalloc(len+1) ;
  57.     memcpy(shell, s, len) ; shell[len] = 0 ;
  58.     command_opt = p ;
  59.   }
  60.   else
  61.   if ( s = getenv("COMSPEC") )
  62.   {
  63.     shell = s ;
  64.     command_opt = " /c" ;
  65.     /* leading space needed because of bug in command.com */
  66.   }
  67.   else
  68.   {
  69.     errmsg(0,
  70.     "cannot exec(), must set MAWKSHELL or COMSPEC in environment" ) ;
  71.     exit(1) ;
  72.   }
  73. }
  74.  
  75.  
  76. /* large model use spawnl() so we don't have to understand
  77.    far memory management
  78.  
  79.    small model use assembler routine xDOSexec() to save code
  80.    space
  81. */
  82.  
  83. int DOSexec( command )
  84.   char *command ;
  85. {
  86. #if  SM_DOS
  87.   extern int PROTO(xDOSexec, (char *, void*)) ;
  88.   struct {
  89.     int env_seg ;
  90.     char far *cmdline ;
  91.     unsigned fcb[4] ;
  92.   } block ;
  93.   int len ;
  94. #endif
  95.  
  96.   char xbuff[256] ;
  97.  
  98.   if ( ! shell )  get_shell() ;
  99.  
  100.   (void) sprintf(xbuff+1, "%s %s", command_opt, command) ;
  101.  
  102. #if  SM_DOS
  103.   block.env_seg = 0 ;
  104.   block.cmdline = xbuff ;
  105.   (void) memset(block.fcb, 0xff, sizeof(block.fcb)) ;
  106.   len = strlen(xbuff+1) ;
  107.   if ( len > 126 ) len = 126 ;
  108.   xbuff[0] = len ;
  109.   xbuff[len+1] = '\r' ;
  110. #endif
  111.  
  112.   (void) fflush(stderr) ; (void) fflush(stdout) ;
  113.  
  114. #if SM_DOS
  115.   return xDOSexec(shell, &block) ;
  116. #else
  117.   return   spawnl(P_WAIT, shell, shell, xbuff+1, (char *) 0 ) ;
  118. #endif
  119. }
  120.  
  121.  
  122. static int next_tmp ;  /* index for naming temp files */
  123. static char *tmpdir ;  /* directory to hold temp files */
  124. static unsigned mawkid ; /* unique to this mawk process */
  125.  
  126.  
  127. /* compute the unique temp file name associated with id */
  128. char *tmp_file_name(id, buffer )
  129.   int id ;
  130.   char *buffer ;
  131. {
  132.   if ( mawkid == 0 )
  133.   {
  134.     /* first time */
  135.     union {
  136.     void far *ptr ;
  137.     unsigned w[2] ;
  138.     } xptr ;
  139.  
  140.     xptr.ptr = (void far*)&mawkid ;
  141.     mawkid = xptr.w[1] ;
  142.  
  143.     tmpdir = getenv("MAWKTMPDIR") ;
  144.     if ( ! tmpdir || strlen(tmpdir) > 80 ) tmpdir = "" ;
  145.   }
  146.  
  147.   (void) sprintf(buffer, "%sMAWK%04X.%03X",tmpdir, mawkid, id) ;
  148.   return buffer ;
  149. }
  150.  
  151. /* open a pipe, returning a temp file identifier by
  152.    reference
  153. */
  154.  
  155. PTR  get_pipe( command, type, tmp_idp)
  156.   char *command ;
  157.   int type, *tmp_idp ;
  158. {
  159.   PTR  retval ;
  160.   char xbuff[256] ;
  161.   char *tmpname ;
  162.  
  163.  
  164.   *tmp_idp = next_tmp ;
  165.   tmpname = tmp_file_name(next_tmp, xbuff+163) ;
  166.  
  167.   if ( type == PIPE_OUT )  
  168.   {
  169.     retval = (PTR) fopen(tmpname, (binmode()&2)? "wb":"w") ;
  170.   }
  171.   else
  172.   { 
  173.     sprintf(xbuff, "%s > %s" , command, tmpname) ;
  174.     tmp_idp[1] = DOSexec(xbuff) ;
  175.     retval = (PTR) FINopen(tmpname, 0) ;
  176.   }
  177.  
  178.   next_tmp++ ;
  179.   return retval ;
  180. }
  181.  
  182. /* closing a fake pipes involves running the out pipe 
  183.    command
  184. */
  185.  
  186. int close_fake_outpipe(command, tid)
  187.   char *command ;
  188.   int tid ; /* identifies the temp file */
  189. {
  190.   char xbuff[256] ;
  191.   char *tmpname = tmp_file_name(tid, xbuff+163) ;
  192.   int retval ;
  193.  
  194.   sprintf(xbuff, "%s < %s", command, tmpname) ;
  195.   retval = DOSexec(xbuff) ;
  196.   (void) unlink(tmpname) ;
  197.   return retval ;
  198. }
  199.  
  200. #endif  /* MSDOS */
  201.