/plugins/Console/tags/release-4-1-2/console/SystemShellBuiltIn.java

# · Java · 724 lines · 565 code · 98 blank · 61 comment · 57 complexity · 74c1e8635f6b58e2a8d4cd3fedc3d330 MD5 · raw file

  1. /*
  2. * SystemShellBuiltIn.java - Commands handled by system shell itself
  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.util.*;
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.jedit.browser.VFSBrowser;
  27. import org.gjt.sp.jedit.gui.DockableWindowManager;
  28. import org.gjt.sp.jedit.help.HelpViewer;
  29. import org.gjt.sp.jedit.io.VFSManager;
  30. import org.gjt.sp.util.Log;
  31. //}}}
  32. public abstract class SystemShellBuiltIn
  33. {
  34. //{{{ SystemShellBuiltIn constructor
  35. public SystemShellBuiltIn()
  36. {
  37. name = getClass().getName();
  38. name = name.substring(name.lastIndexOf('$') + 1);
  39. help = jEdit.getProperty("console.shell." + name + ".usage");
  40. if(help == null)
  41. Log.log(Log.WARNING,this,name + " is missing usage info");
  42. } //}}}
  43. //{{{ getOptions() method
  44. public Option[] getOptions()
  45. {
  46. return new Option[0];
  47. } //}}}
  48. //{{{ getConsoleState() method
  49. private static SystemShell.ConsoleState getConsoleState(Console console)
  50. {
  51. return ConsolePlugin.getSystemShell().getConsoleState(console);
  52. } //}}}
  53. //{{{ Option class
  54. public class Option
  55. {
  56. public char shortName;
  57. public String longName;
  58. public boolean takesArgument;
  59. public Option(char shortName, String longName, boolean takesArgument)
  60. {
  61. this.shortName = shortName;
  62. this.longName = longName;
  63. this.takesArgument = takesArgument;
  64. }
  65. } //}}}
  66. //{{{ getMinArguments() method
  67. public int getMinArguments()
  68. {
  69. return 0;
  70. } //}}}
  71. //{{{ getMaxArgument() method
  72. public int getMaxArguments()
  73. {
  74. // meaning, no maximum
  75. return -1;
  76. } //}}}
  77. //{{{ execute() method
  78. public void execute(Console console, Output output, Output error, Vector args)
  79. {
  80. Hashtable values = new Hashtable();
  81. Option[] options = getOptions();
  82. for(int i = 0; i < args.size(); i++)
  83. {
  84. String arg = (String)args.elementAt(i);
  85. //{{{ end of options
  86. if(arg.equals("--"))
  87. {
  88. args.removeElementAt(i);
  89. break;
  90. }
  91. else if(arg.equals("--help"))
  92. {
  93. error.print(null,help);
  94. return;
  95. } //}}}
  96. //{{{ long option
  97. else if(arg.startsWith("--"))
  98. {
  99. if(arg.length() == 2)
  100. continue;
  101. args.removeElementAt(i);
  102. i--;
  103. String longName = arg.substring(2);
  104. boolean no;
  105. if(longName.startsWith("no-"))
  106. {
  107. no = true;
  108. longName = longName.substring(3);
  109. }
  110. else
  111. no = false;
  112. Option option = null;
  113. for(int j = 0; j < options.length; j++)
  114. {
  115. if(options[j].longName.equals(longName))
  116. {
  117. option = options[j];
  118. break;
  119. }
  120. }
  121. if(option == null)
  122. {
  123. String[] pp = { longName };
  124. error.print(console.getErrorColor(),
  125. jEdit.getProperty("console.shell.bad-opt-long",pp));
  126. return;
  127. }
  128. if(option.takesArgument)
  129. {
  130. if(no)
  131. {
  132. String[] pp = { longName };
  133. error.print(console.getErrorColor(),
  134. jEdit.getProperty("console.shell.no-arg-long",pp));
  135. return;
  136. }
  137. if(i == args.size() - 1)
  138. {
  139. String[] pp = { longName };
  140. error.print(console.getErrorColor(),
  141. jEdit.getProperty("console.shell.need-arg-long",pp));
  142. return;
  143. }
  144. values.put(longName,args.elementAt(i + 1));
  145. args.removeElementAt(i + 1);
  146. }
  147. else
  148. {
  149. if(no)
  150. values.put(longName,Boolean.FALSE);
  151. else
  152. values.put(longName,Boolean.TRUE);
  153. }
  154. } //}}}
  155. //{{{ short option
  156. else if(arg.startsWith("-") || arg.startsWith("+"))
  157. {
  158. if(arg.length() == 1)
  159. continue;
  160. args.removeElementAt(i);
  161. i--;
  162. boolean no = (arg.charAt(0) == '+');
  163. for(int j = 1; j < arg.length(); j++)
  164. {
  165. char shortName = arg.charAt(j);
  166. Option option = null;
  167. for(int k = 0; k < options.length; k++)
  168. {
  169. if(options[k].shortName == shortName)
  170. {
  171. option = options[k];
  172. break;
  173. }
  174. }
  175. if(option == null)
  176. {
  177. String[] pp = { String.valueOf(shortName) };
  178. error.print(console.getErrorColor(),
  179. jEdit.getProperty("console.shell.bad-opt",pp));
  180. return;
  181. }
  182. if(no)
  183. values.put(option.longName,Boolean.FALSE);
  184. else
  185. values.put(option.longName,Boolean.TRUE);
  186. }
  187. } //}}}
  188. }
  189. int min = getMinArguments();
  190. int max = getMaxArguments();
  191. if(args.size() < getMinArguments()
  192. || (max != -1 && args.size() > getMaxArguments()))
  193. {
  194. error.print(console.getErrorColor(),
  195. jEdit.getProperty("console.shell.bad-args"));
  196. return;
  197. }
  198. execute(console,output,error,args,values);
  199. } //}}}
  200. //{{{ execute() method
  201. protected abstract void execute(Console console, Output output,
  202. Output error, Vector args, Hashtable values); //}}}
  203. //}}}
  204. //{{{ Protected members
  205. protected String name;
  206. protected String help;
  207. //}}}
  208. //{{{ Inner classes
  209. //{{{ alias class
  210. static class alias extends SystemShellBuiltIn
  211. {
  212. public int getMinArguments()
  213. {
  214. return 2;
  215. }
  216. public int getMaxArguments()
  217. {
  218. return 2;
  219. }
  220. public void execute(Console console, Output output,
  221. Output error, Vector args, Hashtable values)
  222. {
  223. Hashtable aliases = ConsolePlugin.getSystemShell()
  224. .getAliases();
  225. aliases.put(args.elementAt(0),args.elementAt(1));
  226. }
  227. } //}}}
  228. //{{{ aliases class
  229. static class aliases extends SystemShellBuiltIn
  230. {
  231. public int getMinArguments()
  232. {
  233. return 0;
  234. }
  235. public int getMaxArguments()
  236. {
  237. return 0;
  238. }
  239. public void execute(Console console, Output output,
  240. Output error, Vector args, Hashtable values)
  241. {
  242. Hashtable aliases = ConsolePlugin.getSystemShell().getAliases();
  243. Vector returnValue = new Vector();
  244. Enumeration keys = aliases.keys();
  245. while(keys.hasMoreElements())
  246. {
  247. Object key = keys.nextElement();
  248. returnValue.addElement(key + "=" + aliases.get(key));
  249. }
  250. MiscUtilities.quicksort(returnValue,
  251. new MiscUtilities.StringICaseCompare());
  252. for(int i = 0; i < returnValue.size(); i++)
  253. {
  254. output.print(null,(String)returnValue.elementAt(i));
  255. }
  256. }
  257. } //}}}
  258. //{{{ browse class
  259. static class browse extends SystemShellBuiltIn
  260. {
  261. public int getMaxArguments()
  262. {
  263. return 1;
  264. }
  265. public Option[] getOptions()
  266. {
  267. return new Option[] {
  268. new Option('n',"new-window",false)
  269. };
  270. }
  271. public void execute(Console console, Output output,
  272. Output error, Vector args, Hashtable values)
  273. {
  274. String currentDirectory = getConsoleState(
  275. console).currentDirectory;
  276. String directory = (args.size() == 0
  277. ? currentDirectory
  278. : MiscUtilities.constructPath(currentDirectory,
  279. (String)args.elementAt(0)));
  280. if(values.get("new-window") != null)
  281. VFSBrowser.browseDirectoryInNewWindow(console.getView(),directory);
  282. else
  283. VFSBrowser.browseDirectory(console.getView(),directory);
  284. }
  285. } //}}}
  286. //{{{ cd class
  287. static class cd extends SystemShellBuiltIn
  288. {
  289. public int getMaxArguments()
  290. {
  291. return 1;
  292. }
  293. public void execute(Console console, Output output,
  294. Output error, Vector args, Hashtable values)
  295. {
  296. SystemShell.ConsoleState state = getConsoleState(console);
  297. String newDir;
  298. if(args.size() == 0)
  299. {
  300. state.setCurrentDirectory(console,
  301. System.getProperty("user.home"));
  302. }
  303. else
  304. {
  305. String arg = (String)args.elementAt(0);
  306. if(arg.equals("-"))
  307. state.gotoLastDirectory(console);
  308. else
  309. {
  310. state.setCurrentDirectory(console,
  311. MiscUtilities.constructPath(
  312. state.currentDirectory,
  313. (String)args.elementAt(0)));
  314. }
  315. }
  316. }
  317. } //}}}
  318. //{{{ clear class
  319. static class clear extends SystemShellBuiltIn
  320. {
  321. public int getMinArguments()
  322. {
  323. return 0;
  324. }
  325. public int getMaxArguments()
  326. {
  327. return 0;
  328. }
  329. public void execute(Console console, Output output,
  330. Output error, Vector args, Hashtable values)
  331. {
  332. console.clear();
  333. }
  334. } //}}}
  335. //{{{ dirstack class
  336. static class dirstack extends SystemShellBuiltIn
  337. {
  338. public int getMaxArguments()
  339. {
  340. return 0;
  341. }
  342. public void execute(Console console, Output output,
  343. Output error, Vector args, Hashtable values)
  344. {
  345. Stack directoryStack = getConsoleState(console)
  346. .directoryStack;
  347. for(int i = 0; i < directoryStack.size(); i++)
  348. {
  349. output.print(null,(String)directoryStack.elementAt(i));
  350. }
  351. }
  352. } //}}}
  353. //{{{ echo class
  354. static class echo extends SystemShellBuiltIn
  355. {
  356. public int getMinArguments()
  357. {
  358. return 1;
  359. }
  360. public int getMaxArguments()
  361. {
  362. return -1;
  363. }
  364. public void execute(Console console, Output output,
  365. Output error, Vector args, Hashtable values)
  366. {
  367. for(int i = 0; i < args.size(); i++)
  368. {
  369. output.print(null,(String)args.elementAt(i));
  370. }
  371. }
  372. } //}}}
  373. //{{{ edit class
  374. static class edit extends SystemShellBuiltIn
  375. {
  376. public int getMinArguments()
  377. {
  378. return 1;
  379. }
  380. public void execute(Console console, Output output,
  381. Output error, Vector args, Hashtable values)
  382. {
  383. String currentDirectory = getConsoleState(
  384. console).currentDirectory;
  385. for(int i = 0; i < args.size(); i++)
  386. {
  387. jEdit.openFile(console.getView(),currentDirectory,
  388. (String)args.elementAt(i),false,null);
  389. }
  390. }
  391. } //}}}
  392. //{{{ env class
  393. static class env extends SystemShellBuiltIn
  394. {
  395. public int getMinArguments()
  396. {
  397. return 0;
  398. }
  399. public int getMaxArguments()
  400. {
  401. return 0;
  402. }
  403. public void execute(Console console, Output output,
  404. Output error, Vector args, Hashtable values)
  405. {
  406. Hashtable variables = ConsolePlugin.getSystemShell().getVariables();
  407. Vector returnValue = new Vector();
  408. Enumeration keys = variables.keys();
  409. while(keys.hasMoreElements())
  410. {
  411. Object key = keys.nextElement();
  412. returnValue.addElement(key + "=" + variables.get(key));
  413. }
  414. MiscUtilities.quicksort(returnValue,
  415. new MiscUtilities.StringICaseCompare());
  416. for(int i = 0; i < returnValue.size(); i++)
  417. {
  418. output.print(null,(String)returnValue.elementAt(i));
  419. }
  420. }
  421. } //}}}
  422. //{{{ help class
  423. static class help extends SystemShellBuiltIn
  424. {
  425. static String HELP_PATH = "jeditresource:/Console.jar!/index.html";
  426. public int getMinArguments()
  427. {
  428. return 0;
  429. }
  430. public int getMaxArguments()
  431. {
  432. return 1;
  433. }
  434. public void execute(Console console, Output output,
  435. Output error, Vector args, Hashtable values)
  436. {
  437. if(args.size() == 1)
  438. {
  439. String cmd = (String)args.get(0);
  440. if(cmd.startsWith("%"))
  441. cmd = cmd.substring(1);
  442. String help = jEdit.getProperty("console.shell."
  443. + cmd + ".usage");
  444. // if command name specified, print its usage
  445. if(help != null)
  446. error.print(null,help);
  447. else
  448. new HelpViewer(cmd);
  449. }
  450. else
  451. new HelpViewer(HELP_PATH);
  452. }
  453. } //}}}
  454. //{{{ kill class
  455. static class kill extends SystemShellBuiltIn
  456. {
  457. public int getMaxArguments()
  458. {
  459. return 1;
  460. }
  461. public void execute(Console console, Output output,
  462. Output error, Vector args, Hashtable values)
  463. {
  464. SystemShell.ConsoleState state = getConsoleState(console);
  465. ConsoleProcess process = state.process;
  466. if(process == null)
  467. {
  468. error.print(console.getErrorColor(),
  469. jEdit.getProperty("console.shell.noproc"));
  470. return;
  471. }
  472. process.stop();
  473. }
  474. } //}}}
  475. //{{{ popd class
  476. static class popd extends SystemShellBuiltIn
  477. {
  478. public int getMaxArguments()
  479. {
  480. return 0;
  481. }
  482. public void execute(Console console, Output output,
  483. Output error, Vector args, Hashtable values)
  484. {
  485. SystemShell.ConsoleState state = getConsoleState(console);
  486. Stack directoryStack = state.directoryStack;
  487. if(directoryStack.isEmpty())
  488. {
  489. error.print(console.getErrorColor(),
  490. jEdit.getProperty("console.shell.popd.error"));
  491. return;
  492. }
  493. String newDir = (String)directoryStack.pop();
  494. state.setCurrentDirectory(console,newDir);
  495. }
  496. } //}}}
  497. //{{{ pushd class
  498. static class pushd extends SystemShellBuiltIn
  499. {
  500. public int getMaxArguments()
  501. {
  502. return 0;
  503. }
  504. public void execute(Console console, Output output,
  505. Output error, Vector args, Hashtable values)
  506. {
  507. SystemShell.ConsoleState state = getConsoleState(console);
  508. Stack directoryStack = state.directoryStack;
  509. directoryStack.push(state.currentDirectory);
  510. String[] pp = { state.currentDirectory };
  511. error.print(null,jEdit.getProperty("console.shell.pushd.ok",pp));
  512. }
  513. } //}}}
  514. //{{{ pwd class
  515. static class pwd extends SystemShellBuiltIn
  516. {
  517. public int getMinArguments()
  518. {
  519. return 0;
  520. }
  521. public int getMaxArguments()
  522. {
  523. return 0;
  524. }
  525. public void execute(Console console, Output output,
  526. Output error, Vector args, Hashtable values)
  527. {
  528. // shell will print prompt with current working dir
  529. }
  530. } //}}}
  531. //{{{ run class
  532. static class run extends SystemShellBuiltIn
  533. {
  534. public int getMinArguments()
  535. {
  536. return 1;
  537. }
  538. public void execute(Console console, Output output,
  539. Output error, Vector args, Hashtable values)
  540. {
  541. String currentDirectory = getConsoleState(
  542. console).currentDirectory;
  543. for(int i = 0; i < args.size(); i++)
  544. {
  545. Macros.runScript(console.getView(),
  546. MiscUtilities.constructPath(
  547. currentDirectory,
  548. (String)args.get(i)),
  549. false);
  550. }
  551. }
  552. } //}}}
  553. //{{{ set class
  554. static class set extends SystemShellBuiltIn
  555. {
  556. public int getMinArguments()
  557. {
  558. return 2;
  559. }
  560. public int getMaxArguments()
  561. {
  562. return 2;
  563. }
  564. public void execute(Console console, Output output,
  565. Output error, Vector args, Hashtable values)
  566. {
  567. Hashtable variables = ConsolePlugin.getSystemShell().getVariables();
  568. variables.put(args.elementAt(0),args.elementAt(1));
  569. }
  570. } //}}}
  571. //{{{ unalias
  572. static class unalias extends SystemShellBuiltIn
  573. {
  574. public int getMinArguments()
  575. {
  576. return 1;
  577. }
  578. public int getMaxArguments()
  579. {
  580. return 1;
  581. }
  582. public void execute(Console console, Output output,
  583. Output error, Vector args, Hashtable values)
  584. {
  585. Hashtable aliases = ConsolePlugin.getSystemShell().getAliases();
  586. aliases.remove(args.elementAt(0));
  587. }
  588. } //}}}
  589. //{{{ unset class
  590. static class unset extends SystemShellBuiltIn
  591. {
  592. public int getMinArguments()
  593. {
  594. return 1;
  595. }
  596. public int getMaxArguments()
  597. {
  598. return 1;
  599. }
  600. public void execute(Console console, Output output,
  601. Output error, Vector args, Hashtable values)
  602. {
  603. Hashtable variables = ConsolePlugin.getSystemShell().getVariables();
  604. variables.remove(args.elementAt(0));
  605. }
  606. } //}}}
  607. //{{{ version class
  608. static class version extends SystemShellBuiltIn
  609. {
  610. public int getMinArguments()
  611. {
  612. return 0;
  613. }
  614. public int getMaxArguments()
  615. {
  616. return 0;
  617. }
  618. public void execute(Console console, Output output,
  619. Output error, Vector args, Hashtable values)
  620. {
  621. output.print(null,jEdit.getProperty(
  622. "plugin.console.ConsolePlugin.version"));
  623. }
  624. } //}}}
  625. //}}}
  626. }