Search in sources :

Example 11 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Posix method tail.

protected void tail(CommandSession session, Process process, String[] argv) throws Exception {
    String[] usage = { "tail -  displays last lines of file", "Usage: tail [-f] [-q] [-c # | -n #] [file ...]", "  -? --help                    Show help", "  -q --quiet                   Suppress headers when printing multiple sources", "  -f --follow                  Do not stop at end of file", "  -F --FOLLOW                  Follow and check for file renaming or rotation", "  -n --lines=LINES             Number of lines to print", "  -c --bytes=BYTES             Number of bytes to print" };
    Options opt = parseOptions(session, usage, argv);
    if (opt.isSet("lines") && opt.isSet("bytes")) {
        throw new IllegalArgumentException("usage: tail [-f] [-q] [-c # | -n #] [file ...]");
    }
    int lines;
    int bytes;
    if (opt.isSet("lines")) {
        lines = opt.getNumber("lines");
        bytes = Integer.MAX_VALUE;
    } else if (opt.isSet("bytes")) {
        lines = Integer.MAX_VALUE;
        bytes = opt.getNumber("bytes");
    } else {
        lines = 10;
        bytes = Integer.MAX_VALUE;
    }
    boolean follow = opt.isSet("follow") || opt.isSet("FOLLOW");
    AtomicReference<Object> lastPrinted = new AtomicReference<>();
    WatchService watchService = follow ? session.currentDir().getFileSystem().newWatchService() : null;
    Set<Path> watched = new HashSet<>();
    class Input implements Closeable {

        String name;

        Path path;

        Reader reader;

        StringBuilder buffer;

        long ino;

        long size;

        public Input(String name) {
            this.name = name;
            this.buffer = new StringBuilder();
        }

        public void open() {
            if (reader == null) {
                try {
                    InputStream is;
                    if ("-".equals(name)) {
                        is = new StdInSource(process).read();
                    } else {
                        path = session.currentDir().resolve(name);
                        is = Files.newInputStream(path);
                        if (opt.isSet("FOLLOW")) {
                            try {
                                ino = (Long) Files.getAttribute(path, "unix:ino");
                            } catch (Exception e) {
                            // Ignore
                            }
                        }
                        size = Files.size(path);
                    }
                    reader = new InputStreamReader(is);
                } catch (IOException e) {
                // Ignore
                }
            }
        }

        @Override
        public void close() throws IOException {
            if (reader != null) {
                try {
                    reader.close();
                } finally {
                    reader = null;
                }
            }
        }

        public boolean tail() throws IOException {
            open();
            if (reader != null) {
                if (buffer != null) {
                    char[] buf = new char[1024];
                    int nb;
                    while ((nb = reader.read(buf)) > 0) {
                        buffer.append(buf, 0, nb);
                        if (bytes > 0 && buffer.length() > bytes) {
                            buffer.delete(0, buffer.length() - bytes);
                        } else {
                            int l = 0;
                            int i = -1;
                            while ((i = buffer.indexOf("\n", i + 1)) >= 0) {
                                l++;
                            }
                            if (l > lines) {
                                i = -1;
                                l = l - lines;
                                while (--l >= 0) {
                                    i = buffer.indexOf("\n", i + 1);
                                }
                                buffer.delete(0, i + 1);
                            }
                        }
                    }
                    String toPrint = buffer.toString();
                    print(toPrint);
                    buffer = null;
                    if (follow && path != null) {
                        Path parent = path.getParent();
                        if (!watched.contains(parent)) {
                            parent.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
                            watched.add(parent);
                        }
                    }
                    return follow;
                } else if (follow && path != null) {
                    while (true) {
                        long newSize = Files.size(path);
                        if (size != newSize) {
                            char[] buf = new char[1024];
                            int nb;
                            while ((nb = reader.read(buf)) > 0) {
                                print(new String(buf, 0, nb));
                            }
                            size = newSize;
                        }
                        if (opt.isSet("FOLLOW")) {
                            long newIno = 0;
                            try {
                                newIno = (Long) Files.getAttribute(path, "unix:ino");
                            } catch (Exception e) {
                            // Ignore
                            }
                            if (ino != newIno) {
                                close();
                                open();
                                ino = newIno;
                                size = -1;
                                continue;
                            }
                        }
                        break;
                    }
                    return true;
                } else {
                    return false;
                }
            } else {
                Path parent = path.getParent();
                if (!watched.contains(parent)) {
                    parent.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
                    watched.add(parent);
                }
                return true;
            }
        }

        private void print(String toPrint) {
            if (lastPrinted.get() != this && opt.args().size() > 1 && !opt.isSet("quiet")) {
                process.out().println();
                process.out().println("==> " + name + " <==");
            }
            process.out().print(toPrint);
            lastPrinted.set(this);
        }
    }
    if (opt.args().isEmpty()) {
        opt.args().add("-");
    }
    List<Input> inputs = new ArrayList<>();
    for (String name : opt.args()) {
        Input input = new Input(name);
        inputs.add(input);
    }
    try {
        boolean cont = true;
        while (cont) {
            cont = false;
            for (Input input : inputs) {
                cont |= input.tail();
            }
            if (cont) {
                WatchKey key = watchService.take();
                key.pollEvents();
                key.reset();
            }
        }
    } catch (InterruptedException e) {
    // Ignore, this is the only way to quit
    } finally {
        for (Input input : inputs) {
            input.close();
        }
    }
}
Also used : Options(org.jline.builtins.Options) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) AttributedString(org.jline.utils.AttributedString) HashSet(java.util.HashSet) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) WatchKey(java.nio.file.WatchKey) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) IOException(java.io.IOException) WatchService(java.nio.file.WatchService)

Example 12 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Posix method less.

protected void less(CommandSession session, Process process, String[] argv) throws Exception {
    String[] usage = { "less -  file pager", "Usage: less [OPTIONS] [FILES]", "  -? --help                    Show help", "  -e --quit-at-eof             Exit on second EOF", "  -E --QUIT-AT-EOF             Exit on EOF", "  -F --quit-if-one-screen      Exit if entire file fits on first screen", "  -q --quiet --silent          Silent mode", "  -Q --QUIET --SILENT          Completely  silent", "  -S --chop-long-lines         Do not fold long lines", "  -i --ignore-case             Search ignores lowercase case", "  -I --IGNORE-CASE             Search ignores all case", "  -x --tabs                    Set tab stops", "  -N --LINE-NUMBERS            Display line number for each line", "     --no-init                 Disable terminal initialization", "     --no-keypad               Disable keypad handling" };
    boolean hasExtendedOptions = false;
    try {
        Less.class.getField("quitIfOneScreen");
        hasExtendedOptions = true;
    } catch (NoSuchFieldException e) {
        List<String> ustrs = new ArrayList<>(Arrays.asList(usage));
        ustrs.removeIf(s -> s.contains("--quit-if-one-screen") || s.contains("--no-init") || s.contains("--no-keypad"));
        usage = ustrs.toArray(new String[ustrs.size()]);
    }
    Options opt = parseOptions(session, usage, argv);
    List<Source> sources = new ArrayList<>();
    if (opt.args().isEmpty()) {
        opt.args().add("-");
    }
    for (String arg : opt.args()) {
        if ("-".equals(arg)) {
            sources.add(new StdInSource(process));
        } else {
            sources.add(new PathSource(session.currentDir().resolve(arg), arg));
        }
    }
    if (!process.isTty(1)) {
        for (Source source : sources) {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(source.read()))) {
                cat(process, reader, opt.isSet("LINE-NUMBERS"));
            }
        }
        return;
    }
    Less less = new Less(Shell.getTerminal(session));
    less.quitAtFirstEof = opt.isSet("QUIT-AT-EOF");
    less.quitAtSecondEof = opt.isSet("quit-at-eof");
    less.quiet = opt.isSet("quiet");
    less.veryQuiet = opt.isSet("QUIET");
    less.chopLongLines = opt.isSet("chop-long-lines");
    less.ignoreCaseAlways = opt.isSet("IGNORE-CASE");
    less.ignoreCaseCond = opt.isSet("ignore-case");
    if (opt.isSet("tabs")) {
        less.tabs = opt.getNumber("tabs");
    }
    less.printLineNumbers = opt.isSet("LINE-NUMBERS");
    if (hasExtendedOptions) {
        Less.class.getField("quitIfOneScreen").set(less, opt.isSet("quit-if-one-screen"));
        Less.class.getField("noInit").set(less, opt.isSet("no-init"));
        Less.class.getField("noKeypad").set(less, opt.isSet("no-keypad"));
    }
    less.run(sources);
}
Also used : Arrays(java.util.Arrays) PathSource(org.jline.builtins.Source.PathSource) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) IntConsumer(java.util.function.IntConsumer) FileTime(java.nio.file.attribute.FileTime) IntBinaryOperator(java.util.function.IntBinaryOperator) CommandSession(org.apache.felix.service.command.CommandSession) AttributedString(org.jline.utils.AttributedString) WatchKey(java.nio.file.WatchKey) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Context(org.apache.felix.gogo.jline.Shell.Context) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Options(org.jline.builtins.Options) TTop(org.jline.builtins.TTop) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Predicate(java.util.function.Predicate) Set(java.util.Set) Source(org.jline.builtins.Source) Reader(java.io.Reader) Instant(java.time.Instant) OSUtils(org.jline.utils.OSUtils) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Capability(org.jline.utils.InfoCmp.Capability) Commands(org.jline.builtins.Commands) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Attributes(org.jline.terminal.Attributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Process(org.apache.felix.service.command.Process) LinkOption(java.nio.file.LinkOption) FilterInputStream(java.io.FilterInputStream) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Nano(org.jline.builtins.Nano) Terminal(org.jline.terminal.Terminal) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) CommandProcessor(org.apache.felix.service.command.CommandProcessor) AttributedStyle(org.jline.utils.AttributedStyle) Files(java.nio.file.Files) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WatchService(java.nio.file.WatchService) TreeMap(java.util.TreeMap) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Closeable(java.io.Closeable) DateTimeFormatter(java.time.format.DateTimeFormatter) BufferedReader(java.io.BufferedReader) Comparator(java.util.Comparator) Less(org.jline.builtins.Less) Collections(java.util.Collections) InputStream(java.io.InputStream) Options(org.jline.builtins.Options) InputStreamReader(java.io.InputStreamReader) PathSource(org.jline.builtins.Source.PathSource) ArrayList(java.util.ArrayList) AttributedString(org.jline.utils.AttributedString) PathSource(org.jline.builtins.Source.PathSource) Source(org.jline.builtins.Source) BufferedReader(java.io.BufferedReader) List(java.util.List) ArrayList(java.util.ArrayList) Less(org.jline.builtins.Less)

Example 13 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Posix method parseOptions.

protected Options parseOptions(CommandSession session, String[] usage, Object[] argv) throws Exception {
    Options opt = Options.compile(usage, s -> get(session, s)).parse(argv, true);
    if (opt.isSet("help")) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        opt.usage(new PrintStream(baos));
        throw new HelpException(baos.toString());
    }
    return opt;
}
Also used : Arrays(java.util.Arrays) PathSource(org.jline.builtins.Source.PathSource) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) IntConsumer(java.util.function.IntConsumer) FileTime(java.nio.file.attribute.FileTime) IntBinaryOperator(java.util.function.IntBinaryOperator) CommandSession(org.apache.felix.service.command.CommandSession) AttributedString(org.jline.utils.AttributedString) WatchKey(java.nio.file.WatchKey) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Context(org.apache.felix.gogo.jline.Shell.Context) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Options(org.jline.builtins.Options) TTop(org.jline.builtins.TTop) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Predicate(java.util.function.Predicate) Set(java.util.Set) Source(org.jline.builtins.Source) Reader(java.io.Reader) Instant(java.time.Instant) OSUtils(org.jline.utils.OSUtils) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) Pattern(java.util.regex.Pattern) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Capability(org.jline.utils.InfoCmp.Capability) Commands(org.jline.builtins.Commands) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Attributes(org.jline.terminal.Attributes) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Process(org.apache.felix.service.command.Process) LinkOption(java.nio.file.LinkOption) FilterInputStream(java.io.FilterInputStream) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) PosixFilePermissions(java.nio.file.attribute.PosixFilePermissions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Nano(org.jline.builtins.Nano) Terminal(org.jline.terminal.Terminal) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) CommandProcessor(org.apache.felix.service.command.CommandProcessor) AttributedStyle(org.jline.utils.AttributedStyle) Files(java.nio.file.Files) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WatchService(java.nio.file.WatchService) TreeMap(java.util.TreeMap) AttributedStringBuilder(org.jline.utils.AttributedStringBuilder) Closeable(java.io.Closeable) DateTimeFormatter(java.time.format.DateTimeFormatter) BufferedReader(java.io.BufferedReader) Comparator(java.util.Comparator) Less(org.jline.builtins.Less) Collections(java.util.Collections) InputStream(java.io.InputStream) Options(org.jline.builtins.Options) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 14 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Procedural method doIf.

protected Object doIf(CommandSession session, Process process, Object[] argv) throws Exception {
    String[] usage = { "if -  if / then / else construct", "Usage: if {condition} {if-action} ... {else-action}", "  -? --help                    Show help" };
    Options opt = parseOptions(session, usage, argv);
    List<Function> functions = getFunctions(opt);
    if (functions == null || functions.size() < 2) {
        process.err().println("usage: if {condition} {if-action} ... {else-action}");
        process.error(2);
        return null;
    }
    for (int i = 0, length = functions.size(); i < length; ++i) {
        if (i == length - 1 || isTrue(session, ((Function) opt.argObjects().get(i++)))) {
            return ((Function) opt.argObjects().get(i)).execute(session, null);
        }
    }
    return null;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 15 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Procedural method doEach.

protected List<Object> doEach(CommandSession session, Process process, Object[] argv) throws Exception {
    String[] usage = { "each -  loop over the elements", "Usage: each [-r] elements { closure }", "         elements              an array to iterate on", "         closure               a closure to call", "  -? --help                    Show help", "  -r --result                  Return a list containing each iteration result" };
    Options opt = parseOptions(session, usage, argv);
    Collection<Object> elements = getElements(opt);
    List<Function> functions = getFunctions(opt);
    if (elements == null || functions == null || functions.size() != 1) {
        process.err().println("usage: each elements { closure }");
        process.err().println("       elements: an array to iterate on");
        process.err().println("       closure: a function or closure to call");
        process.error(2);
        return null;
    }
    List<Object> args = new ArrayList<>();
    List<Object> results = new ArrayList<>();
    args.add(null);
    for (Object x : elements) {
        checkInterrupt();
        args.set(0, x);
        try {
            results.add(functions.get(0).execute(session, args));
        } catch (BreakException b) {
            break;
        } catch (ContinueException c) {
            continue;
        }
    }
    return opt.isSet("result") ? results : null;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function) ArrayList(java.util.ArrayList)

Aggregations

Options (org.jline.builtins.Options)41 AttributedString (org.jline.utils.AttributedString)16 Function (org.apache.felix.service.command.Function)15 ArrayList (java.util.ArrayList)14 Process (org.apache.felix.service.command.Process)12 InputStreamReader (java.io.InputStreamReader)10 BufferedReader (java.io.BufferedReader)9 InputStream (java.io.InputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 FilterInputStream (java.io.FilterInputStream)8 PrintStream (java.io.PrintStream)8 Path (java.nio.file.Path)8 CommandSession (org.apache.felix.service.command.CommandSession)8 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Reader (java.io.Reader)6 TreeMap (java.util.TreeMap)6 Closeable (java.io.Closeable)5