Commit 07255ee8 authored by Guillem Jover's avatar Guillem Jover

libdpkg: Refactor dpkg_options_parse_pkgname()

parent 25d6273c
......@@ -66,6 +66,7 @@ libdpkg_la_SOURCES = \
namevalue.c \
nfmalloc.c \
options.c \
options-parsers.c \
parse.c \
parsehelp.c \
path.c \
......
......@@ -173,6 +173,7 @@ LIBDPKG_PRIVATE {
dpkg_options_load;
dpkg_options_parse;
dpkg_options_parse_arg_int;
dpkg_options_parse_pkgname;
badusage;
cipaction; # XXX variable, do not export
setaction;
......
/*
* dpkg - main program for package management
* options-helper.c - command-line options parser helpers
*
* Copyright © 2014 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 <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <dpkg/i18n.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/error.h>
#include <dpkg/pkg-spec.h>
#include <dpkg/options.h>
/**
* Parse an argument as a package name.
*
* The string is parsed as a singleton package name, and a #pkginfo instance
* is returned.
*
* @param cmd The command information structure currently parsed.
* @param name The package name argument
*
* @return A package instance.
*/
struct pkginfo *
dpkg_options_parse_pkgname(const struct cmdinfo *cmd, const char *name)
{
struct pkginfo *pkg;
struct dpkg_error err;
pkg = pkg_spec_parse_pkg(name, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cmd->olong, name, err.str);
return pkg;
}
......@@ -3,6 +3,7 @@
* options.h - option parsing functions
*
* Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
* Copyright © 2008-2014 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
......@@ -22,6 +23,7 @@
#define LIBDPKG_OPTIONS_H
#include <dpkg/macros.h>
#include <dpkg/dpkg-db.h>
DPKG_BEGIN_DECLS
......@@ -64,6 +66,9 @@ void dpkg_options_parse(const char *const **argvp,
long dpkg_options_parse_arg_int(const struct cmdinfo *cmd, const char *str);
struct pkginfo *
dpkg_options_parse_pkgname(const struct cmdinfo *cmd, const char *name);
/**
* Current cmdinfo action.
*/
......
......@@ -20,6 +20,7 @@ lib/dpkg/log.c
lib/dpkg/mlib.c
lib/dpkg/nfmalloc.c
lib/dpkg/options.c
lib/dpkg/options-parsers.c
lib/dpkg/parse.c
lib/dpkg/parsehelp.c
lib/dpkg/path.c
......
......@@ -48,7 +48,6 @@
#include <dpkg/arch.h>
#include <dpkg/subproc.h>
#include <dpkg/command.h>
#include <dpkg/pkg-spec.h>
#include <dpkg/options.h>
#include "main.h"
......@@ -370,14 +369,9 @@ set_ignore_depends(const struct cmdinfo *cip, const char *value)
}
p= copy;
while (*p) {
struct dpkg_error err;
struct pkginfo *pkg;
pkg = pkg_spec_parse_pkg(p, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cip->olong, p, err.str);
pkg = dpkg_options_parse_pkgname(cip, p);
pkg_list_prepend(&ignoredependss, pkg);
p+= strlen(p)+1;
......
......@@ -41,7 +41,6 @@
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-list.h>
#include <dpkg/pkg-queue.h>
#include <dpkg/pkg-spec.h>
#include <dpkg/string.h>
#include <dpkg/options.h>
......@@ -108,14 +107,9 @@ enqueue_specified(const char *const *argv)
const char *thisarg;
while ((thisarg = *argv++) != NULL) {
struct dpkg_error err;
struct pkginfo *pkg;
pkg = pkg_spec_parse_pkg(thisarg, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, thisarg, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
if (pkg->status == stat_notinstalled &&
str_match_end(pkg->set->name, DEBEXT)) {
badusage(_("you must specify packages by their own names, "
......
......@@ -4,7 +4,7 @@
*
* Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
* Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
* Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
* Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
* Copyright © 2011 Linaro Limited
* Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
*
......@@ -456,12 +456,7 @@ enqperpackage(const char *const *argv)
modstatdb_open(msdbrw_readonly);
while ((thisarg = *argv++) != NULL) {
struct dpkg_error err;
pkg = pkg_spec_parse_pkg(thisarg, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, thisarg, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
switch (cipaction->arg_int) {
case act_status:
......@@ -684,7 +679,6 @@ control_path_file(struct pkginfo *pkg, const char *control_file)
static int
control_path(const char *const *argv)
{
struct dpkg_error err;
struct pkginfo *pkg;
const char *pkgname;
const char *control_file;
......@@ -703,11 +697,7 @@ control_path(const char *const *argv)
modstatdb_open(msdbrw_readonly);
pkg = pkg_spec_parse_pkg(pkgname, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, pkgname, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
if (pkg->status == stat_notinstalled)
ohshit(_("package '%s' is not installed"),
pkg_name(pkg, pnaw_nonambig));
......@@ -725,7 +715,6 @@ control_path(const char *const *argv)
static int
control_list(const char *const *argv)
{
struct dpkg_error err;
struct pkginfo *pkg;
const char *pkgname;
......@@ -735,11 +724,7 @@ control_list(const char *const *argv)
modstatdb_open(msdbrw_readonly);
pkg = pkg_spec_parse_pkg(pkgname, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, pkgname, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
if (pkg->status == stat_notinstalled)
ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
......@@ -753,7 +738,6 @@ control_list(const char *const *argv)
static int
control_show(const char *const *argv)
{
struct dpkg_error err;
struct pkginfo *pkg;
const char *pkgname;
const char *filename;
......@@ -772,11 +756,7 @@ control_show(const char *const *argv)
modstatdb_open(msdbrw_readonly);
pkg = pkg_spec_parse_pkg(pkgname, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, pkgname, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, pkgname);
if (pkg->status == stat_notinstalled)
ohshit(_("package '%s' is not installed"), pkg_name(pkg, pnaw_nonambig));
......
......@@ -2,7 +2,7 @@
* dpkg - main program for package management
* verify.c - verify package integrity
*
* Copyright © 2012 Guillem Jover <guillem@debian.org>
* Copyright © 2012-2014 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
......@@ -28,7 +28,6 @@
#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/pkg-spec.h>
#include <dpkg/options.h>
#include "filesdb.h"
......@@ -150,13 +149,7 @@ verify(const char *const *argv)
const char *thisarg;
while ((thisarg = *argv++)) {
struct dpkg_error err;
pkg = pkg_spec_parse_pkg(thisarg, &err);
if (pkg == NULL)
badusage(_("--%s needs a valid package name but '%.250s' is not: %s"),
cipaction->olong, thisarg, err.str);
pkg = dpkg_options_parse_pkgname(cipaction, thisarg);
if (pkg->status == stat_notinstalled) {
notice(_("package '%s' is not installed"),
pkg_name(pkg, pnaw_nonambig));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment