pipe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. ** The pipe algo is somewhat similar to the luaL_Buffer algo, except that it uses
  3. ** table to store the LUAL_BUFFERSIZE byte array chunks instead of the stack.
  4. ** Writing is always to the last UD in the table and overflow pushes a new UD to
  5. ** the end of the table. Reading is always from the first UD in the table and
  6. ** underrun removes the first UD to shift a new one into slot 2. (Slot 1 of the
  7. ** table is reserved for the pipe reader function with 0 denoting no reader.)
  8. **
  9. ** Reads and writes may span multiple UD buffers and if the read spans multiple UDs
  10. ** then the parts are collected as strings on the Lua stack and then concatenated
  11. ** with a lua_concat().
  12. **
  13. ** Note that pipe tables also support the undocumented length and tostring
  14. ** operators for debugging puposes, so if p is a pipe then #p[i] gives the
  15. ** effective length of pipe slot i and printing p[i] gives its contents.
  16. **
  17. ** The pipe library also supports the automatic scheduling of a reader task. This
  18. ** is declared by including a Lua CB function and an optional prioirty for it to
  19. ** execute at in the pipe.create() call. The reader task may or may not empty the
  20. ** FIFO (and there is also nothing to stop the task also writing to the FIFO. The
  21. ** reader automatically reschedules itself if the pipe contains unread content.
  22. **
  23. ** The reader tasks may be interleaved with other tasks that write to the pipe and
  24. ** others that don't. Any task writing to the pipe will also trigger the posting
  25. ** of a read task if one is not already pending. In this way at most only one
  26. ** pending reader task is pending, and this prevents overrun of the task queueing
  27. ** system.
  28. **
  29. ** Implementation Notes:
  30. **
  31. ** - The Pipe slot 1 is used to store the Lua CB function reference of the reader
  32. ** task. Note that is actually an auxiliary wrapper around the supplied Lua CB
  33. ** function, and this wrapper also uses upvals to store internal pipe state.
  34. ** The remaining slots are the Userdata buffer chunks.
  35. **
  36. ** - This internal state needs to be shared with the pipe_write function, but a
  37. ** limitation of Lua 5.1 is that C functions cannot share upvals; to avoid this
  38. ** constraint, this function is also denormalised to act as the pipe_write
  39. ** function: if Arg1 is the pipe then its a pipe:write() otherwise its a
  40. ** CB wrapper.
  41. **
  42. ** Also note that the pipe module is used by the Lua VM and therefore the create
  43. ** read, and unread methods are exposed as directly callable C functions. (Write
  44. ** is available throogh pipe[1].)
  45. **
  46. ** Read the docs/modules/pipe.md documentation for a functional description.
  47. */
  48. #include "module.h"
  49. #include "lauxlib.h"
  50. #include <string.h>
  51. #include "platform.h"
  52. #define INVALID_LEN ((unsigned)-1)
  53. #define LUA_USE_MODULES_PIPE
  54. typedef struct buffer {
  55. int start, end;
  56. char buf[LUAL_BUFFERSIZE];
  57. } buffer_t;
  58. #define AT_TAIL 0x00
  59. #define AT_HEAD 0x01
  60. #define WRITING 0x02
  61. static buffer_t *checkPipeUD (lua_State *L, int ndx);
  62. static buffer_t *newPipeUD(lua_State *L, int ndx, int n);
  63. static int pipe_write_aux(lua_State *L);
  64. LROT_TABLE(pipe_meta);
  65. /* Validation and utility functions */
  66. // [-0, +0, v]
  67. static buffer_t *checkPipeTable (lua_State *L, int tbl, int flags) {
  68. int m = lua_gettop(L), n = lua_objlen(L, tbl);
  69. if (lua_istable(L, tbl) && lua_getmetatable(L, tbl)) {
  70. lua_pushrotable(L, LROT_TABLEREF(pipe_meta));/* push comparison metatable */
  71. if (lua_rawequal(L, -1, -2)) { /* check these match */
  72. buffer_t *ud;
  73. if (n == 1) {
  74. ud = (flags & WRITING) ? newPipeUD(L, tbl, 2) : NULL;
  75. } else {
  76. int i = flags & AT_HEAD ? 2 : n; /* point to head or tail of T */
  77. lua_rawgeti(L, tbl, i); /* and fetch UD */
  78. ud = checkPipeUD(L, -1);
  79. }
  80. lua_settop(L, m);
  81. return ud; /* and return ptr to buffer_t rec */
  82. }
  83. }
  84. luaL_argerror(L, tbl, "pipe table");
  85. return NULL; /* NORETURN avoid compiler error */
  86. }
  87. static buffer_t *checkPipeUD (lua_State *L, int ndx) { // [-0, +0, v]
  88. luaL_checktype(L, ndx, LUA_TUSERDATA); /* NORETURN on error */
  89. buffer_t *ud = lua_touserdata(L, ndx);
  90. if (ud && lua_getmetatable(L, ndx)) { /* Get UD and its metatable? */
  91. lua_pushrotable(L, LROT_TABLEREF(pipe_meta)); /* push correct metatable */
  92. if (lua_rawequal(L, -1, -2)) { /* Do these match? */
  93. lua_pop(L, 2); /* remove both metatables */
  94. return ud; /* and return ptr to buffer_t rec */
  95. }
  96. }
  97. }
  98. static buffer_t *newPipeUD(lua_State *L, int ndx, int n) { // [-0,+0,-]
  99. buffer_t *ud = (buffer_t *) lua_newuserdata(L, sizeof(buffer_t));
  100. lua_pushrotable(L, LROT_TABLEREF(pipe_meta)); /* push the metatable */
  101. lua_setmetatable(L, -2); /* set UD's metabtable to pipe_meta */
  102. ud->start = ud->end = 0;
  103. lua_rawseti(L, ndx, n); /* T[#T+1] = new UD */
  104. return ud; /* ud points to new T[#T] */
  105. }
  106. #define CHAR_DELIM -1
  107. #define CHAR_DELIM_KEEP -2
  108. static char getsize_delim (lua_State *L, int ndx, int *len) { // [-0, +0, v]
  109. char delim;
  110. int n;
  111. if( lua_type( L, ndx ) == LUA_TNUMBER ) {
  112. n = luaL_checkinteger( L, ndx );
  113. luaL_argcheck(L, n > 0, ndx, "invalid chunk size");
  114. delim = '\0';
  115. } else if (lua_isnil(L, ndx)) {
  116. n = CHAR_DELIM;
  117. delim = '\n';
  118. } else {
  119. size_t ldelim;
  120. const char *s = lua_tolstring( L, 2, &ldelim);
  121. n = ldelim == 2 && s[1] == '+' ? CHAR_DELIM_KEEP : CHAR_DELIM ;
  122. luaL_argcheck(L, ldelim + n == 0, ndx, "invalid delimiter");
  123. delim = s[0];
  124. }
  125. if (len)
  126. *len = n;
  127. return delim;
  128. }
  129. /*
  130. ** Read CB Initiator AND pipe_write. If arg1 == the pipe, then this is a pipe
  131. ** write(); otherwise it is the Lua CB wapper for the task post. This botch allows
  132. ** these two functions to share Upvals within the Lua 5.1 VM:
  133. */
  134. #define UVpipe lua_upvalueindex(1) // The pipe table object
  135. #define UVfunc lua_upvalueindex(2) // The CB's Lua function
  136. #define UVprio lua_upvalueindex(3) // The task priority
  137. #define UVstate lua_upvalueindex(4) // Pipe state;
  138. #define CB_NOT_USED 0
  139. #define CB_ACTIVE 1
  140. #define CB_WRITE_UPDATED 2
  141. #define CB_QUIESCENT 4
  142. /*
  143. ** Note that nothing precludes the Lua CB function from itself writing to the
  144. ** pipe and in this case this routine will call itself recursively.
  145. **
  146. ** The Lua CB itself takes the pipe object as a parameter and returns an optional
  147. ** boolean to force or to suppress automatic retasking if needed. If omitted,
  148. ** then the default is to repost if the pipe is not empty, otherwise the task
  149. ** chain is left to lapse.
  150. */
  151. static int pipe_write_and_read_poster (lua_State *L) {
  152. int state = lua_tointeger(L, UVstate);
  153. if (lua_rawequal(L, 1, UVpipe)) {
  154. /* arg1 == the pipe, so this was invoked as a pipe_write() */
  155. if (pipe_write_aux(L) && state && !(state & CB_WRITE_UPDATED)) {
  156. /*
  157. * if this resulted in a write and not already in a CB and not already
  158. * toggled the write update then post the task
  159. */
  160. state |= CB_WRITE_UPDATED;
  161. lua_pushinteger(L, state);
  162. lua_replace(L, UVstate); /* Set CB state write updated flag */
  163. if (state == CB_QUIESCENT | CB_WRITE_UPDATED) {
  164. lua_rawgeti(L, 1, 1); /* Get CB ref from pipe[1] */
  165. luaL_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
  166. }
  167. }
  168. } else if (state != CB_NOT_USED) {
  169. /* invoked by the luaN_taskpost() so call the Lua CB */
  170. int repost; /* can take the values CB_WRITE_UPDATED or 0 */
  171. lua_pushinteger(L, CB_ACTIVE); /* CB state set to active only */
  172. lua_replace(L, UVstate);
  173. lua_pushvalue(L, UVfunc); /* Lua CB function */
  174. lua_pushvalue(L, UVpipe); /* pipe table */
  175. lua_call(L, 1, 1); /* Errors are thrown back to caller */
  176. /*
  177. * On return from the Lua CB, the task is never reposted if the pipe is empty.
  178. * If it is not empty then the Lua CB return status determines when reposting
  179. * occurs:
  180. * - true = repost
  181. * - false = don't repost
  182. * - nil = only repost if there has been a write update.
  183. */
  184. if (lua_isboolean(L,-1)) {
  185. repost = (lua_toboolean(L, -1) == true &&
  186. lua_objlen(L, UVpipe) > 1) ? CB_WRITE_UPDATED : 0;
  187. } else {
  188. repost = state & CB_WRITE_UPDATED;
  189. }
  190. state = CB_QUIESCENT | repost;
  191. lua_pushinteger(L, state); /* Update the CB state */
  192. lua_replace(L, UVstate);
  193. if (repost) {
  194. lua_rawgeti(L, UVpipe, 1); /* Get CB ref from pipe[1] */
  195. luaL_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
  196. }
  197. }
  198. return 0;
  199. }
  200. /* Lua callable methods. Since the metatable is linked to both the pipe table */
  201. /* and the userdata entries the __len & __tostring functions must handle both */
  202. // Lua: buf = pipe.create()
  203. int pipe_create(lua_State *L) {
  204. int prio = -1;
  205. lua_settop(L, 2); /* fix stack sze as 2 */
  206. if (!lua_isnil(L, 1)) {
  207. luaL_checktype(L, 1, LUA_TFUNCTION); /* non-nil arg1 must be a function */
  208. if (lua_isnil(L, 2)) {
  209. prio = PLATFORM_TASK_PRIORITY_MEDIUM;
  210. } else {
  211. prio = (int) lua_tointeger(L, 2);
  212. luaL_argcheck(L, prio >= PLATFORM_TASK_PRIORITY_LOW &&
  213. prio <= PLATFORM_TASK_PRIORITY_HIGH, 2, "invalid priority");
  214. }
  215. }
  216. lua_createtable (L, 1, 0); /* create pipe table */
  217. lua_pushrotable(L, LROT_TABLEREF(pipe_meta));
  218. lua_setmetatable(L, -2); /* set pipe table's metabtable to pipe_meta */
  219. lua_pushvalue(L, -1); /* UV1: pipe object */
  220. lua_pushvalue(L, 1); /* UV2: CB function */
  221. lua_pushinteger(L, prio); /* UV3: task priority */
  222. lua_pushinteger(L, prio == -1 ? CB_NOT_USED : CB_QUIESCENT);
  223. lua_pushcclosure(L, pipe_write_and_read_poster, 4); /* post aux func as C task */
  224. lua_rawseti(L, -2, 1); /* and wrtie to T[1] */
  225. return 1; /* return the table */
  226. }
  227. // len = #pipeobj[i]
  228. static int pipe__len (lua_State *L) {
  229. if (lua_istable(L, 1)) {
  230. lua_pushinteger(L, lua_objlen(L, 1));
  231. } else {
  232. buffer_t *ud = checkPipeUD(L, 1);
  233. lua_pushinteger(L, ud->end - ud->start);
  234. }
  235. return 1;
  236. }
  237. //Lua s = pipeUD:tostring()
  238. static int pipe__tostring (lua_State *L) {
  239. if (lua_istable(L, 1)) {
  240. lua_pushfstring(L, "Pipe: %p", lua_topointer(L, 1));
  241. } else {
  242. buffer_t *ud = checkPipeUD(L, 1);
  243. lua_pushlstring(L, ud->buf + ud->start, ud->end - ud->start);
  244. }
  245. return 1;
  246. }
  247. // Lua: rec = p:read(end_or_delim) // also [-2, +1,- ]
  248. int pipe_read(lua_State *L) {
  249. buffer_t *ud = checkPipeTable(L, 1, AT_HEAD);
  250. int i, k=0, n;
  251. lua_settop(L,2);
  252. const char delim = getsize_delim(L, 2, &n);
  253. lua_pop(L,1);
  254. while (ud && n) {
  255. int want, used, avail = ud->end - ud->start;
  256. if (n < 0 /* one of the CHAR_DELIM flags */) {
  257. /* getting a delimited chunk so scan for delimiter */
  258. for (i = ud->start; i < ud->end && ud->buf[i] != delim; i++) {}
  259. /* Can't have i = ud->end and ud->buf[i] == delim so either */
  260. /* we've scanned full buffer avail OR we've hit a delim char */
  261. if (i == ud->end) {
  262. want = used = avail; /* case where we've scanned without a hit */
  263. } else {
  264. want = used = i + 1 - ud->start; /* case where we've hit a delim */
  265. if (n == CHAR_DELIM)
  266. want--;
  267. n = 0; /* force loop exit because delim found */
  268. }
  269. } else {
  270. want = used = (n < avail) ? n : avail;
  271. n -= used;
  272. }
  273. lua_pushlstring(L, ud->buf + ud->start, want); /* part we want */
  274. k++;
  275. ud->start += used;
  276. if (ud->start == ud->end) {
  277. /* shift the pipe array down overwriting T[1] */
  278. int nUD = lua_objlen(L, 1);
  279. for (i = 2; i < nUD; i++) { /* for i = 2, nUD-1 */
  280. lua_rawgeti(L, 1, i+1); lua_rawseti(L, 1, i); /* T[i] = T[i+1] */
  281. }
  282. lua_pushnil(L); lua_rawseti(L, 1, nUD--); /* T[n] = nil */
  283. if (nUD>1) {
  284. lua_rawgeti(L, 1, 2);
  285. ud = checkPipeUD(L, -1);
  286. lua_pop(L, 1);
  287. } else {
  288. ud = NULL;
  289. }
  290. }
  291. }
  292. if (k)
  293. lua_concat(L, k);
  294. else
  295. lua_pushnil(L);
  296. return 1;
  297. }
  298. // Lua: buf:unread(some_string)
  299. int pipe_unread(lua_State *L) {
  300. size_t l = INVALID_LEN;
  301. const char *s = lua_tolstring(L, 2, &l);
  302. if (l==0)
  303. return 0;
  304. luaL_argcheck(L, l != INVALID_LEN, 2, "must be a string");
  305. buffer_t *ud = checkPipeTable(L, 1, AT_HEAD | WRITING);
  306. do {
  307. int used = ud->end - ud->start;
  308. int lrem = LUAL_BUFFERSIZE-used;
  309. if (used == LUAL_BUFFERSIZE) {
  310. /* If the current UD is full insert a new UD at T[2] */
  311. int i, nUD = lua_objlen(L, 1);
  312. for (i = nUD; i > 0; i--) { /* for i = nUD-1,1,-1 */
  313. lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */
  314. }
  315. ud = newPipeUD(L, 1, 1);
  316. used = 0; lrem = LUAL_BUFFERSIZE;
  317. } else if (ud->start < l) {
  318. /* If the unread can't fit it before the start then shift content to end */
  319. memmove(ud->buf + lrem,
  320. ud->buf + ud->start, used); /* must be memmove not cpy */
  321. ud->start = lrem; ud->end = LUAL_BUFFERSIZE;
  322. }
  323. if (l <= (unsigned )lrem)
  324. break;
  325. /* If we've got here then the remaining string is strictly longer than the */
  326. /* remaining buffer space, so top off the buffer before looping around again */
  327. l -= lrem;
  328. memcpy(ud->buf, s + l, lrem);
  329. ud->start = 0;
  330. } while(1);
  331. /* Copy any residual tail to the UD buffer. Note that this is l>0 and */
  332. ud->start -= l;
  333. memcpy(ud->buf + ud->start, s, l);
  334. return 0;
  335. }
  336. // Lua: buf:write(some_string)
  337. static int pipe_write_aux(lua_State *L) {
  338. size_t l = INVALID_LEN;
  339. const char *s = lua_tolstring(L, 2, &l);
  340. //dbg_printf("pipe write(%u): %s", l, s);
  341. if (l==0)
  342. return false;
  343. luaL_argcheck(L, l != INVALID_LEN, 2, "must be a string");
  344. buffer_t *ud = checkPipeTable(L, 1, AT_TAIL | WRITING);
  345. do {
  346. int used = ud->end - ud->start;
  347. if (used == LUAL_BUFFERSIZE) {
  348. /* If the current UD is full insert a new UD at T[end] */
  349. ud = newPipeUD(L, 1, lua_objlen(L, 1)+1);
  350. used = 0;
  351. } else if (LUAL_BUFFERSIZE - ud->end < l) {
  352. /* If the write can't fit it at the end then shift content to the start */
  353. memmove(ud->buf, ud->buf + ud->start, used); /* must be memmove not cpy */
  354. ud->start = 0; ud->end = used;
  355. }
  356. int lrem = LUAL_BUFFERSIZE-used;
  357. if (l <= (unsigned )lrem)
  358. break;
  359. /* If we've got here then the remaining string is longer than the buffer */
  360. /* space left, so top off the buffer before looping around again */
  361. memcpy(ud->buf + ud->end, s, lrem);
  362. ud->end += lrem;
  363. l -= lrem;
  364. s += lrem;
  365. } while(1);
  366. /* Copy any residual tail to the UD buffer. Note that this is l>0 and */
  367. memcpy(ud->buf + ud->end, s, l);
  368. ud->end += l;
  369. return true;
  370. }
  371. // Lua: fread = pobj:reader(1400) -- or other number
  372. // fread = pobj:reader('\n') -- or other delimiter (delim is stripped)
  373. // fread = pobj:reader('\n+') -- or other delimiter (delim is retained)
  374. #define TBL lua_upvalueindex(1)
  375. #define N lua_upvalueindex(2)
  376. static int pipe_read_aux(lua_State *L) {
  377. lua_settop(L, 0); /* ignore args since we use upvals instead */
  378. lua_pushvalue(L, TBL);
  379. lua_pushvalue(L, N);
  380. return pipe_read(L);
  381. }
  382. static int pipe_reader(lua_State *L) {
  383. lua_settop(L,2);
  384. checkPipeTable (L, 1,AT_HEAD);
  385. getsize_delim(L, 2, NULL);
  386. lua_pushcclosure(L, pipe_read_aux, 2); /* return (closure, nil, table) */
  387. return 1;
  388. }
  389. LROT_BEGIN(pipe_funcs, NULL, 0)
  390. LROT_FUNCENTRY( __len, pipe__len )
  391. LROT_FUNCENTRY( __tostring, pipe__tostring )
  392. LROT_FUNCENTRY( read, pipe_read )
  393. LROT_FUNCENTRY( reader, pipe_reader )
  394. LROT_FUNCENTRY( unread, pipe_unread )
  395. LROT_END(pipe_funcs, NULL, 0)
  396. /* Using a index func is needed because the write method is at pipe[1] */
  397. static int pipe__index(lua_State *L) {
  398. lua_settop(L,2);
  399. const char *k=lua_tostring(L,2);
  400. if(!strcmp(k,"write")){
  401. lua_rawgeti(L, 1, 1);
  402. } else {
  403. lua_pushrotable(L, LROT_TABLEREF(pipe_funcs));
  404. lua_replace(L, 1);
  405. lua_rawget(L, 1);
  406. }
  407. return 1;
  408. }
  409. LROT_BEGIN(pipe_meta, NULL, LROT_MASK_INDEX)
  410. LROT_FUNCENTRY( __index, pipe__index)
  411. LROT_FUNCENTRY( __len, pipe__len )
  412. LROT_FUNCENTRY( __tostring, pipe__tostring )
  413. LROT_END(pipe_meta, NULL, LROT_MASK_INDEX)
  414. LROT_BEGIN(pipe, NULL, 0)
  415. LROT_FUNCENTRY( create, pipe_create )
  416. LROT_END(pipe, NULL, 0)
  417. NODEMCU_MODULE(PIPE, "pipe", pipe, NULL);