// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: cmndline.cc,v 1.13 2002/09/14 05:29:22 jgg Exp $
+// $Id: cmndline.cc,v 1.15 2003/02/10 01:40:58 doogie Exp $
/* ######################################################################
Command Line Class - Sophisticated command line parser
##################################################################### */
/*}}}*/
// Include files /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/cmndline.h"
-#endif
#include <apt-pkg/cmndline.h>
#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>
#include <apti18n.h>
/*}}}*/
+using namespace std;
// CommandLine::CommandLine - Constructor /*{{{*/
// ---------------------------------------------------------------------
for (; I != argc; I++)
*Files++ = argv[I];
*Files = 0;
-
+
+ SaveInConfig(argc, argv);
+
return true;
}
/*}}}*/
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)
- return _error->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv[I]);
+ return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]);
// = is trailing
if (J[1] == 0)
{
if (I+1 >= argc)
- return _error->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv[I]);
+ return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]);
Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
}
else
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];
+ 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 = &argv[i][j+1];
+ for (; *c != '\0' && *c != ' '; ++c);
+ if (*c == '\0') 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);
+}
+ /*}}}*/