controllib.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  157. my @or_list = ();
  158. ALTERNATE:
  159. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  160. my ($package, $relation, $version);
  161. $package = $1 if ($dep_or =~ s/^(\S+)\s*//m);
  162. ($relation, $version) = ($1, $2) if ($dep_or =~ s/^\((=|<=|>=|<<?|>>?)\s*([^)]+).*\)//m);
  163. my @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]//m);
  164. if ($reduce_arch && @arches) {
  165. my $seen_arch='';
  166. foreach my $arch (@arches) {
  167. $arch=lc($arch);
  168. if ($arch eq $host_arch) {
  169. $seen_arch=1;
  170. next;
  171. } elsif ($arch eq "!$host_arch") {
  172. next ALTERNATE;
  173. } elsif ($arch =~ /!/) {
  174. # This is equivilant to
  175. # having seen the current arch,
  176. # unless the current arch
  177. # is also listed..
  178. $seen_arch=1;
  179. }
  180. }
  181. if (! $seen_arch) {
  182. next;
  183. }
  184. }
  185. return undef if (length($dep_or));
  186. push @or_list, [ $package, $relation, $version, \@arches ];
  187. }
  188. push @dep_list, \@or_list;
  189. }
  190. \@dep_list;
  191. }
  192. sub showdep {
  193. my ($dep_list, $show_arch) = @_;
  194. my @and_list;
  195. foreach my $dep_and (@$dep_list) {
  196. my @or_list = ();
  197. foreach my $dep_or (@$dep_and) {
  198. my ($package, $relation, $version, $arch_list) = @$dep_or;
  199. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  200. }
  201. push @and_list, join(' | ', @or_list);
  202. }
  203. join(', ', @and_list);
  204. }
  205. sub parsechangelog {
  206. defined($c=open(CDATA,"-|")) || &syserr("fork for parse changelog");
  207. binmode(CDATA);
  208. if (!$c) {
  209. @al=($parsechangelog);
  210. push(@al,"-F$changelogformat") if length($changelogformat);
  211. push(@al,"-v$since") if length($since);
  212. push(@al,"-l$changelogfile");
  213. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  214. }
  215. &parsecdata('L',0,"parsed version of changelog");
  216. close(CDATA); $? && &subprocerr("parse changelog");
  217. $substvar{'Source-Version'}= $fi{"L Version"};
  218. }
  219. sub setsourcepackage {
  220. if (length($sourcepackage)) {
  221. $v eq $sourcepackage ||
  222. &error("source package has two conflicting values - $sourcepackage and $v");
  223. } else {
  224. $sourcepackage= $v;
  225. }
  226. }
  227. sub parsecdata {
  228. local ($source,$many,$whatmsg) = @_;
  229. # many=0: ordinary control data like output from dpkg-parsechangelog
  230. # many=1: many paragraphs like in source control file
  231. # many=-1: single paragraph of control data optionally signed
  232. local ($index,$cf,$paraborder);
  233. $index=''; $cf=''; $paraborder=1;
  234. while (<CDATA>) {
  235. s/\s*\n$//;
  236. next if (m/^$/ and $paraborder);
  237. next if (m/^#/);
  238. $paraborder=0;
  239. if (m/^(\S+)\s*:\s*(.*)$/) {
  240. $cf=$1; $v=$2;
  241. $cf= &capit($cf);
  242. $fi{"$source$index $cf"}= $v;
  243. $fi{"o:$source$index $cf"}= $1;
  244. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  245. } elsif (m/^\s+\S/) {
  246. length($cf) || &syntax("continued value line not in field");
  247. $fi{"$source$index $cf"}.= "\n$_";
  248. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  249. $many == -2 && syntax("expected blank line before PGP signature");
  250. while (<CDATA>) { last if m/^$/; }
  251. $many= -2;
  252. } elsif (m/^$/) {
  253. $paraborder = 1;
  254. if ($many>0) {
  255. $index++; $cf='';
  256. } elsif ($many == -2) {
  257. $_= <CDATA>;
  258. length($_) ||
  259. &syntax("expected PGP signature, found EOF after blank line");
  260. s/\n$//;
  261. m/^-----BEGIN PGP/ ||
  262. &syntax("expected PGP signature, found something else \`$_'");
  263. $many= -3; last;
  264. } else {
  265. &syntax("found several \`paragraphs' where only one expected");
  266. }
  267. } else {
  268. &syntax("line with unknown format (not field-colon-value)");
  269. }
  270. }
  271. $many == -2 && &syntax("found start of PGP body but no signature");
  272. if (length($cf)) { $index++; }
  273. $index || &syntax("empty file");
  274. return $index;
  275. }
  276. sub unknown {
  277. &warn("unknown information field " . $fi{"o:$_"} . " in input data in $_[0]");
  278. }
  279. sub syntax {
  280. &error("syntax error in $whatmsg at line $.: $_[0]");
  281. }
  282. sub failure { die "$progname: failure: $_[0]\n"; }
  283. sub syserr { die "$progname: failure: $_[0]: $!\n"; }
  284. sub error { die "$progname: error: $_[0]\n"; }
  285. sub internerr { die "$progname: internal error: $_[0]\n"; }
  286. sub warn { warn "$progname: warning: $_[0]\n"; }
  287. sub usageerr { print(STDERR "$progname: @_\n\n"); &usageversion; exit(2); }
  288. sub warnerror { if ($warnable_error) { &warn( @_ ); } else { &error( @_ ); } }
  289. sub subprocerr {
  290. local ($p) = @_;
  291. if (WIFEXITED($?)) {
  292. die "$progname: failure: $p gave error exit status ".WEXITSTATUS($?)."\n";
  293. } elsif (WIFSIGNALED($?)) {
  294. die "$progname: failure: $p died from signal ".WTERMSIG($?)."\n";
  295. } else {
  296. die "$progname: failure: $p failed with unknown exit code $?\n";
  297. }
  298. }
  299. 1;