controllib.pl 12 KB

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