interp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. static inline struct klist_node *find_name(char *s);
  34. extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
  35. int interp_init(void) {
  36. klist_init(&libraries);
  37. return 0;
  38. }
  39. int interp_do_keyword(char *p, char c) {
  40. struct klist_node *n;
  41. int type, out, i, attrs, flag = 0;
  42. char *name = NULL, *save;
  43. void *dlib;
  44. lbsave = p;
  45. switch (c) {
  46. case '$':
  47. /* push a number in (with a name) */
  48. ++p;
  49. _skip_spaces(p);
  50. getword(p, &type, &out);
  51. if (type != WORDTYPE_NAME)
  52. exit(generic_error("'$' requires a name", lbsave, lno));
  53. name = strndup(p, out);
  54. p += out;
  55. _skip_spaces(p);
  56. getword(p, &type, &out);
  57. if (type == WORDTYPE_KWORD) {
  58. if (out != '=')
  59. exit(generic_error("invalid keyword after name", lbsave, lno));
  60. p++;
  61. _skip_spaces(p);
  62. getword(p, &type, &out);
  63. }
  64. if (type != WORDTYPE_NR) {
  65. if (type)
  66. exit(generic_error("'$' requires a number", lbsave, lno));
  67. out = 0;
  68. }
  69. p++;
  70. _skip_word(p);
  71. attrs = getattrmask(p, vardef_attrtab);
  72. push_nr(out, name, attrs);
  73. break;
  74. /* ABANDONED IN COMPILE MODE */
  75. case '`':
  76. /* pop a number */
  77. if (klist_empty(&stack))
  78. exit(generic_error("stack underflow", lbsave, lno));
  79. _pop_nr();
  80. break;
  81. case '!':
  82. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  83. printf("stack dump: \n");
  84. ansi_csi_ed(stdout);
  85. if (klist_empty(&stack))
  86. printf("\t(nothing)\n");
  87. for (i = 0, n = stack.next; n != &stack; n = n->next, i++) {
  88. if (n->payload)
  89. /* named */
  90. printf("\t%d: \t\t%d = %x (%s)\n", i,
  91. n->value, n->value, (char *) n->payload);
  92. else
  93. printf("\t%d: \t\t%d = %x\n", i, n->value, n->value);
  94. }
  95. break;
  96. case '<':
  97. ++p;
  98. _skip_spaces(p);
  99. /* import shared library */
  100. p = getword(p, &type, &out);
  101. if (type != WORDTYPE_STR)
  102. exit(generic_error("'<' requires a library name (string)",
  103. lbsave, lno));
  104. name = strndup(p + 1, out);
  105. dlib = dlopen(name, RTLD_LAZY);
  106. if (!dlib) {
  107. generic_error(dlerror(), linebuffer_save, lno);
  108. exit(generic_error("import library failed", lbsave, lno));
  109. }
  110. n = malloc(sizeof(struct klist_node));
  111. klist_init(n);
  112. n->payload = dlib;
  113. /* join to library list */
  114. klist_add(&libraries, n);
  115. free(name);
  116. break;
  117. case '@':
  118. ++p;
  119. _skip_spaces(p);
  120. /* function call. the real funny thing is going on :-) */
  121. do_funcall(p);
  122. break;
  123. default:
  124. save = p;
  125. /* setting 'i = 0', or function call */
  126. getword(p, &type, &out);
  127. if (type == WORDTYPE_KWORD && out == '&') {
  128. flag = 1;
  129. p++;
  130. getword(p, &type, &out);
  131. }
  132. if (type != WORDTYPE_NAME)
  133. exit(generic_error("unknown keyword", lbsave, lno));
  134. name = strndup(p, out);
  135. _skip_word(p);
  136. _skip_spaces(p);
  137. if (*p == '(') {
  138. /* function calling */
  139. do_funcall(save);
  140. break;
  141. }
  142. if (!(n = find_name(name)))
  143. exit(generic_error("name not found", lbsave, lno));
  144. if (n->xmask & 4)
  145. /* read only */
  146. exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
  147. if (n->xmask & 1 && !flag)
  148. exit(generic_error("no '&' found before global variable", lbsave, lno));
  149. if (!getword(p, &type, &out))
  150. exit(generic_error("no equal", lbsave, lno));
  151. if (out != '=')
  152. exit(generic_error("not a '='", lbsave, lno));
  153. p++;
  154. _skip_spaces(p);
  155. if (!getword(p, &type, &out))
  156. exit(generic_error("no value", lbsave, lno));
  157. n->value = out;
  158. free(name);
  159. break;
  160. }
  161. return 0;
  162. }
  163. static int do_funcall(char *p) {
  164. struct klist_node *n;
  165. char *name;
  166. int type, out, res;
  167. void *func;
  168. /* get function name */
  169. p = getword(p, &type, &out);
  170. if (type != WORDTYPE_NAME)
  171. exit(generic_error("a function name required", lbsave, lno));
  172. name = strndup(p, out);
  173. for (n = libraries.next; n != &libraries; n = n->next) {
  174. if ((func = dlsym(n->payload, name))) {
  175. /* real function call */
  176. res = do_dl_funcall(func, p + out);
  177. printf("%s: %d\n", name, res);
  178. free(name);
  179. return 0;
  180. }
  181. }
  182. error("%s", name);
  183. exit(generic_error("function not found", lbsave, lno));
  184. return 0;
  185. }
  186. static inline struct klist_node *find_name(char *np) {
  187. struct klist_node *n;
  188. for (n = stack.next; n != &stack; n = n->next)
  189. if (!strcmp(n->payload, np))
  190. return n;
  191. return NULL;
  192. }
  193. /* p: the argument list (string) */
  194. static int do_dl_funcall(void *f, char *p) {
  195. struct arg_entry *args, *next_arg;
  196. struct klist_node *node;
  197. char *name, *dest;
  198. int type, out, n = 0, res;
  199. /* get argument list and call */
  200. _skip_spaces(p);
  201. args = malloc(sizeof(struct arg_entry));
  202. next_arg = args;
  203. if (*p == ';')
  204. goto noarg;
  205. while (getword(p, &type, &out)) {
  206. switch (type) {
  207. case WORDTYPE_NAME:
  208. name = strndup(p, out);
  209. if (!(node = find_name(name))) {
  210. error("%s", name);
  211. generic_error("name not found", lbsave, lno);
  212. free(name);
  213. exit(1);
  214. }
  215. free(name);
  216. out = node->value;
  217. /* fall through */
  218. case WORDTYPE_NR:
  219. arg_init(next_arg);
  220. next_arg->arg_v = out;
  221. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  222. break;
  223. case WORDTYPE_STR:
  224. name = strndup(p + 1, out);
  225. dest = malloc(out - 1);
  226. next_arg->arg_s = dest;
  227. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  228. do_check_string(name, dest);
  229. free(name);
  230. break;
  231. case WORDTYPE_KWORD:
  232. if (out != ',' && out != '(' && out != ')')
  233. exit(generic_error("unknow keyword after arguments", lbsave, lno));
  234. if (out == '(')
  235. if (n)
  236. exit(generic_error("invalid '(' after arguments", lbsave, lno));
  237. if (out == ')') {
  238. p++;
  239. _skip_spaces(p);
  240. /* ; */
  241. getword(p, &type, &out);
  242. if (type)
  243. exit(generic_error("arguments after ')'", lbsave, lno));
  244. goto noarg;
  245. }
  246. break;
  247. default:
  248. exit(generic_error("error type passing argument", lbsave, lno));
  249. break;
  250. }
  251. if (type == WORDTYPE_STR)
  252. p += out + 2;
  253. else if (type == WORDTYPE_KWORD) {
  254. p++;
  255. _skip_spaces(p);
  256. }
  257. else
  258. _skip_word(p);
  259. ++n;
  260. }
  261. noarg: res = arch_funcall(f, args, n);
  262. next_arg = args->next;
  263. for (; args; args = next_arg) {
  264. next_arg = args->next;
  265. if (args->arg_s)
  266. free(args->arg_s);
  267. free(args);
  268. }
  269. return res;
  270. }
  271. static int do_check_string(char *p, char *dest) {
  272. int i, stat = 0;
  273. char ascii = 0;
  274. int high, low;
  275. char buffer[2] = {0, 0};
  276. for (i = 0; p[i]; ) {
  277. if (!stat) {
  278. if (p[i] == '\\')
  279. stat = 1;
  280. else
  281. dest[i] = p[i];
  282. ++i;
  283. continue;
  284. }
  285. switch (p[i]) {
  286. case '\\':
  287. dest[i - 1] = '\\';
  288. break;
  289. case 'n':
  290. dest[i - 1] = '\n';
  291. break;
  292. case 't':
  293. dest[i - 1] = '\t';
  294. break;
  295. case 'x':
  296. /* \xXX */
  297. buffer[0] = p[i + 1];
  298. buffer[1] = p[i + 2];
  299. if (!buffer[0] || !buffer[1])
  300. exit(generic_error("'x' requires a sequence looks like '\\xXX'",
  301. lbsave, lno));
  302. if (buffer[0] >= 'A' && buffer[0] <= 'F')
  303. high = atoX(buffer[0]);
  304. else
  305. high = atox(buffer[0]);
  306. if (buffer[1] >= 'a' && buffer[1] <= 'f')
  307. low = atox(buffer[1]);
  308. else
  309. low = atoX(buffer[1]);
  310. ascii = (high << 4) + low;
  311. dest[i - 2] = ascii;
  312. break;
  313. case '0'...'7':
  314. exit(generic_error("no support for octal numbers. use hexadecimal instead",
  315. lbsave, lno));
  316. break;
  317. default:
  318. exit(generic_error("unknow sequence. \n", lbsave, lno));
  319. }
  320. stat = 0;
  321. i++;
  322. }
  323. return 0;
  324. }