| 1 |
/** |
|---|
| 2 |
* Helpful process functions |
|---|
| 3 |
* |
|---|
| 4 |
* Authors: |
|---|
| 5 |
* Gregor Richards |
|---|
| 6 |
* |
|---|
| 7 |
* License: |
|---|
| 8 |
* Copyright (c) 2006, 2007 Gregor Richards |
|---|
| 9 |
* |
|---|
| 10 |
* Permission is hereby granted, free of charge, to any person obtaining a |
|---|
| 11 |
* copy of this software and associated documentation files (the "Software"), |
|---|
| 12 |
* to deal in the Software without restriction, including without limitation |
|---|
| 13 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
|---|
| 14 |
* and/or sell copies of the Software, and to permit persons to whom the |
|---|
| 15 |
* Software is furnished to do so, subject to the following conditions: |
|---|
| 16 |
* |
|---|
| 17 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 18 |
* all copies or substantial portions of the Software. |
|---|
| 19 |
* |
|---|
| 20 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 21 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 22 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 23 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 24 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|---|
| 25 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 26 |
* DEALINGS IN THE SOFTWARE. |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
module hcf.process; |
|---|
| 30 |
|
|---|
| 31 |
import tango.io.File; |
|---|
| 32 |
import tango.io.FilePath; |
|---|
| 33 |
import tango.io.Stdout; |
|---|
| 34 |
|
|---|
| 35 |
import tango.text.Util; |
|---|
| 36 |
|
|---|
| 37 |
import tango.stdc.stdlib; |
|---|
| 38 |
import tango.stdc.stringz; |
|---|
| 39 |
|
|---|
| 40 |
private { |
|---|
| 41 |
version (Windows) { |
|---|
| 42 |
import bcd.windows.windows; |
|---|
| 43 |
} else { |
|---|
| 44 |
extern (C) int dup2(int, int); |
|---|
| 45 |
extern (C) int fork(); |
|---|
| 46 |
extern (C) int pipe(int[2]); |
|---|
| 47 |
extern (C) int read(int, void*, size_t); |
|---|
| 48 |
extern (C) int write(int, void*, size_t); |
|---|
| 49 |
extern (C) int close(int); |
|---|
| 50 |
extern (C) size_t waitpid(size_t, int*, int); |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
/** system with char[] */ |
|---|
| 55 |
int system(char[] cmd) |
|---|
| 56 |
{ |
|---|
| 57 |
return tango.stdc.stdlib.system(toUtf8z(cmd)); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
/** system + guarantee success */ |
|---|
| 61 |
void systemOrDie(char[] cmd) |
|---|
| 62 |
{ |
|---|
| 63 |
int res; |
|---|
| 64 |
Stdout.flush(); Stderr.flush(); |
|---|
| 65 |
res = system(cmd); |
|---|
| 66 |
if (res) // CyberShadow 2007.02.22: Display a message before exiting |
|---|
| 67 |
{ |
|---|
| 68 |
int p = locate(cmd, ' '); |
|---|
| 69 |
if(p!=-1) cmd=cmd[0..p]; |
|---|
| 70 |
Stdout("Command ")(" returned with code ")(res)(", aborting.").newline; |
|---|
| 71 |
exit(1); |
|---|
| 72 |
} |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
/** system + output */ |
|---|
| 76 |
int sayAndSystem(char[] cmd) |
|---|
| 77 |
{ |
|---|
| 78 |
Stdout("+ ")(cmd).newline; |
|---|
| 79 |
Stdout.flush(); Stderr.flush(); |
|---|
| 80 |
return system(cmd); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
/** systemOrDie + output */ |
|---|
| 84 |
void saySystemDie(char[] cmd) |
|---|
| 85 |
{ |
|---|
| 86 |
Stdout("+ ")(cmd).newline; |
|---|
| 87 |
systemOrDie(cmd); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
/** system + use a response file */ |
|---|
| 91 |
int systemResponse(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) |
|---|
| 92 |
{ |
|---|
| 93 |
int ret; |
|---|
| 94 |
char[][] elems = split(cmd, " "); |
|---|
| 95 |
|
|---|
| 96 |
/* the output is elems past 1 joined with \n */ |
|---|
| 97 |
char[] resp = join(elems[1..$], "\n"); |
|---|
| 98 |
(new File(rfile)).write(resp); |
|---|
| 99 |
|
|---|
| 100 |
Stdout.flush(); Stderr.flush(); |
|---|
| 101 |
ret = system(elems[0] ~ " " ~ rflag ~ rfile); |
|---|
| 102 |
|
|---|
| 103 |
if (deleteRFile) (new FilePath(rfile)).remove(); |
|---|
| 104 |
|
|---|
| 105 |
return ret; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
/** systemResponse + guarantee success */ |
|---|
| 109 |
void systemROrDie(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) |
|---|
| 110 |
{ |
|---|
| 111 |
int res; |
|---|
| 112 |
res = systemResponse(cmd, rflag, rfile, deleteRFile); |
|---|
| 113 |
if (res) // CyberShadow 2007.02.22: Display a message before exiting |
|---|
| 114 |
{ |
|---|
| 115 |
int p = locate(cmd, ' '); |
|---|
| 116 |
if(p!=-1) cmd=cmd[0..p]; |
|---|
| 117 |
Stdout("Command ")(cmd)(" returned with code ")(res)(", aborting.").newline; |
|---|
| 118 |
exit(1); |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
/** systemResponse + output */ |
|---|
| 123 |
int sayAndSystemR(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) |
|---|
| 124 |
{ |
|---|
| 125 |
Stdout("+ ")(cmd).newline; |
|---|
| 126 |
return systemResponse(cmd, rflag, rfile, deleteRFile); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
/** systemROrDie + output */ |
|---|
| 130 |
void saySystemRDie(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) |
|---|
| 131 |
{ |
|---|
| 132 |
Stdout("+ ")(cmd).newline; |
|---|
| 133 |
systemROrDie(cmd, rflag, rfile, deleteRFile); |
|---|
| 134 |
} |
|---|