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

New functions, standard_startup, standard_shutdown, that contain the most
common functions that all dpkg binaries call.

Adam Heath пре 24 година
родитељ
комит
3dc5dd229b
11 измењених фајлова са 109 додато и 72 уклоњено
  1. 7 0
      ChangeLog
  2. 2 12
      dpkg-deb/main.c
  3. 2 10
      dselect/main.cc
  4. 7 0
      include/dpkg.h.in
  5. 1 1
      include/myopt.h
  6. 1 1
      lib/Makefile.in
  7. 20 0
      lib/myopt.c
  8. 63 0
      lib/startup.c
  9. 2 22
      main/main.c
  10. 2 14
      main/query.c
  11. 2 12
      split/main.c

+ 7 - 0
ChangeLog

@@ -1,3 +1,10 @@
+Fri May 24 00:11:01 CDT 2002 Adam Heath <doogie@debian.org>
+
+  * include/dpkg.h.in, include/myopt.h, lib/Makefile.in, lib/myopt.c,
+    main/main.c main/query.c dpkg-deb/main.c split/main.c dselect/main.cc,
+    lib/startup.c: New functions, standard_startup, standard_shutdown, that
+    contain the most common functions that all dpkg binaries call.
+
 Thu May 23 23:23:03 CDT 2002 Adam Heath <doogie@debian.org>
 
   * lib/lock.c: Use setcloexec wrapper instead of calling fcntl directly.

+ 2 - 12
dpkg-deb/main.c

@@ -164,23 +164,13 @@ int main(int argc, const char *const *argv) NONRETURNING;
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
 
-  setlocale(LC_ALL, "");
   setlocale(LC_NUMERIC, "POSIX");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
-    error_unwind(ehflag_bombout); exit(2);
-  }
-  push_error_handler(&ejbuf,print_error_fatal,0);
-
-  myopt(&argv,cmdinfos);
+  standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
   if (!cipaction) badusage(_("need an action option"));
 
   unsetenv("GZIP");
   action(argv);
-  set_error_display(0,0);
-  error_unwind(ehflag_normaltidy);
+  standard_shutdown();
   exit(0);
 }
 

+ 2 - 10
dselect/main.cc

@@ -467,7 +467,6 @@ urqresult urq_quit(void) {
 
 int main(int, const char *const *argv) {
   jmp_buf ejbuf;
-  char *home, *homerc;
 
   setlocale(LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
@@ -479,13 +478,7 @@ int main(int, const char *const *argv) {
   }
   push_error_handler(&ejbuf,print_error_fatal,0);
 
-  myfileopt(CONFIGDIR "/" DSELECT ".cfg", cmdinfos);
-  if ((home= getenv("HOME")) != NULL) {
-       homerc= (char*)malloc((strlen(home)+strlen("/." DSELECT ".cfg")+1)*sizeof(char));
-       sprintf(homerc, "%s/.%s.cfg", home, DSELECT);
-       myfileopt(homerc, cmdinfos);
-       free(homerc);
-  }
+  loadcfgfile(DSELECT, cmdinfos);
   myopt(&argv,cmdinfos);
 
   if (*argv) {
@@ -501,8 +494,7 @@ int main(int, const char *const *argv) {
   }
 
   cursesoff();
-  set_error_display(0,0);
-  error_unwind(ehflag_normaltidy);
+  standard_shutdown();
   return(0);
 }
 

+ 7 - 0
include/dpkg.h.in

@@ -28,6 +28,8 @@
 #include <stdio.h>
 #include <sys/types.h>
 
+#include <myopt.h>
+
 #define ARCHIVEVERSION     "2.0"
 #define SPLITVERSION       "2.1"
 #define OLDARCHIVEVERSION  "0.939000"
@@ -163,6 +165,11 @@
 extern const char thisname[]; /* defined separately in each program */
 extern const char printforhelp[];
 
+/*** from startup.c ***/
+
+void standard_startup(jmp_buf *ejbuf, int argc, const char *const **argv, const char *prog, int loadcfg, const struct cmdinfo cmdinfos[]);
+void standard_shutdown(void);
+
 /*** from ehandle.c ***/
 
 void push_error_handler(jmp_buf *jbufp,

+ 1 - 1
include/myopt.h

@@ -38,5 +38,5 @@ struct cmdinfo {
 
 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);
 #endif /* MYOPT_H */

+ 1 - 1
lib/Makefile.in

@@ -9,7 +9,7 @@ include ../Makefile.conf
 SOURCES		= compat.c database.c dbmodify.c dump.c ehandle.c fields.c \
 		    lock.c mlib.c myopt.c nfmalloc.c parse.c parsehelp.c \
 		    showcright.c showpkg.c tarfn.c varbuf.c vercmp.c md5.c \
-		    utils.c
+		    utils.c startup.c
 
 OBJECTS		= $(patsubst %.c, %.o, $(SOURCES))
 GENFILES	= $(OBJECTS) libdpkg.a

+ 20 - 0
lib/myopt.c

@@ -23,6 +23,7 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+#include <stdlib.h>
 
 #include <config.h>
 #include <myopt.h>
@@ -84,6 +85,25 @@ void myfileopt(const char* fn, const struct cmdinfo* cmdinfos) {
   if (fclose(file)) ohshite(_("error closing configuration file `%.255s'"), fn);
 }
 
+void loadcfgfile(const char *prog, const struct cmdinfo* cmdinfos) {
+  char *home, *file;
+  int l1, l2;
+  l1 = strlen(CONFIGDIR "/.cfg") + strlen(prog);
+  file = malloc(l1 + 1);
+  sprintf(file, CONFIGDIR "/%s.cfg", prog);
+  myfileopt(file, cmdinfos);
+  if ((home = getenv("HOME")) != NULL) {
+    l2 = strlen(home) + strlen("/.cfg") + strlen(prog);
+    if (l2 > l1) {
+      free(file);
+      file = malloc(l2 + 1);
+      l1 = l2;
+    }
+    myfileopt(file, cmdinfos);
+  }
+  free(file);
+}
+
 void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) {
   const struct cmdinfo *cip;
   const char *p, *value;

+ 63 - 0
lib/startup.c

@@ -0,0 +1,63 @@
+/*
+ * dpkg - main program for package management
+ * main.c - main program
+ *
+ * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <limits.h>
+#include <ctype.h>
+
+#include <config.h>
+#include <dpkg.h>
+#include <dpkg-db.h>
+#include <version.h>
+#include <myopt.h>
+
+void standard_startup(jmp_buf *ejbuf, int argc, const char *const **argv, const char *prog, int loadcfg, const struct cmdinfo cmdinfos[]) {
+
+  setlocale(LC_ALL, "");
+  bindtextdomain(PACKAGE, LOCALEDIR);
+  textdomain(PACKAGE);
+
+  if (setjmp(*ejbuf)) { /* expect warning about possible clobbering of argv */
+    error_unwind(ehflag_bombout); exit(2);
+  }
+  push_error_handler(ejbuf,print_error_fatal,0);
+
+  umask(022); /* Make sure all our status databases are readable. */
+ 
+  if (loadcfg)
+    loadcfgfile(prog, cmdinfos);
+
+  myopt(argv,cmdinfos);
+}
+
+void standard_shutdown(void) {
+  set_error_display(0,0);
+  error_unwind(ehflag_normaltidy);
+}

+ 2 - 22
main/main.c

@@ -547,27 +547,8 @@ printf("line=`%*s'\n",(int)linevb.used,linevb.buf);
 int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   static void (*actionfunction)(const char *const *argv);
-  char *home, *homerc;
 
-  setlocale(LC_ALL, "");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
-    error_unwind(ehflag_bombout); exit(2);
-  }
-  push_error_handler(&ejbuf,print_error_fatal,0);
-
-  umask(022); /* Make sure all our status databases are readable. */
- 
-  myfileopt(CONFIGDIR "/" DPKG ".cfg", cmdinfos);
-  if ((home= getenv("HOME")) != NULL) {
-       homerc= (char*)malloc((strlen(home)+strlen("/." DPKG ".cfg")+1)*sizeof(char));
-       sprintf(homerc, "%s/.%s.cfg", home, DPKG);
-       myfileopt(homerc, cmdinfos);
-       free(homerc);
-  }
-  myopt(&argv,cmdinfos);
+  standard_startup(&ejbuf, argc, &argv, DPKG, 1, cmdinfos);
   if (!cipaction) badusage(_("need an action option"));
 
   setvbuf(stdout,0,_IONBF,0);
@@ -577,8 +558,7 @@ int main(int argc, const char *const *argv) {
 
   actionfunction(argv);
 
-  set_error_display(0,0);
-  error_unwind(ehflag_normaltidy);
+  standard_shutdown();
 
   return reportbroken_retexitstatus();
 }

+ 2 - 14
main/query.c

@@ -533,18 +533,7 @@ int main(int argc, const char *const *argv) {
   jmp_buf ejbuf;
   static void (*actionfunction)(const char *const *argv);
 
-  setlocale(LC_ALL, "");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
-    error_unwind(ehflag_bombout); exit(2);
-  }
-  push_error_handler(&ejbuf,print_error_fatal,0);
-
-  umask(022); /* Make sure all our status databases are readable. */
- 
-  myopt(&argv,cmdinfos);
+  standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
   if (!cipaction) badusage(_("need an action option"));
 
   setvbuf(stdout,0,_IONBF,0);
@@ -554,8 +543,7 @@ int main(int argc, const char *const *argv) {
 
   actionfunction(argv);
 
-  set_error_display(0,0);
-  error_unwind(ehflag_normaltidy);
+  standard_shutdown();
 
   return reportbroken_retexitstatus();
 }

+ 2 - 12
split/main.c

@@ -153,16 +153,7 @@ int main(int argc, const char *const *argv) {
   int l;
   char *p;
 
-  setlocale(LC_ALL, "");
-  bindtextdomain(PACKAGE, LOCALEDIR);
-  textdomain(PACKAGE);
-
-  if (setjmp(ejbuf)) { /* expect warning about possible clobbering of argv */
-    error_unwind(ehflag_bombout); exit(2);
-  }
-  push_error_handler(&ejbuf,print_error_fatal,NULL);
-
-  myopt(&argv,cmdinfos);
+  standard_startup(&ejbuf, argc, &argv, NULL, 0, cmdinfos);
   if (!cipaction) badusage(_("need an action option"));
 
   l= strlen(depotdir);
@@ -178,7 +169,6 @@ int main(int argc, const char *const *argv) {
 
   if (ferror(stderr)) werr("stderr");
   
-  set_error_display(NULL,NULL);
-  error_unwind(ehflag_normaltidy);
+  standard_shutdown();
   exit(0);
 }