controllib.pl 8.1 KB

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