Преглед изворни кода

Move action and obsolete related functionality to the myopt module

Reduce repeated code, and localize it where it belongs.
Guillem Jover пре 15 година
родитељ
комит
c9b7af92e5
13 измењених фајлова са 41 додато и 93 уклоњено
  1. 0 1
      TODO
  2. 0 1
      dpkg-deb/dpkg-deb.h
  3. 0 12
      dpkg-deb/main.c
  4. 0 1
      dpkg-split/dpkg-split.h
  5. 0 13
      dpkg-split/main.c
  6. 3 0
      lib/dpkg/libdpkg.Versions
  7. 25 0
      lib/dpkg/myopt.c
  8. 13 0
      lib/dpkg/myopt.h
  9. 0 20
      src/divertcmd.c
  10. 0 16
      src/main.c
  11. 0 1
      src/main.h
  12. 0 14
      src/querycmd.c
  13. 0 14
      src/statcmd.c

+ 0 - 1
TODO

@@ -44,7 +44,6 @@ TODO
    - Get rid of unuseful "unsigned" modifiers.
    - Use internerr instead of assert, and print more meaninful messages.
    - Make actionfunction return int, and avoid global exitcode variables.
-   - Move ACTION and OBSOLETE, etc to ‘myopt.h’.
    - Replace raw write and read calls with safe variants (signals etc).
    - Make deb build version a version instead of doing checks over a string.
    - Use enums for currently hardcoded literals (tdus, dependtry, etc).

+ 0 - 1
dpkg-deb/dpkg-deb.h

@@ -27,7 +27,6 @@ dofunction do_contents, do_control, do_showinfo;
 dofunction do_info, do_field, do_extract, do_vextract, do_fsystarfile;
 
 extern int debugflag, nocheckflag, oldformatflag;
-extern const struct cmdinfo *cipaction;
 
 void extracthalf(const char *debar, const char *directory,
                  const char *taroption, int admininfo);

+ 0 - 12
dpkg-deb/main.c

@@ -134,9 +134,7 @@ const char printforhelp[]=
 int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
 struct compressor *compressor = &compressor_gzip;
 int compress_level = -1;
-const struct cmdinfo *cipaction = NULL;
 
-static void setaction(const struct cmdinfo *cip, const char *value);
 static void setcompresstype(const struct cmdinfo *cip, const char *value);
 
 static void
@@ -155,9 +153,6 @@ set_compress_level(const struct cmdinfo *cip, const char *value)
   compress_level = level;
 }
 
-#define ACTION(longopt, shortopt, code, function) \
- { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
-
 static const struct cmdinfo cmdinfos[]= {
   ACTION("build",         'b', 0, do_build),
   ACTION("contents",      'c', 0, do_contents),
@@ -181,13 +176,6 @@ static const struct cmdinfo cmdinfos[]= {
   {  NULL,           0,   0, NULL,           NULL,         NULL             }
 };
 
-static void setaction(const struct cmdinfo *cip, const char *value) {
-  if (cipaction)
-    badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-             cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
-  cipaction= cip;
-}
-
 static void setcompresstype(const struct cmdinfo *cip, const char *value) {
   compressor = compressor_find_by_name(value);
   if (compressor == NULL)

+ 0 - 1
dpkg-split/dpkg-split.h

@@ -49,7 +49,6 @@ struct partqueue {
    */
 };
 
-extern const struct cmdinfo *cipaction;
 extern struct partqueue *queue;
 
 extern long opt_maxpartsize;

+ 0 - 13
dpkg-split/main.c

@@ -96,7 +96,6 @@ usage(const struct cmdinfo *cip, const char *value)
 const char thisname[]= SPLITTER;
 const char printforhelp[]= N_("Type dpkg-split --help for help.");
 
-const struct cmdinfo *cipaction=NULL;
 struct partqueue *queue= NULL;
 
 long opt_maxpartsize = SPLITPARTDEFMAX;
@@ -114,8 +113,6 @@ void rerreof(FILE *f, const char *fn) {
   ohshit(_("unexpected end of file in %.250s"),fn);
 }
 
-static void setaction(const struct cmdinfo *cip, const char *value);
-
 static void setpartsize(const struct cmdinfo *cip, const char *value) {
   long newpartsize;
   char *endp;
@@ -132,9 +129,6 @@ static void setpartsize(const struct cmdinfo *cip, const char *value) {
              (HEADERALLOWANCE >> 10) + 1);
 }
 
-#define ACTION(longopt, shortopt, code, function) \
-{ longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
-
 static const struct cmdinfo cmdinfos[]= {
   ACTION("split",   's',  0,  do_split),
   ACTION("join",    'j',  0,  do_join),
@@ -153,13 +147,6 @@ static const struct cmdinfo cmdinfos[]= {
   {  NULL,              0                                              }
 };
 
-static void setaction(const struct cmdinfo *cip, const char *value) {
-  if (cipaction)
-    badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-             cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
-  cipaction= cip;
-}
-
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   int l;

+ 3 - 0
lib/dpkg/libdpkg.Versions

@@ -124,6 +124,9 @@ LIBDPKG_PRIVATE {
 	badusage;
 	# printforhelp;		# XXX variable, do not require external
 	# thisname;		# XXX variable, do not require external
+	cipaction;		# XXX variable, do not export
+	setaction;
+	setobsolete;
 
 	# General logging
 	log_file;		# XXX variable, do not export

+ 25 - 0
lib/dpkg/myopt.c

@@ -263,3 +263,28 @@ void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
     }
   }
 }
+
+void
+setobsolete(const struct cmdinfo *cip, const char *value)
+{
+  warning(_("obsolete option '--%s'\n"), cip->olong);
+}
+
+const struct cmdinfo *cipaction = NULL;
+
+/* XXX: This function is a hack. */
+static inline int
+option_short(int c)
+{
+  return c ? c : '\b';
+}
+
+void
+setaction(const struct cmdinfo *cip, const char *value)
+{
+  if (cipaction)
+    badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
+             option_short(cip->oshort), cip->olong,
+             option_short(cipaction->oshort), cipaction->olong);
+  cipaction = cip;
+}

+ 13 - 0
lib/dpkg/myopt.h

@@ -49,6 +49,19 @@ void myfileopt(const char* fn, const struct cmdinfo* cmdinfos);
 void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos);
 void loadcfgfile(const char *prog, const struct cmdinfo *cmdinfos);
 
+/**
+ * Current cmdinfo action.
+ */
+extern const struct cmdinfo *cipaction;
+
+void setaction(const struct cmdinfo *cip, const char *value);
+void setobsolete(const struct cmdinfo *cip, const char *value);
+
+#define ACTION(longopt, shortopt, code, function) \
+ { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
+#define OBSOLETE(longopt, shortopt) \
+ { longopt, shortopt, 0, NULL, NULL, setobsolete, 0, NULL, NULL }
+
 DPKG_END_DECLS
 
 #endif /* MYOPT_H */

+ 0 - 20
src/divertcmd.c

@@ -49,7 +49,6 @@
 const char thisname[] = "dpkg-divert";
 const char printforhelp[] = N_("Use --help for help about querying packages.");
 
-const struct cmdinfo *cipaction = NULL;
 const char *admindir = ADMINDIR;
 
 static bool opt_pkgname_match_any = true;
@@ -659,22 +658,6 @@ diversion_listpackage(const char *const *argv)
 	return 0;
 }
 
-static inline int
-option_short(int c)
-{
-	return c ? c : '\b';
-}
-
-static void
-setaction(const struct cmdinfo *cip, const char *value)
-{
-	if (cipaction)
-		badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-		         option_short(cip->oshort), cip->olong,
-		         option_short(cipaction->oshort), cipaction->olong);
-	cipaction = cip;
-}
-
 static void
 setpackage(const struct cmdinfo *cip, const char *value)
 {
@@ -696,9 +679,6 @@ setdivertto(const struct cmdinfo *cip, const char *value)
 		badusage(_("divert-to may not contain newlines"));
 }
 
-#define ACTION(longopt, shortopt, code, function) \
- { longopt, shortopt, 0, 0, 0, setaction, code, 0, (voidfnp)function }
-
 static const struct cmdinfo cmdinfo_add =
 	ACTION("add",         0, 0, diversion_add);
 

+ 0 - 16
src/main.c

@@ -172,7 +172,6 @@ const char printforhelp[]= N_(
 "\n"
 "Options marked [*] produce a lot of output - pipe it through `less' or `more' !");
 
-const struct cmdinfo *cipaction = NULL;
 int f_pending=0, f_recursive=0, f_alsoselect=1, f_skipsame=0, f_noact=0;
 int f_autodeconf=0, f_nodebsig=0;
 int f_triggers = 0;
@@ -219,17 +218,6 @@ static const struct forceinfo {
   {  NULL                                               }
 };
 
-static void setaction(const struct cmdinfo *cip, const char *value) {
-  if (cipaction)
-    badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-             cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
-  cipaction= cip;
-}
-
-static void setobsolete(const struct cmdinfo *cip, const char *value) {
-  warning(_("obsolete option '--%s'\n"), cip->olong);
-}
-
 static void setdebug(const struct cmdinfo *cpi, const char *value) {
   char *endp;
 
@@ -464,10 +452,6 @@ static const struct cmdinfo cmdinfos[]= {
    * The action entries are made with the ACTION macro, as they all
    * have a very similar structure.
    */
-#define ACTION(longopt,shortopt,code,function) \
- { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
-#define OBSOLETE(longopt,shortopt) \
- { longopt, shortopt, 0, NULL, NULL, setobsolete, 0, NULL, NULL }
 #define ACTIONBACKEND(longopt, shortopt, backend) \
  { longopt, shortopt, 0, NULL, NULL, setaction, 0, (void *)backend, (voidfnp)execbackend }
 

+ 0 - 1
src/main.h

@@ -119,7 +119,6 @@ enum conffopt {
 
 extern const char *const statusstrings[];
 
-extern const struct cmdinfo *cipaction;
 extern int f_pending, f_recursive, f_alsoselect, f_skipsame, f_noact;
 extern int f_autodeconf, f_nodebsig;
 extern int f_triggers;

+ 0 - 14
src/querycmd.c

@@ -642,27 +642,13 @@ usage(const struct cmdinfo *ci, const char *value)
 const char thisname[]= "dpkg-query";
 const char printforhelp[]= N_("Use --help for help about querying packages.");
 
-const struct cmdinfo *cipaction = NULL;
-
 const char *admindir= ADMINDIR;
 
-static void setaction(const struct cmdinfo *cip, const char *value) {
-  if (cipaction)
-    badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-             cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
-  cipaction= cip;
-}
-
 static const struct cmdinfo cmdinfos[]= {
   /* This table has both the action entries in it and the normal options.
    * The action entries are made with the ACTION macro, as they all
    * have a very similar structure.
    */
-#define ACTION(longopt,shortopt,code,function) \
- { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
-#define OBSOLETE(longopt,shortopt) \
- { longopt, shortopt, 0, NULL, NULL, setobsolete, 0, NULL, NULL }
-
   ACTION( "listfiles",                      'L', act_listfiles,     enqperpackage   ),
   ACTION( "status",                         's', act_status,        enqperpackage   ),
   ACTION( "print-avail",                    'p', act_printavail,    enqperpackage   ),

+ 0 - 14
src/statcmd.c

@@ -99,23 +99,12 @@ usage(const struct cmdinfo *cip, const char *value)
 	exit(0);
 }
 
-const struct cmdinfo *cipaction = NULL;
 const char *admindir = ADMINDIR;
 
 static int opt_verbose = 1;
 static int opt_force = 0;
 static int opt_update = 0;
 
-static void
-setaction(const struct cmdinfo *cip, const char *value)
-{
-	if (cipaction)
-		badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
-		         cip->oshort, cip->olong,
-		         cipaction->oshort, cipaction->olong);
-	cipaction = cip;
-}
-
 static char *
 path_cleanup(const char *path)
 {
@@ -366,9 +355,6 @@ statoverride_list(const char *const *argv)
 	return ret;
 }
 
-#define ACTION(longopt, shortopt, code, function) \
- { longopt, shortopt, 0, 0, 0, setaction, code, 0, (voidfnp)function }
-
 static const struct cmdinfo cmdinfos[] = {
 	ACTION("add",    0, act_install,   statoverride_add),
 	ACTION("remove", 0, act_remove,    statoverride_remove),