summary refs log tree commit diff
path: root/daemon.h
diff options
context:
space:
mode:
Diffstat (limited to 'daemon.h')
-rw-r--r--daemon.h43
1 files changed, 37 insertions, 6 deletions
diff --git a/daemon.h b/daemon.h
index db82ee2..7290420 100644
--- a/daemon.h
+++ b/daemon.h
@@ -14,6 +14,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <errno.h>
 #include <grp.h>
 #include <pwd.h>
 #include <stdint.h>
@@ -49,6 +50,42 @@ static inline int prependAdd(const char *command) {
 	return 0;
 }
 
+struct Line {
+	size_t len;
+	char buf[512];
+};
+
+static inline char *lineFlush(struct Line *line) {
+	if (!line->len) return NULL;
+	line->buf[line->len++] = '\0';
+	return line->buf;
+}
+
+static inline char *lineRead(struct Line *line, int fd) {
+	char *nul = memchr(line->buf, '\0', line->len);
+	if (nul) {
+		nul++;
+		line->len -= nul - line->buf;
+		memmove(line->buf, nul, line->len);
+	}
+
+	size_t cap = sizeof(line->buf) - line->len - 1;
+	if (!cap) return lineFlush(line);
+
+	ssize_t len = read(fd, &line->buf[line->len], cap);
+	if (len < 0 && errno != EAGAIN) return NULL;
+	if (len > 0) line->len += len;
+
+	char *nl = memchr(line->buf, '\n', line->len);
+	if (nl) {
+		*nl = '\0';
+		return line->buf;
+	} else {
+		errno = EAGAIN;
+		return NULL;
+	}
+}
+
 enum {
 	SHELL,
 	PATH,
@@ -70,12 +107,6 @@ enum State {
 	Restart,
 };
 
-enum { LineCap = 512 };
-struct Line {
-	size_t len;
-	char buf[LineCap];
-};
-
 struct Service {
 	char *name;
 	char *command;
0810b290ed48e617e6fe12&follow=1'>Switch gr alias back to git rebaseJune McEnroe 2020-10-27Allow cd host: to cd to same path over sshJune McEnroe 2020-10-27Use SendEnv for cd host:pathJune McEnroe 2020-10-27Allow cd host:path over sshJune McEnroe 2020-10-07Use mandoc -T utf8 for text.June McEnroe 2020-09-20Add The Awakened KingdomJune McEnroe 2020-09-12Move /opt/local back, cheat port select to use system manJune McEnroe 2020-09-12Move /opt/local behind /usr againJune McEnroe 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe 2020-09-07Add SunglassesJune McEnroe 2020-09-06Add Between the BreathsJune McEnroe 2020-09-04Open /dev/tty in nudgeJune McEnroe 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe 2020-08-29Add tweets from retweetsJune McEnroe