interp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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, *save;
  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_NAME)
  53. exit(generic_error("'$' requires a name", lbsave, lno));
  54. name = strndup(p, out);
  55. p += out;
  56. _skip_spaces(p);
  57. getword(p, &type, &out);
  58. if (type == WORDTYPE_KWORD) {
  59. if (out != '=')
  60. exit(generic_error("invalid keyword after name", lbsave, lno));
  61. p++;
  62. _skip_spaces(p);
  63. getword(p, &type, &out);
  64. }
  65. if (type != WORDTYPE_NR) {
  66. if (type)
  67. exit(generic_error("'$' requires a number", lbsave, lno));
  68. out = 0;
  69. }
  70. p++;
  71. _skip_word(p);
  72. attrs = getattrmask(p, vardef_attrtab);
  73. push_nr(out, name, attrs);
  74. break;
  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_NAME)
  128. exit(generic_error("unknow keyword", lbsave, lno));
  129. name = np = strndup(p, out);
  130. if (name[0] == '&')
  131. name++;
  132. _skip_word(p);
  133. _skip_spaces(p);
  134. if (*p == '(') {
  135. /* function calling */
  136. do_funcall(save);
  137. break;
  138. }
  139. if (!(n = find_name(name)))
  140. exit(generic_error("name not found", lbsave, lno));
  141. if (n->xmask & 4)
  142. /* read only */
  143. exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
  144. if (n->xmask & 1 && np == name)
  145. exit(generic_error("no '&' found before global variable", lbsave, lno));
  146. if (!getword(p, &type, &out))
  147. exit(generic_error("no equal", lbsave, lno));
  148. if (out != '=')
  149. exit(generic_error("not a '='", lbsave, lno));
  150. p++;
  151. _skip_spaces(p);
  152. if (!getword(p, &type, &out))
  153. exit(generic_error("no value", lbsave, lno));
  154. n->value = out;
  155. free(np);
  156. break;
  157. }
  158. return 0;
  159. }
  160. static int do_funcall(char *p) {
  161. struct klist_node *n;
  162. char *name;
  163. int type, out, res;
  164. void *func;
  165. /* get function name */
  166. p = getword(p, &type, &out);
  167. if (type != WORDTYPE_NAME)
  168. exit(generic_error("a function name required", lbsave, lno));
  169. name = strndup(p, out);
  170. for (n = libraries.next; n != &libraries; n = n->next) {
  171. if ((func = dlsym(n->payload, name))) {
  172. /* real function call */
  173. res = do_dl_funcall(func, p + out);
  174. printf("%s: %d\n", name, res);
  175. free(name);
  176. return 0;
  177. }
  178. }
  179. error(name);
  180. exit(generic_error("function not found", lbsave, lno));
  181. return 0;
  182. }
  183. static inline struct klist_node *find_name(char *np) {
  184. struct klist_node *n;
  185. for (n = stack.next; n != &stack; n = n->next)
  186. if (!strcmp(n->payload, np))
  187. return n;
  188. return NULL;
  189. }
  190. /* p: the argument list (string) */
  191. static int do_dl_funcall(void *f, char *p) {
  192. struct arg_entry *args, *next_arg;
  193. struct klist_node *node;
  194. char *name, *dest;
  195. int type, out, n = 0, res;
  196. /* get argument list and call */
  197. _skip_spaces(p);
  198. args = malloc(sizeof(struct arg_entry));
  199. next_arg = args;
  200. if (*p == ';')
  201. goto noarg;
  202. while (getword(p, &type, &out)) {
  203. switch (type) {
  204. case WORDTYPE_NAME:
  205. name = strndup(p, out);
  206. if (!(node = find_name(name))) {
  207. error(name);
  208. generic_error("name not found", lbsave, lno);
  209. free(name);
  210. exit(1);
  211. }
  212. free(name);
  213. out = node->value;
  214. /* fall through */
  215. case WORDTYPE_NR:
  216. arg_init(next_arg);
  217. next_arg->arg_v = out;
  218. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  219. break;
  220. case WORDTYPE_STR:
  221. name = strndup(p + 1, out);
  222. dest = malloc(out - 1);
  223. next_arg->arg_s = dest;
  224. next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
  225. do_check_string(name, dest);
  226. free(name);
  227. break;
  228. case WORDTYPE_KWORD:
  229. if (out != ',' && out != '(' && out != ')')
  230. exit(generic_error("unknow keyword after arguments", lbsave, lno));
  231. if (out == '(')
  232. if (n)
  233. exit(generic_error("invalid '(' after arguments", lbsave, lno));
  234. if (out == ')') {
  235. p++;
  236. _skip_spaces(p);
  237. /* ; */
  238. getword(p, &type, &out);
  239. if (type)
  240. exit(generic_error("arguments after ')'", lbsave, lno));
  241. goto noarg;
  242. }
  243. break;
  244. default:
  245. exit(generic_error("error type passing argument", lbsave, lno));
  246. break;
  247. }
  248. if (type == WORDTYPE_STR)
  249. p += out + 2;
  250. else if (type == WORDTYPE_KWORD) {
  251. p++;
  252. _skip_spaces(p);
  253. }
  254. else
  255. _skip_word(p);
  256. ++n;
  257. }
  258. noarg: res = arch_funcall(f, args, n);
  259. next_arg = args->next;
  260. for (; args; args = next_arg) {
  261. next_arg = args->next;
  262. if (args->arg_s)
  263. free(args->arg_s);
  264. free(args);
  265. }
  266. return res;
  267. }
  268. static int do_check_string(char *p, char *dest) {
  269. int i, stat = 0;
  270. char ascii = 0;
  271. int high, low;
  272. char buffer[2] = {0, 0};
  273. for (i = 0; p[i]; ) {
  274. if (!stat) {
  275. if (p[i] == '\\')
  276. stat = 1;
  277. else
  278. dest[i] = p[i];
  279. ++i;
  280. continue;
  281. }
  282. switch (p[i]) {
  283. case '\\':
  284. dest[i - 1] = '\\';
  285. break;
  286. case 'n':
  287. dest[i - 1] = '\n';
  288. break;
  289. case 't':
  290. dest[i - 1] = '\t';
  291. break;
  292. case 'x':
  293. /* \xXX */
  294. buffer[0] = p[i + 1];
  295. buffer[1] = p[i + 2];
  296. if (!buffer[0] || !buffer[1])
  297. exit(generic_error("'x' requires a sequence looks like '\\xXX'",
  298. lbsave, lno));
  299. if (buffer[0] >= 'A' && buffer[0] <= 'F')
  300. high = atoX(buffer[0]);
  301. else
  302. high = atox(buffer[0]);
  303. if (buffer[1] >= 'a' && buffer[1] <= 'f')
  304. low = atox(buffer[1]);
  305. else
  306. low = atoX(buffer[1]);
  307. ascii = (high << 4) + low;
  308. dest[i - 2] = ascii;
  309. break;
  310. case '0'...'7':
  311. exit(generic_error("no support for octal numbers. use hexadecimal instead",
  312. lbsave, lno));
  313. break;
  314. default:
  315. exit(generic_error("unknow sequence. \n", lbsave, lno));
  316. }
  317. stat = 0;
  318. i++;
  319. }
  320. return 0;
  321. }