|
@@ -31,467 +31,54 @@
|
|
|
#include <klist.h>
|
|
#include <klist.h>
|
|
|
#include <tripl/tripl.h>
|
|
#include <tripl/tripl.h>
|
|
|
#include <tripl/limits.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_stack_dump(void);
|
|
|
|
|
|
|
|
-struct klist_node libraries;
|
|
|
|
|
|
|
+struct klist_node stack;
|
|
|
|
|
|
|
|
-static char *lbsave;
|
|
|
|
|
|
|
|
|
|
|
|
+struct arch interp_arch_fn = {
|
|
|
|
|
+ .arch_init = interp_init,
|
|
|
|
|
+ .arch_new_var = interp_new_var,
|
|
|
|
|
+ .arch_stack_dump = interp_stack_dump,
|
|
|
|
|
+ .arch_import_shared = NULL,
|
|
|
|
|
+ .arch_funcall = NULL,
|
|
|
|
|
+ .arch_set_var = NULL
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
-static int do_funcall(char *p),
|
|
|
|
|
- do_dl_funcall(void *f, char *p),
|
|
|
|
|
- do_check_string(char *p, char *d);
|
|
|
|
|
|
|
|
|
|
-static inline struct klist_node *find_name(char *s);
|
|
|
|
|
-
|
|
|
|
|
-extern int arch_funcall(void *f, struct arg_entry *p, int nargs);
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-int interp_init(void) {
|
|
|
|
|
- klist_init(&libraries);
|
|
|
|
|
|
|
+__arch int interp_init(void) {
|
|
|
|
|
+ klist_init(&stack);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-int interp_do_keyword(char *p, char c) {
|
|
|
|
|
- struct klist_node *n;
|
|
|
|
|
- int type, out, i, attrs, flag = 0;
|
|
|
|
|
-
|
|
|
|
|
- char *name = NULL, *save;
|
|
|
|
|
- void *dlib;
|
|
|
|
|
-
|
|
|
|
|
- lbsave = p;
|
|
|
|
|
-
|
|
|
|
|
- switch (c) {
|
|
|
|
|
- case '$':
|
|
|
|
|
- /* push a number in (with a name) */
|
|
|
|
|
- ++p;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type != WORDTYPE_NAME)
|
|
|
|
|
- exit(generic_error("'$' requires a name", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- name = strndup(p, out);
|
|
|
|
|
-
|
|
|
|
|
- p += out;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type == WORDTYPE_KWORD) {
|
|
|
|
|
- if (out != '=')
|
|
|
|
|
- exit(generic_error("invalid keyword after name", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- p++;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (type != WORDTYPE_NR) {
|
|
|
|
|
- if (type)
|
|
|
|
|
- exit(generic_error("'$' requires a number", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- out = 0;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- p++;
|
|
|
|
|
- _skip_word(p);
|
|
|
|
|
-
|
|
|
|
|
- attrs = getattrmask(p, vardef_attrtab);
|
|
|
|
|
- push_nr(out, name, attrs);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- /* ABANDONED IN COMPILE MODE */
|
|
|
|
|
- case '`':
|
|
|
|
|
- /* pop a number */
|
|
|
|
|
-
|
|
|
|
|
- if (klist_empty(&stack))
|
|
|
|
|
- exit(generic_error("stack underflow", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- _pop_nr();
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case '!':
|
|
|
|
|
- ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
|
|
|
|
|
-
|
|
|
|
|
- printf("stack dump: \n");
|
|
|
|
|
-
|
|
|
|
|
- ansi_csi_ed(stdout);
|
|
|
|
|
-
|
|
|
|
|
- if (klist_empty(&stack))
|
|
|
|
|
- printf("\t(nothing)\n");
|
|
|
|
|
-
|
|
|
|
|
- for (i = 0, n = stack.next; n != &stack; n = n->next, i++) {
|
|
|
|
|
- if (n->payload)
|
|
|
|
|
- /* named */
|
|
|
|
|
- printf("\t%d: \t\t%d = %x (%s)\n", i,
|
|
|
|
|
- n->value, n->value, (char *) n->payload);
|
|
|
|
|
-
|
|
|
|
|
- else
|
|
|
|
|
- printf("\t%d: \t\t%d = %x\n", i, n->value, n->value);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case '<':
|
|
|
|
|
- ++p;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- /* import shared library */
|
|
|
|
|
- p = getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type != WORDTYPE_STR)
|
|
|
|
|
- exit(generic_error("'<' requires a library name (string)",
|
|
|
|
|
- lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- name = strndup(p + 1, out);
|
|
|
|
|
-
|
|
|
|
|
- dlib = dlopen(name, RTLD_LAZY);
|
|
|
|
|
-
|
|
|
|
|
- if (!dlib) {
|
|
|
|
|
- generic_error(dlerror(), linebuffer_save, lno);
|
|
|
|
|
- exit(generic_error("import library failed", lbsave, lno));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- n = malloc(sizeof(struct klist_node));
|
|
|
|
|
-
|
|
|
|
|
- klist_init(n);
|
|
|
|
|
- n->payload = dlib;
|
|
|
|
|
-
|
|
|
|
|
- /* join to library list */
|
|
|
|
|
- klist_add(&libraries, n);
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case '@':
|
|
|
|
|
- ++p;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- /* function call. the real funny thing is going on :-) */
|
|
|
|
|
- do_funcall(p);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- default:
|
|
|
|
|
- save = p;
|
|
|
|
|
-
|
|
|
|
|
- /* setting 'i = 0', or function call */
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type == WORDTYPE_KWORD && out == '&') {
|
|
|
|
|
- flag = 1;
|
|
|
|
|
- p++;
|
|
|
|
|
-
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (type != WORDTYPE_NAME)
|
|
|
|
|
- exit(generic_error("unknown keyword", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- name = strndup(p, out);
|
|
|
|
|
-
|
|
|
|
|
- _skip_word(p);
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- if (*p == '(') {
|
|
|
|
|
- /* function calling */
|
|
|
|
|
- do_funcall(save);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!(n = find_name(name)))
|
|
|
|
|
- exit(generic_error("name not found", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (n->xmask & 4)
|
|
|
|
|
- /* read only */
|
|
|
|
|
- exit(generic_error("variable sets attribute 'read-only'", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (n->xmask & 1 && !flag)
|
|
|
|
|
- exit(generic_error("no '&' found before global variable", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (!getword(p, &type, &out))
|
|
|
|
|
- exit(generic_error("no equal", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (out != '=')
|
|
|
|
|
- exit(generic_error("not a '='", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- p++;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- if (!getword(p, &type, &out))
|
|
|
|
|
- exit(generic_error("no value", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- n->value = out;
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static int do_funcall(char *p) {
|
|
|
|
|
- struct klist_node *n;
|
|
|
|
|
- char *name;
|
|
|
|
|
- int type, out, res;
|
|
|
|
|
-
|
|
|
|
|
- void *func;
|
|
|
|
|
-
|
|
|
|
|
- /* get function name */
|
|
|
|
|
-
|
|
|
|
|
- p = getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type != WORDTYPE_NAME)
|
|
|
|
|
- exit(generic_error("a function name required", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- name = strndup(p, out);
|
|
|
|
|
-
|
|
|
|
|
- for (n = libraries.next; n != &libraries; n = n->next) {
|
|
|
|
|
- if ((func = dlsym(n->payload, name))) {
|
|
|
|
|
- /* real function call */
|
|
|
|
|
-
|
|
|
|
|
- res = do_dl_funcall(func, p + out);
|
|
|
|
|
-
|
|
|
|
|
- printf("%s: %d\n", name, res);
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- return 0;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- error("%s", name);
|
|
|
|
|
- exit(generic_error("function not found", lbsave, lno));
|
|
|
|
|
|
|
+__arch int interp_new_var(char *name, long value, int attr) {
|
|
|
|
|
+ push_nr(value, name, attr);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-static inline struct klist_node *find_name(char *np) {
|
|
|
|
|
|
|
+__arch int interp_stack_dump(void) {
|
|
|
struct klist_node *n;
|
|
struct klist_node *n;
|
|
|
|
|
+ int i;
|
|
|
|
|
|
|
|
- for (n = stack.next; n != &stack; n = n->next)
|
|
|
|
|
- if (!strcmp(n->payload, np))
|
|
|
|
|
- return n;
|
|
|
|
|
-
|
|
|
|
|
- return NULL;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-/* p: the argument list (string) */
|
|
|
|
|
-
|
|
|
|
|
-static int do_dl_funcall(void *f, char *p) {
|
|
|
|
|
- struct arg_entry *args, *next_arg;
|
|
|
|
|
- struct klist_node *node;
|
|
|
|
|
- char *name, *dest;
|
|
|
|
|
- int type, out, n = 0, res;
|
|
|
|
|
-
|
|
|
|
|
- /* get argument list and call */
|
|
|
|
|
-
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- args = malloc(sizeof(struct arg_entry));
|
|
|
|
|
- next_arg = args;
|
|
|
|
|
-
|
|
|
|
|
- if (*p == ';')
|
|
|
|
|
- goto noarg;
|
|
|
|
|
-
|
|
|
|
|
- while (getword(p, &type, &out)) {
|
|
|
|
|
- switch (type) {
|
|
|
|
|
- case WORDTYPE_NAME:
|
|
|
|
|
- name = strndup(p, out);
|
|
|
|
|
-
|
|
|
|
|
- if (!(node = find_name(name))) {
|
|
|
|
|
- error("%s", name);
|
|
|
|
|
- generic_error("name not found", lbsave, lno);
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- exit(1);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- out = node->value;
|
|
|
|
|
-
|
|
|
|
|
- /* fall through */
|
|
|
|
|
-
|
|
|
|
|
- case WORDTYPE_NR:
|
|
|
|
|
- arg_init(next_arg);
|
|
|
|
|
-
|
|
|
|
|
- next_arg->arg_v = out;
|
|
|
|
|
-
|
|
|
|
|
- next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case WORDTYPE_STR:
|
|
|
|
|
- name = strndup(p + 1, out);
|
|
|
|
|
- dest = malloc(out - 1);
|
|
|
|
|
-
|
|
|
|
|
- next_arg->arg_s = dest;
|
|
|
|
|
- next_arg = next_arg->next = malloc(sizeof(struct arg_entry));
|
|
|
|
|
-
|
|
|
|
|
- do_check_string(name, dest);
|
|
|
|
|
-
|
|
|
|
|
- free(name);
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case WORDTYPE_KWORD:
|
|
|
|
|
- if (out != ',' && out != '(' && out != ')')
|
|
|
|
|
- exit(generic_error("unknow keyword after arguments", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (out == '(')
|
|
|
|
|
- if (n)
|
|
|
|
|
- exit(generic_error("invalid '(' after arguments", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (out == ')') {
|
|
|
|
|
- p++;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
-
|
|
|
|
|
- /* ; */
|
|
|
|
|
-
|
|
|
|
|
- getword(p, &type, &out);
|
|
|
|
|
-
|
|
|
|
|
- if (type)
|
|
|
|
|
- exit(generic_error("arguments after ')'", lbsave, lno));
|
|
|
|
|
|
|
+ ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_LIGHTBLUE, BG_BLACK);
|
|
|
|
|
|
|
|
- goto noarg;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ printf("stack dump: \n");
|
|
|
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ ansi_csi_ed(stdout);
|
|
|
|
|
|
|
|
- default:
|
|
|
|
|
- exit(generic_error("error type passing argument", lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (type == WORDTYPE_STR)
|
|
|
|
|
- p += out + 2;
|
|
|
|
|
-
|
|
|
|
|
- else if (type == WORDTYPE_KWORD) {
|
|
|
|
|
- p++;
|
|
|
|
|
- _skip_spaces(p);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- else
|
|
|
|
|
- _skip_word(p);
|
|
|
|
|
-
|
|
|
|
|
- ++n;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-noarg: res = arch_funcall(f, args, n);
|
|
|
|
|
-
|
|
|
|
|
- next_arg = args->next;
|
|
|
|
|
-
|
|
|
|
|
- for (; args; args = next_arg) {
|
|
|
|
|
- next_arg = args->next;
|
|
|
|
|
-
|
|
|
|
|
- if (args->arg_s)
|
|
|
|
|
- free(args->arg_s);
|
|
|
|
|
-
|
|
|
|
|
- free(args);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return res;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-static int do_check_string(char *p, char *dest) {
|
|
|
|
|
- int i, stat = 0;
|
|
|
|
|
- char ascii = 0;
|
|
|
|
|
- int high, low;
|
|
|
|
|
- char buffer[2] = {0, 0};
|
|
|
|
|
-
|
|
|
|
|
- for (i = 0; p[i]; ) {
|
|
|
|
|
- if (!stat) {
|
|
|
|
|
- if (p[i] == '\\')
|
|
|
|
|
- stat = 1;
|
|
|
|
|
-
|
|
|
|
|
- else
|
|
|
|
|
- dest[i] = p[i];
|
|
|
|
|
-
|
|
|
|
|
- ++i;
|
|
|
|
|
-
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- switch (p[i]) {
|
|
|
|
|
- case '\\':
|
|
|
|
|
- dest[i - 1] = '\\';
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case 'n':
|
|
|
|
|
- dest[i - 1] = '\n';
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case 't':
|
|
|
|
|
- dest[i - 1] = '\t';
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case 'x':
|
|
|
|
|
- /* \xXX */
|
|
|
|
|
- buffer[0] = p[i + 1];
|
|
|
|
|
- buffer[1] = p[i + 2];
|
|
|
|
|
-
|
|
|
|
|
- if (!buffer[0] || !buffer[1])
|
|
|
|
|
- exit(generic_error("'x' requires a sequence looks like '\\xXX'",
|
|
|
|
|
- lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- if (buffer[0] >= 'A' && buffer[0] <= 'F')
|
|
|
|
|
- high = atoX(buffer[0]);
|
|
|
|
|
-
|
|
|
|
|
- else
|
|
|
|
|
- high = atox(buffer[0]);
|
|
|
|
|
-
|
|
|
|
|
- if (buffer[1] >= 'a' && buffer[1] <= 'f')
|
|
|
|
|
- low = atox(buffer[1]);
|
|
|
|
|
-
|
|
|
|
|
- else
|
|
|
|
|
- low = atoX(buffer[1]);
|
|
|
|
|
-
|
|
|
|
|
- ascii = (high << 4) + low;
|
|
|
|
|
-
|
|
|
|
|
- dest[i - 2] = ascii;
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case '0'...'7':
|
|
|
|
|
- exit(generic_error("no support for octal numbers. use hexadecimal instead",
|
|
|
|
|
- lbsave, lno));
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- default:
|
|
|
|
|
- exit(generic_error("unknow sequence. \n", lbsave, lno));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- stat = 0;
|
|
|
|
|
- i++;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ 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);
|
|
|
|
|
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|