interpreter.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #define _POSIX_C_SOURCE 1
  18. #include <arpa/inet.h>
  19. #include <errno.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <strings.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <readline/readline.h>
  28. #include <readline/history.h>
  29. #include "interpreter.h"
  30. // = 0: OK
  31. // > 0: EOF
  32. // < 0: error (errno)
  33. // function readn
  34. // * Copyright (c) 1996 W. Richard Stevens. All rights reserved.
  35. ssize_t /* Read "n" bytes from a descriptor. */
  36. readn(int fd, void *vptr, size_t n)
  37. {
  38. size_t nleft;
  39. ssize_t nread;
  40. char *ptr;
  41. ptr = vptr;
  42. nleft = n;
  43. while (nleft > 0) {
  44. if ( (nread = read(fd, ptr, nleft)) < 0) {
  45. if (errno == EINTR)
  46. nread = 0; /* and call read() again */
  47. else
  48. return (-1);
  49. } else if (nread == 0)
  50. break; /* EOF */
  51. nleft -= nread;
  52. ptr += nread;
  53. }
  54. return (n - nleft); /* return >= 0 */
  55. }
  56. /* >=0: OK
  57. * <0 : error, errno.
  58. */
  59. /* function writen
  60. * Copyright (c) 1996 W. Richard Stevens. All rights reserved.
  61. */
  62. /* Write "n" bytes to a descriptor. */
  63. ssize_t writen(int fd, const void *vptr, size_t n) {
  64. size_t nleft;
  65. ssize_t nwritten;
  66. const char *ptr;
  67. ptr = vptr;
  68. nleft = n;
  69. while (nleft > 0) {
  70. if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
  71. if (nwritten < 0 && errno == EINTR)
  72. nwritten = 0; /* and call write() again */
  73. else
  74. return (-1); /* error */
  75. }
  76. nleft -= nwritten;
  77. ptr += nwritten;
  78. }
  79. return (n);
  80. }
  81. char *int2bytes(int n, char *bytes) {
  82. for (int i = 0; i < sizeof(int); ++i) {
  83. bytes[i] = n & 0xFF;
  84. n >>= 8;
  85. }
  86. return bytes;
  87. }
  88. int cmd_from_lisp(struct session *s) {
  89. if (readn(s->fd, &(s->cmd), sizeof(char)) < 0) {
  90. perror("cmd_from_lisp");
  91. return EXIT_FAILURE;
  92. }
  93. return 0;
  94. }
  95. int dump_to_stream(struct session *s, FILE *stream, char *perror_string) {
  96. size_t n = s->i;
  97. if (isatty(STDIN_FILENO) && isatty(fileno(stream))) {
  98. char *pos = s->data + n - 1;
  99. size_t len = 0;
  100. for (; pos >= s->data; --pos, ++len) {
  101. if (*pos == '\n') {
  102. ++pos;
  103. s->readline_prompt_fail = false;
  104. break;
  105. }
  106. }
  107. if (pos < s->data) pos = s->data;
  108. if (s->readline_prompt_len + len < 256) {
  109. memcpy(s->readline_prompt + s->readline_prompt_len, pos, len);
  110. s->readline_prompt_len += len;
  111. *(s->readline_prompt + s->readline_prompt_len) = 0;
  112. } else {
  113. // pray such a long string is not going to be used as prompt
  114. *(s->readline_prompt) = 0;
  115. s->readline_prompt_len = 0;
  116. s->readline_prompt_fail = true;
  117. }
  118. }
  119. if (fwrite(s->data, 1, n, stream) != n) {
  120. perror(perror_string);
  121. return EXIT_FAILURE;
  122. }
  123. return 0;
  124. }
  125. int dump_to_stdout(struct session *s) {
  126. return dump_to_stream(s, stdout, "dump_to_stdout");
  127. }
  128. int dump_to_stderr(struct session *s) {
  129. return dump_to_stream(s, stderr, "dump_to_stderr");
  130. }
  131. int flush_stream(FILE *stream, char *perror_string) {
  132. if (fflush(stream)) {
  133. perror(perror_string);
  134. return EXIT_FAILURE;
  135. }
  136. return 0;
  137. }
  138. int flush_stdout(struct session *s) {
  139. return flush_stream(stdout, "flush_stdout");
  140. }
  141. int flush_stderr(struct session *s) {
  142. return flush_stream(stderr, "flush_stderr");
  143. }
  144. int fread_from_stdin(struct session *s) {
  145. s->i = (uint32_t) fread(s->data, 1, MAX_DATA_SIZE, stdin);
  146. if (s->i < MAX_DATA_SIZE && ferror(stdin)) {
  147. perror("fread_from_stdin");
  148. return EXIT_FAILURE;
  149. }
  150. return 0;
  151. }
  152. int fgets_from_stdin(struct session *s) {
  153. if (fgets(s->data, MAX_DATA_SIZE, stdin)) {
  154. s->i = (uint32_t) strlen(s->data);
  155. } else if(ferror(stdin)) {
  156. perror("fgets_from_stdin");
  157. return EXIT_FAILURE;
  158. } else {
  159. s->i = 0;
  160. }
  161. return 0;
  162. }
  163. int readline_from_stdin(struct session *s) {
  164. if (s->line_read_start >= s->line_read_len) {
  165. free(s->line_read);
  166. if (s->readline_prompt_fail) {
  167. s->line_read = readline("");
  168. } else {
  169. // kill the line, hopefully it's in the prefix
  170. printf("\33[2K\r");
  171. s->line_read = readline(s->readline_prompt);
  172. }
  173. // readline ends with a newline, so reset the prompt
  174. *(s->readline_prompt) = 0;
  175. s->readline_prompt_len = 0;
  176. s->readline_prompt_fail = false;
  177. if (s->line_read && *(s->line_read)) add_history(s->line_read);
  178. s->line_read_start = 0;
  179. s->line_read_len = (s->line_read) ? strlen(s->line_read) : 0;
  180. if (s->line_read) {
  181. *(s->line_read + s->line_read_len) = '\n';
  182. ++(s->line_read_len);
  183. }
  184. }
  185. if (s->line_read_start >= s->line_read_len) {
  186. s->i = 0;
  187. return 0;
  188. }
  189. if (s->line_read_len - s->line_read_start <= MAX_DATA_SIZE) {
  190. memcpy(s->data, s->line_read + s->line_read_start, s->line_read_len - s->line_read_start);
  191. s->i = s->line_read_len - s->line_read_start;
  192. s->line_read_start = s->line_read_len;
  193. return 0;
  194. }
  195. memcpy(s->data, s->line_read + s->line_read_start, MAX_DATA_SIZE);
  196. s->i = MAX_DATA_SIZE;
  197. s->line_read_start += MAX_DATA_SIZE;
  198. return 0;
  199. }
  200. // should perror be called by the caller instead?
  201. int int_to_lisp(struct session *s) {
  202. uint32_t nw = htonl(s->i);
  203. if (writen(s->fd, &nw, sizeof(uint32_t)) < 0) {
  204. perror("int_to_lisp");
  205. return EXIT_FAILURE;
  206. }
  207. return 0;
  208. }
  209. int data_to_lisp (struct session *s) {
  210. if (int_to_lisp(s)) return EXIT_FAILURE; // send_int may call perror
  211. if (s->i > 0) {
  212. if (writen(s->fd, s->data, (size_t) s->i) < 0) {
  213. perror("data_to_lisp");
  214. return EXIT_FAILURE;
  215. }
  216. }
  217. return 0;
  218. }
  219. int string_to_lisp (struct session *s) {
  220. size_t len = strlen(s->string);
  221. s->i = (uint32_t) len;
  222. if (int_to_lisp(s)) return EXIT_FAILURE; // send_int may call perror
  223. if (len > 0) {
  224. if (writen(s->fd, s->string, len) < 0) {
  225. perror("string_to_lisp");
  226. return EXIT_FAILURE;
  227. }
  228. }
  229. return 0;
  230. }
  231. int save_lisp_argc (struct session *s) {
  232. s->i = (uint32_t) s->lisp_argc;
  233. return 0;
  234. }
  235. int save_lisp_arg (struct session *s) {
  236. if (s->i >= s->lisp_argc) {
  237. perror("save_lisp_arg");
  238. return EXIT_FAILURE;
  239. }
  240. s->string = s->lisp_argv[s->i];
  241. return 0;
  242. }
  243. int save_arg0 (struct session *s) {
  244. s->string = s->argv0;
  245. return 0;
  246. }
  247. int save_env (struct session *s) {
  248. s->string = getenv(s->data);
  249. s->i = (s->string != NULL);
  250. return 0;
  251. }
  252. int save_cwd (struct session *s) {
  253. if(!getcwd(s->data, MAX_DATA_SIZE)) {
  254. perror("getcwd error");
  255. return EXIT_FAILURE;
  256. }
  257. s->string = s->data;
  258. return 0;
  259. }
  260. int save_isatty (struct session *s) {
  261. s->i = isatty(0);
  262. return 0;
  263. }
  264. int quit(struct session *s) {
  265. s->cont = false;
  266. return 0;
  267. }
  268. int pong(struct session *s) {
  269. s->i = PONG;
  270. return int_to_lisp(s);
  271. }
  272. int int_from_lisp(struct session *s) {
  273. uint32_t n = 0;
  274. if (readn(s->fd, &n, sizeof(uint32_t)) < 0) {
  275. perror("int_from_lisp");
  276. return EXIT_FAILURE;
  277. }
  278. s->i = ntohl(n);
  279. return 0;
  280. }
  281. int data_from_lisp(struct session *s) {
  282. if (int_from_lisp(s)) {
  283. perror("data_from_lisp");
  284. return EXIT_FAILURE;
  285. }
  286. if (s->i >= MAX_DATA_SIZE) return EXIT_FAILURE;
  287. if (readn(s->fd, s->data, (size_t) s->i) < 0) {
  288. perror("data_from_lisp");
  289. return EXIT_FAILURE;
  290. }
  291. return 0;
  292. }
  293. dispatch_fun_t dispatch_functions[NUM_COMMANDS];
  294. void set_dispatch_functions() {
  295. dispatch_functions[0] = quit;
  296. dispatch_functions[1] = pong;
  297. dispatch_functions[2] = int_from_lisp;
  298. dispatch_functions[3] = data_from_lisp;
  299. dispatch_functions[4] = int_to_lisp;
  300. dispatch_functions[5] = data_to_lisp;
  301. dispatch_functions[6] = isatty(0) ? readline_from_stdin : fread_from_stdin;
  302. dispatch_functions[7] = dump_to_stdout;
  303. dispatch_functions[8] = flush_stdout;
  304. dispatch_functions[9] = dump_to_stderr;
  305. dispatch_functions[10] = flush_stderr;
  306. dispatch_functions[11] = string_to_lisp;
  307. dispatch_functions[12] = save_arg0;
  308. dispatch_functions[13] = save_lisp_argc;
  309. dispatch_functions[14] = save_lisp_arg;
  310. dispatch_functions[15] = save_env;
  311. dispatch_functions[16] = save_cwd;
  312. dispatch_functions[17] = save_isatty;
  313. }
  314. void init_readline() {
  315. rl_bind_key ('\t', rl_insert);
  316. }