summaryrefslogtreecommitdiff
path: root/fs/fd.c
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-09-18 03:03:28 +0200
committerMiguel <m.i@gmx.at>2018-09-18 03:03:28 +0200
commit2d91384197847a7e8fe2c3f548918a8277d3086d (patch)
tree7c93404e290a0ffbdaf9a8a94766d7bd0fd6e4f2 /fs/fd.c
parent06e6e427c76bdb88a7f72dd04411d95a4bda3270 (diff)
sysfs, errno, improve foolshell, etc
Diffstat (limited to 'fs/fd.c')
-rw-r--r--fs/fd.c101
1 files changed, 88 insertions, 13 deletions
diff --git a/fs/fd.c b/fs/fd.c
index ed8a3b1..43ffb0d 100644
--- a/fs/fd.c
+++ b/fs/fd.c
@@ -4,6 +4,9 @@
#include "kmalloc.h"
#include "ext2.h"
#include "log.h"
+#include "lib/string/string.h"
+#include "lib/printf/printf.h"
+#include "ringbuffer.h"
bool fd_write(fd* f,uint8_t c)
{
@@ -20,21 +23,14 @@ bool fd_has(fd* f)
return f->has(f->data);
}
-bool fd_close(fd* f)
+bool fd_eof(fd* f)
{
- return f->close(f->data);
+ return f->eof(f->data);
}
-fd fd_from_fifo(fifo* fif)
+bool fd_close(fd* f)
{
- fd f;
- f.type=FD_TYPE_FIFO_BUFFERED;
- f.data=fif;
- f.read=fifo_get;
- f.write=fifo_put;
-// f.close=fd_close;
- f.has=fifo_has;
- return f;
+ return f->close(f->data);
}
//----
@@ -62,17 +58,75 @@ uint8_t ext2_read(uint32_t* data)
bool ext2_has(uint32_t* data)
{
+ return 1;
+}
+
+bool ext2_eof(uint32_t* data)
+{
uint32_t save=data[1];
uint32_t c=ext2_read_inode(VMEM_EXT2_RAMIMAGE,data[0],&c,&data[1],1);
data[1]=save;
- return (c>0);
-
+ return (c==0);
}
+
void ext2_close(void* x)
{
kbfree(x);
}
+uint8_t sysfs_get(uint32_t *data)
+{
+ return ringbuffer_get(data);
+}
+
+bool sysfs_has(uint32_t *data)
+{
+ return true;
+}
+
+bool sysfs_eof(uint32_t *data)
+{
+ return !ringbuffer_has(data);
+}
+
+void sysfs_close(uint32_t *data)
+{
+ ringbuffer *r=data;
+ ringbuffer_free(data); // free ringbuffer buffer
+ kbfree(data); // free memory holding ringbuffer information
+}
+
+void get_sysfs_data(ringbuffer *r,char *format_string, ...)
+{
+ char buf[256];
+ va_list va;
+ va_start(va,format_string);
+ tfp_vsprintf(buf,format_string,va);
+ va_end(va);
+
+ for(int i=0;i<strlen(buf);i++)
+ ringbuffer_put(r,buf[i]);
+
+ ringbuffer_put(r,'\n');
+}
+
+bool fifo_eof(uint32_t* data)
+{
+ return false;
+}
+fd fd_from_fifo(fifo* fif)
+{
+ fd f;
+ f.type=FD_TYPE_FIFO_BUFFERED;
+ f.data=fif;
+ f.read=fifo_get;
+ f.write=fifo_put;
+ f.eof=fifo_eof;
+// f.close=fd_close; //TODO.. etc also otehr and cleanups
+ f.has=fifo_has;
+ return f;
+}
+
fd fd_from_path(char* path)
{
fd f;
@@ -81,6 +135,27 @@ fd fd_from_path(char* path)
f.read=ext2_read;
f.write=ext2_write;
f.close=ext2_close;
+ f.eof=ext2_eof;
f.has=ext2_has;
return f;
}
+
+fd fd_from_sysfs(void(*g)(ringbuffer *r,void (*f)(ringbuffer *r,char *fmt, ...)))
+{
+ fd f;
+ f.type=FD_TYPE_SYSFS;
+ f.data=kballoc(1);
+ ringbuffer r=ringbuffer_init(1);
+
+ g(&r,get_sysfs_data);
+ memcpy(f.data,&r,sizeof(ringbuffer));
+
+ f.read=sysfs_get;
+ f.write=0;
+ f.close=sysfs_close;
+ f.has=sysfs_has;
+ f.eof=sysfs_eof;
+
+ return f;
+}
+