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