/module.c

https://github.com/dpapavas/luaprompt · C · 230 lines · 151 code · 50 blank · 29 comment · 29 complexity · ee08ca9536f10a8455ef70c310fd6f3f MD5 · raw file

  1. /* Copyright (C) 2015 Papavasileiou Dimitris
  2. *
  3. * Permission is hereby granted, free of charge, to any person
  4. * obtaining a copy of this software and associated documentation
  5. * files (the "Software"), to deal in the Software without
  6. * restriction, including without limitation the rights to use, copy,
  7. * modify, merge, publish, distribute, sublicense, and/or sell copies
  8. * of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <lualib.h>
  26. #include <lauxlib.h>
  27. #include "prompt.h"
  28. #if LUA_VERSION_NUM == 501
  29. #define LUA_OK 0
  30. #endif
  31. static int describe (lua_State *L)
  32. {
  33. lua_pushstring(L, luap_describe(L, 1));
  34. return 1;
  35. }
  36. static int enter (lua_State *L)
  37. {
  38. luap_enter(L);
  39. return 0;
  40. }
  41. static int call (lua_State *L)
  42. {
  43. if (lua_gettop(L) < 1 || lua_type(L, 1) != LUA_TFUNCTION) {
  44. lua_settop(L, 0);
  45. lua_pushboolean(L, 0);
  46. } else {
  47. if(luap_call(L, lua_gettop(L) - 1) != LUA_OK) {
  48. lua_pushboolean(L, 0);
  49. }
  50. }
  51. return lua_gettop(L);
  52. }
  53. static void update_index (lua_State *L)
  54. {
  55. const char *k;
  56. k = lua_tostring(L, -1);
  57. if (!k) {
  58. k = "";
  59. }
  60. if (!strcmp(k, "prompts")) {
  61. const char *single, *multi;
  62. luap_getprompts(L, &single, &multi);
  63. lua_newtable(L);
  64. lua_pushstring(L, single);
  65. lua_rawseti(L, -2, 1);
  66. lua_pushstring(L, multi);
  67. lua_rawseti(L, -2, 2);
  68. } else if (!strcmp(k, "colorize")) {
  69. int colorize;
  70. luap_getcolor(L, &colorize);
  71. lua_pushboolean(L, colorize);
  72. } else if (!strcmp(k, "history")) {
  73. const char *history;
  74. luap_gethistory(L, &history);
  75. if (history) {
  76. lua_pushstring(L, history);
  77. } else {
  78. lua_pushboolean(L, 0);
  79. }
  80. } else if (!strcmp(k, "name")) {
  81. const char *name;
  82. luap_getname(L, &name);
  83. lua_pushstring(L, name);
  84. }
  85. /* Update the __index table. */
  86. luaL_getmetafield(L, -3, "__index");
  87. lua_insert(L, -3);
  88. lua_rawset(L, -3);
  89. lua_pop(L, 1);
  90. }
  91. static int prompt_newindex (lua_State *L)
  92. {
  93. const char *k;
  94. k = lua_tostring(L, 2);
  95. if (!k) {
  96. k = "";
  97. }
  98. if (!strcmp(k, "prompts")) {
  99. const char *single, *multi;
  100. lua_rawgeti(L, 3, 1);
  101. single = lua_tostring(L, -1);
  102. lua_rawgeti(L, 3, 2);
  103. multi = lua_tostring(L, -1);
  104. lua_pop(L, 2);
  105. luap_setprompts(L, single, multi);
  106. } else if (!strcmp(k, "colorize")) {
  107. luap_setcolor(L, lua_toboolean(L, 3));
  108. } else if (!strcmp(k, "history")) {
  109. luap_sethistory(L, lua_tostring(L, 3));
  110. } else if (!strcmp(k, "name")) {
  111. luap_setname(L, lua_tostring(L, 3));
  112. } else {
  113. lua_rawset (L, 1);
  114. return 0;
  115. }
  116. /* Update the __index table. */
  117. lua_pop(L, 1);
  118. update_index(L);
  119. return 0;
  120. }
  121. int luaopen_prompt(lua_State* L) {
  122. static const luaL_Reg functions[] = {
  123. {"describe", describe},
  124. {"call", call},
  125. {"enter", enter},
  126. {NULL, NULL},
  127. };
  128. /* Set default values. */
  129. luap_setname(L, "lua");
  130. luap_setprompts (L, "> ", ">> ");
  131. /* Create the prompt table. */
  132. #if LUA_VERSION_NUM == 501
  133. luaL_register(L, "prompt", functions);
  134. #else
  135. lua_newtable (L);
  136. #endif
  137. {
  138. lua_newtable (L);
  139. /* __index */
  140. lua_newtable(L);
  141. lua_setfield (L, -2, "__index");
  142. /* __newindex */
  143. lua_pushcfunction (L, prompt_newindex);
  144. lua_setfield (L, -2, "__newindex");
  145. lua_setmetatable (L, -2);
  146. }
  147. lua_pushliteral(L, "version");
  148. lua_pushliteral(L, LUAP_VERSION);
  149. lua_settable(L, -3);
  150. lua_pushliteral(L, "copyrights");
  151. lua_createtable(L, 2, 0);
  152. lua_pushstring(L,
  153. "luaprompt " LUAP_VERSION " Copyright (C) "
  154. "2012-2015 Dimitris Papavasiliou" );
  155. lua_rawseti(L, -2, 1);
  156. #if LUA_VERSION_NUM == 501
  157. lua_pushstring(L, LUA_VERSION " " LUA_COPYRIGHT);
  158. #else
  159. lua_pushliteral(L, LUA_COPYRIGHT);
  160. #endif
  161. lua_rawseti(L, -2, 2);
  162. lua_settable(L, -3);
  163. lua_pushliteral(L, "interactive");
  164. lua_pushboolean (L, (isatty (STDIN_FILENO) &&
  165. isatty (STDOUT_FILENO)));
  166. lua_settable(L, -3);
  167. /* Initialize the __index table. */
  168. lua_pushliteral(L, "prompts");
  169. update_index(L);
  170. lua_pushliteral(L, "colorize");
  171. update_index(L);
  172. lua_pushliteral(L, "history");
  173. update_index(L);
  174. lua_pushliteral(L, "name");
  175. update_index(L);
  176. #if LUA_VERSION_NUM != 501
  177. luaL_setfuncs(L, functions, 0);
  178. #endif
  179. return 1;
  180. }