Changeset 695

Show
Ignore:
Timestamp:
07/24/07 01:42:51 (1 year ago)
Author:
Gregor
Message:

hcf/process.d: Tangofied.

sss/conf.d: Adjusted for Tangofied hcf/process.d.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/tango/hcf/process.d

    r654 r695  
    2929module hcf.process; 
    3030 
    31 public import std.process; 
    32 alias std.process.system system; 
     31import tango.io.File; 
     32import tango.io.FilePath; 
     33import tango.io.Stdout; 
    3334 
    34 import std.stdio; 
    35 import std.string; 
    36 import std.stream; 
     35import tango.text.Util; 
    3736 
    38 import std.c.stdio
    39 import std.c.stdlib
     37import tango.stdc.stdlib
     38import tango.stdc.stringz
    4039 
    4140private { 
     
    5352} 
    5453 
    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[] */ 
     55int system(char[] cmd) 
     56
     57    return tango.stdc.stdlib.system(toUtf8z(cmd)); 
    25658} 
    25759 
     
    26062{ 
    26163    int res; 
    262     fflush(stdout); fflush(stderr); 
     64    Stdout.flush(); Stderr.flush(); 
    26365    res = system(cmd); 
    26466    if (res)  // CyberShadow 2007.02.22: Display a message before exiting 
    26567    { 
    266         int p = cmd.find(' '); 
     68        int p = locate(cmd, ' '); 
    26769        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
    26971        exit(1); 
    27072    } 
     
    27476int sayAndSystem(char[] cmd) 
    27577{ 
    276     writefln("+ %s", cmd)
    277     fflush(stdout); fflush(stderr); 
     78    Stdout("+ ")(cmd).newline
     79    Stdout.flush(); Stderr.flush(); 
    27880    return system(cmd); 
    27981} 
     
    28284void saySystemDie(char[] cmd) 
    28385{ 
    284     writefln("+ %s", cmd)
     86    Stdout("+ ")(cmd).newline
    28587    systemOrDie(cmd); 
    28688} 
     
    29092{ 
    29193    int ret; 
    292     char[][] elems = std.string.split(cmd, " "); 
     94    char[][] elems = split(cmd, " "); 
    29395     
    29496    /* 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); 
    29799     
    298     fflush(stdout); fflush(stderr); 
     100    Stdout.flush(); Stderr.flush(); 
    299101    ret = system(elems[0] ~ " " ~ rflag ~ rfile); 
    300102     
    301     if (deleteRFile) std.file.remove(rfile); 
     103    if (deleteRFile) (new FilePath(rfile)).remove(); 
    302104     
    303105    return ret; 
     
    311113    if (res)  // CyberShadow 2007.02.22: Display a message before exiting 
    312114    { 
    313         int p = cmd.find(' '); 
     115        int p = locate(cmd, ' '); 
    314116        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
    316118        exit(1); 
    317119    } 
     
    321123int sayAndSystemR(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) 
    322124{ 
    323     writefln("+ %s", cmd)
     125    Stdout("+ ")(cmd).newline
    324126    return systemResponse(cmd, rflag, rfile, deleteRFile); 
    325127} 
     
    328130void saySystemRDie(char[] cmd, char[] rflag, char[] rfile, bool deleteRFile) 
    329131{ 
    330     writefln("+ %s", cmd)
     132    Stdout("+ ")(cmd).newline
    331133    systemROrDie(cmd, rflag, rfile, deleteRFile); 
    332134} 
  • branches/tango/sss/conf.d

    r694 r695  
    3434import tango.io.FilePath; 
    3535import tango.io.FileSystem; 
     36import tango.io.protocol.Reader; 
    3637 
    3738import tango.sys.Environment; 
     39import tango.sys.Process; 
    3840 
    3941import tango.text.Ascii; 
     
    153155     
    154156    // get some default usedirs 
    155     useDirs ~= canonPath(installPrefix ~ std.path.sep ~ ".."); 
     157    useDirs ~= canonPath(installPrefix ~ FileConst.PathSeparatorChar ~ ".."); 
    156158    version (Posix) { 
    157159        char[] home = Environment.get("HOME"); 
    158160        if (home != "") { 
    159             useDirs ~= canonPath(home ~ std.path.sep ~ "d"); 
     161            useDirs ~= canonPath(home ~ FileConst.PathSeparatorChar ~ "d"); 
    160162        } 
    161163    } 
     
    200202        if (!dsssLibTestDPrefix.length) 
    201203            dsssLibTestDPrefix = canonPath( 
    202                 installPrefix ~ std.path.sep
    203                 "sss" ~ std.path.sep
     204                installPrefix ~ FileConst.PathSeparatorChar
     205                "sss" ~ FileConst.PathSeparatorChar
    204206                "dsss_lib_test.d"); 
    205207    } else { 
     
    270272        if (!dsssLibTestDPrefix.length) 
    271273            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
    276278                "dsss_lib_test.d"); 
    277279    } 
     
    305307    if (!srcListPrefix.length) 
    306308        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
    310312            "sources"); 
    311313     
     
    904906                   cmd[0..5] == "eval ") { 
    905907            // run the command, execute its output 
    906             PStream proc; 
     908            Process proc; 
     909            Reader rdr; 
    907910             
    908911            cmd = cmd[5..$]; 
    909912            if (tango.text.Ascii.toLower(getExt(cmd)) == "d") { 
    910                 proc = new PStream(dsss_build ~ "-full -exec " ~ cmd); 
     913                proc = new Process(dsss_build ~ "-full -exec " ~ cmd); 
    911914            } else { 
    912                 proc = new PStream(cmd); 
     915                proc = new Process(cmd); 
    913916            } 
    914917             
    915918            // now catch its output 
     919            rdr = new Reader(proc.stdout); 
    916920            char[] readbuf; 
    917921            char readc; 
    918922            while (true) { 
    919923                try { 
    920                     proc.read(readc); 
     924                    rdr.get(readc); 
    921925                } catch (Exception e) { 
    922926                    break; 
     
    928932            manifest ~= dsssScriptedStep(conf, readbuf); 
    929933             
    930             proc.close(); 
     934            proc.kill(); 
    931935             
    932936        } else if (cmd.length > 4 && 
     
    10341038                if (!found) { 
    10351039                    // 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
    10371041                    exit(1); 
    10381042                }