Search in sources :

Example 1 with CREIOException

use of com.laytonsmith.core.exceptions.CRE.CREIOException in project CommandHelper by EngineHub.

the class IncludeCache method get.

public static ParseTree get(File file, Target t) {
    CHLog.GetLogger().Log(TAG, LogLevel.DEBUG, "Loading " + file, t);
    if (cache.containsKey(file)) {
        CHLog.GetLogger().Log(TAG, LogLevel.INFO, "Returning " + file + " from cache", t);
        return cache.get(file);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Cache does not already contain file. Compiling and caching.", t);
    // We have to pull the file from the FS, and compile it.
    if (!Security.CheckSecurity(file)) {
        throw new CRESecurityException("The script cannot access " + file + " due to restrictions imposed by the base-dir setting.", t);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Security check passed", t);
    try {
        String s = new ZipReader(file).getFileContents();
        ParseTree tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(s, file, true));
        CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Compilation succeeded, adding to cache.", t);
        IncludeCache.add(file, tree);
        return tree;
    } catch (ConfigCompileException ex) {
        throw new CREIncludeException("There was a compile error when trying to include the script at " + file + "\n" + ex.getMessage() + " :: " + file.getName() + ":" + ex.getLineNum(), t);
    } catch (ConfigCompileGroupException ex) {
        StringBuilder b = new StringBuilder();
        b.append("There were compile errors when trying to include the script at ").append(file).append("\n");
        for (ConfigCompileException e : ex.getList()) {
            b.append(e.getMessage()).append(" :: ").append(e.getFile().getName()).append(":").append(e.getLineNum());
        }
        throw new CREIncludeException(b.toString(), t);
    } catch (IOException ex) {
        throw new CREIOException("The script at " + file + " could not be found or read in.", t);
    }
}
Also used : CREIncludeException(com.laytonsmith.core.exceptions.CRE.CREIncludeException) ZipReader(com.laytonsmith.PureUtilities.ZipReader) IOException(java.io.IOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree) CRESecurityException(com.laytonsmith.core.exceptions.CRE.CRESecurityException)

Example 2 with CREIOException

use of com.laytonsmith.core.exceptions.CRE.CREIOException in project CommandHelper by EngineHub.

the class Interpreter method doBuiltin.

public boolean doBuiltin(String script) {
    List<String> args = StringUtils.ArgParser(script);
    if (args.size() > 0) {
        String command = args.get(0);
        args.remove(0);
        command = command.toLowerCase(Locale.ENGLISH);
        switch(command) {
            case "help":
                pl(getHelpMsg());
                pl("Shell builtins:");
                pl("cd <dir> - Runs cd() with the provided argument.");
                pl("s - equivalent to cd('..').");
                pl("echo - Prints the arguments. If -e is set as the first argument, arguments are sent to colorize() first.");
                pl("exit - Exits shellMode, and returns back to normal mscript mode.");
                pl("logout - Exits the shell entirely with a return code of 0.");
                pl("pwd - Runs pwd()");
                pl("help - Prints this message.");
                return true;
            case "cd":
            case "s":
                if ("s".equals(command)) {
                    args.add("..");
                }
                if (args.size() > 1) {
                    pl(RED + "Too many arguments passed to cd");
                    return true;
                }
                Construct[] a = new Construct[0];
                if (args.size() == 1) {
                    a = new Construct[] { new CString(args.get(0), Target.UNKNOWN) };
                }
                try {
                    new Cmdline.cd().exec(Target.UNKNOWN, env, a);
                } catch (CREIOException ex) {
                    pl(RED + ex.getMessage());
                }
                return true;
            case "pwd":
                pl(new Cmdline.pwd().exec(Target.UNKNOWN, env).val());
                return true;
            case "exit":
                // We need previous code to intercept, we cannot do this here.
                throw new Error("I should not run");
            case "logout":
                new Cmdline.exit().exec(Target.UNKNOWN, env, new CInt(0, Target.UNKNOWN));
                // won't actually run
                return true;
            case "echo":
                // TODO Probably need some variable interpolation maybe? Otherwise, I don't think this command
                // is actually useful as is, because this is not supposed to be a scripting environment.. that's
                // what the normal shell is for.
                boolean colorize = false;
                if (args.size() > 0 && "-e".equals(args.get(0))) {
                    colorize = true;
                    args.remove(0);
                }
                String output = StringUtils.Join(args, " ");
                if (colorize) {
                    output = new Echoes.colorize().exec(Target.UNKNOWN, env, new CString(output, Target.UNKNOWN)).val();
                }
                pl(output);
                return true;
        }
    }
    return false;
}
Also used : CString(com.laytonsmith.core.constructs.CString) CString(com.laytonsmith.core.constructs.CString) Echoes(com.laytonsmith.core.functions.Echoes) CInt(com.laytonsmith.core.constructs.CInt) Construct(com.laytonsmith.core.constructs.Construct) Cmdline(com.laytonsmith.core.functions.Cmdline) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException)

Aggregations

CREIOException (com.laytonsmith.core.exceptions.CRE.CREIOException)2 ZipReader (com.laytonsmith.PureUtilities.ZipReader)1 ParseTree (com.laytonsmith.core.ParseTree)1 CInt (com.laytonsmith.core.constructs.CInt)1 CString (com.laytonsmith.core.constructs.CString)1 Construct (com.laytonsmith.core.constructs.Construct)1 CREIncludeException (com.laytonsmith.core.exceptions.CRE.CREIncludeException)1 CRESecurityException (com.laytonsmith.core.exceptions.CRE.CRESecurityException)1 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)1 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)1 Cmdline (com.laytonsmith.core.functions.Cmdline)1 Echoes (com.laytonsmith.core.functions.Echoes)1 IOException (java.io.IOException)1