]> granicus.if.org Git - pdns/commitdiff
Add various argument parsing options (asGuid, asUid etc) - thanks to Aki Tuomi
authorBert Hubert <bert.hubert@netherlabs.nl>
Sat, 15 Nov 2008 19:33:26 +0000 (19:33 +0000)
committerBert Hubert <bert.hubert@netherlabs.nl>
Sat, 15 Nov 2008 19:33:26 +0000 (19:33 +0000)
git-svn-id: svn://svn.powerdns.com/pdns/trunk/pdns@1269 d19b8d6e-7fed-0310-83ef-9ca221ded41b

pdns/arguments.cc
pdns/arguments.hh

index f5ec3a6917d8621fc2313357f1e230542985779e..30e4b9496aca8e4324e1be0cf80cafd91ece45fd 100644 (file)
@@ -163,20 +163,113 @@ const string & ArgvMap::operator[](const string &arg)
   return params[arg];
 }
 
+#ifndef WIN32
+mode_t ArgvMap::asMode(const string &arg) 
+{
+  mode_t mode;
+  const char *cptr_orig;
+  char *cptr_ret = NULL;
+
+  if(!parmIsset(arg))
+   throw ArgException(string("Undefined but needed argument: '")+arg+"'");
+
+  cptr_orig = params[arg].c_str();
+  mode = static_cast<mode_t>(strtol(cptr_orig, &cptr_ret, 8));
+  if (mode == 0 && cptr_ret == cptr_orig) 
+    throw ArgException("'" + arg + string("' contains invalid octal mode"));
+   return mode;
+}
+
+gid_t ArgvMap::asGid(const string &arg)
+{
+  gid_t gid;
+  const char *cptr_orig;
+  char *cptr_ret = NULL;
+
+  if(!parmIsset(arg))
+   throw ArgException(string("Undefined but needed argument: '")+arg+"'");
+
+  cptr_orig = params[arg].c_str();
+  gid = static_cast<gid_t>(strtol(cptr_orig, &cptr_ret, 0));
+  if (gid == 0 && cptr_ret == cptr_orig) {
+    // try to resolve
+    struct group *group = getgrnam(params[arg].c_str());
+    if (group == NULL)
+     throw ArgException("'" + arg + string("' contains invalid group"));
+    gid = group->gr_gid;
+   }
+   return gid;
+}
+
+uid_t ArgvMap::asUid(const string &arg)
+{
+  uid_t uid;
+  const char *cptr_orig;
+  char *cptr_ret = NULL;
+
+  if(!parmIsset(arg))
+   throw ArgException(string("Undefined but needed argument: '")+arg+"'");
+
+  cptr_orig = params[arg].c_str();
+  uid = static_cast<uid_t>(strtol(cptr_orig, &cptr_ret, 0));
+  if (uid == 0 && cptr_ret == cptr_orig) {
+    // try to resolve
+    struct passwd *pwent = getpwnam(params[arg].c_str());
+    if (pwent == NULL)
+     throw ArgException("'" + arg + string("' contains invalid group"));
+    uid = pwent->pw_uid;
+   }
+   return uid;
+}
+#endif
+
 int ArgvMap::asNum(const string &arg)
 {
+  int retval;
+  const char *cptr_orig;
+  char *cptr_ret = NULL;
+
   if(!parmIsset(arg))
     throw ArgException(string("Undefined but needed argument: '")+arg+"'");
 
-  return atoi(params[arg].c_str());
+  // treat empty values as zeros
+  if (params[arg].empty())
+   return 0;
+
+  cptr_orig = params[arg].c_str();
+  retval = static_cast<int>(strtol(cptr_orig, &cptr_ret, 0));
+  if (!retval && cptr_ret == cptr_orig)
+   throw ArgException("'"+arg+string("' is not valid number"));
+
+  return retval;
+}
+
+bool ArgvMap::isEmpty(const string &arg) 
+{
+   if(!parmIsset(arg))
+    return true;
+   return params[arg].empty();
 }
 
 double ArgvMap::asDouble(const string &arg)
 {
+  double retval;
+  const char *cptr_orig;
+  char *cptr_ret = NULL;
+
   if(!parmIsset(arg))
     throw ArgException(string("Undefined but needed argument: '")+arg+"'");
 
-  return atof(params[arg].c_str());
+  if (params[arg].empty())
+   return 0.0;
+
+  cptr_orig = params[arg].c_str();
+  retval = strtod(cptr_orig, &cptr_ret);
+  if (retval == 0 && cptr_ret == cptr_orig)
+   throw ArgException("'"+arg+string("' is not valid double"));
+
+  return retval;
 }
 
 ArgvMap::ArgvMap()
index 31153b58505362c98c1138aff7dbbf7e2dec1b8a..4dd81fc6a4aa8977c632d9fa7af52f66e8385de6 100644 (file)
 #include <iostream>
 #include "misc.hh"
 #include "ahuexception.hh"
+#ifndef WIN32
+# include <sys/types.h>
+# include <pwd.h>
+# include <grp.h>
+#endif
 
 using namespace std;
 
@@ -88,6 +93,11 @@ public:
   bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
   bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)
   int asNum(const string &var); //!< return a variable value as a number
+#ifndef WIN32
+  mode_t asMode(const string &var); //<!< return value interprepted as octal number
+  uid_t asUid(const string &var); //!< return user id, resolves if necessary
+  gid_t asGid(const string &var); //!< return group id, resolves if necessary
+#endif
   double asDouble(const string &var); //!< return a variable value as a number
   string &set(const string &); //!< Gives a writable reference and allocates space for it
   string &set(const string &, const string &); //!< Does the same but also allows to specify a help message
@@ -96,6 +106,7 @@ public:
   string helpstring(string prefix=""); //!< generates the --help
   string configstring(); //!< generates the --mkconfig
   bool contains(const string &var, const string &val);
+  bool isEmpty(const string &var); //<! checks if variable has value
 
   vector<string>list();
   string getHelp(const string &item);