controllib.pl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. $substvar{'Format'}= 1.7;
  17. $substvar{'Newline'}= "\n";
  18. $substvar{'Space'}= " ";
  19. $substvar{'Tab'}= "\t";
  20. $maxsubsts=50;
  21. $warnable_error= 1;
  22. $progname= $0; $progname= $& if $progname =~ m,[^/]+$,;
  23. $getlogin = getlogin();
  24. if(!defined($getlogin)) {
  25. open(SAVEIN, "<&STDIN");
  26. close(STDIN);
  27. open(STDIN, "<&STDERR");
  28. $getlogin = getlogin();
  29. close(STDIN);
  30. open(STDIN, "<&SAVEIN");
  31. close(SAVEIN);
  32. }
  33. if(!defined($getlogin)) {
  34. open(SAVEIN, "<&STDIN");
  35. close(STDIN);
  36. open(STDIN, "<&STDOUT");
  37. $getlogin = getlogin();
  38. close(STDIN);
  39. open(STDIN, "<&SAVEIN");
  40. close(SAVEIN);
  41. }
  42. if (defined ($ENV{'LOGNAME'})) {
  43. @fowner = getpwnam ($ENV{'LOGNAME'});
  44. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $ENV{'LOGNAME'})); }
  45. } elsif (defined ($getlogin)) {
  46. @fowner = getpwnam ($getlogin);
  47. if (! @fowner) { die (sprintf ('unable to get login information for username "%s"', $getlogin)); }
  48. } else {
  49. &warn (sprintf ('no utmp entry available and LOGNAME not defined; using uid of process (%d)', $<));
  50. @fowner = getpwuid ($<);
  51. if (! @fowner) { die (sprintf ('unable to get login information for uid %d', $<)); }
  52. }
  53. @fowner = @fowner[2,3];
  54. sub capit {
  55. return defined($capit{lc $_[0]}) ? $capit{lc $_[0]} :
  56. (uc substr($_[0],0,1)).(lc substr($_[0],1));
  57. }
  58. sub findarch {
  59. $arch=`dpkg-architecture -qDEB_HOST_ARCH`;
  60. $? && &subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  61. chomp $arch;
  62. $substvar{'Arch'}= $arch;
  63. }
  64. sub substvars {
  65. my ($v) = @_;
  66. my ($lhs,$vn,$rhs,$count);
  67. $count=0;
  68. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  69. # If we have consumed more from the leftover data, then
  70. # reset the recursive counter.
  71. $count= 0 if (length($') < length($rhs));
  72. $count < $maxsubsts ||
  73. &error("too many substitutions - recursive ? - in \`$v'");
  74. $lhs=$`; $vn=$1; $rhs=$';
  75. if (defined($substvar{$vn})) {
  76. $v= $lhs.$substvar{$vn}.$rhs;
  77. $count++;
  78. } else {
  79. &warn("unknown substitution variable \${$vn}");
  80. $v= $lhs.$rhs;
  81. }
  82. }
  83. return $v;
  84. }
  85. sub outputclose {
  86. my ($dosubstvars) = @_;
  87. for $f (keys %f) { $substvar{"F:$f"}= $f{$f}; }
  88. if (length($varlistfile) and $dosubstvars) {
  89. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  90. if (open(SV,"< $varlistfile")) {
  91. while (<SV>) {
  92. next if m/^\#/ || !m/\S/;
  93. s/\s*\n$//;
  94. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  95. &error("bad line in substvars file $varlistfile at line $.");
  96. $substvar{$1}= $';
  97. }
  98. close(SV);
  99. } elsif ($! != ENOENT ) {
  100. &error("unable to open substvars file $varlistfile: $!");
  101. }
  102. }
  103. for $f (sort { $fieldimps{$b} <=> $fieldimps{$a} } keys %f) {
  104. $v= $f{$f};
  105. if ($dosubstvars) {
  106. $v= &substvars($v);
  107. }
  108. $v =~ m/\S/ || next; # delete whitespace-only fields
  109. $v =~ m/\n\S/ && &internerr("field $f has newline then non whitespace >$v<");
  110. $v =~ m/\n[ \t]*\n/ && &internerr("field $f has blank lines >$v<");
  111. $v =~ m/\n$/ && &internerr("field $f has trailing newline >$v<");
  112. if ($dosubstvars) {
  113. $v =~ s/,[\s,]*,/,/g;
  114. $v =~ s/^\s*,\s*//;
  115. $v =~ s/\s*,\s*$//;
  116. }
  117. $v =~ s/\$\{\}/\$/g;
  118. print("$f: $v\n") || &syserr("write error on control data");
  119. }
  120. close(STDOUT) || &syserr("write error on close control data");
  121. }
  122. sub parsecontrolfile {
  123. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  124. open(CDATA,"< $controlfile") || &error("cannot read control file $controlfile: $!");
  125. $indices= &parsecdata('C',1,"control file $controlfile");
  126. $indices >= 2 || &error("control file must have at least one binary package part");
  127. for ($i=1;$i<$indices;$i++) {
  128. defined($fi{"C$i Package"}) ||
  129. &error("per-package paragraph $i in control info file is ".
  130. "missing Package line");
  131. }
  132. }
  133. sub parsechangelog {
  134. defined($c=open(CDATA,"-|")) || &syserr("fork for parse changelog");
  135. if (!$c) {
  136. @al=($parsechangelog);
  137. push(@al,"-F$changelogformat") if length($changelogformat);
  138. push(@al,"-v$since") if length($since);
  139. push(@al,"-l$changelogfile");
  140. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  141. }
  142. &parsecdata('L',0,"parsed version of changelog");
  143. close(CDATA); $? && &subprocerr("parse changelog");
  144. $substvar{'Source-Version'}= $fi{"L Version"};
  145. }
  146. sub setsourcepackage {
  147. if (length($sourcepackage)) {
  148. $v eq $sourcepackage ||
  149. &error("source package has two conflicting values - $sourcepackage and $v");
  150. } else {
  151. $sourcepackage= $v;
  152. }
  153. }
  154. sub parsecdata {
  155. local ($source,$many,$whatmsg) = @_;
  156. # many=0: ordinary control data like output from dpkg-parsechangelog
  157. # many=1: many paragraphs like in source control file
  158. # many=-1: single paragraph of control data optionally signed
  159. local ($index,$cf,$paraborder);
  160. $index=''; $cf=''; $paraborder=1;
  161. while (<CDATA>) {
  162. s/\s*\n$//;
  163. next if (m/^$/ and $paraborder);
  164. $paraborder=0;
  165. if (m/^(\S+)\s*:\s*(.*)$/) {
  166. $cf=$1; $v=$2;
  167. $cf= &capit($cf);
  168. $fi{"$source$index $cf"}= $v;
  169. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  170. } elsif (m/^\s+\S/) {
  171. length($cf) || &syntax("continued value line not in field");
  172. $fi{"$source$index $cf"}.= "\n$_";
  173. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  174. $many == -2 && syntax("expected blank line before PGP signature");
  175. while (<CDATA>) { last if m/^$/; }
  176. $many= -2;
  177. } elsif (m/^$/) {
  178. $paraborder = 1;
  179. if ($many>0) {
  180. $index++; $cf='';
  181. } elsif ($many == -2) {
  182. $_= <CDATA>;
  183. length($_) ||
  184. &syntax("expected PGP signature, found EOF after blank line");
  185. s/\n$//;
  186. m/^-----BEGIN PGP/ ||
  187. &syntax("expected PGP signature, found something else \`$_'");
  188. $many= -3; last;
  189. } else {
  190. &syntax("found several \`paragraphs' where only one expected");
  191. }
  192. } else {
  193. &syntax("line with unknown format (not field-colon-value)");
  194. }
  195. }
  196. $many == -2 && &syntax("found start of PGP body but no signature");
  197. if (length($cf)) { $index++; }
  198. $index || &syntax("empty file");
  199. return $index;
  200. }
  201. sub unknown {
  202. &warn("unknown information field $_ in input data in $_[0]");
  203. }
  204. sub syntax {
  205. &error("syntax error in $whatmsg at line $.: $_[0]");
  206. }
  207. sub failure { die "$progname: failure: $_[0]\n"; }
  208. sub syserr { die "$progname: failure: $_[0]: $!\n"; }
  209. sub error { die "$progname: error: $_[0]\n"; }
  210. sub internerr { die "$progname: internal error: $_[0]\n"; }
  211. sub warn { warn "$progname: warning: $_[0]\n"; }
  212. sub usageerr { print(STDERR "$progname: @_\n\n"); &usageversion; exit(2); }
  213. sub warnerror { if ($warnable_error) { &warn( @_ ); } else { &error( @_ ); } }
  214. sub subprocerr {
  215. local ($p) = @_;
  216. if (WIFEXITED($?)) {
  217. die "$progname: failure: $p gave error exit status ".WEXITSTATUS($?)."\n";
  218. } elsif (WIFSIGNALED($?)) {
  219. die "$progname: failure: $p died from signal ".WTERMSIG($?)."\n";
  220. } else {
  221. die "$progname: failure: $p failed with unknown exit code $?\n";
  222. }
  223. }
  224. 1;