controllib.pl 11 KB

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