src/jdk.scripting.nashorn.shell/share/classes/jdk/nashorn/tools/jjs/Console.java
author ihse
Sat, 03 Mar 2018 08:21:47 +0100
branchihse-warnings-cflags-branch
changeset 56230 489867818774
parent 47492 560fab171dc7
child 50338 1d5694c1aa03
permissions -rw-r--r--
No longer disable E_OLD_STYLE_FUNC_DEF.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     1
/*
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     4
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    10
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    15
 * accompanied this code).
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    16
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    20
 *
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    23
 * questions.
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    24
 */
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    25
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    26
package jdk.nashorn.tools.jjs;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    27
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    28
import java.io.File;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    29
import java.io.IOException;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    30
import java.io.InputStream;
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    31
import java.io.PrintStream;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    32
import java.io.Writer;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    33
import java.nio.file.Files;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    34
import java.util.function.Function;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    35
import java.util.stream.Collectors;
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    36
import jdk.internal.jline.NoInterruptUnixTerminal;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    37
import jdk.internal.jline.Terminal;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    38
import jdk.internal.jline.TerminalFactory;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    39
import jdk.internal.jline.TerminalFactory.Flavor;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
    40
import jdk.internal.jline.WindowsTerminal;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    41
import jdk.internal.jline.console.ConsoleReader;
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    42
import jdk.internal.jline.console.KeyMap;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    43
import jdk.internal.jline.extra.EditingHistory;
41616
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    44
import jdk.internal.misc.Signal;
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    45
import jdk.internal.misc.Signal.Handler;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    46
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    47
class Console implements AutoCloseable {
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
    48
    private static final String DOCUMENTATION_SHORTCUT = "\033\133\132"; //Shift-TAB
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    49
    private final ConsoleReader in;
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    50
    private final File historyFile;
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    51
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    52
    Console(final InputStream cmdin, final PrintStream cmdout, final File historyFile,
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    53
            final NashornCompleter completer, final Function<String, String> docHelper) throws IOException {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    54
        this.historyFile = historyFile;
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    55
32525
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
    56
        TerminalFactory.registerFlavor(Flavor.WINDOWS, isCygwin()? JJSUnixTerminal::new : JJSWindowsTerminal::new);
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
    57
        TerminalFactory.registerFlavor(Flavor.UNIX, JJSUnixTerminal::new);
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    58
        in = new ConsoleReader(cmdin, cmdout);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    59
        in.setExpandEvents(false);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    60
        in.setHandleUserInterrupt(true);
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    61
        in.setBellEnabled(true);
38494
52305229ea03 8131017: jshell tool: pasting code with tabs invokes tab completion
jlahoda
parents: 38488
diff changeset
    62
        in.setCopyPasteDetection(true);
38898
43383182cae3 8159031: jjs throws NoSuchFileException if ~/.jjs.history does not exist
hannesw
parents: 38494
diff changeset
    63
        final Iterable<String> existingHistory = historyFile.exists() ? Files.readAllLines(historyFile.toPath()) : null;
43383182cae3 8159031: jjs throws NoSuchFileException if ~/.jjs.history does not exist
hannesw
parents: 38494
diff changeset
    64
        in.setHistory(new EditingHistory(in, existingHistory) {
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    65
            @Override protected boolean isComplete(CharSequence input) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    66
                return completer.isComplete(input.toString());
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    67
            }
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    68
        });
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    69
        in.addCompleter(completer);
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    70
        Runtime.getRuntime().addShutdownHook(new Thread((Runnable)this::saveHistory));
47492
560fab171dc7 8190698: jjs tool of jdk.scripting.nashorn.shell module should not statically depend on java.desktop
sundar
parents: 47216
diff changeset
    71
        bind(DOCUMENTATION_SHORTCUT, (Runnable) ()->showDocumentation(docHelper));
41616
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    72
        try {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    73
            Signal.handle(new Signal("CONT"), new Handler() {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    74
                @Override public void handle(Signal sig) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    75
                    try {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    76
                        in.getTerminal().reset();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    77
                        in.redrawLine();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    78
                        in.flush();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    79
                    } catch (Exception ex) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    80
                        ex.printStackTrace();
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    81
                    }
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    82
                }
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    83
            });
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    84
        } catch (IllegalArgumentException ignored) {
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    85
            //the CONT signal does not exist on this platform
89347182c199 8166183: jshell tool: on return from Ctrl-Z, garbage on screen, dies with Ctrl-C
jlahoda
parents: 38898
diff changeset
    86
        }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    87
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    88
32240
d7c7a5dc92d8 8133652: Implement tab-completion for member select expressions
sundar
parents: 32152
diff changeset
    89
    String readLine(final String prompt) throws IOException {
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    90
        return in.readLine(prompt);
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    91
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    92
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    93
    @Override
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    94
    public void close() {
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    95
        saveHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    96
    }
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
    97
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
    98
    private void saveHistory() {
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
    99
        try (Writer out = Files.newBufferedWriter(historyFile.toPath())) {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   100
            String lineSeparator = System.getProperty("line.separator");
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   101
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   102
            out.write(getHistory().save()
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   103
                                  .stream()
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   104
                                  .collect(Collectors.joining(lineSeparator)));
32242
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   105
        } catch (final IOException exp) {}
bbc1ebbb5cdc 8133777: Use file based persistence for history instead of preferences
sundar
parents: 32240
diff changeset
   106
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   107
38488
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   108
    EditingHistory getHistory() {
85c83cc2b4af 8133549: Generalize jshell's EditingHistory
jlahoda
parents: 34735
diff changeset
   109
        return (EditingHistory) in.getHistory();
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   110
    }
32313
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   111
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   112
    boolean terminalEditorRunning() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   113
        Terminal terminal = in.getTerminal();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   114
        if (terminal instanceof JJSUnixTerminal) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   115
            return ((JJSUnixTerminal) terminal).isRaw();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   116
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   117
        return false;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   118
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   119
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   120
    void suspend() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   121
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   122
            in.getTerminal().restore();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   123
        } catch (Exception ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   124
            throw new IllegalStateException(ex);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   125
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   126
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   127
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   128
    void resume() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   129
        try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   130
            in.getTerminal().init();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   131
        } catch (Exception ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   132
            throw new IllegalStateException(ex);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   133
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   134
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   135
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   136
    static final class JJSUnixTerminal extends NoInterruptUnixTerminal {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   137
        JJSUnixTerminal() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   138
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   139
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   140
        boolean isRaw() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   141
            try {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   142
                return getSettings().get("-a").contains("-icanon");
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   143
            } catch (IOException | InterruptedException ex) {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   144
                return false;
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   145
            }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   146
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   147
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   148
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   149
        public void disableInterruptCharacter() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   150
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   151
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   152
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   153
        public void enableInterruptCharacter() {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   154
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   155
    }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   156
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   157
    static final class JJSWindowsTerminal extends WindowsTerminal {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   158
        public JJSWindowsTerminal() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   159
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   160
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   161
        @Override
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   162
        public void init() throws Exception {
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   163
            super.init();
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   164
            setAnsiSupported(false);
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   165
        }
11b284f8c4c6 8133948: Add 'edit' function to allow external editing of scripts
sundar
parents: 32242
diff changeset
   166
    }
32525
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   167
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   168
    private static boolean isCygwin() {
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   169
        return System.getenv("SHELL") != null;
3244ec62a5cb 8135151: jjs should work in cygwin environment
sundar
parents: 32313
diff changeset
   170
    }
34735
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   171
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   172
    private void bind(String shortcut, Object action) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   173
        KeyMap km = in.getKeys();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   174
        for (int i = 0; i < shortcut.length(); i++) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   175
            final Object value = km.getBound(Character.toString(shortcut.charAt(i)));
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   176
            if (value instanceof KeyMap) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   177
                km = (KeyMap) value;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   178
            } else {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   179
                km.bind(shortcut.substring(i), action);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   180
            }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   181
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   182
    }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   183
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   184
    private void showDocumentation(final Function<String, String> docHelper) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   185
        final String buffer = in.getCursorBuffer().buffer.toString();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   186
        final int cursor = in.getCursorBuffer().cursor;
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   187
        final String doc = docHelper.apply(buffer.substring(0, cursor));
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   188
        try {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   189
            if (doc != null) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   190
                in.println();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   191
                in.println(doc);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   192
                in.redrawLine();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   193
                in.flush();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   194
            } else {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   195
                in.beep();
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   196
            }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   197
        } catch (IOException ex) {
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   198
            throw new IllegalStateException(ex);
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   199
        }
0f41d334aa24 8145486: jjs should support documentation key shortcut in interactive mode
sundar
parents: 32525
diff changeset
   200
    }
32152
705d4af0d4d7 8133347: Add makefiles support and basic session, persistence history navigation with jline
sundar
parents:
diff changeset
   201
}