interp.c 8.0 KB

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