interp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_stack_dump(void);
  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. struct arch interp_arch_fn = {
  38. .arch_init = interp_init,
  39. .arch_new_var = interp_new_var,
  40. .arch_stack_dump = interp_stack_dump,
  41. .arch_import_shared = interp_import_shared,
  42. .arch_funcall = NULL,
  43. .arch_set_var = interp_set_var
  44. };
  45. __arch int interp_init(void) {
  46. return 0;
  47. }
  48. static struct klist_node *find_name(char *name) {
  49. struct klist_node *n;
  50. for (n = stack.next; n != &stack; n = n->next)
  51. if (!strcmp(n->payload, name))
  52. return n;
  53. return NULL;
  54. }
  55. __arch int interp_new_var(char *name, long value, int attr) {
  56. push_nr(value, name, attr);
  57. return 0;
  58. }
  59. __arch int interp_set_var(char *name, long value, int attr) {
  60. struct klist_node *n;
  61. /* find the name */
  62. if (!(n = find_name(name)))
  63. exit(generic_error("name not found", psave, lno));
  64. if (n->xmask & 4)
  65. exit(generic_error("variable sets to be read-only", psave, lno));
  66. n->value = value;
  67. return 0;
  68. }
  69. __arch int interp_stack_dump(void) {
  70. struct klist_node *n;
  71. int i;
  72. ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
  73. printf("stack dump: \n");
  74. ansi_csi_ed(stdout);
  75. for (i = 0, n = stack.next; n != &stack; n = n->next, i++)
  76. printf("\t%3d: %s \t%ld @ %08x\n", i, (char *) n->payload, n->value, n->xmask);
  77. return 0;
  78. }
  79. __arch int interp_import_shared(char *name) {
  80. struct klist_node *n;
  81. void *dl;
  82. if (!(dl = dlopen(name, RTLD_LAZY)))
  83. exit(generic_error(dlerror(), psave, lno));
  84. n = malloc(sizeof(struct klist_node));
  85. klist_init(n);
  86. n->payload = dl;
  87. klist_add(&libs, n);
  88. return 0;
  89. }