From e7e10e47476606e3b2274cf66b1e8ea74b236757 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 24 Oct 2015 22:43:37 +0200 Subject: 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 --- apt-private/private-cmndline.cc | 69 +++++++++++++++++++++++++++++++++++++++-- apt-private/private-cmndline.h | 3 +- apt-private/private-main.cc | 32 ++++++++++++++++--- apt-private/private-main.h | 4 ++- 4 files changed, 99 insertions(+), 9 deletions(-) (limited to 'apt-private') 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 @@ -124,6 +124,32 @@ static bool addArgumentsAPTConfig(std::vector &Args, char con else return false; + return true; +} + /*}}}*/ +static bool addArgumentsAPTExtractTemplates(std::vector &Args, char const * const)/*{{{*/ +{ + addArg('t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg); + return true; +} + /*}}}*/ +static bool addArgumentsAPTFTPArchive(std::vector &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 &, char const * const)/*{{{*/ +{ return true; } /*}}}*/ @@ -254,6 +280,12 @@ static bool addArgumentsAPTMark(std::vector &Args, char const addArg('s',"no-act","APT::Mark::Simulate",0); } + return true; +} + /*}}}*/ +static bool addArgumentsAPTSortPkgs(std::vector &Args, char const * const)/*{{{*/ +{ + addArg('s',"source","APT::SortPkgs::Source",0); return true; } /*}}}*/ @@ -299,8 +331,16 @@ std::vector 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 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 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 #include +#include #include @@ -13,14 +14,18 @@ #include -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 -- cgit v1.2.3