From 93235430573634473bccad119631e63979b6f053 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Wed, 8 Aug 2018 12:07:44 -0400 Subject: Add open(1) forwarding to pbd --- bin/.gitignore | 1 + bin/Makefile | 4 ++-- bin/pbd.c | 44 ++++++++++++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/bin/.gitignore b/bin/.gitignore index 6dfe7457..5ff66354 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -4,6 +4,7 @@ dtch glitch hnel modem +open pbcopy pbd pbpaste diff --git a/bin/Makefile b/bin/Makefile index ae667eef..677b5673 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,4 +1,4 @@ -ANY_BINS = atch dtch glitch hnel modem pbcopy pbd pbpaste pngo scheme wake xx +ANY_BINS = atch dtch glitch hnel modem open pbcopy pbd pbpaste pngo scheme wake xx BSD_BINS = klon watch LIN_BINS = bri fbatt fbclock ALL_BINS = $(ANY_BINS) $(BSD_BINS) $(LIN_BINS) @@ -21,7 +21,7 @@ tags: *.c atch: dtch ln -f dtch atch -pbcopy pbpaste: pbd +open pbcopy pbpaste: pbd ln -f pbd $@ scheme.png: scheme diff --git a/bin/pbd.c b/bin/pbd.c index 77832433..4ee11fe6 100644 --- a/bin/pbd.c +++ b/bin/pbd.c @@ -21,14 +21,13 @@ #include #include #include +#include #include #include #include #include -#define UNUSED __attribute__((unused)) - -static void spawn(const char *cmd, int dest, int src) { +static void spawn(const char *cmd, const char *arg, int dest, int src) { pid_t pid = fork(); if (pid < 0) err(EX_OSERR, "fork"); @@ -42,7 +41,7 @@ static void spawn(const char *cmd, int dest, int src) { int fd = dup2(src, dest); if (fd < 0) err(EX_OSERR, "dup2"); - execlp(cmd, cmd, NULL); + execlp(cmd, cmd, arg, NULL); err(EX_UNAVAILABLE, "%s", cmd); } } @@ -74,19 +73,21 @@ static int pbd(void) { error = fcntl(client, F_SETFD, FD_CLOEXEC); if (error) err(EX_IOERR, "fcntl"); - spawn("pbpaste", STDOUT_FILENO, client); - - char p; - ssize_t peek = recv(client, &p, 1, MSG_PEEK); - if (peek < 0) err(EX_IOERR, "recv"); + char c = 0; + ssize_t size = read(client, &c, 1); + if (size < 0) warn("read"); - if (peek) spawn("pbcopy", STDIN_FILENO, client); + switch (c) { + break; case 'p': spawn("pbpaste", NULL, STDOUT_FILENO, client); + break; case 'c': spawn("pbcopy", NULL, STDIN_FILENO, client); + break; case 'o': spawn("xargs", "open", STDIN_FILENO, client); + } close(client); } } -static int pbdClient(void) { +static int pbdClient(char c) { int client = socket(PF_INET, SOCK_STREAM, 0); if (client < 0) err(EX_OSERR, "socket"); @@ -98,6 +99,9 @@ static int pbdClient(void) { int error = connect(client, (struct sockaddr *)&addr, sizeof(addr)); if (error) err(EX_UNAVAILABLE, "connect"); + ssize_t size = write(client, &c, 1); + if (size < 0) err(EX_IOERR, "write"); + return client; } @@ -107,30 +111,38 @@ static void copy(int out, int in) { while (0 < (readSize = read(in, buf, sizeof(buf)))) { ssize_t writeSize = write(out, buf, readSize); if (writeSize < 0) err(EX_IOERR, "write(%d)", out); - if (writeSize < readSize) errx(EX_IOERR, "short write(%d)", out); } if (readSize < 0) err(EX_IOERR, "read(%d)", in); } static int pbcopy(void) { - int client = pbdClient(); + int client = pbdClient('c'); copy(client, STDIN_FILENO); return EX_OK; } static int pbpaste(void) { - int client = pbdClient(); - shutdown(client, SHUT_WR); + int client = pbdClient('p'); copy(STDOUT_FILENO, client); return EX_OK; } -int main(int argc UNUSED, char *argv[]) { +static int open1(char *url) { + if (!url) return EX_USAGE; + int client = pbdClient('o'); + ssize_t size = write(client, url, strlen(url)); + if (size < 0) err(EX_IOERR, "write"); + return EX_OK; +} + +int main(int argc, char *argv[]) { + (void)argc; if (!argv[0][0] || !argv[0][1]) return EX_USAGE; switch (argv[0][2]) { case 'd': return pbd(); case 'c': return pbcopy(); case 'p': return pbpaste(); + case 'e': return open1(argv[1]); default: return EX_USAGE; } } -- cgit 1.4.1 >2014-12-28ui-patch: match git-format-patch(1) outputJohn Keeping 2014-12-28t0108: modernize styleJohn Keeping 2014-12-24Revert "git: use xz compressed archive for download"Jason A. Donenfeld 2014-12-24Use split_ident_line() in parse_user()Lukas Fleischer 2014-12-23footer: link back to cgit home pageJason A. Donenfeld 2014-12-23ui-shared: show absolute time in tooltip for relative datesJohn Keeping 2014-12-23git: use xz compressed archive for downloadChristian Hesse 2014-12-23match other common markdown file extensionsChris Burroughs 2014-12-23repolist: add owner-filterChris Burroughs 2014-12-23ui-shared: add rel-vcs microformat links to HTML headerJohn Keeping 2014-12-23ui-summary: add "rel='vcs-git'" to clone URL linksJohn Keeping 2014-12-23Extract clone URL printing to ui-shared.cJohn Keeping 2014-12-23Remove trailing slash after remove-suffixLukas Fleischer 2014-12-23git: update to v2.2.1Christian Hesse 2014-12-13filter: fix libravatar email-filter https issueChristian Hesse 2014-12-13ui-diff: add "stat only" diff typeJohn Keeping 2014-12-13Change "ss" diff flag to an enumJohn Keeping 2014-12-13ui-shared: remove toggle_ssdiff arg to cgit_diff_link()John Keeping 2014-12-13ui-shared: remove toggle_ssdiff arg to cgit_commit_link()John Keeping 2014-08-07git: update to v2.0.4John Keeping 2014-08-07Always check if README exists in choose_readme()Lukas Fleischer 2014-08-01cgitrc.5: we mean a cgi response, not requestJason A. Donenfeld 2014-07-28ui-stats.c: set parent pointer to NULL after freeing itJohn Keeping 2014-07-28git: update to v2.0.3John Keeping 2014-07-28parsing.c: make commit buffer constJohn Keeping 2014-06-30Bump version.Jason A. Donenfeld 2014-06-29remove debug fprinf() calls that sneaked in with commit 79c985Christian Hesse 2014-06-28git: update to 2.0.1Christian Hesse 2014-06-28ui-patch: Flush stdout after outputting dataJohn Keeping 2014-06-28ui-log: ignore unhandled argumentsJohn Keeping 2014-06-28git: update for git 2.0Christian Hesse 2014-04-17remove trailing whitespaces from source filesChristian Hesse 2014-04-12git: update to 1.9.2Christian Hesse 2014-04-05Fix cgit_parse_url when a repo url is contained in another repo urlJulian Maurice 2014-03-20Makefile: use more reliable git tarball mirrorJason A. Donenfeld 2014-03-20git: update to 1.9.1Christian Hesse