shithub: mq

Download patch

ref: 62b20ac8c055336c2b9506ca742a652be410d8f9
parent: 3ea628211585265e010bd3ec888b85a23831cf58
author: kvik <kvik@a-b.xyz>
date: Mon Oct 5 09:04:20 EDT 2020

pin: make command line interface more explicit

--- a/man/1/pin
+++ b/man/1/pin
@@ -4,12 +4,12 @@
 .SH SYNOPSIS
 pin
 [
-.B -nw
+.B -acw
 ] [
 .B -m
 .I mtpt
 ] [
-.I name
+.I id
 ] [
 .I command
 ]
@@ -22,27 +22,26 @@
 .IR mq (4)
 stream group.
 .PP
-If a session
-.I name
-exists, then
-.B pin mysession
-will join the named session,
-otherwise a new session will be created.
-The command run in the session is
-.IR command ,
+The 
+.B -c
+flag creates a session
+.I id
+running
+.I command,
 or
 .B rc -i
 if command is not given.
 .PP
 The
-.B -n
-flag supresses joining of existing session.
+.B -a
+flag attaches to a session
+.I id.
 .PP
 The -w
 flag writes the
 .I command
 string, or standard input if not given, to
-the input stream of an existing session.
+a session's input stream.
 .PP
 Under the hood,
 .I pin
@@ -64,21 +63,21 @@
 .PP
 Pin an
 .BR rc (1)
-shell, or join an already existing session:
+shell and attach to it
 .EX
-	pin myshell
+	pin -ca myshell
 .EE
 .PP
-Pin some other program:
+Pin another program
 .EX
-	pin remote rcpu -h remote
+	pin -c remote rcpu -h remote
 .EE
 .PP
-Send input to a pinned program:
+Send input to a pinned program
 .EX
 	pin -w myshell echo 'hi, shell'
 .EE
-or:
+or
 .EX
 	echo 'echo hi, shell' | pin -w myshell
 .EE
--- a/rc/pin
+++ b/rc/pin
@@ -1,39 +1,7 @@
 #!/bin/rc
 rfork e
 
-flagfmt = 'n:noattach, w:write, m:mtpt mtpt'
-args = 'name [command]'
-eval `''{aux/getflags $*} || exec aux/usage
-
-srv = pin-$user
-name = $1
-if(~ $#name 0)
-	exec aux/usage
-cmd = $*(2-)
-if(~ $#cmd 0){
-	nocmd = 1
-	cmd = (rc -i)
-}
-if(~ $#mtpt 0)
-	mtpt = /n/pin
-
-# Check if already mounted
-if(! ~ `{walk -n0 -eq $mtpt} a000000000000000.0.80){
-	if(test -e /srv/$srv)
-		mount -c /srv/$srv $mtpt
-	if not
-		mq -s $srv -m $mtpt
-}
-
-mq = $mtpt/$name
-if(~ $#write 1){
-	if(~ $nocmd 1)
-		exec cat >>$mq/0
-	if not
-		exec echo $cmd >>$mq/0
-}
-
-fn detach {
+fn create {
 	echo creating $mq >[1=2]
 
 	mq = $1
@@ -81,7 +49,43 @@
 		wait $apid
 }
 
-if(! test -d $mq)
-	detach $mq
-if(~ $#noattach 0)
+flagfmt = 'a:attach, c:create, w:write, m:mtpt mtpt'
+args = 'id [command]'
+eval `''{aux/getflags $*} || exec aux/usage
+
+srv = pin-$user
+id = $1
+if(~ $#id 0)
+	exec aux/usage
+cmd = $*(2-)
+if(~ $#cmd 0){
+	nocmd = 1
+	cmd = (rc -i)
+}
+if(~ $#mtpt 0)
+	mtpt = /n/pin
+
+# Run and mount if not already mounted
+if(! ~ `{walk -n0 -eq $mtpt} a000000000000000.0.80){
+	if(test -e /srv/$srv)
+		mount -c /srv/$srv $mtpt
+	if not
+		mq -s $srv -m $mtpt
+}
+
+mq = $mtpt/$id
+if(~ $#write 1){
+	if(~ $nocmd 1)
+		exec cat >>$mq/0
+	if not
+		exec echo $cmd >>$mq/0
+}
+if(~ $#create 1){
+	if(test -d $mq){
+		echo $mq already exists >[1=2]
+		exit 'pin exists'
+	}
+	create $mq
+}
+if(~ $#attach 1)
 	attach $mq