X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/d22bcfc4b0b2d0056126a1c690f778b1c3ddaf17..cbbee23e7768750ca1c8b49bdfbf8a650131bbb6:/apt-pkg/contrib/cmndline.cc diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index 54c91d67d..60ce90f1f 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -11,11 +11,19 @@ ##################################################################### */ /*}}}*/ // Include files /*{{{*/ +#include + +#include #include #include #include -#include +#include +#include +#include +#include + +#include /*}}}*/ using namespace std; @@ -25,6 +33,9 @@ using namespace std; CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList), Conf(Conf), FileList(0) { +} +CommandLine::CommandLine() : ArgList(NULL), Conf(NULL), FileList(0) +{ } /*}}}*/ // CommandLine::~CommandLine - Destructor /*{{{*/ @@ -35,6 +46,82 @@ CommandLine::~CommandLine() delete [] FileList; } /*}}}*/ +// CommandLine::GetCommand - return the first non-option word /*{{{*/ +char const * CommandLine::GetCommand(Dispatch const * const Map, + unsigned int const argc, char const * const * const argv) +{ + // if there is a -- on the line there must be the word we search for either + // before it (as -- marks the end of the options) or right after it (as we can't + // decide if the command is actually an option, given that in theory, you could + // have parameters named like commands) + for (size_t i = 1; i < argc; ++i) + { + if (strcmp(argv[i], "--") != 0) + continue; + // check if command is before -- + for (size_t k = 1; k < i; ++k) + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[k], Map[j].Match) == 0) + return Map[j].Match; + // see if the next token after -- is the command + ++i; + if (i < argc) + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[i], Map[j].Match) == 0) + return Map[j].Match; + // we found a --, but not a command + return NULL; + } + // no --, so search for the first word matching a command + // FIXME: How like is it that an option parameter will be also a valid Match ? + for (size_t i = 1; i < argc; ++i) + { + if (*(argv[i]) == '-') + continue; + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[i], Map[j].Match) == 0) + return Map[j].Match; + } + return NULL; +} +char const * CommandLine::GetCommand(DispatchWithHelp const * const Map, + unsigned int const argc, char const * const * const argv) +{ + // if there is a -- on the line there must be the word we search for either + // before it (as -- marks the end of the options) or right after it (as we can't + // decide if the command is actually an option, given that in theory, you could + // have parameters named like commands) + for (size_t i = 1; i < argc; ++i) + { + if (strcmp(argv[i], "--") != 0) + continue; + // check if command is before -- + for (size_t k = 1; k < i; ++k) + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[k], Map[j].Match) == 0) + return Map[j].Match; + // see if the next token after -- is the command + ++i; + if (i < argc) + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[i], Map[j].Match) == 0) + return Map[j].Match; + // we found a --, but not a command + return NULL; + } + // no --, so search for the first word matching a command + // FIXME: How like is it that an option parameter will be also a valid Match ? + for (size_t i = 1; i < argc; ++i) + { + if (*(argv[i]) == '-') + continue; + for (size_t j = 0; Map[j].Match != NULL; ++j) + if (strcmp(argv[i], Map[j].Match) == 0) + return Map[j].Match; + } + return NULL; +} + /*}}}*/ // CommandLine::Parse - Main action member /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -74,7 +161,7 @@ bool CommandLine::Parse(int argc,const char **argv) Args *A; for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); if (A->end() == true) - return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]); + return _error->Error(_("Command line option '%c' [from %s] is not understood in combination with the other options."),*Opt,argv[I]); if (HandleOpt(I,argc,argv,Opt,A) == false) return false; @@ -87,28 +174,28 @@ bool CommandLine::Parse(int argc,const char **argv) Opt++; // Match up to a = against the list - const char *OptEnd = Opt; Args *A; - for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++); - for (A = ArgList; A->end() == false && - stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); + const char *OptEnd = strchrnul(Opt, '='); + for (A = ArgList; A->end() == false && + (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); + ++A); // Failed, look for a word after the first - (no-foo) bool PreceedMatch = false; if (A->end() == true) { - for (; Opt != OptEnd && *Opt != '-'; Opt++); - - if (Opt == OptEnd) - return _error->Error(_("Command line option %s is not understood"),argv[I]); + Opt = (const char*) memchr(Opt, '-', OptEnd - Opt); + if (Opt == NULL) + return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); Opt++; for (A = ArgList; A->end() == false && - stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); + (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); + ++A); // Failed again.. if (A->end() == true && OptEnd - Opt != 1) - return _error->Error(_("Command line option %s is not understood"),argv[I]); + return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); // The option could be a single letter option prefixed by a no-.. if (A->end() == true) @@ -116,7 +203,7 @@ bool CommandLine::Parse(int argc,const char **argv) for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); if (A->end() == true) - return _error->Error(_("Command line option %s is not understood"),argv[I]); + return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); } // The option is not boolean @@ -135,7 +222,9 @@ bool CommandLine::Parse(int argc,const char **argv) for (; I != argc; I++) *Files++ = argv[I]; *Files = 0; - + + SaveInConfig(argc, argv); + return true; } /*}}}*/ @@ -189,12 +278,11 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], if ((A->Flags & ConfigFile) == ConfigFile) return ReadConfigFile(*Conf,Argument); - // Arbitary item specification + // Arbitrary item specification if ((A->Flags & ArbItem) == ArbItem) { - const char *J; - for (J = Argument; *J != 0 && *J != '='; J++); - if (*J == 0) + const char *J = strchr(Argument, '='); + if (J == NULL) return _error->Error(_("Option %s: Configuration item specification must have an =."),argv[I]); // = is trailing @@ -210,8 +298,7 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], return true; } - const char *I = A->ConfName; - for (; *I != 0 && *I != ' '; I++); + const char *I = strchrnul(A->ConfName, ' '); if (*I == ' ') Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument); else @@ -254,7 +341,7 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], // Look for an argument. while (1) { - // Look at preceeding text + // Look at preceding text char Buffer[300]; if (Argument == 0) { @@ -267,10 +354,9 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], // Skip the leading dash const char *J = argv[I]; for (; *J != 0 && *J == '-'; J++); - - const char *JEnd = J; - for (; *JEnd != 0 && *JEnd != '-'; JEnd++); - if (*JEnd != 0) + + const char *JEnd = strchr(J, '-'); + if (JEnd != NULL) { strncpy(Buffer,J,JEnd - J); Buffer[JEnd - J] = 0; @@ -325,8 +411,29 @@ unsigned int CommandLine::FileSize() const } /*}}}*/ // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ -// --------------------------------------------------------------------- -/* */ +bool CommandLine::DispatchArg(DispatchWithHelp *Map,bool NoMatch) +{ + int I; + for (I = 0; Map[I].Match != 0; I++) + { + if (strcmp(FileList[0],Map[I].Match) == 0) + { + bool Res = Map[I].Handler(*this); + if (Res == false && _error->PendingError() == false) + _error->Error("Handler silently failed"); + return Res; + } + } + + // No matching name + if (Map[I].Match == 0) + { + if (NoMatch == true) + _error->Error(_("Invalid operation %s"),FileList[0]); + } + + return false; +} bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) { int I; @@ -351,3 +458,53 @@ bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) return false; } /*}}}*/ +// CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/ +// --------------------------------------------------------------------- +/* We save the commandline here to have it around later for e.g. logging. + It feels a bit like a hack here and isn't bulletproof, but it is better + than nothing after all. */ +void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv) +{ + char cmdline[100 + argc * 50]; + memset(cmdline, 0, sizeof(cmdline)); + unsigned int length = 0; + bool lastWasOption = false; + bool closeQuote = false; + for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length) + { + for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length) + { + cmdline[length] = argv[i][j]; + if (lastWasOption == true && argv[i][j] == '=') + { + // That is possibly an option: Quote it if it includes spaces, + // the benefit is that this will eliminate also most false positives + const char* c = strchr(&argv[i][j+1], ' '); + if (c == NULL) continue; + cmdline[++length] = '"'; + closeQuote = true; + } + } + if (closeQuote == true) + cmdline[length++] = '"'; + // Problem: detects also --hello + if (cmdline[length-1] == 'o') + lastWasOption = true; + cmdline[length] = ' '; + } + cmdline[--length] = '\0'; + _config->Set("CommandLine::AsString", cmdline); +} + /*}}}*/ +CommandLine::Args CommandLine::MakeArgs(char ShortOpt, char const *LongOpt, char const *ConfName, unsigned long Flags)/*{{{*/ +{ + /* In theory, this should be a constructor for CommandLine::Args instead, + but this breaks compatibility as gcc thinks this is a c++11 initializer_list */ + CommandLine::Args arg; + arg.ShortOpt = ShortOpt; + arg.LongOpt = LongOpt; + arg.ConfName = ConfName; + arg.Flags = Flags; + return arg; +} + /*}}}*/