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. &parsesubstvars if ($dosubstvars);
  93. for $f (sort { $fieldimps{$b} <=> $fieldimps{$a} } keys %f) {
  94. $v= $f{$f};
  95. if ($dosubstvars) {
  96. $v= &substvars($v);
  97. }
  98. $v =~ m/\S/ || next; # delete whitespace-only fields
  99. $v =~ m/\n\S/ && &internerr("field $f has newline then non whitespace >$v<");
  100. $v =~ m/\n[ \t]*\n/ && &internerr("field $f has blank lines >$v<");
  101. $v =~ m/\n$/ && &internerr("field $f has trailing newline >$v<");
  102. if ($dosubstvars) {
  103. $v =~ s/,[\s,]*,/,/g;
  104. $v =~ s/^\s*,\s*//;
  105. $v =~ s/\s*,\s*$//;
  106. }
  107. $v =~ s/\$\{\}/\$/g;
  108. print("$f: $v\n") || &syserr("write error on control data");
  109. }
  110. close(STDOUT) || &syserr("write error on close control data");
  111. }
  112. sub parsecontrolfile {
  113. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  114. open(CDATA,"< $controlfile") || &error("cannot read control file $controlfile: $!");
  115. binmode(CDATA);
  116. $indices= &parsecdata('C',1,"control file $controlfile");
  117. $indices >= 2 || &error("control file must have at least one binary package part");
  118. for ($i=1;$i<$indices;$i++) {
  119. defined($fi{"C$i Package"}) ||
  120. &error("per-package paragraph $i in control info file is ".
  121. "missing Package line");
  122. }
  123. defined($fi{"C Source"}) ||
  124. &error("source paragraph in control info file is ".
  125. "missing Source line");
  126. }
  127. my $substvarsparsed = 0;
  128. sub parsesubstvars {
  129. if (length($varlistfile) && !$substvarsparsed) {
  130. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  131. if (open(SV,"< $varlistfile")) {
  132. binmode(SV);
  133. while (<SV>) {
  134. next if m/^\#/ || !m/\S/;
  135. s/\s*\n$//;
  136. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  137. &error("bad line in substvars file $varlistfile at line $.");
  138. $substvar{$1}= $';
  139. }
  140. close(SV);
  141. } elsif ($! != ENOENT ) {
  142. &error("unable to open substvars file $varlistfile: $!");
  143. }
  144. $substvarsparsed = 1;
  145. }
  146. }
  147. sub parsedep {
  148. my ($dep_line, $use_arch, $reduce_arch) = @_;
  149. my @dep_list;
  150. if (!$host_arch) {
  151. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  152. chomp $host_arch;
  153. }
  154. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  155. my @or_list = ();
  156. ALTERNATE:
  157. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  158. my ($package, $relation, $version);
  159. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  160. ($relation, $version) = ($1, $2) if ($dep_or =~ s/^\((=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  161. my @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  162. if ($reduce_arch && @arches) {
  163. my $seen_arch='';
  164. foreach my $arch (@arches) {
  165. $arch=lc($arch);
  166. if ($arch eq $host_arch) {
  167. $seen_arch=1;
  168. next;
  169. } elsif ($arch eq "!$host_arch") {
  170. next ALTERNATE;
  171. } elsif ($arch =~ /!/) {
  172. # This is equivilant to
  173. # having seen the current arch,
  174. # unless the current arch
  175. # is also listed..
  176. $seen_arch=1;
  177. }
  178. }
  179. if (! $seen_arch) {
  180. next;
  181. }
  182. }
  183. return undef if (length($dep_or));
  184. push @or_list, [ $package, $relation, $version, \@arches ];
  185. }
  186. push @dep_list, \@or_list;
  187. }
  188. \@dep_list;
  189. }
  190. sub showdep {
  191. my ($dep_list, $show_arch) = @_;
  192. my @and_list;
  193. foreach my $dep_and (@$dep_list) {
  194. my @or_list = ();
  195. foreach my $dep_or (@$dep_and) {
  196. my ($package, $relation, $version, $arch_list) = @$dep_or;
  197. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  198. }
  199. push @and_list, join(' | ', @or_list);
  200. }
  201. join(', ', @and_list);
  202. }
  203. sub parsechangelog {
  204. defined($c=open(CDATA,"-|")) || &syserr("fork for parse changelog");
  205. binmode(CDATA);
  206. if (!$c) {
  207. @al=($parsechangelog);
  208. push(@al,"-F$changelogformat") if length($changelogformat);
  209. push(@al,"-v$since") if length($since);
  210. push(@al,"-l$changelogfile");
  211. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  212. }
  213. &parsecdata('L',0,"parsed version of changelog");
  214. close(CDATA); $? && &subprocerr("parse changelog");
  215. $substvar{'Source-Version'}= $fi{"L Version"};
  216. }
  217. sub setsourcepackage {
  218. if (length($sourcepackage)) {
  219. $v eq $sourcepackage ||
  220. &error("source package has two conflicting values - $sourcepackage and $v");
  221. } else {
  222. $sourcepackage= $v;
  223. }
  224. }
  225. sub parsecdata {
  226. local ($source,$many,$whatmsg) = @_;
  227. # many=0: ordinary control data like output from dpkg-parsechangelog
  228. # many=1: many paragraphs like in source control file
  229. # many=-1: single paragraph of control data optionally signed
  230. local ($index,$cf,$paraborder);
  231. $index=''; $cf=''; $paraborder=1;
  232. while (<CDATA>) {
  233. s/\s*\n$//;
  234. next if (m/^$/ and $paraborder);
  235. next if (m/^#/);
  236. $paraborder=0;
  237. if (m/^(\S+)\s*:\s*(.*)$/) {
  238. $cf=$1; $v=$2;
  239. $cf= &capit($cf);
  240. $fi{"$source$index $cf"}= $v;
  241. $fi{"o:$source$index $cf"}= $1;
  242. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  243. } elsif (m/^\s+\S/) {
  244. length($cf) || &syntax("continued value line not in field");
  245. $fi{"$source$index $cf"}.= "\n$_";
  246. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  247. $many == -2 && syntax("expected blank line before PGP signature");
  248. while (<CDATA>) { last if m/^$/; }
  249. $many= -2;
  250. } elsif (m/^$/) {
  251. $paraborder = 1;
  252. if ($many>0) {
  253. $index++; $cf='';
  254. } elsif ($many == -2) {
  255. $_= <CDATA>;
  256. length($_) ||
  257. &syntax("expected PGP signature, found EOF after blank line");
  258. s/\n$//;
  259. m/^-----BEGIN PGP/ ||
  260. &syntax("expected PGP signature, found something else \`$_'");
  261. $many= -3; last;
  262. } else {
  263. &syntax("found several \`paragraphs' where only one expected");
  264. }
  265. } else {
  266. &syntax("line with unknown format (not field-colon-value)");
  267. }
  268. }
  269. $many == -2 && &syntax("found start of PGP body but no signature");
  270. if (length($cf)) { $index++; }
  271. $index || &syntax("empty file");
  272. return $index;
  273. }
  274. sub unknown {
  275. &warn("unknown information field " . $fi{"o:$_"} . " in input data in $_[0]");
  276. }
  277. sub syntax {
  278. &error("syntax error in $whatmsg at line $.: $_[0]");
  279. }
  280. sub failure { die "$progname: failure: $_[0]\n"; }
  281. sub syserr { die "$progname: failure: $_[0]: $!\n"; }
  282. sub error { die "$progname: error: $_[0]\n"; }
  283. sub internerr { die "$progname: internal error: $_[0]\n"; }
  284. sub warn { warn "$progname: warning: $_[0]\n"; }
  285. sub usageerr { print(STDERR "$progname: @_\n\n"); &usageversion; exit(2); }
  286. sub warnerror { if ($warnable_error) { &warn( @_ ); } else { &error( @_ ); } }
  287. sub subprocerr {
  288. local ($p) = @_;
  289. if (WIFEXITED($?)) {
  290. die "$progname: failure: $p gave error exit status ".WEXITSTATUS($?)."\n";
  291. } elsif (WIFSIGNALED($?)) {
  292. die "$progname: failure: $p died from signal ".WTERMSIG($?)."\n";
  293. } else {
  294. die "$progname: failure: $p failed with unknown exit code $?\n";
  295. }
  296. }
  297. 1;