Debian Codegraph @

bin/ifeffit_shell.pl

raw
1 #!/usr/bin/perl -w
2 # Simple shell for ifeffit in perl
3 use lib '/usr/local/share/ifeffit/perl';
4 use vars qw($Escape $Prompt $HOME);
5 use subs qw(ShowHist ShowCommands END HELP IfeffitHelp);
6 use Getopt::Std;
7 getopts('xq');
8
9 #---------------------------------------------------
10 # variables the user may want to customize are
11 # initialized before reading .ifeffitrc.
12 $HOME = $ENV{HOME};
13 $RCFile = "$HOME/.ifeffitrc";
14 $Escape = "!";
15 $Prompt_main = "Ifeffit>";
16 $Prompt_more = " ... >";
17 $history_file = "$HOME/.ifeffit_hist";
18 $history_lines = 500;
19 @shell_commands = qw(cd cp mv ls rm mkdir rmdir more cat pwd vi);
20 @ifeffit_commands = qw(plot newplot spline show fftf fftr load print
21 pre_edge read_data write_data exit quit ff2chi
22 zoom cursor help minimize set macro);
23 %IF_coms=(
24 "plot" => "general 2-d plot ",
25 "newplot" => "general 2-d plot, after clearing plot screen ",
26 "spline" => "XAFS background removal, ala autobk ",
27 "show" => "display scalar, array, or text Program Variables ",
28 "print" => "print value of scalar or array variable or expression",
29 "fftf" => "forward (k->R) XAFS Fourier transform ",
30 "fftr" => "reverse (R->k) XAFS Fourier transform ",
31 "load" => "execute a file of Ifeffit commands ",
32 "pre_edge" => "determine e0, pre-edge line, and edge jump of xmu data",
33 "read_data" => "read arrays from data file ",
34 "write_data" => "write arrays and text strings to data file ",
35 "exit" => "leave Ifeffit ",
36 "quit" => "leave Ifeffit ",
37 "ff2chi" => "combine feff paths to give k and chi arrays ",
38 "zoom" => "pick window on plot screen with mouse ",
39 "cursor" => "pick x-y coordinates on plot screen with mouse ",
40 "minimize" => "generalized least-squares fit ",
41 "set" => "define a scalar, array, or text Program Variable ",
42 "macro" => "define an ifeffit macro");
43
44 #---------------------------------------------------
45 # load needed perl modules
46 $,=" "; $|=1 ;
47
48
49 #---------------------------------------------------
50 # load needed perl modules
51 $,=" "; $|=1 ;
52
53 # make sure we can use ifeffit module
54 print " Ifeffit";
55 eval "use Ifeffit";
56 if ($Ifeffit::VERSION) {
57 ifeffit("print \$&build");
58 print " Ifeffit Shell Perl Module $Ifeffit::VERSION ";
59 } else {
60 die "Perl Module not installed properly\n";
61 }
62 # readline module
63 eval "use Term::ReadLine"; my $does_readline = ($@ eq "");
64 unless ($opt_q) {
65 print $does_readline ? "(ReadLine enabled) " : "(No ReadLine) " ;
66 }
67 print "\n";
68 #-------------------------------------
69 # load startup files, then files listed on command line
70 if ($opt_x) {
71 print "no configuration file loaded \n" unless ($opt_q);
72 } elsif ( -e $RCFile ) {
73 print " reading $RCFile ... " unless ($opt_q);
74 eval 'require "$RCFile"';
75 unless ($opt_q) {($@ eq "") ? print " OK\n": print "problem\n"; }
76 }
77 my $retval = 0;
78 foreach my $f (@ARGV) {
79 print " loading $f ..." unless ($opt_q);
80 $retval = ifeffit("load $f\n");
81 unless ($opt_q) { ($retval==0) ? print " OK\n" : print " problem!\n";}
82 }
83
84 my $shellcoms = join ("|", @shell_commands ); # used below in regex match
85 #-------------------------------------
86 # set up readline stuff
87 if ($does_readline) {
88 $term = new Term::ReadLine 'ifeffit', \*STDIN, \*STDOUT ;
89 $term->ornaments(1);
90 # read in history from last session
91 if ((-e "$history_file")&& (open HIST, "<$history_file")) {
92 my @allhist = <HIST>;
93 close HIST;
94 map s/\n//g , @allhist ;
95 foreach (@allhist) { $term->addhistory($_); }
96 }
97 # set completion model: shell_commands and ifeffit_commands
98 # for 1st word, filenames for rest of line
99 my $attribs = $term->Attribs;
100 $attribs->{attempted_completion_function} = sub {
101 my ($text, $line, $start, $end) = @_;
102 if ((substr($line, 0, $start) =~ /^\s*$/) or
103 (substr($line, 0, $start) =~ /^help/i) ) {
104 $attribs->{completion_entry_function} = $attribs->{'list_completion_function'};
105 $attribs->{completion_word} = [@shell_commands, @ifeffit_commands];
106 return $term->completion_matches($text, $attribs->{'completion_entry_function'});
107 } else {
108 $attribs->{completion_entry_function} = $attribs->{'filename_completion_function'};
109 return ();
110 }
111 }
112 }
113 #
114 # determine prompt and reading mechanism
115 if ($does_readline) { # use readline if available
116 $getline = sub { my $p = shift;
117 my $j = $term->readline($p);
118 $j = "__NULL_\n\n" unless $j;
119 chomp $j; $j =~ s|~|$ENV{"HOME"}| ; return $j; };
120 } else { # or use simple read
121 $getline = sub { my $p = shift;
122 print "$p"; my $j = <>;
123 $j = "__NULL_\n\n" unless $j;
124 chomp $j; $j =~ s|~|$ENV{"HOME"}| ; return $j; };
125 }
126 #-------------------------------------
127
128 ## main loop
129 my ($line, $prom, @args);
130 if ($opt_x) {$line += $opt_x};
131 while (1) {
132 $prom = ($retval == -1) ? $Prompt_more : $Prompt_main; # decide prompt
133 $_ = &$getline($prom); # prompt for line
134 next if ((/^\s*$/) or (/^__NULL/));
135 process_command($_) if ($_);
136 }
137 ## done
138 #-------------------------------------
139 sub process_command{
140 $line = $_ = shift;
141 @args = split(); #
142 $_ = lc(shift(@args)); # simplifies the matching below
143 # execute: # based on first word (now in $_)
144 exit if (/^quit$/o or /^exit$/o);
145 if (/^\?$/o or /^help$/o) { Help($line); }
146 elsif (/^\?\?$/o) { ShowCommands;}
147 elsif (/^l$/o) { ShowHist(@args); }
148 elsif (/^cd$/o) { chdir("@args"); }
149 elsif (/^rm$/o) { unlink("@args"); }
150 elsif (/^perl$/o) { eval("@args"); }
151 elsif (/^($shellcoms)$/) { system("$line"); }
152 elsif (/^$Escape/) { system(substr("$line",1)); }
153 else {
154 $line =~ s|~|$ENV{"HOME"}| if (/^load$/o or /^read/o or /^write/o);
155 $retval = ifeffit("$line\n");
156 }
157 }
158 sub ShowHist { # use 'l' to show command history
159 if ($does_readline) {
160 my $n = $#_ > -1 ? shift : 10;
161 my @h = $term->GetHistory();
162 my $min = $#h < $n-1 ? 0 : $#h-$n+1;
163 map {print "$_: $h[$_]\n"} ($min..$#h);
164 }
165 }
166
167 sub ShowCommands {
168 print "Ifeffit is aware of these shell commands:\n ";
169 my $i = 0;
170 foreach my $t (@shell_commands) {
171 print " $t";
172 $i += 1 + length($t);
173 if ($i > 60) {print "\n "; $i = 0;}
174 }
175 print "\n" unless ($i == 0);
176 }
177
178 sub Help {
179 @args = split(/\s+/, $_[0]);
180 if ($#args < 1) { # general help from pod
181 print "Help is on the way ...\n";
182 system ("perldoc $0\n");
183 } else {
184 shift @args;
185 foreach my $t (@args) {IfeffitHelp($t);}
186 }
187 }
188
189 sub END { # executed at 'exit'
190 # save history in history file
191 if ($does_readline) {
192 my @a= $term->GetHistory() if $term->can('GetHistory');
193 $#a-- if $a[-1] =~ (/^\s*(quit|exit)\s*$/); # don't save the exit command!
194 @a= @a[($#a-$history_lines+1)..($#a)] if $#a > $history_lines;
195 if (open HIST, ">$history_file" ) {
196 print HIST join("\n",@a);
197 close HIST;
198 } else {
199 print "Can't open $history_file\n";
200 }
201 }
202 ##print "\tHave a nice day\n" unless ($opt_q);
203 }
204
205 sub IfeffitHelp {
206 my $t = shift;
207 my ($s) = ("unknown command (may be a shell command)");
208 print " $t: ";
209 foreach my $k (keys %IF_coms) {
210 if ($k eq $t) { $s = $IF_coms{$k};}
211 }
212 print " $s\n";
213 }
214
215
216 __END__
217
218 =head1 NAME
219
220 ifeffit - Shell Interface to IFEFFIT
221
222 =head1 SYNOPSIS
223
224 B<ifeffit> is a shell interface to the Ifeffit XAFS Analysis System.
225 Ifeffit commands are entered and executed as they are typed in, giving
226 complete access to the Ifeffit library in a relatively friendly
227 command-line program.
228
229 A sample session might look like this:
230
231 %~>ifeffit
232 Ifeffit 1.0004 Copyright (c) 2000 Matt Newville, Univ of Chicago
233 Ifeffit Shell Perl Module 1.2 (ReadLine enabled)
234 reading /home/newville/.ifeffitrc ... OK
235 Ifeffit> read_data(My.xmu,type=xmu)
236 Ifeffit> spline(energy,xmu, rbkg = 1.0, kweight =1 )
237 Ifeffit> plot(energy, xmu)
238 Ifeffit> plot(energy, bkg)
239
240 =head1 DESCRIPTION
241
242 If you have the Perl module for the ReadLine library (Term::ReadLine and
243 Term::Readkeys are both required), you can use the command-line editing
244 features of the Gnu ReadLine library. Most importantly, this library makes
245 the up arrow scroll through previous commands. It also gives you access to
246 the last 300 commands typed in (stored in the file F<.ifeffit_hist> in your
247 home directory) in previous sessions. The command C<l [number]> shows you
248 the last C<number> commands typed in.
249
250 There is also some support for command-line completion via the tab key.
251 While typing the first word at the command-line prompt, selected ifeffit
252 commands and shell commands will be 'tab-completed', meaning that hitting
253 the tab key will complete as much of a partially-typed command as possible.
254 The list of ifeffit and shell commands recognized for this purpose is
255 customizable. After the first word has been fully typed, the tab key will
256 complete file names in the current working directory.
257
258 e.g.:
259
260 %~>ifeffit
261 Ifeffit 1.0002 Copyright (c) 2000 Matt Newville, Univ of Chicago
262 Ifeffit Shell Perl Module 1.2 (ReadLine enabled)
263 reading /home/newville/.ifeffitrc ... OK
264 Ifeffit>
265
266 =over 4
267
268 =item 1.
269
270 'quit' and 'exit' will exit the program.
271
272 'l' lists the history buffer
273
274 '?' is an alias for help, which will display this document, or give a
275 brief command summary (as in: 'help plot').
276
277 '??' lists the known system shell commands that can be executed from
278 within ifeffit. For other shell commands, use the '!' character
279 to escape to the system shell.
280
281 =item 2.
282
283 The resource file F<~/.ifeffitrc> (if found) is read at start-up to allow
284 customizations. This file is read as a B<perl> script (not as raw ifeffit
285 commands). To allow further confusion (err, customization), you can load
286 files of ifeffit commands at startup -- I<ifeffit myfile> will load the
287 file F<myfile> at start-up, as if you typed 'load myfile' at the
288 command-line. This loading occurs after the F<~/.ifeffitrc> file is read,
289 which can provide a convenient way to override default macro definitions.
290
291 =item 3.
292
293 Shell variables: (can be customized in F<~/.ifeffitrc>)
294
295 $Escape [default = '!'] a line starting with this character is sent
296 to the shell.
297
298 $Prompt [default = 'Ifeffit> '] command-line prompt.
299
300 $HOME [default from Environment Variable] users home directory.
301
302 =item 4.
303
304 A useful procedure for developing and testing ifeffit scripts is
305
306 ifeffit> ! emacs script &
307 -- add ifeffit code to script and save the file
308 ifeffit> load script
309
310 =back
311
312 =head1 CUSTOMIZATION
313
314 A typical ~/.ifeffitrc file might look like this:
315
316 #
317 # start-up perl for shiffit
318 push @shell_commands, qw(emacs grep gunzip diff);
319 #
320 # pre-load some common ifeffit macros
321 ifeffit("load $HOME/.ifeffit_macros ");
322
323 This file is "require'd" by I<ifeffit>. The "push" line, adds a few more
324 commands to the default list of shell commands. The "ifeffit" line loads
325 a bunch of pre-defined ifeffit macros.
326
327 =head1 AUTHOR
328
329 Matthew Newville -- [email protected]
330
331 =head1 SEE ALSO
332
333 I<Term::Readline> perl module to use the GNU Readline library to improve
334 reading the command-line and enabling history mechanism. This module is
335 available from Comprehensive Perl Archive Network.
336
337 I<Ifeffit.pm> perl module to use the Ifeffit XAFS Analysis library from
338 within perl. Several applications distributed as part of the Ifeffit
339 Analysis System, including this program, requires this Ifeffit perl module,
340
341 I<Ifeffit Reference Manual> main reference for the commands of the Ifeffit
342 XAFS Analysis system.
343
344
345 =cut