interp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include <tripl/arch.h>
  29. int interp_init(void);
  30. __arch int interp_new_var(char *name, long value, int attr);
  31. __arch int interp_dump(int);
  32. __arch int interp_set_var(char *name, long value, int attr);
  33. __arch int interp_import_shared(char *name);
  34. extern char *psave;
  35. struct klist_node stack = KLIST_NODE_INIT(&stack),
  36. libs = KLIST_NODE_INIT(&libs);
  37. extern struct klist_node names_list;
  38. struct arch interp_arch_fn = {
  39. .arch_init = interp_init,
  40. .arch_new_var = interp_new_var,
  41. .arch_dump = interp_dump,
  42. .arch_import_shared = interp_import_shared,
  43. .arch_funcall = NULL,
  44. .arch_set_var = interp_set_var
  45. };
  46. __arch int interp_init(void) {
  47. return 0;
  48. }
  49. static struct klist_node *find_name(char *name) {
  50. struct klist_node *n;
  51. for (n = stack.next; n != &stack; n = n->next)
  52. if (!strcmp(n->payload, name))
  53. return n;
  54. return NULL;
  55. }
  56. __arch int interp_new_var(char *name, long value, int attr) {
  57. push_nr(value, name, attr);
  58. return 0;
  59. }
  60. __arch int interp_set_var(char *name, long value, int attr) {
  61. struct klist_node *n;
  62. /* find the name */
  63. if (unlikely(!(n = find_name(name))))
  64. exit(generic_error("name not found", psave, lno));
  65. if (unlikely(n->xmask & 4))
  66. exit(generic_error("variable sets to be read-only", psave, lno));
  67. n->value = value;
  68. return 0;
  69. }
  70. __arch static int interp_stack_dump(void) {
  71. struct klist_node *n;
  72. int i;
  73. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  74. printf("stack dump: \n");
  75. ansi_csi_ed(stdout);
  76. for (i = 0, n = stack.next; n != &stack; n = n->next, i++)
  77. printf("\t%3d: %s \t%ld @ %08x\n", i, (char *) n->payload, n->value, n->xmask);
  78. printf("\n");
  79. return 0;
  80. }
  81. #define NAMES_VARS 1
  82. #define NAMES_FNS 2
  83. #define NAMES_ANY 3
  84. __arch static int interp_names_dump(int names) {
  85. struct klist_node *n;
  86. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  87. if (names == NAMES_VARS)
  88. printf("dump variables: \n");
  89. else if (names == NAMES_FNS)
  90. printf("dump functions: \n");
  91. else
  92. printf("names dump: \n");
  93. ansi_csi_ed(stdout);
  94. for (n = names_list.next; n != &names_list; n = n->next) {
  95. if ((n->value & DECLARE_FN) && (names & NAMES_FNS)) {
  96. int nargs = n->value & (DECLARE_FN - 1);
  97. if (nargs == DECLARE_NOARG)
  98. nargs = 0;
  99. printf("\tfn %s: \t%d arguments\n", (char *) n->payload, nargs);
  100. }
  101. if (!(n->value & DECLARE_FN) && (names & NAMES_VARS))
  102. /* variables */
  103. printf("\t var %s\n", (char *)n->payload);
  104. }
  105. printf("\n");
  106. return 0;
  107. }
  108. __arch static int interp_libs_dump(void) {
  109. struct klist_node *n;
  110. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  111. printf("dump libraries: \n");
  112. ansi_csi_ed(stdout);
  113. for (n = libs.next; n != &libs; n = n->next)
  114. printf("\tlib %s: handle %p\n", (char *) n->data, n->payload);
  115. printf("\n");
  116. return 0;
  117. }
  118. __arch int interp_dump(int attr) {
  119. if (!attr || attr == 32) {
  120. /* dump all */
  121. interp_stack_dump();
  122. interp_names_dump(NAMES_ANY);
  123. interp_libs_dump();
  124. return 0;
  125. }
  126. if (attr & 1)
  127. interp_stack_dump();
  128. if (attr & 2)
  129. interp_names_dump(NAMES_VARS);
  130. if (attr & 4)
  131. interp_names_dump(NAMES_FNS);
  132. if (attr & 8)
  133. interp_names_dump(NAMES_ANY);
  134. if (attr & 16)
  135. interp_libs_dump();
  136. return 0;
  137. }
  138. __arch int interp_import_shared(char *name) {
  139. struct klist_node *n;
  140. void *dl;
  141. if (!(dl = dlopen(name, RTLD_LAZY)))
  142. exit(generic_error(dlerror(), psave, lno));
  143. n = malloc(sizeof(struct klist_node));
  144. klist_init(n);
  145. n->payload = dl;
  146. n->data = name;
  147. klist_add(&libs, n);
  148. return 0;
  149. }