diff options
Diffstat (limited to 'pictor.pl')
-rw-r--r-- | pictor.pl | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/pictor.pl b/pictor.pl new file mode 100644 index 0000000..6d4a523 --- /dev/null +++ b/pictor.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; +use OpenBSD::Pledge; +use OpenBSD::Unveil; + +pledge( qw( rpath unveil ) ) or + die "Unable to pledge: $!"; + +# $term will store the user input. +my $term; + +# User must pass at least one argument. +if (@ARGV < 1) { + say STDERR "usage: wtf.pl term"; + exit 1; +} else { + $term = $ARGV[0]; +} + +# files contains list of all files to search for acronyms. +my @files = ( + '/usr/local/share/misc/acronyms', + '/usr/local/share/misc/acronyms-o', + '/usr/local/share/misc/acronyms.comp' + ); + +# Unveil each file with only read permission. +foreach my $file (@files) { + unveil( $file, "r" ) or + die "Unable to unveil: $!"; +} + +# Block further unveil calls. +unveil() or + die "Unable to lock unveil: $!"; + +# drop pledge permissions +pledge( qw( rpath )) or + die "Unable to pledge: $!"; + +# Search for acronym in every file. +foreach my $file (@files) { + open my $fh, '<', $file or + # The program should continue if the file doesn't exist but + # warn the user about it. + do { + warn "Unable to open $file: $!"; + next; + }; + + while (my $line = readline $fh) { + # \Q is quotemeta, \E terminates it because otherwise it would + # mess with \s. This regex matches when $line starts with + # "$term\s", \s being any kind of whitespace. + print $line if ($line =~ /^\Q${term}\E\s/i); + } +} + +# drop pledge permissions +pledge() or + die "Unable to pledge: $!"; |