~ubuntu-branches/debian/squeeze/aptitude/squeeze

« back to all changes in this revision

Viewing changes to src/cmdline_parse.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Burrows
  • Date: 2007-03-13 20:49:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070313204958-l7040344gthwxlvj
Tags: 0.4.4-4
In addition to the bugfix for ssprintf, the last release included
a new test case to ensure that the bug did not recur.  As it turned
out, this test case revealed that both the old and the new code
were buggy on amd64!  vssnprintf now handles its variable argument
list properly, using va_copy in case it has to access the list a
second time. (Closes: #414617)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// cmdline_parse.cc
 
2
 
 
3
#include "cmdline_parse.h"
 
4
 
 
5
#include <apt-pkg/error.h>
 
6
 
 
7
using namespace std;
 
8
 
 
9
void parse_cmdline(vector<arg_parser *> parsers, int argc, char *argv[])
 
10
{
 
11
  // Assumes that argv[0] is useless (ie, the command name)
 
12
  int loc=1;
 
13
 
 
14
  while(loc<argc)
 
15
    {
 
16
      bool done=false;
 
17
 
 
18
      for(int i=0; i<parsers.size() && !done; ++i)
 
19
        done=parsers[i].parse_arg(argc, argv);
 
20
 
 
21
      if(!done)
 
22
        _error->Error("Invalid command-line option '%s'", argv[argc]);
 
23
 
 
24
      if(_error->PendingError())
 
25
        return;
 
26
    }
 
27
}
 
28
 
 
29
class dashopt_parser:public arg_parser
 
30
{
 
31
  char shortopt;
 
32
  char *longopt;
 
33
  sigc::slot0<void> callback;
 
34
 
 
35
public:
 
36
  bool parse_arg(int argc, char *argv[], int &loc)
 
37
  {
 
38
    if(argv[loc][0]=='-')
 
39
      {
 
40
        if(argv[loc][1]=='-')
 
41
          {
 
42
            if(longopt && !strcmp(argv[loc]+2, longopt))
 
43
              {
 
44
                ++loc;
 
45
                return true;
 
46
              }
 
47
            else
 
48
              return false;
 
49
          }
 
50
        else if(shortopt && argv[loc][1]==shortopt && argv[loc][2]='\0')
 
51
          {
 
52
            ++loc;
 
53
            return true;
 
54
          }
 
55
        else
 
56
          return false;
 
57
      }
 
58
    else
 
59
      return false;
 
60
  }
 
61
};
 
62
 
 
63
class dashopt_witharg_parser:public arg_parser
 
64
{
 
65
  char shortopt;
 
66
  char *longopt;
 
67
  sigc::slot1<void, std::string> callback;
 
68
 
 
69
public:
 
70
  bool parse_arg(int argc, char *argv[], int &loc)
 
71
  {
 
72
    if(argv[loc][0]=='-')
 
73
      {
 
74
        if(argv[loc][1]=='-')
 
75
          {
 
76
            if(longopt && !strcmp(argv[loc]+2, longopt))
 
77
              {
 
78
                ++loc;
 
79
                return true;
 
80
              }
 
81
            else
 
82
              return false;
 
83
          }
 
84
        else if(shortopt && argv[loc][1]==shortopt && argv[loc][2]='\0')
 
85
          {
 
86
            ++loc;
 
87
            return true;
 
88
          }
 
89
        else
 
90
          return false;
 
91
      }
 
92
    else
 
93
      return false;
 
94
  }
 
95
};
 
96