1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // Copyright (c) 2011-2015 Realtime Embedded 4 // 5 // This file is licensed under the MIT License. 6 // Full license text is available in 'licenses/MIT.txt'. 7 // 8 using AntShell.Commands; 9 using Antmicro.Renode.UserInterface.Tokenizer; 10 using System; 11 using Antmicro.Renode.Utilities; 12 using System.Collections.Generic; 13 using Antmicro.Renode.Exceptions; 14 15 namespace Antmicro.Renode.UserInterface.Commands 16 { 17 public class ExecuteCommand : Command 18 { PrintHelp(ICommandInteraction writer)19 public override void PrintHelp(ICommandInteraction writer) 20 { 21 base.PrintHelp(writer); 22 writer.WriteLine(); 23 writer.WriteLine("Provide a command or {0} to execute.".FormatWith(noun)); 24 writer.WriteLine(); 25 writer.WriteLine("Available {0}s:".FormatWith(noun)); 26 foreach(var variable in GetVariables()) 27 { 28 writer.WriteLine("\t{0}".FormatWith(variable)); 29 } 30 } 31 32 [Runnable] Run(ICommandInteraction writer, params Token[] tokens)33 public virtual void Run(ICommandInteraction writer, params Token[] tokens) 34 { 35 if(tokens.Length == 1 && tokens[0] is VariableToken) 36 { 37 var macroLines = GetVariable(tokens[0] as VariableToken).GetObjectValue().ToString().Split('\n'); 38 foreach(var line in macroLines) 39 { 40 if(!monitor.Parse(line, writer)) 41 { 42 throw new RecoverableException(string.Format("Parsing line '{0}' failed.", line)); 43 } 44 } 45 } 46 else 47 { 48 if(!monitor.ParseTokens(tokens, writer)) 49 { 50 throw new RecoverableException("Parsing failed."); 51 } 52 } 53 } 54 ExecuteCommand(Monitor monitor, string name, string noun, Func<VariableToken, Token> getVariable, Func<IEnumerable<string>> getVariables)55 public ExecuteCommand(Monitor monitor, string name, string noun, Func<VariableToken, Token> getVariable, Func<IEnumerable<string>> getVariables):base(monitor, name, "executes a command or the content of a {0}.".FormatWith(noun)) 56 { 57 GetVariable = getVariable; 58 GetVariables = getVariables; 59 this.noun = noun; 60 } 61 62 private readonly string noun; 63 private readonly Func<VariableToken, Token> GetVariable; 64 private readonly Func<IEnumerable<string>> GetVariables; 65 } 66 } 67 68