]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/cmndline.cc
merged from donkult
[apt.git] / apt-pkg / contrib / cmndline.cc
index 54c91d67d919986cb0ca4514f0f9b58626fa1b93..997f26bc7541b61a2d106256658c94d82c0a69e8 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include files                                                       /*{{{*/
+#include<config.h>
+
+#include <apt-pkg/configuration.h>
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 
 #include <apt-pkg/cmndline.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 
-#include <apti18n.h>    
+#include <apti18n.h>
                                                                        /*}}}*/
 using namespace std;
 
                                                                        /*}}}*/
 using namespace std;
 
@@ -135,7 +138,9 @@ bool CommandLine::Parse(int argc,const char **argv)
    for (; I != argc; I++)
       *Files++ = argv[I];
    *Files = 0;
    for (; I != argc; I++)
       *Files++ = argv[I];
    *Files = 0;
-   
+
+   SaveInConfig(argc, argv);
+
    return true;
 }
                                                                        /*}}}*/
    return true;
 }
                                                                        /*}}}*/
@@ -189,7 +194,7 @@ bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
       if ((A->Flags & ConfigFile) == ConfigFile)
         return ReadConfigFile(*Conf,Argument);
 
       if ((A->Flags & ConfigFile) == ConfigFile)
         return ReadConfigFile(*Conf,Argument);
 
-      // Arbitary item specification
+      // Arbitrary item specification
       if ((A->Flags & ArbItem) == ArbItem)
       {
         const char *J;
       if ((A->Flags & ArbItem) == ArbItem)
       {
         const char *J;
@@ -351,3 +356,41 @@ bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
    return false;
 }
                                                                        /*}}}*/
    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);
+}
+                                                                       /*}}}*/