controllib.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $arch=`dpkg-architecture -qDEB_HOST_ARCH`;
  68. $? && &subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  69. chomp $arch;
  70. $substvar{'Arch'}= $arch;
  71. }
  72. sub substvars {
  73. my ($v) = @_;
  74. my ($lhs,$vn,$rhs,$count);
  75. $count=0;
  76. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  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. if (length($varlistfile) and $dosubstvars) {
  94. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  95. if (open(SV,"< $varlistfile")) {
  96. while (<SV>) {
  97. next if m/^\#/ || !m/\S/;
  98. s/\s*\n$//;
  99. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  100. &error("bad line in substvars file $varlistfile at line $.");
  101. $substvar{$1}= $';
  102. }
  103. close(SV);
  104. } elsif ($! != ENOENT ) {
  105. &error("unable to open substvars file $varlistfile: $!");
  106. }
  107. }
  108. for $f (sort { $fieldimps{$b} <=> $fieldimps{$a} } keys %f) {
  109. $v= $f{$f};
  110. if ($dosubstvars) {
  111. $v= &substvars($v);
  112. }
  113. $v =~ m/\S/ || next; # delete whitespace-only fields
  114. $v =~ m/\n\S/ && &internerr("field $f has newline then non whitespace >$v<");
  115. $v =~ m/\n[ \t]*\n/ && &internerr("field $f has blank lines >$v<");
  116. $v =~ m/\n$/ && &internerr("field $f has trailing newline >$v<");
  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);
  160. $index=''; $cf='';
  161. while (<CDATA>) {
  162. s/\s*\n$//;
  163. if (m/^(\S+)\s*:\s*(.*)$/) {
  164. $cf=$1; $v=$2;
  165. $cf= &capit($cf);
  166. $fi{"$source$index $cf"}= $v;
  167. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  168. } elsif (m/^\s+\S/) {
  169. length($cf) || &syntax("continued value line not in field");
  170. $fi{"$source$index $cf"}.= "\n$_";
  171. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  172. while (<CDATA>) { last if m/^$/; }
  173. $many= -2;
  174. } elsif (m/^$/) {
  175. if ($many>0) {
  176. $index++; $cf='';
  177. } elsif ($many == -2) {
  178. $_= <CDATA>;
  179. length($_) ||
  180. &syntax("expected PGP signature, found EOF after blank line");
  181. s/\n$//;
  182. m/^-----BEGIN PGP/ ||
  183. &syntax("expected PGP signature, found something else \`$_'");
  184. $many= -3; last;
  185. } else {
  186. &syntax("found several \`paragraphs' where only one expected");
  187. }
  188. } else {
  189. &syntax("line with unknown format (not field-colon-value)");
  190. }
  191. }
  192. $many == -2 && &syntax("found start of PGP body but no signature");
  193. if (length($cf)) { $index++; }
  194. $index || &syntax("empty file");
  195. return $index;
  196. }
  197. sub unknown {
  198. &warn("unknown information field $_ in input data in $_[0]");
  199. }
  200. sub syntax {
  201. &error("syntax error in $whatmsg at line $.: $_[0]");
  202. }
  203. sub failure { die "$progname: failure: $_[0]\n"; }
  204. sub syserr { die "$progname: failure: $_[0]: $!\n"; }
  205. sub error { die "$progname: error: $_[0]\n"; }
  206. sub internerr { die "$progname: internal error: $_[0]\n"; }
  207. sub warn { warn "$progname: warning: $_[0]\n"; }
  208. sub usageerr { print(STDERR "$progname: @_\n\n"); &usageversion; exit(2); }
  209. sub subprocerr {
  210. local ($p) = @_;
  211. if (WIFEXITED($?)) {
  212. die "$progname: failure: $p gave error exit status ".WEXITSTATUS($?)."\n";
  213. } elsif (WIFSIGNALED($?)) {
  214. die "$progname: failure: $p died from signal ".WTERMSIG($?)."\n";
  215. } else {
  216. die "$progname: failure: $p failed with unknown exit code $?\n";
  217. }
  218. }
  219. 1;