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