001: /*
002: * ShellCommand.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: ShellCommand.java,v 1.4 2003/06/16 15:21:29 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.BufferedWriter;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStreamWriter;
028: import java.util.ArrayList;
029:
030: public final class ShellCommand implements Runnable {
031: private final String cmdline;
032: private final File workingDirectory;
033: private final String input;
034: private final StringBuffer output = new StringBuffer();
035: private int exitValue = -1;
036:
037: public ShellCommand(String cmdline) {
038: this (cmdline, null, null);
039: }
040:
041: public ShellCommand(String cmdline, File workingDirectory) {
042: this (cmdline, workingDirectory, null);
043: }
044:
045: public ShellCommand(String cmdline, File workingDirectory,
046: String input) {
047: this .cmdline = cmdline;
048: this .workingDirectory = workingDirectory;
049: this .input = input;
050: }
051:
052: public final String getOutput() {
053: return output.toString();
054: }
055:
056: public final int exitValue() {
057: return exitValue;
058: }
059:
060: private void appendOutput(String s) {
061: output.append(s);
062: }
063:
064: public void run() {
065: Process process = null;
066: try {
067: if (cmdline != null) {
068: if (Platform.isPlatformUnix()) {
069: if (workingDirectory != null) {
070: FastStringBuffer sb = new FastStringBuffer(
071: "\\cd \"");
072: sb.append(workingDirectory.canonicalPath());
073: sb.append("\" && ");
074: sb.append(cmdline);
075: String[] cmdarray = { "/bin/sh", "-c",
076: sb.toString() };
077: process = Runtime.getRuntime().exec(cmdarray);
078: } else {
079: String[] cmdarray = { "/bin/sh", "-c", cmdline };
080: process = Runtime.getRuntime().exec(cmdarray);
081: }
082: } else if (Platform.isPlatformWindows()) {
083: ArrayList list = new ArrayList();
084: list.add("cmd.exe");
085: list.add("/c");
086: if (workingDirectory != null) {
087: FastStringBuffer sb = new FastStringBuffer(
088: "cd /d \"");
089: sb.append(workingDirectory.canonicalPath());
090: sb.append("\" && ");
091: sb.append(cmdline);
092: list.addAll(Utilities.tokenize(sb.toString()));
093: } else
094: list.addAll(Utilities.tokenize(cmdline));
095: final int size = list.size();
096: String[] cmdarray = new String[size];
097: for (int i = 0; i < size; i++)
098: cmdarray[i] = (String) list.get(i);
099: process = Runtime.getRuntime().exec(cmdarray);
100: }
101: }
102: } catch (IOException e) {
103: Log.error(e);
104: }
105: if (process != null) {
106: ShellCommandReaderThread stdoutThread = new ShellCommandReaderThread(
107: process.getInputStream());
108: stdoutThread.start();
109: ShellCommandReaderThread stderrThread = new ShellCommandReaderThread(
110: process.getErrorStream());
111: stderrThread.start();
112: if (input != null) {
113: BufferedWriter writer = new BufferedWriter(
114: new OutputStreamWriter(process
115: .getOutputStream()));
116: try {
117: writer.write(input);
118: writer.flush();
119: writer.close();
120: } catch (IOException e) {
121: Log.error(e);
122: }
123: }
124: try {
125: exitValue = process.waitFor();
126: } catch (InterruptedException e) {
127: Log.error(e);
128: }
129: try {
130: stdoutThread.join();
131: } catch (InterruptedException e) {
132: Log.error(e);
133: }
134: try {
135: stderrThread.join();
136: } catch (InterruptedException e) {
137: Log.error(e);
138: }
139: }
140: }
141:
142: private class ShellCommandReaderThread extends ReaderThread {
143: // If this constructor is private, we run into jikes 1.15 bug #2256.
144: ShellCommandReaderThread(InputStream inputStream) {
145: super (inputStream);
146: }
147:
148: public void update(final String s) {
149: appendOutput(s);
150: }
151: }
152:
153: public static void shellCommand() {
154: if (!Platform.isPlatformUnix())
155: return;
156: final Editor editor = Editor.currentEditor();
157: InputDialog d = new InputDialog(editor, "Command:",
158: "Shell Command", null);
159: d.setHistory(new History("shellCommand.command"));
160: editor.centerDialog(d);
161: d.show();
162: String command = d.getInput();
163: if (command == null)
164: return;
165: command = command.trim();
166: if (command.length() == 0)
167: return;
168: AsynchronousShellCommand.startShellCommand(editor, command);
169: }
170:
171: public static void shellCommand(String command) {
172: if (!Platform.isPlatformUnix())
173: return;
174: AsynchronousShellCommand.startShellCommand(Editor
175: .currentEditor(), command);
176: }
177: }
|