controllib.pl 14 KB

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