PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Console/tags/release-3-1/console/OperatingSystem.java

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