From: Adam Heath Date: Fri, 24 May 2002 05:16:43 +0000 (+0000) Subject: New functions, standard_startup, standard_shutdown, that contain the most X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3dc5dd229b5044494db300e6fef7fd47363ce0b6;p=dpkg New functions, standard_startup, standard_shutdown, that contain the most common functions that all dpkg binaries call. --- diff --git a/ChangeLog b/ChangeLog index b65e8fac..7e5b8b01 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri May 24 00:11:01 CDT 2002 Adam Heath + + * 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 * lib/lock.c: Use setcloexec wrapper instead of calling fcntl directly. diff --git a/dpkg-deb/main.c b/dpkg-deb/main.c index 910a3b50..54b10618 100644 --- a/dpkg-deb/main.c +++ b/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); } diff --git a/dselect/main.cc b/dselect/main.cc index 009a2d38..831bfef9 100644 --- a/dselect/main.cc +++ b/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); } diff --git a/include/dpkg.h.in b/include/dpkg.h.in index aa67011f..06c4074d 100644 --- a/include/dpkg.h.in +++ b/include/dpkg.h.in @@ -28,6 +28,8 @@ #include #include +#include + #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, diff --git a/include/myopt.h b/include/myopt.h index dd7b9326..cb59f824 100644 --- a/include/myopt.h +++ b/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 */ diff --git a/lib/Makefile.in b/lib/Makefile.in index 05b91e57..74484383 100644 --- a/lib/Makefile.in +++ b/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 diff --git a/lib/myopt.c b/lib/myopt.c index b769a1e3..9d7c044c 100644 --- a/lib/myopt.c +++ b/lib/myopt.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -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; diff --git a/lib/startup.c b/lib/startup.c new file mode 100644 index 00000000..32c835de --- /dev/null +++ b/lib/startup.c @@ -0,0 +1,63 @@ +/* + * dpkg - main program for package management + * main.c - main program + * + * Copyright (C) 1994,1995 Ian Jackson + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +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); +} diff --git a/main/main.c b/main/main.c index 8feb679e..407702b9 100644 --- a/main/main.c +++ b/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(); } diff --git a/main/query.c b/main/query.c index ac0a309e..9b8c69cb 100644 --- a/main/query.c +++ b/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(); } diff --git a/split/main.c b/split/main.c index 6cbb0d3e..0f9d77b0 100644 --- a/split/main.c +++ b/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); }