Browse Source

libdpkg: Refactor integer parsing into new dpkg_options_parse_arg_int()

Guillem Jover 12 years ago
parent
commit
b44d62d911
5 changed files with 23 additions and 23 deletions
  1. 1 6
      dpkg-deb/main.c
  2. 1 0
      lib/dpkg/libdpkg.map
  3. 15 0
      lib/dpkg/options.c
  4. 2 0
      lib/dpkg/options.h
  5. 4 17
      src/main.c

+ 1 - 6
dpkg-deb/main.c

@@ -189,13 +189,8 @@ static void
 set_compress_level(const struct cmdinfo *cip, const char *value)
 {
   long level;
-  char *end;
-
-  errno = 0;
-  level = strtol(value, &end, 0);
-  if (value == end || *end || errno != 0)
-    badusage(_("invalid integer for -%c: '%.250s'"), cip->oshort, value);
 
+  level = dpkg_options_parse_arg_int(cip, value);
   if (level < 0 || level > 9)
     badusage(_("invalid compression level for -%c: %ld'"), cip->oshort, level);
 

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -162,6 +162,7 @@ LIBDPKG_PRIVATE {
 	# Configuration and command line handling
 	dpkg_options_load;
 	dpkg_options_parse;
+	dpkg_options_parse_arg_int;
 	badusage;
 	cipaction;		# XXX variable, do not export
 	setaction;

+ 15 - 0
lib/dpkg/options.c

@@ -25,6 +25,7 @@
 
 #include <errno.h>
 #include <ctype.h>
+#include <limits.h>
 #include <string.h>
 #include <dirent.h>
 #include <stdarg.h>
@@ -273,6 +274,20 @@ dpkg_options_parse(const char *const **argvp, const struct cmdinfo *cmdinfos,
   }
 }
 
+long
+dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str)
+{
+  long value;
+  char *end;
+
+  errno = 0;
+  value = strtol(str, &end, 0);
+  if (str == end || *end || value < 0 || value > INT_MAX || errno != 0)
+    badusage(_("invalid integer for --%s: `%.250s'"), cmd->olong, str);
+
+  return value;
+}
+
 void
 setobsolete(const struct cmdinfo *cip, const char *value)
 {

+ 2 - 0
lib/dpkg/options.h

@@ -62,6 +62,8 @@ void dpkg_options_load(const char *prog, const struct cmdinfo *cmdinfos);
 void dpkg_options_parse(const char *const **argvp,
                         const struct cmdinfo *cmdinfos, const char *help_str);
 
+long dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str);
+
 /**
  * Current cmdinfo action.
  */

+ 4 - 17
src/main.c

@@ -381,24 +381,13 @@ static void ignoredepends(const struct cmdinfo *cip, const char *value) {
 }
 
 static void setinteger(const struct cmdinfo *cip, const char *value) {
-  long v;
-  char *ep;
-
-  errno = 0;
-  v = strtol(value, &ep, 0);
-  if (value == ep || *ep || v < 0 || v > INT_MAX || errno != 0)
-    badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
-  *cip->iassignto= v;
+  *cip->iassignto = dpkg_options_parse_arg_int(cip, value);
 }
 
 static void setpipe(const struct cmdinfo *cip, const char *value) {
   long v;
-  char *ep;
 
-  errno = 0;
-  v = strtol(value, &ep, 0);
-  if (value == ep || *ep || v < 0 || v > INT_MAX || errno != 0)
-    badusage(_("invalid integer for --%s: `%.250s'"),cip->olong,value);
+  v = dpkg_options_parse_arg_int(cip, value);
 
   statusfd_add(v);
 }
@@ -760,10 +749,8 @@ commandfd(const char *const *argv)
   pipein = *argv++;
   if (pipein == NULL || *argv)
     badusage(_("--%s takes exactly one argument"), cipaction->olong);
-  errno = 0;
-  infd = strtol(pipein, &endptr, 10);
-  if (pipein == endptr || *endptr || infd < 0 || infd > INT_MAX || errno != 0)
-    ohshit(_("invalid integer for --%s: `%.250s'"), cipaction->olong, pipein);
+
+  infd = dpkg_options_parse_arg_int(cipaction, pipein);
   in = fdopen(infd, "r");
   if (in == NULL)
     ohshite(_("couldn't open `%i' for stream"), (int) infd);