[hackers] [tabbed] add xembed wrapper utility || Connor Lane Smith

From: <git_AT_suckless.org>
Date: Sat, 9 May 2015 21:06:15 +0200 (CEST)

commit d60069a3e7a9e382f3221f8e405ad8c05abeea20
Author: Connor Lane Smith <cls_AT_lubutu.com>
Date: Wed May 6 17:46:10 2015 +0100

    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_AT_r-36.net>

diff --git a/LICENSE b/LICENSE
index f5244dd..d8e9678 100644
--- a/LICENSE
+++ b/LICENSE
_AT_@ -1,8 +1,8 @@
 MIT/X Consortium License
 
 © 2009-2011 Enno Boland <g s01 de>
-© 2011 Connor Lane Smith <cls_AT_lubutu.com>
-© 2012-2014 Christoph Lohmann <20h_AT_r-36.net>
+© 2011,2015 Connor Lane Smith <cls_AT_lubutu.com>
+© 2012-2015 Christoph Lohmann <20h_AT_r-36.net>
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
diff --git a/Makefile b/Makefile
index 32cc25b..acc3246 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -3,10 +3,11 @@
 
 include config.mk
 
-SRC = tabbed.c
+SRC = tabbed.c xembed.c
 OBJ = ${SRC:.c=.o}
+BIN = ${OBJ:.o=}
 
-all: options tabbed
+all: options ${BIN}
 
 options:
         _AT_echo tabbed build options:
_AT_@ -24,13 +25,13 @@ config.h:
         _AT_echo creating $@ from config.def.h
         _AT_cp config.def.h $@
 
-tabbed: tabbed.o
+.o:
         _AT_echo CC -o $@
- _AT_${CC} -o $@ tabbed.o ${LDFLAGS}
+ _AT_${CC} -o $@ $< ${LDFLAGS}
 
 clean:
         _AT_echo cleaning
- _AT_rm -f tabbed ${OBJ} tabbed-${VERSION}.tar.gz
+ _AT_rm -f ${BIN} ${OBJ} tabbed-${VERSION}.tar.gz
 
 dist: clean
         _AT_echo creating dist tarball
_AT_@ -42,19 +43,23 @@ dist: clean
         _AT_rm -rf tabbed-${VERSION}
 
 install: all
- _AT_echo installing executable file to ${DESTDIR}${PREFIX}/bin
+ _AT_echo installing executable files to ${DESTDIR}${PREFIX}/bin
         _AT_mkdir -p ${DESTDIR}${PREFIX}/bin
- _AT_cp -f tabbed ${DESTDIR}${PREFIX}/bin
+ _AT_cp -f ${BIN} ${DESTDIR}${PREFIX}/bin
         _AT_chmod 755 ${DESTDIR}${PREFIX}/bin/tabbed
- _AT_echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
+ _AT_echo installing manual pages to ${DESTDIR}${MANPREFIX}/man1
         _AT_mkdir -p ${DESTDIR}${MANPREFIX}/man1
         _AT_sed "s/VERSION/${VERSION}/g" < tabbed.1 > ${DESTDIR}${MANPREFIX}/man1/tabbed.1
         _AT_chmod 644 ${DESTDIR}${MANPREFIX}/man1/tabbed.1
+ _AT_sed "s/VERSION/${VERSION}/g" < xembed.1 > ${DESTDIR}${MANPREFIX}/man1/xembed.1
+ _AT_chmod 644 ${DESTDIR}${MANPREFIX}/man1/xembed.1
 
 uninstall:
- _AT_echo removing executable file from ${DESTDIR}${PREFIX}/bin
+ _AT_echo removing executable files from ${DESTDIR}${PREFIX}/bin
         _AT_rm -f ${DESTDIR}${PREFIX}/bin/tabbed
- _AT_echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
+ _AT_rm -f ${DESTDIR}${PREFIX}/bin/xembed
+ _AT_echo removing manual pages from ${DESTDIR}${MANPREFIX}/man1
         _AT_rm -f ${DESTDIR}${MANPREFIX}/man1/tabbed.1
+ _AT_rm -f ${DESTDIR}${MANPREFIX}/man1/xembed.1
 
 .PHONY: all options clean dist install uninstall
diff --git a/tabbed.1 b/tabbed.1
index 83af311..ec6e0b2 100644
--- a/tabbed.1
+++ b/tabbed.1
_AT_@ -156,7 +156,7 @@ See the LICENSE file for the authors.
 .SH LICENSE
 See the LICENSE file for the terms of redistribution.
 .SH SEE ALSO
-.BR st (1)
+.BR st (1),
+.BR xembed (1)
 .SH BUGS
 Please report them.
-
diff --git a/xembed.1 b/xembed.1
new file mode 100644
index 0000000..5b0c28c
--- /dev/null
+++ b/xembed.1
_AT_@ -0,0 +1,35 @@
+.TH XEMBED 1 tabbed\-VERSION
+.SH NAME
+xembed \- XEmbed foreground process
+.SH SYNOPSIS
+.B xembed
+.I flag command
+.RI [ "argument ..." ]
+.SH DESCRIPTION
+If the environment variable XEMBED is set, and
+.B xembed
+is in the foreground of its controlling tty, it will execute
+.IP
+command flag $XEMBED [argument ...]
+.LP
+Otherwise it will execute
+.IP
+command [argument ...]
+.LP
+.SH EXAMPLE
+In a terminal emulator within a
+.B tabbed
+session, the shell alias
+.IP
+$ alias surf='xembed -e surf'
+.LP
+will cause `surf' to open in a new tab, unless it is run in the background,
+i.e. `surf &', in which case it will instead open in a new window.
+.SH AUTHORS
+See the LICENSE file for the authors.
+.SH LICENSE
+See the LICENSE file for the terms of redistribution.
+.SH SEE ALSO
+.BR tabbed (1)
+.SH BUGS
+Please report them.
diff --git a/xembed.c b/xembed.c
new file mode 100644
index 0000000..0b36603
--- /dev/null
+++ b/xembed.c
_AT_@ -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;
+}
Received on Sat May 09 2015 - 21:06:15 CEST

This archive was generated by hypermail 2.3.0 : Sat May 09 2015 - 21:12:15 CEST