Pārlūkot izejas kodu

Dpkg::ErrorHandling: Move syntaxerr to Dpkg::Control::HashCore::parse_error

This places the function where it belongs.
Guillem Jover 13 gadi atpakaļ
vecāks
revīzija
9a1e6db62b

+ 31 - 12
scripts/Dpkg/Control/HashCore.pm

@@ -18,7 +18,7 @@ package Dpkg::Control::HashCore;
 use strict;
 use warnings;
 
-our $VERSION = '1.00';
+our $VERSION = '1.01';
 
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
@@ -157,6 +157,19 @@ sub get_option {
 Parse the content of $file. Exits in case of errors. Returns true if some
 fields have been parsed.
 
+=item $c->parse_error($file, $fmt, ...)
+
+Prints an error message and dies on syntax parse errors.
+
+=cut
+
+sub parse_error {
+    my ($self, $file, $msg) = (shift, shift, shift);
+
+    $msg = sprintf($msg, @_) if (@_);
+    error(_g('syntax error in %s at line %d: %s'), $file, $., $msg);
+}
+
 =item $c->parse($fh, $description)
 
 Parse a control file from the given filehandle. Exits in case of errors.
@@ -183,7 +196,7 @@ sub parse {
 	    $parabody = 1;
 	    if (exists $self->{$1}) {
 		unless ($$self->{allow_duplicate}) {
-		    syntaxerr($desc, sprintf(_g('duplicate field %s found'), $1));
+		    $self->parse_error($desc, _g('duplicate field %s found'), $1);
 		}
 	    }
 	    $self->{$1} = $2;
@@ -191,7 +204,7 @@ sub parse {
 	} elsif (m/^\s(\s*\S.*)$/) {
 	    my $line = $1;
 	    unless (defined($cf)) {
-                syntaxerr($desc, _g('continued value line not in field'));
+		$self->parse_error($desc, _g('continued value line not in field'));
             }
 	    if ($line =~ /^\.+$/) {
 		$line = substr $line, 1;
@@ -205,19 +218,19 @@ sub parse {
 		    last if m/^\s*$/;
 		}
 	    } else {
-		syntaxerr($desc, _g('PGP signature not allowed here'));
+		$self->parse_error($desc, _g('PGP signature not allowed here'));
 	    }
 	} elsif (m/^$/ || ($expect_pgp_sig && m/^-----BEGIN PGP SIGNATURE-----$/)) {
 	    if ($expect_pgp_sig) {
 		# Skip empty lines
 		$_ = <$fh> while defined($_) && $_ =~ /^\s*$/;
 		length($_) ||
-                    syntaxerr($desc, _g('expected PGP signature, found EOF ' .
-                                        'after blank line'));
+		    $self->parse_error($desc, _g('expected PGP signature, ' .
+		                                 'found EOF after blank line'));
 		s/\s*\n$//;
 		unless (m/^-----BEGIN PGP SIGNATURE-----$/) {
-		    syntaxerr($desc, sprintf(_g('expected PGP signature, ' .
-                                                "found something else \`%s'"), $_));
+		    $self->parse_error($desc, _g('expected PGP signature, ' .
+		                                 "found something else \`%s'"), $_);
                 }
 		# Skip PGP signature
 		while (<$fh>) {
@@ -225,7 +238,7 @@ sub parse {
 		    last if m/^-----END PGP SIGNATURE-----$/;
 		}
 		unless (defined($_)) {
-                    syntaxerr($desc, _g('unfinished PGP signature'));
+		    $self->parse_error($desc, _g('unfinished PGP signature'));
                 }
 		# This does not mean the signature is correct, that needs to
 		# be verified by gnupg.
@@ -233,13 +246,13 @@ sub parse {
 	    }
 	    last; # Finished parsing one block
 	} else {
-	    syntaxerr($desc,
-                      _g('line with unknown format (not field-colon-value)'));
+	    $self->parse_error($desc,
+	                       _g('line with unknown format (not field-colon-value)'));
 	}
     }
 
     if ($expect_pgp_sig and not $$self->{is_pgp_signed}) {
-        syntaxerr($desc, _g('unfinished PGP signature'));
+	$self->parse_error($desc, _g('unfinished PGP signature'));
     }
 
     return defined($cf);
@@ -503,6 +516,12 @@ sub NEXTKEY {
 
 =back
 
+=head1 CHANGES
+
+=head2 Version 1.01
+
+New method: parse_error().
+
 =head1 AUTHOR
 
 Raphaël Hertzog <hertzog@debian.org>.

+ 5 - 3
scripts/Dpkg/Control/Info.pm

@@ -100,17 +100,19 @@ sub parse {
     return if not $cdata->parse($fh, $desc);
     $self->{source} = $cdata;
     unless (exists $cdata->{Source}) {
-	syntaxerr($desc, _g('first block lacks a source field'));
+	$cdata->parse_error($desc, _g('first block lacks a source field'));
     }
     while (1) {
 	$cdata = Dpkg::Control->new(type => CTRL_INFO_PKG);
         last if not $cdata->parse($fh, $desc);
 	push @{$self->{packages}}, $cdata;
 	unless (exists $cdata->{Package}) {
-	    syntaxerr($desc, _g("block lacks the '%s' field"), 'Package');
+	    $cdata->parse_error($desc, _g("block lacks the '%s' field"),
+	                        'Package');
 	}
 	unless (exists $cdata->{Architecture}) {
-	    syntaxerr($desc, _g("block lacks the '%s' field"), 'Architecture');
+	    $cdata->parse_error($desc, _g("block lacks the '%s' field"),
+	                        'Architecture');
 	}
 
     }

+ 2 - 9
scripts/Dpkg/ErrorHandling.pm

@@ -16,14 +16,14 @@ package Dpkg::ErrorHandling;
 use strict;
 use warnings;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 use Dpkg ();
 use Dpkg::Gettext;
 
 use Exporter qw(import);
 our @EXPORT = qw(report_options info warning error errormsg
-                 syserr internerr subprocerr usageerr syntaxerr);
+                 syserr internerr subprocerr usageerr);
 our @EXPORT_OK = qw(report);
 
 my $quiet_warnings = 0;
@@ -109,11 +109,4 @@ sub usageerr(@)
     exit(2);
 }
 
-sub syntaxerr {
-    my ($file, $msg) = (shift, shift);
-
-    $msg = sprintf($msg, @_) if (@_);
-    error(_g('syntax error in %s at line %d: %s'), $file, $., $msg);
-}
-
 1;