package INSTALL::query; use INSTALL::common; use strict; my $CONST_Columns; my $CONST_Rows; my $CONST_Initialized; BEGIN { $CONST_Columns = 80; $CONST_Rows = 24; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Internal functions #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # On solaris, background processes (which do not have control of the tty # are unable to invoke stty -- it will block indefinitely). As a result, # I have added this function to be invoked once and only once, after the # code has initialized, and then only in interactive mode. Anything else # will result in hanging characteristics. sub Initialize { if ((not $CONST_Initialized) && interactive) { my @stty_output = split("\n",`stty -a 2> /dev/null`); foreach(@stty_output) { if (/columns\s+[=]?\s*([0-9]+)/) { my $value = $1; if ($value > 0) { $CONST_Columns = $value; } } elsif (/([0-9]+)\s+columns/) { # AIX's twisted format. my $value = $1; if ($value > 0) { $CONST_Columns = $value; } } if (/rows\s+[=]?\s*([0-9]+)/) { my $value = $1; if ($value > 0) { $CONST_Rows = $value; } } elsif (/([0-9]+)\s+rows/) { # AIX's twisted format. my $value = $1; if ($value > 0) { $CONST_Rows = $value; } } } } # print "Columns: $CONST_Columns\n"; # print "Rows: $CONST_Rows\n"; $CONST_Initialized = 1; } #------------------------------------------------------------------------------ # Confirms / enforces that each item of the list is only listed once in the # output. #------------------------------------------------------------------------------ sub squeeze_list { my %UniqueMap; my @result; foreach my $item (@_) { unless (exists $UniqueMap{$item}) { $UniqueMap{$item} = undef; push @result, $item; } } return(@result); } #------------------------------------------------------------------------------ # Constructs a string which is the result of repeating , # times. #------------------------------------------------------------------------------ sub repeat_text { ($#_ == 1) || die "Invalid number of arguments."; my ($repeat_text, $count) = @_; return($repeat_text x $count); } #------------------------------------------------------------------------------ # Places into a justified region of , surrounded by # and characters. The may span several # lines. #------------------------------------------------------------------------------ sub justify_text { sub justify_text_block { ($#_ == 3) || die "Invalid number of arguments."; my ($text, $justify_length, $terminal_left, $terminal_right) = @_; my $length = 0; my $result; $justify_length -= length($terminal_left) + length($terminal_right); my $terminal_left_length = length($terminal_left); while($text) { if ($text =~ /(\s*\S*\s*)/) { my $word = $1; my $pattern = quotemeta($word); $text =~ s/^$pattern//; if ($length == 0) { $result .= $terminal_left; } chomp $word; my $word_length = length($word); if ($word_length > $justify_length) { if ($length) { $result .= repeat_text(" ",$justify_length - $length) . "$terminal_right\n$terminal_left$word$terminal_right\n"; } else { $result .= "$word$terminal_right\n"; } $length = 0; } elsif (($word_length + $length) >= $justify_length) { $result .= repeat_text(" ",$justify_length - $length) . "$terminal_right\n$terminal_left" . $word; $length = $word_length; } else { $result .= $word; $length += $word_length; } } } if ($length && ($length <= $justify_length)) { $result .= repeat_text(" ",$justify_length - $length)."$terminal_right\n"; } return($result); } ($#_ == 3) || die "Invalid number of arguments."; my ($text, $justify_length, $terminal_left, $terminal_right) = @_; my @newline_text = split(m@\n@,$text); my $result = ""; foreach(@newline_text) { $result .= justify_text_block($_,$justify_length,$terminal_left,$terminal_right); } return($result); } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # External functions #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #------------------------------------------------------------------------------ # This method queries the user such that the resultant value will be passed to # a user defined function . If this function returned success, # then the value is accepted and the result is returned. If the function # returns failed, $options{invalid_choice} will be displayed to the user and # the user will be requeried. # # Parameter 1 () is the string which will be prompted to the user # to answer. # # Parameter 2 () is the user defined function which will be # queried for answer validity. # # Parameter 3 - n () are optional parameters which, among # other things, provides a list of options which will be listed alongside the # as valid options. It should be noted that these options must # pass lest a problem will occur. These parameters may also # be keyed option specifiers. For example, "terminal_mark=?" will cause the # terminal character at the end of to be '?'. The following # list of options exists: # terminal_mark=