controllib.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h);
  6. our $dpkglibdir;
  7. push(@INC,$dpkglibdir);
  8. require 'dpkg-gettext.pl';
  9. textdomain("dpkg-dev");
  10. our $sourcepackage; # - name of sourcepackage
  11. our %f; # - fields ???
  12. our %fi; # - map of fields values. keys are of the form "S# key"
  13. # where S is source (L is changelog, C is control)
  14. # and # is an index
  15. our %fieldimps;
  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 Replaces Provides);
  24. our @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  25. Build-Conflicts Build-Conflicts-Indep);
  26. our $warnable_error = 1;
  27. our $quiet_warnings = 0;
  28. our $version;
  29. our $progname = $0;
  30. $progname = $& if $progname =~ m,[^/]+$,;
  31. sub getfowner
  32. {
  33. my $getlogin = getlogin();
  34. if (!defined($getlogin)) {
  35. open(SAVEIN, "<&STDIN");
  36. open(STDIN, "<&STDERR");
  37. $getlogin = getlogin();
  38. close(STDIN);
  39. open(STDIN, "<&SAVEIN");
  40. close(SAVEIN);
  41. }
  42. if (!defined($getlogin)) {
  43. open(SAVEIN, "<&STDIN");
  44. open(STDIN, "<&STDOUT");
  45. $getlogin = getlogin();
  46. close(STDIN);
  47. open(STDIN, "<&SAVEIN");
  48. close(SAVEIN);
  49. }
  50. my @fowner;
  51. if (defined($ENV{'LOGNAME'})) {
  52. @fowner = getpwnam($ENV{'LOGNAME'});
  53. if (!@fowner) {
  54. die(sprintf(_g('unable to get login information for username "%s"'), $ENV{'LOGNAME'}));
  55. }
  56. } elsif (defined($getlogin)) {
  57. @fowner = getpwnam($getlogin);
  58. if (!@fowner) {
  59. die(sprintf(_g('unable to get login information for username "%s"'), $getlogin));
  60. }
  61. } else {
  62. warning(sprintf(_g('no utmp entry available and LOGNAME not defined; using uid of process (%d)'), $<));
  63. @fowner = getpwuid($<);
  64. if (!@fowner) {
  65. die (sprintf(_g('unable to get login information for uid %d'), $<));
  66. }
  67. }
  68. @fowner = @fowner[2,3];
  69. return @fowner;
  70. }
  71. sub capit {
  72. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  73. return join '-', @pieces;
  74. }
  75. {
  76. my $host_arch;
  77. sub get_host_arch()
  78. {
  79. return $host_arch if defined $host_arch;
  80. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  81. $? && subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  82. chomp $host_arch;
  83. return $host_arch;
  84. }
  85. }
  86. sub debian_arch_fix
  87. {
  88. my ($os, $cpu) = @_;
  89. if ($os eq "linux") {
  90. return $cpu;
  91. } else {
  92. return "$os-$cpu";
  93. }
  94. }
  95. sub debian_arch_split {
  96. local ($_) = @_;
  97. if (/^([^-]*)-(.*)/) {
  98. return ($1, $2);
  99. } elsif (/any/ || /all/) {
  100. return ($_, $_);
  101. } else {
  102. return ("linux", $_);
  103. }
  104. }
  105. sub debian_arch_eq {
  106. my ($a, $b) = @_;
  107. my ($a_os, $a_cpu) = debian_arch_split($a);
  108. my ($b_os, $b_cpu) = debian_arch_split($b);
  109. return ("$a_os-$a_cpu" eq "$b_os-$b_cpu");
  110. }
  111. sub debian_arch_is {
  112. my ($real, $alias) = @_;
  113. my ($real_os, $real_cpu) = debian_arch_split($real);
  114. my ($alias_os, $alias_cpu) = debian_arch_split($alias);
  115. if ("$real_os-$real_cpu" eq "$alias_os-$alias_cpu") {
  116. return 1;
  117. } elsif ("$alias_os-$alias_cpu" eq "any-any") {
  118. return 1;
  119. } elsif ("$alias_os-$alias_cpu" eq "any-$real_cpu") {
  120. return 1;
  121. } elsif ("$alias_os-$alias_cpu" eq "$real_os-any") {
  122. return 1;
  123. }
  124. return 0;
  125. }
  126. sub substvars {
  127. my ($v) = @_;
  128. my $lhs;
  129. my $vn;
  130. my $rhs = '';
  131. my $count = 0;
  132. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  133. # If we have consumed more from the leftover data, then
  134. # reset the recursive counter.
  135. $count= 0 if (length($POSTMATCH) < length($rhs));
  136. $count < $maxsubsts ||
  137. &error(sprintf(_g("too many substitutions - recursive ? - in \`%s'"), $v));
  138. $lhs=$`; $vn=$1; $rhs=$';
  139. if (defined($substvar{$vn})) {
  140. $v= $lhs.$substvar{$vn}.$rhs;
  141. $count++;
  142. } else {
  143. warning(sprintf(_g("unknown substitution variable \${%s}"), $vn));
  144. $v= $lhs.$rhs;
  145. }
  146. }
  147. return $v;
  148. }
  149. sub set_field_importance(@)
  150. {
  151. my @fields = @_;
  152. my $i = 1;
  153. grep($fieldimps{$_} = $i++, @fields);
  154. }
  155. sub sort_field_by_importance($$)
  156. {
  157. my ($a, $b) = @_;
  158. if (defined $fieldimps{$a} && defined $fieldimps{$b}) {
  159. $fieldimps{$a} <=> $fieldimps{$b};
  160. } elsif (defined($fieldimps{$a})) {
  161. -1;
  162. } elsif (defined($fieldimps{$b})) {
  163. 1;
  164. } else {
  165. $a cmp $b;
  166. }
  167. }
  168. sub outputclose {
  169. my ($varlistfile) = @_;
  170. for my $f (keys %f) {
  171. $substvar{"F:$f"} = $f{$f};
  172. }
  173. &parsesubstvars($varlistfile) if (defined($varlistfile));
  174. for my $f (sort sort_field_by_importance keys %f) {
  175. my $v = $f{$f};
  176. if (defined($varlistfile)) {
  177. $v= &substvars($v);
  178. }
  179. $v =~ m/\S/ || next; # delete whitespace-only fields
  180. $v =~ m/\n\S/ && &internerr(sprintf(_g("field %s has newline then non whitespace >%s<"), $f, $v));
  181. $v =~ m/\n[ \t]*\n/ && &internerr(sprintf(_g("field %s has blank lines >%s<"), $f, $v));
  182. $v =~ m/\n$/ && &internerr(sprintf(_g("field %s has trailing newline >%s<"), $f, $v));
  183. if (defined($varlistfile)) {
  184. $v =~ s/,[\s,]*,/,/g;
  185. $v =~ s/^\s*,\s*//;
  186. $v =~ s/\s*,\s*$//;
  187. }
  188. $v =~ s/\$\{\}/\$/g;
  189. print("$f: $v\n") || &syserr(_g("write error on control data"));
  190. }
  191. close(STDOUT) || &syserr(_g("write error on close control data"));
  192. }
  193. sub parsecontrolfile {
  194. my $controlfile = shift;
  195. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  196. open(CDATA,"< $controlfile") || &error(sprintf(_g("cannot read control file %s: %s"), $controlfile, $!));
  197. binmode(CDATA);
  198. my $indices = parsecdata(\*CDATA, 'C', 1,
  199. sprintf(_g("control file %s"), $controlfile));
  200. $indices >= 2 || &error(_g("control file must have at least one binary package part"));
  201. for (my $i = 1; $i < $indices; $i++) {
  202. defined($fi{"C$i Package"}) ||
  203. &error(sprintf(_g("per-package paragraph %d in control ".
  204. "info file is missing Package line"),
  205. $i));
  206. }
  207. defined($fi{"C Source"}) ||
  208. &error(_g("source paragraph in control info file is ".
  209. "missing Source line"));
  210. }
  211. my $substvarsparsed = 0;
  212. sub parsesubstvars {
  213. my $varlistfile = shift;
  214. if (length($varlistfile) && !$substvarsparsed) {
  215. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  216. if (open(SV,"< $varlistfile")) {
  217. binmode(SV);
  218. while (<SV>) {
  219. next if m/^\#/ || !m/\S/;
  220. s/\s*\n$//;
  221. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  222. &error(sprintf(_g("bad line in substvars file %s at line %d"),
  223. $varlistfile, $.));
  224. $substvar{$1}= $';
  225. }
  226. close(SV);
  227. } elsif ($! != ENOENT ) {
  228. &error(sprintf(_g("unable to open substvars file %s: %s"),
  229. $varlistfile, $!));
  230. }
  231. $substvarsparsed = 1;
  232. }
  233. }
  234. sub parsedep {
  235. my ($dep_line, $use_arch, $reduce_arch) = @_;
  236. my @dep_list;
  237. my $host_arch = get_host_arch();
  238. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  239. my @or_list = ();
  240. ALTERNATE:
  241. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  242. my ($package, $relation, $version);
  243. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  244. ($relation, $version) = ($1, $2)
  245. if ($dep_or =~ s/^\(\s*(=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  246. my @arches;
  247. @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  248. if ($reduce_arch && @arches) {
  249. my $seen_arch='';
  250. foreach my $arch (@arches) {
  251. $arch=lc($arch);
  252. if (debian_arch_is($host_arch, $arch)) {
  253. $seen_arch=1;
  254. next;
  255. } elsif ($arch =~ /^!/) {
  256. my $not_arch;
  257. ($not_arch = $arch) =~ s/^!//;
  258. if (debian_arch_is($host_arch, $not_arch)) {
  259. next ALTERNATE;
  260. } else {
  261. # This is equivilant to
  262. # having seen the current arch,
  263. # unless the current arch
  264. # is also listed..
  265. $seen_arch=1;
  266. }
  267. }
  268. }
  269. if (! $seen_arch) {
  270. next;
  271. }
  272. }
  273. if (length($dep_or)) {
  274. warning(sprintf(_g("can't parse dependency %s"), $dep_and));
  275. return undef;
  276. }
  277. push @or_list, [ $package, $relation, $version, \@arches ];
  278. }
  279. push @dep_list, \@or_list;
  280. }
  281. \@dep_list;
  282. }
  283. sub showdep {
  284. my ($dep_list, $show_arch) = @_;
  285. my @and_list;
  286. foreach my $dep_and (@$dep_list) {
  287. my @or_list = ();
  288. foreach my $dep_or (@$dep_and) {
  289. my ($package, $relation, $version, $arch_list) = @$dep_or;
  290. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  291. }
  292. push @and_list, join(' | ', @or_list);
  293. }
  294. join(', ', @and_list);
  295. }
  296. sub parsechangelog {
  297. my ($changelogfile, $changelogformat, $since) = @_;
  298. defined(my $c = open(CDATA, "-|")) || syserr(_g("fork for parse changelog"));
  299. if ($c) {
  300. binmode(CDATA);
  301. parsecdata(\*CDATA, 'L', 0, _g("parsed version of changelog"));
  302. close(CDATA);
  303. $? && subprocerr(_g("parse changelog"));
  304. } else {
  305. binmode(STDOUT);
  306. my @al = ($parsechangelog);
  307. push(@al,"-l$changelogfile");
  308. push(@al, "-F$changelogformat") if defined($changelogformat);
  309. push(@al, "-v$since") if defined($since);
  310. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  311. }
  312. }
  313. sub init_substvars
  314. {
  315. $substvar{'Format'} = 1.7;
  316. $substvar{'Newline'} = "\n";
  317. $substvar{'Space'} = " ";
  318. $substvar{'Tab'} = "\t";
  319. # XXX: Source-Version is now deprecated, remove in the future.
  320. $substvar{'Source-Version'}= $fi{"L Version"};
  321. $substvar{'binary:Version'} = $fi{"L Version"};
  322. $substvar{'source:Version'} = $fi{"L Version"};
  323. $substvar{'source:Version'} =~ s/\+b[0-9]+$//;
  324. $substvar{'source:Upstream-Version'} = $fi{"L Version"};
  325. $substvar{'source:Upstream-Version'} =~ s/-[^-]*$//;
  326. # FIXME: this needs all progs using controllib to set $version as 'our'.
  327. # We expect the calling program to set $version.
  328. $substvar{"dpkg:Version"} = $version;
  329. $substvar{"dpkg:Upstream-Version"} = $version;
  330. $substvar{"dpkg:Upstream-Version"} =~ s/-[^-]+$//;
  331. }
  332. sub init_substvar_arch()
  333. {
  334. $substvar{'Arch'} = get_host_arch();
  335. }
  336. sub checkpackagename {
  337. my $name = shift || '';
  338. $name =~ m/[^-+.0-9a-z]/o &&
  339. &error(sprintf(_g("source package name `%s' contains illegal character `%s'"), $name, $&));
  340. $name =~ m/^[0-9a-z]/o ||
  341. &error(sprintf(_g("source package name `%s' starts with non-alphanum"), $name));
  342. }
  343. sub checkversion {
  344. my $version = shift || '';
  345. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  346. &error(sprintf(_g("version number contains illegal character `%s'"), $&));
  347. }
  348. sub setsourcepackage {
  349. my $v = shift;
  350. checkpackagename( $v );
  351. if (defined($sourcepackage)) {
  352. $v eq $sourcepackage ||
  353. &error(sprintf(_g("source package has two conflicting values - %s and %s"), $sourcepackage, $v));
  354. } else {
  355. $sourcepackage= $v;
  356. }
  357. }
  358. sub readmd5sum {
  359. (my $md5sum = shift) or return;
  360. $md5sum =~ s/^([0-9a-f]{32})\s*\*?-?\s*\n?$/$1/o
  361. || &failure(sprintf(_g("md5sum gave bogus output `%s'"), $md5sum));
  362. return $md5sum;
  363. }
  364. # XXX: Should not be a global!!
  365. my $whatmsg;
  366. sub parsecdata {
  367. my ($cdata, $source, $many);
  368. ($cdata, $source, $many, $whatmsg) = @_;
  369. # many=0: ordinary control data like output from dpkg-parsechangelog
  370. # many=1: many paragraphs like in source control file
  371. # many=-1: single paragraph of control data optionally signed
  372. my $index = '';
  373. my $cf = '';
  374. my $paraborder = 1;
  375. while (<$cdata>) {
  376. s/\s*\n$//;
  377. next if (m/^$/ and $paraborder);
  378. next if (m/^#/);
  379. $paraborder=0;
  380. if (m/^(\S+)\s*:\s*(.*)$/) {
  381. $cf = $1;
  382. my $v = $2;
  383. $cf= &capit($cf);
  384. $fi{"$source$index $cf"}= $v;
  385. $fi{"o:$source$index $cf"}= $1;
  386. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  387. } elsif (m/^\s+\S/) {
  388. length($cf) || &syntax(_g("continued value line not in field"));
  389. $fi{"$source$index $cf"}.= "\n$_";
  390. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  391. $many == -2 && syntax(_g("expected blank line before PGP signature"));
  392. while (<$cdata>) {
  393. last if m/^$/;
  394. }
  395. $many= -2;
  396. } elsif (m/^$/) {
  397. $paraborder = 1;
  398. if ($many>0) {
  399. $index++; $cf='';
  400. } elsif ($many == -2) {
  401. $_ = <$cdata> while defined($_) && $_ =~ /^\s*$/;
  402. length($_) ||
  403. &syntax(_g("expected PGP signature, found EOF after blank line"));
  404. s/\n$//;
  405. m/^-----BEGIN PGP/ ||
  406. &syntax(sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  407. $many= -3; last;
  408. } else {
  409. while (<$cdata>) {
  410. /^\s*$/ ||
  411. &syntax(_g("found several \`paragraphs' where only one expected"));
  412. }
  413. }
  414. } else {
  415. &syntax(_g("line with unknown format (not field-colon-value)"));
  416. }
  417. }
  418. $many == -2 && &syntax(_g("found start of PGP body but no signature"));
  419. if (length($cf)) { $index++; }
  420. $index || &syntax(_g("empty file"));
  421. return $index;
  422. }
  423. sub unknown {
  424. my $field = $_;
  425. warning(sprintf(_g("unknown information field '%s' in input data in %s"),
  426. $field, $_[0]));
  427. }
  428. sub syntax {
  429. &error(sprintf(_g("syntax error in %s at line %d: %s"), $whatmsg, $., $_[0]));
  430. }
  431. sub failure { die sprintf(_g("%s: failure: %s"), $progname, $_[0])."\n"; }
  432. sub syserr { die sprintf(_g("%s: failure: %s: %s"), $progname, $_[0], $!)."\n"; }
  433. sub error { die sprintf(_g("%s: error: %s"), $progname, $_[0])."\n"; }
  434. sub internerr { die sprintf(_g("%s: internal error: %s"), $progname, $_[0])."\n"; }
  435. sub warning
  436. {
  437. if (!$quiet_warnings) {
  438. warn sprintf(_g("%s: warning: %s"), $progname, $_[0])."\n";
  439. }
  440. }
  441. sub usageerr
  442. {
  443. printf(STDERR "%s: %s\n\n", $progname, "@_");
  444. &usage;
  445. exit(2);
  446. }
  447. sub warnerror
  448. {
  449. if ($warnable_error) {
  450. warning(@_);
  451. } else {
  452. error(@_);
  453. }
  454. }
  455. sub subprocerr {
  456. my ($p) = @_;
  457. require POSIX;
  458. if (POSIX::WIFEXITED($?)) {
  459. die sprintf(_g("%s: failure: %s gave error exit status %s"),
  460. $progname, $p, POSIX::WEXITSTATUS($?))."\n";
  461. } elsif (POSIX::WIFSIGNALED($?)) {
  462. die sprintf(_g("%s: failure: %s died from signal %s"),
  463. $progname, $p, POSIX::WTERMSIG($?))."\n";
  464. } else {
  465. die sprintf(_g("%s: failure: %s failed with unknown exit code %d"),
  466. $progname, $p, $?)."\n";
  467. }
  468. }
  469. 1;