diff options
| author | Connor Lane Smith <cls@lubutu.com> | 2015-05-06 17:46:10 +0100 |
|---|---|---|
| committer | Christoph Lohmann <20h@r-36.net> | 2015-05-09 20:03:08 +0200 |
| commit | d60069a3e7a9e382f3221f8e405ad8c05abeea20 (patch) | |
| tree | 457490ce70458e79e7adeb3ec019a2bf74cd93dc /xembed.c | |
| parent | 0728caee306423cd1dc139e000828329c9285ab7 (diff) | |
| download | tabbed-d60069a3e7a9e382f3221f8e405ad8c05abeea20.tar.gz | |
add xembed wrapper utility
xembed will cause a command to attempt to XEmbed into the window given
by the environment variable XEMBED, so long as it is in the foreground
of its controlling terminal. This causes a process to effectively take
the place of the terminal window, unless it is backgrounded.
Signed-off-by: Christoph Lohmann <20h@r-36.net>
Diffstat (limited to 'xembed.c')
| -rw-r--r-- | xembed.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/xembed.c b/xembed.c new file mode 100644 index 0000000..0b36603 --- /dev/null +++ b/xembed.c @@ -0,0 +1,46 @@ +/* + * See LICENSE file for copyright and license details. + */ + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +int +main(int argc, char **argv) +{ + char *xembed; + int tty; + pid_t pgrp, tcpgrp; + + if(argc < 3) { + fprintf(stderr, "usage: %s flag cmd ...\n", argv[0]); + return 2; + } + + if(!(xembed = getenv("XEMBED"))) + goto noembed; + + if((tty = open("/dev/tty", O_RDONLY)) < 0) + goto noembed; + + pgrp = getpgrp(); + tcpgrp = tcgetpgrp(tty); + + close(tty); + + if(pgrp == tcpgrp) { /* in foreground of tty */ + argv[0] = argv[2]; + argv[2] = xembed; + } + else { +noembed: + argv += 2; + } + + execvp(argv[0], argv); + + perror(argv[0]); /* failed to execute */ + return 1; +} |