controllib.pl 7.7 KB

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