controllib.pl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h);
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. use Dpkg::ErrorHandling qw(warning error failure internerr syserr subprocerr);
  9. use Dpkg::Arch qw(get_host_arch debarch_is);
  10. use Dpkg::Fields qw(capit sort_field_by_importance);
  11. textdomain("dpkg-dev");
  12. our $sourcepackage; # - name of sourcepackage
  13. our %f; # - fields ???
  14. our %fi; # - map of fields values. keys are of the form "S# key"
  15. # where S is source (L is changelog, C is control)
  16. # and # is an index
  17. our %p2i; # - map from datafile+packagename to index in controlfile
  18. # (used if multiple packages can be listed). Key is
  19. # "S key" where S is the source and key is the packagename
  20. my $maxsubsts = 50;
  21. our %substvar; # - map with substitution variables
  22. my $parsechangelog = 'dpkg-parsechangelog';
  23. sub getfowner
  24. {
  25. my $getlogin = getlogin();
  26. if (!defined($getlogin)) {
  27. open(SAVEIN, "<&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. open(STDIN, "<&STDOUT");
  37. $getlogin = getlogin();
  38. close(STDIN);
  39. open(STDIN, "<&SAVEIN");
  40. close(SAVEIN);
  41. }
  42. my @fowner;
  43. if (defined($ENV{'LOGNAME'})) {
  44. @fowner = getpwnam($ENV{'LOGNAME'});
  45. if (!@fowner) {
  46. die(sprintf(_g('unable to get login information for username "%s"'), $ENV{'LOGNAME'}));
  47. }
  48. } elsif (defined($getlogin)) {
  49. @fowner = getpwnam($getlogin);
  50. if (!@fowner) {
  51. die(sprintf(_g('unable to get login information for username "%s"'), $getlogin));
  52. }
  53. } else {
  54. warning(_g('no utmp entry available and LOGNAME not defined; ' .
  55. 'using uid of process (%d)'), $<);
  56. @fowner = getpwuid($<);
  57. if (!@fowner) {
  58. die (sprintf(_g('unable to get login information for uid %d'), $<));
  59. }
  60. }
  61. @fowner = @fowner[2,3];
  62. return @fowner;
  63. }
  64. sub substvars {
  65. my ($v) = @_;
  66. my $lhs;
  67. my $vn;
  68. my $rhs = '';
  69. my $count = 0;
  70. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  71. # If we have consumed more from the leftover data, then
  72. # reset the recursive counter.
  73. $count= 0 if (length($POSTMATCH) < length($rhs));
  74. $count < $maxsubsts ||
  75. error(_g("too many substitutions - recursive ? - in \`%s'"), $v);
  76. $lhs=$`; $vn=$1; $rhs=$';
  77. if (defined($substvar{$vn})) {
  78. $v= $lhs.$substvar{$vn}.$rhs;
  79. $count++;
  80. } else {
  81. warning(_g("unknown substitution variable \${%s}"), $vn);
  82. $v= $lhs.$rhs;
  83. }
  84. }
  85. return $v;
  86. }
  87. sub outputclose {
  88. my ($varlistfile) = @_;
  89. for my $f (keys %f) {
  90. $substvar{"F:$f"} = $f{$f};
  91. }
  92. &parsesubstvars($varlistfile) if (defined($varlistfile));
  93. for my $f (sort sort_field_by_importance keys %f) {
  94. my $v = $f{$f};
  95. if (defined($varlistfile)) {
  96. $v= &substvars($v);
  97. }
  98. $v =~ m/\S/ || next; # delete whitespace-only fields
  99. $v =~ m/\n\S/ &&
  100. internerr(_g("field %s has newline then non whitespace >%s<"),
  101. $f, $v);
  102. $v =~ m/\n[ \t]*\n/ &&
  103. internerr(_g("field %s has blank lines >%s<"), $f, $v);
  104. $v =~ m/\n$/ &&
  105. internerr(_g("field %s has trailing newline >%s<"), $f, $v);
  106. if (defined($varlistfile)) {
  107. $v =~ s/,[\s,]*,/,/g;
  108. $v =~ s/^\s*,\s*//;
  109. $v =~ s/\s*,\s*$//;
  110. }
  111. $v =~ s/\$\{\}/\$/g;
  112. print("$f: $v\n") || &syserr(_g("write error on control data"));
  113. }
  114. close(STDOUT) || &syserr(_g("write error on close control data"));
  115. }
  116. sub parsecontrolfile {
  117. my $controlfile = shift;
  118. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  119. open(CDATA, "< $controlfile") ||
  120. error(_g("cannot read control file %s: %s"), $controlfile, $!);
  121. binmode(CDATA);
  122. my $indices = parsecdata(\*CDATA, 'C', 1,
  123. sprintf(_g("control file %s"), $controlfile));
  124. $indices >= 2 || &error(_g("control file must have at least one binary package part"));
  125. for (my $i = 1; $i < $indices; $i++) {
  126. defined($fi{"C$i Package"}) ||
  127. error(_g("per-package paragraph %d in control " .
  128. "info file is missing Package line"), $i);
  129. }
  130. defined($fi{"C Source"}) ||
  131. &error(_g("source paragraph in control info file is ".
  132. "missing Source line"));
  133. }
  134. my $substvarsparsed = 0;
  135. sub parsesubstvars {
  136. my $varlistfile = shift;
  137. if (length($varlistfile) && !$substvarsparsed) {
  138. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  139. if (open(SV,"< $varlistfile")) {
  140. binmode(SV);
  141. while (<SV>) {
  142. next if m/^\#/ || !m/\S/;
  143. s/\s*\n$//;
  144. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  145. error(_g("bad line in substvars file %s at line %d"),
  146. $varlistfile, $.);
  147. $substvar{$1}= $';
  148. }
  149. close(SV);
  150. } elsif ($! != ENOENT ) {
  151. error(_g("unable to open substvars file %s: %s"), $varlistfile, $!);
  152. }
  153. $substvarsparsed = 1;
  154. }
  155. }
  156. sub parsechangelog {
  157. my ($changelogfile, $changelogformat, $since) = @_;
  158. defined(my $c = open(CDATA, "-|")) || syserr(_g("fork for parse changelog"));
  159. if ($c) {
  160. binmode(CDATA);
  161. parsecdata(\*CDATA, 'L', 0, _g("parsed version of changelog"));
  162. close(CDATA);
  163. $? && subprocerr(_g("parse changelog"));
  164. } else {
  165. binmode(STDOUT);
  166. my @al = ($parsechangelog);
  167. push(@al,"-l$changelogfile");
  168. push(@al, "-F$changelogformat") if defined($changelogformat);
  169. push(@al, "-v$since") if defined($since);
  170. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  171. }
  172. }
  173. sub init_substvars
  174. {
  175. $substvar{'Format'} = 1.7;
  176. $substvar{'Newline'} = "\n";
  177. $substvar{'Space'} = " ";
  178. $substvar{'Tab'} = "\t";
  179. # XXX: Source-Version is now deprecated, remove in the future.
  180. $substvar{'Source-Version'}= $fi{"L Version"};
  181. $substvar{'binary:Version'} = $fi{"L Version"};
  182. $substvar{'source:Version'} = $fi{"L Version"};
  183. $substvar{'source:Version'} =~ s/\+b[0-9]+$//;
  184. $substvar{'source:Upstream-Version'} = $fi{"L Version"};
  185. $substvar{'source:Upstream-Version'} =~ s/-[^-]*$//;
  186. $substvar{"dpkg:Version"} = $version;
  187. $substvar{"dpkg:Upstream-Version"} = $version;
  188. $substvar{"dpkg:Upstream-Version"} =~ s/-[^-]+$//;
  189. }
  190. sub init_substvar_arch()
  191. {
  192. $substvar{'Arch'} = get_host_arch();
  193. }
  194. sub checkpackagename {
  195. my $name = shift || '';
  196. $name =~ m/[^-+.0-9a-z]/o &&
  197. error(_g("source package name `%s' contains illegal character `%s'"),
  198. $name, $&);
  199. $name =~ m/^[0-9a-z]/o ||
  200. error(_g("source package name `%s' starts with non-alphanum"), $name);
  201. }
  202. sub checkversion {
  203. my $version = shift || '';
  204. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  205. error(_g("version number contains illegal character `%s'"), $&);
  206. }
  207. sub setsourcepackage {
  208. my $v = shift;
  209. checkpackagename( $v );
  210. if (defined($sourcepackage)) {
  211. $v eq $sourcepackage ||
  212. error(_g("source package has two conflicting values - %s and %s"),
  213. $sourcepackage, $v);
  214. } else {
  215. $sourcepackage= $v;
  216. }
  217. }
  218. sub readmd5sum {
  219. (my $md5sum = shift) or return;
  220. $md5sum =~ s/^([0-9a-f]{32})\s*\*?-?\s*\n?$/$1/o
  221. || failure(_g("md5sum gave bogus output `%s'"), $md5sum);
  222. return $md5sum;
  223. }
  224. # XXX: Should not be a global!!
  225. my $whatmsg;
  226. sub parsecdata {
  227. my ($cdata, $source, $many);
  228. ($cdata, $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. my $index = '';
  233. my $cf = '';
  234. my $paraborder = 1;
  235. while (<$cdata>) {
  236. s/\s*\n$//;
  237. next if (m/^$/ and $paraborder);
  238. next if (m/^#/);
  239. $paraborder=0;
  240. if (m/^(\S+?)\s*:\s*(.*)$/) {
  241. $cf = $1;
  242. my $v = $2;
  243. $cf= &capit($cf);
  244. $fi{"$source$index $cf"}= $v;
  245. $fi{"o:$source$index $cf"}= $1;
  246. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  247. } elsif (m/^\s+\S/) {
  248. length($cf) || &syntax(_g("continued value line not in field"));
  249. $fi{"$source$index $cf"}.= "\n$_";
  250. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  251. $many == -2 && syntax(_g("expected blank line before PGP signature"));
  252. while (<$cdata>) {
  253. last if m/^$/;
  254. }
  255. $many= -2;
  256. } elsif (m/^$/) {
  257. $paraborder = 1;
  258. if ($many>0) {
  259. $index++; $cf='';
  260. } elsif ($many == -2) {
  261. $_ = <$cdata> while defined($_) && $_ =~ /^\s*$/;
  262. length($_) ||
  263. &syntax(_g("expected PGP signature, found EOF after blank line"));
  264. s/\n$//;
  265. m/^-----BEGIN PGP/ ||
  266. &syntax(sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  267. $many= -3; last;
  268. } else {
  269. while (<$cdata>) {
  270. /^\s*$/ ||
  271. &syntax(_g("found several \`paragraphs' where only one expected"));
  272. }
  273. }
  274. } else {
  275. &syntax(_g("line with unknown format (not field-colon-value)"));
  276. }
  277. }
  278. $many == -2 && &syntax(_g("found start of PGP body but no signature"));
  279. if (length($cf)) { $index++; }
  280. $index || &syntax(_g("empty file"));
  281. return $index;
  282. }
  283. sub syntax {
  284. error(_g("syntax error in %s at line %d: %s"), $whatmsg, $., $_[0]);
  285. }
  286. 1;