ソースを参照

libdpkg: Add dpkg-based program startup and shutdown functions

These will perform any necessary action when starting and exiting a
dpkg-based program.
Guillem Jover 12 年 前
コミット
0c977fa968
共有12 個のファイルを変更した118 個の追加45 個の削除を含む
  1. 2 4
      dpkg-deb/main.c
  2. 2 5
      dpkg-split/main.c
  3. 2 2
      dselect/main.cc
  4. 2 0
      lib/dpkg/Makefile.am
  5. 1 12
      lib/dpkg/dpkg.h
  6. 57 0
      lib/dpkg/program.c
  7. 41 0
      lib/dpkg/program.h
  8. 3 4
      src/divertcmd.c
  9. 2 5
      src/main.c
  10. 2 4
      src/querycmd.c
  11. 2 4
      src/statcmd.c
  12. 2 5
      src/trigcmd.c

+ 2 - 4
dpkg-deb/main.c

@@ -23,7 +23,6 @@
 #include <compat.h>
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/wait.h>
 
 #include <assert.h>
@@ -257,8 +256,7 @@ int main(int argc, const char *const *argv) {
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
 
-  dpkg_set_progname(BACKEND);
-  standard_startup();
+  dpkg_program_init(BACKEND);
   myopt(&argv, cmdinfos, printforhelp);
 
   if (!cipaction) badusage(_("need an action option"));
@@ -270,7 +268,7 @@ int main(int argc, const char *const *argv) {
 
   ret = cipaction->action(argv);
 
-  standard_shutdown();
+  dpkg_program_done();
 
   return ret;
 }

+ 2 - 5
dpkg-split/main.c

@@ -22,7 +22,6 @@
 #include <compat.h>
 
 #include <sys/types.h>
-#include <sys/stat.h>
 
 #include <assert.h>
 #include <errno.h>
@@ -160,9 +159,7 @@ int main(int argc, const char *const *argv) {
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
 
-  dpkg_set_progname(SPLITTER);
-  dpkg_set_report_buffer(stdout);
-  standard_startup();
+  dpkg_program_init(SPLITTER);
   myopt(&argv, cmdinfos, printforhelp);
 
   admindir = dpkg_db_set_dir(admindir);
@@ -175,7 +172,7 @@ int main(int argc, const char *const *argv) {
 
   m_output(stderr, _("<standard error>"));
 
-  standard_shutdown();
+  dpkg_program_done();
 
   return ret;
 }

+ 2 - 2
dselect/main.cc

@@ -24,7 +24,6 @@
 #include <compat.h>
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/wait.h>
 
 #include <assert.h>
@@ -536,6 +535,7 @@ main(int, const char *const *argv)
   }
 
   cursesoff();
-  standard_shutdown();
+  dpkg_program_done();
+
   return(0);
 }

+ 2 - 0
lib/dpkg/Makefile.am

@@ -62,6 +62,7 @@ libdpkg_a_SOURCES = \
 	pkg-show.c \
 	pkg-spec.c \
 	progname.c \
+	program.c \
 	progress.c \
 	report.c \
 	string.c \
@@ -107,6 +108,7 @@ pkginclude_HEADERS = \
 	pkg-show.h \
 	pkg-spec.h \
 	progname.h \
+	program.h \
 	progress.h \
 	report.h \
 	string.h \

+ 1 - 12
lib/dpkg/dpkg.h

@@ -119,18 +119,7 @@ DPKG_BEGIN_DECLS
 #include <dpkg/progname.h>
 #include <dpkg/ehandle.h>
 #include <dpkg/report.h>
-
-/*** from startup.c ***/
-
-#define standard_startup() do { \
-  push_error_context(); \
-  /* Make sure all our status databases are readable. */ \
-  umask(022); \
-} while (0)
-
-#define standard_shutdown() do { \
-  pop_error_context(ehflag_normaltidy); \
-} while (0)
+#include <dpkg/program.h>
 
 /*** log.c ***/
 

+ 57 - 0
lib/dpkg/program.c

@@ -0,0 +1,57 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * program.c - dpkg-based program support
+ *
+ * Copyright © 2013 Guillem Jover <guillem@debian.org>
+ *
+ * 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <sys/stat.h>
+
+#include <stdio.h>
+
+#include <dpkg/progname.h>
+#include <dpkg/report.h>
+#include <dpkg/ehandle.h>
+#include <dpkg/program.h>
+
+/**
+ * Standard initializations when starting a dpkg-based program.
+ *
+ * @param progname The program name.
+ */
+void
+dpkg_program_init(const char *progname)
+{
+	dpkg_set_progname(progname);
+	dpkg_set_report_buffer(stdout);
+
+	push_error_context();
+
+	/* Set sane default permissions for newly created files. */
+	umask(022);
+}
+
+/**
+ * Standard cleanups before terminating a dpkg-based program.
+ */
+void
+dpkg_program_done(void)
+{
+	pop_error_context(ehflag_normaltidy);
+}

+ 41 - 0
lib/dpkg/program.h

@@ -0,0 +1,41 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * program.h - dpkg-based program support
+ *
+ * Copyright © 2013 Guillem Jover <guillem@debian.org>
+ *
+ * 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 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBDPKG_PROGRAM_H
+#define LIBDPKG_PROGRAM_H
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+/**
+ * @defgroup program Program support
+ * @ingroup dpkg-public
+ * @{
+ */
+
+void dpkg_program_init(const char *progname);
+void dpkg_program_done(void);
+
+/** @} */
+
+DPKG_END_DECLS
+
+#endif

+ 3 - 4
src/divertcmd.c

@@ -771,9 +771,7 @@ main(int argc, const char * const *argv)
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	dpkg_set_progname("dpkg-divert");
-	dpkg_set_report_buffer(stdout);
-	standard_startup();
+	dpkg_program_init("dpkg-divert");
 	myopt(&argv, cmdinfos, printforhelp);
 
 	admindir = dpkg_db_set_dir(admindir);
@@ -792,7 +790,8 @@ main(int argc, const char * const *argv)
 	ret = cipaction->action(argv);
 
 	modstatdb_shutdown();
-	standard_shutdown();
+
+	dpkg_program_done();
 
 	return ret;
 }

+ 2 - 5
src/main.c

@@ -25,7 +25,6 @@
 #include <compat.h>
 
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/wait.h>
 
 #include <errno.h>
@@ -841,9 +840,7 @@ int main(int argc, const char *const *argv) {
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
 
-  dpkg_set_progname("dpkg");
-  dpkg_set_report_buffer(stdout);
-  standard_startup();
+  dpkg_program_init("dpkg");
   loadcfgfile(DPKG, cmdinfos);
   myopt(&argv, cmdinfos, printforhelp);
 
@@ -870,7 +867,7 @@ int main(int argc, const char *const *argv) {
   if (is_invoke_action(cipaction->arg_int))
     run_invoke_hooks(cipaction->olong, post_invoke_hooks);
 
-  standard_shutdown();
+  dpkg_program_done();
 
   return reportbroken_retexitstatus(ret);
 }

+ 2 - 4
src/querycmd.c

@@ -865,9 +865,7 @@ int main(int argc, const char *const *argv) {
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
 
-  dpkg_set_progname("dpkg-query");
-  dpkg_set_report_buffer(stdout);
-  standard_startup();
+  dpkg_program_init("dpkg-query");
   myopt(&argv, cmdinfos, printforhelp);
 
   admindir = dpkg_db_set_dir(admindir);
@@ -878,7 +876,7 @@ int main(int argc, const char *const *argv) {
 
   ret = cipaction->action(argv);
 
-  standard_shutdown();
+  dpkg_program_done();
 
   return !!ret;
 }

+ 2 - 4
src/statcmd.c

@@ -350,9 +350,7 @@ main(int argc, const char *const *argv)
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	dpkg_set_progname("dpkg-statoverride");
-	dpkg_set_report_buffer(stdout);
-	standard_startup();
+	dpkg_program_init("dpkg-statoverride");
 	myopt(&argv, cmdinfos, printforhelp);
 
 	admindir = dpkg_db_set_dir(admindir);
@@ -365,7 +363,7 @@ main(int argc, const char *const *argv)
 
 	ret = cipaction->action(argv);
 
-	standard_shutdown();
+	dpkg_program_done();
 
 	return ret;
 }

+ 2 - 5
src/trigcmd.c

@@ -24,7 +24,6 @@
 
 #include <sys/types.h>
 #include <sys/ioctl.h>
-#include <sys/stat.h>
 #include <sys/termios.h>
 
 #include <fcntl.h>
@@ -215,9 +214,7 @@ main(int argc, const char *const *argv)
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	dpkg_set_progname("dpkg-trigger");
-	dpkg_set_report_buffer(stdout);
-	standard_startup();
+	dpkg_program_init("dpkg-trigger");
 	myopt(&argv, cmdinfos, printforhelp);
 
 	admindir = dpkg_db_set_dir(admindir);
@@ -256,7 +253,7 @@ main(int argc, const char *const *argv)
 		trigdef_process_done();
 	}
 
-	standard_shutdown();
+	dpkg_program_done();
 
 	return 0;
 }