From b639b99b3e8f4cf77560d8d473b13d992ac8eb10 Mon Sep 17 00:00:00 2001 From: Alexis211 Date: Sat, 24 Oct 2009 18:24:46 +0200 Subject: More work on userland syscalls : Files are implemented. TextFile now is a common (= kernel and userland) library. --- Source/Library/Common/TextFile.class.cpp | 30 +++++++++++ Source/Library/Common/TextFile.class.h | 31 +++++++++++ Source/Library/Interface/File.iface.h | 33 ++++++++++++ Source/Library/Interface/Process.iface.h | 4 +- Source/Library/Interface/Sys.iface.h | 3 ++ Source/Library/Link.ld | 1 + Source/Library/Makefile | 5 +- Source/Library/Userland/Binding/FSNode.class.h | 1 + Source/Library/Userland/Binding/File.class.h | 68 +++++++++++++++++++++++++ Source/Library/Userland/Binding/Process.class.h | 7 ++- Source/Library/Userland/Binding/Sys.ns.cpp | 15 ------ Source/Library/Userland/Binding/Sys.ns.h | 26 +++++++++- Source/Library/Userland/Start.cpp | 8 ++- 13 files changed, 208 insertions(+), 24 deletions(-) create mode 100644 Source/Library/Common/TextFile.class.cpp create mode 100644 Source/Library/Common/TextFile.class.h create mode 100644 Source/Library/Interface/File.iface.h create mode 100644 Source/Library/Userland/Binding/File.class.h delete mode 100644 Source/Library/Userland/Binding/Sys.ns.cpp (limited to 'Source/Library') diff --git a/Source/Library/Common/TextFile.class.cpp b/Source/Library/Common/TextFile.class.cpp new file mode 100644 index 0000000..040e2d7 --- /dev/null +++ b/Source/Library/Common/TextFile.class.cpp @@ -0,0 +1,30 @@ +#include "TextFile.class.h" + +bool TextFile::write(String str, bool addnl) { + ByteArray a(str, m_encoding); + if (addnl) a += (u8int)'\n'; + return File::write(a); +} + +String TextFile::readLine(char separator) { + const u32int bufflen = 512; + String ret; + ByteArray temp; + while (1) { + temp.resize(bufflen); + u32int r = read(temp); + u32int l = r; + for (u32int i = 0; i < r; i++) { + if (temp[i] == separator) { + l = i; + temp.resize(i); + break; + } + } + ret += temp.toString(m_encoding); + if (l != r or r != bufflen) { + if (l != r) seek((r - l) - 1, SM_BACKWARD); + return ret; + } + } +} diff --git a/Source/Library/Common/TextFile.class.h b/Source/Library/Common/TextFile.class.h new file mode 100644 index 0000000..3749b6a --- /dev/null +++ b/Source/Library/Common/TextFile.class.h @@ -0,0 +1,31 @@ +#ifndef DEF_TEXTFILE_CLASS_H +#define DEF_TEXTFILE_CLASS_H + +#ifdef THIS_IS_MELON_KERNEL +#include +#else +#include +#endif + +class TextFile : public File { + private: + u8int m_encoding; + + public: +#ifdef THIS_IS_MELON_KERNEL + TextFile(u8int encoding = UE_UTF8) : File() { m_encoding = encoding; } + TextFile(String filename, u8int mode = FM_READ, FSNode* start = 0, u8int encoding = UE_UTF8) + : File(filename, mode, start) { m_encoding = encoding; } +#else + TextFile(u32int id, u8int encoding = UE_UTF8) : File(id) { m_encoding = encoding; } + TextFile(String filename, u8int mode = FM_READ, FSNode start = FSNode(0), u8int encoding = UE_UTF8) + : File(filename, mode, start) { m_encoding = encoding; } +#endif + ~TextFile() {} + + void setEncoding(u8int encoding = UE_UTF8) { m_encoding = encoding; } + bool write(String str, bool addnl = false); // Addnl = wether or not to add \n at end + String readLine(char separator = '\n'); +}; + +#endif diff --git a/Source/Library/Interface/File.iface.h b/Source/Library/Interface/File.iface.h new file mode 100644 index 0000000..e1030b8 --- /dev/null +++ b/Source/Library/Interface/File.iface.h @@ -0,0 +1,33 @@ +#ifndef DEF_FILE_IFACE_H +#define DEF_FILE_IFACE_H + +enum { + FM_READ = 0, //Open for read, put cursor at beginning + FM_TRUNCATE = 1, //Open for write, truncating file before + FM_APPEND = 2, //Open for write, put cursor at end + FM_REPLACE = 3 //Open for write, put cursor at beginning +}; + +enum { + SM_FORWARD = 0, //Seek from actual position + SM_BACKWARD = 1, //Seek from actual position, backward + SM_BEGINNING = 2, //Seek from start of file + SM_END = 3, //Seek from end of file +}; + +#define FLIF_OBJTYPE 0x13 + +#define FLIF_SOPEN 0x01 + +#define FLIF_CLOSE 0x05 +#define FLIF_VALID 0x06 + +#define FLIF_READ 0x0A +#define FLIF_WRITE 0x0B + +#define FLIF_SEEK 0x10 +#define FLIF_POSITION 0x11 +#define FLIF_LENGTH 0x12 +#define FLIF_EOF 0x13 + +#endif diff --git a/Source/Library/Interface/Process.iface.h b/Source/Library/Interface/Process.iface.h index b79639d..52543aa 100644 --- a/Source/Library/Interface/Process.iface.h +++ b/Source/Library/Interface/Process.iface.h @@ -11,6 +11,8 @@ #define PRIF_FREEPAGE 0x03 #define PRIF_GETPID 0x04 #define PRIF_GETPPID 0x05 -#define PRIF_GETCMDLINE 0x06 + +#define PRIF_ARGC 0x10 +#define PRIF_ARGV 0x11 #endif diff --git a/Source/Library/Interface/Sys.iface.h b/Source/Library/Interface/Sys.iface.h index c734f52..bebab41 100644 --- a/Source/Library/Interface/Sys.iface.h +++ b/Source/Library/Interface/Sys.iface.h @@ -5,5 +5,8 @@ #define SYIF_HALT 0x1 #define SYIF_REBOOT 0x2 +#define SYIF_UPTIME 0x3 +#define SYIF_TOTALRAM 0x4 +#define SYIF_FREERAM 0x5 #endif diff --git a/Source/Library/Link.ld b/Source/Library/Link.ld index f06f568..68591fd 100644 --- a/Source/Library/Link.ld +++ b/Source/Library/Link.ld @@ -1,4 +1,5 @@ ENTRY (start) +INPUT (Melon.o) SECTIONS{ . = 0x10000000; diff --git a/Source/Library/Makefile b/Source/Library/Makefile index 7091116..5a7a039 100644 --- a/Source/Library/Makefile +++ b/Source/Library/Makefile @@ -1,7 +1,7 @@ .PHONY: clean, mrproper CXX = g++ -CXXFLAGS = -nostartfiles -nostdlib -fno-exceptions -fno-rtti -I Common -I Userland -I Interface -D THIS_IS_MELON_USERLAND +CXXFLAGS = -nostartfiles -nostdlib -ffreestanding -fno-exceptions -fno-rtti -I Common -I Userland -I Interface -D THIS_IS_MELON_USERLAND ASM = nasm ASMFLAGS = -f elf @@ -16,7 +16,8 @@ Objects = Common/WChar.class.uo \ Common/Heap.class.uo \ Common/Heap-index.class.uo \ Common/String.class.uo \ - Userland/Binding/Sys.ns.uo \ + Common/TextFile.class.uo \ + Common/ByteArray.class.uo \ Userland/Syscall/Syscall.wtf.uo \ Userland/Syscall/RessourceCaller.class.uo \ Userland/Start.uo diff --git a/Source/Library/Userland/Binding/FSNode.class.h b/Source/Library/Userland/Binding/FSNode.class.h index 6a860ea..a7adbc0 100644 --- a/Source/Library/Userland/Binding/FSNode.class.h +++ b/Source/Library/Userland/Binding/FSNode.class.h @@ -2,6 +2,7 @@ #define DEF_FSNODE_CLASS_H #include +#include #include class FSNode : public RessourceCaller { diff --git a/Source/Library/Userland/Binding/File.class.h b/Source/Library/Userland/Binding/File.class.h new file mode 100644 index 0000000..4d1ac16 --- /dev/null +++ b/Source/Library/Userland/Binding/File.class.h @@ -0,0 +1,68 @@ +#ifndef DEF_FILE_CLASS_H +#define DEF_FILE_CLASS_H + +#include +#include +#include +#include +#include + +class File : public RessourceCaller { + public: + File(String name, u8int mode, FSNode start = FSNode(0)) : + RessourceCaller(sCall(FLIF_OBJTYPE, FLIF_SOPEN, (u32int)&name, mode, start.resId()), FLIF_OBJTYPE) { + } + File(u32int id) : RessourceCaller(id, FLIF_OBJTYPE) {} + + void close() { + doCall(FLIF_CLOSE); + } + bool validOpened() { + return (doCall(FLIF_VALID) != 0); + } + u32int read(u32int max_length, u8int *ptr) { + return (doCall(FLIF_READ, max_length, (u32int)ptr)); + } + bool write(u32int length, u8int* ptr) { + return (doCall(FLIF_WRITE, length, (u32int)ptr) != 0); + } + u32int read(ByteArray &data) { + if (!valid()) { + data.clear(); + return 0; + } + u32int l = read(data.size(), (u8int*)data); + if (l != data.size()) data.resize(l); + return l; + } + bool write(ByteArray &data) { + if (!valid()) return false; + return write(data.size(), (u8int*)data); + } + + template bool read(T* elem) { + return (read(sizeof(T), (u8int*)elem) == sizeof(T)); + } + template bool write(T* elem) { + return write(sizeof(T), (u8int*)elem); + } + + bool seek(u64int count, u8int mode) { + union { + u64int x; + u32int a[2]; + } wat = {count}; + return (doCall(FLIF_SEEK, wat.a[0], wat.a[1], mode) != 0); + } + u64int position() { + return *((u64int*)doCall(FLIF_POSITION)); + } + u64int length() { + return *((u64int*)doCall(FLIF_LENGTH)); + } + bool eof() { + return (doCall(FLIF_EOF) != 0); + } +}; + +#endif diff --git a/Source/Library/Userland/Binding/Process.class.h b/Source/Library/Userland/Binding/Process.class.h index ddca6be..9687ea9 100644 --- a/Source/Library/Userland/Binding/Process.class.h +++ b/Source/Library/Userland/Binding/Process.class.h @@ -29,8 +29,11 @@ class Process : public RessourceCaller { u32int getPpid() { return doCall(PRIF_GETPPID); } - String getCmdline() { - return String::unserialize(doCall(PRIF_GETCMDLINE)); + u32int argc() { + return doCall(PRIF_ARGC); + } + String argv(u32int idx) { + return String::unserialize(doCall(PRIF_ARGV, idx)); } }; diff --git a/Source/Library/Userland/Binding/Sys.ns.cpp b/Source/Library/Userland/Binding/Sys.ns.cpp deleted file mode 100644 index 7083e56..0000000 --- a/Source/Library/Userland/Binding/Sys.ns.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "Sys.ns.h" -#include - -namespace Sys { - -void halt() { - RessourceCaller::sCall(SYIF_IFID, SYIF_HALT); -} - -void reboot() { - RessourceCaller::sCall(SYIF_IFID, SYIF_REBOOT); -} - -} diff --git a/Source/Library/Userland/Binding/Sys.ns.h b/Source/Library/Userland/Binding/Sys.ns.h index 3a8426b..eb09504 100644 --- a/Source/Library/Userland/Binding/Sys.ns.h +++ b/Source/Library/Userland/Binding/Sys.ns.h @@ -1,9 +1,31 @@ #ifndef DEF_SYS_NS_H #define DEF_SYS_NS_H +#include +#include + namespace Sys { - void halt(); - void reboot(); + +inline void halt() { + RessourceCaller::sCall(SYIF_IFID, SYIF_HALT); +} + +inline void reboot() { + RessourceCaller::sCall(SYIF_IFID, SYIF_REBOOT); +} + +inline u32int uptime() { //Returns uptime in seconds + return RessourceCaller::sCall(SYIF_IFID, SYIF_UPTIME); +} + +inline u32int totalRam() { //Returns total amount of RAM in Ko + return RessourceCaller::sCall(SYIF_IFID, SYIF_TOTALRAM); +} + +inline u32int freeRam() { //Returns free amount of RAM in Ko + return RessourceCaller::sCall(SYIF_IFID, SYIF_FREERAM); +} + } #endif diff --git a/Source/Library/Userland/Start.cpp b/Source/Library/Userland/Start.cpp index dee7da6..be4f81e 100644 --- a/Source/Library/Userland/Start.cpp +++ b/Source/Library/Userland/Start.cpp @@ -13,7 +13,7 @@ Heap heap; VirtualTerminal invt(0), outvt(0); -int main(); +int main(const Vector& args); extern "C" void start() { //Call static constructors @@ -24,7 +24,11 @@ extern "C" void start() { heap.create(0x40000000, 0x00100000, 0x00004000); //Initially create a 1M heap with 16ko index invt = VirtualTerminal::getIn(); outvt = VirtualTerminal::getOut(); - u32int r = main(); + u32int argc = Process::get().argc(); + Vector args(argc); + for (u32int i = 0; i < argc; i++) args[i] = Process::get().argv(i); + + u32int r = main(args); //Call static destructors for(u32int * call = &start_dtors; call < &end_dtors; call++) { -- cgit v1.2.3