controllib.pl.in 14 KB

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