pike.git / lib / modules / Process.pmod

version» Context lines:

pike.git/lib/modules/Process.pmod:1:   #pike __REAL_VERSION__    - import Stdio; -  - #if !constant(strerror) - #define strerror(X) ("errno="+X) - #endif -  +    constant create_process = __builtin.create_process;    -  + //!   class Process   {    inherit __builtin.create_process;    -  +  //!    void create( string|array(string) args, mapping m )    {    if( stringp( args ) )    args = split_quoted_string( args );    if( m )    ::create( args, m );    else    ::create( args );    }   }    -  + //!   int exec(string file,string ... foo)   {    if (sizeof(file)) {    string path;       if(search(file,"/") >= 0)    return exece(combine_path(getcwd(),file),foo,getenv());       path=getenv("PATH");       foreach(path ? path/":" : ({}) , path)    if(file_stat(path=combine_path(path,file)))    return exece(path, foo,getenv());    }    return 69;   }      static array(string) search_path_entries=0; -  +  + //!   string search_path(string command)   {    if (command=="" || command[0]=='/') return command;       if (!search_path_entries)    {    array(string) e=(getenv("PATH")||"")/":"-({""});       multiset(string) filter=(<>);    search_path_entries=({});
pike.git/lib/modules/Process.pmod:72:    }    foreach (search_path_entries, string path)    {    string p=combine_path(path,command);    Stdio.Stat s=file_stat(p);    if (s && s->mode&0111) return p;    }    return 0;   }    + //!   string sh_quote(string s)   {    return replace(s,    ({"\\", "'", "\"", " "}),    ({"\\\\", "\\'", "\\\"","\\ "}));   }    -  + //!   array(string) split_quoted_string(string s)   {    sscanf(s,"%*[ \n\t]%s",s);    s=replace(s,    ({"\"", "'", "\\", " ", "\t", "\n"}),    ({"\0\"","\0'","\0\\","\0 ","\0\t","\0\n"}));    array(string) x=s/"\0";    array(string) ret=({x[0]});       for(int e=1;e<sizeof(x);e++)
pike.git/lib/modules/Process.pmod:143:    break;       default:    ret[-1]+="\0"+x[e];    break;    }    }    return ret;   }    + //!   Process spawn(string s,object|void stdin,object|void stdout,object|void stderr,    function|void cleanup, mixed ... args)   { - #if 1 +     mapping data=(["env":getenv()]);    if(stdin) data->stdin=stdin;    if(stdout) data->stdout=stdout;    if(stderr) data->stderr=stderr;   #if defined(__NT__)    // if the command string s is not quoted, add double quotes to    // make sure it is not modified by create_process    if (strlen(s) <= 1 || s[0] != '\"' || s[strlen(s)-1] != '\"')    s = "\"" + s + "\"";    return create_process(({ "cmd", "/c", s }),data);   #elif defined(__amigaos__)    return create_process(split_quoted_string(s),data);   #else /* !__NT__||__amigaos__ */    return create_process(({ "/bin/sh", "-c", s }),data);   #endif /* __NT__||__amigaos__ */ - #else +     -  Process pid; -  - #if constant(fork) -  pid=fork(); - #endif -  -  if(pid) -  { -  return pid; -  }else{ -  if(stdin ) stdin ->dup2(File("stdin")); -  if(stdout) stdout->dup2(File("stdout")); -  if(stderr) stderr->dup2(File("stderr")); -  -  if(stdin ) destruct(stdin); -  if(stdout) destruct(stdout); -  if(stderr) destruct(stderr); -  -  if (cleanup) { -  cleanup(@args); +    }    -  exec("/bin/sh","-c",s); -  exit(69); -  } - #endif - } -  + //!   string popen(string s)   {    object p;    string t; -  object f = File(); +  object f = Stdio.File();       if (!f) error("Popen failed. (couldn't create pipe)\n");       p=f->pipe(Stdio.PROP_IPC);    if(!p) error("Popen failed. (couldn't create pipe)\n");    spawn(s,0,p,0, destruct, f);    p->close();    destruct(p);       t=f->read(0x7fffffff);
pike.git/lib/modules/Process.pmod:221:    f->close();    destruct(f);    error("Popen failed with error "+e+".\n");    } else {    f->close();    destruct(f);    }    return t;   }    + //!   int system(string s)   {    return spawn(s)->wait();   }      #ifndef __NT__   #if constant(fork)   constant fork = predef::fork;   #endif      #if constant(exece)   constant exece = predef::exece;   #endif      #if constant(fork) -  +  + //!   class Spawn   {    object stdin;    object stdout;    object stderr;    array(object) fd;       object pid;       private object low_spawn(array(void|object(Stdio.File)) fdp,