This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch master in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=e8dddc5bd19195f48aeeccdc6eb82bfc275e3627 commit e8dddc5bd19195f48aeeccdc6eb82bfc275e3627 Author: Guillem Jover <guil...@debian.org> AuthorDate: Wed Aug 15 05:10:12 2018 +0200 libdpkg: Split pager specific code into its own module We also namespace the pager function with the module name. --- debian/changelog | 1 + lib/dpkg/Makefile.am | 2 ++ lib/dpkg/command.c | 21 ----------- lib/dpkg/command.h | 1 - lib/dpkg/file.c | 3 +- lib/dpkg/libdpkg.map | 3 +- lib/dpkg/{trigname.c => pager.c} | 34 +++++++++++------- lib/dpkg/{program.h => pager.h} | 20 ++++++----- lib/dpkg/t/.gitignore | 1 + lib/dpkg/t/Makefile.am | 1 + lib/dpkg/t/t-command.c | 38 +------------------- lib/dpkg/t/t-pager.c | 75 ++++++++++++++++++++++++++++++++++++++++ po/POTFILES.in | 1 + src/configure.c | 3 +- 14 files changed, 120 insertions(+), 84 deletions(-) diff --git a/debian/changelog b/debian/changelog index c0e1bea44..f8dc624ce 100644 --- a/debian/changelog +++ b/debian/changelog @@ -203,6 +203,7 @@ dpkg (1.19.1) UNRELEASED; urgency=medium - libdpkg: Add new pkg_infodb_reset_dir(). - libdpkg: Add new m_dup() function. - libdpkg: Factor out package stanza printing into its own function. + - libdpkg: Split pager specific code into its own module. * Build system: - Set distribution tarball format to ustar, instead of default v7 format. - Mark PO4A and POD2MAN as precious variables. diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am index 7ecf6639f..58bd403ab 100644 --- a/lib/dpkg/Makefile.am +++ b/lib/dpkg/Makefile.am @@ -80,6 +80,7 @@ libdpkg_la_SOURCES = \ nfmalloc.c \ options.c \ options-parsers.c \ + pager.c \ parse.c \ parsehelp.c \ path.c \ @@ -138,6 +139,7 @@ pkginclude_HEADERS = \ macros.h \ namevalue.h \ options.h \ + pager.h \ parsedump.h \ path.h \ pkg.h \ diff --git a/lib/dpkg/command.c b/lib/dpkg/command.c index b6a79d7c8..c8b8ae5af 100644 --- a/lib/dpkg/command.c +++ b/lib/dpkg/command.c @@ -182,27 +182,6 @@ command_exec(struct command *cmd) ohshite(_("unable to execute %s (%s)"), cmd->name, cmd->filename); } - -/** - * Get a suitable pager. - * - * @return A string representing a pager. - */ -const char * -command_get_pager(void) -{ - const char *pager; - - if (!isatty(1)) - return CAT; - - pager = getenv("PAGER"); - if (str_is_unset(pager)) - pager = DEFAULTPAGER; - - return pager; -} - /** * Execute a shell with a possible command. * diff --git a/lib/dpkg/command.h b/lib/dpkg/command.h index 8cd2af9f1..7d2098a29 100644 --- a/lib/dpkg/command.h +++ b/lib/dpkg/command.h @@ -54,7 +54,6 @@ void command_add_args(struct command *cmd, ...) DPKG_ATTR_SENTINEL; void command_exec(struct command *cmd) DPKG_ATTR_NORET; -const char *command_get_pager(void); void command_shell(const char *cmd, const char *name) DPKG_ATTR_NORET; /** @} */ diff --git a/lib/dpkg/file.c b/lib/dpkg/file.c index 3b3511549..c62c3121f 100644 --- a/lib/dpkg/file.c +++ b/lib/dpkg/file.c @@ -33,6 +33,7 @@ #include <dpkg/i18n.h> #include <dpkg/subproc.h> #include <dpkg/command.h> +#include <dpkg/pager.h> #include <dpkg/fdio.h> #include <dpkg/file.h> @@ -210,7 +211,7 @@ file_show(const char *filename) struct command cmd; const char *pager; - pager = command_get_pager(); + pager = pager_get_exec(); command_init(&cmd, pager, _("showing file on pager")); command_add_arg(&cmd, pager); diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index cc570f09c..b562d7aad 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -181,10 +181,11 @@ LIBDPKG_PRIVATE { command_add_argv; command_add_args; command_exec; - command_get_pager; command_shell; command_destroy; + pager_get_exec; + setcloexec; # Compression support diff --git a/lib/dpkg/trigname.c b/lib/dpkg/pager.c similarity index 63% copy from lib/dpkg/trigname.c copy to lib/dpkg/pager.c index d27114051..a1cb10107 100644 --- a/lib/dpkg/trigname.c +++ b/lib/dpkg/pager.c @@ -1,9 +1,8 @@ /* * libdpkg - Debian packaging suite library routines - * trigname.c - trigger name handling + * pager.c - pager execution support * - * Copyright © 2007 Canonical Ltd - * Written by Ian Jackson <ijack...@chiark.greenend.org.uk> + * Copyright © 2018 Guillem Jover <guil...@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 @@ -22,21 +21,30 @@ #include <config.h> #include <compat.h> +#include <stdlib.h> +#include <unistd.h> + +#include <dpkg/dpkg.h> #include <dpkg/i18n.h> -#include <dpkg/triglib.h> +#include <dpkg/string.h> +#include <dpkg/pager.h> +/** + * Get a suitable pager. + * + * @return A string representing a pager. + */ const char * -trig_name_is_illegal(const char *p) +pager_get_exec(void) { - int c; + const char *pager; - if (!*p) - return _("empty trigger names are not permitted"); + if (!isatty(1)) + return CAT; - while ((c = *p++)) { - if (c <= ' ' || c >= 0177) - return _("trigger name contains invalid character"); - } + pager = getenv("PAGER"); + if (str_is_unset(pager)) + pager = DEFAULTPAGER; - return NULL; + return pager; } diff --git a/lib/dpkg/program.h b/lib/dpkg/pager.h similarity index 72% copy from lib/dpkg/program.h copy to lib/dpkg/pager.h index e4fe4f4a0..8ecc93b97 100644 --- a/lib/dpkg/program.h +++ b/lib/dpkg/pager.h @@ -1,8 +1,8 @@ /* * libdpkg - Debian packaging suite library routines - * program.h - dpkg-based program support + * pager.h - pager execution support * - * Copyright © 2013 Guillem Jover <guil...@debian.org> + * Copyright © 2018 Guillem Jover <guil...@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 @@ -18,24 +18,26 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef LIBDPKG_PROGRAM_H -#define LIBDPKG_PROGRAM_H +#ifndef LIBDPKG_PAGER_H +#define LIBDPKG_PAGER_H #include <dpkg/macros.h> DPKG_BEGIN_DECLS /** - * @defgroup program Program support - * @ingroup dpkg-public + * @defgroup pager Pager execution + * @ingroup dpkg-internal * @{ */ -void dpkg_program_init(const char *progname); -void dpkg_program_done(void); +struct pager; + +const char * +pager_get_exec(void); /** @} */ DPKG_END_DECLS -#endif +#endif /* LIBDPKG_PAGER_H */ diff --git a/lib/dpkg/t/.gitignore b/lib/dpkg/t/.gitignore index 6d5e0abd7..ee1ab73ec 100644 --- a/lib/dpkg/t/.gitignore +++ b/lib/dpkg/t/.gitignore @@ -12,6 +12,7 @@ t-error t-file t-macros t-mod-db +t-pager t-path t-pkginfo t-pkg-list diff --git a/lib/dpkg/t/Makefile.am b/lib/dpkg/t/Makefile.am index e655d72d5..6dff6267a 100644 --- a/lib/dpkg/t/Makefile.am +++ b/lib/dpkg/t/Makefile.am @@ -32,6 +32,7 @@ test_programs = \ t-progname \ t-subproc \ t-command \ + t-pager \ t-varbuf \ t-ar \ t-tar \ diff --git a/lib/dpkg/t/t-command.c b/lib/dpkg/t/t-command.c index b59abd4e4..099884560 100644 --- a/lib/dpkg/t/t-command.c +++ b/lib/dpkg/t/t-command.c @@ -208,44 +208,9 @@ test_command_shell(void) test_pass(ret == 0); } -static void -test_dup_file(int fd, const char *filename, int flags) -{ - int newfd; - - newfd = open(filename, flags); - dup2(newfd, fd); - close(newfd); -} - -static void -test_command_pager(void) -{ - const char *pager, *default_pager; - int origfd = dup(STDOUT_FILENO); - - /* Test stdout being a tty. */ - test_todo_block("environment might not expose controlling terminal") { - test_dup_file(STDOUT_FILENO, "/dev/tty", O_WRONLY); - setenv("PAGER", "test-pager", 1); - pager = command_get_pager(); - unsetenv("PAGER"); - default_pager = command_get_pager(); - dup2(origfd, STDOUT_FILENO); - test_str(pager, ==, "test-pager"); - test_str(default_pager, ==, DEFAULTPAGER); - } - - /* Test stdout not being a tty. */ - test_dup_file(STDOUT_FILENO, "/dev/null", O_WRONLY); - pager = command_get_pager(); - dup2(origfd, STDOUT_FILENO); - test_str(pager, ==, CAT); -} - TEST_ENTRY(test) { - test_plan(52); + test_plan(49); test_command_init(); test_command_grow_argv(); @@ -254,5 +219,4 @@ TEST_ENTRY(test) test_command_add_args(); test_command_exec(); test_command_shell(); - test_command_pager(); } diff --git a/lib/dpkg/t/t-pager.c b/lib/dpkg/t/t-pager.c new file mode 100644 index 000000000..2481e8065 --- /dev/null +++ b/lib/dpkg/t/t-pager.c @@ -0,0 +1,75 @@ +/* + * libdpkg - Debian packaging suite library routines + * t-pager.c - test pager implementation + * + * Copyright © 2010-2012 Guillem Jover <guil...@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 <https://www.gnu.org/licenses/>. + */ + +#include <config.h> +#include <compat.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +#include <dpkg/test.h> +#include <dpkg/pager.h> +#include <dpkg/dpkg.h> + +static void +test_dup_file(int fd, const char *filename, int flags) +{ + int newfd; + + newfd = open(filename, flags); + dup2(newfd, fd); + close(newfd); +} + +static void +test_pager_get_exec(void) +{ + const char *pager, *default_pager; + int origfd = dup(STDOUT_FILENO); + + /* Test stdout being a tty. */ + test_todo_block("environment might not expose controlling terminal") { + test_dup_file(STDOUT_FILENO, "/dev/tty", O_WRONLY); + setenv("PAGER", "test-pager", 1); + pager = pager_get_exec(); + unsetenv("PAGER"); + default_pager = pager_get_exec(); + dup2(origfd, STDOUT_FILENO); + test_str(pager, ==, "test-pager"); + test_str(default_pager, ==, DEFAULTPAGER); + } + + /* Test stdout not being a tty. */ + test_dup_file(STDOUT_FILENO, "/dev/null", O_WRONLY); + pager = pager_get_exec(); + dup2(origfd, STDOUT_FILENO); + test_str(pager, ==, CAT); +} + +TEST_ENTRY(test) +{ + test_plan(3); + + test_pager_get_exec(); +} diff --git a/po/POTFILES.in b/po/POTFILES.in index ce3e29c56..43212d541 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -38,6 +38,7 @@ lib/dpkg/namevalue.c lib/dpkg/nfmalloc.c lib/dpkg/options-parsers.c lib/dpkg/options.c +lib/dpkg/pager.c lib/dpkg/parse.c lib/dpkg/parsehelp.c lib/dpkg/path-remove.c diff --git a/src/configure.c b/src/configure.c index 076abbd9d..d021e2040 100644 --- a/src/configure.c +++ b/src/configure.c @@ -52,6 +52,7 @@ #include <dpkg/path.h> #include <dpkg/subproc.h> #include <dpkg/command.h> +#include <dpkg/pager.h> #include <dpkg/triglib.h> #include <dpkg/db-fsys.h> @@ -206,7 +207,7 @@ show_diff(const char *old, const char *new) sprintf(cmdbuf, DIFF " -Nu %.250s %.250s | %.250s", str_quote_meta(old), str_quote_meta(new), - command_get_pager()); + pager_get_exec()); command_shell(cmdbuf, _("conffile difference visualizer")); } -- Dpkg.Org's dpkg