]> mj.ucw.cz Git - libucw.git/blobdiff - lib/perl/Query.pm
One more deadly testcase.
[libucw.git] / lib / perl / Query.pm
index b39b5ac0d9abcdcbc38fad953d1c6c502f82a7e4..9a8b168e79978047138394ffbd537829a5ef6fc0 100644 (file)
@@ -1,6 +1,6 @@
 #      Perl module for sending queries to Sherlock search servers and parsing answers
 #
-#      (c) 2002 Martin Mares <mj@ucw.cz>
+#      (c) 2002--2003 Martin Mares <mj@ucw.cz>
 #
 #      This software may be freely distributed and used according to the terms
 #      of the GNU Lesser General Public License.
@@ -117,6 +117,7 @@ sub command($$) {
 
        # Status line
        my $stat = <$sock>;
+       $stat = "-903 Incomplete reply" if !defined $stat;
        chomp $stat;
        $stat =~ /^[+-]/ or return "-901 Reply parse error";
 
@@ -141,41 +142,26 @@ sub command($$) {
 }
 
 our $hdr_syntax = {
-       'D' => {
-               'D' => "",
+       '(D' => {
                'W' => [],
                'P' => [],
                'n' => [],
-               'T' => "",
-               '-' => "",
                '.' => [],
        },
        '.' => [],
-       '' => ""
 };
 
 our $card_syntax = {
-       'U' => {
-               'U' => "",
-               'D' => "",
-               'E' => "",
-               'L' => "",
-               'T' => "",
-               'c' => "",
-               's' => "",
+       '(U' => {
                'V' => [],
-               'b' => "",
-               'i' => "",
                'y' => [],
-               'z' => "",
+               'E' => [],
        },
        'M' => [],
        'X' => [],
-       '' => ""
 };
 
 our $footer_syntax = {
-       '' => ""
 };
 
 sub query($$) {
@@ -221,18 +207,19 @@ sub do_parse_tree($$$$) {
        my $syntax = shift @_;
 
        while ($i < @$raw) {
-               $raw->[$i] =~ /^(.)(.*)/;
-               if (!defined($syntax->{$1}) && !defined($syntax->{''})) { return $i; }
-               if (ref $syntax->{$1} eq "ARRAY") {
+               $raw->[$i] =~ /^([^(]|\(.)(.*)/;
+               if ($1 eq ")") {
+                       return $i;
+               } elsif (!defined($syntax->{$1})) {
+                       $cooked->{$1} = $2 if !defined($cooked->{$1});
+                       $i++;
+               } elsif (ref $syntax->{$1} eq "ARRAY") {
                        push @{$cooked->{$1}}, $2;
                        $i++;
                } elsif (ref $syntax->{$1} eq "HASH") {
                        my $block = {};
                        push @{$cooked->{$1}}, $block;
-                       $i = do_parse_tree($raw, $i, $block, $syntax->{$1});
-               } else {
-                       $cooked->{$1} = $2 if !defined($cooked->{$1});
-                       $i++;
+                       $i = do_parse_tree($raw, $i+1, $block, $syntax->{$1});
                }
        }
        return $i;