Fri Jul 5 12:22:51 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / elf / eval.c
blob91415bb6b61c9a17ec3c3de0f68135ee92252d15
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <stdlib.h>
4 #include <dlfcn.h>
6 static void *funcall (char **stringp);
7 static void *eval (char **stringp);
9 static void *
10 funcall (char **stringp)
12 void *args[strlen (*stringp)], **ap = args;
13 void *argcookie = &args[1];
17 /* Evaluate the next token. */
18 *ap++ = eval (stringp);
20 /* Whitespace is irrelevant. */
21 while (isspace (**stringp))
22 ++*stringp;
24 /* Terminate at closing paren or end of line. */
25 } while (**stringp != '\0' && **stringp != ')');
26 if (**stringp != '\0')
27 /* Swallow closing paren. */
28 ++*stringp;
30 /* Do it to it. */
31 __builtin_return (__builtin_apply (args[0],
32 &argcookie,
33 (char *) ap - (char *) &args[1]));
36 static void *
37 eval (char **stringp)
39 void *value;
40 char *p = *stringp, c;
42 /* Whitespace is irrelevant. */
43 while (isspace (*p))
44 ++p;
46 switch (*p)
48 case '"':
49 /* String constant. */
50 value = ++p;
52 if (*p == '\\')
54 switch (*strcpy (p, p + 1))
56 case 't':
57 *p = '\t';
58 break;
59 case 'n':
60 *p = '\n';
61 break;
63 ++p;
65 while (*p != '\0' && *p++ != '"');
66 if (p[-1] == '"')
67 p[-1] = '\0';
68 break;
70 case '(':
71 *stringp = ++p;
72 return funcall (stringp);
74 default:
75 /* Try to parse it as a number. */
76 value = (void *) strtol (p, stringp, 0);
77 if (*stringp != p)
78 return value;
80 /* Anything else is a symbol that produces its address. */
81 value = p;
83 ++p;
84 while (*p != '\0' && !isspace (*p) && !ispunct (*p));
85 c = *p;
86 *p = '\0';
87 value = dlsym (NULL, value);
88 *p = c;
89 break;
92 *stringp = p;
93 return value;
97 void
98 _start (void)
100 char *buf = NULL;
101 size_t bufsz = 0;
103 while (getline (&buf, &bufsz, stdin) > 0)
105 char *p = buf;
106 eval (&p);
109 exit (0);