#!/usr/bin/perl -w # # $Id: ag_tty,v 1.4 2004/02/24 12:03:57 visnov Exp $ # Author: Martin Vidner # Stanislav Visnovsky # # An agent for /dev/tty using Perl readline library use ycp; use strict; use Term::ReadLine; use Encode; # query the current encoding use I18N::Langinfo qw(langinfo CODESET); my $codeset = langinfo(CODESET()); # # MAIN cycle # # read the agent arguments $_ = ; # no input at all - simply exit # exit if ! defined $_; # reply to the client (this actually gets eaten by the ScriptingAgent) ycp::Return (undef); my $has_tty=0; my $term; my $OUT; eval { $term = Term::ReadLine->new( 'Simple Readline interface'); $OUT = $term->OUT || \*STDOUT; $term->ornaments(0); $has_tty=1; }; if ($@) { my $OUT = \*STDOUT; } my $prompt = "YaST2> "; while ( ) { my ($command, $path, $argument) = ycp::ParseCommand ($_); if ($command eq "Write") { if( $path eq "." ) { # recode from utf8 (broken YaST) # it sends almost UTF-8 (but encodes some chars as octals), so Perl # does not like it as UTF-8. Let's force the conversion my $octets = encode( "iso-8859-1", $argument ); Encode::from_to($octets, "utf-8", $codeset); print $OUT $octets ,"\n"; ycp::Return ( "true" ); } elsif ( $path eq ".prompt" ) { $prompt = $argument; ycp::Return( "true" ); } elsif ( $path eq ".stderr" ) { # recode from utf8 (broken YaST) # it sends almost UTF-8 (but encodes some chars as octals), so Perl # does not like it as UTF-8. Let's force the conversion my $octets = encode( "iso-8859-1", $argument ); Encode::from_to($octets, "utf-8", $codeset); print STDERR $octets ,"\n"; ycp::Return ( "true" ); } else { y2error ("Unrecognized path! '$path'"); ycp::Return (undef); } } elsif ($command eq "Read") { if ($path eq ".") { # the 1 prevents returning strings as integers/booleans $_ = $term->readline($prompt) if ($has_tty); if( defined ($_) ) { ycp::Return ($_, 1); } else { ycp::Return (undef); } } else { y2error ("Unrecognized path! '$path'"); ycp::Return (undef); } } elsif ($command eq "result") { exit; } # Unknown command else { y2error ("Unknown instruction $command or argument: ", ref ($argument)); ycp::Return (undef); } print "\n"; }