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

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