Explorar el Código

Use ACTION macros instead of ad-hoc action function handling

This makes the code uniform with the rest of the code base, removes
the possibility of mismatched entries from cmdinfos and dofunctions
arrays, and reduces the scope of the action pointer to main().
Guillem Jover hace 16 años
padre
commit
a67107c051
Se han modificado 4 ficheros con 25 adiciones y 48 borrados
  1. 0 1
      dpkg-deb/dpkg-deb.h
  2. 14 26
      dpkg-deb/main.c
  3. 0 1
      dpkg-split/dpkg-split.h
  4. 11 20
      dpkg-split/main.c

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

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

+ 14 - 26
dpkg-deb/main.c

@@ -135,7 +135,6 @@ int debugflag=0, nocheckflag=0, oldformatflag=BUILDOLDPKGFORMAT;
 struct compressor *compressor = &compressor_gzip;
 struct compressor *compressor = &compressor_gzip;
 int compress_level = -1;
 int compress_level = -1;
 const struct cmdinfo *cipaction = NULL;
 const struct cmdinfo *cipaction = NULL;
-dofunction *action = NULL;
 
 
 static void setaction(const struct cmdinfo *cip, const char *value);
 static void setaction(const struct cmdinfo *cip, const char *value);
 static void setcompresstype(const struct cmdinfo *cip, const char *value);
 static void setcompresstype(const struct cmdinfo *cip, const char *value);
@@ -156,31 +155,20 @@ set_compress_level(const struct cmdinfo *cip, const char *value)
   compress_level = level;
   compress_level = level;
 }
 }
 
 
-static dofunction *const dofunctions[]= {
-  do_build,
-  do_contents,
-  do_control,
-  do_info,
-  do_field,
-  do_extract,
-  do_vextract,
-  do_fsystarfile,
-  do_showinfo
-};
+#define ACTION(longopt, shortopt, code, function) \
+ { longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
 
 
-/* NB: the entries using setaction must appear first and be in the
- * same order as dofunctions:
- */
 static const struct cmdinfo cmdinfos[]= {
 static const struct cmdinfo cmdinfos[]= {
-  { "build",         'b', 0, NULL,           NULL,         setaction        },
-  { "contents",      'c', 0, NULL,           NULL,         setaction        },
-  { "control",       'e', 0, NULL,           NULL,         setaction        },
-  { "info",          'I', 0, NULL,           NULL,         setaction        },
-  { "field",         'f', 0, NULL,           NULL,         setaction        },
-  { "extract",       'x', 0, NULL,           NULL,         setaction        },
-  { "vextract",      'X', 0, NULL,           NULL,         setaction        },
-  { "fsys-tarfile",  0,   0, NULL,           NULL,         setaction        },
-  { "show",          'W', 0, NULL,           NULL,         setaction        },
+  ACTION("build",         'b', 0, do_build),
+  ACTION("contents",      'c', 0, do_contents),
+  ACTION("control",       'e', 0, do_control),
+  ACTION("info",          'I', 0, do_info),
+  ACTION("field",         'f', 0, do_field),
+  ACTION("extract",       'x', 0, do_extract),
+  ACTION("vextract",      'X', 0, do_vextract),
+  ACTION("fsys-tarfile",  0,   0, do_fsystarfile),
+  ACTION("show",          'W', 0, do_showinfo),
+
   { "new",           0,   0, &oldformatflag, NULL,         NULL,          0 },
   { "new",           0,   0, &oldformatflag, NULL,         NULL,          0 },
   { "old",           0,   0, &oldformatflag, NULL,         NULL,          1 },
   { "old",           0,   0, &oldformatflag, NULL,         NULL,          1 },
   { "debug",         'D', 0, &debugflag,     NULL,         NULL,          1 },
   { "debug",         'D', 0, &debugflag,     NULL,         NULL,          1 },
@@ -198,8 +186,6 @@ static void setaction(const struct cmdinfo *cip, const char *value) {
     badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
     badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
   cipaction= cip;
   cipaction= cip;
-  assert((int)(cip - cmdinfos) < (int)(array_count(dofunctions)));
-  action= dofunctions[cip-cmdinfos];
 }
 }
 
 
 static void setcompresstype(const struct cmdinfo *cip, const char *value) {
 static void setcompresstype(const struct cmdinfo *cip, const char *value) {
@@ -210,6 +196,7 @@ static void setcompresstype(const struct cmdinfo *cip, const char *value) {
 
 
 int main(int argc, const char *const *argv) {
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   jmp_buf ejbuf;
+  dofunction *action;
 
 
   setlocale(LC_NUMERIC, "POSIX");
   setlocale(LC_NUMERIC, "POSIX");
   setlocale(LC_ALL, "");
   setlocale(LC_ALL, "");
@@ -222,6 +209,7 @@ int main(int argc, const char *const *argv) {
   if (!cipaction) badusage(_("need an action option"));
   if (!cipaction) badusage(_("need an action option"));
 
 
   unsetenv("GZIP");
   unsetenv("GZIP");
+  action = (dofunction *)cipaction->farg;
   action(argv);
   action(argv);
   standard_shutdown();
   standard_shutdown();
   exit(0);
   exit(0);

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

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

+ 11 - 20
dpkg-split/main.c

@@ -96,7 +96,6 @@ usage(const struct cmdinfo *cip, const char *value)
 const char thisname[]= SPLITTER;
 const char thisname[]= SPLITTER;
 const char printforhelp[]= N_("Type dpkg-split --help for help.");
 const char printforhelp[]= N_("Type dpkg-split --help for help.");
 
 
-dofunction *action=NULL;
 const struct cmdinfo *cipaction=NULL;
 const struct cmdinfo *cipaction=NULL;
 struct partqueue *queue= NULL;
 struct partqueue *queue= NULL;
 
 
@@ -133,25 +132,17 @@ static void setpartsize(const struct cmdinfo *cip, const char *value) {
              (HEADERALLOWANCE >> 10) + 1);
              (HEADERALLOWANCE >> 10) + 1);
 }
 }
 
 
-static dofunction *const dofunctions[]= {
-  do_split,
-  do_join,
-  do_info,
-  do_auto,
-  do_queue,
-  do_discard,
-};
+#define ACTION(longopt, shortopt, code, function) \
+{ longopt, shortopt, 0, NULL, NULL, setaction, code, NULL, (voidfnp)function }
 
 
-/* NB: the entries using setaction must appear first and be in the
- * same order as dofunctions:
- */
 static const struct cmdinfo cmdinfos[]= {
 static const struct cmdinfo cmdinfos[]= {
-  { "split",        's',  0,  NULL, NULL,             setaction           },
-  { "join",         'j',  0,  NULL, NULL,             setaction           },
-  { "info",         'I',  0,  NULL, NULL,             setaction           },
-  { "auto",         'a',  0,  NULL, NULL,             setaction           },
-  { "listq",        'l',  0,  NULL, NULL,             setaction           },
-  { "discard",      'd',  0,  NULL, NULL,             setaction           },
+  ACTION("split",   's',  0,  do_split),
+  ACTION("join",    'j',  0,  do_join),
+  ACTION("info",    'I',  0,  do_info),
+  ACTION("auto",    'a',  0,  do_auto),
+  ACTION("listq",   'l',  0,  do_queue),
+  ACTION("discard", 'd',  0,  do_discard),
+
   { "help",         'h',  0,  NULL, NULL,             usage               },
   { "help",         'h',  0,  NULL, NULL,             usage               },
   { "version",       0,   0,  NULL, NULL,             printversion        },
   { "version",       0,   0,  NULL, NULL,             printversion        },
   { "depotdir",      0,   1,  NULL, &opt_depotdir,    NULL                },
   { "depotdir",      0,   1,  NULL, &opt_depotdir,    NULL                },
@@ -167,14 +158,13 @@ static void setaction(const struct cmdinfo *cip, const char *value) {
     badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
     badusage(_("conflicting actions -%c (--%s) and -%c (--%s)"),
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
   cipaction= cip;
   cipaction= cip;
-  assert((int)(cip - cmdinfos) < (int)(array_count(dofunctions)));
-  action= dofunctions[cip-cmdinfos];
 }
 }
 
 
 int main(int argc, const char *const *argv) {
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   jmp_buf ejbuf;
   int l;
   int l;
   char *p;
   char *p;
+  dofunction *action;
 
 
   setlocale(LC_ALL, "");
   setlocale(LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
   bindtextdomain(PACKAGE, LOCALEDIR);
@@ -194,6 +184,7 @@ int main(int argc, const char *const *argv) {
   }
   }
 
 
   setvbuf(stdout,NULL,_IONBF,0);
   setvbuf(stdout,NULL,_IONBF,0);
+  action = (dofunction *)cipaction->farg;
   action(argv);
   action(argv);
 
 
   m_output(stderr, _("<standard error>"));
   m_output(stderr, _("<standard error>"));