controllib.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #!/usr/bin/perl
  2. # Global variables:
  3. # $v - value parameter to function
  4. # $sourcepackage - name of sourcepackage
  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. @pkg_dep_fields = qw(Replaces Provides Depends Pre-Depends Recommends Suggests
  14. Conflicts Enhances);
  15. @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  16. Build-Conflicts Build-Conflicts-Indep);
  17. $substvar{'Format'}= 1.7;
  18. $substvar{'Newline'}= "\n";
  19. $substvar{'Space'}= " ";
  20. $substvar{'Tab'}= "\t";
  21. $maxsubsts=50;
  22. $warnable_error= 1;
  23. $progname= $0; $progname= $& if $progname =~ m,[^/]+$,;
  24. $getlogin = getlogin();
  25. if(!defined($getlogin)) {
  26. open(SAVEIN, "<&STDIN");
  27. close(STDIN);
  28. open(STDIN, "<&STDERR");
  29. $getlogin = getlogin();
  30. close(STDIN);
  31. open(STDIN, "<&SAVEIN");
  32. close(SAVEIN);
  33. }
  34. if(!defined($getlogin)) {
  35. open(SAVEIN, "<&STDIN");
  36. close(STDIN);
  37. open(STDIN, "<&STDOUT");
  38. $getlogin = getlogin();
  39. close(STDIN);
  40. open(STDIN, "<&SAVEIN");
  41. close(SAVEIN);
  42. }
  43. if (defined ($ENV{'LOGNAME'})) {
  44. @fowner = getpwnam ($ENV{'LOGNAME'});
  45. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $ENV{'LOGNAME'})); }
  46. } elsif (defined ($getlogin)) {
  47. @fowner = getpwnam ($getlogin);
  48. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $getlogin)); }
  49. } else {
  50. &warn (sprintf ('no utmp entry available and LOGNAME not defined; using uid of process (%d)', $<));
  51. @fowner = getpwuid ($<);
  52. if (! @fowner) { die (sprintf ('unable to get login information for uid %d', $<)); }
  53. }
  54. @fowner = @fowner[2,3];
  55. sub capit {
  56. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  57. return join '-', @pieces;
  58. }
  59. sub findarch {
  60. $arch=`dpkg-architecture -qDEB_HOST_ARCH`;
  61. $? && &subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  62. chomp $arch;
  63. $substvar{'Arch'}= $arch;
  64. }
  65. sub substvars {
  66. my ($v) = @_;
  67. my ($lhs,$vn,$rhs,$count);
  68. $count=0;
  69. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  70. # If we have consumed more from the leftover data, then
  71. # reset the recursive counter.
  72. $count= 0 if (length($') < length($rhs));
  73. $count < $maxsubsts ||
  74. &error("too many substitutions - recursive ? - in \`$v'");
  75. $lhs=$`; $vn=$1; $rhs=$';
  76. if (defined($substvar{$vn})) {
  77. $v= $lhs.$substvar{$vn}.$rhs;
  78. $count++;
  79. } else {
  80. &warn("unknown substitution variable \${$vn}");
  81. $v= $lhs.$rhs;
  82. }
  83. }
  84. return $v;
  85. }
  86. sub outputclose {
  87. my ($dosubstvars) = @_;
  88. for $f (keys %f) { $substvar{"F:$f"}= $f{$f}; }
  89. &parsesubstvars if ($dosubstvars);
  90. for $f (sort { $fieldimps{$b} <=> $fieldimps{$a} } keys %f) {
  91. $v= $f{$f};
  92. if ($dosubstvars) {
  93. $v= &substvars($v);
  94. }
  95. $v =~ m/\S/ || next; # delete whitespace-only fields
  96. $v =~ m/\n\S/ && &internerr("field $f has newline then non whitespace >$v<");
  97. $v =~ m/\n[ \t]*\n/ && &internerr("field $f has blank lines >$v<");
  98. $v =~ m/\n$/ && &internerr("field $f has trailing newline >$v<");
  99. if ($dosubstvars) {
  100. $v =~ s/,[\s,]*,/,/g;
  101. $v =~ s/^\s*,\s*//;
  102. $v =~ s/\s*,\s*$//;
  103. }
  104. $v =~ s/\$\{\}/\$/g;
  105. print("$f: $v\n") || &syserr("write error on control data");
  106. }
  107. close(STDOUT) || &syserr("write error on close control data");
  108. }
  109. sub parsecontrolfile {
  110. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  111. open(CDATA,"< $controlfile") || &error("cannot read control file $controlfile: $!");
  112. binmode(CDATA);
  113. $indices= &parsecdata('C',1,"control file $controlfile");
  114. $indices >= 2 || &error("control file must have at least one binary package part");
  115. for ($i=1;$i<$indices;$i++) {
  116. defined($fi{"C$i Package"}) ||
  117. &error("per-package paragraph $i in control info file is ".
  118. "missing Package line");
  119. }
  120. defined($fi{"C Source"}) ||
  121. &error("source paragraph in control info file is ".
  122. "missing Source line");
  123. }
  124. my $substvarsparsed = 0;
  125. sub parsesubstvars {
  126. if (length($varlistfile) && !$substvarsparsed) {
  127. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  128. if (open(SV,"< $varlistfile")) {
  129. binmode(SV);
  130. while (<SV>) {
  131. next if m/^\#/ || !m/\S/;
  132. s/\s*\n$//;
  133. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  134. &error("bad line in substvars file $varlistfile at line $.");
  135. $substvar{$1}= $';
  136. }
  137. close(SV);
  138. } elsif ($! != ENOENT ) {
  139. &error("unable to open substvars file $varlistfile: $!");
  140. }
  141. $substvarsparsed = 1;
  142. }
  143. }
  144. sub parsedep {
  145. my ($dep_line, $use_arch, $reduce_arch) = @_;
  146. my @dep_list;
  147. if (!$host_arch) {
  148. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  149. chomp $host_arch;
  150. }
  151. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  152. my @or_list = ();
  153. ALTERNATE:
  154. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  155. my ($package, $relation, $version);
  156. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  157. ($relation, $version) = ($1, $2)
  158. if ($dep_or =~ s/^\(\s*(=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  159. my @arches;
  160. @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  161. if ($reduce_arch && @arches) {
  162. my $seen_arch='';
  163. foreach my $arch (@arches) {
  164. $arch=lc($arch);
  165. if ($arch eq $host_arch) {
  166. $seen_arch=1;
  167. next;
  168. } elsif ($arch eq "!$host_arch") {
  169. next ALTERNATE;
  170. } elsif ($arch =~ /!/) {
  171. # This is equivilant to
  172. # having seen the current arch,
  173. # unless the current arch
  174. # is also listed..
  175. $seen_arch=1;
  176. }
  177. }
  178. if (! $seen_arch) {
  179. next;
  180. }
  181. }
  182. if (length($dep_or)) {
  183. &warn("can't parse dependency $dep_and");
  184. return undef;
  185. }
  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> while defined($_) && $_ =~ /^\s*$/;
  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. while (<CDATA>) {
  266. /^\s*$/ ||
  267. &syntax("found several \`paragraphs' where only one expected");
  268. }
  269. }
  270. } else {
  271. &syntax("line with unknown format (not field-colon-value)");
  272. }
  273. }
  274. $many == -2 && &syntax("found start of PGP body but no signature");
  275. if (length($cf)) { $index++; }
  276. $index || &syntax("empty file");
  277. return $index;
  278. }
  279. sub unknown {
  280. my $field = $_;
  281. &warn("unknown information field \`$field\' 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;