forked from xlang-foundation/xlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
X.cpp
200 lines (191 loc) · 4.3 KB
/
X.cpp
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
// LitePy.cpp : Defines the entry point for the application.
//
#include <signal.h>
#include <vector>
#include <string>
#include <cstring>
#include "X.h"
#include "xload.h"
#include "cli.h"
#if (WIN32)
#define Path_Sep_S "\\"
#define Path_Sep '\\'
#else
#include <string.h> //for memcpy
#define Path_Sep_S "/"
#define Path_Sep '/'
#endif
struct ParamConfig
{
X::Config config;
bool print_usage = false;//-help |-? |-h
bool cli = false;
};
X::XLoad g_xLoad;
void signal_callback_handler(int signum)
{
X::AppEventCode code = g_xLoad.HandleAppEvent(signum);
if (code == X::AppEventCode::Exit)
{
exit(signum);
}
signal(SIGINT, signal_callback_handler);
}
void PrintUsage()
{
std::cout <<
"xlang [-dbg] [-enable_python|-python] \n\
[-run_as_backend|-backend] [-event_loop]\n\
[-c \"code,use \\n as line separator\"]\n\
[-cli]\n\
[file parameters]" << std::endl;
std::cout << "xlang -help | -? | -h for help" << std::endl;
}
bool ParseCommandLine(std::vector<std::string>& params, ParamConfig& paramCfg)
{
//first one is exe file name with path
std::string& progName = params[0];
auto pos = progName.rfind(Path_Sep);
if (pos != progName.npos)
{
std::string strAppPath = progName.substr(0, pos);
paramCfg.config.appPath = new char[strAppPath.length() + 1];
memcpy((char*)paramCfg.config.appPath, strAppPath.data(), strAppPath.length() + 1);
}
paramCfg.config.appFullName = new char[progName.length() + 1];
memcpy((char*)paramCfg.config.appFullName, progName.data(), progName.length() + 1);
if (params.size() == 1)
{
paramCfg.print_usage = true;
return true;
}
std::string strPassInParams;
int i = 1;
bool starFile = false;
while(i< (int)params.size())
{
std::string& s = params[i];
if (s.size() == 0)
{
continue;
}
if (!starFile && s[0] == '-')
{
if (s == "-c")
{//pass code as string
i++;
if (i < (int)params.size())
{
auto& s_i = params[i];
paramCfg.config.inlineCode = new char[s_i.length() + 1];
memcpy((char*)paramCfg.config.inlineCode, s_i.data(), s_i.length() + 1);
i++;
}
}
else if (s == "-help" || s == "-?" || s == "-h" || s == "-H")
{
paramCfg.print_usage = true;
i++;
}
else if (s.find("-c ")==0)
{//pass code as string
auto s_i = params[i].substr(2);
paramCfg.config.inlineCode = new char[s_i.length() + 1];
memcpy((char*)paramCfg.config.inlineCode, s_i.data(), s_i.length() + 1);
i++;
}
else if (s == "-dbg")
{
paramCfg.config.dbg = true;
i++;
}
else if (s == "-cli")
{
paramCfg.cli = true;
i++;
}
else if (s == "-enable_python" || s == "-python")
{
paramCfg.config.enablePython = true;
i++;
}
else if (s == "-enable_python_debug" || s == "-python_debug")
{
paramCfg.config.enablePythonDebug = true;
i++;
}
else if (s == "-run_as_backend" || s == "-backend")
{
paramCfg.config.runAsBackend = true;
i++;
}
else if (s == "-event_loop")
{
paramCfg.config.enterEventLoop = true;
i++;
}
}
else if(!starFile)
{
starFile = true;
//first one is file name
paramCfg.config.fileName = new char[s.length() + 1];
memcpy((char*)paramCfg.config.fileName, s.data(), s.length() + 1);
i++;
}
else
{//parse passIn Params after file name
if (strPassInParams.empty())
{
strPassInParams = s;
}
else
{
strPassInParams += "\n"+s;
}
i++;
}
}
if (!strPassInParams.empty())
{
paramCfg.config.passInParams = new char[strPassInParams.length() + 1];
memcpy((char*)paramCfg.config.passInParams, strPassInParams.data(), strPassInParams.length() + 1);
}
return true;
}
#include <thread>
void func()
{
}
void Workaround_WSLThread_Problem()
{
static std::vector<std::thread> threads_;
threads_.emplace_back(func);
}
//#include <Windows.h>
int main(int argc, char* argv[])
{
//::MessageBox(NULL, "In Dbg", "XLang", MB_OK);
//Workaround_WSLThread_Problem();
std::vector<std::string> params(argv, argv+argc);
ParamConfig paramConfig;
ParseCommandLine(params, paramConfig);
if (paramConfig.print_usage)
{
PrintUsage();
return 0;
}
signal(SIGINT, signal_callback_handler);
int retCode = g_xLoad.Load(¶mConfig.config);
if (retCode == 0)
{
retCode = g_xLoad.Run();
if (paramConfig.cli)
{
X::CLI cli;
cli.MainLoop();
}
g_xLoad.Unload();
}
return retCode;
}