Newer
Older
#!/usr/bin/perl
#
# Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
#
# Simple perl script to effect system-wide and per-user default
# selections of which MPI implementation to use.
#
use strict;
use Getopt::Long;
use Text::Wrap;
use File::Copy;
#===========================================================================
=head1 NAME
openfoam-selector-menu - A menu-based wrapper around the openfoam-selector command
openfoam-selector-menu [--version] [--help]
=head1 DESCRIPTION
Simple wrapper around the openfoam-selector command-line
Adapted from mpi-selector-menu.
=head1 AUTHOR
Written by Jeff Squyres.
=head1 REPORTING BUGS
Send bug reports to the OpenFabrics general mailing list (see
L<http://www.openfabrics.org/>). This is a high-volume mailing list,
so be sure to put "openfoam-selector" in the subject to ensure that it is
not missed.
=head1 COPYRIGHT
Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
This is free software; see the source for copying conditions. There
is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Copyright (C) 2018 OpenCFD Ltd.
openfoam-selector(1)
=cut
#===========================================================================
sub show_help {
our $silent;
my $ret = shift;
print "$0 options:
--help This help message
--version Display the version of $0.
";
exit($ret);
}
#===========================================================================
sub make_safe_filename {
my $name = shift;
$name =~ s/[ :\/\\\*\&\$\#\@\!\t\n\[\]\{\}\(\)]/_/g;
$name =~ s/^__internal//; # Disallow reserved names
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
return $name;
}
#===========================================================================
sub error {
our $silent;
print STDERR wrap("ERROR: ", " ", @_) . "\n"
if (!$silent);
exit(1);
}
sub warning {
our $silent;
print STDERR wrap("WARNING: ", " ", @_) . "\n"
if (!$silent);
}
sub verbose {
our $silent;
our $verbose_flag;
print wrap("", "", @_) . "\n"
if ($verbose_flag && !$silent);
}
#===========================================================================
sub get_yn {
my $prompt = shift;
my $default = shift;
if (defined($default)) {
if ($default) {
$default = 1;
$prompt .= " (Y/n) ";
} else {
$default = 0;
$prompt .= " (y/N) ";
}
} else {
$prompt .= " (y/n/) ";
}
while (1) {
print $prompt;
my $ans = <STDIN>;
chomp($ans);
if ($ans =~ /y/i) {
return 1;
} elsif ($ans =~ /n/i) {
return 0;
} elsif ("" eq $ans) {
return $default
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
if (defined($default));
}
print "\nPlease choose Y or N\n";
}
}
#===========================================================================
# Set autoflush
select STDOUT;
$| = 1;
# Module options
$Text::Wrap::columns = 76;
&Getopt::Long::Configure("bundling");
my $help = 0;
my $version = 0;
my $ok = Getopt::Long::GetOptions("help|h" => \$help,
"version" => \$version,
);
show_help(1) if (!$ok);
show_help(0) if ($help);
#---------------------------------------------------------------------------
# Version informtion
if ($version) {
print "$0 version 1.0.0
Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Jeff Squyres.\n";
exit(0);
}
#---------------------------------------------------------------------------
# Ensure that we can find the openfoam-selector executable
#---------------------------------------------------------------------------
sub query {
my $arg = shift;
my $ret = `openfoam-selector --query $arg | head -n 1 | cut -d: -f2`;
chomp($ret);
$ret = "<none>" if ($ret eq "");
return $ret;
}
sub list {
my $list = [];
open(CMD, "openfoam-selector --list|");
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
while (<CMD>) {
chomp;
push(@$list, $_);
}
sort @$list;
return $list;
}
sub get_user_or_system {
my $ret;
while (1) {
print "Operator on the per-user or system-wide default (u/s)? ";
$ret = <STDIN>;
if (!defined($ret)) {
print "\n";
exit(0);
}
if ($ret =~ /^\s*u\s*$/i) {
return "u";
} elsif ($ret =~ /^\s*s\s*$/i) {
return "s";
}
}
}
# Main menu
my $registered = list();
my $first = 1;
my $made_changes = 0;
my $warning = "\nWARNING: Changes made to openfoam-selector defaults will not be visible until
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
you start a new shell!\n\n";
while (1) {
my $system_default = query("--system");
my $user_default = query("--user");
if ($first) {
$first = 0;
} else {
print "\n";
}
print "Current system default: $system_default
Current user default: $user_default
\"u\" and \"s\" modifiers can be added to numeric and \"U\"
commands to specify \"user\" or \"system-wide\".\n\n";
my $i = 1;
while ($i - 1 <= $#$registered) {
print "$i. $$registered[$i - 1]\n";
++$i;
}
--$i;
print "U. Unset default
Q. Quit
Selection (1-${i}[us], U[us], Q): ";
my $val = <STDIN>;
# Check for bozo: no stdin
if (!defined($val)) {
print "\n";
print $warning
if ($made_changes);
exit(0);
}
# Check letter options
if ($val =~ /^q$/i) {
print $warning
if ($made_changes);
exit(0);
} elsif ($val =~ /^\s*u\s*s\s*$/i) {
system("openfoam-selector --unset --system");
$made_changes = 1;
} elsif ($val =~ /^\s*u\s*u\s*$/i) {
system("openfoam-selector --unset --user");
$made_changes = 1;
} elsif ($val =~ /^\s*u\s*$/i) {
$val = get_user_or_system();
my $str = "openfoam-selector --unset " .
("s" eq $val ? "--system" : "--user");
system($str);
$made_changes = 1;
# Numeric options
elsif ($val =~ m/^\s*([0-9]+)\s*([us])*\s*$/) {
# In range?
if ($1 < 0 || $1 > $i) {
printf("$1 is not a valid choice\n");
next;
}
# Figure out if system or user
if ("" eq $2) {
$val = get_user_or_system();
} else {
$val = $2;
}
my $i = $1 - 1;
my $str = "openfoam-selector --set $$registered[$i] " .
("s" eq $val ? "--system" : "--user");
system($str);
$made_changes = 1;
}
}
# Shouldn't get here
exit(0);