1#include <stdio.h>
2#include <stdlib.h>
3#include <semaphore.h>
4#include <pthread.h>
5#include <unistd.h>
6
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <sys/queue.h>
10
11#include "env.h"
12#include "scanner.h"
13#include "parser.h"
14#include "eval.h"
15#include "util.h"
16
17enum {
18 VARTBLSIZ = 512,
19};
20
21int
22evalstr(char *input)
23{
24 int r = 1;
25 env *vars;
26 evalerr ret;
27 statement **cmds, *err = emalloc(sizeof(statement));
28 parser *par;
29
30 par = newpar(input);
31 if (!(cmds = parseprog(par, err))) {
32 if (err->d.error.line > 0)
33 fprintf(stderr, "Parser error in line %d: %s\n",
34 err->d.error.line, err->d.error.msg);
35 else
36 fprintf(stderr, "Parser error: %s\n", err->d.error.msg);
37
38 freestmt(err);
39 goto err;
40 } else {
41 free(err);
42 }
43
44 vars = newenv(VARTBLSIZ);
45 if ((ret = eval(vars, cmds)) != EVAL_OK) {
46 fprintf(stderr, "Runtime error: %d\n", ret);
47 goto err;
48 }
49
50 r = 0;
51 freeenv(vars);
52
53err:
54 freestmts(cmds);
55 freepar(par);
56 return r;
57}
58
59int
60main(int argc, char **argv)
61{
62 int ret;
63 FILE *fd;
64 char *input, *fp = argv[1];
65 struct stat st;
66
67 if (argc <= 1)
68 return 1;
69
70 if (stat(fp, &st))
71 die("stat failed");
72 else
73 input = emalloc(st.st_size + 1);
74
75 if (!(fd = fopen(fp, "r")))
76 die("fopen failed");
77 if (fread(input, sizeof(char), st.st_size, fd) != st.st_size)
78 die("short read");
79
80 if (fclose(fd))
81 die("fclose failed");
82
83 input[st.st_size] = '\0';
84 ret = evalstr(input);
85
86 free(input);
87 return ret;
88}