private MainApplicationContext InitializeSession(IEnumerable <string> args, CancellationToken token) { using (var memoryStreamManager = new MemoryStreamManager(InputText)) { var context = ServiceEngine.CreateContext(memoryStreamManager); token.Register(() => context.CancellationSignal = CaughtSignalEnum.INTERRUPTED); using (context.AcquireCurrentThread()) { GlobalScope.HandleDebugOptions(args); Logger.Current.Info(() => LedgerSessionStarting); GlobalScope = new GlobalScope(context.EnvironmentVariables); GlobalScope.Session.FlushOnNextDataFile = true; try { // Look for options and a command verb in the command-line arguments BindScope boundScope = new BindScope(GlobalScope, GlobalScope.Report); args = GlobalScope.ReadCommandArguments(boundScope, args); GlobalScope.Session.ReadJournalFiles(); Status = 0; } catch (CountError errors) { Status = errors.Count; } catch (Exception err) { GlobalScope.ReportError(err); } OutputText = memoryStreamManager.GetOutputText(); ErrorText = memoryStreamManager.GetErrorText(); return(context); } } }
public int Execute(string argString) { using (MainApplicationContext.AcquireCurrentThread()) { var envp = VirtualEnvironment.GetEnvironmentVariables(); var args = CommandLine.PreprocessSingleQuotes(argString); int status = 1; // The very first thing we do is handle some very special command-line // options, since they affect how the environment is setup: // // --verify ; turns on memory tracing // --verbose ; turns on logging // --debug CATEGORY ; turns on debug logging // --trace LEVEL ; turns on trace logging // --memory ; turns on memory usage tracing // --init-file ; directs ledger to use a different init file GlobalScope.HandleDebugOptions(args); // initialize_memory_tracing - [DM] #memory-tracing Logger.Current.Info(() => LedgerStarting); // ::textdomain("ledger"); - [DM] #localization GlobalScope globalScope = null; try { // Create the session object, which maintains nearly all state relating to // this invocation of Ledger; and register all known journal parsers. globalScope = new GlobalScope(envp); globalScope.Session.FlushOnNextDataFile = true; // Look for options and a command verb in the command-line arguments BindScope boundScope = new BindScope(globalScope, globalScope.Report); args = globalScope.ReadCommandArguments(boundScope, args); if (globalScope.ScriptHandler.Handled) { // Ledger is being invoked as a script command interpreter globalScope.Session.ReadJournalFiles(); status = 0; using (StreamReader sr = FileSystem.GetStreamReader(globalScope.ScriptHandler.Str())) { while (status == 0 && !sr.EndOfStream) { string line = sr.ReadLine().Trim(); if (!line.StartsWith("#")) { status = globalScope.ExecuteCommandWrapper(StringExtensions.SplitArguments(line), true); } } } } else if (args.Any()) { // User has invoke a verb at the interactive command-line status = globalScope.ExecuteCommandWrapper(args, false); } else { // Commence the REPL by displaying the current Ledger version VirtualConsole.Output.WriteLine(globalScope.ShowVersionInfo()); globalScope.Session.ReadJournalFiles(); bool exitLoop = false; VirtualConsole.ReadLineName = "Ledger"; string p; while ((p = VirtualConsole.ReadLine(globalScope.PromptString())) != null) { string expansion = null; int result = VirtualConsole.HistoryExpand(p.Trim(), ref expansion); if (result < 0 || result == 2) { throw new LogicError(String.Format(LogicError.ErrorMessageFailedToExpandHistoryReference, p)); } else if (expansion != null) { VirtualConsole.AddHistory(expansion); } CancellationManager.CheckForSignal(); if (!String.IsNullOrWhiteSpace(p) && p != "#") { if (String.Compare(p, "quit", true) == 0) { exitLoop = true; } else { globalScope.ExecuteCommandWrapper(StringExtensions.SplitArguments(p), true); } } if (exitLoop) { break; } } status = 0; // report success } } catch (CountError errors) { // used for a "quick" exit, and is used only if help text (such as // --help) was displayed status = errors.Count; } catch (Exception err) { if (globalScope != null) { globalScope.ReportError(err); } else { VirtualConsole.Error.WriteLine(String.Format(ExceptionDuringInitialization, err.Message)); } } if (globalScope != null) { globalScope.QuickClose(); globalScope.Dispose(); // {DM] It is the most appropriate place to call Dispose for the global scope. } Logger.Current.Info(() => LedgerEnded); // let global_scope leak! // Return the final status to the operating system, either 1 for error or 0 // for a successful completion. return(status); } }
public int Execute(string argString, IDictionary <string, string> envp, TextReader inReader, TextWriter outWriter, TextWriter errorWriter) { Logger.Current.OutWriter = errorWriter; FileSystem.SetConsoleInput(inReader); FileSystem.SetConsoleOutput(outWriter); FileSystem.SetConsoleError(errorWriter); var args = CommandLine.PreprocessSingleQuotes(argString); int status = 1; GlobalScope.HandleDebugOptions(args); // initialize_memory_tracing - not implemented Logger.Info(LedgerStarting); // ::textdomain("ledger"); - not implemented GlobalScope globalScope = null; try { // Create the session object, which maintains nearly all state relating to // this invocation of Ledger; and register all known journal parsers. globalScope = new GlobalScope(envp); globalScope.Session.FlushOnNextDataFile = true; // Look for options and a command verb in the command-line arguments BindScope boundScope = new BindScope(globalScope, globalScope.Report); args = globalScope.ReadCommandArguments(boundScope, args); if (globalScope.ScriptHandler.Handled) { // Ledger is being invoked as a script command interpreter globalScope.Session.ReadJournalFiles(); status = 0; using (StreamReader sr = FileSystem.GetStreamReader(globalScope.ScriptHandler.Str())) { while (status == 0 && !sr.EndOfStream) { string line = sr.ReadLine().Trim(); if (!line.StartsWith("#")) { status = globalScope.ExecuteCommandWrapper(StringExtensions.SplitArguments(line), true); } } } } else if (args.Any()) { // User has invoke a verb at the interactive command-line status = globalScope.ExecuteCommandWrapper(args, false); } else { // Commence the REPL by displaying the current Ledger version FileSystem.ConsoleOutput.WriteLine(globalScope.ShowVersionInfo()); globalScope.Session.ReadJournalFiles(); bool exitLoop = false; // rl_readline_name - TODO string p; while ((p = FileSystem.ReadLine(globalScope.PromptString())) != null) { string expansion = null; int result = HistoryExtensions.HistoryExpand(p.Trim(), ref expansion); if (result < 0 || result == 2) { throw new LogicError(String.Format(LogicError.ErrorMessageFailedToExpandHistoryReference, p)); } else if (expansion != null) { HistoryExtensions.AddHistory(expansion); } // CheckForSignal(); - TODO (most likely, not required) if (!String.IsNullOrWhiteSpace(p) && p != "#") { if (String.Compare(p, "quit", true) == 0) { exitLoop = true; } else { globalScope.ExecuteCommandWrapper(StringExtensions.SplitArguments(p), true); } } if (exitLoop) { break; } } status = 0; // report success } } catch (CountError errors) { // used for a "quick" exit, and is used only if help text (such as // --help) was displayed status = errors.Count; } catch (Exception err) { if (globalScope != null) { globalScope.ReportError(err); } else { FileSystem.ConsoleError.WriteLine(String.Format(ExceptionDuringInitialization, err.Message)); } } if (globalScope != null) { globalScope.QuickClose(); globalScope.Dispose(); // {DM] It is the most appropriate place to call Dispose for the global scope. } Logger.Info(LedgerEnded); // let global_scope leak! // Return the final status to the operating system, either 1 for error or 0 // for a successful completion. return(status); }