-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
138 lines (116 loc) · 3.74 KB
/
main.c
File metadata and controls
138 lines (116 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Shell.js (jssh), JavaScript shell
* Copyright (C) 2015 Yuchi (yuchi518@gmail.com)
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http://www.gnu.org/licenses>.
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
//
// Created by Yuchi on 2015/11/28.
//
#include <stdlib.h>
#include "common.h"
#include "v7.h"
#include "jsc_sys.h"
#include "jsc_file.h"
#include "jsc_net.h"
char *read_file(const char *path, size_t *size);
void print_err_and_res(enum v7_err err, v7_val_t result);
const char *errs_string[6] = {
[V7_OK] = "OK",
[V7_SYNTAX_ERROR] = "Syntax error",
[V7_EXEC_EXCEPTION] = "Exec exception",
[V7_STACK_OVERFLOW] = "Stack overflow",
[V7_AST_TOO_LARGE] = "AST to large",
[V7_INVALID_ARG] = "Invalid arguments",
};
void install_all_js_clibs(struct v7 *v7)
{
jsc_install_sys_lib(v7);
jsc_install_file_lib(v7);
jsc_install_net_lib(v7);
}
void uninstall_all_js_clibs(struct v7 *v7)
{
jsc_uninstall_net_lib(v7);
jsc_uninstall_file_lib(v7);
jsc_uninstall_sys_lib(v7);
}
int main(int argc, char *argv[]) {
enum v7_err err;
v7_val_t exec_result;
struct v7 *v7;
v7 = v7_create();
install_all_js_clibs(v7);
if (argc > 1)
{
int i;
for (i=1; i<argc; i++)
{
const char *js_path = argv[i];
size_t js_size = 0;
char *js_code = read_file(js_path, &js_size);
if (js_size > 2)
{
if (js_code[0] == '#' && js_code[1] == '!') {
js_code += 2;
// fine line tail
while (*js_code != '\0' && *js_code != '\n' && *js_code != '\r') js_code ++;
// find line head
while (*js_code != '\0' && (*js_code == '\n' || *js_code == '\r')) js_code ++;
}
err = v7_exec(v7, js_code, &exec_result);
if (err != V7_OK) print_err_and_res(err, exec_result);
}
}
}
else
{
char *js_string = NULL;
size_t js_string_len = 0;
printf("Shell.js 0.1\n>>> ");
while ((getline(&js_string, &js_string_len, stdin)) != -1) {
err = v7_exec(v7, js_string, &exec_result);
if (err != V7_OK) print_err_and_res(err, exec_result);
printf(">>> ");
}
free(js_string);
}
run_done();
uninstall_all_js_clibs(v7);
v7_destroy(v7);
return 0;
}
char *read_file(const char *path, size_t *size) {
FILE *fp;
char *data = NULL;
if ((fp = fopen(path, "rb")) == NULL) {
} else if (fseek(fp, 0, SEEK_END) != 0) {
fclose(fp);
} else {
*size = ftell(fp);
data = (char *) malloc(*size + 1);
if (data != NULL) {
fseek(fp, 0, SEEK_SET); /* Some platforms might not have rewind(), Oo */
if (fread(data, 1, *size, fp) != *size) {
free(data);
return NULL;
}
data[*size] = '\0';
}
fclose(fp);
}
return data;
}
void print_err_and_res(enum v7_err err, v7_val_t result)
{
printf("err: %s, result: %llx\n", errs_string[err], (long long int)result);
}