pike.git / lib / modules / Process.pmod

version» Context lines:

pike.git/lib/modules/Process.pmod:168:   //! @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 therefor adviceable to set up rlimits if the output has a   //! potientially be very large.   //!   //! @example   //! Process.run( ({ "ls", "-l" }) ); - //! 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=""; +  string gotstdout="", gotstderr="", stdin_str;    int exitcode;       if(!modifiers)    modifiers = ([]);       if(modifiers->stdout || modifiers->stderr)    throw( ({ "Can not redirect stdout or stderr in run_process, "    "please use Process.Process instead.", backtrace() }) );       Stdio.File mystdout = Stdio.File();    Stdio.File mystderr = Stdio.File();    -  object p = Process(cmd, modifiers + ([ +  object p; +  if(modifiers->stdin && stringp(modifiers->stdin)) +  { +  Stdio.File mystdin = Stdio.File(); +  stdin_str = modifiers->stdin; +  p = Process(cmd, modifiers + ([    "stdout":mystdout->pipe(),    "stderr":mystderr->pipe(), -  +  "stdin":mystdin->pipe(Stdio.PROP_IPC|Stdio.PROP_REVERSE)    ])); -  +  Shuffler.Shuffle sf = Shuffler.Shuffler()->shuffle( mystdin ); +  sf->add_source(stdin_str); +  sf->start(); +  } +  else +  p = Process(cmd, modifiers + ([ +  "stdout":mystdout->pipe(), +  "stderr":mystderr->pipe(), +  ]));      #if constant(Thread.Thread)    array readthreads = ({    thread_create( lambda() { gotstdout = mystdout->read(); } ),    thread_create( lambda() { gotstderr = mystderr->read(); } )    });    -  +  +  if(stdin_str) +  while( !p->status() || p->status() == 1 ) +  Pike.DefaultBackend( 1.0 );    exitcode = p->wait();    readthreads->wait();   #else //No threads, use callbacks    mystdout->set_read_callback( lambda( mixed i, string data) {    gotstdout += data;    } );    mystderr->set_read_callback( lambda( mixed i, string data) {    gotstderr += data;    } );    -  while( !p->status() ) // FIXME: What about "1" as in stopped? +  while( !p->status() || p->status() == 1 )    Pike.DefaultBackend( 1.0 );       mystdout->set_read_callback(0);    mystderr->set_read_callback(0);       gotstdout += mystdout->read();    gotstderr += mystderr->read();       exitcode = p->wait();   #endif