1 30 31 package rcm.util; 32 33 import java.io.*; 34 35 public abstract class Exec { 36 public static Debug debug = Debug.QUIET; 37 38 public static Process exec (String [] cmdarray) throws IOException { 39 return exec (cmdarray, null, null); 40 } 41 42 public static Process exec (String [] cmdarray, String [] envp) throws IOException { 43 return exec (cmdarray, envp, null); 44 } 45 46 public static Process exec (String [] cmdarray, String [] envp, File directory) throws IOException { 47 return 48 isWindows () 49 ? execWindows (cmdarray, envp, directory) 50 : execUnix (cmdarray, envp, directory); 51 } 52 53 56 57 static Process execUnix (String [] cmdarray, String [] envp, File directory) throws IOException { 58 61 StringBuffer buf = new StringBuffer (); 63 64 if (directory != null) { 65 buf.append ("cd '"); 67 buf.append (escapeQuote (directory.toString ())); 68 buf.append ("'; "); 69 } 70 71 if (envp != null) { 72 for (int i = 0; i < envp.length; ++i) { 74 String nameval = envp[i]; 75 int equals = nameval.indexOf ('='); 76 if (equals == -1) 77 throw new IOException ("environment variable '" + nameval 78 + "' should have form NAME=VALUE"); 79 buf.append (nameval.substring (0, equals+1)); 80 buf.append ('\''); 81 buf.append (escapeQuote (nameval.substring (equals+1))); 82 buf.append ("\' "); 83 } 84 } 85 86 String cmdname = escapeQuote (cmdarray[0]); 90 Runtime rt = Runtime.getRuntime (); 91 String [] sharray = new String [] { "sh", "-c", buf.toString () + " which \'" + cmdname + "\'" }; 92 Process which = rt.exec (sharray); 93 try { 94 which.waitFor (); 95 } catch (InterruptedException e) { 96 throw new IOException ("interrupted"); 97 } 98 99 if (which.exitValue () != 0) 100 throw new IOException ("can't execute " + cmdname + ": bad command or filename"); 101 102 buf.append ("exec \'"); 104 buf.append (cmdname); 105 buf.append ("\' "); 106 107 for (int i = 1; i < cmdarray.length; ++i) { 109 buf.append ('\''); 110 buf.append (escapeQuote (cmdarray[i])); 111 buf.append ("\' "); 112 } 113 114 debug.println ("executing " + buf); 115 sharray[2] = buf.toString (); 116 return rt.exec (sharray); 117 } 118 119 static String escapeQuote (String s) { 120 if (s.indexOf ('\'') != -1) { 123 debug.println ("replacing single-quotes in " + s); 124 s = Str.replace (s, "'", "'\\''"); 125 debug.println ("to get " + s); 126 } 127 return s; 128 } 129 130 133 134 static boolean isWindows () { 135 String os = System.getProperty ("os.name"); 136 return (os != null && os.startsWith ("Windows")); 137 } 138 139 static boolean isJview () { 140 String vendor = System.getProperty ("java.vendor"); 141 return (vendor != null && vendor.startsWith ("Microsoft")); 142 } 143 144 static Process execWindows (String [] cmdarray, String [] envp, File directory) throws IOException { 145 if (envp != null || directory != null) { 146 if (isJview ()) 147 throw new IOException 149 ("can't use Exec.exec() under Microsoft JVM"); 150 151 if (!linked) { 152 try { 153 System.loadLibrary ("win32exec"); 154 linked = true; 155 } catch (LinkageError e) { 156 throw new IOException ("can't use Exec.exec(): " 157 + e.getMessage ()); 158 } 159 } 160 161 if (envp != null) { 162 for (int i = 0; i < envp.length; ++i) 163 putenv (envp[i]); 164 } 165 166 if (directory != null) 167 chdir (directory.toString ()); 168 } 169 170 return Runtime.getRuntime ().exec (cmdarray); 171 } 172 173 static boolean linked = false; static native boolean putenv (String env); 175 static native boolean chdir (String dir); 176 } 177 | Popular Tags |