stream.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdio.h>
  2. #include "stream.h"
  3. #include "alloc.h"
  4. #include "writer.h"
  5. static Cell* fs_list;
  6. static int num_fs;
  7. static int stream_id;
  8. typedef Cell* (*funcptr2)(Cell* a1, Cell* a2);
  9. // TODO: include fs_list in gc mark
  10. Cell* get_fs_list() {
  11. return fs_list;
  12. }
  13. Cell* fs_open(Cell* path) {
  14. if (!path || path->tag!=TAG_STR) {
  15. printf("[open] error: string required.");
  16. return alloc_nil();
  17. }
  18. Cell* fsl = fs_list;
  19. Cell* fs_cell;
  20. while ((fs_cell = car(fsl))) {
  21. Filesystem* fs = (Filesystem*)fs_cell->next;
  22. //printf("compare %s : %s : %p\n",fs->mount_point->addr, path->addr, strstr(fs->mount_point->addr, path->addr));
  23. if (path->addr && strstr(path->addr, fs->mount_point->addr) == path->addr) {
  24. printf("[open] found matching fs: %s for path: %s\r\n", fs->mount_point->addr, path->addr);
  25. Stream* s = malloc(sizeof(Stream));
  26. s->fs = fs;
  27. s->path = path;
  28. s->id = stream_id;
  29. // TODO: integrate in GC
  30. Cell* stream_cell = alloc_int(stream_id);
  31. stream_cell->tag = TAG_STREAM;
  32. stream_cell->addr = s;
  33. stream_id++;
  34. // open the filesystem
  35. if (s->fs->open_fn && s->fs->open_fn->next) {
  36. printf("[open] open_fn: %p\r\n", s->fs->open_fn->next);
  37. Cell* open_fn = s->fs->open_fn;
  38. ((funcptr2)open_fn->next)(path, NULL);
  39. }
  40. return stream_cell;
  41. }
  42. fsl = cdr(fsl);
  43. }
  44. return alloc_nil();
  45. }
  46. Cell* fs_mmap(Cell* path) {
  47. if (!path || path->tag!=TAG_STR) {
  48. printf("[mmap] error: string required.");
  49. return alloc_nil();
  50. }
  51. Cell* fsl = fs_list;
  52. Cell* fs_cell;
  53. while ((fs_cell = car(fsl))) {
  54. Filesystem* fs = (Filesystem*)fs_cell->next;
  55. if (path->addr && strstr(path->addr, fs->mount_point->addr) == path->addr) {
  56. printf("[mmap] found matching fs: %s for path: %s\r\n", fs->mount_point->addr, path->addr);
  57. if (fs->mmap_fn && fs->mmap_fn->next) {
  58. Cell* mmap_fn = fs->mmap_fn;
  59. return (Cell*)((funcptr2)mmap_fn->next)(path, NULL);
  60. } else {
  61. printf("[mmap] error: fs has no mmap implementation.");
  62. return alloc_nil();
  63. }
  64. }
  65. fsl = cdr(fsl);
  66. }
  67. return alloc_nil();
  68. }
  69. Cell* fs_mount(Cell* path, Cell* handlers) {
  70. if (!path || path->tag!=TAG_STR) {
  71. printf("[mount] error: string required.");
  72. return alloc_nil();
  73. }
  74. Filesystem* fs = malloc(sizeof(Filesystem));
  75. fs->open_fn = car(handlers);
  76. handlers = cdr(handlers);
  77. fs->read_fn = car(handlers);
  78. handlers = cdr(handlers);
  79. fs->write_fn = car(handlers);
  80. handlers = cdr(handlers);
  81. fs->delete_fn = car(handlers);
  82. handlers = cdr(handlers);
  83. fs->mmap_fn = car(handlers);
  84. handlers = cdr(handlers);
  85. fs->mount_point = path;
  86. fs->close_fn = NULL;
  87. Cell* fs_cell = alloc_int(num_fs++);
  88. fs_cell->next = fs;
  89. fs_cell->tag = TAG_FS;
  90. printf("[fs] mounted: %s\r\n",path->addr);
  91. fs_list = alloc_cons(fs_cell, fs_list);
  92. return fs_cell;
  93. }
  94. Cell* wrap_in_lambda(void* cfunc) {
  95. Cell* lbd = alloc_lambda(alloc_nil());
  96. lbd->next = cfunc;
  97. return lbd;
  98. }
  99. // TODO: pass stream (context) to handlers
  100. Cell* stream_read(Cell* stream) {
  101. if (!stream || stream->tag!=TAG_STREAM) {
  102. printf("[fs] error: non-stream passed to recv\r\n");
  103. return alloc_nil();
  104. }
  105. Stream* s = (Stream*)stream->addr;
  106. Cell* read_fn = s->fs->read_fn;
  107. //char debug_buf[256];
  108. //lisp_write(read_fn, debug_buf, 256);
  109. //printf("[stream_read] fn: %s ptr: %p\n",debug_buf,read_fn->next);
  110. return (Cell*)((funcptr2)read_fn->next)(stream,NULL);
  111. }
  112. Cell* stream_write(Cell* stream, Cell* arg) {
  113. if (!stream || stream->tag!=TAG_STREAM) {
  114. printf("[fs] error: non-stream passed to send\r\n");
  115. return alloc_nil();
  116. }
  117. Stream* s = (Stream*)stream->addr;
  118. Cell* write_fn = s->fs->write_fn;
  119. //char debug_buf[256];
  120. //lisp_write(arg, debug_buf, 256);
  121. //printf("[stream_write] fn: %s ptr: %p\n",debug_buf,write_fn->next);
  122. //printf("[stream_write] arg: %s\n",debug_buf);
  123. return (Cell*)((funcptr2)write_fn->next)(stream,arg);
  124. }
  125. void fs_mount_builtin(char* path, void* open_handler, void* read_handler, void* write_handler, void* delete_handler, void* mmap_handler) {
  126. Cell* handlers = alloc_list((Cell*[]){
  127. wrap_in_lambda(open_handler),
  128. wrap_in_lambda(read_handler),
  129. wrap_in_lambda(write_handler),
  130. wrap_in_lambda(delete_handler),
  131. wrap_in_lambda(mmap_handler),
  132. },5);
  133. fs_mount(alloc_string_copy(path), handlers);
  134. }
  135. Cell* filesystems_init() {
  136. fs_list = alloc_nil();
  137. }