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

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