interp.c 3.9 KB

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