-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
231 lines (216 loc) · 6.34 KB
/
main.cc
File metadata and controls
231 lines (216 loc) · 6.34 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "interpretor.h"
#include "deps/config/config_file.h"
#include <iostream>
#include <getopt.h>
void initregex();
void initable();
void loadconfig();
int bracecount(string&);
void help(char* prog){
cout<< prog << " FILE\n\tRun query from FILE\n"
<< prog << " version\n\tshow version and exit\n"
<< prog << " help\n\tshow this help message and exit\n"
<< prog << " \"select from 'data.csv'\"\n\tRun query from command line argument\n"
<< prog << " \"select from {}\" data.csv\n\tRun query from command line argument and substitute {} with the following argument\n"
<< prog << " \"select from\" ./data.csv\n\tRun query from concatinated command line arguments\n"
<< prog << "\n\tRun server to use graphic interface in web browser\n\n"
"Flags:\n"
"\t-b Don't automatically open webui in browser\n"
"\t-m Don't require comma between selections, but do require 'as' for result names\n"
"\t-g Don't show debug info in console\n"
"\t-d Show debug info in console (default)\n"
"\t-e Don't automatically exit 3 minutes after the browser page is closed\n"
"\t-t Output results in terminal as table instead of csv format\n"
"\t-c Output results in terminal as csv instead of table (default)\n"
"\t-y Same as -t but with background colors to help see lines\n"
"\t-w Same as -t but no colors\n"
"\t-j Print json to stdout (limited to first 20000 values)\n"
"\t-h Show this help message and exit\n"
"\t-v Show version and exit\n"
"\t-a Allow other computers to connect and use the browser interface\n"
"\t-l Only allow connections from this computer (default)\n"
"\t-f FILE Run a selectAll query on FILE and output table\n"
"\t-o FILE Save query to FILE\n\n"
"Config file is " << globalSettings.configfilepath << ". These are the settings you can change:\n"
"\tguess_file_header: first row is considered header if no value is a number\n"
"\topen_browser: opposite of -b\n"
"\tshow_debug_info: same as -d and -g\n"
"\texit_automatically: same as -e\n"
"\ttable_or_csv: same as -t and -c\n"
"\tneed_comma_separator: same as -m\n";
exit(0);
}
int main(int argc, char** argv){
bool jsonstdout = false;
string querystring;
string savefile;
loadconfig();
for(char c; (c = getopt(argc, argv, "bhvgdejtywcmalf:o:")) != -1;)
switch(c){
case 'o':
savefile = optarg;
break;
case 'g':
globalSettings.debug = false;
break;
case 'd':
globalSettings.debug = true;
break;
case 'e':
globalSettings.autoexit = false;
break;
case 'f':
globalSettings.termbox = true;
querystring = st('"',optarg,'"');
break;
case 't':
globalSettings.termbox = true;
break;
case 'y':
globalSettings.termbox = true;
globalSettings.tablelinebg = true;
break;
case 'w':
globalSettings.termbox = true;
globalSettings.tablecolor = false;
break;
case 'c':
globalSettings.termbox = false;
break;
case 'j':
jsonstdout = true;
globalSettings.termbox = false;
break;
case 'm':
globalSettings.needcomma = false;
break;
case 'l':
globalSettings.allowconnections = false;
break;
case 'a':
globalSettings.allowconnections = true;
break;
case 'b':
globalSettings.browser = false;
break;
case 'h':
help(argv[0]);
case 'v':
cout << version << endl;
exit(0);
}
initregex();
initable();
runmode = argc > optind ? RUN_SINGLE : RUN_SERVER;
auto arg1 = argv[optind];
try {
//run select * on -f arg
if (!querystring.empty()){
runmode = RUN_SINGLE;
for (int i=optind; i<argc; ++i){
querystring += ' ';
querystring += argv[i];
}
}
//run gui server and exit when done
else if (runmode == RUN_SERVER)
runServer();
//show version and exit
else if (!strcmp(arg1, "version")){
cout << version << endl;
return 0;
}
//show help and exit
else if (!strcmp(arg1, "help"))
help(argv[0]);
//get query from multiple args or substitute {}
else if (argc > optind+1){
querystring = argv[optind];
int bc = bracecount(querystring);
if (bc && (optind + bc != argc-1)){
auto s = bc>1 ? "s" : "";
error("Query with ",bc," '{}' pair",s," must have ",bc," matching argument",s,". Found ",argc-optind-1,'\n');
}
int end = argc - bc;
for (int i=optind+1; i<end; ++i){
querystring += ' ';
querystring += argv[i];
}
for (int i = end; i < end+bc; i++)
querystring = replace_first(querystring, "{}", st("'",argv[i],"'"));
}
//get query from file
else if (strlen(arg1) < 30 && fs_is_regular_file(arg1))
querystring = st(ifstream(arg1).rdbuf());
//get query from single arg
else {
querystring = string(arg1);
int bc = bracecount(querystring);
if (bc)
error("Query with ",bc," '{}' pair",(bc>1?"s":"")," must have ",bc," matching argument",(bc>1?"s":""),". Found 0\n");
}
querySpecs q(querystring, savefile);
if (jsonstdout){
cout << runJsonQuery(q)->tojson().str();
} else {
q.setoutputCsv();
runPlainQuery(q);
}
} catch (...) {
cerr << "Error: "<< EX_STRING << endl;
return 1;
}
return 0;
}
int bracecount(string& q){
int cnt = 0;
i64 idx = -1;
while ((idx = q.find("{}",idx+1)) > -1)
cnt++;
return cnt;
}
void loadconfig(){
vector<string> opts{
"show_debug_info",
"guess_file_header",
"exit_automatically",
"table_or_csv",
"need_comma_separator",
"allow_nonlocal_connections",
"open_browser",
};
string defaultoutput;
fs_create_directories(globalSettings.configdir);
if (fs_is_regular_file(globalSettings.configfilepath)){
ifstream cfile(globalSettings.configfilepath);
if (cfile.good()){
string confversion;
CFG::ReadFile(cfile, opts,
globalSettings.debug,
globalSettings.autoheader,
globalSettings.autoexit,
defaultoutput,
globalSettings.needcomma,
globalSettings.allowconnections,
globalSettings.browser);
if (defaultoutput == "table")
globalSettings.termbox = true;
if (confversion >= version){
return;
}
}
cfile.close();
defaultoutput = globalSettings.termbox ? "table":"csv";
} else {
defaultoutput = globalSettings.termbox ? "table":"csv";
ofstream cfile(globalSettings.configfilepath);
CFG::WriteFile(cfile, opts,
globalSettings.debug,
globalSettings.autoheader,
globalSettings.autoexit,
defaultoutput,
globalSettings.needcomma,
globalSettings.allowconnections,
globalSettings.browser);
}
}