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

Language Java Lines 335
MD5 Hash 2c1d34c391c31be1ad5aeaa8ec7e0989 Estimated Cost $6,514 (why?)
Repository https://jedit.svn.sourceforge.net/svnroot/jedit View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * OperatingSystem.java - Abstracts away OS-specific stuff
 * Copyright (C) 2001 Slava Pestov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package console;

import java.lang.reflect.*;
import java.io.*;
import java.util.*;
import org.gjt.sp.jedit.*;
import org.gjt.sp.util.Log;

abstract class OperatingSystem
{
	abstract boolean shellExpandsGlobs();

	abstract boolean supportsEnvironmentVariables();

	abstract Hashtable getEnvironmentVariables();

	abstract void setUpDefaultAliases(Hashtable aliases);

	abstract boolean cdCommandAvailable();

	abstract Process exec(String[] args, String[] env, String dir)
			throws Exception;

	static OperatingSystem getOperatingSystem()
	{
		if(os == null)
		{
			String osName = System.getProperty("os.name");
			if(osName.startsWith("Windows 9")
				|| osName.equals("Windows ME"))
				os = new Windows9x();
			else if(osName.startsWith("Windows"))
				os = new WindowsNT();
			else if(File.separatorChar == '/'
				&& (osName.indexOf("MacOS") == -1
				|| osName.indexOf("MacOS X") != -1))
				os = new Unix();
			else
				os = new Generic();
		}

		return os;
	}

	private static OperatingSystem os;

	static class Generic extends OperatingSystem
	{
		Generic()
		{
			try
			{
				Class[] classes = { String[].class, String[].class, File.class };
				java13exec = Runtime.class.getMethod("exec",classes);
			}
			catch(Exception e)
			{
				// use Java 1.1/1.2 code instead
			}
		}

		boolean shellExpandsGlobs()
		{
			return true;
		}

		boolean supportsEnvironmentVariables()
		{
			return false;
		}

		Hashtable getEnvironmentVariables()
		{
			return new Hashtable();
		}

		void setUpDefaultAliases(Hashtable aliases)
		{
		}

		boolean cdCommandAvailable()
		{
			return java13exec != null;
		}

		Process exec(String[] args, String[] env, String dir)
			throws Exception
		{
			try
			{
				if(java13exec != null)
				{
					Object[] methodArgs = { args,env,
						new File(dir) };
					return (Process)java13exec.invoke(
						Runtime.getRuntime(),methodArgs);
				}
				else
				{
					return Runtime.getRuntime().exec(args,env);
				}
			}
			catch(InvocationTargetException ite)
			{
				throw (Exception)ite.getTargetException();
			}
			catch(Exception e)
			{
				throw e;
			}
		}

		private Method java13exec;
	}

	static class Unix extends Generic
	{
		boolean supportsEnvironmentVariables()
		{
			return true;
		}

		Hashtable getEnvironmentVariables()
		{
			Hashtable vars = new Hashtable();

			// run env, extract output
			try
			{
				Process env = Runtime.getRuntime().exec("env");
				BufferedReader in = new BufferedReader(
					new InputStreamReader(
					env.getInputStream()));

				String line;
				while((line = in.readLine()) != null)
				{
					Log.log(Log.DEBUG,this,line);
					int index = line.indexOf('=');
					if(index != -1)
					{
						vars.put(line.substring(0,index),
							line.substring(index + 1));
					}
				}

				in.close();
			}
			catch(IOException io)
			{
				Log.log(Log.ERROR,this,io);
			}

			return vars;
		}
	}

	abstract static class Windows extends Generic
	{
		boolean shellExpandsGlobs()
		{
			return false;
		}

		Process exec(String[] args, String[] env, String dir)
			throws Exception
		{

			String commandName = args[0];

			String[] extensionsToTry;
			if(commandName.indexOf('.') == -1)
				extensionsToTry = getExtensionsToTry();
			else
				extensionsToTry = new String[] { "" };

			for(int i = 0; i < extensionsToTry.length; i++)
			{
				args[0] = commandName + extensionsToTry[i];

				try
				{
					return super.exec(args,env,dir);
				}
				catch(Exception e)
				{
					if(i == extensionsToTry.length - 1)
					{
						// throw a new exception cos
						// Windows error messages are
						// a bit cryptic
						throw new Exception(
							jEdit.getProperty(
							"console.shell.not-found-win",
							new String[] { commandName, }));
					}
				}
			}

			// can't happen
			return null;
		}

		abstract String getBuiltInPrefix();

		abstract String[] getExtensionsToTry();

		void setUpDefaultAliases(Hashtable aliases)
		{
			String[] builtins  = { "md", "rd", "del", "dir", "copy",
				"move", "erase", "mkdir", "rmdir", "start", "echo",
				"path", "ver", "vol", "ren", "type"};
			for(int i = 0; i < builtins.length; i++)
			{
				aliases.put(builtins[i],getBuiltInPrefix() + builtins[i]);
			}
		}
	}

	static class Windows9x extends Windows
	{
		boolean supportsEnvironmentVariables()
		{
			return false;
		}

		String getBuiltInPrefix()
		{
			return "command.com /c ";
		}

		String[] getExtensionsToTry()
		{
			return new String[] { ".cmd", ".bat", ".exe", ".com" };
		}
	}

	static class WindowsNT extends Windows
	{
		boolean supportsEnvironmentVariables()
		{
			return true;
		}

		Hashtable getEnvironmentVariables()
		{
			Hashtable vars = new Hashtable();

			// run env, extract output
			try
			{
				Process env = Runtime.getRuntime().exec(
					getBuiltInPrefix() + "set");
				BufferedReader in = new BufferedReader(
					new InputStreamReader(
					env.getInputStream()));

				String line;
				while((line = in.readLine()) != null)
				{
					Log.log(Log.DEBUG,this,line);
					int index = line.indexOf('=');
					if(index != -1)
					{
						vars.put(line.substring(0,index),
							line.substring(index + 1));
					}
				}

				in.close();
			}
			catch(IOException io)
			{
				Log.log(Log.ERROR,this,io);
			}

			String pathext = (String)vars.get("PATHEXT");
			if(pathext != null)
			{
				Vector _extensionsToTry = new Vector();

				StringTokenizer st = new StringTokenizer(pathext,"; ");
				while(st.hasMoreTokens())
				{
					_extensionsToTry.addElement(st.nextToken());
				}

				extensionsToTry = new String[_extensionsToTry.size()];
				_extensionsToTry.copyInto(extensionsToTry);
			}
			else
			{
				extensionsToTry = new String[] { ".cmd",
					".bat", ".exe", ".com" };
			}

			return vars;
		}

		String getBuiltInPrefix()
		{
			return "cmd.exe /c ";
		}

		String[] getExtensionsToTry()
		{
			if(extensionsToTry == null)
				getEnvironmentVariables();

			return extensionsToTry;
		}

		String[] extensionsToTry;
	}
}
Back to Top