1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Bug #218954 : replace "regedit" run with API call.

This commit is contained in:
Oleg Krasilnikov 2008-02-15 12:53:06 +00:00
parent b5f0556e66
commit 443969e7a7
2 changed files with 97 additions and 102 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005 Intel Corporation and others.
* Copyright (c) 2005, 2008 Intel Corporation and others.
* 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
@ -12,52 +12,45 @@ package org.eclipse.cdt.managedbuilder.gnu.cygwin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.core.IBuildPathResolver;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.ManagedBuildManager;
import org.eclipse.cdt.managedbuilder.envvar.IBuildEnvironmentVariable;
import org.eclipse.cdt.managedbuilder.gnu.ui.GnuUIPlugin;
import org.eclipse.cdt.utils.WindowsRegistry;
import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.core.runtime.IPath;
public class CygwinPathResolver implements IBuildPathResolver {
static final String TOOL = "/cygpath -w -p "; //$NON-NLS-1$
static final char BS = '\\'; //$NON-NLS-1$
static final char SLASH = '/'; //$NON-NLS-1$
static final String PROPERTY_OS_NAME = "os.name"; //$NON-NLS-1$
static final String PROPERTY_OS_VALUE = "windows";//$NON-NLS-1$
static final String ARG0 = "regedit"; //$NON-NLS-1$
static final String ARG1 = "/ea"; //$NON-NLS-1$
static final String OUTFILE = "result.txt"; //$NON-NLS-1$
static final String SP = " "; //$NON-NLS-1$
static final String QUOT = "\""; //$NON-NLS-1$
static final String REGISTRY_KEY = "\\SOFTWARE\\Cygnus Solutions\\Cygwin\\mounts v2"; //$NON-NLS-1$
static final String[] REGISTRY_ROOTS = {"\"HKEY_CURRENT_USER", "\"HKEY_LOCAL_MACHINE"}; //$NON-NLS-1$ //$NON-NLS-2$
static final String REGISTRY_BINS = "/usr/bin"; //$NON-NLS-1$
static final String CYGPREF_NAME = "cygdrive prefix"; //$NON-NLS-1$
static final String PATH_NAME = "native"; //$NON-NLS-1$
static final String REG_SZ = "REG_SZ"; //$NON-NLS-1$
static final String BINPATTERN = "/usr/bin"; //$NON-NLS-1$
static final String ETCPATTERN = "/etc"; //$NON-NLS-1$
static final String ROOTPATTERN = "/"; //$NON-NLS-1$
static final String DELIMITER_UNIX = ":"; //$NON-NLS-1$
static final String DELIMITER_WIN = ";"; //$NON-NLS-1$
static final String GCC_VERSION_CMD = "gcc --version"; //$NON-NLS-1$
static final String MINGW_SPECIAL = "mingw "; //$NON-NLS-1$
static final String CYGWIN_SPECIAL = "cygwin "; //$NON-NLS-1$
private static final String TOOL = "/cygpath -w -p "; //$NON-NLS-1$
private static final char BS = '\\'; //$NON-NLS-1$
private static final char SLASH = '/'; //$NON-NLS-1$
private static final String PROPERTY_OS_NAME = "os.name"; //$NON-NLS-1$
private static final String PROPERTY_OS_VALUE = "windows";//$NON-NLS-1$
private static final String SP = " "; //$NON-NLS-1$
private static final String REGISTRY_KEY = "SOFTWARE\\Cygnus Solutions\\Cygwin\\mounts v2\\"; //$NON-NLS-1$
private static final String PATH_NAME = "native"; //$NON-NLS-1$
private static final String EMPTY = ""; //$NON-NLS-1$
private static final String SSLASH = "/"; //$NON-NLS-1$
private static final String BSLASH = "\\\\"; //$NON-NLS-1$
private static final String BINPATTERN = "/usr/bin"; //$NON-NLS-1$
private static final String ETCPATTERN = "/etc"; //$NON-NLS-1$
private static final String ROOTPATTERN = SSLASH;
private static final String DELIMITER_UNIX = ":"; //$NON-NLS-1$
private static final String DELIMITER_WIN = ";"; //$NON-NLS-1$
private static final String GCC_VERSION_CMD = "gcc --version"; //$NON-NLS-1$
private static final String MINGW_SPECIAL = "mingw "; //$NON-NLS-1$
private static final String CYGWIN_SPECIAL = "cygwin "; //$NON-NLS-1$
static boolean checked = false;
static String binCygwin = null;
static String rootCygwin = null;
static String etcCygwin = null;
private static boolean checked = false;
private static String binCygwin = null;
private static String rootCygwin = null;
private static String etcCygwin = null;
/* (non-Javadoc)
* @see org.eclipse.cdt.managedbuilder.core.IBuildPathResolver#resolveBuildPaths(int, java.lang.String, java.lang.String, org.eclipse.cdt.managedbuilder.core.IConfiguration)
@ -73,10 +66,13 @@ public class CygwinPathResolver implements IBuildPathResolver {
String[] result = variableValue.split(DELIMITER_UNIX);
String exePath = getBinPath();
if (exePath == null) { return result; } // no changes
if (exePath == null) {
return result; // no changes
}
File file = new File(exePath);
if (!file.exists() || !file.isDirectory()) { return result; } // no changes
if (!file.exists() || !file.isDirectory()) {
return result; // no changes
}
String s = exePath + TOOL + variableValue;
String[] lines = exec(s, configuration);
if (lines != null && lines.length > 0) {
@ -115,68 +111,36 @@ public class CygwinPathResolver implements IBuildPathResolver {
}
/**
* reads once data from registry (for Win32 only)
* and sets corresponding properties;
*/
private static synchronized void checkRegistry() {
if (checked) return;
try {
etcCygwin = null;
binCygwin = null;
rootCygwin = null;
if (!isWindows()) return;
for(int i = 0; i < REGISTRY_ROOTS.length; i++){
IPath toSave = GnuUIPlugin.getDefault().getStateLocation();
toSave = toSave.addTrailingSeparator().append(OUTFILE);
String[] args = {ARG0, ARG1, toSave.toOSString(), REGISTRY_ROOTS[i]+REGISTRY_KEY+QUOT };
try {
File f = new File(toSave.toOSString());
f.delete();
if (ProcessFactory.getFactory().exec(args).waitFor() == 0 && f.exists() && f.canRead()) {
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
ArrayList ls = new ArrayList(1);
String s;
while ((s = r.readLine() ) != null ) ls.add(s);
r.close();
f.delete();
String[] aus = (String[])ls.toArray(new String[0]);
if (etcCygwin == null) { etcCygwin = getDir(aus, ETCPATTERN); }
if (binCygwin == null) { binCygwin = getDir(aus, BINPATTERN); }
if (rootCygwin == null) { rootCygwin = getDir(aus, ROOTPATTERN);}
}
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (InterruptedException e) {
} catch (SecurityException e) {
}
}
} finally {
checked = true;
}
}
/**
* @param ls - "regedit"'s output
* @param pattern - path to search
* Reads required value from registry.
*
* If there's no such key, tries to read "parent" key
* and forms resulting path from parent path and
* key suffix.
*
* For example, if there's no key for "/etc",
* reads key for "/" (has value "c:/cygwin")
* and forms result "c:/cygwin/etc".
*
* @param user
* if true, reads from HKEY_CURRENT_USER
* if false, reads from HKEY_LOCAL_MACHINE
* @param pattern
* specific registry key value (last segment)
* @return
* corresponding string value
* or null if nothing found
*/
private static String getDir(String[] ls, String pattern) {
String tail = ""; //$NON-NLS-1$
private static String read(boolean user, String pattern) {
String tail = EMPTY;
while (pattern.length() > 0) {
boolean search = false;
for (int i=0; i<ls.length; i++) {
int pos=0;
if (ls[i].lastIndexOf(REGISTRY_KEY) > 0) {
search = ls[i].endsWith(pattern+"]"); //$NON-NLS-1$
}
else if (search && ((pos = ls[i].lastIndexOf(PATH_NAME)) > 0)) {
String s = ls[i].substring(pos + PATH_NAME.length() + 3).trim();
s = s.substring(0, s.length() - 1) + tail;
return s.replaceAll("\\\\+", "/"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
if (pattern.equals(ROOTPATTERN)) break; // no other paths to search
String key = REGISTRY_KEY + pattern;
String s = user ?
WindowsRegistry.getRegistry().getCurrentUserValue(key, PATH_NAME) :
WindowsRegistry.getRegistry().getLocalMachineValue(key, PATH_NAME);
if (s != null)
return (s.concat(tail).replaceAll(BSLASH, SSLASH));
if (pattern.equals(ROOTPATTERN))
break; // no other paths to search
int pos = pattern.lastIndexOf(SLASH);
if (pos < 0) break;
tail = pattern.substring(pos, pattern.length()) + tail;
@ -185,13 +149,33 @@ public class CygwinPathResolver implements IBuildPathResolver {
else
pattern = pattern.substring(0, pos);
}
return null;
return null;
}
/**
* reads once data from registry (for Win32 only)
* and sets corresponding properties;
*/
private static synchronized void checkRegistry() {
if (checked) return;
etcCygwin = null;
binCygwin = null;
rootCygwin = null;
if (!isWindows()) return;
for (int i=0; i<2; i++) {
if (etcCygwin == null)
etcCygwin = read((i==0), ETCPATTERN);
if (binCygwin == null)
binCygwin = read((i==0), BINPATTERN);
if (rootCygwin == null)
rootCygwin = read((i==0), ROOTPATTERN);
}
checked = true;
}
private static String[] exec(String cmd, IConfiguration cfg) {
try {
// Process proc = Runtime.getRuntime().exec(cmd);
IBuildEnvironmentVariable vars[] = ManagedBuildManager.getEnvironmentVariableProvider().getVariables(cfg,true,true);
IEnvironmentVariable vars[] = ManagedBuildManager.getEnvironmentVariableProvider().getVariables(cfg,true);
String env[] = new String[vars.length];
for(int i = 0; i < env.length; i++){
env[i] = vars[i].getName() + "=";
@ -204,16 +188,16 @@ public class CygwinPathResolver implements IBuildPathResolver {
InputStream ein = proc.getInputStream();
BufferedReader d1 = new BufferedReader(new InputStreamReader(ein));
ArrayList ls = new ArrayList(10);
ArrayList<String> ls = new ArrayList<String>(10);
String s;
while ((s = d1.readLine() ) != null ) {
ls.add(s);
}
ein.close();
return (String[])ls.toArray(new String[0]);
return ls.toArray(new String[0]);
}
} catch (IOException e) {
//TODO: log
GnuUIPlugin.getDefault().log(e);
}
return null;
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005 Texas Instruments Inc. and others.
* Copyright (c) 2005, 2008 Texas Instruments Inc. and others.
* 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
@ -11,7 +11,9 @@
package org.eclipse.cdt.managedbuilder.gnu.ui;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.BundleContext;
import java.util.*;
@ -19,6 +21,8 @@ import java.util.*;
* The main plugin class to be used in the desktop.
*/
public class GnuUIPlugin extends Plugin {
public static final String PLUGIN_ID = "org.eclipse.cdt.managedbuilder.gnu.ui"; //$NON-NLS-1$
//The shared instance.
private static GnuUIPlugin plugin;
//Resource bundle.
@ -81,4 +85,11 @@ public class GnuUIPlugin extends Plugin {
return resourceBundle;
}
public void log(Throwable e) {
log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Error", e)); //$NON-NLS-1$
}
public void log(IStatus status) {
getLog().log(status);
}
}