Debian Codegraph @

eg/fileman

raw
1 #!/usr/bin/env perl
2 #
3 # This is a sample program of Term::ReadLine::Gnu perl module. The
4 # origin is a C program in the GNU Readline Libarary manual Edition
5 # 2.1, "2.5.4 A Short Completion Example". This program is under GPL.
6 #
7 # Copyright (C) 1989, 1991 Free Software Foundation, Inc.
8 # Original C version
9 # Copyright (C) 1998 Hiroo Hayashi
10 # Perl version
11
12 # fileman.c -- A tiny application which demonstrates how to use the
13 # GNU Readline library. This application interactively allows users
14 # to manipulate files and their modes.
15
16 use strict;
17 use warnings;
18 use Term::ReadLine;
19
20 # A structure which contains information on the commands this program
21 # can understand.
22
23 my %commands =
24 ('cd' => { func => \&com_cd, doc => "Change to directory DIR" },
25 'delete' => { func => \&com_delete, doc => "Delete FILE" },
26 'help' => { func => \&com_help, doc => "Display this text" },
27 '?' => { func => \&com_help, doc => "Synonym for `help'" },
28 'list' => { func => \&com_list, doc => "List files in DIR" },
29 'ls' => { func => \&com_list, doc => "Synonym for `list'" },
30 'pwd' => { func => \&com_pwd,
31 doc => "Print the current working directory" },
32 'quit' => { func => \&com_quit, doc => "Quit using Fileman" },
33 'rename' => { func => \&com_rename, doc => "Rename FILE to NEWNAME" },
34 'stat' => { func => \&com_stat, doc => "Print out statistics on FILE" },
35 'view' => { func => \&com_view, doc => "View the contents of FILE" },
36 );
37
38 # The name of this program, as taken from argv[0].
39 my $progname = $0;
40
41 # When non-zero, this global means the user is done using this program.
42 my $done = 0;
43
44 my $term = initialize_readline(); # Bind our completer.
45 $term->MinLine(0); # disable implict call of add_history()
46
47 # Loop reading and executing lines until the user quits.
48 while ($done == 0) {
49 my $line = $term->readline ("FileMan: ");
50
51 last unless defined $line;
52
53 # Remove leading and trailing whitespace from the line. Then, if
54 # there is anything left, add it to the history list and execute
55 # it.
56 my $s = stripwhite($line);
57
58 if ($s) {
59 $term->AddHistory($s); # normally this is done implictly
60 execute_line($s);
61 }
62 }
63
64 exit 0;
65
66 # Execute a command line.
67 sub execute_line {
68 my $line = shift;
69
70 my ($word, $arg) = split(' ', $line);
71
72 my $command = find_command ($word);
73
74 unless ($command) {
75 printf STDERR "$word: No such command for FileMan.\n";
76 return (-1);
77 }
78
79 # Call the function.
80 return (&{$command->{func}}($arg));
81 }
82
83 # Look up NAME as the name of a command, and return a pointer to that
84 # command. Return a NULL pointer if NAME isn't a command name.
85 sub find_command {
86 my $name = shift;
87
88 return $commands{$name};
89 }
90
91 # Strip whitespace from the start and end of STRING. Return a pointer
92 # into STRING.
93 sub stripwhite {
94 my $string = shift;
95 $string =~ s/^\s*//;
96 $string =~ s/\s*$//;
97 return $string;
98 }
99
100 #/* **************************************************************** */
101 #/* */
102 #/* Interface to Readline Completion */
103 #/* */
104 #/* **************************************************************** */
105
106 # Tell the GNU Readline library how to complete. We want to try to
107 # complete on command names if this is the first word in the line, or
108 # on filenames if not.
109 sub initialize_readline
110 {
111 # Allow conditional parsing of the ~/.inputrc file.
112 my $term = new Term::ReadLine 'FileMan';
113
114 # Tell the completer that we want a crack first.
115 $term->Attribs->{attempted_completion_function} = \&fileman_completion;
116
117 return $term;
118 }
119
120 # Attempt to complete on the contents of TEXT. START and END bound
121 # the region of rl_line_buffer that contains the word to complete.
122 # TEXT is the word to complete. We can use the entire contents of
123 # rl_line_buffer in case we want to do some simple parsing. Return
124 # the array of matches, or NULL if there aren't any.
125 sub fileman_completion {
126 my ($text, $line, $start, $end) = @_;
127
128 my @matches = ();
129
130 # If this word is at the start of the line, then it is a command
131 # to complete. Otherwise it is the name of a file in the current
132 # directory.
133 @matches = $term->completion_matches ($text, \&command_generator)
134 if ($start == 0);
135
136 return @matches;
137 }
138
139 # Generator function for command completion. STATE lets us know
140 # whether to start from scratch; without any state (i.e. STATE == 0),
141 # then we start at the top of the list.
142
143 ## Term::ReadLine::Gnu has list_completion_function similar with this
144 ## function. I defined new one to be compared with original C version.
145 {
146 my $list_index;
147 my @name;
148
149 sub command_generator {
150 my ($text, $state) = @_;
151
152 # If this is a new word to complete, initialize now. This
153 # includes saving the length of TEXT for efficiency, and
154 # initializing the index variable to 0.
155 unless ($state) {
156 $list_index = 0;
157 @name = keys(%commands);
158 }
159
160 # Return the next name which partially matches from the
161 # command list.
162 while ($list_index <= $#name) {
163 $list_index++;
164 return $name[$list_index - 1]
165 if ($name[$list_index - 1] =~ /^$text/);
166 }
167 # If no names matched, then return NULL.
168 return undef;
169 }
170 }
171
172 #/* **************************************************************** */
173 #/* */
174 #/* FileMan Commands */
175 #/* */
176 #/* **************************************************************** */
177
178
179 # List the file(s) named in arg.
180 sub com_list {
181 my $arg = shift;
182 no warnings 'uninitialized';
183 return (system ("ls -FClg $arg"));
184 }
185
186 sub com_view {
187 my $arg = shift;
188 return 1 unless (valid_argument ("view", $arg));
189
190 return (system "more $arg");
191 }
192
193 sub com_rename {
194 too_dangerous ("rename");
195 return (1);
196 }
197
198 sub com_stat {
199 my $arg = shift;
200
201 return (1) unless valid_argument ("stat", $arg);
202
203 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
204 $atime,$mtime,$ctime,$blksize,$blocks);
205
206 unless (($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
207 $atime,$mtime,$ctime,$blksize,$blocks) = stat($arg)) {
208 print STDERR "$arg: $!\n";
209 return (1);
210 }
211
212 printf("Statistics for \`$arg\':\n");
213
214 printf("%s has %d link%s, and is %d byte%s in length.\n", $arg,
215 $nlink, ($nlink == 1) ? "" : "s",
216 $size, ($size == 1) ? "" : "s");
217 printf("Inode Last Change at: %s\n", scalar localtime ($ctime));
218 printf(" Last access at: %s\n", scalar localtime ($atime));
219 printf(" Last modified at: %s\n", scalar localtime ($mtime));
220 return (0);
221 }
222
223 sub com_delete {
224 too_dangerous("delete");
225 return (1);
226 }
227
228 # Print out help for ARG, or for all of the commands if ARG is not
229 # present.
230 sub com_help {
231 my $arg = shift;
232 my $printed = 0;
233
234 if (defined $arg && $commands{$arg}) {
235 printf ("%s\t\t%s.\n", $arg, $commands{$arg}->{doc});
236 $printed++;
237 }
238
239 unless ($printed) {
240 defined $arg && print "No commands match \`$arg\'. Possibilties are:\n";
241
242 foreach (sort keys(%commands)) {
243 # Print in six columns.
244 if ($printed == 6) {
245 $printed = 0;
246 print "\n";
247 }
248
249 print "$_\t";
250 $printed++;
251 }
252
253 print "\n" if ($printed);
254
255 }
256 return (0);
257 }
258
259 # Change to the directory ARG.
260 sub com_cd {
261 my $arg = shift;
262 unless (chdir ($arg)) {
263 print STDERR "$arg: $!\n";
264 return 1;
265 }
266
267 com_pwd();
268 return (0);
269 }
270
271 # Print out the current working directory.
272 sub com_pwd {
273 my $dir = $ENV{PWD} || `pwd`;
274
275 unless ($dir) {
276 print ("Error getting pwd: $dir\n");
277 return 1;
278 }
279
280 print ("Current directory is $dir\n");
281 return 0;
282 }
283
284 # The user wishes to quit using this program. Just set DONE non-zero.
285 sub com_quit {
286 $done = 1;
287 0;
288 }
289
290 # Function which tells you that you can't do this.
291 sub too_dangerous {
292 my $caller = shift;
293 printf STDERR
294 ("%s: Too dangerous for me to distribute. Write it yourself.\n",
295 $caller);
296 }
297
298 # Return non-zero if ARG is a valid argument for CALLER, else print an
299 # error message and return zero.
300 sub valid_argument {
301 my ($caller, $arg) = @_;
302 if (! $arg) {
303 printf STDERR ("%s: Argument required.\n", $caller);
304 return (0);
305 }
306
307 return (1);
308 }