/* 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 */ #include #include #include #include #include #include #include #include #include #include struct klist_node libraries; static char *lbsave; 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); return 0; } int interp_do_keyword(char *p, char c) { struct klist_node *n; int type, out, i, attrs; char *np; char *name = NULL; 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_NR && type != WORDTYPE_NAME) exit(generic_error("'$' requires a number", lbsave, lno)); if (type == WORDTYPE_NAME) { name = strndup(p, out); p += out; getword(p, &type, &out); if (type != WORDTYPE_NR) exit(generic_error("'$' requires a number", lbsave, lno)); } p++; _skip_word(p); attrs = getattrmask(p, vardef_attrtab); push_nr(out, name, attrs); break; 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 - 1); 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 '@': /* function call. the real funny thing is going on :-) */ do_funcall(p); break; default: /* setting 'i = 0' */ getword(p, &type, &out); if (type != WORDTYPE_NAME) exit(generic_error("unknow keyword", lbsave, lno)); name = np = strndup(p, out); if (name[0] == '&') name++; 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 && np == name) exit(generic_error("no '&' found before global variable", lbsave, lno)); _skip_word(p); _skip_spaces(p); 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(np); 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++; _skip_spaces(p); 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; } } exit(generic_error("function not found", lbsave, lno)); return 0; } static inline struct klist_node *find_name(char *np) { struct klist_node *n; 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))) { 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 - 1); 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; default: exit(generic_error("error type passing argument", lbsave, lno)); break; } if (type != WORDTYPE_STR) { _skip_word(p); } else p += out + 2; ++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++; } return 0; }