override.cc 7.6 KB

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