-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
312 lines (269 loc) · 8.92 KB
/
main.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* main.c
* (C) 2013 Christian Gunderman
* Modified by:
* Author Email: [email protected]
* Modifier Email:
*
* Description:
* The Gunderscript executable application. This application can be used
* as a complete standalone Gunderscript environment, however, it is a
* bit rudimentary and is meant more as an example of how easy it is to
* embed Gunderscript in C projects.
*
* This program 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.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <unistd.h>
#include "gunderscript.h"
#define GXSMAIN_BUILD_SCRIPT "build-script"
#define GXSMAIN_RUN_SCRIPT "run-script"
#define GXSMAIN_RUN_BYTECODE "run-bytecode"
#define GXSMAIN_DEFAULT_MAIN "main"
#define GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN 255
static const size_t stackSize = 100000; /* environment constants */
static const int callbacksSize = 55;
/**
* Prints a brief help message
*/
static void print_help() {
printf("Gunderscript Scripting Environment ");
#ifdef _WIN64
printf("- Win64 Build\n");
#elif _WIN32
printf("- Win32 Build\n");
#elif __linux__
#ifdef __LP64__
printf("- Linux64 Build\n");
#else
printf("- Linux32 Build\n");
#endif /* defined(__LP64__) */
#else
printf("- Unknown Platform;\n");
#endif /* defined(__linux__) */
printf("(C) 2013-2014 Christian Gunderman\n");
printf("http://github.com/gundermanc/gunderscript\n");
#if defined( __DATE__) && defined(__TIME__)
printf("Built on %s at %s\n\n", __DATE__, __TIME__);
#else
printf("Build date unavaiable; Not compiled with GCC;\n\n");
#endif /* defined(__linux__) */
printf("Usage: gunderscript [operation] ... \n");
printf(" Operations:\n");
printf(" build-script [script.gxs] [outputfile.gxb] \n");
printf(" builds a script and outputs the bytecode to specified file.\n");
printf(" run-script [entrypoint] [script.gxs] \n");
printf(" compiles and runs a script from the specfied \"entrypoint\" function.\n");
printf(" run-bytecode [entrypoint] [bytecode.gxb] \n");
printf(" runs a bytecode file generated with \"build-script\" \n\n\n");
printf("Autoexecute:\n");
printf(" Scripts will autoexecute if they are named the same as their copy of Gunderscript.\n");
printf(" For example:\n");
printf("\n If your Gunderscript was called foobar (Linux) or foobar.exe (Windows), then\n");
printf(" your program could be called foobar.gxs (uncompiled script), or\n");
printf(" foobar.gxb (compiled bytecode).");
}
/**
* Memory allocation failed
*/
static void print_alloc_error() {
printf("Error allocating memory for Gunderscript object.");
}
/**
* Print relevant Gunderscript error messages
*/
static void print_error(Gunderscript * ginst) {
if(gunderscript_get_err(ginst) == GUNDERSCRIPTERR_BUILDERR) {
/* compiler.c errors */
printf("\n\nError encountered building script(s)\n");
printf("Build Error Number: %i\n", gunderscript_build_err(ginst));
printf("Detected around Line Number: %i\n", gunderscript_err_line(ginst));
printf("Compiler Error: %s\n", gunderscript_err_message(ginst));
} else if(gunderscript_get_err(ginst) == GUNDERSCRIPTERR_EXECERR) {
/* vm.c and dependencies errors */
printf("\n\nError encountered executing generated bytecode\n");
printf("Virtual Machine Error Number: %i\n", gunderscript_function_err(ginst));
printf("Virtual Machine Error: %s\n", gunderscript_err_message(ginst));
} else {
/* gunderscript.c errors */
printf("\n\nError encountered setting up environment\n");
printf("Setup Error: %s\n", gunderscript_err_message(ginst));
}
}
/**
* Moves the NULL character to remove a program extension, if there is one
*/
static void remove_suffix(char * str) {
size_t len = strlen(str);
int i = 0;
if(len == 0) {
return;
}
/* iterate string right to left */
for(i = len-1; i > 0; i--) {
if(str[i] == '.') {
break;
}
}
/* if no string extension, return */
if(i == 0) {
return;
}
str[i] = '\0';
}
/**
* Execute the script named the same as this executable
* TODO: this logic in this whole file is really messy, buuuttt, I'm lazy
* and the library itself is beautiful so I am going to be lazy and just
* leave this here..lolz
*/
static int execute_associated(Gunderscript * ginst, int argc, char * argv[]) {
char fileName[GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN];
/* does [executable].gxs exist (the associated script) */
strncpy(fileName, argv[0], GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN);
remove_suffix(fileName);
strncat(fileName, ".gxs", GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN - 4);
if(access(fileName, F_OK) == 0) {
/* initialize gunderscript object */
if(!gunderscript_new_full(ginst, stackSize, callbacksSize)) {
print_alloc_error();
return 1;
}
/* compile the input script */
if(!gunderscript_build_file(ginst, fileName)) {
print_error(ginst);
gunderscript_free(ginst);
return 1;
}
/* execute the desired entry point */
if(!gunderscript_function(ginst, GXSMAIN_DEFAULT_MAIN, strlen(GXSMAIN_DEFAULT_MAIN))) {
print_error(ginst);
gunderscript_free(ginst);
return 1;
}
gunderscript_free(ginst);
return 0;
} else {
/* the script file doesn't exist, check for a bytecode file instead */
strncpy(fileName, argv[0], GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN);
remove_suffix(fileName);
strncat(fileName, ".gxb", GXSMAIN_MAX_ASSOCIATED_SCRIPT_FN - 4);
if(access(fileName, F_OK) == 0) {
/* initialize gunderscript object */
if(!gunderscript_new_vm(ginst, stackSize, callbacksSize)) {
print_alloc_error();
return 1;
}
/* import the compiled code from a BIN file */
if(!gunderscript_import_bytecode(ginst, fileName)) {
print_error(ginst);
gunderscript_free(ginst);
return 1;
}
/* execute the desired entry point */
if(!gunderscript_function(ginst, GXSMAIN_DEFAULT_MAIN, strlen(GXSMAIN_DEFAULT_MAIN))) {
print_error(ginst);
gunderscript_free(ginst);
return 1;
}
gunderscript_free(ginst);
return 0;
} else {
print_help();
return 1;
}
}
}
/**
* Program entry point. For a summary of command-line arguments, see
* print_help() or read the code, silly.
*/
int main(int argc, char * argv[]) {
Gunderscript ginst;
/* if no arguments, try to execute associated bytecode / script */
if(argc == 1) {
return execute_associated(&ginst, argc, argv);
}
/* check for proper number of arguments */
if(argc != 4) {
print_help();
return 1;
}
/* build script */
if(strcmp(argv[1], GXSMAIN_BUILD_SCRIPT) == 0) {
/* initialize gunderscript object */
if(!gunderscript_new_full(&ginst, stackSize, callbacksSize)) {
print_alloc_error();
return 1;
}
/* compile the input script */
if(!gunderscript_build_file(&ginst, argv[2])) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
/* export the compiled code to a BIN file */
if(!gunderscript_export_bytecode(&ginst, argv[3])) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
gunderscript_free(&ginst);
return 0;
} else if(strcmp(argv[1], GXSMAIN_RUN_SCRIPT) == 0) {
/* initialize gunderscript object */
if(!gunderscript_new_full(&ginst, stackSize, callbacksSize)) {
print_alloc_error();
return 1;
}
/* compile the input script */
if(!gunderscript_build_file(&ginst, argv[3])) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
/* execute the desired entry point */
if(!gunderscript_function(&ginst, argv[2], strlen(argv[2]))) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
gunderscript_free(&ginst);
return 0;
} else if(strcmp(argv[1], GXSMAIN_RUN_BYTECODE) == 0) {
/* initialize gunderscript object */
if(!gunderscript_new_vm(&ginst, stackSize, callbacksSize)) {
print_alloc_error();
return 1;
}
/* import the compiled code from a BIN file */
if(!gunderscript_import_bytecode(&ginst, argv[3])) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
/* execute the desired entry point */
if(!gunderscript_function(&ginst, argv[2], strlen(argv[2]))) {
print_error(&ginst);
gunderscript_free(&ginst);
return 1;
}
gunderscript_free(&ginst);
return 0;
} else {
print_help();
return 1;
}
return 0;
}