-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (105 loc) · 3.03 KB
/
main.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
#include "engine/Engine.h"
#include "engine/sock.h"
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
bool readFile(const std::string& file, std::string& ret)
{
std::ifstream ifs(file);
if (!ifs)
{
std::cerr << "[FATAL] unable to open file '" << file << "'.\n";
return false;
}
std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
ret = content;
ifs.close();
return true;
}
int main(int argc, char **argv)
{
bool fflag = false; // -f file
char *file = NULL;
bool rflag = false; // -r addr -p port
char *ip = new char[40];
strcpy(ip, "127.0.0.1");
char *port = new char[40];
strcpy(port, "12345");
bool sflag = false; // -s local_port
char *sport = new char[40];
strcpy(sport, "12345");
opterr = 0;
char c;
while ((c = getopt(argc, argv, "s:f:r:p:h")) != -1) {
switch (c) {
case 'h':
fprintf(stderr, "Usage: %s [-s port | -r addr -p port | -f file | -h]\n", argv[0]);
fprintf(stderr, "Options:\n");
fprintf(stderr, "\t-s start as server\n");
fprintf(stderr, "\t-r connect remote server (ip)\n");
fprintf(stderr, "\t-p connect remote server (port)\n");
fprintf(stderr, "\t-f specify input SQL file\n");
fprintf(stderr, "To launch interactive terminal, run without any options.\n");
exit(1);
case 's':
sflag = true;
sport = optarg;
break;
case 'f':
fflag = true;
file = optarg;
break;
case 'r':
rflag = true;
ip = optarg;
break;
case 'p':
rflag = true;
port = optarg;
break;
case '?':
if (optopt == 'f' || optopt == 'r' || optopt == 's') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
} else if(isprint (optopt)) {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
} else {
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
}
exit(1);
default:
exit(1);
}
}
MyBitMap::initConst();
if (sflag) { // start server
runServer(atoi(sport));
return 0;
} else if (rflag) { // start client
int sock = connServer(ip, atoi(port));
if (sock < 0) {
return 1;
}
sendCMD(sock);
return 0;
} else { // local
if (fflag) { // from file
std::string input;
std::string path(file);
if (readFile(path, input)) {
StringEngine se(input);
se.setFile(path);
se.run();
return 0;
}
} else { // interactive
InteractiveEngine ie;
ie.run();
return 0;
}
}
return 0;
}