From a5567dcff02be9b9b5622a6fee51f6cbb0f76727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 19 Jun 2021 10:59:46 +0200 Subject: Fork and exec instead of calling the shell This handles whitespace in arguments properly. --- dwim | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/dwim b/dwim index 4ee2cf8..cde91fc 100755 --- a/dwim +++ b/dwim @@ -59,7 +59,7 @@ for ($phrase) { my $p = path $1; run @EDITOR, "+/$2", $p if -e $p; fail 'file not found' if $DEBUG; - # otherwise fall through + # FALLTHROUGH } if (/^([-_A-Za-z]+) ?\((\d+)\)[):,.]*$/) { @@ -81,6 +81,7 @@ for ($phrase) { for (`grep -n '^$_' "$dir"/*.c "$dir"/*.h`) { run @EDITOR, "+$2", $1 if /([^:]+):(\d+):/; } + # FALLTHROUGH } if (/^<(\S+)>$/) { @@ -113,13 +114,13 @@ for ($phrase) { run @PDF_VIEWER, $p if -e $p and $p =~ /\.pdf$/; # pdf run @EDITOR, $p if -e $p; # file fail 'file not found' if $DEBUG; - # otherwise fall through + # FALLTHROUGH } - # otherwise die "no handler matched by: $phrase\n" } +# dir -- return directory of current window sub dir { for (`xtitle`) { chomp; @@ -133,6 +134,7 @@ sub dir { die "couldn't retrieve current directory: xtitle not installed\n"; } +# path -- return absolute path to file (and chdir to its directory) sub path { my $n = shift; $n =~ s,^~([^/]+),/home/$1,; @@ -143,7 +145,7 @@ done: chdir dirname($n); return $n; } -# take K => V and return environment variable K if defined, otherwise V +# env -- for (K => V), return environment variable K if defined, otherwise V sub env { my %h = @_; my $k = (keys %h)[0]; @@ -151,26 +153,33 @@ sub env { return $h{$k}; } +# handle -- print debug message when handler matches sub handle { $handler = shift; print STDERR "$handler MATCHED\n" if $DEBUG; } +# fail -- print debug message when matched handler fails sub fail { my $msg = shift; print STDERR "$handler FAILED: $msg\n"; } +# run -- launch program and quit sub run { if ($DEBUG) { my @argv = @_; s/(\s)/\\$1/g for @argv; # escape whitespace print STDERR "@argv\n"; } - system "@_ &"; + if (fork == 0) { + exec @_; + die "could not exec: $1\n"; + } exit; } +# dirname -- return path without last part sub dirname { my $path = shift; $path =~ s,/[^/]+,,; -- cgit v1.2.3