summaryrefslogtreecommitdiff
path: root/src/user/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/lib')
-rw-r--r--src/user/lib/fwik/Makefile2
-rw-r--r--src/user/lib/fwik/include/IO/Dir.h24
-rw-r--r--src/user/lib/fwik/include/IO/Node.h5
-rw-r--r--src/user/lib/fwik/io/Dir.cpp37
-rw-r--r--src/user/lib/fwik/io/Node.cpp24
-rw-r--r--src/user/lib/fwik/io/Term.cpp12
-rw-r--r--src/user/lib/libc/include/readline.h1
-rw-r--r--src/user/lib/libc/include/stdio.h1
-rw-r--r--src/user/lib/libc/std/readline.c4
-rw-r--r--src/user/lib/libc/std/stdio.c6
10 files changed, 102 insertions, 14 deletions
diff --git a/src/user/lib/fwik/Makefile b/src/user/lib/fwik/Makefile
index cf8896a..c99b288 100644
--- a/src/user/lib/fwik/Makefile
+++ b/src/user/lib/fwik/Makefile
@@ -1,5 +1,5 @@
Out = _fwik.o
-Obj = String.o io/Node.o io/Term.o io/IOStream.o main.o
+Obj = String.o io/Node.o io/Term.o io/Dir.o io/IOStream.o main.o
ExtObj = $(SrcPath)/user/lib/libc/_libc.o
diff --git a/src/user/lib/fwik/include/IO/Dir.h b/src/user/lib/fwik/include/IO/Dir.h
new file mode 100644
index 0000000..bbfe3ed
--- /dev/null
+++ b/src/user/lib/fwik/include/IO/Dir.h
@@ -0,0 +1,24 @@
+#ifndef DEF_FWIK_IO_DIR_H
+#define DEF_FWIK_IO_DIR_H
+
+#include "Node.h"
+#include <String.h>
+
+class Dir : public Node {
+ void _init();
+
+ public:
+ int pos;
+
+ Dir(FILE f);
+ Dir(const char* file, int mode);
+ Dir(const Node &n);
+ virtual ~Dir();
+
+ String read_ent();
+
+ virtual Dir* as_dir() { return this; }
+};
+
+#endif
+
diff --git a/src/user/lib/fwik/include/IO/Node.h b/src/user/lib/fwik/include/IO/Node.h
index ed8290f..6b5b063 100644
--- a/src/user/lib/fwik/include/IO/Node.h
+++ b/src/user/lib/fwik/include/IO/Node.h
@@ -9,19 +9,22 @@
#include <String.h>
class Term;
+class Dir;
class Node {
public:
FILE fd;
file_info info;
- bool valid;
+ int error; // will be 0 if this is a valid file descriptor
Node(FILE f);
Node(const char* filename, int mode);
+ Node(FILE parent, const char* filename, int mode);
virtual ~Node() {}
void close();
virtual Term* as_term() { return 0; }
+ virtual Dir* as_dir() { return 0; }
};
String path_cat(const String &a, const String &b, bool trailing_slash = true);
diff --git a/src/user/lib/fwik/io/Dir.cpp b/src/user/lib/fwik/io/Dir.cpp
new file mode 100644
index 0000000..cfcc77b
--- /dev/null
+++ b/src/user/lib/fwik/io/Dir.cpp
@@ -0,0 +1,37 @@
+#include <IO/Dir.h>
+
+Dir::Dir(const Node &n) : Node(n) {
+ _init();
+}
+
+Dir::Dir(FILE f) : Node(f) {
+ _init();
+ if (error == E_INVALID_TYPE) libc::close(fd);
+}
+
+Dir::Dir(const char* filename, int mode) : Node(filename, mode) {
+ _init();
+ if (error == E_INVALID_TYPE) libc::close(fd);
+}
+
+void Dir::_init() {
+ if (error < 0) return;
+ pos = 0;
+ if ((info.type & FT_DIR) == 0) {
+ error = E_INVALID_TYPE;
+ }
+}
+
+Dir::~Dir() {
+}
+
+String Dir::read_ent() {
+ char buf[256];
+ int l = libc::read(fd, pos, 256, buf);
+ if (l > 0) {
+ pos++;
+ return String(buf, l);
+ } else {
+ return "";
+ }
+}
diff --git a/src/user/lib/fwik/io/Node.cpp b/src/user/lib/fwik/io/Node.cpp
index daccc18..5585aa8 100644
--- a/src/user/lib/fwik/io/Node.cpp
+++ b/src/user/lib/fwik/io/Node.cpp
@@ -2,24 +2,34 @@
Node::Node(FILE f) {
fd = f;
- int i = libc::statf(f, &info);
- valid = (i == 0);
+ error = libc::statf(f, &info);
}
Node::Node(const char* filename, int mode) {
fd = libc::open(filename, mode);
if (fd < 0) {
- valid = false;
+ if (fd != E_NOT_FOUND) error = libc::stat(filename, &info);
} else {
int i = libc::statf(fd, &info);
- valid = (i == 0);
- if (!valid) libc::close(fd);
+ error = i;
+ if (error < 0) libc::close(fd);
+ }
+}
+
+Node::Node(FILE parent, const char* filename, int mode) {
+ fd = libc::open_relative(parent, filename, mode);
+ if (fd < 0) {
+ if (fd != E_NOT_FOUND) error = libc::stat_relative(parent, filename, &info);
+ } else {
+ int i = libc::statf(fd, &info);
+ error = i;
+ if (error < 0) libc::close(fd);
}
}
void Node::close() {
- if (valid) libc::close(fd);
- valid = false;
+ if (error == 0) libc::close(fd);
+ error = E_INVALID_FD;
}
////////
diff --git a/src/user/lib/fwik/io/Term.cpp b/src/user/lib/fwik/io/Term.cpp
index 1540b9a..f8f686e 100644
--- a/src/user/lib/fwik/io/Term.cpp
+++ b/src/user/lib/fwik/io/Term.cpp
@@ -6,25 +6,33 @@ Term::Term(const Node &n) : Node(n) {
Term::Term(FILE f) : Node(f) {
_init();
+ if (error E_INVALID_TYPE) libc::close(fd);
}
Term::Term(const char* filename, int mode) : Node(filename, mode) {
_init();
+ if (error == E_INVALID_TYPE) libc::close(fd);
}
void Term::_init() {
+ if (error < 0) return;
if (info.type & FT_TERMINAL) {
w = info.size >> 16;
h = info.size & 0xFFFF;
} else {
- valid = false;
+ error = E_INVALID_TYPE;
}
hist.str = 0;
hist.max = 12;
}
Term::~Term() {
- //TODO : free readline history
+ if (hist.str != 0) {
+ for (int i = 0; i < hist.max; i++) {
+ if (hist.str[i] != 0) free(hist.str[i]);
+ }
+ free(hist.str);
+ }
}
void Term::print(const char *s) {
diff --git a/src/user/lib/libc/include/readline.h b/src/user/lib/libc/include/readline.h
index 910ad9a..1ec5baa 100644
--- a/src/user/lib/libc/include/readline.h
+++ b/src/user/lib/libc/include/readline.h
@@ -13,6 +13,7 @@ typedef struct _rdln_hist {
#ifdef __cplusplus
extern "C" { namespace libc {
#endif
+char *readln();
char* freadln(FILE f); // minimal line-reading function. user must free the returned value.
char* freadline(FILE f, readline_history *h);
#ifdef __cplusplus
diff --git a/src/user/lib/libc/include/stdio.h b/src/user/lib/libc/include/stdio.h
index f19ce60..72d355a 100644
--- a/src/user/lib/libc/include/stdio.h
+++ b/src/user/lib/libc/include/stdio.h
@@ -12,7 +12,6 @@ extern FILE term;
void print(const char *s);
void printf(const char *s, ...);
-char *readln();
void fprint(FILE f, const char *s);
void fprintf(FILE f, const char *s, ...);
diff --git a/src/user/lib/libc/std/readline.c b/src/user/lib/libc/std/readline.c
index c2237b6..ab13189 100644
--- a/src/user/lib/libc/std/readline.c
+++ b/src/user/lib/libc/std/readline.c
@@ -35,6 +35,10 @@ char* freadln(FILE f) {
}
}
+char* readln() {
+ return freadln(term);
+}
+
// ** READLINE
diff --git a/src/user/lib/libc/std/stdio.c b/src/user/lib/libc/std/stdio.c
index 23ec989..196c554 100644
--- a/src/user/lib/libc/std/stdio.c
+++ b/src/user/lib/libc/std/stdio.c
@@ -4,14 +4,16 @@
#include <readline.h>
FILE term = 0;
-void print(const char *s) { fprint(term, s); }
+void print(const char *s) {
+ fprint(term, s);
+}
+
void printf(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(term, format, ap);
va_end(ap);
}
-char* readln() { return freadln(term); }
void fprint(FILE f, const char *s) {
write(f, 0, strlen(s), s);