diff options
author | David Kalnischkies <david@kalnischkies.de> | 2015-10-24 22:43:37 +0200 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2015-11-04 18:04:03 +0100 |
commit | e7e10e47476606e3b2274cf66b1e8ea74b236757 (patch) | |
tree | 3daed3cc14405879034c562993a7b5399992f2e3 /apt-private | |
parent | cbbee23e7768750ca1c8b49bdfbf8a650131bbb6 (diff) |
deduplicate main methods
All mains pretty much do the same thing, so lets try a little harder to
move the common parts into -private to have the real differences more
visible.
Git-Dch: Ignore
Diffstat (limited to 'apt-private')
-rw-r--r-- | apt-private/private-cmndline.cc | 69 | ||||
-rw-r--r-- | apt-private/private-cmndline.h | 3 | ||||
-rw-r--r-- | apt-private/private-main.cc | 32 | ||||
-rw-r--r-- | apt-private/private-main.h | 4 |
4 files changed, 99 insertions, 9 deletions
diff --git a/apt-private/private-cmndline.cc b/apt-private/private-cmndline.cc index 265c5d482..a0acbbd79 100644 --- a/apt-private/private-cmndline.cc +++ b/apt-private/private-cmndline.cc @@ -127,6 +127,32 @@ static bool addArgumentsAPTConfig(std::vector<CommandLine::Args> &Args, char con return true; } /*}}}*/ +static bool addArgumentsAPTExtractTemplates(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/ +{ + addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg); + return true; +} + /*}}}*/ +static bool addArgumentsAPTFTPArchive(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/ +{ + addArg(0,"md5","APT::FTPArchive::MD5",0); + addArg(0,"sha1","APT::FTPArchive::SHA1",0); + addArg(0,"sha256","APT::FTPArchive::SHA256",0); + addArg(0,"sha512","APT::FTPArchive::SHA512",0); + addArg('d',"db","APT::FTPArchive::DB",CommandLine::HasArg); + addArg('s',"source-override","APT::FTPArchive::SourceOverride",CommandLine::HasArg); + addArg(0,"delink","APT::FTPArchive::DeLinkAct",0); + addArg(0,"readonly","APT::FTPArchive::ReadOnlyDB",0); + addArg(0,"contents","APT::FTPArchive::Contents",0); + addArg('a',"arch","APT::FTPArchive::Architecture",CommandLine::HasArg); + return true; +} + /*}}}*/ +static bool addArgumentsAPTInternalSolver(std::vector<CommandLine::Args> &, char const * const)/*{{{*/ +{ + return true; +} + /*}}}*/ static bool addArgumentsAPTGet(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/ { if (CmdMatches("install", "remove", "purge", "upgrade", "dist-upgrade", @@ -257,6 +283,12 @@ static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &Args, char const return true; } /*}}}*/ +static bool addArgumentsAPTSortPkgs(std::vector<CommandLine::Args> &Args, char const * const)/*{{{*/ +{ + addArg('s',"source","APT::SortPkgs::Source",0); + return true; +} + /*}}}*/ static bool addArgumentsAPT(std::vector<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/ { if (CmdMatches("list")) @@ -299,8 +331,16 @@ std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char c addArgumentsAPTCDROM(Args, Cmd); else if (strcmp(Program, "apt-config") == 0) addArgumentsAPTConfig(Args, Cmd); + else if (strcmp(Program, "apt-extracttemplates") == 0) + addArgumentsAPTExtractTemplates(Args, Cmd); + else if (strcmp(Program, "apt-ftparchive") == 0) + addArgumentsAPTFTPArchive(Args, Cmd); + else if (strcmp(Program, "apt-internal-solver") == 0) + addArgumentsAPTInternalSolver(Args, Cmd); else if (strcmp(Program, "apt-mark") == 0) addArgumentsAPTMark(Args, Cmd); + else if (strcmp(Program, "apt-sortpkg") == 0) + addArgumentsAPTSortPkgs(Args, Cmd); else if (strcmp(Program, "apt") == 0) addArgumentsAPT(Args, Cmd); @@ -340,10 +380,19 @@ static void BinarySpecificConfiguration(char const * const Binary) /*{{{*/ _config->MoveSubTree(conf.c_str(), NULL); } /*}}}*/ -void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, CommandLine::Args * const Args,/*{{{*/ +void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, char const * const Binary,/*{{{*/ Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char *argv[], bool(*ShowHelp)(CommandLine &, CommandLine::DispatchWithHelp const *)) { - CmdL = CommandLine(Args,_config); + // Args running out of scope invalidates the pointer stored in CmdL, + // but we don't use the pointer after this function, so we ignore + // this problem for now and figure something out if we have to. + std::vector<CommandLine::Args> Args; + if (Cmds != nullptr && Cmds[0].Handler != nullptr) + Args = getCommandArgs(Binary, CommandLine::GetCommand(Cmds, argc, argv)); + else + Args = getCommandArgs(Binary, Binary); + CmdL = CommandLine(Args.data(), _config); + if (Cnf != NULL && pkgInitConfig(**Cnf) == false) { _error->DumpErrors(); @@ -377,3 +426,19 @@ void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * C } } /*}}}*/ +unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds) /*{{{*/ +{ + // Match the operation + bool const returned = (Cmds != nullptr) ? CmdL.DispatchArg(Cmds) : true; + + // Print any errors or warnings found during parsing + bool const Errors = _error->PendingError(); + if (_config->FindI("quiet",0) > 0) + _error->DumpErrors(); + else + _error->DumpErrors(GlobalError::DEBUG); + if (returned == false) + return 100; + return Errors == true ? 100 : 0; +} + /*}}}*/ diff --git a/apt-private/private-cmndline.h b/apt-private/private-cmndline.h index 0d6c0bba6..5674088bc 100644 --- a/apt-private/private-cmndline.h +++ b/apt-private/private-cmndline.h @@ -10,8 +10,9 @@ class Configuration; class pkgSystem; APT_PUBLIC std::vector<CommandLine::Args> getCommandArgs(char const * const Program, char const * const Cmd); -APT_PUBLIC void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, CommandLine::Args * const Args, +APT_PUBLIC void ParseCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * Cmds, char const * const Binary, Configuration * const * const Cnf, pkgSystem ** const Sys, int const argc, const char * argv[], bool(*ShowHelp)(CommandLine &, CommandLine::DispatchWithHelp const *)); +APT_PUBLIC unsigned short DispatchCommandLine(CommandLine &CmdL, CommandLine::DispatchWithHelp const * const Cmds); #endif diff --git a/apt-private/private-main.cc b/apt-private/private-main.cc index 3886c7df6..9eb306834 100644 --- a/apt-private/private-main.cc +++ b/apt-private/private-main.cc @@ -2,6 +2,7 @@ #include <apt-pkg/cmndline.h> #include <apt-pkg/configuration.h> +#include <apt-pkg/fileutl.h> #include <apt-private/private-main.h> @@ -13,14 +14,18 @@ #include <apti18n.h> -void InitSignals() +void InitLocale() /*{{{*/ +{ + setlocale(LC_ALL,""); + textdomain(PACKAGE); +} + /*}}}*/ +void InitSignals() /*{{{*/ { - // Setup the signals signal(SIGPIPE,SIG_IGN); } - - -void CheckSimulateMode(CommandLine &CmdL) + /*}}}*/ +void CheckIfSimulateMode(CommandLine &CmdL) /*{{{*/ { // disable locking in simulation, but show the message only for users // as root hasn't the same problems like unreadable files which can heavily @@ -39,3 +44,20 @@ void CheckSimulateMode(CommandLine &CmdL) _config->Set("Debug::NoLocking",true); } } + /*}}}*/ +void CheckIfCalledByScript(int argc, const char *argv[]) /*{{{*/ +{ + if (unlikely(argc < 1)) return; + + if(!isatty(STDOUT_FILENO) && + _config->FindB("Apt::Cmd::Disable-Script-Warning", false) == false) + { + std::cerr << std::endl + << "WARNING: " << flNotDir(argv[0]) << " " + << "does not have a stable CLI interface. " + << "Use with caution in scripts." + << std::endl + << std::endl; + } +} + /*}}}*/ diff --git a/apt-private/private-main.h b/apt-private/private-main.h index a03bf4441..db6d3e0b7 100644 --- a/apt-private/private-main.h +++ b/apt-private/private-main.h @@ -5,7 +5,9 @@ class CommandLine; -APT_PUBLIC void CheckSimulateMode(CommandLine &CmdL); +APT_PUBLIC void InitLocale(); APT_PUBLIC void InitSignals(); +APT_PUBLIC void CheckIfSimulateMode(CommandLine &CmdL); +APT_PUBLIC void CheckIfCalledByScript(int argc, const char *argv[]); #endif |