interp.c 4.0 KB

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