interp.c 7.3 KB

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