PageRenderTime 60ms CodeModel.GetById 27ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/plugins/Console/tags/release-3-0-oops/console/OperatingSystem.java

#
Java | 321 lines | 248 code | 48 blank | 25 comment | 31 complexity | 02c6fc13f850c00d2f215368ffd168e9 MD5 | raw file
  1/*
  2 * OperatingSystem.java - Abstracts away OS-specific stuff
  3 * Copyright (C) 2001 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package console;
 21
 22import java.lang.reflect.*;
 23import java.io.*;
 24import java.util.*;
 25import org.gjt.sp.jedit.*;
 26import org.gjt.sp.util.Log;
 27
 28abstract class OperatingSystem
 29{
 30	abstract boolean shellExpandsGlobs();
 31
 32	abstract boolean supportsEnvironmentVariables();
 33
 34	abstract Hashtable getEnvironmentVariables();
 35
 36	abstract void setUpDefaultAliases(Hashtable aliases);
 37
 38	abstract boolean cdCommandAvailable();
 39
 40	abstract Process exec(String[] args, String[] env, String dir)
 41			throws Exception;
 42
 43	static OperatingSystem getOperatingSystem()
 44	{
 45		if(os == null)
 46		{
 47			String osName = System.getProperty("os.name");
 48			if(osName.startsWith("Windows 9"))
 49				os = new Windows9x();
 50			else if(osName.startsWith("Windows"))
 51				os = new WindowsNT();
 52			else if(File.separatorChar == '/'
 53				&& (osName.indexOf("MacOS") == -1
 54				|| osName.indexOf("MacOS X") != -1))
 55				os = new Unix();
 56			else
 57				os = new Generic();
 58		}
 59
 60		return os;
 61	}
 62
 63	private static OperatingSystem os;
 64
 65	static class Generic extends OperatingSystem
 66	{
 67		Generic()
 68		{
 69			try
 70			{
 71				Class[] classes = { String[].class, String[].class, File.class };
 72				java13exec = Runtime.class.getMethod("exec",classes);
 73			}
 74			catch(Exception e)
 75			{
 76				// use Java 1.1/1.2 code instead
 77			}
 78		}
 79
 80		boolean shellExpandsGlobs()
 81		{
 82			return true;
 83		}
 84
 85		boolean supportsEnvironmentVariables()
 86		{
 87			return false;
 88		}
 89
 90		Hashtable getEnvironmentVariables()
 91		{
 92			return new Hashtable();
 93		}
 94
 95		void setUpDefaultAliases(Hashtable aliases)
 96		{
 97		}
 98
 99		boolean cdCommandAvailable()
100		{
101			return java13exec != null;
102		}
103
104		Process exec(String[] args, String[] env, String dir)
105			throws Exception
106		{
107			try
108			{
109				if(java13exec != null)
110				{
111					Object[] methodArgs = { args,env,
112						new File(dir) };
113					return (Process)java13exec.invoke(
114						Runtime.getRuntime(),methodArgs);
115				}
116				else
117				{
118					return Runtime.getRuntime().exec(args,env);
119				}
120			}
121			catch(InvocationTargetException ite)
122			{
123				throw (Exception)ite.getTargetException();
124			}
125			catch(Exception e)
126			{
127				throw e;
128			}
129		}
130
131		private Method java13exec;
132	}
133
134	static class Unix extends Generic
135	{
136		boolean supportsEnvironmentVariables()
137		{
138			return true;
139		}
140
141		Hashtable getEnvironmentVariables()
142		{
143			Hashtable vars = new Hashtable();
144
145			// run env, extract output
146			try
147			{
148				Process env = Runtime.getRuntime().exec("env");
149				BufferedReader in = new BufferedReader(
150					new InputStreamReader(
151					env.getInputStream()));
152
153				String line;
154				while((line = in.readLine()) != null)
155				{
156					Log.log(Log.DEBUG,this,line);
157					int index = line.indexOf('=');
158					if(index != -1)
159					{
160						vars.put(line.substring(0,index),
161							line.substring(index + 1));
162					}
163				}
164
165				in.close();
166			}
167			catch(IOException io)
168			{
169				Log.log(Log.ERROR,this,io);
170			}
171
172			return vars;
173		}
174	}
175
176	abstract static class Windows extends Generic
177	{
178		String[] getExtensionsToTry()
179		{
180			if(extensionsToTry == null)
181				getEnvironmentVariables();
182
183			return extensionsToTry;
184		}
185
186		boolean shellExpandsGlobs()
187		{
188			return false;
189		}
190
191		boolean supportsEnvironmentVariables()
192		{
193			return true;
194		}
195
196		Hashtable getEnvironmentVariables()
197		{
198			Hashtable vars = new Hashtable();
199
200			// run env, extract output
201			try
202			{
203				Process env = Runtime.getRuntime().exec(
204					getBuiltInPrefix() + "set");
205				BufferedReader in = new BufferedReader(
206					new InputStreamReader(
207					env.getInputStream()));
208
209				String line;
210				while((line = in.readLine()) != null)
211				{
212					Log.log(Log.DEBUG,this,line);
213					int index = line.indexOf('=');
214					if(index != -1)
215					{
216						vars.put(line.substring(0,index),
217							line.substring(index + 1));
218					}
219				}
220
221				in.close();
222			}
223			catch(IOException io)
224			{
225				Log.log(Log.ERROR,this,io);
226			}
227
228			String pathext = (String)vars.get("PATHEXT");
229			if(pathext != null)
230			{
231				Vector _extensionsToTry = new Vector();
232
233				StringTokenizer st = new StringTokenizer(pathext,"; ");
234				while(st.hasMoreTokens())
235				{
236					_extensionsToTry.addElement(st.nextToken());
237				}
238
239				extensionsToTry = new String[_extensionsToTry.size()];
240				_extensionsToTry.copyInto(extensionsToTry);
241			}
242			else
243			{
244				extensionsToTry = new String[] { ".cmd",
245					".bat", ".exe", ".com" };
246			}
247
248			return vars;
249		}
250
251		Process exec(String[] args, String[] env, String dir)
252			throws Exception
253		{
254
255			String commandName = args[0];
256
257			String[] extensionsToTry;
258			if(commandName.indexOf('.') == -1)
259				extensionsToTry = getExtensionsToTry();
260			else
261				extensionsToTry = new String[] { "" };
262
263			for(int i = 0; i < extensionsToTry.length; i++)
264			{
265				args[0] = commandName + extensionsToTry[i];
266
267				try
268				{
269					return super.exec(args,env,dir);
270				}
271				catch(Exception e)
272				{
273					if(i == extensionsToTry.length - 1)
274					{
275						// throw a new exception cos
276						// Windows error messages are
277						// a bit cryptic
278						throw new Exception(
279							jEdit.getProperty(
280							"console.shell.not-found-win",
281							new String[] { commandName, }));
282					}
283				}
284			}
285
286			// can't happen
287			return null;
288		}
289
290		abstract String getBuiltInPrefix();
291
292		void setUpDefaultAliases(Hashtable aliases)
293		{
294			String[] builtins  = { "md", "rd", "del", "dir", "copy",
295				"move", "erase", "mkdir", "rmdir", "start", "echo",
296				"path", "ver", "vol", "ren", "type"};
297			for(int i = 0; i < builtins.length; i++)
298			{
299				aliases.put(builtins[i],getBuiltInPrefix() + builtins[i]);
300			}
301		}
302
303		private String[] extensionsToTry;
304	}
305
306	static class Windows9x extends Windows
307	{
308		String getBuiltInPrefix()
309		{
310			return "command.com /c ";
311		}
312	}
313
314	static class WindowsNT extends Windows
315	{
316		String getBuiltInPrefix()
317		{
318			return "cmd.exe /c ";
319		}
320	}
321}