interp.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* tripl/arch/interp/interp.c
  2. *
  3. * This file is part of tripl.
  4. *
  5. * tripl is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * tripl is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with tripl. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <string.h>
  23. #include <dlfcn.h>
  24. #include <errno.h>
  25. #include <klist.h>
  26. #include <tripl/tripl.h>
  27. #include <tripl/limits.h>
  28. struct klist_node libraries;
  29. static char *lbsave;
  30. static int do_funcall(char *p),
  31. do_dl_funcall(void *f, char *p),
  32. do_check_string(char *p, char *d);
  33. extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
  34. int interp_init(void) {
  35. klist_init(&libraries);
  36. return 0;
  37. }
  38. int interp_do_keyword(char *p, char c) {
  39. struct klist_node *n;
  40. int type, out, i;
  41. char *name = NULL;
  42. void *dlib;
  43. lbsave = p;
  44. switch (c) {
  45. case '$':
  46. /* push a number in (with a name) */
  47. ++p;
  48. _skip_spaces(p);
  49. getword(p, &type, &out);
  50. if (type != WORDTYPE_NR && type != WORDTYPE_NAME)
  51. exit(generic_error("'$' requires a number", lbsave, lno));
  52. if (type == WORDTYPE_NAME) {
  53. name = strndup(p, out);
  54. p += out;
  55. getword(p, &type, &out);
  56. if (type != WORDTYPE_NR)
  57. exit(generic_error("'$' requires a number", lbsave, lno));
  58. }
  59. push_nr(out, name);
  60. break;
  61. case '`':
  62. /* pop a number */
  63. if (klist_empty(&stack))
  64. exit(generic_error("stack underflow", lbsave, lno));
  65. _pop_nr();
  66. break;
  67. case '!':
  68. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  69. printf("stack dump: \n");
  70. ansi_csi_ed(stdout);
  71. if (klist_empty(&stack))
  72. printf("\t(nothing)\n");
  73. for (i = 0, n = stack.next; n != &stack; n = n->next, i++) {
  74. if (n->payload)
  75. /* named */
  76. printf("\t%d: \t\t%d = %x (%s)\n", i,
  77. n->value, n->value, (char *) n->payload);
  78. else
  79. printf("\t%d: \t\t%d = %x\n", i, n->value, n->value);
  80. }
  81. break;
  82. case '<':
  83. ++p;
  84. _skip_spaces(p);
  85. /* import shared library */
  86. p = getword(p, &type, &out);
  87. if (type != WORDTYPE_STR)
  88. exit(generic_error("'<' requires a library name (string)",
  89. lbsave, lno));
  90. name = strndup(p + 1, out - 1);
  91. dlib = dlopen(name, RTLD_LAZY);
  92. if (!dlib) {
  93. generic_error(dlerror(), linebuffer_save, lno);
  94. exit(generic_error("import library failed", lbsave, lno));
  95. }
  96. n = malloc(sizeof(struct klist_node));
  97. klist_init(n);
  98. n->payload = dlib;
  99. /* join to library list */
  100. klist_add(&libraries, n);
  101. free(name);
  102. break;
  103. case '@':
  104. /* function call. the real funny thing is going on :-) */
  105. do_funcall(p);
  106. break;
  107. }
  108. return 0;
  109. }
  110. static int do_funcall(char *p) {
  111. struct klist_node *n;
  112. char *name;
  113. int type, out, res;
  114. void *func;
  115. /* get function name */
  116. p++;
  117. _skip_spaces(p);
  118. p = getword(p, &type, &out);
  119. if (type != WORDTYPE_NAME)
  120. exit(generic_error("a function name required", lbsave, lno));
  121. name = strndup(p, out);
  122. for (n = libraries.next; n != &libraries; n = n->next) {
  123. if ((func = dlsym(n->payload, name))) {
  124. /* real function call */
  125. res = do_dl_funcall(func, p + out);
  126. printf("%s: %d\n", name, res);
  127. free(name);
  128. return 0;
  129. }
  130. }
  131. exit(generic_error("function not found", lbsave, lno));
  132. return 0;
  133. }
  134. static inline struct klist_node *find_name(char *np) {
  135. struct klist_node *n;
  136. for (n = stack.next; n != &stack; n = n->next)
  137. if (!strcmp(n->payload, np))
  138. return n;
  139. return NULL;
  140. }
  141. /* p: the argument list (string) */
  142. static int do_dl_funcall(void *f, char *p) {
  143. struct arg_entry *args, *next_arg;
  144. struct klist_node *node;
  145. char *name, *dest;
  146. int type, out, n = 0, res;
  147. /* get argument list and call */
  148. _skip_spaces(p);
  149. args = malloc(sizeof(struct arg_entry));
  150. next_arg = args;
  151. if (*p == ';')
  152. goto noarg;
  153. while (getword(p, &type, &out)) {
  154. switch (type) {
  155. case WORDTYPE_NAME:
  156. name = strndup(p, out);
  157. if (!(node = find_name(name))) {
  158. generic_error("name not found", lbsave, lno);
  159. free(name);
  160. exit(1);
  161. }
  162. free(name);
  163. out = node->value;
  164. /* fall through */
  165. case WORDTYPE_NR:
  166. arg_init(next_arg);
  167. next_arg->arg_v = out;
  168. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  169. break;
  170. case WORDTYPE_STR:
  171. name = strndup(p + 1, out - 1);
  172. dest = malloc(out - 1);
  173. next_arg->arg_s = dest;
  174. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  175. do_check_string(name, dest);
  176. free(name);
  177. break;
  178. default:
  179. exit(generic_error("error type passing argument", lbsave, lno));
  180. break;
  181. }
  182. if (type != WORDTYPE_STR) {
  183. _skip_word(p);
  184. }
  185. else
  186. p += out + 1;
  187. ++n;
  188. }
  189. noarg: res = arch_funcall(f, args, n);
  190. next_arg = args->next;
  191. for (; args; args = next_arg) {
  192. next_arg = args->next;
  193. if (args->arg_s)
  194. free(args->arg_s);
  195. free(args);
  196. }
  197. return res;
  198. }
  199. static int do_check_string(char *p, char *dest) {
  200. int i, stat = 0;
  201. char ascii = 0;
  202. int high, low;
  203. char buffer[2] = {0, 0};
  204. for (i = 0; p[i]; ) {
  205. if (!stat) {
  206. if (p[i] == '\\')
  207. stat = 1;
  208. else
  209. dest[i] = p[i];
  210. ++i;
  211. continue;
  212. }
  213. switch (p[i]) {
  214. case '\\':
  215. dest[i - 1] = '\\';
  216. break;
  217. case 'n':
  218. dest[i - 1] = '\n';
  219. break;
  220. case 't':
  221. dest[i - 1] = '\t';
  222. break;
  223. case 'x':
  224. /* \xXX */
  225. buffer[0] = p[i + 1];
  226. buffer[1] = p[i + 2];
  227. if (!buffer[0] || !buffer[1])
  228. exit(generic_error("'x' requires a sequence looks like '\\xXX'",
  229. lbsave, lno));
  230. if (buffer[0] >= 'A' && buffer[0] <= 'F')
  231. high = atoX(buffer[0]);
  232. else
  233. high = atox(buffer[0]);
  234. if (buffer[1] >= 'a' && buffer[1] <= 'f')
  235. low = atox(buffer[1]);
  236. else
  237. low = atoX(buffer[1]);
  238. ascii = (high << 4) + low;
  239. dest[i - 2] = ascii;
  240. break;
  241. case '0'...'7':
  242. exit(generic_error("no support for octal numbers. use hexadecimal instead",
  243. lbsave, lno));
  244. break;
  245. default:
  246. exit(generic_error("unknow sequence. \n", lbsave, lno));
  247. }
  248. stat = 0;
  249. i++;
  250. }
  251. return 0;
  252. }