1 21 22 package org.armedbear.j; 23 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStreamWriter ; 28 import java.util.ArrayList ; 29 30 public final class ShellCommand implements Runnable 31 { 32 private final String cmdline; 33 private final File workingDirectory; 34 private final String input; 35 private final StringBuffer output = new StringBuffer (); 36 private int exitValue = -1; 37 38 public ShellCommand(String cmdline) 39 { 40 this(cmdline, null, null); 41 } 42 43 public ShellCommand(String cmdline, File workingDirectory) 44 { 45 this(cmdline, workingDirectory, null); 46 } 47 48 public ShellCommand(String cmdline, File workingDirectory, String input) 49 { 50 this.cmdline = cmdline; 51 this.workingDirectory = workingDirectory; 52 this.input = input; 53 } 54 55 public final String getOutput() 56 { 57 return output.toString(); 58 } 59 60 public final int exitValue() 61 { 62 return exitValue; 63 } 64 65 private void appendOutput(String s) 66 { 67 output.append(s); 68 } 69 70 public void run() 71 { 72 Process process = null; 73 try { 74 if (cmdline != null) { 75 if (Platform.isPlatformUnix()) { 76 if (workingDirectory != null) { 77 FastStringBuffer sb = new FastStringBuffer("\\cd \""); 78 sb.append(workingDirectory.canonicalPath()); 79 sb.append("\" && "); 80 sb.append(cmdline); 81 String [] cmdarray = {"/bin/sh", "-c", sb.toString()}; 82 process = Runtime.getRuntime().exec(cmdarray); 83 } else { 84 String [] cmdarray = {"/bin/sh", "-c", cmdline}; 85 process = Runtime.getRuntime().exec(cmdarray); 86 } 87 } else if (Platform.isPlatformWindows()) { 88 ArrayList list = new ArrayList (); 89 list.add("cmd.exe"); 90 list.add("/c"); 91 if (workingDirectory != null) { 92 FastStringBuffer sb = new FastStringBuffer("cd /d \""); 93 sb.append(workingDirectory.canonicalPath()); 94 sb.append("\" && "); 95 sb.append(cmdline); 96 list.addAll(Utilities.tokenize(sb.toString())); 97 } else 98 list.addAll(Utilities.tokenize(cmdline)); 99 final int size = list.size(); 100 String [] cmdarray = new String [size]; 101 for (int i = 0; i < size; i++) 102 cmdarray[i] = (String ) list.get(i); 103 process = Runtime.getRuntime().exec(cmdarray); 104 } 105 } 106 } 107 catch (IOException e) { 108 Log.error(e); 109 } 110 if (process != null) { 111 ShellCommandReaderThread stdoutThread = 112 new ShellCommandReaderThread(process.getInputStream()); 113 stdoutThread.start(); 114 ShellCommandReaderThread stderrThread = 115 new ShellCommandReaderThread(process.getErrorStream()); 116 stderrThread.start(); 117 if (input != null) { 118 BufferedWriter writer = 119 new BufferedWriter (new OutputStreamWriter (process.getOutputStream())); 120 try { 121 writer.write(input); 122 writer.flush(); 123 writer.close(); 124 } 125 catch (IOException e) { 126 Log.error(e); 127 } 128 } 129 try { 130 exitValue = process.waitFor(); 131 } 132 catch (InterruptedException e) { 133 Log.error(e); 134 } 135 try { 136 stdoutThread.join(); 137 } 138 catch (InterruptedException e) { 139 Log.error(e); 140 } 141 try { 142 stderrThread.join(); 143 } 144 catch (InterruptedException e) { 145 Log.error(e); 146 } 147 } 148 } 149 150 private class ShellCommandReaderThread extends ReaderThread 151 { 152 ShellCommandReaderThread(InputStream inputStream) 154 { 155 super(inputStream); 156 } 157 158 public void update(final String s) 159 { 160 appendOutput(s); 161 } 162 } 163 164 public static void shellCommand() 165 { 166 if (!Platform.isPlatformUnix()) 167 return; 168 final Editor editor = Editor.currentEditor(); 169 InputDialog d = 170 new InputDialog(editor, "Command:", "Shell Command", null); 171 d.setHistory(new History("shellCommand.command")); 172 editor.centerDialog(d); 173 d.show(); 174 String command = d.getInput(); 175 if (command == null) 176 return; 177 command = command.trim(); 178 if (command.length() == 0) 179 return; 180 AsynchronousShellCommand.startShellCommand(editor, command); 181 } 182 183 public static void shellCommand(String command) 184 { 185 if (!Platform.isPlatformUnix()) 186 return; 187 AsynchronousShellCommand.startShellCommand(Editor.currentEditor(), 188 command); 189 } 190 } 191 | Popular Tags |