use Fcntl; use Term::ReadKey; #----------------------------( promptUser )-----------------------------# # # # FUNCTION: promptUser # # # # PURPOSE: Prompt the user for some type of input, and return the # # input back to the calling program. # # # # ARGS: $promptString - what you want to prompt the user with # # $defaultValue - (optional) a default value for the prompt # # # #-------------------------------------------------------------------------# $PROMPT_NOECHO = 1; sub promptUser { #-------------------------------------------------------------------# # two possible input arguments - $promptString, and $defaultValue # # make the input arguments local variables. # #-------------------------------------------------------------------# local($promptString,$defaultValue, $flags) = @_; #-------------------------------------------------------------------# # if there is a default value, use the first print statement; if # # no default is provided, print the second string. # #-------------------------------------------------------------------# if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; } $| = 1; # force a flush after our print if ($flags and ($flags & $PROMPT_NOECHO)) { ReadMode('noecho'); $_ = ReadLine(0); ReadMode(0); print "\n"; chomp; } else { $_ = ; # get the input from STDIN (presumably the keyboard) } #------------------------------------------------------------------# # remove the newline character from the end of the input the user # # gave us. # #------------------------------------------------------------------# chomp; #-----------------------------------------------------------------# # if we had a $default value, and the user gave us input, then # # return the input; if we had a default, and they gave us no # # no input, return the $defaultValue. # # # # if we did not have a default value, then just return whatever # # the user gave us. if they just hit the key, # # the calling routine will have to deal with that. # #-----------------------------------------------------------------# if (defined($defaultValue) && "$defaultValue") { return $_ ? $_ : $defaultValue; # return $_ if it has a value } else { return $_; } } $infile = $ARGV[0]; printf("infile='$infile'\n"); $infile = promptUser("PDF file to encrypt - ", $infile); sysopen(INFILE, $infile, O_RDONLY) || die $!; close(INFILE); if ($infile =~ /\.pdf$/i) { $outfile = $` . '-x' . '.pdf'; } else { $outfile = $infile . '.pdf'; } $outfile = promptUser("output file - ", $outfile); $oldpass = promptUser("existing password - ", '', $PROMPT_NOECHO); $pass = "foo"; $confirm = ''; while ($pass ne $confirm) { $pass = promptUser("new password - ", $pass, $PROMPT_NOECHO); $confirm = promptUser("confirm new password - ", '', $PROMPT_NOECHO); if ($pass ne $confirm) { print " no match\n"; } } $ok = ""; while (1) { $ok = promptUser("Encrypt '$infile' to '$outfile'? ", "no"); exit if $ok eq "no" || $ok eq "n" || $ok eq "N"; last if ($ok eq "y" || $ok eq "Y" || $ok eq "yes"); } # qpdf --password=xxxxxxxx --encrypt xxxxxxxx xxxxxxxx 128 --use-aes=y -- \ # input.pdf output.pdf if (1) { # this requires a known path $qpdf='/usr/local/bin/qpdf/qpdf'; ## edit the path as needed @args = ($qpdf); if (length($oldpass) > 0) { push @args, "--password=$oldpass"; } push @args, '--encrypt', $pass, $pass; push @args, qw/128 --use-aes=y --/ ; push @args, $infile, $outfile; } else { # let the shell figure out the path @args = qw,/bin/sh -c,; $x = 'qpdf '; if (length($oldpass) > 0) { $x .= "--password='$oldpass' "; } $x .= "--encrypt '$pass' '$pass' "; $x .= '128 --use-aes=y -- ' ; $x .= "'$infile' '$outfile' "; push @args, $x; } $ok = ""; while (1) { $ok = promptUser("Show the command w/ passwords? ", "no"); last if $ok eq "no" || $ok eq "n" || $ok eq "N"; if ($ok eq "y" || $ok eq "Y" || $ok eq "yes") { print join ' ',@args,"\n"; last; } } $ok = ""; $ok = promptUser("Press Enter to proceed - ", ""); #exit 0; system(@args);