4 # ------------------------------------------------------------------
6 # Copyright (C) 2002-2005 Novell/SUSE
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of version 2 of the GNU General Public
10 # License published by the Free Software Foundation.
12 # ------------------------------------------------------------------
21 our @ISA = qw(Exporter);
22 our @EXPORT = qw(y2milestone y2debug ParseCommand Return ycpReturn ycpReturnSkalarAsString ycpReturnHashAsMap ycpGetCommand ycpGetArgType);
27 my $logFile = '/var/log/YaST2/imx-log';
29 if ( open(LOG, ">>$logFile") ) {
31 print LOG "$date: $mesg\n";
40 my $logFile = '/var/log/YaST2/imx-errors';
42 if ( open(LOG, ">>$logFile") ) {
44 print LOG "$date: ERROR: $mesg\n";
52 my $logFile = '/var/log/YaST2/imx-debug';
54 if ( open(LOG, ">>$logFile") ) {
56 print LOG "$date: DEBUG: $mesg\n";
70 if(ref($ref) eq "HASH") {
72 for my $key (keys %$ref) {
73 if($key =~ m/^\d+$/) {
74 $string .= "$key:" . perlToYcp($ref->{$key}) . ",";
76 $string .= "\"$key\":" . perlToYcp($ref->{$key}) . ",";
80 } elsif(ref($ref) eq "ARRAY") {
82 for my $element (@$ref) {
83 $string .= perlToYcp($element) . ',';
86 } elsif(defined $ref) {
88 if($ref =~ m/^(true|false|nil|\d+)$/) {
102 return ycpReturn($data);
110 $string = perlToYcp($data);
112 $string = "(" . perlToYcp($data) . ")";
118 sub ycpReturnHashAsMap {
121 return ycpReturn(\%hash);
124 sub ycpReturnSkalarAsString {
127 return ycpReturn($scalar);
130 #my $data = { foo => [ "one", "two", "three" ], bar => "foobar" };
131 #my $data = [ "foo", [ "one", "two", "three" ], "bar", "foobar" ];
135 my $string = shift || "";
137 my $original_string = $string;
139 my @stack = ( "TOPOFSTACK" );
145 # strip leading whitespace
147 # strip trailing comma or whitespace if they exist
148 $string =~ s/,?\s*$//;
151 if($string =~ s/^\$\[//s) { # beginning of a hash
153 # create a new hash ref
156 # insert it into the tree at our current location
158 # if tree hasn't been set up yet, create it now as a hash
161 } elsif(ref($where) eq "ARRAY") {
163 } elsif(ref($where) eq "HASH") {
165 $where->{$key} = $hash;
167 die "ERROR: trying to insert hash value without a key: $_";
170 die "ERROR: clowns ate my brain: $_";
173 # zero out out the key for the new hash...
176 # push the parent onto the stack
179 # our new "current" location is the newly created hash
182 } elsif($string =~ s/^\[//s) { # beginning of an array
184 # create a new array ref
187 # insert it into the tree at our current location
189 # if tree hasn't been set up yet, create it now as an array
192 } elsif(ref($where) eq "ARRAY") {
193 push @$where, $array;
194 } elsif(ref($where) eq "HASH") {
196 $where->{$key} = $array;
198 die "ERROR: trying to insert hash value without a key: $_";
201 die "ERROR: Can't identify var for translation: $_";
206 # push the parent onto the stack
209 # our new "current" location is the newly created array
212 } elsif($string =~ s/^(true|false|nil)(?=[,:\]])//s) { # true/false
216 $realvalue = 1 if $value eq "true";
217 $realvalue = 0 if $value eq "false";
218 $realvalue = undef if $value eq "nil";
220 # shove it into the right place
221 if(ref($where) eq "HASH") {
223 $where->{$key} = $realvalue;
228 } elsif(ref($where) eq "ARRAY") {
229 push @$where, $realvalue;
231 die "ERROR: awoooga! awooooga!: $string";
233 } elsif($string =~ s/^"([^"]*)"//s) { # normal string
236 # shove it into the right place
239 } elsif(ref($where) eq "HASH") {
241 $where->{$key} = $value;
246 } elsif(ref($where) eq "ARRAY") {
247 push @$where, $value;
249 die "ERROR: dogs don't know it's not bacon: $string";
251 } elsif($string =~ s/^(\d+)(?=[,:\]])//s) { # normal integer
254 # shove it into the right place
255 if(ref($where) eq "HASH") {
257 $where->{$key} = $value;
260 $key = $value; # ??? - can we use a bare integer as a hash key?
262 } elsif(ref($where) eq "ARRAY") {
263 push @$where, $value;
265 die "ERROR: one by one the penguins steal my sanity: $string";
267 } elsif($string =~ s/^\]//) {
268 # hit the end of this containing block, move back up a level
270 if($where eq "TOPOFSTACK") {
271 die "ERROR: popped off top of stack: $string";
274 y2error("ERROR: failed to parse: '$original_string'");
275 die "ERROR: failed to parse: '$original_string'";
278 # strip trailing : or , and any whitespace
279 $string=~ s/^[,:]\s*//s;
282 if(pop(@stack) ne "TOPOFSTACK") {
283 die "ERROR: stack depth mismatch";
293 my $original_string = $string;
295 if($string=~ m/^`?(\S+)\s*\((.+)\)\s*$/) {
296 my ($cmd, $params) = ($1, $2);
298 if($params =~ m/^(\.\S*),\s*(.+)\s*$/) {
299 my ($path, $args) = ($1, ycpToPerl($2));
301 return ($cmd, $path, $args);
302 } elsif($params =~ m/^(\.\S*)$/) {
305 return ($cmd, $path, "");
306 } elsif($cmd eq "result" && $params eq "nil") {
307 return ($cmd, "", "");
308 } elsif($params eq "") {
309 return ($cmd, "", "");
311 die "ERROR: failed to parse params: $params - $original_string\n";
314 die "ERROR: failed to parse command: $string";
320 #my $foo = ycpToPerl('$["one":"1one", "two":"2two", "three":["foo", $["holy":"catfish", "bacon":"cheese"], "baz"]]');
322 #my ($ycommand, $ypath, $yargument) = ParseCommand('Read(.foobar, $["one":"1one", "two":"2two", "three":["foo", $["holy":"catfish", "bacon":false], "baz"]])');
324 #print Data::Dumper->Dump([$ycommand, $ypath, $yargument], [qw(*ycommand *ypath *yargument)]);
325 #print Data::Dumper->Dump([$foo]);