Skip to content

Commit

Permalink
Interactive shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
teplofizik committed Aug 31, 2022
1 parent a61bc50 commit 5a0688b
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 4 deletions.
5 changes: 3 additions & 2 deletions NyaFs/ImageFormat/Elements/Fs/Filesystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public bool Exists(string Path)

public FilesystemItem GetElement(string Path)
{
if (Path == ".") return Root;
if(Path.Length == 0)
if ((Path == ".") || (Path == "/")) return Root;

if (Path.Length == 0)
throw new ArgumentException($"{Path} is empty");

if (Path[0] == '/') Path = Path.Substring(1);
Expand Down
10 changes: 10 additions & 0 deletions NyaFs/ImageFormat/Elements/Fs/FilesystemItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,22 @@ public FilesystemItem(Types.FilesystemItemType Type, string Filename, uint User,
public uint Mode; // Access rights 12 bit 4 4 4

public string Filename;
public string ShortFilename
{
get
{
var Idx = Filename.LastIndexOf('/');
return (Idx > 0) ? Filename.Substring(Idx + 1) : Filename;
}
}

public uint Major = 8;
public uint Minor = 1;
public uint RMajor = 0;
public uint RMinor = 0;

public virtual long Size => 0;

public DateTime Created = DateTime.UnixEpoch;
public DateTime Modified = DateTime.UnixEpoch;
}
Expand Down
2 changes: 2 additions & 0 deletions NyaFs/ImageFormat/Elements/Fs/Items/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public override string ToString()
{
return $"FILE {Filename} {User}:{Group} {Mode:x03} {Content.Length} bytes";
}

public override long Size => Content.Length;
}
}
1 change: 1 addition & 0 deletions NyaFs/ImageFormat/Elements/Fs/Items/SymLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public override string ToString()
{
return $"LINK {Filename} {User}:{Group} {Mode:x03} => {Target}";
}
public override long Size => Target.Length;
}
}
4 changes: 4 additions & 0 deletions NyaFs/Processor/ImageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace NyaFs.Processor
{
public class ImageProcessor
{
public string ActivePath = "/";

ImageFormat.BaseImageBlob Blob = new ImageFormat.BaseImageBlob();

public void SetFs(Filesystem Fs) => Blob.SetFilesystem(0, Fs);
Expand All @@ -25,6 +27,8 @@ public class ImageProcessor

public ImageFormat.BaseImageBlob GetBlob() => Blob;

public bool IsFsLoaded => Blob.IsProvidedFs;

public void Process(Scripting.Script Script)
{
foreach(var S in Script.Steps)
Expand Down
60 changes: 60 additions & 0 deletions NyaFs/Processor/Scripting/Commands/Fs/Interactive/Cd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.Processor.Scripting.Commands.Fs.Interactive
{
class Cd : ScriptStepGenerator
{
public Cd() : base("cd")
{
AddConfig(new ScriptArgsConfig(0, new ScriptArgsParam[] { new Params.FsPathScriptArgsParam() }));
}
public override ScriptStep Get(ScriptArgs Args)
{
return new CdScriptStep(Args.RawArgs[0]);
}

public class CdScriptStep : ScriptStep
{
string Path;

public CdScriptStep(string Path) : base("cd")
{
this.Path = Path;
}

private string CombinePath(string Base, string Name)
{
if ((Base == "/") || (Base == ".")) return Name;

return Base + "/" + Name;
}

public override ScriptStepResult Exec(ImageProcessor Processor)
{
var Fs = Processor.GetFs();
if ((Fs == null) || !Fs.Loaded)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem not loaded");
else
{
if(Path.Length == 0)
return new ScriptStepResult(ScriptStepStatus.Error, "Specify path!");

if (Path == ".")
return new ScriptStepResult(ScriptStepStatus.Ok, Processor.ActivePath);

var Item = Helper.FsHelper.GetItem(Fs, Processor.ActivePath, Path);

if(Item != null)
{
Processor.ActivePath = "/" + Item.Filename;
return new ScriptStepResult(ScriptStepStatus.Error, $"Active directory is changed to {Processor.ActivePath}.");
}
else
return new ScriptStepResult(ScriptStepStatus.Error, "Specified path is not found.");
}
}
}
}
}
117 changes: 117 additions & 0 deletions NyaFs/Processor/Scripting/Commands/Fs/Interactive/Ls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.Processor.Scripting.Commands.Fs.Interactive
{
class Ls : ScriptStepGenerator
{
public Ls() : base("ls")
{
AddConfig(new ScriptArgsConfig(0, new ScriptArgsParam[] { }));
AddConfig(new ScriptArgsConfig(1, new ScriptArgsParam[] { new Params.FsPathScriptArgsParam() }));
}
public override ScriptStep Get(ScriptArgs Args)
{
if(Args.ArgConfig == 0)
return new LsScriptStep();
else
return new LsScriptStep(Args.RawArgs[0]);
}

public class LsScriptStep : ScriptStep
{
string Path = null;

public LsScriptStep() : base("ls")
{

}

public LsScriptStep(string Path) : base("ls")
{
this.Path = Path;
}

private ImageFormat.Elements.Fs.Items.Dir GetTarget(ImageFormat.Elements.Fs.Filesystem Fs, string Active)
{
if (Path == null)
{
try
{
return Fs.GetDirectory(Active);
}
catch(Exception E)
{
return null;
}
}
else
{
return Helper.FsHelper.GetItem(Fs, Active, Path) as ImageFormat.Elements.Fs.Items.Dir;
}
}

public override ScriptStepResult Exec(ImageProcessor Processor)
{
var Fs = Processor.GetFs();
if ((Fs == null) || !Fs.Loaded)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem not loaded");
else
{
var Target = GetTarget(Fs, Processor.ActivePath);

if (Target == null)
return new ScriptStepResult(ScriptStepStatus.Error, "Target directory is not found!");

foreach (var I in Target.Items)
{
Log.Write(0, FormatItem(I));
}

return new ScriptStepResult(ScriptStepStatus.Ok, null);
}
}

private string FormatItem(ImageFormat.Elements.Fs.FilesystemItem Item)
{
var Mode = $"{GetItemType(Item)}{ConvertModeToString(Item.Mode)}";
var User = $"{Item.User}".PadLeft(5);
var Group = $"{Item.Group}".PadLeft(5);

var Size = $"{Item.Size}".PadLeft(12);

var Name = (Item.ItemType == ImageFormat.Types.FilesystemItemType.SymLink) ? $"{Item.ShortFilename} -> {(Item as ImageFormat.Elements.Fs.Items.SymLink).Target}" : Item.ShortFilename;

return $"{Mode} {User} {Group} {Size} {Name}";
}

private string GetItemType(ImageFormat.Elements.Fs.FilesystemItem Item)
{
switch(Item.ItemType)
{
case ImageFormat.Types.FilesystemItemType.File: return "-";
case ImageFormat.Types.FilesystemItemType.Dir: return "d";
case ImageFormat.Types.FilesystemItemType.SymLink: return "l";
case ImageFormat.Types.FilesystemItemType.Node: return "c";
case ImageFormat.Types.FilesystemItemType.Block: return "b";
case ImageFormat.Types.FilesystemItemType.Fifo: return "f";
default: return "?";
}
}
public static string ConvertModeToString(UInt32 Mode)
{
var Res = "";
for (int i = 0; i < 3; i++)
{
UInt32 Part = (Mode >> (2 - i) * 4) & 0x7;

Res += ((Part & 0x04) != 0) ? "r" : "-";
Res += ((Part & 0x02) != 0) ? "w" : "-";
Res += ((Part & 0x01) != 0) ? ((((Mode >> 12 >> (2 - i)) & 0x1) != 1) ? "x" : "s") : "-";
}
return Res;
}
}
}
}
35 changes: 35 additions & 0 deletions NyaFs/Processor/Scripting/Commands/Fs/Interactive/Pwd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NyaFs.Processor.Scripting.Commands.Fs.Interactive
{
class Pwd : ScriptStepGenerator
{
public Pwd() : base("pwd")
{
AddConfig(new ScriptArgsConfig(0, new ScriptArgsParam[] { }));
}
public override ScriptStep Get(ScriptArgs Args)
{
return new PwdScriptStep();
}

public class PwdScriptStep : ScriptStep
{
public PwdScriptStep() : base("pwd")
{

}

public override ScriptStepResult Exec(ImageProcessor Processor)
{
var Fs = Processor.GetFs();
if ((Fs == null) || !Fs.Loaded)
return new ScriptStepResult(ScriptStepStatus.Error, "Filesystem not loaded");
else
return new ScriptStepResult(ScriptStepStatus.Ok, Processor.ActivePath);
}
}
}
}
1 change: 0 additions & 1 deletion NyaFs/Processor/Scripting/Commands/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class Info : ScriptStepGenerator
public Info() : base("info")
{
AddConfig(new ScriptArgsConfig(0, new ScriptArgsParam[] { }));

}

public override ScriptStep Get(ScriptArgs Args)
Expand Down
36 changes: 36 additions & 0 deletions NyaFs/Processor/Scripting/Helper/FsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,41 @@ internal static string DetectFilePath(this ScriptStep S, string Path)

return null;
}

private static ImageFormat.Elements.Fs.FilesystemItem GetItem(ImageFormat.Elements.Fs.Filesystem Fs, string Path)
{
try
{
return Fs.GetElement(Path);
}
catch(Exception)
{
return null;
}
}

internal static ImageFormat.Elements.Fs.FilesystemItem GetItem(ImageFormat.Elements.Fs.Filesystem Fs, string Base, string Path)
{
if (Path == "")
return null;

// TODO: Simplify path with ..
if (Path == "..")
return Fs.GetParentDirectory(Base);

if (Path[0] == '/')
// Provided absolute path
return GetItem(Fs, Path);
else
return GetItem(Fs, CombinePath(Base, Path));
}

internal static string CombinePath(string Base, string Name)
{
if ((Base == "/") || (Base == ".")) return Name;

return Base + "/" + Name;
}

}
}
3 changes: 3 additions & 0 deletions NyaFs/Processor/Scripting/ScriptBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public class ScriptBaseInteractive : ScriptBaseAll
public ScriptBaseInteractive()
{
// ls {path}
Add(new Commands.Fs.Interactive.Ls());
// pwd
Add(new Commands.Fs.Interactive.Pwd());
// cd {path}
Add(new Commands.Fs.Interactive.Cd());
// export {path} {localfilename}
// import {path} {localfilename}
}
Expand Down
5 changes: 4 additions & 1 deletion NyaImageTool/InteractiveShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public void ShellLoop()
{
while(true)
{
Console.Write("Nya> ");
if(Processor.IsFsLoaded)
Console.Write($"Nya:{Processor.ActivePath}> ");
else
Console.Write("Nya> ");

var Readed = Console.ReadLine();

Expand Down
Binary file added docs/images/interactiveshell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5a0688b

Please sign in to comment.