Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple symlinks passing to executable. How to work with? 1

Status
Not open for further replies.

MrCBofBCinTX

Technical User
Dec 24, 2003
164
US
I have a problem with vim-gtk causing an X crash when accidentally starting it outside a terminal. Long story but the window managers are at fault.


I have made a simple script to test for a terminal if vim command is used, if okay, then start renamed vim executable (vim-safe).
# cat /usr/local/bin/vim

#!/bin/sh
test -t 0 && /usr/local/bin/vim-safe $1 $2 $3 $4

Works great, however, vim determines what the actual command is, for example, vim or view or vdiff or gvim or gview or gdiff, etc.

But those commands come through a bunch of symlinks to vim.
There are no other executables.

$ ls /usr/local/bin/gv*
ls /usr/local/bin/gv*
lrwxr-xr-x 1 root wheel 3 Jul 14 16:09 /usr/local/bin/gview -> vim
lrwxr-xr-x 1 root wheel 23 Jul 15 10:13 /usr/local/bin/gvim -> /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 3 Jul 14 16:09 /usr/local/bin/gvimdiff -> vim
-rwxr-xr-x 1 root wheel 143 Jul 4 07:35 /usr/local/bin/gvimtutor


$ ls /usr/local/bin/vi*
lrwxr-xr-x 1 root wheel 3 Jul 14 16:08 /usr/local/bin/view -> vim
-rwxr-xr-x 1 root wheel 61 Jul 15 10:16 /usr/local/bin/vim
-rwxr-xr-x 1 root wheel 1851924 Jul 4 07:35 /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 3 Jul 14 16:08 /usr/local/bin/vimdiff -> vim
-rwxr-xr-x 1 root wheel 2084 Jul 4 07:35 /usr/local/bin/vimtutor


What I can't figure out is how to work it so that I can test vimdiff and view and vimtutor for terminal, since using the script erases that info
 
Anyway, I finished out the easy part, but I still don't see an elegant answer to view and vimdiff.
I am sure that it is enough to move them into a subdirectory, but that is an ugly hack for a not so pretty hack anyway

$ cat /usr/local/bin/vimtutor
#!/bin/sh
test -t 0 && /usr/local/bin/vimtutor-safe $1 $2 $3 $4

$ ls /usr/local/bin/gv*
lrwxr-xr-x 1 root wheel 23 Jul 19 17:12 /usr/local/bin/gview -> /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 23 Jul 15 10:13 /usr/local/bin/gvim -> /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 23 Jul 19 17:13 /usr/local/bin/gvimdiff -> /usr/local/bin/vim-safe
-rwxr-xr-x 1 root wheel 143 Jul 4 07:35 /usr/local/bin/gvimtutor
$ ls /usr/local/bin/vi*
lrwxr-xr-x 1 root wheel 3 Jul 14 16:08 /usr/local/bin/view -> vim
-rwxr-xr-x 1 root wheel 61 Jul 15 10:16 /usr/local/bin/vim
-rwxr-xr-x 1 root wheel 1851924 Jul 4 07:35 /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 3 Jul 14 16:08 /usr/local/bin/vimdiff -> vim
-rwxr-xr-x 1 root wheel 66 Jul 19 17:14 /usr/local/bin/vimtutor
-rwxr-xr-x 1 root wheel 2084 Jul 4 07:35 /usr/local/bin/vimtutor-safe
 
By what means can it be started outside a terminal? From GUI menus or a right-click destktop menu or something?

If so you may be able to add the relevant code to the menu configuration files for the GUI instead.

When writing wrapper scripts a good way to ensure that all parameters are passed intact is this syntax:

Code:
#!/bin/sh
test -t 0 && exec /usr/local/bin/vim-safe "$@"

Also you may as well exec the new binary since the script has no further involvement beyond that point.

Annihilannic.
 
Ah, that was what I was looking for.

I have seen this problem on several i386 computers running OpenBSD. The problem happens with both KDE and scrotwm window managers.

For KDE, run command, with vim instead of gvim will crash X.
For scrotwm, it uses dmenu to start programs, there is a tiny delay which has caused me to enter vim when typing gvim. Crash! Any command entered through dmenu is outside of a terminal at the time.

It is actually a hard problem to fix the window managers, so this was a work around. (and effective)
 
Maybe something like this...
Code:
#!/bin/ksh

case $(basename $0) in
        vim)            /usr/local/bin/gvim $@
                        ;;
        view)           /usr/local/bin/gview $@ 
                        ;;
        vimdiff)        /usr/local/bin/gvimdiff $@
                        ;;
        vimtutor)       /usr/local/bin/gvimtutor $@
                        ;;
        *)              echo "Unknown selection $(basename $0)"
                        ;;
esac
And add in your test if the windows manager is running ot not.

 
The Following seems to work well:
Code:
$ cat /usr/local/bin/vim
#!/bin/sh
test -t 0 && exec /usr/local/bin/vim-safe "$@"
and
Code:
$ cat /usr/local/bin/vimtutor
#!/bin/sh
test -t 0 && exec /usr/local/bin/vimtutor-safe "$@"


$ ls /usr/local/bin/gv*
lrwxr-xr-x 1 root wheel 23 Jul 19 14:33 /usr/local/bin/gview -> /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 23 Jul 19 14:34 /usr/local/bin/gvim -> /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 23 Jul 19 14:34 /usr/local/bin/gvimdiff -> /usr/local/bin/vim-safe
-rwxr-xr-x 1 root wheel 143 Jul 9 23:42 /usr/local/bin/gvimtutor

$ ls /usr/local/bin/vi*
lrwxr-xr-x 1 root wheel 3 Jul 19 06:18 /usr/local/bin/view -> vim
-rwxr-xr-x 1 root wheel 59 Jul 20 13:11 /usr/local/bin/vim
-rwxr-xr-x 1 root wheel 1892356 Jul 9 23:42 /usr/local/bin/vim-safe
lrwxr-xr-x 1 root wheel 3 Jul 19 06:18 /usr/local/bin/vimdiff -> vim
-rwxr-xr-x 1 root wheel 64 Jul 20 13:13 /usr/local/bin/vimtutor
-rwxr-xr-x 1 root wheel 2084 Jul 9 23:42 /usr/local/bin/vimtutor-safe

Any call to g* stuff always works since vim does the appropriate testing and safely falls back to vim if gui won't work.

The only problem is a call to a vim stuff when outside of a terminal inside the buggy window managers.

This seems to be working perfectly now.
Thanks all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top