a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
8f6e2e2001-01-19Per Hedbor constant create_process = __builtin.create_process;
970edb2002-08-09Martin Nilsson //!
8f6e2e2001-01-19Per Hedbor class Process { inherit __builtin.create_process;
970edb2002-08-09Martin Nilsson  //!
8f6e2e2001-01-19Per Hedbor  void create( string|array(string) args, mapping m ) { if( stringp( args ) ) args = split_quoted_string( args ); if( m ) ::create( args, m ); else ::create( args ); } }
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
970edb2002-08-09Martin Nilsson //!
8d49101998-01-21Fredrik Hübinette (Hubbe) int exec(string file,string ... foo)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
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; }
476ff22000-12-18Mirar (Pontus Hagland) static array(string) search_path_entries=0;
970edb2002-08-09Martin Nilsson  //!
476ff22000-12-18Mirar (Pontus Hagland) 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=({}); foreach (e,string s) { string t; if (s[0]=='~') // some shells allow ~-expansion in PATH { if (s[0..1]=="~/" && (t=getenv("HOME"))) s=t+s[1..]; else { // expand user? } } if (!filter[s] /* && directory exist */ ) { search_path_entries+=({s}); filter[s]=1; } } } 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; }
970edb2002-08-09Martin Nilsson //!
98bfd81998-04-16Fredrik Hübinette (Hubbe) string sh_quote(string s) { return replace(s, ({"\\", "'", "\"", " "}), ({"\\\\", "\\'", "\\\"","\\ "})); }
970edb2002-08-09Martin Nilsson //!
4755b72000-02-18Henrik Grubbström (Grubba) array(string) split_quoted_string(string s)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
52fa582000-07-23Fredrik Hübinette (Hubbe)  sscanf(s,"%*[ \n\t]%s",s);
be5bb72000-07-11Fredrik Hübinette (Hubbe)  s=replace(s, ({"\"", "'", "\\", " ", "\t", "\n"}), ({"\0\"","\0'","\0\\","\0 ","\0\t","\0\n"}));
4755b72000-02-18Henrik Grubbström (Grubba)  array(string) x=s/"\0"; array(string) ret=({x[0]});
8392231998-01-08Fredrik Hübinette (Hubbe)  for(int e=1;e<sizeof(x);e++) { switch(x[e][0]) { case '"': ret[-1]+=x[e][1..];
be5bb72000-07-11Fredrik Hübinette (Hubbe)  while(x[++e][0]!='"') { if(strlen(x[e])==1 && x[e][0]=='\\' && x[e+1][0]=='"') e++; ret[-1]+=x[e]; }
8392231998-01-08Fredrik Hübinette (Hubbe)  ret[-1]+=x[e][1..]; break; case '\'': ret[-1]+=x[e][1..];
e5fa3b2001-01-22Fredrik Hübinette (Hubbe)  while(x[++e][0]!='\'') ret[-1]+=x[e];
8392231998-01-08Fredrik Hübinette (Hubbe)  ret[-1]+=x[e][1..]; break; case '\\': if(strlen(x[e])>1) { ret[-1]+=x[e][1..]; }else{ ret[-1]+=x[++e]; } break; case ' ':
be5bb72000-07-11Fredrik Hübinette (Hubbe)  case '\t': case '\n':
f43b942000-07-23Fredrik Hübinette (Hubbe)  while(strlen(x[e])==1) { if(e+1 < sizeof(x)) { if((<' ','\t','\n'>) [x[e+1][0]]) e++; else break; }else{
be5bb72000-07-11Fredrik Hübinette (Hubbe)  return ret;
f43b942000-07-23Fredrik Hübinette (Hubbe)  } }
be5bb72000-07-11Fredrik Hübinette (Hubbe)  ret+=({x[e][1..]});
8392231998-01-08Fredrik Hübinette (Hubbe)  break; default: ret[-1]+="\0"+x[e]; break; } } return ret; }
970edb2002-08-09Martin Nilsson //!
8f6e2e2001-01-19Per Hedbor Process spawn(string s,object|void stdin,object|void stdout,object|void stderr, function|void cleanup, mixed ... args)
8392231998-01-08Fredrik Hübinette (Hubbe) {
c4a7311998-11-05Fredrik Hübinette (Hubbe)  mapping data=(["env":getenv()]);
8392231998-01-08Fredrik Hübinette (Hubbe)  if(stdin) data->stdin=stdin; if(stdout) data->stdout=stdout; if(stderr) data->stderr=stderr;
dc10f22001-10-12Tomas Nilsson #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__)
8392231998-01-08Fredrik Hübinette (Hubbe)  return create_process(split_quoted_string(s),data);
bf2b451998-11-23Marcus Comstedt #else /* !__NT__||__amigaos__ */
4ef98c1998-07-24Henrik Grubbström (Grubba)  return create_process(({ "/bin/sh", "-c", s }),data);
bf2b451998-11-23Marcus Comstedt #endif /* __NT__||__amigaos__ */
8392231998-01-08Fredrik Hübinette (Hubbe) 
c2a4061997-02-06Fredrik Hübinette (Hubbe) }
970edb2002-08-09Martin Nilsson //!
c2a4061997-02-06Fredrik Hübinette (Hubbe) string popen(string s) { object p; string t;
970edb2002-08-09Martin Nilsson  object f = Stdio.File();
dc50841997-11-05Henrik Grubbström (Grubba)  if (!f) error("Popen failed. (couldn't create pipe)\n");
c2a4061997-02-06Fredrik Hübinette (Hubbe) 
bcf9461998-01-27Fredrik Hübinette (Hubbe)  p=f->pipe(Stdio.PROP_IPC);
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; }
970edb2002-08-09Martin Nilsson //!
8392231998-01-08Fredrik Hübinette (Hubbe) int system(string s)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
8392231998-01-08Fredrik Hübinette (Hubbe)  return spawn(s)->wait();
c2a4061997-02-06Fredrik Hübinette (Hubbe) }
cb119a1997-04-07Fredrik Hübinette (Hubbe) 
782bca2000-09-16Per Hedbor #ifndef __NT__
8392231998-01-08Fredrik Hübinette (Hubbe) #if constant(fork)
04c34c1998-01-01Fredrik Hübinette (Hubbe) constant fork = predef::fork; #endif
cb119a1997-04-07Fredrik Hübinette (Hubbe) 
04c34c1998-01-01Fredrik Hübinette (Hubbe) #if constant(exece) constant exece = predef::exece; #endif
df67f71997-12-14Mirar (Pontus Hagland) 
04c34c1998-01-01Fredrik Hübinette (Hubbe) #if constant(fork)
970edb2002-08-09Martin Nilsson  //!
df67f71997-12-14Mirar (Pontus Hagland) class Spawn { object stdin; object stdout; object stderr; array(object) fd;
8392231998-01-08Fredrik Hübinette (Hubbe)  object pid;
df67f71997-12-14Mirar (Pontus Hagland) 
8392231998-01-08Fredrik Hübinette (Hubbe)  private object low_spawn(array(void|object(Stdio.File)) fdp, array(void|object(Stdio.File)) fd_to_close,
4755b72000-02-18Henrik Grubbström (Grubba)  string cmd, void|array(string) args,
8392231998-01-08Fredrik Hübinette (Hubbe)  void|mapping(string:string) env, string|void cwd)
df67f71997-12-14Mirar (Pontus Hagland)  { object(Stdio.File) pie,pied; /* interprocess communication */
8392231998-01-08Fredrik Hübinette (Hubbe)  object pid;
df67f71997-12-14Mirar (Pontus Hagland)  pie=Stdio.File(); pied=pie->pipe(); if(!(pid=fork())) { mixed err=catch { if(cwd && !cd(cwd)) {
a1c95e2001-12-20Martin Nilsson  error( "pike: cannot change cwd to "+cwd+ ": "+strerror(errno())+"\n" );
df67f71997-12-14Mirar (Pontus Hagland)  } 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||({}));
a1c95e2001-12-20Martin Nilsson  error( "pike: failed to exece "+cmd+ ": "+strerror(errno())+"\n" );
df67f71997-12-14Mirar (Pontus Hagland)  }; 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); }
8392231998-01-08Fredrik Hübinette (Hubbe) #if constant(kill)
df67f71997-12-14Mirar (Pontus Hagland)  int kill(int signal) { return predef::kill(pid,signal); }
8392231998-01-08Fredrik Hübinette (Hubbe) #endif
df67f71997-12-14Mirar (Pontus Hagland) 
8392231998-01-08Fredrik Hübinette (Hubbe)  int wait()
df67f71997-12-14Mirar (Pontus Hagland)  {
8392231998-01-08Fredrik Hübinette (Hubbe)  return pid->wait();
df67f71997-12-14Mirar (Pontus Hagland)  } // void set_done_callback(function foo,mixed ... args); // int result(); // array rusage(); }
04c34c1998-01-01Fredrik Hübinette (Hubbe) #endif
782bca2000-09-16Per Hedbor #endif