controllib.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. # Global variables:
  2. # $v - value parameter to function
  3. # $sourcepackage - name of sourcepackage
  4. # %capit - map to get properly capitilized version of fields
  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. grep($capit{lc $_}=$_, qw(Pre-Depends Standards-Version Installed-Size
  14. Build-Depends Build-Depends-Indep
  15. Build-Conflicts Build-Conflicts-Indep));
  16. @pkg_dep_fields = qw(Replaces Provides Depends Pre-Depends Recommends Suggests
  17. Conflicts Enhances);
  18. @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  19. Build-Conflicts Build-Conflicts-Indep);
  20. $substvar{'Format'}= 1.7;
  21. $substvar{'Newline'}= "\n";
  22. $substvar{'Space'}= " ";
  23. $substvar{'Tab'}= "\t";
  24. $maxsubsts=50;
  25. $warnable_error= 1;
  26. $progname= $0; $progname= $& if $progname =~ m,[^/]+$,;
  27. $getlogin = getlogin();
  28. if(!defined($getlogin)) {
  29. open(SAVEIN, "<&STDIN");
  30. close(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. close(STDIN);
  40. open(STDIN, "<&STDOUT");
  41. $getlogin = getlogin();
  42. close(STDIN);
  43. open(STDIN, "<&SAVEIN");
  44. close(SAVEIN);
  45. }
  46. if (defined ($ENV{'LOGNAME'})) {
  47. @fowner = getpwnam ($ENV{'LOGNAME'});
  48. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $ENV{'LOGNAME'})); }
  49. } elsif (defined ($getlogin)) {
  50. @fowner = getpwnam ($getlogin);
  51. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $getlogin)); }
  52. } else {
  53. &warn (sprintf ('no utmp entry available and LOGNAME not defined; using uid of process (%d)', $<));
  54. @fowner = getpwuid ($<);
  55. if (! @fowner) { die (sprintf ('unable to get login information for uid %d', $<)); }
  56. }
  57. @fowner = @fowner[2,3];
  58. sub capit {
  59. return defined($capit{lc $_[0]}) ? $capit{lc $_[0]} :
  60. (uc substr($_[0],0,1)).(lc substr($_[0],1));
  61. }
  62. sub findarch {
  63. $arch=`dpkg-architecture -qDEB_HOST_ARCH`;
  64. $? && &subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  65. chomp $arch;
  66. $substvar{'Arch'}= $arch;
  67. }
  68. sub substvars {
  69. my ($v) = @_;
  70. my ($lhs,$vn,$rhs,$count);
  71. $count=0;
  72. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  73. # If we have consumed more from the leftover data, then
  74. # reset the recursive counter.
  75. $count= 0 if (length($') < length($rhs));
  76. $count < $maxsubsts ||
  77. &error("too many substitutions - recursive ? - in \`$v'");
  78. $lhs=$`; $vn=$1; $rhs=$';
  79. if (defined($substvar{$vn})) {
  80. $v= $lhs.$substvar{$vn}.$rhs;
  81. $count++;
  82. } else {
  83. &warn("unknown substitution variable \${$vn}");
  84. $v= $lhs.$rhs;
  85. }
  86. }
  87. return $v;
  88. }
  89. sub outputclose {
  90. my ($dosubstvars) = @_;
  91. for $f (keys %f) { $substvar{"F:$f"}= $f{$f}; }
  92. if (length($varlistfile) and $dosubstvars) {
  93. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  94. if (open(SV,"< $varlistfile")) {
  95. binmode(SV);
  96. while (<SV>) {
  97. next if m/^\#/ || !m/\S/;
  98. s/\s*\n$//;
  99. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  100. &error("bad line in substvars file $varlistfile at line $.");
  101. $substvar{$1}= $';
  102. }
  103. close(SV);
  104. } elsif ($! != ENOENT ) {
  105. &error("unable to open substvars file $varlistfile: $!");
  106. }
  107. }
  108. for $f (sort { $fieldimps{$b} <=> $fieldimps{$a} } keys %f) {
  109. $v= $f{$f};
  110. if ($dosubstvars) {
  111. $v= &substvars($v);
  112. }
  113. $v =~ m/\S/ || next; # delete whitespace-only fields
  114. $v =~ m/\n\S/ && &internerr("field $f has newline then non whitespace >$v<");
  115. $v =~ m/\n[ \t]*\n/ && &internerr("field $f has blank lines >$v<");
  116. $v =~ m/\n$/ && &internerr("field $f has trailing newline >$v<");
  117. if ($dosubstvars) {
  118. $v =~ s/,[\s,]*,/,/g;
  119. $v =~ s/^\s*,\s*//;
  120. $v =~ s/\s*,\s*$//;
  121. }
  122. $v =~ s/\$\{\}/\$/g;
  123. print("$f: $v\n") || &syserr("write error on control data");
  124. }
  125. close(STDOUT) || &syserr("write error on close control data");
  126. }
  127. sub parsecontrolfile {
  128. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  129. open(CDATA,"< $controlfile") || &error("cannot read control file $controlfile: $!");
  130. binmode(CDATA);
  131. $indices= &parsecdata('C',1,"control file $controlfile");
  132. $indices >= 2 || &error("control file must have at least one binary package part");
  133. for ($i=1;$i<$indices;$i++) {
  134. defined($fi{"C$i Package"}) ||
  135. &error("per-package paragraph $i in control info file is ".
  136. "missing Package line");
  137. foreach my $dep_field (@pkg_dep_fields) {
  138. if (defined($fi{"C$i $dep_field"})) {
  139. ($fi{"C$i $dep_field"} = parsedep($fi{"C$i $dep_field"}, 1, 1)) ||
  140. &error("per-package paragraph $i in control info file ".
  141. "invalid dependency field \`$dep_field'");
  142. }
  143. }
  144. }
  145. foreach my $dep_field (@src_dep_fields) {
  146. if (defined($fi{"C $dep_field"})) {
  147. ($fi{"C $dep_field"} = parsedep($fi{"C $dep_field"}, 1, 1)) ||
  148. &error("source paragraph in control info file ".
  149. "invalid dependency field \`$dep_field'");
  150. }
  151. }
  152. }
  153. sub parsedep {
  154. my ($dep_line, $use_arch, $reduce_arch) = @_;
  155. my @dep_list;
  156. if (!$host_arch) {
  157. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  158. chomp $host_arch;
  159. }
  160. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  161. my @or_list = ();
  162. ALTERNATE:
  163. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  164. my ($package, $relation, $version);
  165. $package = $1 if ($dep_or =~ s/^(\S+)\s*//m);
  166. ($relation, $version) = ($1, $2) if ($dep_or =~ s/^\((=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  167. my @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  168. if ($reduce_arch && @arches) {
  169. my $seen_arch='';
  170. foreach my $arch (@arches) {
  171. $arch=lc($arch);
  172. if ($arch eq $host_arch) {
  173. $seen_arch=1;
  174. next;
  175. } elsif ($arch eq "!$host_arch") {
  176. next ALTERNATE;
  177. } elsif ($arch =~ /!/) {
  178. # This is equivilant to
  179. # having seen the current arch,
  180. # unless the current arch
  181. # is also listed..
  182. $seen_arch=1;
  183. }
  184. }
  185. if (! $seen_arch) {
  186. next;
  187. }
  188. }
  189. return undef if (length($dep_or));
  190. push @or_list, [ $package, $relation, $version, \@arches ];
  191. }
  192. push @dep_list, \@or_list;
  193. }
  194. \@dep_list;
  195. }
  196. sub showdep {
  197. my ($dep_list, $show_arch) = @_;
  198. my @and_list;
  199. foreach my $dep_and (@$dep_list) {
  200. my @or_list = ();
  201. foreach my $dep_or (@$dep_and) {
  202. my ($package, $relation, $version, $arch_list) = @$dep_or;
  203. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  204. }
  205. push @and_list, join(' | ', @or_list);
  206. }
  207. join(', ', @and_list);
  208. }
  209. sub parsechangelog {
  210. defined($c=open(CDATA,"-|")) || &syserr("fork for parse changelog");
  211. binmode(CDATA);
  212. if (!$c) {
  213. @al=($parsechangelog);
  214. push(@al,"-F$changelogformat") if length($changelogformat);
  215. push(@al,"-v$since") if length($since);
  216. push(@al,"-l$changelogfile");
  217. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  218. }
  219. &parsecdata('L',0,"parsed version of changelog");
  220. close(CDATA); $? && &subprocerr("parse changelog");
  221. $substvar{'Source-Version'}= $fi{"L Version"};
  222. }
  223. sub setsourcepackage {
  224. if (length($sourcepackage)) {
  225. $v eq $sourcepackage ||
  226. &error("source package has two conflicting values - $sourcepackage and $v");
  227. } else {
  228. $sourcepackage= $v;
  229. }
  230. }
  231. sub parsecdata {
  232. local ($source,$many,$whatmsg) = @_;
  233. # many=0: ordinary control data like output from dpkg-parsechangelog
  234. # many=1: many paragraphs like in source control file
  235. # many=-1: single paragraph of control data optionally signed
  236. local ($index,$cf,$paraborder);
  237. $index=''; $cf=''; $paraborder=1;
  238. while (<CDATA>) {
  239. s/\s*\n$//;
  240. next if (m/^$/ and $paraborder);
  241. next if (m/^#/);
  242. $paraborder=0;
  243. if (m/^(\S+)\s*:\s*(.*)$/) {
  244. $cf=$1; $v=$2;
  245. $cf= &capit($cf);
  246. $fi{"$source$index $cf"}= $v;
  247. $fi{"o:$source$index $cf"}= $1;
  248. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  249. } elsif (m/^\s+\S/) {
  250. length($cf) || &syntax("continued value line not in field");
  251. $fi{"$source$index $cf"}.= "\n$_";
  252. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  253. $many == -2 && syntax("expected blank line before PGP signature");
  254. while (<CDATA>) { last if m/^$/; }
  255. $many= -2;
  256. } elsif (m/^$/) {
  257. $paraborder = 1;
  258. if ($many>0) {
  259. $index++; $cf='';
  260. } elsif ($many == -2) {
  261. $_= <CDATA>;
  262. length($_) ||
  263. &syntax("expected PGP signature, found EOF after blank line");
  264. s/\n$//;
  265. m/^-----BEGIN PGP/ ||
  266. &syntax("expected PGP signature, found something else \`$_'");
  267. $many= -3; last;
  268. } else {
  269. &syntax("found several \`paragraphs' where only one expected");
  270. }
  271. } else {
  272. &syntax("line with unknown format (not field-colon-value)");
  273. }
  274. }
  275. $many == -2 && &syntax("found start of PGP body but no signature");
  276. if (length($cf)) { $index++; }
  277. $index || &syntax("empty file");
  278. return $index;
  279. }
  280. sub unknown {
  281. &warn("unknown information field " . $fi{"o:$_"} . " in input data in $_[0]");
  282. }
  283. sub syntax {
  284. &error("syntax error in $whatmsg at line $.: $_[0]");
  285. }
  286. sub failure { die "$progname: failure: $_[0]\n"; }
  287. sub syserr { die "$progname: failure: $_[0]: $!\n"; }
  288. sub error { die "$progname: error: $_[0]\n"; }
  289. sub internerr { die "$progname: internal error: $_[0]\n"; }
  290. sub warn { warn "$progname: warning: $_[0]\n"; }
  291. sub usageerr { print(STDERR "$progname: @_\n\n"); &usageversion; exit(2); }
  292. sub warnerror { if ($warnable_error) { &warn( @_ ); } else { &error( @_ ); } }
  293. sub subprocerr {
  294. local ($p) = @_;
  295. if (WIFEXITED($?)) {
  296. die "$progname: failure: $p gave error exit status ".WEXITSTATUS($?)."\n";
  297. } elsif (WIFSIGNALED($?)) {
  298. die "$progname: failure: $p died from signal ".WTERMSIG($?)."\n";
  299. } else {
  300. die "$progname: failure: $p failed with unknown exit code $?\n";
  301. }
  302. }
  303. 1;