c2a4061997-02-06Fredrik Hübinette (Hubbe) #define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
d5190b1997-02-10Fredrik Hübinette (Hubbe) import Stdio;
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
dc50841997-11-05Henrik Grubbström (Grubba) // static private inherit File : file;
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
c4b5791998-02-28Henrik Grubbström (Grubba) #if !constant(strerror) #define strerror(X) ("errno="+X) #endif
c2a4061997-02-06Fredrik Hübinette (Hubbe) varargs int exec(string file,string ... foo) {
bcfe4f1997-06-09John W. Pierce  if (sizeof(file)) { string path;
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bcfe4f1997-06-09John W. Pierce  if(search(file,"/") >= 0) return exece(combine_path(getcwd(),file),foo,getenv());
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
b6277d1997-06-10Fredrik Hübinette (Hubbe)  path=getenv("PATH");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
b6277d1997-06-10Fredrik Hübinette (Hubbe)  foreach(path ? path/":" : ({}) , path)
bcfe4f1997-06-09John W. Pierce  if(file_stat(path=combine_path(path,file))) return exece(path, foo,getenv()); }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  return 69; }
dc50841997-11-05Henrik Grubbström (Grubba) varargs int spawn(string s,object stdin,object stdout,object stderr, function|void cleanup, mixed ... args)
c2a4061997-02-06Fredrik Hübinette (Hubbe) { int pid; pid=fork(); if(pid==-1) error("No more processes.\n"); if(pid) { return pid; }else{
f95b701997-05-21Fredrik Hübinette (Hubbe)  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);
dc50841997-11-05Henrik Grubbström (Grubba)  if (cleanup) { cleanup(@args); }
c2a4061997-02-06Fredrik Hübinette (Hubbe)  exec("/bin/sh","-c",s); exit(69); } } string popen(string s) { object p; string t;
dc50841997-11-05Henrik Grubbström (Grubba)  object f = File(); if (!f) error("Popen failed. (couldn't create pipe)\n");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
dc50841997-11-05Henrik Grubbström (Grubba)  p=f->pipe();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  if(!p) error("Popen failed. (couldn't create pipe)\n");
dc50841997-11-05Henrik Grubbström (Grubba)  spawn(s,0,p,0, destruct, f);
ef293f1997-05-07Henrik Grubbström (Grubba)  p->close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  destruct(p);
dc50841997-11-05Henrik Grubbström (Grubba)  t=f->read(0x7fffffff);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  if(!t) { int e;
dc50841997-11-05Henrik Grubbström (Grubba)  e=f->errno(); f->close(); destruct(f);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  error("Popen failed with error "+e+".\n");
dc50841997-11-05Henrik Grubbström (Grubba)  } else { f->close(); destruct(f);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  } return t; } void system(string s) { object p; int pid; string t;
dc50841997-11-05Henrik Grubbström (Grubba)  object f; f = File(); if (!f) error("System failed.\n");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
dc50841997-11-05Henrik Grubbström (Grubba)  p=f->pipe();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  if(!p) error("System() failed.\n"); p->set_close_on_exec(0); if(pid=fork()) {
dc50841997-11-05Henrik Grubbström (Grubba)  p->close();
c2a4061997-02-06Fredrik Hübinette (Hubbe)  destruct(p); /* Nothing will ever be written here, we are just waiting for it * to close */
dc50841997-11-05Henrik Grubbström (Grubba)  f->read(1);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  }else{
dc50841997-11-05Henrik Grubbström (Grubba)  f->close(); destruct(f);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  exec("/bin/sh","-c",s); exit(69); } }
cb119a1997-04-07Fredrik Hübinette (Hubbe)  constant fork = predef::fork; constant exece = predef::exece;
df67f71997-12-14Mirar (Pontus Hagland)  class Spawn { object stdin; object stdout; object stderr; array(object) fd; int pid; private int low_spawn(array(void|object(Stdio.File)) fdp, array(void|object(Stdio.File)) fd_to_close, string cmd, void|string *args, void|mapping(string:string) env, string|void cwd) { object(Stdio.File) pie,pied; /* interprocess communication */ int pid; pie=Stdio.File(); pied=pie->pipe(); if(!(pid=fork())) { mixed err=catch { if(cwd && !cd(cwd)) { throw(({"pike: cannot change cwd to "+cwd+ ": "+strerror(errno())+"\n", backtrace()})); } if (sizeof(fdp)>0 && fdp[0]) fdp[0]->dup2(Stdio.File("stdin")); if (sizeof(fdp)>1 && fdp[1]) fdp[1]->dup2(Stdio.File("stdout")); if (sizeof(fdp)>2 && fdp[2]) fdp[2]->dup2(Stdio.File("stderr")); /* dup2 fdd[3..] here FIXME FIXME */ foreach (fd_to_close,object f) if (objectp(f)) { f->close(); destruct(f); } pie->close(); destruct(pie); pied->set_close_on_exec(1); if (env) exece(cmd,args||({}),env); else exece(cmd,args||({})); throw(({"pike: failed to exece "+cmd+ ": "+strerror(errno())+"\n", backtrace()})); }; pied->write(encode_value(err)); exit(1); } foreach (fdp,object f) if (objectp(f)) { f->close(); destruct(f); } pied->close(); destruct(pied); mixed err=pie->read(); if (err && err!="") throw(decode_value(err)); pie->close(); destruct(pie); return pid; } void create(string cmd, void|array(string) args, void|mapping(string:string) env, string|void cwd, void|array(object(Stdio.File)|void) ownpipes, void|array(object(Stdio.File)|void) fds_to_close) { if (!ownpipes) { stdin=Stdio.File(); stdout=Stdio.File(); stderr=Stdio.File(); fd=({stdin->pipe(),stdout->pipe(),stderr->pipe()}); fds_to_close=({stdin,stdout,stderr}); } else { fd=ownpipes; if (sizeof(fd)>0) stdin=fd[0]; else stdin=0; if (sizeof(fd)>1) stdout=fd[1]; else stdout=0; if (sizeof(fd)>2) stderr=fd[2]; else stderr=0; } pid=low_spawn(fd,fds_to_close||({}),cmd,args,env,cwd); } int kill(int signal) { return predef::kill(pid,signal); } void wait() { while (kill(0)) sleep(0.01); } // void set_done_callback(function foo,mixed ... args); // int result(); // array rusage(); }