1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-08 00:35:49 +02:00

Add expandEnviroment()

This commit is contained in:
Alain Magloire 2003-01-06 22:02:39 +00:00
parent 33de97b583
commit c3ed134983

View file

@ -9,6 +9,7 @@ import java.text.DateFormat;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -20,6 +21,7 @@ import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDebugConfiguration; import org.eclipse.cdt.debug.core.ICDebugConfiguration;
import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin; import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin;
import org.eclipse.cdt.launch.sourcelookup.DefaultSourceLocator; import org.eclipse.cdt.launch.sourcelookup.DefaultSourceLocator;
import org.eclipse.cdt.utils.spawner.EnvironmentReader;
import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
@ -43,6 +45,11 @@ abstract public class AbstractCLaunchDelegate implements ILaunchConfigurationDel
abstract public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) abstract public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
throws CoreException; throws CoreException;
/**
* Return the save environment variables in the configuration.
* The array does not include the default environment of the target.
* array[n] : name=value
*/
protected String[] getEnvironmentArray(ILaunchConfiguration config) { protected String[] getEnvironmentArray(ILaunchConfiguration config) {
Map env = null; Map env = null;
try { try {
@ -58,18 +65,21 @@ abstract public class AbstractCLaunchDelegate implements ILaunchConfigurationDel
Entry entry; Entry entry;
for (int i = 0; entries.hasNext() && i < array.length; i++) { for (int i = 0; entries.hasNext() && i < array.length; i++) {
entry = (Entry) entries.next(); entry = (Entry) entries.next();
array[i] = ((String) entry.getKey()) + ((String) entry.getValue()); array[i] = ((String) entry.getKey()) + "=" + ((String) entry.getValue());
} }
return array; return array;
} }
/**
* Return the save environment variables of this configuration.
* The array does not include the default environment of the target.
*/
protected Properties getEnvironmentProperty(ILaunchConfiguration config) { protected Properties getEnvironmentProperty(ILaunchConfiguration config) {
Properties prop = new Properties(); Properties prop = new Properties();
Map env = null; Map env = null;
try { try {
env = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ENVIROMENT_MAP, (Map) null); env = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ENVIROMENT_MAP, (Map) null);
} } catch (CoreException e) {
catch (CoreException e) {
} }
if (env == null) if (env == null)
return prop; return prop;
@ -82,6 +92,112 @@ abstract public class AbstractCLaunchDelegate implements ILaunchConfigurationDel
return prop; return prop;
} }
/**
* Return the default Environment of the target.
*/
protected Properties getDefaultEnvironment() {
return EnvironmentReader.getEnvVars();
}
/**
* Expand the variable with the format ${key}. example:
* HOME=/foobar
* NEWHOME = ${HOME}/project
* The environement NEWHOME will be /foobar/project.
*/
protected Properties expandEnvironment(ILaunchConfiguration config ) {
return expandEnvironment(getEnvironmentProperty(config));
}
/**
* Expand the variable with the format ${key}. example:
* HOME=/foobar
* NEWHOME = ${HOME}/project
* The environement NEWHOME will be /foobar/project.
*/
protected Properties expandEnvironment(Properties props) {
Enumeration names = props.propertyNames();
if (names != null) {
while (names.hasMoreElements()) {
String key = (String) names.nextElement();
String value = props.getProperty(key);
if (value != null && value.indexOf('$') != -1) {
StringBuffer sb = new StringBuffer();
StringBuffer param = new StringBuffer();
char prev = '\n';
char ch = prev;
boolean inMacro = false;
boolean inSingleQuote = false;
for (int i = 0; i < value.length(); i++) {
ch = value.charAt(i);
switch (ch) {
case '\'':
if (prev != '\\') {
inSingleQuote = !inSingleQuote;
}
break;
case '$' :
if (!inSingleQuote && prev != '\\') {
if (i < value.length() && value.indexOf('}', i) > 0) {
char c = value.charAt(i + 1);
if (c == '{') {
param.setLength(0);
inMacro = true;
prev = ch;
continue;
}
}
}
break;
case '}' :
if (inMacro) {
inMacro = false;
String v = null;
String p = param.toString();
/* Search in the current property only
* if it is not the same name.
*/
if (!p.equals(key)) {
v = props.getProperty(p);
}
/* Fallback to the default Environemnt. */
if (v == null) {
Properties def = getDefaultEnvironment();
if (def != null) {
v = def.getProperty(p);
}
}
if (v != null) {
sb.append(v);
}
param.setLength(0);
/* Skip the trailing } */
prev = ch;
continue;
}
break;
} /* switch */
if (!inMacro) {
sb.append(ch);
} else {
/* Do not had the '{' */
if (!(ch == '{' && prev == '$')) {
param.append(ch);
}
}
prev = (ch == '\\' && prev == '\\') ? '\n' : ch;
} /* for */
props.setProperty(key, sb.toString());
} /* !if (value ..) */
} /* while() */
} /* if (names != null) */
return props;
}
/** /**
* Returns the working directory specified by * Returns the working directory specified by
* the given launch configuration, or <code>null</code> if none. * the given launch configuration, or <code>null</code> if none.