PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 334 lines | 258 code | 51 blank | 25 comment | 32 complexity | 2c1d34c391c31be1ad5aeaa8ec7e0989 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package console;
  20. import java.lang.reflect.*;
  21. import java.io.*;
  22. import java.util.*;
  23. import org.gjt.sp.jedit.*;
  24. import org.gjt.sp.util.Log;
  25. abstract class OperatingSystem
  26. {
  27. abstract boolean shellExpandsGlobs();
  28. abstract boolean supportsEnvironmentVariables();
  29. abstract Hashtable getEnvironmentVariables();
  30. abstract void setUpDefaultAliases(Hashtable aliases);
  31. abstract boolean cdCommandAvailable();
  32. abstract Process exec(String[] args, String[] env, String dir)
  33. throws Exception;
  34. static OperatingSystem getOperatingSystem()
  35. {
  36. if(os == null)
  37. {
  38. String osName = System.getProperty("os.name");
  39. if(osName.startsWith("Windows 9")
  40. || osName.equals("Windows ME"))
  41. os = new Windows9x();
  42. else if(osName.startsWith("Windows"))
  43. os = new WindowsNT();
  44. else if(File.separatorChar == '/'
  45. && (osName.indexOf("MacOS") == -1
  46. || osName.indexOf("MacOS X") != -1))
  47. os = new Unix();
  48. else
  49. os = new Generic();
  50. }
  51. return os;
  52. }
  53. private static OperatingSystem os;
  54. static class Generic extends OperatingSystem
  55. {
  56. Generic()
  57. {
  58. try
  59. {
  60. Class[] classes = { String[].class, String[].class, File.class };
  61. java13exec = Runtime.class.getMethod("exec",classes);
  62. }
  63. catch(Exception e)
  64. {
  65. // use Java 1.1/1.2 code instead
  66. }
  67. }
  68. boolean shellExpandsGlobs()
  69. {
  70. return true;
  71. }
  72. boolean supportsEnvironmentVariables()
  73. {
  74. return false;
  75. }
  76. Hashtable getEnvironmentVariables()
  77. {
  78. return new Hashtable();
  79. }
  80. void setUpDefaultAliases(Hashtable aliases)
  81. {
  82. }
  83. boolean cdCommandAvailable()
  84. {
  85. return java13exec != null;
  86. }
  87. Process exec(String[] args, String[] env, String dir)
  88. throws Exception
  89. {
  90. try
  91. {
  92. if(java13exec != null)
  93. {
  94. Object[] methodArgs = { args,env,
  95. new File(dir) };
  96. return (Process)java13exec.invoke(
  97. Runtime.getRuntime(),methodArgs);
  98. }
  99. else
  100. {
  101. return Runtime.getRuntime().exec(args,env);
  102. }
  103. }
  104. catch(InvocationTargetException ite)
  105. {
  106. throw (Exception)ite.getTargetException();
  107. }
  108. catch(Exception e)
  109. {
  110. throw e;
  111. }
  112. }
  113. private Method java13exec;
  114. }
  115. static class Unix extends Generic
  116. {
  117. boolean supportsEnvironmentVariables()
  118. {
  119. return true;
  120. }
  121. Hashtable getEnvironmentVariables()
  122. {
  123. Hashtable vars = new Hashtable();
  124. // run env, extract output
  125. try
  126. {
  127. Process env = Runtime.getRuntime().exec("env");
  128. BufferedReader in = new BufferedReader(
  129. new InputStreamReader(
  130. env.getInputStream()));
  131. String line;
  132. while((line = in.readLine()) != null)
  133. {
  134. Log.log(Log.DEBUG,this,line);
  135. int index = line.indexOf('=');
  136. if(index != -1)
  137. {
  138. vars.put(line.substring(0,index),
  139. line.substring(index + 1));
  140. }
  141. }
  142. in.close();
  143. }
  144. catch(IOException io)
  145. {
  146. Log.log(Log.ERROR,this,io);
  147. }
  148. return vars;
  149. }
  150. }
  151. abstract static class Windows extends Generic
  152. {
  153. boolean shellExpandsGlobs()
  154. {
  155. return false;
  156. }
  157. Process exec(String[] args, String[] env, String dir)
  158. throws Exception
  159. {
  160. String commandName = args[0];
  161. String[] extensionsToTry;
  162. if(commandName.indexOf('.') == -1)
  163. extensionsToTry = getExtensionsToTry();
  164. else
  165. extensionsToTry = new String[] { "" };
  166. for(int i = 0; i < extensionsToTry.length; i++)
  167. {
  168. args[0] = commandName + extensionsToTry[i];
  169. try
  170. {
  171. return super.exec(args,env,dir);
  172. }
  173. catch(Exception e)
  174. {
  175. if(i == extensionsToTry.length - 1)
  176. {
  177. // throw a new exception cos
  178. // Windows error messages are
  179. // a bit cryptic
  180. throw new Exception(
  181. jEdit.getProperty(
  182. "console.shell.not-found-win",
  183. new String[] { commandName, }));
  184. }
  185. }
  186. }
  187. // can't happen
  188. return null;
  189. }
  190. abstract String getBuiltInPrefix();
  191. abstract String[] getExtensionsToTry();
  192. void setUpDefaultAliases(Hashtable aliases)
  193. {
  194. String[] builtins = { "md", "rd", "del", "dir", "copy",
  195. "move", "erase", "mkdir", "rmdir", "start", "echo",
  196. "path", "ver", "vol", "ren", "type"};
  197. for(int i = 0; i < builtins.length; i++)
  198. {
  199. aliases.put(builtins[i],getBuiltInPrefix() + builtins[i]);
  200. }
  201. }
  202. }
  203. static class Windows9x extends Windows
  204. {
  205. boolean supportsEnvironmentVariables()
  206. {
  207. return false;
  208. }
  209. String getBuiltInPrefix()
  210. {
  211. return "command.com /c ";
  212. }
  213. String[] getExtensionsToTry()
  214. {
  215. return new String[] { ".cmd", ".bat", ".exe", ".com" };
  216. }
  217. }
  218. static class WindowsNT extends Windows
  219. {
  220. boolean supportsEnvironmentVariables()
  221. {
  222. return true;
  223. }
  224. Hashtable getEnvironmentVariables()
  225. {
  226. Hashtable vars = new Hashtable();
  227. // run env, extract output
  228. try
  229. {
  230. Process env = Runtime.getRuntime().exec(
  231. getBuiltInPrefix() + "set");
  232. BufferedReader in = new BufferedReader(
  233. new InputStreamReader(
  234. env.getInputStream()));
  235. String line;
  236. while((line = in.readLine()) != null)
  237. {
  238. Log.log(Log.DEBUG,this,line);
  239. int index = line.indexOf('=');
  240. if(index != -1)
  241. {
  242. vars.put(line.substring(0,index),
  243. line.substring(index + 1));
  244. }
  245. }
  246. in.close();
  247. }
  248. catch(IOException io)
  249. {
  250. Log.log(Log.ERROR,this,io);
  251. }
  252. String pathext = (String)vars.get("PATHEXT");
  253. if(pathext != null)
  254. {
  255. Vector _extensionsToTry = new Vector();
  256. StringTokenizer st = new StringTokenizer(pathext,"; ");
  257. while(st.hasMoreTokens())
  258. {
  259. _extensionsToTry.addElement(st.nextToken());
  260. }
  261. extensionsToTry = new String[_extensionsToTry.size()];
  262. _extensionsToTry.copyInto(extensionsToTry);
  263. }
  264. else
  265. {
  266. extensionsToTry = new String[] { ".cmd",
  267. ".bat", ".exe", ".com" };
  268. }
  269. return vars;
  270. }
  271. String getBuiltInPrefix()
  272. {
  273. return "cmd.exe /c ";
  274. }
  275. String[] getExtensionsToTry()
  276. {
  277. if(extensionsToTry == null)
  278. getEnvironmentVariables();
  279. return extensionsToTry;
  280. }
  281. String[] extensionsToTry;
  282. }
  283. }