aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-29 12:01:38 +0100
committerRobin Haberkorn <rhaberkorn@fmsbw.de>2025-12-30 02:42:34 +0300
commita7202a1fb911f72c309380b42c0ff995c05ba94c (patch)
treeb808fc297e4cf4dd719778f57dd54c5928986460
parentd48439b1e81cc0d0835c63a8bac14562b4f4b5c7 (diff)
GTK: implemented --detach|-d option for detaching from controlling terminalmaster-fmsbw-ci
This is useful to launch from a terminal without "blocking" this terminal. There are tools like nohup and daemonize (BSD) to do the same, but having it builtin is shorter to write.
-rw-r--r--TODO8
-rw-r--r--configure.ac4
-rw-r--r--src/interface-gtk/interface.c29
-rw-r--r--tests/testsuite.at9
4 files changed, 40 insertions, 10 deletions
diff --git a/TODO b/TODO
index e613582..720cc6d 100644
--- a/TODO
+++ b/TODO
@@ -781,14 +781,6 @@ Features:
in multi-line command lines.
This would be tricky to do in Gtk, though since we're currently using
an overlay.
- * GTK: gsciteco --detach|-d to detach from the running terminal
- (ie. daemonize).
- Will help to occupy less tabs in the terminal emulator.
- In most shells you can type `gsciteco &>/dev/null & disown`, but having
- a shortcut probably makes sense.
- On FreeBSD you could also `daemonize /usr/local/bin/gsciteco`.
- There is --xembed but there are no stable terminal emulators
- with Xembed support.
* <EB> could create directories on demand and clean them up on rubout.
Unless additional files appeared in the meantime, in which case we should
output a warning.
diff --git a/configure.ac b/configure.ac
index c0e9939..31fd8fb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -213,7 +213,9 @@ AC_CHECK_FUNCS([memset setlocale strchr strrchr fstat sscanf], , [
# glib defines G_OS_UNIX instead...
case $host in
*-*-linux* | *-*-*bsd* | *-*-darwin* | *-*-cygwin* | *-*-haiku*)
- AC_CHECK_FUNCS([realpath readlink pathconf fchown dup dup2 getpid open read kill mmap popen pclose isatty], , [
+ # NOTE: Keep this on a single line for compatibility
+ # with ancient versions of Autoconf.
+ AC_CHECK_FUNCS([realpath readlink pathconf fchown dup dup2 getpid open read kill mmap popen pclose isatty fork setsid], , [
AC_MSG_ERROR([Missing libc function])
])
AC_SEARCH_LIBS(dladdr, [dl], , [
diff --git a/src/interface-gtk/interface.c b/src/interface-gtk/interface.c
index a86e3fd..32370d9 100644
--- a/src/interface-gtk/interface.c
+++ b/src/interface-gtk/interface.c
@@ -19,6 +19,7 @@
#include "config.h"
#endif
+#include <stdlib.h>
#include <string.h>
#include <signal.h>
@@ -27,6 +28,7 @@
#include <glib/gstdio.h>
#ifdef G_OS_UNIX
+#include <unistd.h>
#include <glib-unix.h>
#endif
@@ -105,7 +107,7 @@ static struct {
/* current document's name or empty string for "(Unnamed)" buffer */
teco_string_t info_current;
- gboolean no_csd;
+ gboolean no_csd, detach;
gint xembed_id;
GtkWidget *info_bar_widget;
@@ -132,6 +134,26 @@ static struct {
void
teco_interface_init(void)
{
+#ifdef G_OS_UNIX
+ if (teco_interface.detach) {
+ /*
+ * NOTE: There is also daemon() on BSD/Linux,
+ * but the following should be more portable.
+ */
+ pid_t pid = fork();
+ g_assert(pid >= 0);
+ if (pid != 0)
+ /* parent process */
+ exit(EXIT_SUCCESS);
+
+ setsid();
+
+ g_freopen("/dev/null", "r", stdin);
+ g_freopen("/dev/null", "a+", stdout);
+ g_freopen("/dev/null", "a+", stderr);
+ }
+#endif
+
/*
* gtk_init() is not necessary when using gtk_get_option_group(),
* but this will open the default display.
@@ -341,6 +363,11 @@ teco_interface_get_options(void)
G_OPTION_ARG_INT, &teco_interface.xembed_id,
"Embed into an existing X11 Window.", "ID"},
#endif
+#ifdef G_OS_UNIX
+ {"detach", 'd', G_OPTION_FLAG_IN_MAIN,
+ G_OPTION_ARG_NONE, &teco_interface.detach,
+ "Detach from controlling terminal (daemonize).", NULL},
+#endif
{NULL}
};
diff --git a/tests/testsuite.at b/tests/testsuite.at
index a360840..09c4df2 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -460,6 +460,15 @@ AT_CHECK([[$SCITECO -e "@EI'^EQ[\$SCITECOPATH]/opener.tes' M[opener] EF .-11\"N(
AT_CHECK([[$SCITECO -e "@EI'^EQ[\$SCITECOPATH]/opener.tes' M[opener] EF EJ-2\"N(0/0)'" -S +1 data.123]], 0, ignore, ignore)
AT_CLEANUP
+AT_BANNER([Program features])
+
+AT_SETUP([Detaching from terminal])
+AT_SKIP_IF([$SCITECO --help | $GREP -qvz '\-\-detach'])
+AT_CHECK([$SCITECO --detach -e '23= @EW/test.txt/' && sleep 1], 0, [], [])
+# FIXME: What if the "daemon" takes longer than 1 second to write test.txt?
+AT_FAIL_IF([! test -f test.txt])
+AT_CLEANUP
+
AT_BANNER([Regression Tests])
AT_SETUP([Glob patterns with character classes])