Changeset 695
- Timestamp:
- 07/24/07 01:42:51 (1 year ago)
- Files:
-
- branches/tango/hcf/process.d (modified) (9 diffs)
- branches/tango/sss/conf.d (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/tango/hcf/process.d
r654 r695 29 29 module hcf.process; 30 30 31 public import std.process; 32 alias std.process.system system; 31 import tango.io.File; 32 import tango.io.FilePath; 33 import tango.io.Stdout; 33 34 34 import std.stdio; 35 import std.string; 36 import std.stream; 35 import tango.text.Util; 37 36 38 import std.c.stdio;39 import std.c.stdlib;37 import tango.stdc.stdlib; 38 import tango.stdc.stringz; 40 39 41 40 private { … … 53 52 } 54 53 55 class PStream : Stream { 56 /** First init step: this, then start the process based on its result */ 57 void init1() 58 { 59 readable = true; 60 writeable = true; 61 seekable = false; 62 63 version (Posix) { 64 // get our pipes 65 pipe(ip); 66 pipe(op); 67 68 // fork 69 pid = fork(); 70 if (pid == 0) { 71 // dup2 in our stdin/out 72 dup2(ip[0], 0); 73 hcf.process.close(ip[1]); 74 dup2(op[1], 1); 75 dup2(op[1], 2); 76 hcf.process.close(op[0]); 77 } else if (pid == -1) { 78 // boom! 79 throw new StreamException("Failed to fork"); 80 } else { 81 hcf.process.close(ip[0]); 82 hcf.process.close(op[1]); 83 } 84 } else version (Windows) { 85 // get our pipes 86 _SECURITY_ATTRIBUTES sa; 87 sa.nLength = _SECURITY_ATTRIBUTES.sizeof; 88 sa.bInheritHandle = 1; 89 CreatePipe(&ipr, &ipw, &sa, 0); 90 CreatePipe(&opr, &opw, &sa, 0); 91 92 // don't fork yet - it'll have to be done by the next step 93 } 94 } 95 96 /** Use system */ 97 this(char[] command) 98 { 99 init1(); 100 101 version (Posix) { 102 if (pid == 0) { 103 exit(std.process.system(command)); 104 } 105 } else version (Windows) { 106 _STARTUPINFOA si; 107 si.cb = _STARTUPINFOA.sizeof; 108 si.hStdInput = ipr; 109 si.hStdOutput = opw; 110 si.hStdError = opw; 111 si.dwFlags = STARTF_USESTDHANDLES; 112 113 _PROCESS_INFORMATION pi; 114 115 CreateProcessA(null, toStringz(command), null, null, 116 1, 0, null, null, &si, &pi); 117 CloseHandle(ipr); 118 CloseHandle(opw); 119 CloseHandle(pi.hThread); 120 phnd = pi.hProcess; 121 } 122 123 init2(); 124 } 125 126 /** Use execvp */ 127 this(char[] pathname, char[][] argv) 128 { 129 init1(); 130 131 version (Posix) { 132 if (pid == 0) { 133 execvp(pathname, argv); 134 exit(1); 135 } 136 } else version(Windows) { 137 assert(0); 138 } 139 140 init2(); 141 } 142 143 /** The second init part */ 144 void init2() 145 { 146 isopen = true; 147 } 148 149 override size_t readBlock(void* buffer, size_t size) 150 { 151 version (Posix) { 152 int rd = hcf.process.read(op[0], buffer, size); 153 if (rd == -1) { 154 readEOF = true; 155 return 0; 156 } else { 157 readEOF = false; 158 } 159 return rd; 160 } else version (Windows) { 161 uint rd; 162 if (!ReadFile(opr, buffer, size, &rd, null)) { 163 readEOF = true; 164 return 0; 165 } else { 166 readEOF = false; 167 } 168 return rd; 169 } 170 } 171 172 override size_t writeBlock(void* buffer, size_t size) 173 { 174 version (Posix) { 175 int wt = hcf.process.write(ip[1], buffer, size); 176 if (wt == -1) { 177 throw new StreamException("Process closed"); 178 } 179 return wt; 180 } else version (Windows) { 181 uint wt; 182 if (!WriteFile(opr, buffer, size, &wt, null)) { 183 readEOF = true; 184 return 0; 185 } else { 186 readEOF = false; 187 } 188 return wt; 189 } 190 } 191 192 override ulong seek(long offset, SeekPos whence) 193 { 194 throw new StreamException("Cannot seek in PStreams"); 195 } 196 197 /** Close the process, return the result */ 198 void close() 199 { 200 if (isopen) { 201 isopen = false; 202 version (Posix) { 203 waitpid(pid, &eval, 0); 204 hcf.process.close(ip[1]); 205 hcf.process.close(op[0]); 206 } else version (Windows) { 207 GetExitCodeProcess(phnd, &eval); 208 CloseHandle(phnd); 209 CloseHandle(ipw); 210 CloseHandle(opr); 211 } 212 } 213 } 214 215 version (Posix) { 216 /** Get the exit value */ 217 int exitValue() 218 { 219 return eval; 220 } 221 } else version (Windows) { 222 /** Get the exit value */ 223 uint exitValue() 224 { 225 return eval; 226 } 227 } 228 229 private: 230 231 version (Posix) { 232 /** The pid */ 233 int pid; 234 235 /** The exit value */ 236 int eval; 237 238 /** The input pipe */ 239 int[2] ip; 240 241 /** The output pipe */ 242 int[2] op; 243 } else version (Windows) { 244 /** The process handle */ 245 HANDLE phnd; 246 247 /** The exit value */ 248 uint eval; 249 250 /** The input pipe */ 251 HANDLE ipr, ipw; 252 253 /** The output pipe */ 254 HANDLE opr, opw; 255 } 54 /** system with char[] */ 55 int system(char[] cmd) 56 { 57 return tango.stdc.stdlib.system(toUtf8z(cmd)); 256 58 } 257 59 … … 260 62 { 261 63 int res; 262 fflush(stdout); fflush(stderr);64 Stdout.flush(); Stderr.flush(); 263 65 res = system(cmd); 264 66 if (res) // CyberShadow 2007.02.22: Display a message before exiting 265 67 { 266 int p = cmd.find(' ');68 int p = locate(cmd, ' '); 267 69 if(p!=-1) cmd=cmd[0..p]; 268 writefln("Command " ~ cmd ~ " returned with code ", res, ", aborting.");70 Stdout("Command ")(" returned with code ")(res)(", aborting.").newline; 269 71 exit(1); 270 72 } … … 274 76 int sayAndSystem(char[] cmd) 275 77 { 276 writefln("+ %s", cmd);277 fflush(stdout); fflush(stderr);78 Stdout("+ ")(cmd).newline; 79 Stdout.flush(); Stderr.flush(); 278 80 return system(cmd); 279 81 } … … 282 84 void saySystemDie(char[] cmd) 283 85 { 284 writefln("+ %s", cmd);86 Stdout("+ ")(cmd).newline; 285 87 systemOrDie(cmd); 286 88 } … … 290 92 { 291 93 int ret; 292 char[][] elems = s td.string.split(cmd, " ");94 char[][] elems = split(cmd, " "); 293 95 294 96 /* the output is elems past 1 joined with \n */ 295 char[] resp = std.string.join(elems[1..$], "\n");296 std.file.write(rfile,resp);97 char[] resp = join(elems[1..$], "\n"); 98 (new File(rfile)).write(resp); 297 99 298 fflush(stdout); fflush(stderr);100 Stdout.flush(); Stderr.flush(); 299 101 ret = system(elems[0] ~ " " ~ rflag ~ rfile); 300 102 301 if (deleteRFile) std.file.remove(rfile);103 if (deleteRFile) (new FilePath(rfile)).remove(); 302 104 303 105 return ret; … … 311 113 if (res) // CyberShadow 2007.02.22: Display a message before exiting 312 114 { 313 int p = cmd.find(' ');115 int p = locate(cmd, ' '); 314 116 if(p!=-1) cmd=cmd[0..p]; 315 writefln("Command " ~ cmd ~ " returned with code ", res, ", aborting.");117 Stdout("Command ")(cmd)(" returned with code ")(res)(", aborting.").newline; 316 118 exit(1); 317 119 } … … 321 123 int sayAndSystemR(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) 322 124 { 323 writefln("+ %s", cmd);125 Stdout("+ ")(cmd).newline; 324 126 return systemResponse(cmd, rflag, rfile, deleteRFile); 325 127 } … … 328 130 void saySystemRDie(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) 329 131 { 330 writefln("+ %s", cmd);132 Stdout("+ ")(cmd).newline; 331 133 systemROrDie(cmd, rflag, rfile, deleteRFile); 332 134 } branches/tango/sss/conf.d
r694 r695 34 34 import tango.io.FilePath; 35 35 import tango.io.FileSystem; 36 import tango.io.protocol.Reader; 36 37 37 38 import tango.sys.Environment; 39 import tango.sys.Process; 38 40 39 41 import tango.text.Ascii; … … 153 155 154 156 // get some default usedirs 155 useDirs ~= canonPath(installPrefix ~ std.path.sep~ "..");157 useDirs ~= canonPath(installPrefix ~ FileConst.PathSeparatorChar ~ ".."); 156 158 version (Posix) { 157 159 char[] home = Environment.get("HOME"); 158 160 if (home != "") { 159 useDirs ~= canonPath(home ~ std.path.sep~ "d");161 useDirs ~= canonPath(home ~ FileConst.PathSeparatorChar ~ "d"); 160 162 } 161 163 } … … 200 202 if (!dsssLibTestDPrefix.length) 201 203 dsssLibTestDPrefix = canonPath( 202 installPrefix ~ std.path.sep~203 "sss" ~ std.path.sep~204 installPrefix ~ FileConst.PathSeparatorChar ~ 205 "sss" ~ FileConst.PathSeparatorChar ~ 204 206 "dsss_lib_test.d"); 205 207 } else { … … 270 272 if (!dsssLibTestDPrefix.length) 271 273 dsssLibTestDPrefix = canonPath( 272 installPrefix ~ std.path.sep~273 ".." ~ std.path.sep~274 "share" ~ std.path.sep~275 "dsss" ~ std.path.sep~274 installPrefix ~ FileConst.PathSeparatorChar ~ 275 ".." ~ FileConst.PathSeparatorChar ~ 276 "share" ~ FileConst.PathSeparatorChar ~ 277 "dsss" ~ FileConst.PathSeparatorChar ~ 276 278 "dsss_lib_test.d"); 277 279 } … … 305 307 if (!srcListPrefix.length) 306 308 srcListPrefix = canonPath( 307 forcePrefix ~ std.path.sep~308 "share" ~ std.path.sep~309 "dsss" ~ std.path.sep~309 forcePrefix ~ FileConst.PathSeparatorChar ~ 310 "share" ~ FileConst.PathSeparatorChar ~ 311 "dsss" ~ FileConst.PathSeparatorChar ~ 310 312 "sources"); 311 313 … … 904 906 cmd[0..5] == "eval ") { 905 907 // run the command, execute its output 906 PStream proc; 908 Process proc; 909 Reader rdr; 907 910 908 911 cmd = cmd[5..$]; 909 912 if (tango.text.Ascii.toLower(getExt(cmd)) == "d") { 910 proc = new P Stream(dsss_build ~ "-full -exec " ~ cmd);913 proc = new Process(dsss_build ~ "-full -exec " ~ cmd); 911 914 } else { 912 proc = new P Stream(cmd);915 proc = new Process(cmd); 913 916 } 914 917 915 918 // now catch its output 919 rdr = new Reader(proc.stdout); 916 920 char[] readbuf; 917 921 char readc; 918 922 while (true) { 919 923 try { 920 proc.read(readc);924 rdr.get(readc); 921 925 } catch (Exception e) { 922 926 break; … … 928 932 manifest ~= dsssScriptedStep(conf, readbuf); 929 933 930 proc. close();934 proc.kill(); 931 935 932 936 } else if (cmd.length > 4 && … … 1034 1038 if (!found) { 1035 1039 // didn't match anything! 1036 Cout(be)(" is not described in the configuration file.") ;1040 Cout(be)(" is not described in the configuration file.").newline; 1037 1041 exit(1); 1038 1042 }
