| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- /* 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>
- 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;
- }
|