controllib.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h);
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(warning error failure internerr syserr subprocerr);
  9. use Dpkg::Arch qw(get_host_arch debarch_is);
  10. textdomain("dpkg-dev");
  11. our $sourcepackage; # - name of sourcepackage
  12. our %f; # - fields ???
  13. our %fi; # - map of fields values. keys are of the form "S# key"
  14. # where S is source (L is changelog, C is control)
  15. # and # is an index
  16. our %p2i; # - map from datafile+packagename to index in controlfile
  17. # (used if multiple packages can be listed). Key is
  18. # "S key" where S is the source and key is the packagename
  19. my $maxsubsts = 50;
  20. our %substvar; # - map with substitution variables
  21. my $parsechangelog = 'dpkg-parsechangelog';
  22. our @pkg_dep_fields = qw(Pre-Depends Depends Recommends Suggests Enhances
  23. Conflicts Breaks Replaces Provides);
  24. our @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  25. Build-Conflicts Build-Conflicts-Indep);
  26. sub getfowner
  27. {
  28. my $getlogin = getlogin();
  29. if (!defined($getlogin)) {
  30. open(SAVEIN, "<&STDIN");
  31. open(STDIN, "<&STDERR");
  32. $getlogin = getlogin();
  33. close(STDIN);
  34. open(STDIN, "<&SAVEIN");
  35. close(SAVEIN);
  36. }
  37. if (!defined($getlogin)) {
  38. open(SAVEIN, "<&STDIN");
  39. open(STDIN, "<&STDOUT");
  40. $getlogin = getlogin();
  41. close(STDIN);
  42. open(STDIN, "<&SAVEIN");
  43. close(SAVEIN);
  44. }
  45. my @fowner;
  46. if (defined($ENV{'LOGNAME'})) {
  47. @fowner = getpwnam($ENV{'LOGNAME'});
  48. if (!@fowner) {
  49. die(sprintf(_g('unable to get login information for username "%s"'), $ENV{'LOGNAME'}));
  50. }
  51. } elsif (defined($getlogin)) {
  52. @fowner = getpwnam($getlogin);
  53. if (!@fowner) {
  54. die(sprintf(_g('unable to get login information for username "%s"'), $getlogin));
  55. }
  56. } else {
  57. warning(_g('no utmp entry available and LOGNAME not defined; ' .
  58. 'using uid of process (%d)'), $<);
  59. @fowner = getpwuid($<);
  60. if (!@fowner) {
  61. die (sprintf(_g('unable to get login information for uid %d'), $<));
  62. }
  63. }
  64. @fowner = @fowner[2,3];
  65. return @fowner;
  66. }
  67. sub capit {
  68. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  69. return join '-', @pieces;
  70. }
  71. sub substvars {
  72. my ($v) = @_;
  73. my $lhs;
  74. my $vn;
  75. my $rhs = '';
  76. my $count = 0;
  77. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  78. # If we have consumed more from the leftover data, then
  79. # reset the recursive counter.
  80. $count= 0 if (length($POSTMATCH) < length($rhs));
  81. $count < $maxsubsts ||
  82. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  83. $lhs=$`; $vn=$1; $rhs=$';
  84. if (defined($substvar{$vn})) {
  85. $v= $lhs.$substvar{$vn}.$rhs;
  86. $count++;
  87. } else {
  88. warning(_g("unknown substitution variable \${%s}"), $vn);
  89. $v= $lhs.$rhs;
  90. }
  91. }
  92. return $v;
  93. }
  94. my %fieldimps;
  95. sub set_field_importance(@)
  96. {
  97. my @fields = @_;
  98. my $i = 1;
  99. grep($fieldimps{$_} = $i++, @fields);
  100. }
  101. sub sort_field_by_importance($$)
  102. {
  103. my ($a, $b) = @_;
  104. if (defined $fieldimps{$a} && defined $fieldimps{$b}) {
  105. $fieldimps{$a} <=> $fieldimps{$b};
  106. } elsif (defined($fieldimps{$a})) {
  107. -1;
  108. } elsif (defined($fieldimps{$b})) {
  109. 1;
  110. } else {
  111. $a cmp $b;
  112. }
  113. }
  114. sub outputclose {
  115. my ($varlistfile) = @_;
  116. for my $f (keys %f) {
  117. $substvar{"F:$f"} = $f{$f};
  118. }
  119. &parsesubstvars($varlistfile) if (defined($varlistfile));
  120. for my $f (sort sort_field_by_importance keys %f) {
  121. my $v = $f{$f};
  122. if (defined($varlistfile)) {
  123. $v= &substvars($v);
  124. }
  125. $v =~ m/\S/ || next; # delete whitespace-only fields
  126. $v =~ m/\n\S/ &&
  127. internerr(_g("field %s has newline then non whitespace >%s<"),
  128. $f, $v);
  129. $v =~ m/\n[ \t]*\n/ &&
  130. internerr(_g("field %s has blank lines >%s<"), $f, $v);
  131. $v =~ m/\n$/ &&
  132. internerr(_g("field %s has trailing newline >%s<"), $f, $v);
  133. if (defined($varlistfile)) {
  134. $v =~ s/,[\s,]*,/,/g;
  135. $v =~ s/^\s*,\s*//;
  136. $v =~ s/\s*,\s*$//;
  137. }
  138. $v =~ s/\$\{\}/\$/g;
  139. print("$f: $v\n") || &syserr(_g("write error on control data"));
  140. }
  141. close(STDOUT) || &syserr(_g("write error on close control data"));
  142. }
  143. sub parsecontrolfile {
  144. my $controlfile = shift;
  145. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  146. open(CDATA, "< $controlfile") ||
  147. error(_g("cannot read control file %s: %s"), $controlfile, $!);
  148. binmode(CDATA);
  149. my $indices = parsecdata(\*CDATA, 'C', 1,
  150. sprintf(_g("control file %s"), $controlfile));
  151. $indices >= 2 || &error(_g("control file must have at least one binary package part"));
  152. for (my $i = 1; $i < $indices; $i++) {
  153. defined($fi{"C$i Package"}) ||
  154. error(_g("per-package paragraph %d in control " .
  155. "info file is missing Package line"), $i);
  156. }
  157. defined($fi{"C Source"}) ||
  158. &error(_g("source paragraph in control info file is ".
  159. "missing Source line"));
  160. }
  161. my $substvarsparsed = 0;
  162. sub parsesubstvars {
  163. my $varlistfile = shift;
  164. if (length($varlistfile) && !$substvarsparsed) {
  165. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  166. if (open(SV,"< $varlistfile")) {
  167. binmode(SV);
  168. while (<SV>) {
  169. next if m/^\#/ || !m/\S/;
  170. s/\s*\n$//;
  171. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  172. error(_g("bad line in substvars file %s at line %d"),
  173. $varlistfile, $.);
  174. $substvar{$1}= $';
  175. }
  176. close(SV);
  177. } elsif ($! != ENOENT ) {
  178. error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
  179. }
  180. $substvarsparsed = 1;
  181. }
  182. }
  183. sub parsedep {
  184. my ($dep_line, $use_arch, $reduce_arch) = @_;
  185. my @dep_list;
  186. my $host_arch = get_host_arch();
  187. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  188. my @or_list = ();
  189. ALTERNATE:
  190. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  191. my ($package, $relation, $version);
  192. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  193. ($relation, $version) = ($1, $2)
  194. if ($dep_or =~ s/^\(\s*(=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  195. my @arches;
  196. @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  197. if ($reduce_arch && @arches) {
  198. my $seen_arch='';
  199. foreach my $arch (@arches) {
  200. $arch=lc($arch);
  201. if ($arch =~ /^!/) {
  202. my $not_arch;
  203. ($not_arch = $arch) =~ s/^!//;
  204. if (debarch_is($host_arch, $not_arch)) {
  205. next ALTERNATE;
  206. } else {
  207. # This is equivilant to
  208. # having seen the current arch,
  209. # unless the current arch
  210. # is also listed..
  211. $seen_arch=1;
  212. }
  213. } elsif (debarch_is($host_arch, $arch)) {
  214. $seen_arch=1;
  215. next;
  216. }
  217. }
  218. if (! $seen_arch) {
  219. next;
  220. }
  221. }
  222. if (length($dep_or)) {
  223. warning(_g("can't parse dependency %s"), $dep_and);
  224. return undef;
  225. }
  226. push @or_list, [ $package, $relation, $version, \@arches ];
  227. }
  228. push @dep_list, \@or_list;
  229. }
  230. \@dep_list;
  231. }
  232. sub showdep {
  233. my ($dep_list, $show_arch) = @_;
  234. my @and_list;
  235. foreach my $dep_and (@$dep_list) {
  236. my @or_list = ();
  237. foreach my $dep_or (@$dep_and) {
  238. my ($package, $relation, $version, $arch_list) = @$dep_or;
  239. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  240. }
  241. push @and_list, join(' | ', @or_list);
  242. }
  243. join(', ', @and_list);
  244. }
  245. sub parsechangelog {
  246. my ($changelogfile, $changelogformat, $since) = @_;
  247. defined(my $c = open(CDATA, "-|")) || syserr(_g("fork for parse changelog"));
  248. if ($c) {
  249. binmode(CDATA);
  250. parsecdata(\*CDATA, 'L', 0, _g("parsed version of changelog"));
  251. close(CDATA);
  252. $? && subprocerr(_g("parse changelog"));
  253. } else {
  254. binmode(STDOUT);
  255. my @al = ($parsechangelog);
  256. push(@al,"-l$changelogfile");
  257. push(@al, "-F$changelogformat") if defined($changelogformat);
  258. push(@al, "-v$since") if defined($since);
  259. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  260. }
  261. }
  262. sub init_substvars
  263. {
  264. $substvar{'Format'} = 1.7;
  265. $substvar{'Newline'} = "\n";
  266. $substvar{'Space'} = " ";
  267. $substvar{'Tab'} = "\t";
  268. # XXX: Source-Version is now deprecated, remove in the future.
  269. $substvar{'Source-Version'}= $fi{"L Version"};
  270. $substvar{'binary:Version'} = $fi{"L Version"};
  271. $substvar{'source:Version'} = $fi{"L Version"};
  272. $substvar{'source:Version'} =~ s/\+b[0-9]+$//;
  273. $substvar{'source:Upstream-Version'} = $fi{"L Version"};
  274. $substvar{'source:Upstream-Version'} =~ s/-[^-]*$//;
  275. $substvar{"dpkg:Version"} = $version;
  276. $substvar{"dpkg:Upstream-Version"} = $version;
  277. $substvar{"dpkg:Upstream-Version"} =~ s/-[^-]+$//;
  278. }
  279. sub init_substvar_arch()
  280. {
  281. $substvar{'Arch'} = get_host_arch();
  282. }
  283. sub checkpackagename {
  284. my $name = shift || '';
  285. $name =~ m/[^-+.0-9a-z]/o &&
  286. error(_g("source package name `%s' contains illegal character `%s'"),
  287. $name, $&);
  288. $name =~ m/^[0-9a-z]/o ||
  289. error(_g("source package name `%s' starts with non-alphanum"), $name);
  290. }
  291. sub checkversion {
  292. my $version = shift || '';
  293. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  294. error(_g("version number contains illegal character `%s'"), $&);
  295. }
  296. sub setsourcepackage {
  297. my $v = shift;
  298. checkpackagename( $v );
  299. if (defined($sourcepackage)) {
  300. $v eq $sourcepackage ||
  301. error(_g("source package has two conflicting values - %s and %s"),
  302. $sourcepackage, $v);
  303. } else {
  304. $sourcepackage= $v;
  305. }
  306. }
  307. sub readmd5sum {
  308. (my $md5sum = shift) or return;
  309. $md5sum =~ s/^([0-9a-f]{32})\s*\*?-?\s*\n?$/$1/o
  310. || failure(_g("md5sum gave bogus output `%s'"), $md5sum);
  311. return $md5sum;
  312. }
  313. # XXX: Should not be a global!!
  314. my $whatmsg;
  315. sub parsecdata {
  316. my ($cdata, $source, $many);
  317. ($cdata, $source, $many, $whatmsg) = @_;
  318. # many=0: ordinary control data like output from dpkg-parsechangelog
  319. # many=1: many paragraphs like in source control file
  320. # many=-1: single paragraph of control data optionally signed
  321. my $index = '';
  322. my $cf = '';
  323. my $paraborder = 1;
  324. while (<$cdata>) {
  325. s/\s*\n$//;
  326. next if (m/^$/ and $paraborder);
  327. next if (m/^#/);
  328. $paraborder=0;
  329. if (m/^(\S+)\s*:\s*(.*)$/) {
  330. $cf = $1;
  331. my $v = $2;
  332. $cf= &capit($cf);
  333. $fi{"$source$index $cf"}= $v;
  334. $fi{"o:$source$index $cf"}= $1;
  335. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  336. } elsif (m/^\s+\S/) {
  337. length($cf) || &syntax(_g("continued value line not in field"));
  338. $fi{"$source$index $cf"}.= "\n$_";
  339. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  340. $many == -2 && syntax(_g("expected blank line before PGP signature"));
  341. while (<$cdata>) {
  342. last if m/^$/;
  343. }
  344. $many= -2;
  345. } elsif (m/^$/) {
  346. $paraborder = 1;
  347. if ($many>0) {
  348. $index++; $cf='';
  349. } elsif ($many == -2) {
  350. $_ = <$cdata> while defined($_) && $_ =~ /^\s*$/;
  351. length($_) ||
  352. &syntax(_g("expected PGP signature, found EOF after blank line"));
  353. s/\n$//;
  354. m/^-----BEGIN PGP/ ||
  355. &syntax(sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  356. $many= -3; last;
  357. } else {
  358. while (<$cdata>) {
  359. /^\s*$/ ||
  360. &syntax(_g("found several \`paragraphs' where only one expected"));
  361. }
  362. }
  363. } else {
  364. &syntax(_g("line with unknown format (not field-colon-value)"));
  365. }
  366. }
  367. $many == -2 && &syntax(_g("found start of PGP body but no signature"));
  368. if (length($cf)) { $index++; }
  369. $index || &syntax(_g("empty file"));
  370. return $index;
  371. }
  372. sub syntax {
  373. error(_g("syntax error in %s at line %d: %s"), $whatmsg, $., $_[0]);
  374. }
  375. 1;