1 //
2 // Copyright (c) 2010-2020 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 System;
9 using System.Collections.Generic;
10 using Antmicro.Renode.UserInterface.Tokenizer;
11 using AntShell.Commands;
12 using Antmicro.Renode.Core;
13 using System.Linq;
14 using Antmicro.Renode.Utilities;
15 using Antmicro.Renode.Exceptions;
16 
17 namespace Antmicro.Renode.UserInterface.Commands
18 {
19     public class SetCommand : Command
20     {
PrintHelp(ICommandInteraction writer)21         public override void PrintHelp(ICommandInteraction writer)
22         {
23             base.PrintHelp(writer);
24             writer.WriteLine();
25             writer.WriteLine("You must provide the name of the {0}.".FormatWith(noun));
26             writer.WriteLine();
27             writer.WriteLine(String.Format("Usage:\n\r\t{0} {1} \"value\"\n\r\n\r\t{0} {1}\n\r\t\"\"\"\n\r\t[multiline value]\n\r\t\"\"\"", Name, noun));
28         }
29 
30 
31 
ProcessVariable(ICommandInteraction writer, string variableName, bool initialized = false)32         private void ProcessVariable(ICommandInteraction writer, string variableName, bool initialized = false)
33         {
34             variableName = GetVariableName(variableName);
35             EnableStringEater(variableName, initialized ? 2 : 1); //proper string eater level
36             while(GetStringEaterMode() > 0)
37             {
38                 writer.Write("> ");
39                 var line = writer.ReadLine();
40                 if(line == null)
41                 {
42                     DisableStringEater();
43                     break;
44                 }
45                 monitor.Parse(line, writer);
46             }
47         }
48 
49         [Runnable]
Run(ICommandInteraction writer, LiteralToken variable)50         public void Run(ICommandInteraction writer, LiteralToken variable)
51         {
52             ProcessVariable(writer, variable.Value);
53         }
54 
55         [Runnable]
Run(ICommandInteraction writer, VariableToken variable)56         public void Run(ICommandInteraction writer, VariableToken variable)
57         {
58             ProcessVariable(writer, variable.Value);
59         }
60 
61         [Runnable]
Run(ICommandInteraction writer, LiteralToken variable, MultilineStringTerminatorToken dummy)62         public void Run(ICommandInteraction writer, LiteralToken variable, MultilineStringTerminatorToken dummy)
63         {
64             ProcessVariable(writer, variable.Value, true);
65         }
66 
67         [Runnable]
Run(ICommandInteraction writer, VariableToken variable, MultilineStringTerminatorToken dummy)68         public void Run(ICommandInteraction writer, VariableToken variable, MultilineStringTerminatorToken dummy)
69         {
70             ProcessVariable(writer, variable.Value, true);
71         }
72 
73         [Runnable]
Run(ICommandInteraction writer, LiteralToken variable, Token value)74         public void Run(ICommandInteraction writer, LiteralToken variable, Token value)
75         {
76             var varName = variable.Value;
77 
78             varName = GetVariableName(varName);
79             SetVariable(varName, value);
80         }
81 
82         [Runnable]
Run(ICommandInteraction writer, VariableToken variable, Token value)83         public void Run(ICommandInteraction writer, VariableToken variable, Token value)
84         {
85             Run(writer, new LiteralToken(variable.Value), value);
86         }
87 
88         private readonly Action<string, Token> SetVariable;
89         private readonly Action<string, int> EnableStringEater;
90         private readonly Func<string, string> GetVariableName;
91         private readonly Action DisableStringEater;
92         private readonly Func<int> GetStringEaterMode;
93         private readonly String noun;
94 
SetCommand(Monitor monitor, String name, string noun, Action<string, Token> setVariable, Action<string, int> enableStringEater, Action disableStringEater, Func<int> getStringEaterMode, Func<string, string> getVariableName)95         public SetCommand(Monitor monitor, String name, string noun, Action<string, Token> setVariable, Action<string, int> enableStringEater, Action disableStringEater, Func<int> getStringEaterMode,
96             Func<string, string> getVariableName) : base(monitor, name, "sets {0}.".FormatWith(noun))
97         {
98             EnableStringEater = enableStringEater;
99             DisableStringEater = disableStringEater;
100             GetStringEaterMode = getStringEaterMode;
101             GetVariableName = getVariableName;
102             SetVariable = setVariable;
103             this.noun = noun;
104         }
105     }
106 }
107 
108