pike.git / lib / modules / Process.pmod

version» Context lines:

pike.git/lib/modules/Process.pmod:489:   //! having to make sure you read nonblocking yourself.   //!   //! @param args   //! Either a command line array, as the command_args   //! argument to @[create_process()], or a string that   //! will be splitted into a command line array by   //! calling @[split_quoted_string()] in an operating   //! system dependant mode.   //! @param modifiers   //! It takes all the modifiers @[Process.Process] accepts, with - //! the exception of stdout and stderr. Since the point of this - //! function is to handle those you can not supply your own. + //! the exception of stdout and stderr. Each must be either absent, or + //! a function accepting a string; if present, the functions will be called + //! whenever output is made on the corresponding stream, otherwise the data + //! will be collected and returned in the result mapping.   //!   //! If @expr{modifiers->stdin@} is set to a string it will automatically be   //! converted to a pipe that is fed to stdin of the started process.   //!   //! @seealso   //! @[Process.Process] @[create_process]   //!   //! @returns   //! @mapping   //! @member string "stdout" - //! Everything the process wrote on stdout. + //! Everything the process wrote on stdout, unless a stdout function was + //! provided.   //! @member string "stderr" - //! Everything the process wrote on stderr. + //! Everything the process wrote on stderr, similarly.   //! @member int "exitcode"   //! The process' exitcode.   //! @endmapping   //!   //! @note   //! As the entire output of stderr and stdout is stored in the   //! returned mapping it could potentially grow until memory runs out.   //! It is therefore advisable to set up rlimits if the output has a - //! potential to be very large. + //! potential to be very large, or else provide functions to handle + //! partial data.   //!   //! @example   //! Process.run( ({ "ls", "-l" }) );   //! Process.run( ({ "ls", "-l" }), ([ "cwd":"/etc" ]) );   //! Process.run( "ls -l" );   //! Process.run( "awk -F: '{print $2}'", ([ "stdin":"foo:2\nbar:17\n" ]) );   mapping run(string|array(string) cmd, void|mapping modifiers)   {    string gotstdout="", gotstderr="", stdin_str;    int exitcode;       if(!modifiers)    modifiers = ([]);    -  if(modifiers->stdout || modifiers->stderr) +  if((modifiers->stdout && !functionp(modifiers->stdout)) +  || (modifiers->stderr && !functionp(modifiers->stderr)))    throw( ({ "Can not redirect stdout or stderr in Process.run, "    "please use Process.Process instead.", backtrace() }) );       Stdio.File mystdout = Stdio.File();    Stdio.File mystderr = Stdio.File();    Stdio.File mystdin;       object p;    if(stringp(modifiers->stdin))    {
pike.git/lib/modules/Process.pmod:576: Inside #if 0 //constant(Thread.Thread)
      exitcode = p->wait();    threads->wait();   #else    Pike.SmallBackend backend = Pike.SmallBackend();       mystdout->set_backend (backend);    mystderr->set_backend (backend);       mystdout->set_read_callback( lambda( mixed i, string data) { -  gotstdout += data; +  if (modifiers->stdout) modifiers->stdout(data); +  else gotstdout += data;    } );    mystderr->set_read_callback( lambda( mixed i, string data) { -  gotstderr += data; +  if (modifiers->stderr) modifiers->stderr(data); +  else gotstderr += data;    } );    mystdout->set_close_callback( lambda () {    mystdout->set_read_callback(0);    catch { mystdout->close(); };    mystdout = 0;    });    mystderr->set_close_callback( lambda () {    mystderr->set_read_callback(0);    catch { mystderr->close(); };    mystderr = 0;