home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / m / mawk11as.zip / MSDOS / DOSEXEC.C < prev    next >
C/C++ Source or Header  |  1991-12-18  |  3KB  |  165 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.2  91/11/16  10:27:18  brennan
  15.  * BINMODE 
  16.  * 
  17.  * Revision 1.1  91/10/29  09:45:56  brennan
  18.  * Initial revision
  19.  * 
  20. */
  21.  
  22. /* system() and pipes() for MSDOS */
  23.  
  24. #include "mawk.h"
  25.  
  26. #if MSDOS
  27. #include "memory.h"
  28. #include "files.h"
  29. #include "fin.h"
  30.  
  31. #include <process.h>
  32.  
  33. static void PROTO(get_shell, (void)) ;
  34.  
  35. static char *shell ;     /* e.g.   "c:\\sys\\command.com"  */
  36. static char *command_opt ;  /*  " /c"  */
  37.  
  38. static void get_shell()
  39. { char *s , *p ;
  40.   int len ;
  41.  
  42.   if ( !(s = getenv("MAWKSHELL")) )
  43.   {
  44.     errmsg(0, "MAWKSHELL not set in environment") ; exit(1) ;
  45.   }
  46.   p = s ;
  47.   while ( *p != ' ' && *p != '\t' ) p++ ;
  48.   len = p - s ;
  49.   shell = (char *) zmalloc( len + 1 ) ;
  50.   (void) memcpy(shell, s, len) ;  shell[len] = 0 ;
  51.   command_opt = p ;
  52. }
  53.  
  54.  
  55. /* large model use spawnl() so we don't have to understand
  56.    far memory management
  57.  
  58.    small model use assembler routine xDOSexec() to save code
  59.    space
  60. */
  61.  
  62. int DOSexec( command )
  63.   char *command ;
  64. {
  65. #if  SM_DOS
  66.   extern int PROTO(xDOSexec, (char *, void*)) ;
  67.   struct {
  68.     int env_seg ;
  69.     char far *cmdline ;
  70.     unsigned fcb[4] ;
  71.   } block ;
  72.   int len ;
  73. #endif
  74.  
  75.   char xbuff[256] ;
  76.  
  77.   if ( ! shell )  get_shell() ;
  78.  
  79.   (void) sprintf(xbuff+1, "%s %s", command_opt, command) ;
  80.  
  81. #if  SM_DOS
  82.   block.env_seg = 0 ;
  83.   block.cmdline = xbuff ;
  84.   (void) memset(block.fcb, 0xff, sizeof(block.fcb)) ;
  85.   len = strlen(xbuff+1) ;
  86.   if ( len > 126 ) len = 126 ;
  87.   xbuff[0] = len ;
  88.   xbuff[len+1] = '\r' ;
  89. #endif
  90.  
  91.   (void) fflush(stderr) ; (void) fflush(stdout) ;
  92.  
  93. #if SM_DOS
  94.   return xDOSexec(shell, &block) ;
  95. #else
  96.   return   spawnl(P_WAIT, shell, shell, xbuff+1, (char *) 0 ) ;
  97. #endif
  98. }
  99.  
  100.  
  101. static int next_tmp ;
  102. static char *tmpdir ;
  103.  
  104. /* put the name of a temp file in string buff */
  105.  
  106. char *tmp_file_name( id )
  107.   int id ;
  108. {
  109.   (void) sprintf(string_buff, "%sMAWK%04X.TMP", tmpdir, id) ;
  110.   return string_buff ;
  111. }
  112.  
  113. PTR  get_pipe( command, type, tmp_idp)
  114.   char *command ;
  115.   int type, *tmp_idp ;
  116. {
  117.   PTR  retval ;
  118.   char *tmpfile ;
  119.  
  120.   if ( ! tmpdir )
  121.   {
  122.     tmpdir = getenv("MAWKTMPDIR") ;
  123.     if ( ! tmpdir )  tmpdir = "" ;
  124.   }
  125.  
  126.   *tmp_idp = next_tmp ;
  127.   tmpfile = tmp_file_name(next_tmp) ;
  128.  
  129.   if ( type == PIPE_OUT )  
  130.       retval = (PTR) fopen(tmpfile, (binmode()&2)? "wb":"w") ;
  131.   else
  132.   { char xbuff[256] ;
  133.     
  134.     sprintf(xbuff, "%s > %s" , command, tmpfile) ;
  135.     DOSexec(xbuff) ;
  136.     retval = (PTR) FINopen(tmpfile, 0) ;
  137.   }
  138.  
  139.   next_tmp++ ;
  140.   return retval ;
  141. }
  142.  
  143. /* closing a fake pipes involves running the out pipe 
  144.    command
  145. */
  146.  
  147. int close_fake_outpipe(command, tid)
  148.   char *command ;
  149.   int tid ; /* identifies the temp file */
  150. {
  151.   char *tmpname = tmp_file_name(tid) ;
  152.   char xbuff[256] ;
  153.   int retval ;
  154.  
  155.   sprintf(xbuff, "%s < %s", command, tmpname) ;
  156.   retval = DOSexec(xbuff) ;
  157.   (void) unlink(tmpname) ;
  158.   return retval ;
  159. }
  160.  
  161. #endif  /* MSDOS */
  162.  
  163.  
  164.  
  165.