Search in sources :

Example 1 with SignalHandler

use of com.laytonsmith.PureUtilities.SignalHandler in project CommandHelper by EngineHub.

the class Interpreter method doStartup.

private void doStartup() throws IOException, DataSourceException, URISyntaxException, Profiles.InvalidProfileException {
    Installer.Install(MethodScriptFileLocations.getDefault().getConfigDirectory());
    Installer.InstallCmdlineInterpreter();
    env = Static.GenerateStandaloneEnvironment(false);
    env.getEnv(GlobalEnv.class).SetCustom("cmdline", true);
    if (Prefs.UseColors()) {
        TermColors.EnableColors();
    } else {
        TermColors.DisableColors();
    }
    String auto_include = FileUtil.read(MethodScriptFileLocations.getDefault().getCmdlineInterpreterAutoIncludeFile());
    try {
        MethodScriptCompiler.execute(auto_include, MethodScriptFileLocations.getDefault().getCmdlineInterpreterAutoIncludeFile(), true, env, null, null, null);
    } catch (ConfigCompileException ex) {
        ConfigRuntimeException.HandleUncaughtException(ex, "Interpreter will continue to run, however.", null);
    } catch (ConfigCompileGroupException ex) {
        ConfigRuntimeException.HandleUncaughtException(ex, null);
    }
    // Install our signal handlers.
    SignalHandler.SignalCallback signalHandler = new SignalHandler.SignalCallback() {

        @Override
        public boolean handle(SignalType type) {
            if (isExecuting) {
                env.getEnv(GlobalEnv.class).SetInterrupt(true);
                if (scriptThread != null) {
                    scriptThread.interrupt();
                }
                for (Thread t : env.getEnv(GlobalEnv.class).GetDaemonManager().getActiveThreads()) {
                    t.interrupt();
                }
            } else {
                ctrlCcount++;
                if (ctrlCcount > MAX_CTRL_C_MASHES) {
                    // Ok, ok, we get the hint.
                    StreamUtils.GetSystemOut().println();
                    StreamUtils.GetSystemOut().flush();
                    // Standard Ctrl+C exit code
                    System.exit(130);
                }
                pl(YELLOW + "\nUse exit() to exit the shell." + reset());
                p(getPrompt());
            }
            return true;
        }
    };
    try {
        SignalHandler.addHandler(Signals.SIGTERM, signalHandler);
    } catch (IllegalArgumentException ex) {
    // Oh well.
    }
    try {
        SignalHandler.addHandler(Signals.SIGINT, signalHandler);
    } catch (IllegalArgumentException ex) {
    // Oh well again.
    }
}
Also used : SignalType(com.laytonsmith.PureUtilities.SignalType) SignalHandler(com.laytonsmith.PureUtilities.SignalHandler) GlobalEnv(com.laytonsmith.core.environments.GlobalEnv) CString(com.laytonsmith.core.constructs.CString) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException)

Aggregations

SignalHandler (com.laytonsmith.PureUtilities.SignalHandler)1 SignalType (com.laytonsmith.PureUtilities.SignalType)1 CString (com.laytonsmith.core.constructs.CString)1 GlobalEnv (com.laytonsmith.core.environments.GlobalEnv)1 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)1 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)1