Hi all

I need to have run command with standard input, std. output and error output which I use in past. After discover found hb_process* functions and find that are little difficult to use. Attached is syscmd.prg and is good example of using hb_process* functions. I get inspired with uhttpd. Be free to use or include this in harbour if find useful

best regards
Davor




#include "fileio.ch"

/*

Function SYSCMD()

SYSCMD(<cCmd>, <cStdin>, @<cStdOut>, @<cStdError>)      --> <nResultCode>

SYSCMD() runs system command <cCmd> and sends to this command string <sStdin> 
with a  standart input. Standard output is writen to cStdOut and error output 
is writen to cStdError



        <cCmd> String, is the command to execute 

        <cStdIn> String, passed to stdinput 

        <cStdout> String, is the string with the standard output of executed 
command

        <cStdError> Strung, is the string with the error output of executet 
command 

Returns:

        Returns -1 on error or returns errorcode from OS

*/


#ifdef TEST

PROC main( )

   LOCAL cOut := "", cErr := "", nRet, cSecOut := ""

   nRet := syscmd( "ls -l", , @cOut )
   ? cOut
   nRet := syscmd( "grep prg ", cOut, @cSecOut, @cErr )
   ? "nRet", nRet
   ? "cOut", cSecOut
   ? "cErr:",  cErr
   ?

   RETURN

#endif



FUNCTION syscmd( cCmd, cStdIn, cStdOut, cStdErr )

   LOCAL nErrorLevel := 0
   LOCAL hProc, hStdIn, hStdOut, hStdErr

   hProc := hb_ProcessOpen( cCmd, @hStdIn, @hStdOut, @hStdErr )
   IF hProc == F_ERROR
      nErrorLevel := F_ERROR
   ELSE
      IF cStdIn != NIL
         FWrite( hStdIn, cStdIn )
         FClose( hStdIn )
      ENDIF
      IF cStdOut != NIL
         cStdOut := ReadStd( hStdOut ) 
      ENDIF
      IF cStdErr != NIL
         cStdErr := ReadStd( hStdErr )
      ENDIF
      nErrorLevel := hb_processvalue( hProc )
      hb_processclose( hProc, .T. )
   ENDIF

   RETURN nErrorLevel

STATIC FUNCTION ReadStd( hHandle )

   LOCAL cBuffer := Space ( 200 )
   LOCAL cOutPut := ""
   LOCAL nLen

   DO WHILE ( nLen := FRead( hHandle, @cBuffer, Len( cBuffer ) ) ) > 0
      cOutPut += SubStr( cBuffer, 1, nLen )
      cBuffer := Space( 200 )
   ENDDO
   FClose ( hHandle )

   RETURN AllTrim( cOutPut )

_______________________________________________
Harbour mailing list
[email protected]
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to