1
0

stream.c 4.6 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] found matching fs: %s for path: %s\r\n", fs->mount_point->ar.addr, 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. printf("[open] open_fn: %p\r\n", s->fs->open_fn->dr.next);
  39. ((funcptr2)open_fn->dr.next)(path, NULL);
  40. }
  41. return stream_cell;
  42. }
  43. fsl = cdr(fsl);
  44. }
  45. return alloc_nil();
  46. }
  47. Cell* fs_mmap(Cell* path) {
  48. Cell* fsl = fs_list;
  49. Cell* fs_cell;
  50. if (!path || path->tag!=TAG_STR) {
  51. printf("[mmap] error: string required.");
  52. return alloc_nil();
  53. }
  54. while ((fs_cell = car(fsl))) {
  55. Filesystem* fs = (Filesystem*)fs_cell->dr.next;
  56. if (path->ar.addr && strstr(path->ar.addr, fs->mount_point->ar.addr) == path->ar.addr) {
  57. printf("[mmap] found matching fs: %s for path: %s\r\n", fs->mount_point->ar.addr, path->ar.addr);
  58. if (fs->mmap_fn && fs->mmap_fn->dr.next) {
  59. Cell* mmap_fn = fs->mmap_fn;
  60. return (Cell*)((funcptr2)mmap_fn->dr.next)(path, NULL);
  61. } else {
  62. printf("[mmap] error: fs has no mmap implementation.");
  63. return alloc_nil();
  64. }
  65. }
  66. fsl = cdr(fsl);
  67. }
  68. return alloc_nil();
  69. }
  70. Cell* fs_mount(Cell* path, Cell* handlers) {
  71. Filesystem* fs;
  72. Cell* fs_cell;
  73. if (!path || path->tag!=TAG_STR) {
  74. printf("[mount] error: string required.");
  75. return alloc_nil();
  76. }
  77. fs = malloc(sizeof(Filesystem));
  78. fs->open_fn = car(handlers);
  79. handlers = cdr(handlers);
  80. fs->read_fn = car(handlers);
  81. handlers = cdr(handlers);
  82. fs->write_fn = car(handlers);
  83. handlers = cdr(handlers);
  84. fs->delete_fn = car(handlers);
  85. handlers = cdr(handlers);
  86. fs->mmap_fn = car(handlers);
  87. handlers = cdr(handlers);
  88. fs->mount_point = path;
  89. fs->close_fn = NULL;
  90. fs_cell = alloc_int(num_fs++);
  91. fs_cell->dr.next = fs;
  92. fs_cell->tag = TAG_FS;
  93. printf("[fs] mounted: %s\r\n",path->ar.addr);
  94. fs_list = alloc_cons(fs_cell, fs_list);
  95. return fs_cell;
  96. }
  97. Cell* wrap_in_lambda(void* cfunc) {
  98. Cell* lbd = alloc_lambda(alloc_nil());
  99. lbd->dr.next = cfunc;
  100. return lbd;
  101. }
  102. // TODO: pass stream (context) to handlers
  103. Cell* stream_read(Cell* stream) {
  104. Stream* s;
  105. Cell* read_fn;
  106. if (!stream || stream->tag!=TAG_STREAM) {
  107. printf("[fs] error: non-stream passed to recv\r\n");
  108. return alloc_nil();
  109. }
  110. s = (Stream*)stream->ar.addr;
  111. read_fn = s->fs->read_fn;
  112. //char debug_buf[256];
  113. //lisp_write(read_fn, debug_buf, 256);
  114. //printf("[stream_read] fn: %s ptr: %p\n",debug_buf,read_fn->dr.next);
  115. return (Cell*)((funcptr2)read_fn->dr.next)(stream,NULL);
  116. }
  117. Cell* stream_write(Cell* stream, Cell* arg) {
  118. Stream* s;
  119. Cell* write_fn;
  120. if (!stream || stream->tag!=TAG_STREAM) {
  121. printf("[fs] error: non-stream passed to send\r\n");
  122. return alloc_nil();
  123. }
  124. s = (Stream*)stream->ar.addr;
  125. write_fn = s->fs->write_fn;
  126. //char debug_buf[256];
  127. //lisp_write(arg, debug_buf, 256);
  128. //printf("[stream_write] fn: %s ptr: %p\n",debug_buf,write_fn->dr.next);
  129. //printf("[stream_write] arg: %s\n",debug_buf);
  130. return (Cell*)((funcptr2)write_fn->dr.next)(stream,arg);
  131. }
  132. void fs_mount_builtin(char* path, void* open_handler, void* read_handler, void* write_handler, void* delete_handler, void* mmap_handler) {
  133. Cell* items[] = {
  134. wrap_in_lambda(open_handler),
  135. wrap_in_lambda(read_handler),
  136. wrap_in_lambda(write_handler),
  137. wrap_in_lambda(delete_handler),
  138. wrap_in_lambda(mmap_handler),
  139. };
  140. Cell* handlers = alloc_list(items,5);
  141. fs_mount(alloc_string_copy(path), handlers);
  142. }
  143. Cell* filesystems_init() {
  144. fs_list = alloc_nil();
  145. }