summaryrefslogtreecommitdiff
path: root/ftparchive/override.cc
blob: 16fefeca568d406b0851686482aba07a1b9a16f3 (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################

   Override
   
   Store the override file.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include <config.h>

#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>

#include <utility>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "override.h"

#include <apti18n.h>
									/*}}}*/

// Override::ReadOverride - Read the override file			/*{{{*/
// ---------------------------------------------------------------------
/* This parses the override file and reads it into the map */
bool Override::ReadOverride(string const &File,bool const &Source)
{
   if (File.empty() == true)
      return true;
   
   FILE *F = fopen(File.c_str(),"r");
   if (F == 0)
      return _error->Errno("fopen",_("Unable to open %s"),File.c_str());
   
   char Line[1000];
   unsigned long long Counter = 0;
   while (fgets(Line,sizeof(Line),F) != 0)
   {
      Counter++;
      Item Itm;

      // Silence 
      for (char *I = Line; *I != 0; I++)
	 if (*I == '#')
	    *I = 0;

      // Strip space leading up to the package name, skip blank lines
      char *Pkg = Line;
      for (; isspace(*Pkg) && *Pkg != 0;Pkg++);
      if (*Pkg == 0)
	 continue;

#define APT_FIND_NEXT_FIELD \
      for (End++; isspace(*End) != 0 && *End != 0; ++End) \
	 /* skip spaces */ ; \
      Start = End; \
      for (; isspace(*End) == 0 && *End != 0; ++End) \
	 /* find end of word */ ;

#define APT_WARNING_MALFORMED_LINE(FIELD) \
      if (*End == 0) \
      { \
	 _error->Warning(_("Malformed override %s line %llu (%s)"),File.c_str(), \
			 Counter, FIELD ); \
	 continue; \
      } \
      *End = 0;

      // Find the package and zero..
      char *Start;
      char *End = Pkg;
      for (; isspace(*End) == 0 && *End != 0; End++);
      APT_WARNING_MALFORMED_LINE("pkgname");

      APT_FIND_NEXT_FIELD;

      // Find the priority
      if (Source == false)
      {
	 APT_WARNING_MALFORMED_LINE("priority");
	 Itm.Priority = Start;

	 APT_FIND_NEXT_FIELD;
      }

      // Find the Section
      APT_WARNING_MALFORMED_LINE("section");
      Itm.FieldOverride["Section"] = Start;

      // Source override files only have the two columns
      if (Source == true)
      {
	 Mapping[Pkg] = Itm;
	 continue;
      }

      // Find the =>
      for (End++; isspace(*End) != 0 && *End != 0; End++);
      if (*End != 0)
      {
	 Start = End;
	 for (; *End != 0 && (End[0] != '=' || End[1] != '>'); End++);
	 if (*End == 0 || strlen(End) < 4)
	 {
	    Itm.OldMaint = "*";
	    Itm.NewMaint = _strstrip(Start);
	 }
	 else
	 {
	    *End = 0;
	    Itm.OldMaint = _strstrip(Start);
	    
	    End += 3;
	    Itm.NewMaint = _strstrip(End);
	 }	 
      }

      Mapping[Pkg] = Itm;
   }

   if (ferror(F))
      _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str());
   fclose(F);
   return true;
}
									/*}}}*/
// Override::ReadExtraOverride - Read the extra override file		/*{{{*/
// ---------------------------------------------------------------------
/* This parses the extra override file and reads it into the map */
bool Override::ReadExtraOverride(string const &File,bool const &/*Source*/)
{
   if (File.empty() == true)
      return true;
   
   FILE *F = fopen(File.c_str(),"r");
   if (F == 0)
      return _error->Errno("fopen",_("Unable to open %s"),File.c_str());
  
   char Line[1000];
   unsigned long long Counter = 0;
   while (fgets(Line,sizeof(Line),F) != 0)
   {
      Counter++;

      // Silence 
      for (char *I = Line; *I != 0; I++)
	 if (*I == '#')
	    *I = 0;

      // Strip space leading up to the package name, skip blank lines
      char *Pkg = Line;
      for (; isspace(*Pkg) && *Pkg != 0;Pkg++);
      if (Pkg == 0)
	 continue;

      // Find the package and zero..
      char *End = Pkg;
      for (; isspace(*End) == 0 && *End != 0; End++);
      if (*End == 0)
      {
	 _error->Warning(_("Malformed override %s line %llu #1"),File.c_str(),
			 Counter);
	 continue;
      }      
      *End = 0;

      // Find the field
      for (End++; isspace(*End) != 0 && *End != 0; End++);
      char *Field = End;
      for (; isspace(*End) == 0 && *End != 0; End++);
      if (*End == 0)
      {
	 _error->Warning(_("Malformed override %s line %llu #2"),File.c_str(),
			 Counter);
	 continue;
      }
      *End = 0;
      
      // Find the field value 
      for (End++; isspace(*End) != 0 && *End != 0; End++);
      char *Value = End;
      for (; *End != 0; End++);
      for (; isspace(*(End-1)) && End > Value; End--);
      if (End == Value)
      {
	 _error->Warning(_("Malformed override %s line %llu #3"),File.c_str(),
			 Counter);
	 continue;
      }      
      *End = 0;

      Mapping[Pkg].FieldOverride[Field] = Value;
   }

   if (ferror(F))
      _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str());
   fclose(F);
   return true;
}
									/*}}}*/

// Override::GetItem - Get a architecture specific item			/*{{{*/
// ---------------------------------------------------------------------
/* Returns a override item for the given package and the given architecture.
 * Treats "all" special
 */
Override::Item* Override::GetItem(string const &Package, string const &Architecture)
{
   map<string,Item>::const_iterator I = Mapping.find(Package);
   map<string,Item>::iterator J = Mapping.find(Package + "/" + Architecture);

   if (I == Mapping.end() && J == Mapping.end())
   {
      return 0;
   }

   Item *result = new Item;
   if (I == Mapping.end()) *result = J->second;
   else
   {
      *result = I->second;
      if (J != Mapping.end())
      {
	 Item *R = &J->second;
	 if (R->Priority != "") result->Priority = R->Priority;
	 if (R->OldMaint != "") result->OldMaint = R->OldMaint;
	 if (R->NewMaint != "") result->NewMaint = R->NewMaint;
	 for (map<string,string>::const_iterator foI = R->FieldOverride.begin();
	      foI != R->FieldOverride.end(); ++foI)
         {
	    result->FieldOverride[foI->first] = foI->second;
	 }
      }
   }
   return result;
}


// Override::Item::SwapMaint - Swap the maintainer field if necessary	/*{{{*/
// ---------------------------------------------------------------------
/* Returns the new maintainer string after evaluating the rewriting rule. If
   there is a rule but it does not match then the empty string is returned,
   also if there was no rewrite rule the empty string is returned. Failed
   indicates if there was some kind of problem while rewriting. */
string Override::Item::SwapMaint(string const &Orig,bool &Failed)
{
   Failed = false;
   
   // Degenerate case..
   if (NewMaint.empty() == true)
	 return OldMaint;
   
   if (OldMaint == "*")
      return NewMaint;
   
   /* James: ancient, eliminate it, however it is still being used in the main
      override file. Thus it persists.*/
#if 1
   // Break OldMaint up into little bits on double slash boundaries.
   string::const_iterator End = OldMaint.begin();
   while (1)
   {
      string::const_iterator Start = End;      
      for (; End < OldMaint.end() &&
	   (End + 3 >= OldMaint.end() || End[0] != ' ' || 
	    End[1] != '/' || End[2] != '/'); ++End);
      if (stringcasecmp(Start,End,Orig.begin(),Orig.end()) == 0)
	 return NewMaint;
      
      if (End >= OldMaint.end())
	 break;

      // Skip the divider and white space
      for (; End < OldMaint.end() && (*End == '/' || *End == ' '); ++End);
   }
#else
   if (stringcasecmp(OldMaint.begin(),OldMaint.end(),Orig.begin(),Orig.end()) == 0)
      return NewMaint;   
#endif
   
   Failed = true;
   return string();
}
									/*}}}*/