-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
169 lines (145 loc) · 4.88 KB
/
main.cpp
File metadata and controls
169 lines (145 loc) · 4.88 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
/*
Copyright (c) 2025, 2026 acrion innovations GmbH
Authors: Stefan Zipproth, s.zipproth@acrion.ch
This file is part of zelph, see https://github.com/acrion/zelph and https://zelph.org
zelph is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
AGPL licensing:
zelph is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
zelph 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with zelph. If not, see <https://www.gnu.org/licenses/>.
*/
#include "interactive.hpp"
#include "versions.hpp"
#ifdef _WIN32
#include <Windows.h> // for SetConsoleOutputCP
#include <fcntl.h> // for _O_U16TEXT
#include <io.h> // for _setmode
#include <stdio.h> // for _fileno
#else
#include <sys/types.h> // for fork
#include <sys/wait.h> // for waitpid
#include <unistd.h> // for execvp, getenv
#endif
#include <iostream>
#include <vector>
using namespace zelph::console;
Interactive interactive;
int main(int argc, char** argv)
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
#elif defined(NDEBUG)
if (getenv("ZELPH_NO_RLWRAP") == nullptr)
{
FILE* pipe = popen("command -v rlwrap", "r");
if (pipe)
{
char buffer[128];
std::string rlwrap_path;
while (fgets(buffer, sizeof(buffer), pipe) != nullptr)
{
rlwrap_path += buffer;
}
int status = pclose(pipe);
if (status == 0 && !rlwrap_path.empty())
{
setenv("ZELPH_NO_RLWRAP", "1", 1);
std::vector<char*> exec_args;
exec_args.push_back(const_cast<char*>("rlwrap"));
exec_args.push_back(const_cast<char*>("-m"));
exec_args.push_back(argv[0]);
for (int i = 1; i < argc; ++i)
{
exec_args.push_back(argv[i]);
}
exec_args.push_back(nullptr);
execvp("rlwrap", exec_args.data());
perror("execvp failed");
return 1;
}
}
}
#endif
try
{
std::vector<std::string> script_files;
bool show_version = false;
std::vector<std::string> script_args;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "-v" || arg == "--version")
{
show_version = true;
}
else if (script_files.empty())
{
script_files.push_back(arg);
}
else
{
script_args.push_back(arg);
}
}
if (show_version)
{
std::cout << zelph::get_version_description() << std::endl;
return 0;
}
for (const auto& file : script_files)
{
interactive.process_file(file, script_args);
}
if (script_files.empty())
{
std::string exit_command = ".exit";
interactive.out("zelph " + zelph::console::Interactive::get_version());
interactive.out("-- REPL mode - type .help for commands, " + exit_command + " to exit --");
interactive.out("");
auto make_prompt = [&]() -> std::string
{
if (interactive.is_accumulating())
return "";
else
return interactive.get_lang()
+ (interactive.is_auto_run_active() ? "> " : "-> ");
};
interactive.prompt(make_prompt(), false);
std::string line;
while (std::getline(std::cin, line))
{
if (line == exit_command)
break;
if (line.empty())
{
interactive.out("type .help for help --");
interactive.prompt(make_prompt(), false);
continue;
}
try
{
interactive.process(line);
}
catch (const std::exception& e)
{
interactive.err(e.what());
}
interactive.prompt(make_prompt(), false);
}
interactive.out("");
}
}
catch (std::exception& ex)
{
interactive.err(ex.what());
}
return 0;
}