diff options
author | John Ankarström <john@ankarstrom.se> | 2021-06-19 10:59:46 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-06-19 10:59:46 +0200 |
commit | a5567dcff02be9b9b5622a6fee51f6cbb0f76727 (patch) | |
tree | 6ea04e48e9087fe80fa1def71e6a5fc77ef81db3 | |
parent | 3781b4c6935a64ec0a6e505b0b40d7a9c2ab3790 (diff) | |
download | dwim-a5567dcff02be9b9b5622a6fee51f6cbb0f76727.tar.gz |
Fork and exec instead of calling the shell
This handles whitespace in arguments properly.
-rwxr-xr-x | dwim | 19 |
1 files changed, 14 insertions, 5 deletions
@@ -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,/[^/]+,,; |