| 1 | package Tk::Shell; | ||
| 2 | my $RCSRevKey = '$Revision: 0.44 $'; | ||
| 3 | $RCSRevKey =~ /Revision: (.*?) /; | ||
| 4 | $VERSION=$1; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Tk::Shell.pm -- Handlers for Tk::Workspace.pm shell clients. | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | This module is incomplete. | ||
| 13 | |||
| 14 | There's no API or calling synopsis at the moment. It's | ||
| 15 | primary purpose is to provide some abstraction, and hopefully | ||
| 16 | data hiding, from the parent Workspace process. | ||
| 17 | |||
| 18 | Results may be unpredictable due to the I/O buffering behavior of | ||
| 19 | called programs. Most programs that have line-oriented output | ||
| 20 | should work okay. At this point Workspaces do not understand | ||
| 21 | terminal escape sequences. | ||
| 22 | |||
| 23 | The buffers of the child process's STDOUT and STDERR pipes are set to | ||
| 24 | an abnormally high value: 65535 bytes each. Hopefully that will help | ||
| 25 | prevent blocking from either channel. If the programs that you run | ||
| 26 | within the browser create more output than that, then you'd probably | ||
| 27 | want to run them in an xterm, anyway. If your Perl installation | ||
| 28 | doesn't have that much memory available, you can adjust the numbers | ||
| 29 | (the third argument in the setvbuf calls) downward, and be more | ||
| 30 | careful of what programs you run. | ||
| 31 | |||
| 32 | Broken pipe errors happen occasionally. The program prints a message | ||
| 33 | to the browser's STDERR (that is, on the terminal, unless you've | ||
| 34 | redirected STDERR from the shell prompt), and goes on, instead of | ||
| 35 | killing the process that's running the browser. | ||
| 36 | |||
| 37 | This module is tested under several varieties of Linux, and Solaris. | ||
| 38 | It is untested, and probably unusable, under MS Windoze. | ||
| 39 | |||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | require Exporter; | ||
| 44 | require Carp; | ||
| 45 | |||
| 46 | @ISA=qw(Exporter); | ||
| 47 | @EXPORT_OK=qw( VERSION ishell shell_client shell_cmd ); | ||
| 48 | |||
| 49 | use Env qw( PS1 HOME ); | ||
| 50 | use FileHandle; | ||
| 51 | use IO::File; | ||
| 52 | use IPC::Open3; | ||
| 53 | use IO::Select; | ||
| 54 | use POSIX qw(:sys_wait_h :signal_h); | ||
| 55 | |||
| 56 | # Solaris seems to want the pid of the process, not -1, in waitpid. | ||
| 57 | # But, it doesn't seem to leave as many defunct processes lying | ||
| 58 | # around, either. | ||
| 59 | my $os = `uname -a`; | ||
| 60 | if ( $os =~ /^Linux/ ) { $SIG{CHLD} = \&REAP }; | ||
| 61 | sub REAP { | ||
| 62 | my $childpid; | ||
| 63 | while(($childpid = waitpid( -1, WNOHANG)) > 0 ) { | ||
| 64 | if( WIFSTOPPED( $childpid ) ) { | ||
| 65 | kill SIGKILL, $childpid; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | $SIG{CHLD} = \&REAP; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Handle a broken pipe, which happens occasionally. | ||
| 72 | # Linux doesn't record the pid of the signalling process, or | ||
| 73 | # the signal is received after the child is gone. | ||
| 74 | $SIG{PIPE} = \&PIPESIG; | ||
| 75 | sub PIPESIG { | ||
| 76 | my $childpid = waitpid( -1, WHNOHANG ); | ||
| 77 | print STDERR "Broken Pipe: PID = $childpid, Status: $?.\n"; | ||
| 78 | $SIG{PIPE} = \&PIPESIG; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $ppid; | ||
| 82 | |||
| 83 | # external programs that the shell executes | ||
| 84 | sub shell_client { | ||
| 85 | my ($self) = @_; | ||
| 86 | my $t = $self -> {text}; | ||
| 87 | my $cmdline; my $cmd; | ||
| 88 | my $p = &prompt( $ENV{'PS1'} ); | ||
| 89 | my $o; my $output = ''; | ||
| 90 | |||
| 91 | my $startofprompt = | ||
| 92 | $t -> search( -backwards, -exact, $p, | ||
| 93 | $t -> index( 'insert' ) ); | ||
| 94 | |||
| 95 | $cmdline = $t -> get( $startofprompt, 'insert' ); | ||
| 96 | $cmd = substr $cmdline, length $p ; | ||
| 97 | chop $cmd; | ||
| 98 | goto NEXT if( length $cmd <= 1 ); | ||
| 99 | |||
| 100 | # mimick some built-ins. | ||
| 101 | if( $cmd =~ /exit/ ) { | ||
| 102 | $self -> {window} -> bind( '<KeyPress-Return>', '' ); | ||
| 103 | return; | ||
| 104 | } elsif ( $cmd =~ /^cd$/ ) { | ||
| 105 | chdir $ENV{'HOME'}; | ||
| 106 | goto NEXT; | ||
| 107 | } elsif ( ( $cmd =~ /cd/ ) || ( $cmd =~ /chdir/ ) ) { | ||
| 108 | $cmd =~ s/(cd )|(chdir )//; | ||
| 109 | chdir $cmd; | ||
| 110 | goto NEXT; | ||
| 111 | } | ||
| 112 | |||
| 113 | $shpid = open3( *IN, *OUT, *ERR, ($cmd) ); | ||
| 114 | autoflush OUT, 1; | ||
| 115 | autoflush ERR, 1; | ||
| 116 | print IN $cmd; | ||
| 117 | close( IN ); | ||
| 118 | |||
| 119 | my $selector = IO::Select -> new(); | ||
| 120 | $selector -> add( *OUT, *ERR ); | ||
| 121 | my @ch_read; | ||
| 122 | my $handle; | ||
| 123 | my $o; my $e; | ||
| 124 | |||
| 125 | *OUT -> setvbuf( $o, _IOLBF, 65535 ); | ||
| 126 | *ERR -> setvbuf( $e, _IOLBF, 65535 ); | ||
| 127 | |||
| 128 | while ( @ch_read = $selector -> can_read( 5 ) ) { | ||
| 129 | foreach $handle ( @ch_read ) { | ||
| 130 | if( fileno( $handle ) == fileno( OUT ) ) { | ||
| 131 | $o = *OUT -> getline; | ||
| 132 | $t -> insert( $t -> index( 'insert' ), $o ); | ||
| 133 | } elsif( fileno( $handle ) == fileno( ERR ) ) { | ||
| 134 | $e = *ERR -> getline; | ||
| 135 | $t -> insert( $t -> index( 'insert' ), $e ); | ||
| 136 | } | ||
| 137 | $t -> see( 'insert' ); | ||
| 138 | $t -> update; | ||
| 139 | $selector -> remove( $handle ) if eof( $handle ); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | close( OUT ); | ||
| 143 | close( ERR ); | ||
| 144 | NEXT: | ||
| 145 | $p = &prompt( $ENV{'PS1'} ); | ||
| 146 | $t -> insert( $t -> index( 'insert' ), $p ); | ||
| 147 | $t -> see( 'insert' ); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub ishell { | ||
| 151 | my ($self) = @_; | ||
| 152 | my $t = $self -> {text}; | ||
| 153 | my $w = $self -> {window}; | ||
| 154 | my $p = &prompt( $ENV{'PS1'} ); | ||
| 155 | $w -> bind( '<KeyPress-Return>', sub{shell_client( $self )}); | ||
| 156 | $t -> insert( $t -> index( 'insert' ), "\n$p" ); | ||
| 157 | $t -> see( 'insert' ); | ||
| 158 | } | ||
| 159 | |||
| 160 | # subset of bash prompt syntax only for now. | ||
| 161 | sub prompt { | ||
| 162 | my ($s) = @_; | ||
| 163 | my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = | ||
| 164 | localtime; | ||
| 165 | my $calyear = $year + 1900; | ||
| 166 | my $mname = $months[$mon]; | ||
| 167 | my $day = $weekdays[$wday]; | ||
| 168 | |||
| 169 | if( $s =~ m/\\/ ) { | ||
| 170 | if ( $s =~ m/\\[hH]/ ) { | ||
| 171 | my $hme = `hostname`; | ||
| 172 | chop $hme; | ||
| 173 | $s =~ s/\\h/$hme/; | ||
| 174 | } | ||
| 175 | # eat a possible ANSI sequence. | ||
| 176 | if ( $s =~ m/\\e/ || $s =~ m/\\033/ ) { | ||
| 177 | $s =~ s/(\\e|\\033)]*[^;]*\;*//; | ||
| 178 | } | ||
| 179 | if ( $s =~ m/\\t/ ) { | ||
| 180 | $s =~ s/\\t/$hour:$min:$sec/; | ||
| 181 | } | ||
| 182 | if ( $s =~ m/\\T/ ) { | ||
| 183 | my $thour = (($hour == 12)?12:($hour - 12)); | ||
| 184 | $s =~ s/\\T/$thour:$min:$sec/; | ||
| 185 | } | ||
| 186 | if ( $s =~ m/\\@/ ) { | ||
| 187 | my $thour = (($hour == 12)?12:($hour - 12)); | ||
| 188 | my $merid = ((($hour<12||$hour==24))?'am':'pm'); | ||
| 189 | $s =~ s/\\@/$thour:$min$merid/; | ||
| 190 | } | ||
| 191 | if ( $s =~ m/\[wW]/ ) { | ||
| 192 | } | ||
| 193 | my $dir = `pwd`; | ||
| 194 | chop $dir; | ||
| 195 | $s =~ s/\\W/$dir/; | ||
| 196 | } | ||
| 197 | if ( $s =~ m/\\d/ ) { | ||
| 198 | $s =~ s/\\d/$day $mname $mday/; | ||
| 199 | } | ||
| 200 | # eat an octal sequence, | ||
| 201 | $s =~ s/\\[0-9][0-9][0-9]//; | ||
| 202 | # gobble newlines, | ||
| 203 | $s =~ s/\n//s; | ||
| 204 | # and other prompt variables not yet implemented | ||
| 205 | $s =~ s/\\(u|v|V|a|!|\$|\\|\[)//g; | ||
| 206 | # doesn't work with an empty prompt, so... | ||
| 207 | if ( ! $s ) { $s = "# "; } | ||
| 208 | return $s; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub shell_cmd { | ||
| 212 | my ($self) = @_; | ||
| 213 | my $t = $self -> text; | ||
| 214 | local $cmd; local $output; | ||
| 215 | local $cmdentry; | ||
| 216 | tie( *TEXT, 'Tk::TextUndo', $t ); | ||
| 217 | $cmddialog = ($self -> window) -> Dialog( -title => 'Shell Command', | ||
| 218 | -buttons => ["Ok", "Cancel"], | ||
| 219 | -default_button => "Ok" ); | ||
| 220 | $cmdentry = $cmddialog -> add( 'Entry', -width => 30 ) -> pack; | ||
| 221 | $cmddialog -> Show; | ||
| 222 | $cmd = $cmdentry -> get; | ||
| 223 | $output = `$cmd`; | ||
| 224 | print TEXT $output; | ||
| 225 | untie *TEXT; | ||
| 226 | $cmddialog -> destroy; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; |