root/trunk/Dish/dish

Revision 1, 6.2 kB (checked in by wombat, 10 years ago)

Initial revision

  • Property svn:executable set to *
Line 
1#!/usr/bin/perl -w
2#
3#       $rcs = ' $Id: dish,v 1.1 2001/01/24 03:34:06 wombat Exp $ ' ;   
4#
5# Copyright (c) 2000 Dan Cardamore  <wombat@hld.ca>
6#
7# This program is free software released under the GNU GPL.
8#
9
10
11package Dish;
12use strict;
13use Term::ReadLine;     # this is the Gnu one written by Hiroo Hayashi
14use Curses;
15use Curses::Widgets qw( :all );
16
17
18use vars qw/$term $readconfig $HISTORY @path $login $window/;
19$login = getpwuid($<);
20
21##################
22###  Start of TEMPORARY defines
23$HISTORY = $ENV{'HOME'} . "/.dish_history";
24@path = split /:/, $ENV{'PATH'};
25###
26#################
27
28sub Version()
29{
30        print "Dish version 0.1\n";
31        print "Uses: Curses, Curses::Widgets, Term::ReadLine::Gnu\n";
32        print "Using Term::ReadLine: " . $term->ReadLine . "\n";
33}
34
35sub initCurses()
36{
37        $window = Curses->new or die "Can't get new Curses window\n";
38}
39
40sub exitCurses()
41{
42        endwin();
43}
44
45sub mainWindow()
46{
47        #       $window->erase();
48        # This function selects a few common colours for the foreground colour
49}
50
51# This routine initializes the terminal for readline and curses
52sub init()
53{
54        $term = new Term::ReadLine 'dish';
55        $readconfig = $term->Attribs;
56
57        $term->stifle_history(1000);  # limit history to 1000 lines
58        $term->MinLine(undef);        # don't automatically add lines to history
59
60        if ( -f $HISTORY)
61        {
62                        $term->ReadHistory($HISTORY) or print "No history\n";
63        }
64        else { system("touch $HISTORY"); }  # create the history file
65
66        #  Now setup some key bindings
67        $readconfig->{attempted_completion_function} = \&keywordCompletion;
68        $readconfig->{completion_display_matches_hook} = \&displayMatchList;
69
70        $SIG{INT} = sub {
71                        $term->modifying;
72                        $term->delete_text;
73                        $readconfig->{point} = $readconfig->{end} = 0;
74                        $term->redisplay;
75        };
76        return;
77}
78
79# determines if this is a perl or system keyword
80sub keywordCompletion($$$$)
81{
82        my ($text, $line, $start, $end) = @_;
83        my $perlflag = 0;
84        if ($perlflag == 1)
85        {
86                return &perlCompletion($text,$line,$start,$end);
87        }
88        else
89        {
90                return &systemCompletion($text,$line,$start,$end);
91        }
92}
93
94sub systemCompletion($$$$)
95{
96        my ($text, $line, $start, $end) = @_;
97        my @matches = getSystemFunctionMatches($text);
98
99        if (substr($line, 0, $start) =~ /^\s*$/) {
100                $readconfig->{completion_word} = \@matches;
101                undef $readconfig->{completion_display_matches_hook};
102                return $term->completion_matches($text,
103                                                                                                                                                 $readconfig->{'list_completion_function'});
104        }
105        elsif ($line =~ /^\s*(\.\/)\s/)
106        {
107                $readconfig->{completion_display_matches_hook} = \&systemFilenameDisplayMatches;
108                return $term->completion_matches($text,
109                                                                                                                                                 \&systemFilenameCompletion);
110        }
111
112        else {                  # put mput lcd
113                undef $readconfig->{completion_display_matches_hook};
114                return ();              # local file name completion
115        }
116
117#       $readconfig->{completion_append_character} = '';
118#       return $term->completion_matches($text, \&systemSearchPath);
119}
120
121sub systemFilenameDisplayMatches()
122{
123        my($matches, $num_matches, $max_length) = @_;
124        map { $_ =~ s|.*/([^/])|$1|; }(@{$matches});
125        $term->display_match_list($matches);
126        $term->forced_update_display;
127}
128
129sub systemSearchPathFunction()
130{
131        my ($text, $state) = @_;
132        my $i;
133        my @matches;
134       
135        if ($state)
136        {
137                $i++;
138        }
139        else
140        {
141                $i = 0;
142                @matches = "?";
143
144                foreach my $dir ( (".", @path) )
145                {
146                        opendir (DIR, $dir);
147                        my @files = readdir(DIR);
148                        closedir (DIR);
149
150                        @files = grep(/^$text/, @files);
151                        if ($#files > 0)
152                        {
153                                # remove . and ..
154                                @files = grep (!/^\.\.?$/, @files);
155                        }
156                        push @matches, @files;
157                }
158        }
159
160        my $entry;
161        my $most = $text;  # this will be used to complete as much as possible but not more
162
163        for (; $i <= $#matches; $i++)
164        {
165                print "i = $i\n";
166                $entry = $matches[$i];
167                return $entry if ($entry =~ /^\Q$text/);
168        }
169        return undef;
170}
171
172sub getSystemFunctionMatches()
173{
174        my ($text, $state) = @_;
175        my $i;
176        my @matches;
177
178        $i = 0;
179        @matches = "?";
180
181        foreach my $dir ( (".", @path) )
182        {
183                opendir (DIR, $dir);
184                my @files = readdir(DIR);
185                closedir (DIR);
186
187                @files = grep(/^$text/, @files);
188                if ($#files > 0)
189                {
190                        # remove . and ..
191                        @files = grep (!/^\.\.?$/, @files);
192                }
193                push @matches, @files;
194        }
195        @matches = sort @matches;
196        return @matches;
197}
198
199
200sub perlCompletion($$$$)
201{
202        my ($text, $line, $start, $end) = @_;
203
204#       if (substr($line, 0, $start) =~ m/\$([\w:]+)\s*(->)?\s*{\s*['"]?$/) {
205#               # $foo{key, $foo->{key
206#               $readconfig->{completion_append_character} = '}';
207#               return $term->completion_matches($text, \&perl_hash_key_completion_function);
208#       }
209#       elsif (substr($line, 0, $start) =~ m/\$([\w:]+)\s*->\s*['"]?$/) {
210#               # $foo->method
211#               $readconfig->{completion_append_character} = ' ';
212#               return $term->completion_matches($text, \&perl_method_completion_function);
213#       }
214#       else { # Perl symbol completion
215#               $readconfig->{completion_append_character} = '';
216#               return  $term->completion_matches($text, \&perl_symbol_completion_function);
217#       }
218}
219
220sub displayMatchList($$$$)
221{
222    my ($text, $line, $start, $end) = @_;
223
224}
225
226sub quit()
227{
228    $term->WriteHistory($HISTORY);
229
230
231    print "\n";
232    exit(0);                     # clean exit
233}
234
235
236sub prompt()
237{
238    my $host = $ENV{'HOSTNAME'};
239    my $dir = $ENV{PWD};
240    $dir =~ s/^$ENV{'HOME'}/~/;
241#    my $prompt = "[$login\@$host $dir]\$ ";   $dir doesn't refresh
242    my $prompt = "[$login\@$host]-> ";
243
244    return $prompt;
245}
246
247sub executePerl()
248{
249
250}
251
252sub executeSystem($)
253{
254        my ($command) = @_;
255
256        if ($command =~ /^\s*cd/)
257        {
258                $command =~ s/^\s*cd\s*//;
259                chdir($command);
260                return;
261        }
262        elsif ($command =~ /^\s*version/)
263        {
264                Version();
265                return;
266        }
267        elsif ($command =~ /\&\s*$/)
268        {
269                # this one needs to be backgrounded
270               
271        }
272  else
273        {
274                        system($command);
275        }
276        return;
277}
278
279sub getline()
280{
281        my ($line, $command);
282        $command = '';
283        while (1)
284        {
285                $line = $term->readline($command ? '> ' : prompt());
286                return undef unless (defined $line);
287
288                if ($line =~ /\\$/) # testing if this is multi line
289                {
290                        chop $line;       # remove newline char
291                        $command = $command ? $command . " $line" : $line;
292                }
293                else
294                {
295                        $command = $command ? $command . " $line" : $line; #get the command
296                        $term->addhistory($command) if (length($command) > 0);  # add to history
297                        return $command;
298                }
299        }
300}
301
302sub main()
303{
304        my $perlcommand = undef;
305        my $command;
306        while ( defined ($command = &getline) )
307        {
308                if ($perlcommand)
309                {
310                        &executePerl($command);
311                }
312                else
313                {
314                        &executeSystem($command);
315                }
316        }
317        &quit();   #  we're done now so quit
318}
319
320# Start:
321&init();
322&main();
Note: See TracBrowser for help on using the browser.