📄 sessioncommandgroup.java
字号:
usage = help_enter; return 1; // // Echo // } else if ("echo".startsWith(args[0])) { int pos = 1; boolean nl = true; if (args.length >= 2 && "-n".equals(args[1])) { nl = false; pos = 2; } while (pos < args.length) { out.print(args[pos]); if (++pos < args.length) { out.print(" "); } } if (nl) { out.println(); } return 0; // // Leave // } else if ("leave".startsWith(args[0])) { if (args.length == 1) { si.currentGroup = ""; return 0; } usage = help_leave; // // Prompt // } else if ("prompt".startsWith(args[0])) { if (args.length == 2) { si.prompt = args[1]; return 0; } usage = help_prompt; // // Quit // } else if ("quit".startsWith(args[0])) { if (args.length == 1) { si.close(); return 0; } usage = help_quit; // // Save // } else if ("save".startsWith(args[0])) { File file = null; if (args.length == 1) { file = bc.getDataFile(SessionImpl.ALIAS_SAVE); } else if (args.length == 2) { file = new File(args[1]); } if (file != null) { try { OutputStream p = new FileOutputStream(file); si.aliases.save(p); } catch (IOException e) { out.println("Failed to save aliases: " + e); return 1; } return 0; } usage = help_save; // // Restore // } else if ("restore".startsWith(args[0])) { if (args.length == 1) { si.aliases.setDefault(); return 0; } else if (args.length == 2) { try { InputStream r = new FileInputStream(new File(args[1])); si.aliases.clear(); si.aliases.restore(r); } catch (IOException e) { out.println("Failed to restore aliases from " + args[1] + ": " + e); return 1; } return 0; } usage = help_restore; // // Source // } else if ("source".startsWith(args[0])) { if (args.length == 2) { InputStreamReader sin = null; try { URL surl = new URL(args[1]); sin = new InputStreamReader(surl.openStream()); SessionImpl ss = new SessionImpl(bc, "source: " + args[1], sin, out, null); ss.prompt = null; ss.start(); try { ss.join(); } catch (InterruptedException ignore) { } ss.close(); } catch (IOException e) { out.println("Failed to source URL: " + e.getMessage()); return 1; } finally { if (sin != null) { try { sin.close(); } catch (IOException ignore) { } } } return 0; } usage = help_source; // // Unalias // } else if ("unalias".startsWith(args[0])) { if (si == null) { out.println("Unalias not available from runCommand method"); return 1; } if (args.length == 2) { if (si.aliases.remove(args[1]) != null) { return 0; } return 1; } usage = help_unalias; } } if (usage != null) { usage = "Usage: " + usage; } else { usage = getLongHelp(); } out.println(usage); return -1; } /** * Help about available command groups. * * @param out * output device to print result */ int help(PrintWriter out, SessionImpl si) { ArrayList cg = new ArrayList(); cg.add(this); ServiceReference[] refs = null; try { refs = bc.getServiceReferences(Command.COMMAND_GROUP, null); } catch (InvalidSyntaxException ignore) { } if (refs != null) { for (int i = 0; i < refs.length; i++) { CommandGroup c = (CommandGroup) bc.getService(refs[i]); if (c != null) { String n = c.getGroupName(); int j; for (j = 0; j < cg.size(); j++) { if (n.compareTo(((CommandGroup) cg.get(j)) .getGroupName()) > 0) { break; } } cg.add(j, c); } } } out .println("Available command groups (type 'enter' to enter a group):"); for (Iterator e = cg.iterator(); e.hasNext();) { CommandGroup c = (CommandGroup) e.next(); out.println(c.getGroupName() + " - " + c.getShortHelp()); } if (refs != null) { for (int i = 0; i < refs.length; i++) { bc.ungetService(refs[i]); } } /* * out.println(""); out.println("Available aliases:"); for (Enumeration * e = si.aliases.keys(); e.hasMoreElements();) { String a = (String) * e.nextElement(); out.println(a + " = " + si.aliases.getString(a)); } */ return 0; } /** * Help about a command group. * * @param out * output device to print result */ int helpAbout(String group, PrintWriter out, SessionImpl si) { try { ServiceReference ref = Command.matchCommandGroup(bc, group); if (ref != null) { CommandGroup c = (CommandGroup) bc.getService(ref); if (c != null) { out.print(c.getLongHelp()); bc.ungetService(ref); return 0; } } else { // Null means session command group out.print(getLongHelp()); return 0; } } catch (IOException e) { out.println(e.getMessage()); return 1; } out.println("No such command group: " + group); return 1; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -