1 //
2 // Copyright (c) 2010-2024 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 System.IO;
13 using Antmicro.Renode.Core;
14 using Antmicro.Renode.Utilities;
15 
16 namespace Antmicro.Renode.UserInterface.Commands
17 {
18     public class IncludeFileCommand : Command
19     {
PrintHelp(ICommandInteraction writer)20         public override void PrintHelp(ICommandInteraction writer)
21         {
22             base.PrintHelp(writer);
23 
24             writer.WriteLine("\nTo load a script you have to provide an existing file name.");
25             writer.WriteLine();
26             writer.WriteLine("Supported file formats:");
27             writer.WriteLine("*.cs   - plugin file");
28             writer.WriteLine("*.py   - python script");
29             writer.WriteLine("*.repl - platform description file");
30             writer.WriteLine("other  - monitor script");
31         }
32 
33         [Runnable]
Run(ICommandInteraction writer, StringToken path)34         public bool Run(ICommandInteraction writer, StringToken path)
35         {
36             return Run(writer, path.Value);
37         }
38 
Run(ICommandInteraction writer, ReadFilePath path)39         private bool Run(ICommandInteraction writer, ReadFilePath path)
40         {
41             using(var progress = EmulationManager.Instance.ProgressMonitor.Start("Including script: " + path))
42             {
43                 bool result = false;
44                 switch(Path.GetExtension(path))
45                 {
46                 case ".py":
47                     result = PythonExecutor(path, writer);
48                     break;
49                 case ".cs":
50                     result = CsharpExecutor(path, writer);
51                     break;
52                 case ".repl":
53                     result = ReplExecutor(path, writer);
54                     break;
55                 default:
56                     result = ScriptExecutor(path);
57                     break;
58                 }
59 
60                 return result;
61             }
62         }
63 
64         private readonly Func<string,bool> ScriptExecutor;
65         private readonly Func<string, ICommandInteraction, bool> CsharpExecutor;
66         private readonly Func<string, ICommandInteraction, bool> PythonExecutor;
67         private readonly Func<string, ICommandInteraction, bool> ReplExecutor;
68 
IncludeFileCommand(Monitor monitor, Func<string, ICommandInteraction, bool> pythonExecutor, Func<string, bool> scriptExecutor, Func<string, ICommandInteraction, bool> csharpExecutor, Func<string, ICommandInteraction, bool> replExecutor)69         public IncludeFileCommand(Monitor monitor, Func<string, ICommandInteraction, bool> pythonExecutor, Func<string, bool> scriptExecutor, Func<string, ICommandInteraction, bool> csharpExecutor, Func<string, ICommandInteraction, bool> replExecutor) : base(monitor, "include", "loads a Monitor script, Python code, platform file or a plugin class.", "i")
70         {
71             this.CsharpExecutor = csharpExecutor;
72             this.PythonExecutor = pythonExecutor;
73             this.ScriptExecutor = scriptExecutor;
74             this.ReplExecutor = replExecutor;
75         }
76     }
77 }
78 
79