| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /* tripl/arch/interp/interp.c
- *
- * This file is part of tripl.
- *
- * tripl is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * tripl is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with tripl. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <string.h>
- #include <dlfcn.h>
- #include <errno.h>
- #include <klist.h>
- #include <tripl/tripl.h>
- #include <tripl/limits.h>
- #include <tripl/arch.h>
- int interp_init(void);
- __arch int interp_new_var(char *name, long value, int attr);
- __arch int interp_dump(int);
- __arch int interp_set_var(char *name, long value, int attr);
- __arch int interp_import_shared(char *name);
- extern char *psave;
- struct klist_node stack = KLIST_NODE_INIT(&stack),
- libs = KLIST_NODE_INIT(&libs);
- extern struct klist_node names_list;
- struct arch interp_arch_fn = {
- .arch_init = interp_init,
- .arch_new_var = interp_new_var,
- .arch_dump = interp_dump,
- .arch_import_shared = interp_import_shared,
- .arch_funcall = NULL,
- .arch_set_var = interp_set_var
- };
- __arch int interp_init(void) {
- return 0;
- }
- static struct klist_node *find_name(char *name) {
- struct klist_node *n;
- for (n = stack.next; n != &stack; n = n->next)
- if (!strcmp(n->payload, name))
- return n;
- return NULL;
- }
- __arch int interp_new_var(char *name, long value, int attr) {
- push_nr(value, name, attr);
- return 0;
- }
- __arch int interp_set_var(char *name, long value, int attr) {
- struct klist_node *n;
- /* find the name */
- if (unlikely(!(n = find_name(name))))
- exit(generic_error("name not found", psave, lno));
- if (unlikely(n->xmask & 4))
- exit(generic_error("variable sets to be read-only", psave, lno));
- n->value = value;
- return 0;
- }
- __arch static int interp_stack_dump(void) {
- struct klist_node *n;
- int i;
- ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
- printf("stack dump: \n");
- ansi_csi_ed(stdout);
- for (i = 0, n = stack.next; n != &stack; n = n->next, i++)
- printf("\t%3d: %s \t%ld @ %08x\n", i, (char *) n->payload, n->value, n->xmask);
- printf("\n");
- return 0;
- }
- #define NAMES_VARS 1
- #define NAMES_FNS 2
- #define NAMES_ANY 3
- __arch static int interp_names_dump(int names) {
- struct klist_node *n;
- ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
- if (names == NAMES_VARS)
- printf("dump variables: \n");
- else if (names == NAMES_FNS)
- printf("dump functions: \n");
- else
- printf("names dump: \n");
- ansi_csi_ed(stdout);
- for (n = names_list.next; n != &names_list; n = n->next) {
- if ((n->value & DECLARE_FN) && (names & NAMES_FNS)) {
- int nargs = n->value & (DECLARE_FN - 1);
- if (nargs == DECLARE_NOARG)
- nargs = 0;
- printf("\tfn %s: \t%d arguments\n", (char *) n->payload, nargs);
- }
-
- if (!(n->value & DECLARE_FN) && (names & NAMES_VARS))
- /* variables */
- printf("\t var %s\n", (char *)n->payload);
- }
- printf("\n");
- return 0;
- }
- __arch static int interp_libs_dump(void) {
- struct klist_node *n;
- ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
- printf("dump libraries: \n");
- ansi_csi_ed(stdout);
- for (n = libs.next; n != &libs; n = n->next)
- printf("\tlib %s: handle %p\n", (char *) n->data, n->payload);
- printf("\n");
- return 0;
- }
- __arch int interp_dump(int attr) {
- if (!attr || attr == 32) {
- /* dump all */
- interp_stack_dump();
- interp_names_dump(NAMES_ANY);
- interp_libs_dump();
- return 0;
- }
- if (attr & 1)
- interp_stack_dump();
- if (attr & 2)
- interp_names_dump(NAMES_VARS);
- if (attr & 4)
- interp_names_dump(NAMES_FNS);
- if (attr & 8)
- interp_names_dump(NAMES_ANY);
- if (attr & 16)
- interp_libs_dump();
- return 0;
- }
- __arch int interp_import_shared(char *name) {
- struct klist_node *n;
- void *dl;
-
- if (!(dl = dlopen(name, RTLD_LAZY)))
- exit(generic_error(dlerror(), psave, lno));
- n = malloc(sizeof(struct klist_node));
- klist_init(n);
- n->payload = dl;
- n->data = name;
- klist_add(&libs, n);
- return 0;
- }
|