xronos /Microsoft.Scripting/Hosting/Shell/CommandLine.cs

Language C# Lines 454
MD5 Hash f13ed03d1cef40b3b8e2a362aaff82b8 Estimated Cost $7,498 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using Microsoft.Scripting.Hosting.Providers;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Hosting.Shell {
    /// <summary>
    /// Command line hosting service.
    /// </summary>
    public class CommandLine {
        private LanguageContext _language;
        private IConsole _console;
        private ConsoleOptions _options;
        private ScriptScope _scope;
        private ScriptEngine _engine;
        private ICommandDispatcher _commandDispatcher;
        private int? _terminatingExitCode;

        protected IConsole Console { get { return _console; } }
        protected ConsoleOptions Options { get { return _options; } }
        protected ScriptEngine Engine { get { return _engine; } }
        public ScriptScope ScriptScope { get { return _scope; } protected set { _scope = value; } }

        /// <summary>
        /// Scope is not remotable, and this only works in the same AppDomain.
        /// </summary>
        protected Scope Scope { 
            get {
                if (_scope == null) {
                    return null;
                }
                return _scope.Scope; 
            }
            set {
                _scope = new ScriptScope(_engine, value);
            }
        }
        
        protected LanguageContext Language {
            get {
                // LanguageContext is not remotable, and this only works in the same AppDomain.
                if (_language == null) {
                    _language = HostingHelpers.GetLanguageContext(_engine);
                }
                return _language;
            }
        }

        protected virtual string Prompt { get { return ">>> "; } }
        public virtual string PromptContinuation { get { return "... "; } }
        protected virtual string Logo { get { return null; } }

        public CommandLine() {
        }

        protected virtual void Initialize() {
            if (_commandDispatcher == null) {
                _commandDispatcher = CreateCommandDispatcher();
            }
        }

        protected virtual Scope CreateScope() {
            return new Scope();
        }

        protected virtual ICommandDispatcher CreateCommandDispatcher() {
            return new SimpleCommandDispatcher();
        }

        public virtual void Terminate(int exitCode) {
            // The default implementation just sets a flag. Derived types can support better termination
            _terminatingExitCode = exitCode;
        }

        /// <summary>
        /// Executes the comand line - depending upon the options provided we will
        /// either run a single file, a single command, or enter the interactive loop.
        /// </summary>
        public int Run(ScriptEngine engine, IConsole console, ConsoleOptions options) {
            ContractUtils.RequiresNotNull(engine, "engine");
            ContractUtils.RequiresNotNull(console, "console");
            ContractUtils.RequiresNotNull(options, "options");

            _engine = engine;
            _options = options;
            _console = console;

            Initialize();

            try {
                return Run();

#if !SILVERLIGHT // ThreadAbortException.ExceptionState
            } catch (System.Threading.ThreadAbortException tae) {
                if (tae.ExceptionState is KeyboardInterruptException) {
                    Thread.ResetAbort();
                    return -1;
                } else {
                    throw;
                }
#endif
            } finally {
                Shutdown();
                _engine = null;
                _options = null;
                _console = null;
            }
        }

        /// <summary>
        /// Runs the command line.  Languages can override this to provide custom behavior other than:
        ///     1. Running a single command
        ///     2. Running a file
        ///     3. Entering the interactive console loop.
        /// </summary>
        /// <returns></returns>
        protected virtual int Run() {
            int result;

            if (_options.Command != null) {
                result = RunCommand(_options.Command);
            } else if (_options.FileName != null) {
                result = RunFile(_options.FileName);
            } else {
                return RunInteractive();
            }

            if (_options.Introspection) {
                return RunInteractiveLoop();
            }

            return result;
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        protected virtual void Shutdown() {
            try {
                _engine.Runtime.Shutdown();
            } catch (Exception e) {
                UnhandledException(e);
            }
        }

        protected virtual int RunFile(string fileName) {
            return RunFile(_engine.CreateScriptSourceFromFile(fileName));
        }

        protected virtual int RunCommand(string command) {
            return RunFile(_engine.CreateScriptSourceFromString(command, SourceCodeKind.Statements));
        }

        /// <summary>
        /// Runs the specified filename
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        protected virtual int RunFile(ScriptSource source) {
            int result = 1;

            if (Options.HandleExceptions) {
                try {
                    result = source.ExecuteProgram();
                } catch (Exception e) {
                    UnhandledException(e);
                }
            } else {
                result = source.ExecuteProgram();
            }

            return result;
        }

        protected void PrintLogo() {
            if (Logo != null) {
                _console.Write(Logo, Style.Out);
            }
        }

        #region Interactivity

        /// <summary>
        /// Starts the interactive loop.  Performs any initialization necessary before
        /// starting the loop and then calls RunInteractiveLoop to start the loop.
        /// 
        /// Returns the exit code when the interactive loop is completed.
        /// </summary>
        protected virtual int RunInteractive() {
            PrintLogo();
            return RunInteractiveLoop();
        }

        /// <summary>
        /// Runs the interactive loop.  Repeatedly parse and run interactive actions
        /// until an exit code is received.  If any exceptions are unhandled displays
        /// them to the console
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        protected int RunInteractiveLoop() {
            if (_scope == null) {
                _scope = _engine.CreateScope();
            }

#if !SILVERLIGHT // Remote console
            string remoteRuntimeChannel = _options.RemoteRuntimeChannel;
            if (remoteRuntimeChannel != null) {
                // Publish the ScriptScope so that the host can use it
                Remote.RemoteRuntimeServer.StartServer(remoteRuntimeChannel, _scope);
                return 0;
            }
#endif
            int? res = null;

            do {
                if (Options.HandleExceptions) {
                    try {
                        res = TryInteractiveAction();
#if SILVERLIGHT 
                    } catch (ExitProcessException e) {
                        res = e.ExitCode;
#endif
                    }  catch (Exception e) {
                        if (CommandLine.IsFatalException(e)) {
                            // Some exceptions are too dangerous to try to catch
                            throw;
                        }

                        // There should be no unhandled exceptions in the interactive session
                        // We catch all (most) exceptions here, and just display it,
                        // and keep on going
                        UnhandledException(e);
                    }
                } else {
                    res = TryInteractiveAction();
                }

            } while (res == null);

            return res.Value;
        }

        internal static bool IsFatalException(Exception e) {
            ThreadAbortException tae = e as ThreadAbortException;
            if (tae != null) {
#if SILVERLIGHT // ThreadAbortException.ExceptionState
                return true;
#else
                if ((tae.ExceptionState as KeyboardInterruptException) == null) {
                    return true;
                }
#endif
            }
            return false;
        }

        protected virtual void UnhandledException(Exception e) {
            ExceptionOperations exceptionOperations = _engine.GetService<ExceptionOperations>();
            _console.WriteLine(exceptionOperations.FormatException(e), Style.Error);
        }

        /// <summary>
        /// Attempts to run a single interaction and handle any language-specific
        /// exceptions.  Base classes can override this and call the base implementation
        /// surrounded with their own exception handling.
        /// 
        /// Returns null if successful and execution should continue, or an exit code.
        /// </summary>
        protected virtual int? TryInteractiveAction() {
            int? result = null;

            try {
                result = RunOneInteraction();
#if SILVERLIGHT // ThreadAbortException.ExceptionState
            } catch (ThreadAbortException) {
#else
            } catch (ThreadAbortException tae) {
                KeyboardInterruptException pki = tae.ExceptionState as KeyboardInterruptException;
                if (pki != null) {
                    UnhandledException(tae);
                    Thread.ResetAbort();
                } else {
                    throw;
                }
#endif
            }

            return result;
        }

        /// <summary>
        /// Parses a single interactive command or a set of statements and executes it.  
        /// 
        /// Returns null if successful and execution should continue, or the appropiate exit code.
        /// 
        /// We check if the code read is an interactive command or statements is by checking for NewLine
        /// If the code contains NewLine, it's a set of statements (most probably from SendToConsole)
        /// If the code does not contain a NewLine, it's an interactive command typed by the user at the prompt
        /// </summary>
        private int? RunOneInteraction() {
            bool continueInteraction;
            string s = ReadStatement(out continueInteraction);

            if (continueInteraction == false) {
                return (_terminatingExitCode == null) ? 0 : _terminatingExitCode;
            }

            if (String.IsNullOrEmpty(s)) {
                // Is it an empty line?
                _console.Write(String.Empty, Style.Out);
                return null;
            }

            ExecuteCommand(_engine.CreateScriptSourceFromString(s, SourceCodeKind.InteractiveCode));
            return null;
        }

        protected object ExecuteCommand(ScriptSource source) {
            ErrorListener errorListener = new ErrorSinkProxyListener(ErrorSink);
            CompiledCode compiledCode = source.Compile(_engine.GetCompilerOptions(_scope), errorListener);
            return _commandDispatcher.Execute(compiledCode, _scope);
        }

        protected virtual ErrorSink ErrorSink {
            get { return ErrorSink.Default; }
        }

        /// <summary>
        /// Private helper function to see if we should treat the current input as a blank link.
        /// 
        /// We do this if we only have auto-indent text.
        /// </summary>
        private static bool TreatAsBlankLine(string line, int autoIndentSize) {
            if (line.Length == 0) return true;
            if (autoIndentSize != 0 && line.Trim().Length == 0 && line.Length == autoIndentSize) {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Read a statement, which can potentially be a multiple-line statement suite (like a class declaration).
        /// </summary>
        /// <param name="continueInteraction">Should the console session continue, or did the user indicate 
        /// that it should be terminated?</param>
        /// <returns>Expression to evaluate. null for empty input</returns>
        protected string ReadStatement(out bool continueInteraction) {
            StringBuilder b = new StringBuilder();
            int autoIndentSize = 0;

            _console.Write(Prompt, Style.Prompt);

            while (true) {
                string line = ReadLine(autoIndentSize);
                continueInteraction = true;

                if (line == null || (_terminatingExitCode != null)) {
                    continueInteraction = false;
                    return null;
                }

                bool allowIncompleteStatement = TreatAsBlankLine(line, autoIndentSize);
                b.Append(line);
                // Note that this does not use Environment.NewLine because some languages (eg. Python) only
                // recognize \n as a line terminator.
                b.Append("\n");

                string code = b.ToString();

                ScriptSource command = _engine.CreateScriptSourceFromString(code, SourceCodeKind.InteractiveCode);
                ScriptCodeParseResult props = command.GetCodeProperties(_engine.GetCompilerOptions(_scope));

                if (SourceCodePropertiesUtils.IsCompleteOrInvalid(props, allowIncompleteStatement)) {
                    return props != ScriptCodeParseResult.Empty ? code : null;
                }

                if (_options.AutoIndent && _options.AutoIndentSize != 0) {
                    autoIndentSize = GetNextAutoIndentSize(code);
                }

                // Keep on reading input
                _console.Write(PromptContinuation, Style.Prompt);
            }
        }

        /// <summary>
        /// Gets the next level for auto-indentation
        /// </summary>
        protected virtual int GetNextAutoIndentSize(string text) {
            return 0;
        }

        protected virtual string ReadLine(int autoIndentSize) {
            return _console.ReadLine(autoIndentSize);
        }

        internal protected virtual TextWriter GetOutputWriter(bool isErrorOutput) {
            return isErrorOutput ? System.Console.Error : System.Console.Out;
        }

        //private static DynamicSite<object, IList<string>>  _memberCompletionSite =
        //    new DynamicSite<object, IList<string>>(OldDoOperationAction.Make(Operators.GetMemberNames));

        public IList<string> GetMemberNames(string code) {
            object value = _engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression).Execute(_scope);
            return _engine.Operations.GetMemberNames(value);
            // TODO: why doesn't this work ???
            //return _memberCompletionSite.Invoke(new CodeContext(_scope, _engine), value);
        }

        public virtual IList<string> GetGlobals(string name) {
            List<string> res = new List<string>();
            foreach (SymbolId scopeName in _scope.Scope.Keys) {
                string strName = SymbolTable.IdToString(scopeName);
                if (strName.StartsWith(name)) {
                    res.Add(strName);
                }
            }

            return res;
        }

        #endregion

        class SimpleCommandDispatcher : ICommandDispatcher {
            public object Execute(CompiledCode compiledCode, ScriptScope scope) {
                return compiledCode.Execute(scope);
            }
        }
    }

}
Back to Top