about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-08-21 15:11:41 +0530
committerAndinus <andinus@nand.sh>2020-08-21 15:11:41 +0530
commitfabb0d70274058e786fbc60af5052233dcff7c52 (patch)
treefc5dd47448ed6f2ee0c9ef413788b58a32a25f95
parenta7bd114aeb9fed876b8fdacd5a5f50d0c02427a5 (diff)
downloadlyra-fabb0d70274058e786fbc60af5052233dcff7c52.tar.gz
Restructure code, remove OpenBSD::Unveil
Moved the main logic to top. This program is too small to benefit from
OpenBSD::Unveil.

    <pardis> unveil doesn't cross execve

That's why `mg' was able to edit the file even when I hadn't asked for
write permission.
-rwxr-xr-xlyra.pl52
1 files changed, 12 insertions, 40 deletions
diff --git a/lyra.pl b/lyra.pl
index 8a4d2b2..3fa0700 100755
--- a/lyra.pl
+++ b/lyra.pl
@@ -7,29 +7,21 @@ use feature 'say';
 use Path::Tiny;
 use IPC::Run3;
 
-use constant is_OpenBSD => $^O eq "openbsd";
-require OpenBSD::Unveil
-    if is_OpenBSD;
-sub unveil {
-    if (is_OpenBSD) {
-        return OpenBSD::Unveil::unveil(@_);
-    } else {
-        return 1;
-    }
-}
-
-# Unveil @INC.
-foreach my $path (@INC) {
-    unveil( $path, 'rx' )
-        or die "Unable to unveil: $!\n";
-}
-
 my %dispatch = (
     "edit" => \&edit,
     "random" => \&random,
     "help" => \&HelpMessage,
 );
 
+if ( $ARGV[0]
+         and $dispatch{ $ARGV[0] } ) {
+    $dispatch{ $ARGV[0] }->();
+} elsif ( scalar @ARGV == 0 ) {
+    HelpMessage();
+} else {
+    say "lyra: no such option";
+}
+
 sub HelpMessage {
     say qq{Usage:
     edit
@@ -40,44 +32,24 @@ sub HelpMessage {
         Show this text.}
 }
 
-sub unveil_fortune_file {
+sub fortune_file_path {
     my $fortune_file = "$ENV{HOME}/quotes.txt";
     $fortune_file = $ARGV[1] if $ARGV[1]; # Use $ARGV[1] as path if it exists.
-    unveil( $fortune_file, "r" )
-        or die "Unable to unveil: $!\n"; # Unveil $path as read-only.
     return $fortune_file;
 }
 
-# Open fortune file with a text editor.
 sub edit {
-    # Unveil $PATH.
-    foreach my $path ( split(/:/, $ENV{PATH}) ) {
-        unveil( $path, "rx" )
-            or die "Unable to unveil: $!\n";
-    }
-
     my @editor = split(/ /, $ENV{EDITOR});
     push @editor, "vi"
         unless scalar @editor; # Push "vi" if @editor is empty.
 
-    my $fortune_file = unveil_fortune_file();
+    my $fortune_file = fortune_file_path();
     run3 [ @editor, $fortune_file ];
 }
 
-# Print a random fortune from $path.
 sub random {
-    my $fortune_file = unveil_fortune_file();
+    my $fortune_file = fortune_file_path();
     my $file = path($fortune_file)->absolute;
     my @fortunes = split/\n%\n/, $file->slurp;
-
     say $fortunes[ rand @fortunes ]; # Print random fortune.
 }
-
-if ( $ARGV[0]
-         and $dispatch{ $ARGV[0] } ) {
-    $dispatch{ $ARGV[0] }->();
-} elsif ( scalar @ARGV == 0 ) {
-    HelpMessage();
-} else {
-    say "lyra: no such option";
-}