controllib.pl 14 KB

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