Пример #1
0
        static int Main(string[] args)
        {
            String        command                   = null;
            List <String> scriptFilename            = new List <String>();
            bool          showUsage                 = false;
            bool          nonDebugNonGuiExitOnError = false;
            bool          showIncludes              = false;
            int           errorCode                 = 0; // 0==success, the return value

            PrintVersion();

            ParseArguments(args,
                           out command, out scriptFilename, out showUsage,
                           out nonDebugNonGuiExitOnError, out showIncludes, out errorCode);

            if (showUsage)
            {
                PrintUsage();
                return(errorCode);
            }

            TextReader reader;
            bool       showPrompt;
            bool       readFromConsole;

            errorCode = DetermineAndOpenInputSource(command, scriptFilename,
                                                    out reader, out showPrompt, out readFromConsole);
            if (errorCode != 0)
            {
                return(errorCode);
            }

            GrShell               shell     = new GrShell(reader);
            GrShellImpl           shellImpl = new GrShellImpl();
            IGrShellImplForDriver impl      = shellImpl;
            GrShellDriver         driver    = new GrShellDriver(shell, impl);

            shell.SetImpl(shellImpl);
            shell.SetDriver(driver);
            driver.tokenSources.Push(shell.token_source);
            driver.showIncludes            = showIncludes;
            impl.nonDebugNonGuiExitOnError = nonDebugNonGuiExitOnError;

            try
            {
                driver.conditionalEvaluationResults.Push(true);

                while (!driver.Quitting && !driver.Eof)
                {
                    driver.ShowPromptAsNeeded(showPrompt);

                    bool success = shell.ParseShellCommand();

                    errorCode = HandleEofOrErrorIfNonConsoleShell(scriptFilename, success, nonDebugNonGuiExitOnError,
                                                                  shell, driver,
                                                                  ref reader, ref showPrompt, ref readFromConsole);
                    if (errorCode != 0)
                    {
                        return(errorCode);
                    }
                }

                driver.conditionalEvaluationResults.Pop();
            }
            catch (Exception e)
            {
                Console.WriteLine("exit due to " + e.Message);
                errorCode = -2;
            }
            finally
            {
                impl.Cleanup();
            }

            return(errorCode);
        }
Пример #2
0
        public bool Include(String filename, String from, String to)
        {
            try
            {
                if (showIncludes)
                {
                    impl.debugOut.WriteLine("Including " + filename);
                }

                TextReader reader = null;
                if (filename.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase))
                {
                    FileStream filereader = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    reader = new StreamReader(new GZipStream(filereader, CompressionMode.Decompress));
                }
                else
                {
                    reader = new StreamReader(filename);
                }
                if (from != null || to != null)
                {
                    reader = new FromToReader(reader, from, to);
                }

                using (reader)
                {
                    SimpleCharStream    charStream  = new SimpleCharStream(reader);
                    GrShellTokenManager tokenSource = new GrShellTokenManager(charStream);
                    tokenSources.Push(tokenSource);

                    try
                    {
                        grShell.ReInit(tokenSource);

                        while (!Quitting && !Eof)
                        {
                            if (!grShell.ParseShellCommand())
                            {
                                impl.errOut.WriteLine("Shell command parsing failed in include of \"" + filename + "\" (at nesting level " + tokenSources.Count + ")");
                                return(false);
                            }
                        }
                        Eof = false;
                    }
                    finally
                    {
                        if (showIncludes)
                        {
                            impl.debugOut.WriteLine("Leaving " + filename);
                        }

                        tokenSources.Pop();
                        grShell.ReInit(tokenSources.Peek());
                    }
                }
            }
            catch (Exception e)
            {
                impl.errOut.WriteLine("Error during include of \"" + filename + "\": " + e.Message);
                return(false);
            }

            return(true);
        }