controllib.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h);
  6. our $dpkglibdir;
  7. our $pkgdatadir;
  8. push(@INC,$dpkglibdir);
  9. require 'dpkg-gettext.pl';
  10. textdomain("dpkg-dev");
  11. our $sourcepackage; # - name of sourcepackage
  12. our %f; # - fields ???
  13. our %fi; # - map of fields values. keys are of the form "S# key"
  14. # where S is source (L is changelog, C is control)
  15. # and # is an index
  16. our %fieldimps;
  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. our @pkg_dep_fields = qw(Pre-Depends Depends Recommends Suggests Enhances
  24. Conflicts Replaces Provides);
  25. our @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  26. Build-Conflicts Build-Conflicts-Indep);
  27. our $warnable_error = 1;
  28. our $quiet_warnings = 0;
  29. our $version;
  30. our $progname = $0;
  31. $progname = $& if $progname =~ m,[^/]+$,;
  32. sub getfowner
  33. {
  34. my $getlogin = getlogin();
  35. if (!defined($getlogin)) {
  36. open(SAVEIN, "<&STDIN");
  37. open(STDIN, "<&STDERR");
  38. $getlogin = getlogin();
  39. close(STDIN);
  40. open(STDIN, "<&SAVEIN");
  41. close(SAVEIN);
  42. }
  43. if (!defined($getlogin)) {
  44. open(SAVEIN, "<&STDIN");
  45. open(STDIN, "<&STDOUT");
  46. $getlogin = getlogin();
  47. close(STDIN);
  48. open(STDIN, "<&SAVEIN");
  49. close(SAVEIN);
  50. }
  51. my @fowner;
  52. if (defined($ENV{'LOGNAME'})) {
  53. @fowner = getpwnam($ENV{'LOGNAME'});
  54. if (!@fowner) {
  55. die(sprintf(_g('unable to get login information for username "%s"'), $ENV{'LOGNAME'}));
  56. }
  57. } elsif (defined($getlogin)) {
  58. @fowner = getpwnam($getlogin);
  59. if (!@fowner) {
  60. die(sprintf(_g('unable to get login information for username "%s"'), $getlogin));
  61. }
  62. } else {
  63. warning(sprintf(_g('no utmp entry available and LOGNAME not defined; using uid of process (%d)'), $<));
  64. @fowner = getpwuid($<);
  65. if (!@fowner) {
  66. die (sprintf(_g('unable to get login information for uid %d'), $<));
  67. }
  68. }
  69. @fowner = @fowner[2,3];
  70. return @fowner;
  71. }
  72. sub capit {
  73. my @pieces = map { ucfirst(lc) } split /-/, $_[0];
  74. return join '-', @pieces;
  75. }
  76. #
  77. # Architecture library
  78. #
  79. my (@cpu, @os);
  80. my (%cputable, %ostable);
  81. my (%cputable_re, %ostable_re);
  82. my %debtriplet_to_debarch;
  83. my %debarch_to_debtriplet;
  84. {
  85. my $host_arch;
  86. sub get_host_arch()
  87. {
  88. return $host_arch if defined $host_arch;
  89. $host_arch = `dpkg-architecture -qDEB_HOST_ARCH`;
  90. $? && subprocerr("dpkg-architecture -qDEB_HOST_ARCH");
  91. chomp $host_arch;
  92. return $host_arch;
  93. }
  94. }
  95. sub get_valid_arches()
  96. {
  97. read_cputable() if (!@cpu);
  98. read_ostable() if (!@os);
  99. foreach my $os (@os) {
  100. foreach my $cpu (@cpu) {
  101. my $arch = debtriplet_to_debarch(split(/-/, $os, 2), $cpu);
  102. print $arch."\n" if defined($arch);
  103. }
  104. }
  105. }
  106. sub read_cputable
  107. {
  108. local $_;
  109. open CPUTABLE, "$pkgdatadir/cputable"
  110. or syserr(_g("unable to open cputable"));
  111. while (<CPUTABLE>) {
  112. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  113. $cputable{$1} = $2;
  114. $cputable_re{$1} = $3;
  115. push @cpu, $1;
  116. }
  117. }
  118. close CPUTABLE;
  119. }
  120. sub read_ostable
  121. {
  122. local $_;
  123. open OSTABLE, "$pkgdatadir/ostable"
  124. or syserr(_g("unable to open ostable"));
  125. while (<OSTABLE>) {
  126. if (m/^(?!\#)(\S+)\s+(\S+)\s+(\S+)/) {
  127. $ostable{$1} = $2;
  128. $ostable_re{$1} = $3;
  129. push @os, $1;
  130. }
  131. }
  132. close OSTABLE;
  133. }
  134. sub read_triplettable()
  135. {
  136. read_cputable() if (!@cpu);
  137. local $_;
  138. open TRIPLETTABLE, "$pkgdatadir/triplettable"
  139. or syserr(_g("unable to open triplettable"));
  140. while (<TRIPLETTABLE>) {
  141. if (m/^(?!\#)(\S+)\s+(\S+)/) {
  142. my $debtriplet = $1;
  143. my $debarch = $2;
  144. if ($debtriplet =~ /<cpu>/) {
  145. foreach my $_cpu (@cpu) {
  146. (my $dt = $debtriplet) =~ s/<cpu>/$_cpu/;
  147. (my $da = $debarch) =~ s/<cpu>/$_cpu/;
  148. $debarch_to_debtriplet{$da} = $dt;
  149. $debtriplet_to_debarch{$dt} = $da;
  150. }
  151. } else {
  152. $debarch_to_debtriplet{$2} = $1;
  153. $debtriplet_to_debarch{$1} = $2;
  154. }
  155. }
  156. }
  157. close TRIPLETTABLE;
  158. }
  159. sub debtriplet_to_gnutriplet(@)
  160. {
  161. read_cputable() if (!@cpu);
  162. read_ostable() if (!@os);
  163. my ($abi, $os, $cpu) = @_;
  164. return undef unless defined($abi) && defined($os) && defined($cpu) &&
  165. exists($cputable{$cpu}) && exists($ostable{"$abi-$os"});
  166. return join("-", $cputable{$cpu}, $ostable{"$abi-$os"});
  167. }
  168. sub gnutriplet_to_debtriplet($)
  169. {
  170. my ($gnu) = @_;
  171. return undef unless defined($gnu);
  172. my ($gnu_cpu, $gnu_os) = split(/-/, $gnu, 2);
  173. return undef unless defined($gnu_cpu) && defined($gnu_os);
  174. read_cputable() if (!@cpu);
  175. read_ostable() if (!@os);
  176. my ($os, $cpu);
  177. foreach my $_cpu (@cpu) {
  178. if ($gnu_cpu =~ /^$cputable_re{$_cpu}$/) {
  179. $cpu = $_cpu;
  180. last;
  181. }
  182. }
  183. foreach my $_os (@os) {
  184. if ($gnu_os =~ /^(.*-)?$ostable_re{$_os}$/) {
  185. $os = $_os;
  186. last;
  187. }
  188. }
  189. return undef if !defined($cpu) || !defined($os);
  190. return (split(/-/, $os, 2), $cpu);
  191. }
  192. sub debtriplet_to_debarch(@)
  193. {
  194. read_triplettable() if (!%debtriplet_to_debarch);
  195. my ($abi, $os, $cpu) = @_;
  196. if (!defined($abi) || !defined($os) || !defined($cpu)) {
  197. return undef;
  198. } elsif (exists $debtriplet_to_debarch{"$abi-$os-$cpu"}) {
  199. return $debtriplet_to_debarch{"$abi-$os-$cpu"};
  200. } else {
  201. return undef;
  202. }
  203. }
  204. sub debarch_to_debtriplet($)
  205. {
  206. read_triplettable() if (!%debarch_to_debtriplet);
  207. local ($_) = @_;
  208. my $arch;
  209. # FIXME: 'any' is handled here, to be able to do debarch_eq('any', foo).
  210. if (/^any$/ || /^all$/) {
  211. return ($_, $_, $_);
  212. } elsif (/^linux-([^-]*)/) {
  213. # XXX: Might disappear in the future, not sure yet.
  214. $arch = $1;
  215. } else {
  216. $arch = $_;
  217. }
  218. my $triplet = $debarch_to_debtriplet{$arch};
  219. if (defined($triplet)) {
  220. return split('-', $triplet, 3);
  221. } else {
  222. return undef;
  223. }
  224. }
  225. sub debwildcard_to_debtriplet($)
  226. {
  227. local ($_) = @_;
  228. if (/any/) {
  229. if (/^([^-]*)-([^-]*)-(.*)/) {
  230. return ($1, $2, $3);
  231. } elsif (/^([^-]*)-([^-]*)$/) {
  232. return ('any', $1, $2);
  233. } else {
  234. return ($_, $_, $_);
  235. }
  236. } else {
  237. return debarch_to_debtriplet($_);
  238. }
  239. }
  240. sub debarch_eq($$)
  241. {
  242. my ($a, $b) = @_;
  243. my @a = debarch_to_debtriplet($a);
  244. my @b = debarch_to_debtriplet($b);
  245. return 0 if grep(!defined, (@a, @b));
  246. return ($a[0] eq $b[0] && $a[1] eq $b[1] && $a[2] eq $b[2]);
  247. }
  248. sub debarch_is($$)
  249. {
  250. my ($real, $alias) = @_;
  251. my @real = debarch_to_debtriplet($real);
  252. my @alias = debwildcard_to_debtriplet($alias);
  253. return 0 if grep(!defined, (@real, @alias));
  254. if (($alias[0] eq $real[0] || $alias[0] eq 'any') &&
  255. ($alias[1] eq $real[1] || $alias[1] eq 'any') &&
  256. ($alias[2] eq $real[2] || $alias[2] eq 'any')) {
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. sub substvars {
  262. my ($v) = @_;
  263. my $lhs;
  264. my $vn;
  265. my $rhs = '';
  266. my $count = 0;
  267. while ($v =~ m/\$\{([-:0-9a-z]+)\}/i) {
  268. # If we have consumed more from the leftover data, then
  269. # reset the recursive counter.
  270. $count= 0 if (length($POSTMATCH) < length($rhs));
  271. $count < $maxsubsts ||
  272. &error(sprintf(_g("too many substitutions - recursive ? - in \`%s'"), $v));
  273. $lhs=$`; $vn=$1; $rhs=$';
  274. if (defined($substvar{$vn})) {
  275. $v= $lhs.$substvar{$vn}.$rhs;
  276. $count++;
  277. } else {
  278. warning(sprintf(_g("unknown substitution variable \${%s}"), $vn));
  279. $v= $lhs.$rhs;
  280. }
  281. }
  282. return $v;
  283. }
  284. sub set_field_importance(@)
  285. {
  286. my @fields = @_;
  287. my $i = 1;
  288. grep($fieldimps{$_} = $i++, @fields);
  289. }
  290. sub sort_field_by_importance($$)
  291. {
  292. my ($a, $b) = @_;
  293. if (defined $fieldimps{$a} && defined $fieldimps{$b}) {
  294. $fieldimps{$a} <=> $fieldimps{$b};
  295. } elsif (defined($fieldimps{$a})) {
  296. -1;
  297. } elsif (defined($fieldimps{$b})) {
  298. 1;
  299. } else {
  300. $a cmp $b;
  301. }
  302. }
  303. sub outputclose {
  304. my ($varlistfile) = @_;
  305. for my $f (keys %f) {
  306. $substvar{"F:$f"} = $f{$f};
  307. }
  308. &parsesubstvars($varlistfile) if (defined($varlistfile));
  309. for my $f (sort sort_field_by_importance keys %f) {
  310. my $v = $f{$f};
  311. if (defined($varlistfile)) {
  312. $v= &substvars($v);
  313. }
  314. $v =~ m/\S/ || next; # delete whitespace-only fields
  315. $v =~ m/\n\S/ && &internerr(sprintf(_g("field %s has newline then non whitespace >%s<"), $f, $v));
  316. $v =~ m/\n[ \t]*\n/ && &internerr(sprintf(_g("field %s has blank lines >%s<"), $f, $v));
  317. $v =~ m/\n$/ && &internerr(sprintf(_g("field %s has trailing newline >%s<"), $f, $v));
  318. if (defined($varlistfile)) {
  319. $v =~ s/,[\s,]*,/,/g;
  320. $v =~ s/^\s*,\s*//;
  321. $v =~ s/\s*,\s*$//;
  322. }
  323. $v =~ s/\$\{\}/\$/g;
  324. print("$f: $v\n") || &syserr(_g("write error on control data"));
  325. }
  326. close(STDOUT) || &syserr(_g("write error on close control data"));
  327. }
  328. sub parsecontrolfile {
  329. my $controlfile = shift;
  330. $controlfile="./$controlfile" if $controlfile =~ m/^\s/;
  331. open(CDATA,"< $controlfile") || &error(sprintf(_g("cannot read control file %s: %s"), $controlfile, $!));
  332. binmode(CDATA);
  333. my $indices = parsecdata(\*CDATA, 'C', 1,
  334. sprintf(_g("control file %s"), $controlfile));
  335. $indices >= 2 || &error(_g("control file must have at least one binary package part"));
  336. for (my $i = 1; $i < $indices; $i++) {
  337. defined($fi{"C$i Package"}) ||
  338. &error(sprintf(_g("per-package paragraph %d in control ".
  339. "info file is missing Package line"),
  340. $i));
  341. }
  342. defined($fi{"C Source"}) ||
  343. &error(_g("source paragraph in control info file is ".
  344. "missing Source line"));
  345. }
  346. my $substvarsparsed = 0;
  347. sub parsesubstvars {
  348. my $varlistfile = shift;
  349. if (length($varlistfile) && !$substvarsparsed) {
  350. $varlistfile="./$varlistfile" if $varlistfile =~ m/\s/;
  351. if (open(SV,"< $varlistfile")) {
  352. binmode(SV);
  353. while (<SV>) {
  354. next if m/^\#/ || !m/\S/;
  355. s/\s*\n$//;
  356. m/^(\w[-:0-9A-Za-z]*)\=/ ||
  357. &error(sprintf(_g("bad line in substvars file %s at line %d"),
  358. $varlistfile, $.));
  359. $substvar{$1}= $';
  360. }
  361. close(SV);
  362. } elsif ($! != ENOENT ) {
  363. &error(sprintf(_g("unable to open substvars file %s: %s"),
  364. $varlistfile, $!));
  365. }
  366. $substvarsparsed = 1;
  367. }
  368. }
  369. sub parsedep {
  370. my ($dep_line, $use_arch, $reduce_arch) = @_;
  371. my @dep_list;
  372. my $host_arch = get_host_arch();
  373. foreach my $dep_and (split(/,\s*/m, $dep_line)) {
  374. my @or_list = ();
  375. ALTERNATE:
  376. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  377. my ($package, $relation, $version);
  378. $package = $1 if ($dep_or =~ s/^([a-zA-Z0-9][a-zA-Z0-9+._-]*)\s*//m);
  379. ($relation, $version) = ($1, $2)
  380. if ($dep_or =~ s/^\(\s*(=|<=|>=|<<?|>>?)\s*([^)]+).*\)\s*//m);
  381. my @arches;
  382. @arches = split(/\s+/m, $1) if ($use_arch && $dep_or =~ s/^\[([^]]+)\]\s*//m);
  383. if ($reduce_arch && @arches) {
  384. my $seen_arch='';
  385. foreach my $arch (@arches) {
  386. $arch=lc($arch);
  387. if ($arch =~ /^!/) {
  388. my $not_arch;
  389. ($not_arch = $arch) =~ s/^!//;
  390. if (debarch_is($host_arch, $not_arch)) {
  391. next ALTERNATE;
  392. } else {
  393. # This is equivilant to
  394. # having seen the current arch,
  395. # unless the current arch
  396. # is also listed..
  397. $seen_arch=1;
  398. }
  399. } elsif (debarch_is($host_arch, $arch)) {
  400. $seen_arch=1;
  401. next;
  402. }
  403. }
  404. if (! $seen_arch) {
  405. next;
  406. }
  407. }
  408. if (length($dep_or)) {
  409. warning(sprintf(_g("can't parse dependency %s"), $dep_and));
  410. return undef;
  411. }
  412. push @or_list, [ $package, $relation, $version, \@arches ];
  413. }
  414. push @dep_list, \@or_list;
  415. }
  416. \@dep_list;
  417. }
  418. sub showdep {
  419. my ($dep_list, $show_arch) = @_;
  420. my @and_list;
  421. foreach my $dep_and (@$dep_list) {
  422. my @or_list = ();
  423. foreach my $dep_or (@$dep_and) {
  424. my ($package, $relation, $version, $arch_list) = @$dep_or;
  425. push @or_list, $package . ($relation && $version ? " ($relation $version)" : '') . ($show_arch && @$arch_list ? " [@$arch_list]" : '');
  426. }
  427. push @and_list, join(' | ', @or_list);
  428. }
  429. join(', ', @and_list);
  430. }
  431. sub parsechangelog {
  432. my ($changelogfile, $changelogformat, $since) = @_;
  433. defined(my $c = open(CDATA, "-|")) || syserr(_g("fork for parse changelog"));
  434. if ($c) {
  435. binmode(CDATA);
  436. parsecdata(\*CDATA, 'L', 0, _g("parsed version of changelog"));
  437. close(CDATA);
  438. $? && subprocerr(_g("parse changelog"));
  439. } else {
  440. binmode(STDOUT);
  441. my @al = ($parsechangelog);
  442. push(@al,"-l$changelogfile");
  443. push(@al, "-F$changelogformat") if defined($changelogformat);
  444. push(@al, "-v$since") if defined($since);
  445. exec(@al) || &syserr("exec parsechangelog $parsechangelog");
  446. }
  447. }
  448. sub init_substvars
  449. {
  450. $substvar{'Format'} = 1.7;
  451. $substvar{'Newline'} = "\n";
  452. $substvar{'Space'} = " ";
  453. $substvar{'Tab'} = "\t";
  454. # XXX: Source-Version is now deprecated, remove in the future.
  455. $substvar{'Source-Version'}= $fi{"L Version"};
  456. $substvar{'binary:Version'} = $fi{"L Version"};
  457. $substvar{'source:Version'} = $fi{"L Version"};
  458. $substvar{'source:Version'} =~ s/\+b[0-9]+$//;
  459. $substvar{'source:Upstream-Version'} = $fi{"L Version"};
  460. $substvar{'source:Upstream-Version'} =~ s/-[^-]*$//;
  461. # FIXME: this needs all progs using controllib to set $version as 'our'.
  462. # We expect the calling program to set $version.
  463. $substvar{"dpkg:Version"} = $version;
  464. $substvar{"dpkg:Upstream-Version"} = $version;
  465. $substvar{"dpkg:Upstream-Version"} =~ s/-[^-]+$//;
  466. }
  467. sub init_substvar_arch()
  468. {
  469. $substvar{'Arch'} = get_host_arch();
  470. }
  471. sub checkpackagename {
  472. my $name = shift || '';
  473. $name =~ m/[^-+.0-9a-z]/o &&
  474. &error(sprintf(_g("source package name `%s' contains illegal character `%s'"), $name, $&));
  475. $name =~ m/^[0-9a-z]/o ||
  476. &error(sprintf(_g("source package name `%s' starts with non-alphanum"), $name));
  477. }
  478. sub checkversion {
  479. my $version = shift || '';
  480. $version =~ m/[^-+:.0-9a-zA-Z~]/o &&
  481. &error(sprintf(_g("version number contains illegal character `%s'"), $&));
  482. }
  483. sub setsourcepackage {
  484. my $v = shift;
  485. checkpackagename( $v );
  486. if (defined($sourcepackage)) {
  487. $v eq $sourcepackage ||
  488. &error(sprintf(_g("source package has two conflicting values - %s and %s"), $sourcepackage, $v));
  489. } else {
  490. $sourcepackage= $v;
  491. }
  492. }
  493. sub readmd5sum {
  494. (my $md5sum = shift) or return;
  495. $md5sum =~ s/^([0-9a-f]{32})\s*\*?-?\s*\n?$/$1/o
  496. || &failure(sprintf(_g("md5sum gave bogus output `%s'"), $md5sum));
  497. return $md5sum;
  498. }
  499. # XXX: Should not be a global!!
  500. my $whatmsg;
  501. sub parsecdata {
  502. my ($cdata, $source, $many);
  503. ($cdata, $source, $many, $whatmsg) = @_;
  504. # many=0: ordinary control data like output from dpkg-parsechangelog
  505. # many=1: many paragraphs like in source control file
  506. # many=-1: single paragraph of control data optionally signed
  507. my $index = '';
  508. my $cf = '';
  509. my $paraborder = 1;
  510. while (<$cdata>) {
  511. s/\s*\n$//;
  512. next if (m/^$/ and $paraborder);
  513. next if (m/^#/);
  514. $paraborder=0;
  515. if (m/^(\S+)\s*:\s*(.*)$/) {
  516. $cf = $1;
  517. my $v = $2;
  518. $cf= &capit($cf);
  519. $fi{"$source$index $cf"}= $v;
  520. $fi{"o:$source$index $cf"}= $1;
  521. if (lc $cf eq 'package') { $p2i{"$source $v"}= $index; }
  522. } elsif (m/^\s+\S/) {
  523. length($cf) || &syntax(_g("continued value line not in field"));
  524. $fi{"$source$index $cf"}.= "\n$_";
  525. } elsif (m/^-----BEGIN PGP/ && $many<0) {
  526. $many == -2 && syntax(_g("expected blank line before PGP signature"));
  527. while (<$cdata>) {
  528. last if m/^$/;
  529. }
  530. $many= -2;
  531. } elsif (m/^$/) {
  532. $paraborder = 1;
  533. if ($many>0) {
  534. $index++; $cf='';
  535. } elsif ($many == -2) {
  536. $_ = <$cdata> while defined($_) && $_ =~ /^\s*$/;
  537. length($_) ||
  538. &syntax(_g("expected PGP signature, found EOF after blank line"));
  539. s/\n$//;
  540. m/^-----BEGIN PGP/ ||
  541. &syntax(sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  542. $many= -3; last;
  543. } else {
  544. while (<$cdata>) {
  545. /^\s*$/ ||
  546. &syntax(_g("found several \`paragraphs' where only one expected"));
  547. }
  548. }
  549. } else {
  550. &syntax(_g("line with unknown format (not field-colon-value)"));
  551. }
  552. }
  553. $many == -2 && &syntax(_g("found start of PGP body but no signature"));
  554. if (length($cf)) { $index++; }
  555. $index || &syntax(_g("empty file"));
  556. return $index;
  557. }
  558. sub unknown {
  559. my $field = $_;
  560. warning(sprintf(_g("unknown information field '%s' in input data in %s"),
  561. $field, $_[0]));
  562. }
  563. sub syntax {
  564. &error(sprintf(_g("syntax error in %s at line %d: %s"), $whatmsg, $., $_[0]));
  565. }
  566. sub failure { die sprintf(_g("%s: failure: %s"), $progname, $_[0])."\n"; }
  567. sub syserr { die sprintf(_g("%s: failure: %s: %s"), $progname, $_[0], $!)."\n"; }
  568. sub error { die sprintf(_g("%s: error: %s"), $progname, $_[0])."\n"; }
  569. sub internerr { die sprintf(_g("%s: internal error: %s"), $progname, $_[0])."\n"; }
  570. sub warning
  571. {
  572. if (!$quiet_warnings) {
  573. warn sprintf(_g("%s: warning: %s"), $progname, $_[0])."\n";
  574. }
  575. }
  576. sub usageerr
  577. {
  578. printf(STDERR "%s: %s\n\n", $progname, "@_");
  579. &usage;
  580. exit(2);
  581. }
  582. sub warnerror
  583. {
  584. if ($warnable_error) {
  585. warning(@_);
  586. } else {
  587. error(@_);
  588. }
  589. }
  590. sub subprocerr {
  591. my ($p) = @_;
  592. require POSIX;
  593. if (POSIX::WIFEXITED($?)) {
  594. die sprintf(_g("%s: failure: %s gave error exit status %s"),
  595. $progname, $p, POSIX::WEXITSTATUS($?))."\n";
  596. } elsif (POSIX::WIFSIGNALED($?)) {
  597. die sprintf(_g("%s: failure: %s died from signal %s"),
  598. $progname, $p, POSIX::WTERMSIG($?))."\n";
  599. } else {
  600. die sprintf(_g("%s: failure: %s failed with unknown exit code %d"),
  601. $progname, $p, $?)."\n";
  602. }
  603. }
  604. 1;