commit 4cf6ba137fc2ca92f3066c390b89542d88735ef6 from: Omar Polo date: Fri Feb 11 18:12:17 2022 UTC remote open: open an url into telescope from outside of it if telescope is started with an url while there is already another instance of it running and the -S flag is not provided, the link will be automagically opened into the running instance of telescope. Telescope now listens on a UNIX domain socket in ~/.telescope/ctl (or ~/.cache/telescope/ctl if XDG is used) for commands. commit - 0f559c556ffae888dccd908739c09005bedbf6d0 commit + 4cf6ba137fc2ca92f3066c390b89542d88735ef6 blob - 265e705c9a2c4a1917bddc5d07f838847b635c6e blob + d7293171fd6f3b450a3e0c8b472bf8acaa83e432 --- Makefile.am +++ Makefile.am @@ -7,6 +7,7 @@ EXTRA_telescope_SOURCES = compat/ohash.h compat/queue. telescope_SOURCES = cmd.c \ cmd.gen.c \ compl.c \ + control.c \ defaults.c \ downloads.c \ fs.c \ @@ -16,6 +17,7 @@ telescope_SOURCES = cmd.c \ include/cmd.h \ include/compat.h \ include/compl.h \ + include/control.h \ include/defaults.h \ include/fs.h \ include/keymap.h \ blob - eb0e0e72278479b0d75de55646689c421aa9b1df blob + f7631998b7a0f5abf1fc2d827c8610be026c3ff0 --- fs.c +++ fs.c @@ -75,6 +75,7 @@ char config_path_base[PATH_MAX]; char data_path_base[PATH_MAX]; char cache_path_base[PATH_MAX]; +char ctlsock_path[PATH_MAX]; char config_path[PATH_MAX]; char lockfile_path[PATH_MAX]; char bookmark_file[PATH_MAX]; @@ -673,6 +674,8 @@ fs_init(void) { init_paths(); + join_path(ctlsock_path, cache_path_base, "/ctl", + sizeof(ctlsock_path)); join_path(config_path, config_path_base, "/config", sizeof(config_path)); join_path(lockfile_path, cache_path_base, "/lock", blob - c1a0fce9a9af9b827c7f23a1ee95b36269b4cd3f blob + 6337fed6e6cfa36597a25e71245cf9b17eec0d16 --- include/fs.h +++ include/fs.h @@ -25,6 +25,7 @@ extern char config_path_base[PATH_MAX]; extern char data_path_base[PATH_MAX]; extern char cache_path_base[PATH_MAX]; +extern char ctlsock_path[PATH_MAX]; extern char config_path[PATH_MAX]; extern char lockfile_path[PATH_MAX]; extern char bookmark_file[PATH_MAX]; blob - 23b5fe74d9e6ef4a90f187460e9620a548bc523d blob + 66bad0bf2f576e29b0f3724503635a7823945e35 --- include/telescope.h +++ include/telescope.h @@ -75,6 +75,8 @@ enum imsg_type { IMSG_SESSION_TAB, IMSG_SESSION_TAB_HIST, IMSG_SESSION_END, + + IMSG_CTL_OPEN_URL, }; enum line_type { blob - 4ce510af9bce453b372b9f7fc77068b879b2b563 blob + 0f2c57ee4215b0e220bf6d9e0f1a1f0ae42ae4c6 --- sandbox.c +++ sandbox.c @@ -37,7 +37,7 @@ sandbox_net_process(void) void sandbox_ui_process(void) { - if (pledge("stdio tty recvfd", NULL) == -1) + if (pledge("stdio tty unix recvfd", NULL) == -1) err(1, "pledge"); } blob - cce3b0a13dbdcac30ab92ff4f795cb0eedf830ca blob + 61f4fec2fae4c283a874d775710363ada79f4885 --- telescope.c +++ telescope.c @@ -17,6 +17,7 @@ #include "compat.h" #include +#include #include #include @@ -27,6 +28,7 @@ #include #include +#include "control.h" #include "defaults.h" #include "fs.h" #include "mcache.h" @@ -136,6 +138,7 @@ static int make_request(struct tab *, struct get_req static int make_fs_request(struct tab *, int, const char *); static int do_load_url(struct tab *, const char *, const char *, int); static pid_t start_child(enum telescope_process, const char *, int); +static void send_url(const char *); static struct proto { const char *schema; @@ -1140,6 +1143,30 @@ start_child(enum telescope_process p, const char *argv argv[argc++] = NULL; execvp(argv0, (char *const *)argv); err(1, "execvp(%s)", argv0); +} + +static void +send_url(const char *url) +{ + struct sockaddr_un sun; + struct imsgbuf ibuf; + int ctl_sock; + + if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + err(1, "socket"); + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path)); + + if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) + err(1, "connect: %s", ctlsock_path); + + imsg_init(&ibuf, ctl_sock); + imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url, + strlen(url) + 1); + imsg_flush(&ibuf); + close(ctl_sock); } int @@ -1171,6 +1198,7 @@ main(int argc, char * const *argv) { struct imsgev net_ibuf, fs_ibuf; pid_t pid; + int control_fd; int pipe2net[2], pipe2fs[2]; int ch, configtest = 0, fail = 0; int proc = -1; @@ -1261,9 +1289,15 @@ main(int argc, char * const *argv) (download_path = strdup("/tmp/")) == NULL) errx(1, "strdup"); - if (!safe_mode && (sessionfd = lock_session()) == -1) + if (!safe_mode && (sessionfd = lock_session()) == -1) { + if (has_url) { + send_url(url); + exit(0); + } + errx(1, "can't lock session, is another instance of " "telescope already running?"); + } /* Start children. */ if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1) @@ -1287,6 +1321,12 @@ main(int argc, char * const *argv) event_init(); + if (!safe_mode) { + if ((control_fd = control_init(ctlsock_path)) == -1) + err(1, "control_init %s", ctlsock_path); + control_listen(control_fd); + } + /* initialize the in-memory cache store */ mcache_init();