summaryrefslogtreecommitdiff
path: root/src/generic/apt/parse_dpkg_status.cc
blob: 492b4d6b8cc6c5083ddd16fa362d4c906a0e6a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// parse_dpkg_status.cc
//
//   Copyright (C) 2008 Daniel Burrows
//
//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.

#include "parse_dpkg_status.h"

#include <aptitude.h>

#include <stdlib.h>

namespace aptitude
{
  namespace apt
  {
    namespace
    {
      // Status constants.
      const std::string status_pmerror("pmerror");
      const std::string status_pmconffile("pmconffile");

      // Parses [[:space:]]* and returns the next character.
      const char *parse_whitespace(const char * &start,
				   const char * const end)
      {
	while(start < end && isspace(*start))
	  ++start;

	return start;
      }

      // Parses [^:]*:, stripping whitespace and returning the text.
      std::string parse_colon_fragment(const char * &start,
				       const char * const end)
      {
	const char * const begin = parse_whitespace(start, end);

	while(start < end && *start != ':')
	  ++start;

	const char *last = start;
	if(start < end)
	  ++start;

	while(last != begin && isspace(last[-1]))
	  --last;

	return std::string(begin, last);
      }

      // Parses '[^']', returning the enclosed text.
      std::string parse_single_quoted_string(const char * start,
					     const char * const end)
      {
	parse_whitespace(start, end);
	if(start == end || *start != '\'')
	  // Erk!
	  return _("<aptitude: internal parse error: no apostrophe>");

	++start;
	const char * const rval_begin = start;
	while(start < end && *start != '\'')
	  ++start;

	if(start >= end)
	  return _("<aptitude: internal parse error: missing terminal apostrophe>");

	const char * const rval_end = start;
	++start;

	return std::string(rval_begin, rval_end);
      }

      std::string strip_range(const char *begin, const char *end)
      {
	while(begin < end && isspace(*begin))
	  ++begin;
	while(end > begin && isspace(end[-1]))
	  --end;

	return std::string(begin, end);
      }
    }

    std::ostream &operator<<(std::ostream &out, const dpkg_status_message &msg)
    {
      switch(msg.get_type())
	{
	case dpkg_status_message::error:
	  out << "error(percent = "
	      << msg.get_percent()
	      << ", package = "
	      << msg.get_package()
	      << ", text = "
	      << msg.get_text()
	      << ")";
	  break;

	case dpkg_status_message::conffile:
	  out << "conffile(existing_filename = "
	      << msg.get_existing_filename()
	      << ", new_filename = "
	      << msg.get_new_filename()
	      << ", percent = "
	      << msg.get_percent()
	      << ", text = "
	      << msg.get_text()
	      << ")";
	  break;

	case dpkg_status_message::status:
	  out << "status(percent = "
	      << msg.get_percent()
	      << ", package = "
	      << msg.get_package()
	      << ", text = "
	      << msg.get_text()
	      << ")";
	  break;
	}

      return out;
    }

    dpkg_status_message dpkg_status_message::parse(const char *buf, size_t len)
    {
      const char * const end = buf + len;

      const char *where = buf;

      // First find out what type of message it is.
      const std::string status(parse_colon_fragment(where, end));

      if(status == status_pmerror)
	{
	  // error: pkg: percent: message

	  const std::string pkg(parse_colon_fragment(where, end));
	  const std::string percent_str(parse_colon_fragment(where, end));
	  const std::string msg(strip_range(where, end));

	  const double percent = strtod(percent_str.c_str(), NULL);

	  return dpkg_status_message::make_error(percent, pkg, msg);
	}
      else if(status == status_pmconffile)
	{
	  // pmconffile: conffile-display-name: percent: 'old-file' 'new-file'

	  const std::string conffile(parse_colon_fragment(where, end));
	  const std::string percent_str(parse_colon_fragment(where, end));
	  const std::string msg(strip_range(where, end));

	  const double percent = strtod(percent_str.c_str(), NULL);

	  const std::string old_filename(parse_single_quoted_string(where, end));
	  const std::string new_filename(parse_single_quoted_string(where, end));

	  return dpkg_status_message::make_conffile(old_filename,
						    new_filename,
						    percent,
						    conffile);
	}
      else
	{
	  // (status): pkg: percent: message

	  const std::string package(parse_colon_fragment(where, end));
	  const std::string percent_str(parse_colon_fragment(where, end));
	  const std::string msg(strip_range(where, end));

	  const double percent = strtod(percent_str.c_str(), NULL);

	  return dpkg_status_message::make_status(percent, package, msg);
	}
    }

    dpkg_status_parser::dpkg_status_parser()
      : msg()
    {
    }

    void dpkg_status_parser::process_input(const char *buf, size_t len)
    {
      const char *begin = buf;
      const char * const end = buf + len;

      const char *where = begin;

      while(where < end)
	{
	  while(where < end && *where != '\n')
	    ++where;

	  msg.append(begin, where);
	  pending_messages.push_back(dpkg_status_message::parse(msg.c_str(), msg.size()));
	  msg.clear();

	  if(where < end)
	    ++where;

	  begin = where;
	}
    }
  }
}