1 | #include <iostream> |
---|
2 | #include <vector> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <getopt.h> |
---|
5 | #include <fstream> |
---|
6 | #include "wrequest.h" |
---|
7 | #include "shellenv.h" |
---|
8 | #include "stopos.h" |
---|
9 | |
---|
10 | struct returnvalue |
---|
11 | { |
---|
12 | std::string msg; |
---|
13 | std::string key; |
---|
14 | std::string comm; |
---|
15 | std::string count; |
---|
16 | std::string present; |
---|
17 | std::string present0; |
---|
18 | std::string value; |
---|
19 | |
---|
20 | void parse(std::string &s) |
---|
21 | { |
---|
22 | // we expect somewhere in the output of the server a line like |
---|
23 | // STOPOS:/OK/123/beautiful |
---|
24 | // OK -> msg |
---|
25 | // 123 -> key |
---|
26 | // 1 -> comm |
---|
27 | // beautiful -> value |
---|
28 | // |
---|
29 | // OK, 123 and beautiful are hex coded |
---|
30 | this->msg = ""; |
---|
31 | this->key = ""; |
---|
32 | this->comm = "0"; |
---|
33 | this->count = "0"; |
---|
34 | this->present = "0"; |
---|
35 | this->present0 = "0"; |
---|
36 | this->value = ""; |
---|
37 | |
---|
38 | size_t p = s.find("STOPOS:"); |
---|
39 | if (p == s.npos) |
---|
40 | return; |
---|
41 | |
---|
42 | size_t q = s.find('\n',p+1); |
---|
43 | std::string l = s.substr(p,q-p); |
---|
44 | std::vector <std::string> v; |
---|
45 | |
---|
46 | split(v,l,outsep); |
---|
47 | std::cerr << "WTD:"<<__LINE__<<":"<<l<<std::endl; |
---|
48 | for (unsigned int i=1; i<v.size(); i++) |
---|
49 | { |
---|
50 | switch (i) |
---|
51 | { |
---|
52 | case 1: this->msg = zhextostr(v[i]); break; |
---|
53 | case 2: this->key = zhextostr(v[i]); break; |
---|
54 | case 3: this->comm = zhextostr(v[i]); break; |
---|
55 | case 4: this->count = zhextostr(v[i]); break; |
---|
56 | case 5: this->present = zhextostr(v[i]); break; |
---|
57 | case 6: this->present0 = zhextostr(v[i]); break; |
---|
58 | case 7: this->value = zhextostr(v[i]); break; |
---|
59 | } |
---|
60 | // special case for key: if key == "", we try to take |
---|
61 | // it from the environment. |
---|
62 | if (trim(this->key) == "") |
---|
63 | this->key = envtostr("STOPOS_KEY"); |
---|
64 | } |
---|
65 | } |
---|
66 | }; |
---|
67 | |
---|
68 | void usage() |
---|
69 | { |
---|
70 | std::cerr << "Usage:" << std::endl; |
---|
71 | |
---|
72 | std::cerr << program_name << " -h,--help : this message" << std::endl; |
---|
73 | std::cerr << program_name << " -c,--create [-p,--pool POOL] [-i,--input INPUTFILE]" << std::endl; |
---|
74 | std::cerr << program_name << " -n,--next [-p,--pool POOL] [-d --lockdir LOCKDIR ] [-k,--key KEY] [-m,--max MAX]" << std::endl; |
---|
75 | std::cerr << program_name << " -r,--ready [-p,--pool POOL] [-d --lockdir LOCKDIR ] [-k,--key KEY]" << std::endl; |
---|
76 | std::cerr << program_name << " -s,--stats [-p,--pool POOL] [-d --lockdir LOCKDIR ]" << std::endl; |
---|
77 | std::cerr << program_name << " -u,--unhandled [-p,--pool POOL] [-d --lockdir LOCKDIR ]" << std::endl; |
---|
78 | std::cerr << program_name << " -v,--version" << std::endl; |
---|
79 | std::cerr << "Defaults:" << std::endl; |
---|
80 | std::cerr << " POOL : " << "d.defpath_pool" << std::endl; |
---|
81 | std::cerr << " LOCKDIR : " << "d.defpath_lockdir" << std::endl; |
---|
82 | std::cerr << " MAX : " << "1" << std::endl; |
---|
83 | std::cerr << " INPUTFILE: " << "d.definputfilename"; |
---|
84 | //if ("d.definputfilename" == "-") |
---|
85 | // std::cerr << " (standard input)"; |
---|
86 | std::cerr << std::endl; |
---|
87 | } |
---|
88 | |
---|
89 | int handle_add(wrequest &w,const std::string &fname,returnvalue &r) |
---|
90 | { |
---|
91 | |
---|
92 | int rc; |
---|
93 | std::ifstream inp; |
---|
94 | std::istream *pinp; |
---|
95 | if(fname == "") // read from stdin |
---|
96 | pinp = &std::cin; |
---|
97 | else |
---|
98 | { |
---|
99 | inp.open(fname.c_str()); |
---|
100 | if (!inp.is_open()) |
---|
101 | { |
---|
102 | r.msg = "ERROR: Cannot open '"+fname+"'"; |
---|
103 | return 1; |
---|
104 | } |
---|
105 | pinp = &inp; |
---|
106 | } |
---|
107 | std::string line; |
---|
108 | size_t n = 0; |
---|
109 | w.set_command("add"); |
---|
110 | std::string body,header; |
---|
111 | while (getline(*pinp,line)) |
---|
112 | { |
---|
113 | std::cerr << "WTD:" << __FILE__<<":"<<__LINE__ << "line:'" << line <<"'" << std::endl; |
---|
114 | w.set_value(line); |
---|
115 | rc = w.send_message(body,header); |
---|
116 | std::cerr << "WTD:" << __FILE__<<":"<<__LINE__ << "body" << std::endl << body << std::endl << "/body\n"; |
---|
117 | if (rc != 0) |
---|
118 | { |
---|
119 | r.msg = "ERROR: Cannot connect to server"; |
---|
120 | return 1; |
---|
121 | } |
---|
122 | r.parse(body); |
---|
123 | if (r.msg != "OK") |
---|
124 | break; |
---|
125 | n++; |
---|
126 | } |
---|
127 | std::cerr << program_name << ": Number of lines added = "<<n<<std::endl; |
---|
128 | if (r.msg != "OK") |
---|
129 | return 1; |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | int main(int argc, char*argv[]) |
---|
134 | { |
---|
135 | std::cerr << "start stoposclient ...\n"; |
---|
136 | wrequest w; |
---|
137 | |
---|
138 | int rc; |
---|
139 | |
---|
140 | rc = w.read_config(); |
---|
141 | if (rc != 0 || w.get_config().size() != w.config_size) |
---|
142 | { |
---|
143 | std::cerr << "No valid config file found, creating it ..." << std::endl; |
---|
144 | rc = w.write_config(); |
---|
145 | if (rc != 0) |
---|
146 | { |
---|
147 | std::cerr << "Cannot write config file, exiting." << std::endl; |
---|
148 | return 1; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | bool addflag=0; |
---|
153 | std::string inputfilename; |
---|
154 | std::vector <std::string> parms; |
---|
155 | |
---|
156 | static struct option long_options[] = |
---|
157 | { |
---|
158 | {"help", 0, 0, 'h'}, |
---|
159 | {"pool", 1, 0, 'p'}, |
---|
160 | {"file", 1, 0, 'f'}, |
---|
161 | {"version", 0, 0, 'v'}, |
---|
162 | {"multi", 0, 0, 'm'}, |
---|
163 | {0, 0, 0, 0} |
---|
164 | }; |
---|
165 | |
---|
166 | // determine name of pool, in case it is not set on commandline |
---|
167 | |
---|
168 | std::string pool = default_pool; |
---|
169 | std::string p = envtostr("STOPOS_POOL"); |
---|
170 | |
---|
171 | if (p != "") |
---|
172 | pool = p; |
---|
173 | |
---|
174 | if (trim(w.get_pool()) != "") |
---|
175 | pool = w.get_pool(); |
---|
176 | |
---|
177 | while(1) |
---|
178 | { |
---|
179 | int c = getopt_long(argc,argv,"-hp:f:vm",long_options,0); |
---|
180 | if (c == -1) |
---|
181 | { |
---|
182 | if (optind < argc) // unexpected extra argument |
---|
183 | { |
---|
184 | std::cerr << program_name << ": Unexpected argument:" << argv[optind] << std::endl; |
---|
185 | return 1; |
---|
186 | } |
---|
187 | break; |
---|
188 | } |
---|
189 | if ( c == '?' || c == ':' ) |
---|
190 | { |
---|
191 | std::cerr << "Invalid parameter, try "<< argv[0]<<" --help " <<std::endl; |
---|
192 | return 1; |
---|
193 | } |
---|
194 | switch(c) |
---|
195 | { |
---|
196 | case 'h': |
---|
197 | usage(); |
---|
198 | return 0; |
---|
199 | break; |
---|
200 | case 'p': |
---|
201 | pool = optarg; |
---|
202 | break; |
---|
203 | case 'f': |
---|
204 | inputfilename = optarg; |
---|
205 | break; |
---|
206 | case 'v': |
---|
207 | std::cerr << program_version << std::endl; |
---|
208 | break; |
---|
209 | case 'm': |
---|
210 | w.set_multi("yes"); |
---|
211 | break; |
---|
212 | case 1: |
---|
213 | parms.push_back(optarg); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | std::cerr << "WTD:" << __LINE__ << ":POOL:" << pool <<std::endl; |
---|
218 | w.set_pool(pool); |
---|
219 | w.set_serverurl("http://localhost:5050/cgi-bin/stoposserver/"); |
---|
220 | std::string prot; |
---|
221 | prot = "gdbm"; |
---|
222 | prot = "files"; |
---|
223 | prot = "mysql"; |
---|
224 | prot = "flat"; |
---|
225 | w.set_protocol(prot); |
---|
226 | w.set_whoami(envtostr("USER")); |
---|
227 | |
---|
228 | rc = 0; |
---|
229 | returnvalue r; |
---|
230 | |
---|
231 | std::string command = parms[0]; |
---|
232 | |
---|
233 | if(command == "create") |
---|
234 | { |
---|
235 | w.set_command("create"); |
---|
236 | } |
---|
237 | else if(command == "purge") |
---|
238 | { |
---|
239 | w.set_command("purge"); |
---|
240 | } |
---|
241 | else if(command == "status") |
---|
242 | { |
---|
243 | w.set_command("status"); |
---|
244 | } |
---|
245 | else if(command == "next") |
---|
246 | { |
---|
247 | w.set_command("next"); |
---|
248 | } |
---|
249 | else if(command == "dump") |
---|
250 | { |
---|
251 | w.set_command("dump"); |
---|
252 | } |
---|
253 | else if(command == "remove") |
---|
254 | { |
---|
255 | w.set_command("remove"); |
---|
256 | std::cerr << "WTD:" << __LINE__ << "VALUE:" << w.get_value()<<":" <<std::endl; |
---|
257 | if (parms.size() < 2) |
---|
258 | { |
---|
259 | std::string a=envtostr("STOPOS_KEY"); |
---|
260 | if (a == "") |
---|
261 | { |
---|
262 | std::cerr << program_name << ": No key found, exit.\n"; |
---|
263 | return 1; |
---|
264 | } |
---|
265 | w.set_value(a); |
---|
266 | std::cerr << "WTD:" << __LINE__ << "VALUE:" << w.get_value() <<std::endl; |
---|
267 | } |
---|
268 | else |
---|
269 | w.set_value(parms[1]); |
---|
270 | } |
---|
271 | else if(command == "add") |
---|
272 | { |
---|
273 | addflag = 1; |
---|
274 | } |
---|
275 | else |
---|
276 | { |
---|
277 | std::cerr << "Invalid command: '"<<command<<"'"<<std::endl; |
---|
278 | } |
---|
279 | |
---|
280 | if(addflag) |
---|
281 | { |
---|
282 | rc |= handle_add(w,inputfilename,r); |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | std::string body,header; |
---|
287 | |
---|
288 | rc = w.send_message(body,header); |
---|
289 | |
---|
290 | std::cerr << "WTD: rc:" << rc << std::endl; |
---|
291 | std::cerr << "WTD: header:" <<std::endl <<header << std::endl; |
---|
292 | std::cerr << "WTD: body:" <<std::endl <<body << std::endl; |
---|
293 | |
---|
294 | if (rc != 0) |
---|
295 | r.msg = "ERROR: Cannot connect to server"; |
---|
296 | else |
---|
297 | r.parse(body); |
---|
298 | } |
---|
299 | |
---|
300 | int shelltype; |
---|
301 | |
---|
302 | char *shell = getenv("SHELL"); |
---|
303 | std::string sh; |
---|
304 | if (shell == 0) |
---|
305 | sh = "bash"; |
---|
306 | else |
---|
307 | sh = shell; |
---|
308 | |
---|
309 | if (sh.find("csh") != sh.npos) |
---|
310 | shelltype=SHELL_CSH; |
---|
311 | else |
---|
312 | shelltype = SHELL_SH; |
---|
313 | |
---|
314 | std::cout << shellenv(r.msg, "STOPOS_RC", shelltype); |
---|
315 | std::cout << shellenv(r.key, "STOPOS_KEY", shelltype); |
---|
316 | std::cout << shellenv(r.comm, "STOPOS_COMMITTED", shelltype); |
---|
317 | std::cout << shellenv(r.count, "STOPOS_COUNT", shelltype); |
---|
318 | std::cout << shellenv(r.present, "STOPOS_PRESENT", shelltype); |
---|
319 | std::cout << shellenv(r.present0, "STOPOS_PRESENT0", shelltype); |
---|
320 | std::cout << shellenv(r.value, "STOPOS_VALUE", shelltype); |
---|
321 | |
---|
322 | if (command == "status") |
---|
323 | { |
---|
324 | std::cerr << "lines added: " << r.count <<std::endl; |
---|
325 | std::cerr << "lines present: " << r.present <<std::endl; |
---|
326 | std::cerr << "lines never comitted: " << r.present0 << std::endl; |
---|
327 | } |
---|
328 | if (r.msg != "OK") |
---|
329 | { |
---|
330 | std::cerr << program_name<<": "<<r.msg << std::endl; |
---|
331 | rc = 1; |
---|
332 | } |
---|
333 | |
---|
334 | std::cerr << "WTD: all is well that ends well\n"; |
---|
335 | return rc; |
---|
336 | } |
---|