controllib.pl.in 14 KB

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