controllib.pl 14 KB

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