You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

290 lines
7.7 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: override.cc,v 1.4 2003/02/10 07:34:41 doogie Exp $
  4. /* ######################################################################
  5. Override
  6. Store the override file.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include "override.h"
  11. #include <apti18n.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/error.h>
  14. #include <stdio.h>
  15. #include "override.h"
  16. /*}}}*/
  17. // Override::ReadOverride - Read the override file /*{{{*/
  18. // ---------------------------------------------------------------------
  19. /* This parses the override file and reads it into the map */
  20. bool Override::ReadOverride(string const &File,bool const &Source)
  21. {
  22. if (File.empty() == true)
  23. return true;
  24. FILE *F = fopen(File.c_str(),"r");
  25. if (F == 0)
  26. return _error->Errno("fopen",_("Unable to open %s"),File.c_str());
  27. char Line[500];
  28. unsigned long Counter = 0;
  29. while (fgets(Line,sizeof(Line),F) != 0)
  30. {
  31. Counter++;
  32. Item Itm;
  33. // Silence
  34. for (char *I = Line; *I != 0; I++)
  35. if (*I == '#')
  36. *I = 0;
  37. // Strip space leading up to the package name, skip blank lines
  38. char *Pkg = Line;
  39. for (; isspace(*Pkg) && *Pkg != 0;Pkg++);
  40. if (Pkg == 0)
  41. continue;
  42. // Find the package and zero..
  43. char *Start = Pkg;
  44. char *End = Pkg;
  45. for (; isspace(*End) == 0 && *End != 0; End++);
  46. if (*End == 0)
  47. {
  48. _error->Warning(_("Malformed override %s line %lu #1"),File.c_str(),
  49. Counter);
  50. continue;
  51. }
  52. *End = 0;
  53. // Find the priority
  54. if (Source == false)
  55. {
  56. for (End++; isspace(*End) != 0 && *End != 0; End++);
  57. Start = End;
  58. for (; isspace(*End) == 0 && *End != 0; End++);
  59. if (*End == 0)
  60. {
  61. _error->Warning(_("Malformed override %s line %lu #2"),File.c_str(),
  62. Counter);
  63. continue;
  64. }
  65. *End = 0;
  66. Itm.Priority = Start;
  67. }
  68. // Find the Section
  69. for (End++; isspace(*End) != 0 && *End != 0; End++);
  70. Start = End;
  71. for (; isspace(*End) == 0 && *End != 0; End++);
  72. if (*End == 0)
  73. {
  74. _error->Warning(_("Malformed override %s line %lu #3"),File.c_str(),
  75. Counter);
  76. continue;
  77. }
  78. *End = 0;
  79. Itm.FieldOverride["Section"] = Start;
  80. // Source override files only have the two columns
  81. if (Source == true)
  82. {
  83. Mapping[Pkg] = Itm;
  84. continue;
  85. }
  86. // Find the =>
  87. for (End++; isspace(*End) != 0 && *End != 0; End++);
  88. if (*End != 0)
  89. {
  90. Start = End;
  91. for (; *End != 0 && (End[0] != '=' || End[1] != '>'); End++);
  92. if (*End == 0 || strlen(End) < 4)
  93. {
  94. Itm.OldMaint = "*";
  95. Itm.NewMaint = _strstrip(Start);
  96. }
  97. else
  98. {
  99. *End = 0;
  100. Itm.OldMaint = _strstrip(Start);
  101. End += 3;
  102. Itm.NewMaint = _strstrip(End);
  103. }
  104. }
  105. Mapping[Pkg] = Itm;
  106. }
  107. if (ferror(F))
  108. _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str());
  109. fclose(F);
  110. return true;
  111. }
  112. /*}}}*/
  113. // Override::ReadExtraOverride - Read the extra override file /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* This parses the extra override file and reads it into the map */
  116. bool Override::ReadExtraOverride(string const &File,bool const &Source)
  117. {
  118. if (File.empty() == true)
  119. return true;
  120. FILE *F = fopen(File.c_str(),"r");
  121. if (F == 0)
  122. return _error->Errno("fopen",_("Unable to open %s"),File.c_str());
  123. char Line[500];
  124. unsigned long Counter = 0;
  125. while (fgets(Line,sizeof(Line),F) != 0)
  126. {
  127. Counter++;
  128. // Silence
  129. for (char *I = Line; *I != 0; I++)
  130. if (*I == '#')
  131. *I = 0;
  132. // Strip space leading up to the package name, skip blank lines
  133. char *Pkg = Line;
  134. for (; isspace(*Pkg) && *Pkg != 0;Pkg++);
  135. if (Pkg == 0)
  136. continue;
  137. // Find the package and zero..
  138. char *End = Pkg;
  139. for (; isspace(*End) == 0 && *End != 0; End++);
  140. if (*End == 0)
  141. {
  142. _error->Warning(_("Malformed override %s line %lu #1"),File.c_str(),
  143. Counter);
  144. continue;
  145. }
  146. *End = 0;
  147. // Find the field
  148. for (End++; isspace(*End) != 0 && *End != 0; End++);
  149. char *Field = End;
  150. for (; isspace(*End) == 0 && *End != 0; End++);
  151. if (*End == 0)
  152. {
  153. _error->Warning(_("Malformed override %s line %lu #2"),File.c_str(),
  154. Counter);
  155. continue;
  156. }
  157. *End = 0;
  158. // Find the field value
  159. for (End++; isspace(*End) != 0 && *End != 0; End++);
  160. char *Value = End;
  161. for (; *End != 0; End++);
  162. for (; isspace(*(End-1)) && End > Value; End--);
  163. if (End == Value)
  164. {
  165. _error->Warning(_("Malformed override %s line %lu #3"),File.c_str(),
  166. Counter);
  167. continue;
  168. }
  169. *End = 0;
  170. Mapping[Pkg].FieldOverride[Field] = Value;
  171. }
  172. if (ferror(F))
  173. _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str());
  174. fclose(F);
  175. return true;
  176. }
  177. /*}}}*/
  178. // Override::GetItem - Get a architecture specific item /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* Returns a override item for the given package and the given architecture.
  181. * Treats "all" special
  182. */
  183. Override::Item* Override::GetItem(string const &Package, string const &Architecture)
  184. {
  185. map<string,Item>::const_iterator I = Mapping.find(Package);
  186. map<string,Item>::iterator J = Mapping.find(Package + "/" + Architecture);
  187. if (I == Mapping.end() && J == Mapping.end())
  188. {
  189. return 0;
  190. }
  191. Item *result = new Item;
  192. if (I == Mapping.end()) *result = J->second;
  193. else
  194. {
  195. *result = I->second;
  196. if (J != Mapping.end())
  197. {
  198. Item *R = &J->second;
  199. if (R->Priority != "") result->Priority = R->Priority;
  200. if (R->OldMaint != "") result->OldMaint = R->OldMaint;
  201. if (R->NewMaint != "") result->NewMaint = R->NewMaint;
  202. for (map<string,string>::const_iterator foI = R->FieldOverride.begin();
  203. foI != R->FieldOverride.end(); foI++)
  204. {
  205. result->FieldOverride[foI->first] = foI->second;
  206. }
  207. }
  208. }
  209. return result;
  210. };
  211. // Override::Item::SwapMaint - Swap the maintainer field if necessary /*{{{*/
  212. // ---------------------------------------------------------------------
  213. /* Returns the new maintainer string after evaluating the rewriting rule. If
  214. there is a rule but it does not match then the empty string is returned,
  215. also if there was no rewrite rule the empty string is returned. Failed
  216. indicates if there was some kind of problem while rewriting. */
  217. string Override::Item::SwapMaint(string const &Orig,bool &Failed)
  218. {
  219. Failed = false;
  220. // Degenerate case..
  221. if (NewMaint.empty() == true)
  222. return OldMaint;
  223. if (OldMaint == "*")
  224. return NewMaint;
  225. /* James: ancient, eliminate it, however it is still being used in the main
  226. override file. Thus it persists.*/
  227. #if 1
  228. // Break OldMaint up into little bits on double slash boundaries.
  229. string::const_iterator End = OldMaint.begin();
  230. while (1)
  231. {
  232. string::const_iterator Start = End;
  233. for (; End < OldMaint.end() &&
  234. (End + 3 >= OldMaint.end() || End[0] != ' ' ||
  235. End[1] != '/' || End[2] != '/'); End++);
  236. if (stringcasecmp(Start,End,Orig.begin(),Orig.end()) == 0)
  237. return NewMaint;
  238. if (End >= OldMaint.end())
  239. break;
  240. // Skip the divider and white space
  241. for (; End < OldMaint.end() && (*End == '/' || *End == ' '); End++);
  242. }
  243. #else
  244. if (stringcasecmp(OldMaint.begin(),OldMaint.end(),Orig.begin(),Orig.end()) == 0)
  245. return NewMaint;
  246. #endif
  247. Failed = true;
  248. return string();
  249. }
  250. /*}}}*/