From 2f6ddb49ec97625f943c2fb4f3c5f0323692e9bc Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Wed, 30 Nov 2022 19:33:05 -0800 Subject: [PATCH] working directory tracking closes #1 --- bin/init.py | 2 +- bin/ls.py | 7 ++++--- bin/ps.py | 4 +++- bin/pwd.py | 10 ++++++++++ bin/sh.py | 6 ++++-- fibonaccios/__init__.py | 20 +++++++++++++++----- lib/console.py | 4 ++++ 7 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 bin/pwd.py diff --git a/bin/init.py b/bin/init.py index f4f7049..5945fd0 100644 --- a/bin/init.py +++ b/bin/init.py @@ -7,7 +7,7 @@ def get_app_class(template): lines = f.readlines() for line in lines: if line.strip(): - last_pid = self.os.launch(*shlex.split(line)) + last_pid = self.launch(*shlex.split(line)) self.transfer_console(last_pid) self.next_function = 'loop' diff --git a/bin/ls.py b/bin/ls.py index addd213..30e4545 100644 --- a/bin/ls.py +++ b/bin/ls.py @@ -1,4 +1,5 @@ import os +import console def get_app_class(template): class App(template): @@ -10,13 +11,13 @@ def get_app_class(template): try: for file in os.listdir(path): - self.console.write(file) + console.write_str(self.console, file + "\n") except OSError as e: try: os.stat(path) - self.console.write(path) + console.write_str(self.console, path + "\n") except OSError: - self.console.write(f"ls: cannot access '{path}': No such file or directory") + console.write_str(self.console, f"ls: cannot access '{path}': No such file or directory\n") self.exit() diff --git a/bin/ps.py b/bin/ps.py index 23994dc..3fa7dca 100644 --- a/bin/ps.py +++ b/bin/ps.py @@ -1,3 +1,5 @@ +import console + def get_app_class(template): class App(template): def F_main(self): @@ -5,7 +7,7 @@ def get_app_class(template): for pid, app in apps.items(): args = app.argv[1:] name = app.name - self.console.write(f'{pid} {name} {args}') + console.write_str(self.console, f'{pid} {name} {args}\n') self.exit() return App diff --git a/bin/pwd.py b/bin/pwd.py new file mode 100644 index 0000000..7e19437 --- /dev/null +++ b/bin/pwd.py @@ -0,0 +1,10 @@ +import os +import console + +def get_app_class(template): + class App(template): + def F_main(self): + console.write_str(self.console, os.getcwd() + "\n") + self.exit() + + return App diff --git a/bin/sh.py b/bin/sh.py index 520dab7..baf23b4 100644 --- a/bin/sh.py +++ b/bin/sh.py @@ -19,11 +19,13 @@ def get_app_class(template): if line: try: if line[0] == 'bg': - self.os.launch(*line[1:]) + self.launch(*line[1:]) + elif line[0] == 'cd': + self.chdir(line[1]) elif line[0] == 'exit': self.exit() else: - pid = self.os.launch(*line) + pid = self.launch(*line) self.wait(pid) self.transfer_console(pid) except fibonaccios.exceptions.AppNotFound as e: diff --git a/fibonaccios/__init__.py b/fibonaccios/__init__.py index d696d7f..e44e301 100644 --- a/fibonaccios/__init__.py +++ b/fibonaccios/__init__.py @@ -5,19 +5,22 @@ from fibonaccios.apploader import get_app import fibonaccios.apploader # apploader hack import time import usb_cdc +import os class App: - def __init__(self, os, pid, name, *argv): + def __init__(self, os, pid, name, wd, *argv): self.os = os self.pid = pid self.name = name self.store = {} self.next_function = 'main' self.run_at = -1 + self.wd = wd self.argv = (self,) + argv self.waiting_for = -1 def iterate(self): + os.chdir(self.wd) if time.monotonic() >= self.run_at and not self.waiting_for in self.os.apps: getattr(self, f'F_{self.next_function}')() @@ -34,6 +37,10 @@ class App: else: raise NotConsoleController(self.os.controllers[-1]) + def chdir(self, wd): + os.chdir(wd) + self.wd = os.getcwd() + def jump(self, function): self.next_function = function @@ -41,7 +48,10 @@ class App: self.run_at = time.monotonic() + seconds def exec(self, command, *argv): - self.os.apps[self.pid] = get_app(command)(self.os, self.pid, command, *argv) + self.os.apps[self.pid] = get_app(command)(self.os, self.pid, command, self.wd, *argv) + + def launch(self, command, *argv): + return self.os.launch(command, self.wd, *argv) def wait(self, pid): self.waiting_for = pid @@ -65,7 +75,7 @@ class OS: self.console = usb_cdc.console usb_cdc.console.timeout = 0 self.next_pidi = 0 - self.launch('init') + self.launch('init', '/') def iterate(self): pid = list(self.apps.keys())[self.next_pidi] @@ -87,8 +97,8 @@ class OS: while not self.controllers[-1] in self.apps.keys(): self.controllers.pop() - def launch(self, app, *argv): - self.apps[self.next_new] = get_app(app)(self, self.next_new, app, *argv) + def launch(self, app, wd, *argv): + self.apps[self.next_new] = get_app(app)(self, self.next_new, app, wd, *argv) self.next_new += 1 return self.next_new - 1 diff --git a/lib/console.py b/lib/console.py index 9de2797..c523898 100644 --- a/lib/console.py +++ b/lib/console.py @@ -15,3 +15,7 @@ def read(console, buffer): buffer.append(char) else: return None + + +def write_str(console, string, encoding="utf-8"): + console.write(string.encode(encoding))