You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
4.9 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #pragma once
  23. #include <map>
  24. #include <set>
  25. #include <string>
  26. #include <vector>
  27. #include <fstream>
  28. #include <iostream>
  29. #include "misc.hh"
  30. #include "pdnsexception.hh"
  31. #include <sys/types.h>
  32. #include <pwd.h>
  33. #include <grp.h>
  34. #include "namespaces.hh"
  35. typedef PDNSException ArgException;
  36. /** This class helps parsing argc and argv into a map of parameters. We have 3 kinds of formats:
  37. -w this leads to a key/value pair of "w"/void
  38. --port=25 "port"/"25"
  39. --daemon "daemon"/void
  40. We do not support "--port 25" syntax.
  41. It can also read from a file. This file can contain '#' to delimit comments.
  42. Some sample code:
  43. \code
  44. ArgvMap R;
  45. R.set("port")="25"; // use this to specify default parameters
  46. R.file("./default.conf"); // parse configuration file
  47. R.parse(argc, argv); // read the arguments from main()
  48. cout<<"Will we be a daemon?: "<<R.isset("daemon")<<endl;
  49. cout<<"Our port will be "<<R["port"]<<endl;
  50. map<string,string>::const_iterator i;
  51. cout<<"via iterator"<<endl;
  52. for(i=R.begin();i!=R.end();i++)
  53. cout<<i->first<<"="<<i->second<<endl;
  54. \endcode
  55. */
  56. class ArgvMap
  57. {
  58. public:
  59. ArgvMap();
  60. void parse(int &argc, char **argv, bool lax=false); //!< use this to parse from argc and argv
  61. void laxParse(int &argc, char **argv) //!< use this to parse from argc and argv
  62. {
  63. parse(argc,argv,true);
  64. }
  65. void preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var
  66. bool preParseFile(const char *fname, const string &arg, const string& theDefault=""); //!< use this to preparse a single var in configuration
  67. bool file(const char *fname, bool lax=false); //!< Parses a file with parameters
  68. bool file(const char *fname, bool lax, bool included);
  69. bool laxFile(const char *fname)
  70. {
  71. return file(fname,true);
  72. }
  73. bool parseFile(const char *fname, const string& arg, bool lax); //<! parse one line
  74. typedef map<string,string> param_t; //!< use this if you need to know the content of the map
  75. bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
  76. bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)
  77. int asNum(const string &var, int def=0); //!< return a variable value as a number or the default if the variable is empty
  78. mode_t asMode(const string &var); //!< return value interpreted as octal number
  79. uid_t asUid(const string &var); //!< return user id, resolves if necessary
  80. gid_t asGid(const string &var); //!< return group id, resolves if necessary
  81. double asDouble(const string &var); //!< return a variable value as a number
  82. string &set(const string &); //!< Gives a writable reference and allocates space for it
  83. string &set(const string &, const string &); //!< Does the same but also allows one to specify a help message
  84. void setCmd(const string &, const string &); //!< Add a command flag
  85. string &setSwitch(const string &, const string &); //!< Add a switch flag
  86. string helpstring(string prefix=""); //!< generates the --help
  87. string configstring(bool current, bool full); //!< generates the --config
  88. bool contains(const string &var, const string &val);
  89. bool isEmpty(const string &var); //!< checks if variable has value
  90. void setDefault(const string &var, const string &value);
  91. void setDefaults();
  92. vector<string>list();
  93. string getHelp(const string &item);
  94. const param_t::const_iterator begin(); //!< iterator semantics
  95. const param_t::const_iterator end(); //!< iterator semantics
  96. const string &operator[](const string &); //!< iterator semantics
  97. const vector<string>&getCommands();
  98. void gatherIncludes(std::vector<std::string> &extraConfigs);
  99. private:
  100. void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false);
  101. typedef map<string,string> params_t;
  102. params_t params;
  103. map<string,string> helpmap;
  104. map<string,string> defaultmap;
  105. map<string,string> d_typeMap;
  106. vector<string> d_cmds;
  107. std::set<string> d_cleared;
  108. };
  109. extern ArgvMap &arg();