capture.ew help file

library capture.ew

DESCRIPTION: Library to simplify capture of standard i/o handles of a child process.
PLATFORM: windows
AUTHOR: Jacques Deschênes
DATE: 2007-09-23

interface

  • global constants
  • global procedures and functions
  • notes

    global constants

    constants used by option parameter of capture_exec() function.

    CAPTURE_STDIN capture child process standard input (keyboard, handle 0).
    CAPTURE_STDOUT capture child process standard output (console output, handle 1).
    CAPTURE_STDERR capture child process stderr (console errors, handle 2).

    constants used to access handles sequence returned by capture_exec() function.

    siSTDIN index to get handle used when writting to child process standard input.
    siSTDOUT index to get handle used when reading child process standard output.
    siSTDERR index to get handle used when reading child process standard error.
    back to table of content


    global functions and procedures.

    FUNCTION: capture_exec(sequence cmd_arg, integer option)
    DESCRIPTON: Create a child process and capture its console i/o
    ARGUMENTS:

    OUTPUT: sequence, contain {hChildStdinWr,hChildStdoutRd,hChildStdErrRd} or 0 if an error occured.
    EXAMPLE:
    object handles,line
    sequence dir_capture
    handles = capture_exec("cmd /c dir *.exw",CAPTURE_STDOUT) -- capture dir command output.
    dir_capture={}
    line = capture_gets(handles[siSTDOUT])
    while sequence(line) do
    dir_capture = append(dir_capture,line)
    line = capture_gets(handles[siSTDOUT])
    end while
    back to table of content


    FUNCTION: capture_gets(atom hReadFromPipe)
    DESCRIPTION: read a line of text from captured stdout or stderr
    ARGUMENTS:

    OUTPUT: object, if success return sequence of character else return 0
    EXAMPLE:
    object line
    line = capture_gets(handles[siSTDOUT])
    back to table of content


    PROCEDURE: capture_puts(atom hWriteToPipe, object data)
    DESCRIPTION: write a char or string to a pipe.
    ARGUMENTS:

    EXAMPLE:
    capture_puts(handles[siSTDIN],"hello world\n")
    back to table of content


    PROCEDURE: capture_terminate()
    DESCRIPTION: close all pipes and terminate child process if still active
    ARGUMENTS: none


    PROCEDURE: capture_close_handle(atom hPipe)
    DESCRIPTION: close a pipe handle
    ARGUMENTS:

    EXAMPLE:
    capture_close_handle(handles[siSTDIN])--close handle of standard input capture.
    back to table of content


    usage notes

    1. Writing to child process
      handles[siSTDIN] is the only handle that can be used with capture_puts()

    2. internals comamnds
      To capture output of commands internal to cmd.exe or command.exe, like dir,set,del,etc, cmd_arg string of capture_exec() must include the command shell itself.
      For example: handles=capture_exec("dir *.ew",CAPTURE_STDOUT) will fail.
      Instead you must write: handles=capture_exec("cmd.exe /c dir *.ew",CAPTURE_STDOUT)

    3. capturing standard input
      When capturing standard input and output (or error) in the same capture the handle handles[siSTDIN] must be closed before reading from handles[siSTDOUT] or handles[siSTDERR], otherwise capture_gets() will never return.
      example:
      object handles, line
      atom hStdInPipeWr
      handles = capture_exec("cmd.exe /c sort",CAPTURE_STDIN+CAPTURE_STDOUT)
      hStdInPipeWr = handles[siSTDIN]
      capture_puts(hStdInPipeWr,"cba\r\n")
      capture_puts(hStdInPipeWr,"bac\r\n")
      capture_puts(hStdInPipeWr,"acb\r\n")
      capture_close_handle(hStdInPipeWr)--must be close before proceeding with capture_gets()
      line = capture_gets(handles[siSTDOUT])
    back to table of content