示例#1
0
                protected override void Run()
                {
                    if (Argv.Count == 1)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }
                    bool    symbolic = true;
                    NixPath source   = MainSession.WorkingDirectory.Combine(Argv[1]);
                    NixPath destination;

                    if (Argv.Count == 2)
                    {
                        destination = new NixPath(source.TopPath());
                    }
                    else
                    {
                        destination = MainSession.WorkingDirectory.Combine(Argv[2]);
                    }

                    if (MainSystem.RootDrive.MakeLink(source, MainSession.WorkingDirectory.Combine(destination)) != 1)
                    {
                        StdOut.WriteLine("Error attempting to write link.");
                    }
                }
示例#2
0
                protected override void Run()
                {
                    if (Argv.Count <= 2)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    NixPath fromPath = MainSession.PhysicalDirectory.Combine(Argv[1]);
                    NixPath toPath   = OpenPath(Argv[2]);

                    try
                    {
                        MainSystem.RootDrive.Move(fromPath, toPath);
                    }
                    catch (System.IO.FileNotFoundException exp)
                    {
                        StdOut.WriteLine("No such file or directory: " + Argv[1]);
                    }
                    catch (System.Exception exp)
                    {
                        StdOut.WriteLine(exp.Message);
                    }
                    return;
                }
示例#3
0
 protected override void Run()
 {
     if (Argv.Count == 1)
     {
         // Read from stdin
     }
     else
     {
         for (int i = 1; i < Argv.Count; i++)
         {
             NixPath path = OpenPath(Argv[i]);
             if (MainSystem.RootDrive.IsFileOrDirectory(path))
             {
                 try
                 {
                     using (Stream file = MainSystem.RootDrive.OpenFile(path, FileAccess.Read, FileMode.Open))
                     {
                         StreamReader reader = new StreamReader(file);
                         StdOut.Write(reader.ReadToEnd());
                     }
                 }
                 catch (System.Exception exp)
                 {
                     StdOut.WriteLine("Error reading file for cat: " + exp.Message);
                 }
             }
             else
             {
                 StdOut.Write(GetCommand() + ": " + Argv[i] + ": No such file or directory\n");
             }
         }
     }
 }
示例#4
0
            public ProgramRunType GetProgramRunType(string cmd)
            {
                NixPath path = new NixPath(cmd);

                ProgramRunType result = new ProgramRunType();
                string         pathTo = RootDrive.GetPathTo(RootDrive.FollowLinks(path));

                Debug.Log("GPRT: " + path.ToString() + " | " + pathTo);
                using (StreamReader reader = new StreamReader(pathTo))
                {
                    string firstLine = reader.ReadLine();
                    if (firstLine.Length > 2)
                    {
                        if (firstLine[0] == '#' && firstLine[1] == '!')
                        {
                            Debug.Log("Has a shebang! " + firstLine);
                            result.BinaryCode = false;
                            result.Shebang    = firstLine.Substring(2);
                        }
                        // Definitly not a good way of determining if it is a DLL
                        // Need to look further into the file to determine if it really is.
                        else if (firstLine[0] == 'M' && firstLine[1] == 'Z')
                        {
                            Debug.Log("Possibly a DLL");
                            result.BinaryCode = true;
                        }
                    }
                }
                return(result);
            }
示例#5
0
                protected override void Run()
                {
                    if (Argv.Count <= 2)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    NixPath fromPath = MainSession.PhysicalDirectory.Combine(Argv[1]);
                    NixPath toPath   = OpenPath(Argv[2]);

                    StdOut.WriteLine("Copying from: " + fromPath + "\nCopying to: " + toPath);
                    try
                    {
                        MainSystem.RootDrive.Copy(fromPath, toPath);
                    }
                    catch (System.IO.FileNotFoundException exp)
                    {
                        StdOut.WriteLine("No such file or directory: " + Argv[1]);
                    }
                    catch (System.Exception exp)
                    {
                        StdOut.WriteLine(exp.Message);
                    }

                    //StdOut.Write ("No such file or directory: " + Argv[1]);

                    /*if (MainSystem.RootDrive.IsDirectory(newPath.ToString())) {
                     *  MainSession.SetWorkingDirectory(newPath);
                     * }
                     * else {
                     *  StdOut.Write(newPath.ToString() + " is not a directory.\n");
                     * }*/
                    return;
                }
示例#6
0
            public Stream OpenFileHandler(string filename, FileMode mode, FileAccess access)
            {
                //NixPath path = OpenPath(filename);
                NixPath path = MainSystem.RootDrive.FollowLinks(MainSession.WorkingDirectory.Combine(new NixPath(filename)));

                return(MainSystem.RootDrive.OpenFile(path, access, mode));
            }
示例#7
0
            public FindCommandResult FindCommand(string cmd)
            {
                Debug.Log("Searching for: " + cmd);
                FindCommandResult result = new FindCommandResult();

                string [] envPathSplit = BaseSession.GetEnvValue("PATH").Split(new char[] { ':' });

                for (int i = 0; i < envPathSplit.Length; i++)
                {
                    NixPath path = new NixPath(envPathSplit[i]);
                    path.AppendPath(cmd);
                    if (RootDrive.IsFile(path))
                    {
                        result.Path    = path.ToString();
                        result.Builtin = false;
                        result.Found   = true;
                        return(result);
                    }
                }

                if (BinPrograms.ContainsKey(cmd))
                {
                    result.Path    = cmd;
                    result.Builtin = true;
                    result.Found   = true;
                    return(result);
                }

                result.Found = false;
                return(result);
            }
                public void DeleteDirectory(NixPath path, bool recursive)
                {
                    string tpath = path.ToString();

                    Debug.Log("Delete dir: " + tpath);
                    Directory.Delete(GetPathTo(tpath), recursive);
                }
示例#9
0
            public NixPath Combine(NixPath path)
            {
                NixPath newPath = new NixPath(this.ToString());

                newPath.AppendPath(path.ToString());
                Dirty = true;
                return(newPath);
            }
示例#10
0
 public NixPath(NixPath path)
 {
     Absolute = path.Absolute;
     Path     = new List <string>();
     if (path.Path.Count > 0)
     {
         SetPath(path.ToString());
     }
 }
示例#11
0
                protected List <string> CheckFiles(string input, bool onlyOne)
                {
                    List <string> result = new List <string>();

                    if (input == null || input.Length == 0)
                    {
                        return(result);
                    }

                    // Path is already correct?
                    // Extend to deal with situations where the file/folder already exists
                    // but there are other entires that will still match it.
                    NixPath full = OpenPath(input);

                    if (full.IsRoot() ||
                        MainSystem.RootDrive.IsFile(full) ||
                        MainSystem.RootDrive.IsDirectory(full))
                    {
                        return(result);
                    }

                    NixPath toCheck     = new NixPath(input);
                    string  filename    = toCheck.PopPath();
                    NixPath toCheckFull = OpenPath(toCheck);

                    //Debug.Log("To check: " + toCheck.ToString() + " | " + toCheckFull.ToString() + " | " + filename);
                    if (MainSystem.RootDrive.IsDirectory(toCheck))
                    {
                        SOD.Nix.FileSystem.FileNode[] files = MainSystem.RootDrive.ListFiles(toCheckFull);
                        if (files != null)
                        {
                            for (int i = 0; i < files.Length; i++)
                            {
                                FileInfo info = files[i].Info;
                                if (info.Name.IndexOf(filename) == 0)
                                {
                                    string  entry     = info.Name;
                                    NixPath entryPath = new NixPath(toCheck);
                                    entryPath.AppendPath(entry);

                                    if (info.Attributes == FileAttributes.Directory)
                                    {
                                        result.Add(entryPath.ToString() + "/");
                                    }
                                    else
                                    {
                                        result.Add(entryPath.ToString());
                                    }
                                }
                            }
                        }
                    }
                    return(result);
                }
示例#12
0
                public void Execute(string input)
                {
                    List <string> result = Parse(input);

                    if (result.Count == 1)
                    {
                        MainSystem.Execute(MainSession, result[0]);
                    }
                    else if (result.Count > 1)
                    {
                        string args    = "";
                        string operand = null;
                        string args2   = "";
                        args = result[0];
                        if (result.Count >= 3)
                        {
                            operand = result[1];
                            args2   = result[2];
                        }

                        if (args2.Length == 0)
                        {
                            MainSystem.Execute(MainSession, args);
                        }
                        else
                        {
                            if (operand == ">" || operand == "<")
                            {
                                NixPath filePath = new NixPath(args2);
                                NixPath path     = OpenPath(filePath);
                                using (Stream fstream = MainSystem.RootDrive.OpenFile(path, FileAccess.Write, FileMode.Create))
                                {
                                    if (operand == ">")
                                    {
                                        MainSystem.Execute(MainSession, args, fstream, null);
                                    }
                                    else if (operand == "<")
                                    {
                                        MainSystem.Execute(MainSession, args, null, fstream);
                                    }
                                }
                            }
                            else if (operand == "|")
                            {
                                NixStream tmp = new NixStream();
                                MainSystem.Execute(MainSession, args, tmp, null);
                                MainSystem.Execute(MainSession, args2, null, tmp);
                            }
                        }
                    }
                }
示例#13
0
            public int RenameFileHandler(string fromname, string toname)
            {
                NixPath frompath = MainSession.PhysicalDirectory.Combine(new NixPath(fromname));
                NixPath topath   = MainSession.PhysicalDirectory.Combine(new NixPath(toname));

                try
                {
                    MainSystem.RootDrive.Rename(frompath, topath);
                }
                catch (Exception exp)
                {
                    StdOut.WriteLine("Error renaming file from Lua: " + exp.Message);
                    return(-1);
                }
                return(0);
            }
示例#14
0
 public string GetPathTo(NixPath path)
 {
     try
     {
         if (path.Absolute)
         {
             return(RootFolder + path.BuildString(true, true));
         }
         return(RootFolder + "\\" + path.BuildString(true, true));
     }
     catch (Exception exp)
     {
         Debug.Log("Error getting path: " + exp.Message);
         return("err");
     }
 }
示例#15
0
                protected override void Run()
                {
                    if (Argv.Count < 4)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }

                    string type = "lua";

                    for (int i = 1; i < Argv.Count; i++)
                    {
                        switch (Argv[i])
                        {
                        case "-t":
                            type = Argv[i + 1];
                            break;
                        }
                    }

                    NixPath sourceFile = OpenPath(Argv[Argv.Count - 2]);
                    NixPath destFile   = OpenPath(Argv[Argv.Count - 1]);

                    if (type != "lua")
                    {
                        StdOut.WriteLine("Unknown device type: " + type);
                        return;
                    }

                    if (!MainSystem.RootDrive.IsFile(sourceFile))
                    {
                        StdOut.WriteLine("Cannot find file: " + sourceFile.ToString());
                        return;
                    }

                    if (type == "lua")
                    {
                        Device.LuaDevice device = new Device.LuaDevice(MainSession, MainSystem);
                        device.LoadFile(sourceFile);
                        MainSystem.MainDeviceManager.AddDevice(device);
                        MainSystem.RootDrive.MakeCharacterDevice(destFile, device.Id);
                    }
                }
示例#16
0
                public int MakeLink(NixPath source, NixPath destination)
                {
                    byte [] pathBytes  = System.Text.Encoding.UTF8.GetBytes(source.ToString());
                    NixPath outputFile = FollowLinks(destination);

                    if (outputFile != null)
                    {
                        using (FileStream output = File.OpenWrite(GetPathTo(destination)))
                        {
                            output.Write(SymlinkHeader, 0, SymlinkHeader.Length);
                            output.Write(pathBytes, 0, pathBytes.Length);
                        }
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
示例#17
0
                public int MakeCharacterDevice(NixPath destination, int id)
                {
                    byte [] idBytes    = System.Text.Encoding.UTF8.GetBytes(id.ToString());
                    NixPath outputFile = FollowLinks(destination);

                    if (outputFile != null)
                    {
                        using (FileStream output = File.OpenWrite(GetPathTo(destination)))
                        {
                            output.Write(CharacterDeviceHeader, 0, CharacterDeviceHeader.Length);
                            output.Write(idBytes, 0, idBytes.Length);
                        }
                        return(1);
                    }
                    else
                    {
                        return(0);
                    }
                }
示例#18
0
 public void Copy(NixPath fromPath, NixPath toPath)
 {
     if (!IsFileOrDirectory(fromPath))
     {
         throw new System.IO.FileNotFoundException();
     }
     if (IsDirectory(toPath))
     {
         // Cannot copy to a directory.
         throw new Exception("Cannot copy to a directory");
     }
     try
     {
         File.Copy(GetPathTo(fromPath), GetPathTo(toPath));
     }
     catch (Exception exp)
     {
         throw new Exception(exp.Message);
     }
 }
示例#19
0
            public Session()
            {
                BaseSession = this;

                WorkingDirectory           = new NixPath();
                WorkingDirectory.Absolute  = true;
                PhysicalDirectory          = new NixPath();
                PhysicalDirectory.Absolute = true;

                ActiveStack                      = new Stack <Bin.Program>();
                EnvironmentVariables             = new Dictionary <string, string>();
                EnvironmentVariables["HOSTNAME"] = "engineering_comp";
                EnvironmentVariables["USER"]     = "******";
                EnvironmentVariables["HOSTTYPE"] = "spaceship";
                //EnvironmentVariables["PWD"] = WorkingDirectory.ToString();
                EnvironmentVariables["PWD"]    = "/";
                EnvironmentVariables["PS1"]    = @"\[\033[1;32m\]$USER@$HOSTNAME\[\033[0m\]:\[\033[1;34m\]$PWD\[\033[0m\]- ";
                EnvironmentVariables["PATH"]   = @"/usr/bin";
                EnvironmentVariables["TMPDIR"] = @"/tmp";
            }
示例#20
0
                public Stream OpenFile(NixPath path, FileAccess access, FileMode mode)
                {
                    string openPath = GetPathTo(path);
                    int    deviceId = GetDeviceId(path);

                    if (deviceId != -1)
                    {
                        //return MainSystem.MainDeviceManager.FindDevice(deviceId);
                        Device.CharacterDevice device = MainSystem.MainDeviceManager.FindDevice(deviceId);
                        if (device != null)
                        {
                            return(device.CreateStream());
                        }
                        return(null);
                    }
                    else
                    {
                        return(File.Open(openPath, mode, access));
                    }
                    return(null);
                }
示例#21
0
                public NixPath FollowLinks(NixPath path)
                {
                    if (path.Path.Count == 0)
                    {
                        return(path);
                    }
                    NixPath build = new NixPath();

                    if (path.Absolute)
                    {
                        build.Absolute = true;
                    }
                    for (int i = 0; i < path.Path.Count; i++)
                    {
                        if (path.Path[i] == "..")
                        {
                            build.PopPath();
                            continue;
                        }
                        build.AppendPath(path.Path[i]);
                        if (IsDirectory(build))
                        {
                            continue;
                        }

                        NixPath link = GetLink(build);
                        while (link != null)
                        {
                            build = link;
                            link  = GetLink(link);
                        }

                        if (build == null)
                        {
                            return(path);
                        }
                    }
                    return(build);
                }
示例#22
0
            public int RemoveFileHandler(string filename)
            {
                NixPath path = MainSession.PhysicalDirectory.Combine(new NixPath(filename));

                try
                {
                    if (MainSystem.RootDrive.IsDirectory(path))
                    {
                        MainSystem.RootDrive.DeleteDirectory(path, false);
                    }
                    else
                    {
                        MainSystem.RootDrive.DeleteFile(path);
                    }
                }
                catch (Exception exp)
                {
                    StdOut.WriteLine("Error removing file from Lua: " + exp.Message);
                    return(-1);
                }
                return(0);
            }
示例#23
0
 public void Move(NixPath fromPath, NixPath toPath)
 {
     if (!IsFileOrDirectory(fromPath))
     {
         throw new System.IO.FileNotFoundException();
     }
     if (IsDirectory(toPath))
     {
         NixPath newPath = new NixPath(toPath.ToString());
         newPath.AppendPath(fromPath.TopPath());
         File.Move(GetPathTo(fromPath), GetPathTo(newPath));
         return;
     }
     try
     {
         File.Move(GetPathTo(fromPath), GetPathTo(toPath));
     }
     catch (Exception exp)
     {
         throw new Exception(exp.Message);
     }
 }
示例#24
0
                public void MakeDirectory(NixPath path, bool createParents)
                {
                    string pathStr = GetPathTo(path);

                    if (createParents)
                    {
                        Directory.CreateDirectory(pathStr);
                    }
                    else
                    {
                        NixPath t = new NixPath(path.ToString());
                        t.PopPath();
                        if (IsDirectory(t))
                        {
                            Directory.CreateDirectory(pathStr);
                        }
                        else
                        {
                            throw new DirectoryNotFoundException();
                        }
                    }
                }
示例#25
0
                protected override void Run()
                {
                    LuaSystem luaSys = new LuaSystem(MainSession, MainSystem, StdOut, StdIn, StdErr);
                    Lua       l      = luaSys.Lua;

                    try
                    {
                        if (Argv.Count > 1)
                        {
                            NixPath newPath = OpenPath(Argv[1]);
                            string  file    = MainSystem.RootDrive.GetPathTo(newPath.ToString());
                            Debug.Log("File to load: " + newPath.ToString());
                            if (File.Exists(file))
                            {
                                string argStr = "arg={}\n";
                                argStr += "arg[0]=\"" + Argv[1].Replace("\\", "/") + "\"\n";
                                for (int i = 2; i < Argv.Count; i++)
                                {
                                    argStr += "arg[" + (i - 1) + "]=\"" + Argv[i] + "\"\n";
                                }
                                l.DoString(argStr);
                                l.DoFile(newPath.ToString());
                            }
                            else
                            {
                                StdOut.WriteLine("Unable to find file: " + Argv[1]);
                            }
                        }
                        else
                        {
                            // Do stdin stuff
                        }
                    }
                    catch (Exception exp)
                    {
                        StdOut.WriteLine("Exception executing Lua: " + exp.Message);
                    }
                }
示例#26
0
                public FileNode[] ListFiles(NixPath path)
                {
                    string tpath = GetPathTo(path);

                    if (!Directory.Exists(tpath))
                    {
                        return(null);
                    }
                    string[]   entries = Directory.GetFileSystemEntries(tpath);
                    FileNode[] result  = new FileNode[entries.Length];
                    for (int i = 0; i < entries.Length; i++)
                    {
                        try{
                            result[i]         = new FileNode(entries[i]);
                            result[i].Symlink = GetLink(entries[i]);
                        }
                        catch (Exception x)
                        {
                            Debug.Log("Exception: " + x.Message);
                        }
                    }
                    return(result);
                }
示例#27
0
                protected override void Run()
                {
                    NixPath newPath = MainSession.PhysicalDirectory;

                    if (Argv.Count > 1)
                    {
                        //newPath = MainSession.WorkingDirectory.Combine(Argv[1]);
                        newPath = OpenPath(Argv[1]);
                    }

                    SOD.Nix.FileSystem.FileNode[] files = MainSystem.RootDrive.ListFiles(newPath.ToString());
                    if (files != null)
                    {
                        for (int i = 0; i < files.Length; i++)
                        {
                            FileInfo info = files[i].Info;
                            if (files[i].Symlink != null)
                            {
                                StdOut.Write("@");
                            }
                            StdOut.Write(info.Name);
                            if ((info.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
                            {
                                StdOut.Write("/");
                            }
                            if (files[i].Symlink != null)
                            {
                                StdOut.Write(" -> " + files[i].Symlink.ToString());
                            }
                            StdOut.Write("\n");
                        }
                    }
                    else
                    {
                        StdOut.Write(newPath.ToString() + " is not a directory.\n");
                    }
                }
示例#28
0
                protected override void Run()
                {
                    if (Argv.Count <= 1)
                    {
                        StdOut.WriteLine("Need help");
                        return;
                    }
                    try{
                        NixPath newPath      = MainSession.WorkingDirectory.Combine(Argv[1]).ResolvePath();
                        NixPath followedPath = MainSystem.RootDrive.FollowLinks(MainSession.PhysicalDirectory.Combine(Argv[1]));

                        if (MainSystem.RootDrive.IsDirectory(followedPath))
                        {
                            MainSession.SetWorkingDirectory(newPath);
                        }
                        else
                        {
                            StdOut.Write(newPath.ToString() + " is not a directory.\n");
                        }
                    }
                    catch (System.Exception exp) {
                        Debug.Log("CD EXP: " + exp.Message);
                    }
                }
示例#29
0
 public void SetWorkingDirectory(NixPath path)
 {
     WorkingDirectory            = path;
     EnvironmentVariables["PWD"] = path.ToString();
     PhysicalDirectory           = MainSystem.RootDrive.FollowLinks(path);
 }
示例#30
0
 public FileNode(string path)
 {
     Info    = new FileInfo(path);
     Symlink = null;
 }