Line |
Branch |
Exec |
Source |
1 |
|
|
#include "script.h" |
2 |
|
|
#include <csignal> |
3 |
|
|
#include <cstdlib> |
4 |
|
|
#include <iostream> |
5 |
|
|
#include <string> |
6 |
|
|
|
7 |
|
|
volatile std::sig_atomic_t g_signal_status; |
8 |
|
|
|
9 |
|
✗ |
void signal_handler(int signal) |
10 |
|
|
{ |
11 |
|
✗ |
g_signal_status = signal; |
12 |
|
✗ |
} |
13 |
|
|
|
14 |
|
✗ |
inline void print_data(const char* msg) |
15 |
|
|
{ |
16 |
|
✗ |
std::cout << "\033[32m"; |
17 |
|
✗ |
std::cout << msg; |
18 |
|
✗ |
std::cout << "\033[0m"; |
19 |
|
✗ |
} |
20 |
|
|
|
21 |
|
✗ |
inline void print_error(const char* msg) |
22 |
|
|
{ |
23 |
|
✗ |
std::cout << "\033[31m"; |
24 |
|
✗ |
std::cout << msg << std::endl; |
25 |
|
✗ |
std::cout << "\033[0m"; |
26 |
|
✗ |
} |
27 |
|
|
|
28 |
|
✗ |
inline void print_success() |
29 |
|
|
{ |
30 |
|
✗ |
std::cout << "\033[34m"; |
31 |
|
✗ |
std::cout << " (success)"; |
32 |
|
✗ |
std::cout << "\033[0m\n"; |
33 |
|
✗ |
} |
34 |
|
|
|
35 |
|
✗ |
int main() |
36 |
|
|
{ |
37 |
|
✗ |
std::signal(SIGINT, signal_handler); |
38 |
|
|
|
39 |
|
✗ |
script::engine eng{print_data}; |
40 |
|
|
script::command cmd; |
41 |
|
✗ |
std::string operand; |
42 |
|
|
|
43 |
|
✗ |
std::cout << "Enter a text to process or \"exit\" to end the program:\n"; |
44 |
|
|
|
45 |
|
✗ |
std::string input; |
46 |
|
|
while (true) |
47 |
|
|
{ |
48 |
|
✗ |
if (g_signal_status != 0) |
49 |
|
✗ |
break; |
50 |
|
|
|
51 |
|
✗ |
std::getline(std::cin, input); |
52 |
|
|
|
53 |
|
✗ |
if (input == "exit") |
54 |
|
✗ |
break; |
55 |
|
|
|
56 |
|
✗ |
script::parse(input, cmd, operand); |
57 |
|
|
|
58 |
|
✗ |
if (cmd == script::command::INVALID) |
59 |
|
|
{ |
60 |
|
✗ |
print_error("invalid command"); |
61 |
|
✗ |
continue; |
62 |
|
|
} |
63 |
|
|
|
64 |
|
✗ |
const auto res = eng.run(cmd, operand); |
65 |
|
|
|
66 |
|
✗ |
if (!res.empty()) |
67 |
|
|
{ |
68 |
|
✗ |
print_error(res.c_str()); |
69 |
|
✗ |
continue; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
✗ |
print_success(); |
73 |
|
✗ |
} |
74 |
|
|
|
75 |
|
✗ |
return EXIT_SUCCESS; |
76 |
|
✗ |
} |
77 |
|
|
|