stream.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. Cell* fsl = fs_list;
  15. Cell* fs_cell;
  16. if (!path || path->tag!=TAG_STR) {
  17. printf("[open] error: string required.");
  18. return alloc_nil();
  19. }
  20. while ((fs_cell = car(fsl))) {
  21. Filesystem* fs = (Filesystem*)fs_cell->dr.next;
  22. //printf("compare %s : %s : %p\n",fs->mount_point->ar.addr, path->ar.addr, strstr(fs->mount_point->ar.addr, path->ar.addr));
  23. if (path->ar.addr && strstr(path->ar.addr, fs->mount_point->ar.addr) == path->ar.addr) {
  24. Stream* s = malloc(sizeof(Stream));
  25. Cell* stream_cell;
  26. printf("[open] via %s: %s\r\n", (char*)fs->mount_point->ar.addr, (char*)path->ar.addr);
  27. s->fs = fs;
  28. s->path = path;
  29. s->id = stream_id;
  30. // TODO: integrate in GC
  31. stream_cell = alloc_int(stream_id);
  32. stream_cell->tag = TAG_STREAM;
  33. stream_cell->ar.addr = s;
  34. stream_id++;
  35. // open the filesystem
  36. if (s->fs->open_fn && s->fs->open_fn->dr.next) {
  37. Cell* open_fn = s->fs->open_fn;
  38. ((funcptr2)open_fn->dr.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. Cell* fsl = fs_list;
  48. Cell* fs_cell;
  49. if (!path || path->tag!=TAG_STR) {
  50. printf("[mmap] error: string required.");
  51. return alloc_nil();
  52. }
  53. while ((fs_cell = car(fsl))) {
  54. Filesystem* fs = (Filesystem*)fs_cell->dr.next;
  55. if (path->ar.addr && strstr(path->ar.addr, fs->mount_point->ar.addr) == path->ar.addr) {
  56. printf("[mmap] found matching fs: %s for path: %s\r\n", (char*)fs->mount_point->ar.addr, (char*)path->ar.addr);
  57. if (fs->mmap_fn && fs->mmap_fn->dr.next) {
  58. Cell* mmap_fn = fs->mmap_fn;
  59. return (Cell*)((funcptr2)mmap_fn->dr.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. Filesystem* fs;
  71. Cell* fs_cell;
  72. if (!path || path->tag!=TAG_STR) {
  73. printf("[mount] error: string required.");
  74. return alloc_nil();
  75. }
  76. fs = malloc(sizeof(Filesystem));
  77. fs->open_fn = car(handlers);
  78. handlers = cdr(handlers);
  79. fs->read_fn = car(handlers);
  80. handlers = cdr(handlers);
  81. fs->write_fn = car(handlers);
  82. handlers = cdr(handlers);
  83. fs->delete_fn = car(handlers);
  84. handlers = cdr(handlers);
  85. fs->mmap_fn = car(handlers);
  86. handlers = cdr(handlers);
  87. fs->mount_point = path;
  88. fs->close_fn = NULL;
  89. fs_cell = alloc_int(num_fs++);
  90. fs_cell->dr.next = fs;
  91. fs_cell->tag = TAG_FS;
  92. printf("[fs] mounted: %s\r\n",(char*)path->ar.addr);
  93. fs_list = alloc_cons(fs_cell, fs_list);
  94. return fs_cell;
  95. }
  96. Cell* wrap_in_lambda(void* cfunc) {
  97. Cell* lbd = alloc_lambda(alloc_nil());
  98. lbd->dr.next = cfunc;
  99. return lbd;
  100. }
  101. // TODO: pass stream (context) to handlers
  102. Cell* stream_read(Cell* stream) {
  103. Stream* s;
  104. Cell* read_fn;
  105. if (!stream || stream->tag!=TAG_STREAM) {
  106. printf("[fs] error: non-stream passed to recv\r\n");
  107. return alloc_nil();
  108. }
  109. s = (Stream*)stream->ar.addr;
  110. read_fn = s->fs->read_fn;
  111. //char debug_buf[256];
  112. //lisp_write(read_fn, debug_buf, 256);
  113. //printf("[stream_read] fn: %s ptr: %p\n",debug_buf,read_fn->dr.next);
  114. return (Cell*)((funcptr2)read_fn->dr.next)(stream,NULL);
  115. }
  116. Cell* stream_write(Cell* stream, Cell* arg) {
  117. Stream* s;
  118. Cell* write_fn;
  119. if (!stream || stream->tag!=TAG_STREAM) {
  120. printf("[fs] error: non-stream passed to send\r\n");
  121. return alloc_nil();
  122. }
  123. s = (Stream*)stream->ar.addr;
  124. write_fn = s->fs->write_fn;
  125. //char debug_buf[256];
  126. //lisp_write(arg, debug_buf, 256);
  127. //printf("[stream_write] fn: %s ptr: %p\n",debug_buf,write_fn->dr.next);
  128. //printf("[stream_write] arg: %s\n",debug_buf);
  129. return (Cell*)((funcptr2)write_fn->dr.next)(stream,arg);
  130. }
  131. void fs_mount_builtin(char* path, void* open_handler, void* read_handler, void* write_handler, void* delete_handler, void* mmap_handler) {
  132. Cell* items[] = {
  133. wrap_in_lambda(open_handler),
  134. wrap_in_lambda(read_handler),
  135. wrap_in_lambda(write_handler),
  136. wrap_in_lambda(delete_handler),
  137. wrap_in_lambda(mmap_handler),
  138. };
  139. Cell* handlers = alloc_list(items,5);
  140. fs_mount(alloc_string_copy(path), handlers);
  141. }
  142. Cell* filesystems_init() {
  143. fs_list = alloc_nil();
  144. return fs_list;
  145. }