-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (182 loc) · 5.15 KB
/
main.cpp
File metadata and controls
199 lines (182 loc) · 5.15 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
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
/*
Copyright (c), 2016-2021 mwiede Software
Name of the Project: V1 Script
Author(s): Matthias Wiede, mwiede@mwiede.de
Licence: GPL V3
*/
#include "wscript.h"
#include "ast.h"
#include <sys/resource.h>
using namespace std;
extern int g_programMode;
#ifdef _LOG_PERFORMANCE
W64 g_timerCounterStart;
W64 g_timerCounter;
W64 g_timerFreq;
WFile g_performanceLogFile;
#endif
int main(int argc, char *argv[], char *envp[])
{
int ret = 0;
try {
// dup2(1, 2); // Redirect stderr to stdout
int argIdx = 0;
bool fHelp = true;
bool fEval = false;
bool fFork = false;
bool fRedirectOutput = false;
// int priority = 0;
WString eval, outputFilename;
WCSTR filename = NULL;
InterpreterContext ctx;
ctx.ctx = &ctx;
// Startup
wScriptStartup(ctx);
// Init arguments
DataValue datavalue;
datavalue.datatype = DataValue::DATATYPE_ARRAY;
datavalue.flags = DataValue::FLAGS_NOWRITE;
ctx.symbols[0]->put("argv", datavalue);
// Read params
for (int i = 0; i < argc; i++) {
const char *currArg = argv[i];
if (i == 0 || filename != NULL) {
// Save arg
DataValue datavalue = (WCSTR)currArg;
ctx.symbols[0]->get("argv").arrayList.put(WFormattedString("%u", argIdx++), datavalue)->m_value.flags |= DataValue::FLAGS_NOWRITE;
continue;
}
if (fEval) {
if (eval.length() > 0)
eval.append(" ");
eval.append((WCSTR)currArg);
continue;
}
if (fRedirectOutput) {
if (outputFilename.length() == 0) {
outputFilename = (WCSTR)currArg;
continue;
}
}
if (currArg[0] == '-') {
switch (currArg[1]) {
/*
case 'b':
fFork = true; fHelp = false;
break;
*/
case 'h':
fHelp = true;
break;
case 'v':
fHelp = false;
printf("%g\r\n", WSCRIPT_VERSION);
break;
case 'l':
fHelp = false;
ctx.fOnlySyntaxCheck = true;
break;
/*
case 'p':
if ((i+1)<argc) {
i++;
priority = _wtoi (argv[i]);
switch (priority) {
case -2: priority = IDLE_PRIORITY_CLASS; break;
case -1: priority = BELOW_NORMAL_PRIORITY_CLASS; break;
case 0: priority = NORMAL_PRIORITY_CLASS; break;
case 1: priority = ABOVE_NORMAL_PRIORITY_CLASS; break;
case 2: priority = HIGH_PRIORITY_CLASS; break;
case 3: priority = REALTIME_PRIORITY_CLASS; break;
default:
printf ("Illegal process priority: %i\r\n", priority);
exit (1);
break;
}
SetPriorityClass (GetCurrentProcess(), priority);
}
else {
printf ("Process priority required.\r\n");
exit (1);
}
break;
*/
case 'r':
fHelp = false;
fEval = true;
break;
case 'o':
fHelp = false;
fRedirectOutput = true;
break;
case 'w':
fHelp = false;
ctx.programMode = g_programMode = InterpreterContext::PROGRAM_MODE_WEB;
ctx.fAutoNL = false;
wScriptInitCGIVariables(ctx);
break;
case '-':
if (strcmp(currArg, "--help") == 0) {
fHelp = true;
break;
}
default:
printf("Unknown option: %s\r\n", currArg);
exit(1);
break;
}
}
else if (filename == NULL) {
// Save arg
DataValue datavalue = DataValue((WCSTR)currArg);
ArrayHT::WHash *hash = ctx.symbols[0]->get("argv").arrayList.put(WFormattedString("%u", argIdx++), datavalue);
filename = hash->m_value.value.c_str();
hash->m_value.flags |= DataValue::FLAGS_NOWRITE;
fHelp = false;
}
}
if (fHelp) {
printf(
"V1 Script Language %g\r\n"
"https://v1-script.net\n\r\n"
"Usage:\r\n\r\nv1 [options] <filename>\r\n\r\n"
"Options:\r\n\r\n" /* "-b\t\tRun in background (hide window)\r\n"\ */
"-l\t\tSyntax check only (lint)\r\n"
"-o <file>\tPrint and append output to file\r\n" /* "-p <priority>\tProcess priority: -2=IDLE .. 0=Normal .. 3=Realtime\r\n"\ */
"-r <code>\tRun code without <?v1 ?> tags\r\n"
"-v\t\tShow version\r\n"
"-w\t\tWeb mode (CGI)\r\n\r\n",
WSCRIPT_VERSION);
exit(0);
}
if (fRedirectOutput) {
if (!std::freopen((WCSTR)outputFilename, "a+", stdout)) {
printf("Cannot create output file: %s\r\n", (WCSTR)outputFilename);
exit(1);
}
std::freopen((WCSTR)outputFilename, "a+", stderr);
}
if (filename != NULL) {
ret = ctx.execute(filename, 2);
}
else if (fEval) {
DataValue retvalue;
ret = ctx.eval(eval, eval.length(), retvalue);
}
// Print Headers in Web/CGI Mode finally!
ctx.printHeaders();
wScriptShutdown(ctx);
}
catch (WException &e) {
fprintf(stdout, "Fatal error: %s\r\n", (WCSTR)e.toString());
ret = -1;
}
fflush(stdout);
#ifdef _LOG_PERFORMANCE
W64 counter;
WSystem::getPerformanceCounter(counter);
double tDStart = (double)(counter - g_timerCounterStart) / (double)g_timerFreq;
g_performanceLogFile.writeln(WFormattedString("Complete script time: %.6f sec", tDStart));
#endif
return ret;
}