/* Copyright 2013 Willem Vermin, SURFsara Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include #include #include #include #include #include #include "wrequest.h" #include "shellenv.h" #include "stopos.h" struct returnvalue { std::string msg; std::string key; std::string comm; std::string count; std::string present; std::string present0; std::string value; void parse(std::string &s) { // we expect somewhere in the output of the server a line like // STOPOS:/OK/123/beautiful // OK -> msg // 123 -> key // 1 -> comm // beautiful -> value // // OK, 123 and beautiful are hex coded this->msg = ""; this->key = ""; this->comm = "0"; this->count = "0"; this->present = "0"; this->present0 = "0"; this->value = ""; size_t p = s.find("STOPOS:"); if (p == s.npos) { this->msg="Error: stopos server not running."; return; } size_t q = s.find('\n',p+1); std::string l = s.substr(p,q-p); std::vector v; split(v,l,outsep); for (unsigned int i=1; imsg = zhextostr(v[i]); break; case 2: this->key = zhextostr(v[i]); break; case 3: this->comm = zhextostr(v[i]); break; case 4: this->count = zhextostr(v[i]); break; case 5: this->present = zhextostr(v[i]); break; case 6: this->present0 = zhextostr(v[i]); break; case 7: this->value = zhextostr(v[i]); break; } // special case for key: if key == "", we try to take // it from the environment. if (trim(this->key) == "") this->key = envtostr("STOPOS_KEY"); } } }; void usage() { std::cerr << "Usage:" << std::endl; std::cerr << program_name << " -h,--help" << std::endl; std::cerr << program_name << " -v,--version" << std::endl; std::cerr << program_name << " create [-p,--pool POOL]" << std::endl; std::cerr << program_name << " purge [-p,--pool POOL]" << std::endl; std::cerr << program_name << " pools" << std::endl; std::cerr << program_name << " add [-p,--pool POOL] [FILENAME]" << std::endl; std::cerr << program_name << " next [-p,--pool POOL] [-m,--mult]" << std::endl; std::cerr << program_name << " remove [-p,--pool POOL] [KEY]" << std::endl; std::cerr << program_name << " status [-p,--pool POOL]" << std::endl; std::cerr << program_name << " dump [-p,--pool POOL]" << std::endl; std::cerr << " -q,--quiet added to any command will reduce the ouput" << std::endl; std::cerr << "Defaults:" << std::endl; std::cerr << " POOL : pool" << std::endl; std::cerr << " FILENAME : standard input" << std::endl; std::cerr << "Environment:"< maxlinelength) { r.msg = "Error: line length exeeds "+ NumberToString(maxlinelength); break; } w.set_value(line); for (int i=0; i parms; static struct option long_options[] = { {"help", 0, 0, 'h'}, {"pool", 1, 0, 'p'}, {"version", 0, 0, 'v'}, {"multi", 0, 0, 'm'}, {"quiet", 0, 0, 'q'}, {0, 0, 0, 0} }; // determine name of pool, in case it is not set on commandline std::string pool = default_pool; std::string p = envtostr("STOPOS_POOL"); if (p != "") pool = p; if (trim(w.get_pool()) != "") pool = w.get_pool(); bool quiet = 0; while(1) { int c = getopt_long(argc,argv,"-hp:vmq",long_options,0); if (c == -1) { if (optind < argc) // unexpected extra argument { std::cerr << program_name << ": Unexpected argument:" << argv[optind] << std::endl; return 1; } break; } if ( c == '?' || c == ':' ) { std::cerr << program_name << ": Invalid parameter, try "<< program_name<<" --help " <1) if (command !="remove" && command != "add") { std::cerr << program_name << ": Extraneous parameter:'"< 1) inputfilename = parms[1]; rc |= handle_add(w,inputfilename,r,quiet); } else { std::string body,header; for (int i=0; i0) std::cerr << r.value << std::endl; } if (r.msg != "OK" && ! quiet) { std::cerr << program_name<<": "<