diff options
Diffstat (limited to 'fs/fd.c')
| -rw-r--r-- | fs/fd.c | 101 |
1 files changed, 88 insertions, 13 deletions
@@ -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; +} + |
