controllib.pl 12 KB

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