| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002 - 2008 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License version 2 as published |
|---|
| 7 | by the Free Software Foundation |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | */ |
|---|
| 18 | #include "arguments.hh" |
|---|
| 19 | #include <boost/algorithm/string.hpp> |
|---|
| 20 | using namespace boost; |
|---|
| 21 | |
|---|
| 22 | const ArgvMap::param_t::const_iterator ArgvMap::begin() |
|---|
| 23 | { |
|---|
| 24 | return params.begin(); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | const ArgvMap::param_t::const_iterator ArgvMap::end() |
|---|
| 28 | { |
|---|
| 29 | return params.end(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | string & ArgvMap::set(const string &var) |
|---|
| 33 | { |
|---|
| 34 | return params[var]; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | bool ArgvMap::mustDo(const string &var) |
|---|
| 38 | { |
|---|
| 39 | return ((*this)[var]!="no") && ((*this)[var]!="off"); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | vector<string>ArgvMap::list() |
|---|
| 43 | { |
|---|
| 44 | vector<string> ret; |
|---|
| 45 | for(map<string,string>::const_iterator i=params.begin();i!=params.end();++i) |
|---|
| 46 | ret.push_back(i->first); |
|---|
| 47 | return ret; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | string ArgvMap::getHelp(const string &item) |
|---|
| 51 | { |
|---|
| 52 | return helpmap[item]; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | string & ArgvMap::set(const string &var, const string &help) |
|---|
| 56 | { |
|---|
| 57 | helpmap[var]=help; |
|---|
| 58 | d_typeMap[var]="Parameter"; |
|---|
| 59 | return set(var); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void ArgvMap::setCmd(const string &var, const string &help) |
|---|
| 63 | { |
|---|
| 64 | helpmap[var]=help; |
|---|
| 65 | d_typeMap[var]="Command"; |
|---|
| 66 | set(var)="no"; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | string & ArgvMap::setSwitch(const string &var, const string &help) |
|---|
| 70 | { |
|---|
| 71 | helpmap[var]=help; |
|---|
| 72 | d_typeMap[var]="Switch"; |
|---|
| 73 | return set(var); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | bool ArgvMap::contains(const string &var, const string &val) |
|---|
| 78 | { |
|---|
| 79 | vector<string> parts; |
|---|
| 80 | vector<string>::const_iterator i; |
|---|
| 81 | |
|---|
| 82 | stringtok( parts, params[var], ", \t" ); |
|---|
| 83 | for( i = parts.begin(); i != parts.end(); i++ ) { |
|---|
| 84 | if( *i == val ) { |
|---|
| 85 | return true; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | string ArgvMap::helpstring(string prefix) |
|---|
| 94 | { |
|---|
| 95 | if(prefix=="no") |
|---|
| 96 | prefix=""; |
|---|
| 97 | |
|---|
| 98 | string help; |
|---|
| 99 | |
|---|
| 100 | for(map<string,string>::const_iterator i=helpmap.begin(); |
|---|
| 101 | i!=helpmap.end(); |
|---|
| 102 | i++) |
|---|
| 103 | { |
|---|
| 104 | if(!prefix.empty() && i->first.find(prefix)) // only print items with prefix |
|---|
| 105 | continue; |
|---|
| 106 | |
|---|
| 107 | help+=" --"; |
|---|
| 108 | help+=i->first; |
|---|
| 109 | |
|---|
| 110 | string type=d_typeMap[i->first]; |
|---|
| 111 | |
|---|
| 112 | if(type=="Parameter") |
|---|
| 113 | help+="=..."; |
|---|
| 114 | else if(type=="Switch") |
|---|
| 115 | { |
|---|
| 116 | help+=" | --"+i->first+"=yes"; |
|---|
| 117 | help+=" | --"+i->first+"=no"; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | help+="\n\t"; |
|---|
| 122 | help+=i->second; |
|---|
| 123 | help+="\n"; |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | return help; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | string ArgvMap::configstring() |
|---|
| 130 | { |
|---|
| 131 | string help; |
|---|
| 132 | |
|---|
| 133 | help="# Autogenerated configuration file template\n"; |
|---|
| 134 | for(map<string,string>::const_iterator i=helpmap.begin(); |
|---|
| 135 | i!=helpmap.end(); |
|---|
| 136 | i++) |
|---|
| 137 | { |
|---|
| 138 | if(d_typeMap[i->first]=="Command") |
|---|
| 139 | continue; |
|---|
| 140 | |
|---|
| 141 | help+="#################################\n"; |
|---|
| 142 | help+="# "; |
|---|
| 143 | help+=i->first; |
|---|
| 144 | help+="\t"; |
|---|
| 145 | help+=i->second; |
|---|
| 146 | help+="\n#\n"; |
|---|
| 147 | help+="# "+i->first+"="+params[i->first]+"\n\n"; |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | return help; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | const string & ArgvMap::operator[](const string &arg) |
|---|
| 155 | { |
|---|
| 156 | if(!parmIsset(arg)) |
|---|
| 157 | throw ArgException(string("Undefined but needed argument: '")+arg+"'"); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | return params[arg]; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | int ArgvMap::asNum(const string &arg) |
|---|
| 164 | { |
|---|
| 165 | if(!parmIsset(arg)) |
|---|
| 166 | throw ArgException(string("Undefined but needed argument: '")+arg+"'"); |
|---|
| 167 | |
|---|
| 168 | return atoi(params[arg].c_str()); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | double ArgvMap::asDouble(const string &arg) |
|---|
| 172 | { |
|---|
| 173 | if(!parmIsset(arg)) |
|---|
| 174 | throw ArgException(string("Undefined but needed argument: '")+arg+"'"); |
|---|
| 175 | |
|---|
| 176 | return atof(params[arg].c_str()); |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | ArgvMap::ArgvMap() |
|---|
| 180 | { |
|---|
| 181 | |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | bool ArgvMap::parmIsset(const string &var) |
|---|
| 185 | { |
|---|
| 186 | return (params.find(var)!=params.end()); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | void ArgvMap::parseOne(const string &arg, const string &parseOnly, bool lax) |
|---|
| 190 | { |
|---|
| 191 | string var, val; |
|---|
| 192 | string::size_type pos; |
|---|
| 193 | |
|---|
| 194 | if(!arg.find("--") &&(pos=arg.find("="))!=string::npos) // this is a --port=25 case |
|---|
| 195 | { |
|---|
| 196 | var=arg.substr(2,pos-2); |
|---|
| 197 | val=arg.substr(pos+1); |
|---|
| 198 | } |
|---|
| 199 | else if(!arg.find("--") && (arg.find("=")==string::npos)) // this is a --daemon case |
|---|
| 200 | { |
|---|
| 201 | var=arg.substr(2); |
|---|
| 202 | val=""; |
|---|
| 203 | } |
|---|
| 204 | else if(arg[0]=='-') |
|---|
| 205 | { |
|---|
| 206 | var=arg.substr(1); |
|---|
| 207 | val=""; |
|---|
| 208 | } |
|---|
| 209 | else { // command |
|---|
| 210 | d_cmds.push_back(arg); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if(var!="" && (parseOnly.empty() || var==parseOnly)) { |
|---|
| 214 | |
|---|
| 215 | pos=val.find_first_not_of(" \t"); // strip leading whitespace |
|---|
| 216 | if(pos && pos!=string::npos) |
|---|
| 217 | val=val.substr(pos); |
|---|
| 218 | |
|---|
| 219 | if(parmIsset(var)) |
|---|
| 220 | params[var]=val; |
|---|
| 221 | else |
|---|
| 222 | if(!lax) |
|---|
| 223 | throw ArgException("Trying to set unexisting parameter '"+var+"'"); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | const vector<string>&ArgvMap::getCommands() |
|---|
| 228 | { |
|---|
| 229 | return d_cmds; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | void ArgvMap::parse(int &argc, char **argv, bool lax) |
|---|
| 233 | { |
|---|
| 234 | for(int n=1;n<argc;n++) { |
|---|
| 235 | parseOne(argv[n],"",lax); |
|---|
| 236 | } |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | void ArgvMap::preParse(int &argc, char **argv, const string &arg) |
|---|
| 240 | { |
|---|
| 241 | for(int n=1;n<argc;n++) { |
|---|
| 242 | string varval=argv[n]; |
|---|
| 243 | if(!varval.find("--"+arg)) |
|---|
| 244 | parseOne(argv[n]); |
|---|
| 245 | } |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | bool ArgvMap::preParseFile(const char *fname, const string &arg, const string& theDefault) |
|---|
| 249 | { |
|---|
| 250 | params[arg]=theDefault; |
|---|
| 251 | |
|---|
| 252 | ifstream f(fname); |
|---|
| 253 | if(!f) |
|---|
| 254 | return false; |
|---|
| 255 | |
|---|
| 256 | string line; |
|---|
| 257 | string pline; |
|---|
| 258 | string::size_type pos; |
|---|
| 259 | |
|---|
| 260 | while(getline(f,pline)) { |
|---|
| 261 | trim_right(pline); |
|---|
| 262 | |
|---|
| 263 | if(pline[pline.size()-1]=='\\') { |
|---|
| 264 | line+=pline.substr(0,pline.length()-1); |
|---|
| 265 | continue; |
|---|
| 266 | } |
|---|
| 267 | else |
|---|
| 268 | line+=pline; |
|---|
| 269 | |
|---|
| 270 | // strip everything after a # |
|---|
| 271 | if((pos=line.find("#"))!=string::npos) |
|---|
| 272 | line=line.substr(0,pos); |
|---|
| 273 | |
|---|
| 274 | // strip trailing spaces |
|---|
| 275 | trim_right(line); |
|---|
| 276 | |
|---|
| 277 | // strip leading spaces |
|---|
| 278 | if((pos=line.find_first_not_of(" \t\r\n"))!=string::npos) |
|---|
| 279 | line=line.substr(pos); |
|---|
| 280 | |
|---|
| 281 | // gpgsql-basic-query=sdfsdfs dfsdfsdf sdfsdfsfd |
|---|
| 282 | |
|---|
| 283 | parseOne( string("--") + line, arg ); |
|---|
| 284 | line=""; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | return true; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | bool ArgvMap::file(const char *fname, bool lax) |
|---|
| 292 | { |
|---|
| 293 | ifstream f(fname); |
|---|
| 294 | if(!f) { |
|---|
| 295 | return false; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | string line; |
|---|
| 299 | string pline; |
|---|
| 300 | string::size_type pos; |
|---|
| 301 | |
|---|
| 302 | while(getline(f,pline)) { |
|---|
| 303 | trim_right(pline); |
|---|
| 304 | if(pline.empty()) |
|---|
| 305 | continue; |
|---|
| 306 | |
|---|
| 307 | if(pline[pline.size()-1]=='\\') { |
|---|
| 308 | line+=pline.substr(0,pline.length()-1); |
|---|
| 309 | |
|---|
| 310 | continue; |
|---|
| 311 | } |
|---|
| 312 | else |
|---|
| 313 | line+=pline; |
|---|
| 314 | |
|---|
| 315 | // strip everything after a # |
|---|
| 316 | if((pos=line.find("#"))!=string::npos) |
|---|
| 317 | line=line.substr(0,pos); |
|---|
| 318 | |
|---|
| 319 | // strip trailing spaces |
|---|
| 320 | trim(line); |
|---|
| 321 | |
|---|
| 322 | parseOne(string("--")+line,"",lax); |
|---|
| 323 | line=""; |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | return true; |
|---|
| 327 | } |
|---|