-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
115 lines (109 loc) · 4.02 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
/* Main program for JSI */
#include <stdlib.h>
#include <string.h>
#ifdef VALUE_DEBUG
#include "jsiInt.h"
#else
#include "jsi.h"
#endif
static int deleted = 0, exitCode = 0;
static int InterpDelete(Jsi_Interp *interp, void *ptr) {
exitCode = (int)ptr;
deleted = 1;
return JSI_OK;
}
int main(int argc, char **argv)
{
int rc = JSI_OK, jsFound = 0;
Jsi_Interp* interp = Jsi_InterpCreate(NULL, argc, argv, 0);
Jsi_InterpOnDelete(interp, &InterpDelete);
#ifndef NO_JAZ
/* Mount zip at end of executable */
Jsi_Value *v = Jsi_Executable(interp);
const char *exeFile = Jsi_ValueString(interp, v, NULL);
if (argc != 2 || Jsi_Strcmp(argv[1], "--nozvfs")) {
rc = Jsi_ExecZip(interp, exeFile, JSI_ZVFS_DIR, &jsFound);
if (rc >= 0) {
if (!jsFound) {
#if (!defined(JSI_OMIT_FILESYS)) && (!defined(JSI_OMIT_ZVFS))
fprintf(stderr, "warning: no main.jsi or jsiIndex.jsi\n");
#endif
}
if (deleted)
exit(exitCode);
else if (rc != JSI_OK) {
fprintf(stderr, "Error\n");
exit(1);
}
}
}
#endif
#ifdef USER_EXTENSION
extern int USER_EXTENSION(Jsi_Interp *interp);
if (USER_EXTENSION (interp) != JSI_OK) {
fprintf(stderr, "extension load failed");
exit(1);
}
#endif
Jsi_ShiftArgs(interp);
if (argc == 1) {
rc = Jsi_Interactive(interp, JSI_OUTPUT_QUOTE|JSI_OUTPUT_NEWLINES);
} else {
if (argc == 2 && (Jsi_Strcmp(argv[1], "--help")==0 || Jsi_Strcmp(argv[1], "-h" )==0)) {
puts("usage: jsi ?--help | -e SCRIPT | FILE arg arg ...?");
exit(0);
}
if (argc == 2 && (Jsi_Strcmp(argv[1], "--version")==0 || Jsi_Strcmp(argv[1], "-v" )==0)) {
char str[200] = "\n";
Jsi_Channel chan = Jsi_Open(interp, Jsi_ValueNewStringKey(interp, "/zvfs/lib/sourceid.txt"), "r");
if (chan)
Jsi_Read(chan, str, sizeof(str));
printf("%d.%d.%d (%" JSI_NUMGFMT ") %s", JSI_VERSION_MAJOR, JSI_VERSION_MINOR, JSI_VERSION_RELEASE, Jsi_Version(), str);
exit(0);
}
if (argc > 2 && (Jsi_Strcmp(argv[1], "--invoke")==0 || Jsi_Strcmp(argv[1], "-i" )==0)) {
Jsi_DString dStr = {};
Jsi_DSAppend(&dStr, "jsi_invokeCmd(\"", argv[2], "\",console.args.slice(1));", NULL);
rc = Jsi_EvalString(interp, Jsi_DSValue(&dStr), JSI_EVAL_NOSKIPBANG);
Jsi_DSFree(&dStr);
}
else if (argc == 3 && (Jsi_Strcmp(argv[1], "--eval")==0 || Jsi_Strcmp(argv[1], "-e" )==0))
rc = Jsi_EvalString(interp, argv[2], JSI_EVAL_NOSKIPBANG);
else {
Jsi_Value *vf = NULL;
const char *ext = strrchr(argv[1], '.');
/* Support running "main.jsi" from a zip file. */
if (ext && (strcmp(ext,".zip")==0 ||strcmp(ext,".jsz")==0 ) ) {
rc = Jsi_ExecZip(interp, argv[1], NULL, &jsFound);
if (rc<0) {
fprintf(stderr, "zip mount failed\n");
exit(1);
}
if (!(jsFound&JSI_ZIP_MAIN)) {
fprintf(stderr, "main.jsi not found\n");
exit(1);
}
} else {
if (argc>1)
vf = Jsi_ValueNewStringKey(interp, argv[1]);
rc = Jsi_EvalFile(interp, vf, JSI_EVAL_ARGV0|JSI_EVAL_INDEX);
}
}
if (deleted)
exit(exitCode);
if (rc == 0) {
/* Skip output from an ending semicolon which evaluates to undefined */
Jsi_Value *ret = Jsi_ReturnValue(interp);
if (!Jsi_ValueIsType(interp, ret, JSI_VT_UNDEF)) {
Jsi_DString dStr = {};
fputs(Jsi_ValueGetDString(interp, ret, &dStr, 0), stdout);
Jsi_DSFree(&dStr);
fputs("\n", stdout);
}
} else
fputs("ERROR\n", stderr);
}
if (!deleted)
Jsi_InterpDelete(interp);
return rc;
}