ソースを参照

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 15 年 前
コミット
a67107c051
共有4 個のファイルを変更した25 個の追加48 個の削除を含む
  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 const struct cmdinfo *cipaction;
-extern dofunction *action;
 
 void extracthalf(const char *debar, const char *directory,
                  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;
 int compress_level = -1;
 const struct cmdinfo *cipaction = NULL;
-dofunction *action = NULL;
 
 static void setaction(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;
 }
 
-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[]= {
-  { "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 },
   { "old",           0,   0, &oldformatflag, 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)"),
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
   cipaction= cip;
-  assert((int)(cip - cmdinfos) < (int)(array_count(dofunctions)));
-  action= dofunctions[cip-cmdinfos];
 }
 
 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) {
   jmp_buf ejbuf;
+  dofunction *action;
 
   setlocale(LC_NUMERIC, "POSIX");
   setlocale(LC_ALL, "");
@@ -222,6 +209,7 @@ int main(int argc, const char *const *argv) {
   if (!cipaction) badusage(_("need an action option"));
 
   unsetenv("GZIP");
+  action = (dofunction *)cipaction->farg;
   action(argv);
   standard_shutdown();
   exit(0);

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

@@ -49,7 +49,6 @@ struct partqueue {
    */
 };
 
-extern dofunction *action;
 extern const struct cmdinfo *cipaction;
 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 printforhelp[]= N_("Type dpkg-split --help for help.");
 
-dofunction *action=NULL;
 const struct cmdinfo *cipaction=NULL;
 struct partqueue *queue= NULL;
 
@@ -133,25 +132,17 @@ static void setpartsize(const struct cmdinfo *cip, const char *value) {
              (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[]= {
-  { "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               },
   { "version",       0,   0,  NULL, NULL,             printversion        },
   { "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)"),
              cip->oshort, cip->olong, cipaction->oshort, cipaction->olong);
   cipaction= cip;
-  assert((int)(cip - cmdinfos) < (int)(array_count(dofunctions)));
-  action= dofunctions[cip-cmdinfos];
 }
 
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   int l;
   char *p;
+  dofunction *action;
 
   setlocale(LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
@@ -194,6 +184,7 @@ int main(int argc, const char *const *argv) {
   }
 
   setvbuf(stdout,NULL,_IONBF,0);
+  action = (dofunction *)cipaction->farg;
   action(argv);
 
   m_output(stderr, _("<standard error>"));