1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-25 18:05:33 +02:00

shell supported simple command operation

This commit is contained in:
David McKnight 2007-01-15 19:52:24 +00:00
parent 560175bd3a
commit 80a3419d85

View file

@ -1,46 +1,30 @@
/********************************************************************************
* Copyright (c) 2006 IBM Corporation. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Initial Contributors:
* The following IBM employees contributed to the Remote System Explorer
* component that contains this file: David McKnight, Kushal Munir,
* Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson,
* Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
*
* Contributors:
* {Name} (company) - description of contribution.
********************************************************************************/
package org.eclipse.rse.subsystems.shells.core.model; package org.eclipse.rse.subsystems.shells.core.model;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile;
import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCmdSubSystem; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCmdSubSystem;
import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCommandShell; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCommandShell;
import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteOutput; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteOutput;
import org.eclipse.swt.widgets.Display;
public class SimpleCommandOperation public class SimpleCommandOperation
{ {
private IRemoteCmdSubSystem _subsystem; protected IRemoteCmdSubSystem _subsystem;
private IRemoteFile _workingDirectory; protected IRemoteFile _workingDirectory;
private IRemoteCommandShell _cmdShell; protected IRemoteCommandShell _cmdShell;
private int _outputLineIndex = 0; protected List _envVars;
protected int _outputLineIndex = 0;
protected boolean _runAsShell = false;
public SimpleCommandOperation(IRemoteCmdSubSystem subsystem, IRemoteFile workingDirectory) public SimpleCommandOperation(IRemoteCmdSubSystem subsystem, IRemoteFile workingDirectory, boolean runAsShell)
{ {
_subsystem = subsystem; _subsystem = subsystem;
_workingDirectory = workingDirectory; _workingDirectory = workingDirectory;
} _envVars = new ArrayList();
_runAsShell = runAsShell;
public void runCommand(String command) throws Exception
{
Object[] result =_subsystem.runCommand(command, _workingDirectory, false);
_cmdShell = (IRemoteCommandShell)result[0];
} }
public IRemoteCommandShell getCommandShell() public IRemoteCommandShell getCommandShell()
@ -48,11 +32,118 @@ public class SimpleCommandOperation
return _cmdShell; return _cmdShell;
} }
public void setEnvironmentVariable(String name, String value)
{
_envVars.add(name + "=" + value); //$NON-NLS-1$
}
public void setEnvironmentVariables(String[] names, String[] values)
{
for (int i = 0; i < names.length; i++)
{
setEnvironmentVariable(names[i], values[i]);
}
}
public void setEnvironmentVariables(String[] vars)
{
for (int i = 0; i < vars.length; i++)
{
_envVars.add(vars[i]);
}
}
public String[] getEnvironmentVariables()
{
String[] vars = new String[_envVars.size()];
for (int i = 0; i < vars.length; i++)
{
vars[i] = (String)_envVars.get(i);
}
return vars;
}
/**
* Run a command
* @param command the command to run
* @param exitShell indicates whether to exit the shell after running the command
* @throws Exception
*/
public void runCommand(String command, boolean exitShell) throws Exception
{
if (_runAsShell)
{
_cmdShell = _subsystem.runShell(null, _workingDirectory);
_subsystem.sendCommandToShell(new NullProgressMonitor(), command, _cmdShell);
if (exitShell)
{
_subsystem.sendCommandToShell(new NullProgressMonitor(), "exit", _cmdShell); //$NON-NLS-1$
}
}
else
{
Object[] result =_subsystem.runCommand(new NullProgressMonitor(), command, _workingDirectory, false);
_cmdShell= (IRemoteCommandShell)result[0];
}
}
/**
* Launch a shell with the specified exports and command
* @param exports the command to initialize the shell environment
* @param command the command to run
* @param exitShell indicates whether to exit the shell after running the command
* @throws Exception
*/
public void runCommandInShell(String exports, String command, boolean exitShell) throws Exception
{
_runAsShell = true;
_cmdShell = _subsystem.runShell(null, _workingDirectory);
if (exports != null)
{
_subsystem.sendCommandToShell(new NullProgressMonitor(), exports, _cmdShell);
}
_subsystem.sendCommandToShell(new NullProgressMonitor(), command, _cmdShell);
if (exitShell)
{
exitShell();
}
}
public void removeShell()
{
try
{
_subsystem.removeShell(_cmdShell);
}
catch (Exception e)
{
}
}
public void exitShell()
{
if (_runAsShell)
{
try
{
_subsystem.sendCommandToShell(new NullProgressMonitor(), "exit", _cmdShell); //$NON-NLS-1$
}
catch (Exception e)
{
}
}
}
public void putInput(String input) throws Exception public void putInput(String input) throws Exception
{ {
if (isActive()) if (isActive())
{ {
_subsystem.sendCommandToShell(input, _cmdShell); _subsystem.sendCommandToShell(new NullProgressMonitor(), input, _cmdShell);
} }
} }
@ -69,8 +160,11 @@ public class SimpleCommandOperation
{ {
if (_cmdShell != null) if (_cmdShell != null)
{ {
if (_cmdShell.getSize() > _outputLineIndex)
if (_cmdShell.listOutput().length > _outputLineIndex) {
return true;
}
else if (_cmdShell.listOutput().length > _outputLineIndex)
{ {
return true; return true;
} }
@ -82,7 +176,7 @@ public class SimpleCommandOperation
{ {
if (_cmdShell != null && _cmdShell.isActive()) if (_cmdShell != null && _cmdShell.isActive())
{ {
_cmdShell.getCommandSubSystem().cancelShell(_cmdShell); _cmdShell.getCommandSubSystem().cancelShell(null, _cmdShell);
} }
} }
@ -91,39 +185,44 @@ public class SimpleCommandOperation
{ {
if (_cmdShell != null) if (_cmdShell != null)
{ {
boolean isActive = true;
if (!hasMoreOutput() && waitForOutput) if (!hasMoreOutput() && waitForOutput)
{ {
Display display = Display.getCurrent(); while (!hasMoreOutput() && isActive)
try
{ {
while (!hasMoreOutput() && isActive())
try
{ {
while (display!=null && display.readAndDispatch()) { isActive = isActive();
//Process everything on event queue Thread.sleep(100);
} }
if (!hasMoreOutput() && isActive()) Thread.sleep(100); catch (Exception e)
{
e.printStackTrace();
} }
}
catch (InterruptedException e)
{
//Cancel waiting
}
if (!isActive())
{
return null;
} }
} }
{ {
Object output = _cmdShell.getOutputAt(_outputLineIndex); Object[] out = _cmdShell.listOutput();
_outputLineIndex++; if (out.length > _outputLineIndex)
if (output instanceof IRemoteOutput)
{ {
return ((IRemoteOutput)output).getText(); Object output = out[_outputLineIndex];
_outputLineIndex++;
if (output instanceof IRemoteOutput)
{
return ((IRemoteOutput)output).getText();
}
else if (output instanceof IRemoteFile)
{
return ((IRemoteFile)output).getLabel();
}
} }
else if (output instanceof IRemoteFile) else if (!isActive)
{ {
return ((IRemoteFile)output).getLabel(); return null;
} }
} }
@ -131,4 +230,4 @@ public class SimpleCommandOperation
return ""; //$NON-NLS-1$ return ""; //$NON-NLS-1$
} }
} }