controllib.pl 14 KB

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