Plan 9 from Bell Labs’s /usr/web/sources/contrib/mycroftiv/root/sys/src/cmd/iosrv/io

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#!/bin/rc

# server+client dual-mode wrapper script for iosrv functionality
# requires iosrv and ioshell installed 

# if a /srv exists matching $1, or the default iorc.$pid, we are a client - otherwise start a server
testin = $1
testdef = `{ls /srv/ |grep iorc}
if (! ~ $testin ''){
	if (! ~ $testin -*){
		testsrv = `{ls /srv |grep $testin}
	}
	if (~ $testsrv ?*){
		client = yes
	}
}
if (~ $testin ''){
	if (~ $testdef ?*){
		client = yes
	}
}

if(~ $client yes){
# attach to an existing iosrv rc service
	name = $1
	shift
	if (~ $name ''){
		name = `{ls /srv |grep iorc |awk -F / '{print $3}'}
	}
	if (! test -e /srv/$name){
		echo '/srv/'$name' does not exist but a variant does, please recheck your command'
		ls /srv |grep $name
		exit
	}
	while(~ $1 ??*){
		switch($1){
		case zap
			clear = yes
			shift
		case slow
			pokey = yes
			shift
		case fast
			zoom = yes
			shift
		case here
			makenew = yes
			shift
		case *
			shift
		}
	}
	if (! ~ $#name 1) {
		echo multiple possible targets please select only one
		exit
	}
	mount -c /srv/$name /n/$name
	echo rawon >/dev/consctl >[2]/dev/null &
	echo scroll >/mnt/wsys/wctl >[2]/dev/null &
	sleep 1
	if (~ $makenew yes){
		newstart = `{cat /n/$name/trihubs}
		echo $newstart trihubs exist already creating new and attaching
		nextnum = `{echo $newstart +1 |hoc}
		echo $nextnum >/n/$name/trihubs
		fd0 = `{echo $newstart '* 3' |hoc}
		fd1 = `{echo  $fd0 +1 |hoc}
		fd2 = `{echo $fd1 + 1 |hoc}
		targctl = /n/$name/ctl/data
		echo 1 >/n/$name/attach$fd0
		echo target files $fd0 $fd1 $fd2 $targctl
		echo 'h'$fd0's'0 >$targctl
		echo 'h'$fd1's'0 >$targctl
		echo 'h'$fd2's'0 >$targctl
		echo 'h'$fd0'o'0 >$targctl
		echo 'h'$fd1'o'0 >$targctl
		echo 'h'$fd2'e'0 >$targctl
		sleep 2
		rfork
		rc -i </n/$name/'H'$fd0'out'0'/data1' >/n/$name/'H'$fd1'in'0'/data' >[2]/n/$name/'H'$fd2'in'0'/data' &
		rfork
		ioshell /n/$name/'H'$fd0'in'0'/data' /n/$name/'H'$fd1'out'0'/data1' /n/$name/'H'$fd2'err'0'/data1' $targctl
		exit
	}
	number = `{cat /n/$name/attach0}
	if (~ $number ''){
		echo 'didnt find a session number to use, attempting high arbitrary number'
		number = 49
	}
	if (~ $clear yes){
		echo h1c >/n/$name/ctl/data
		sleep 1
		echo h2c >/n/$name/ctl/data
		sleep 1
		echo H1 and H2 buffer cleared
	}
	if (~ $pokey yes){
		echo h1t110 >/n/$name/ctl/data
		echo h2t110 >/n/$name/ctl/data
		echo 'slow speed on H1 and H2 - 110 ms sleeptime'
	}
	if (~ $zoom yes){
		echo h0t0 >/n/$name/ctl/data
		echo h1t0 >/n/$name/ctl/data
		echo h2t25 >/n/$name/ctl/data
		echo 'fast speed - no delay on fd 0 and 1, 25 ms on fd 2'
	}
	echo h0i$number >/n/$name/ctl/data
	echo h1o$number >/n/$name/ctl/data
	echo h2e$number >/n/$name/ctl/data
	echo h2t40 >/n/$name/ctl/data
	outnumber = `{echo $number +1 |hoc}
	echo $outnumber >/n/$name/attach0
	ioshell /n/$name/H0in$number/data /n/$name/H1out$number/data1 /n/$name/H2err$number/data1 /n/$name/ctl/data
	exit
}

# arrival here means we are making a server
# setup persistent rc or command using iosrv

echo rawon >/dev/consctl >[2]/dev/null &
iocmd = 'iosrv'

# some flags here are undocumented pass-through flags for iosrv options such as not starting srvfs or ramfs

while (~ $1 -*){
	switch($1){
	case -a
		attach = yes
		shift
	case -i
		interactive = yes
		log = yes
		shift
	case -l
		log = yes
		writelog = yes
		if (! ~ $3 ''){
			logfile = $2
			shift
		}
		if (~ $logfile -*){
			echo logfile does not like $1 as a logfile name please reformat your command
			echo 'USAGE: io [ -a -i -v -p ] [-l [ logfile ] ] [ -c command ] srvname'
			exit
		}
		if (~ $logfile ''){
			logfile = '/tmp/iolog'$pid
		}
		echo logging to $logfile
		shift
	case -h
		iocmd = $iocmd^' -h'
		shift
	case -p
		iocmd = $iocmd^' -p'
		shift
	case -r
		iocmd = $iocmd^' -r'
		shift
	case -s
		iocmd = $iocmd^' -s'
		shift
	case -v
		iocmd = $iocmd^' -v'
		shift
	case -c
		noshell = yes
		xcmd = `{echo $2}
		echo executing $xcmd instead of rc -i as content of initial iosrv pipes
		shift
		shift
	case *
		echo bad flag $1
		shift
	}
}
name = $1
shift
if (~ $name '') {
	name = iorc.$pid
}
if(test -e /srv/$name){
	echo '/srv/'$name' already exists, please choose a different name (if you were trying to connect to that iosrv, do so without flags)'
	exit
}
iocmd = $iocmd^' '$name
if (! ~ $log yes){
#	echo 'attempting to set window to scroll'
	echo scroll >/mnt/wsys/wctl >[2] /dev/null &
	iocmd = $iocmd^' >/dev/null'
}
if (~ $writelog yes){
	iocmd = $iocmd^' >'$logfile
}
iocmd = $iocmd^' &'
echo starting $iocmd
rfork
rc -c $iocmd &
sleep 3
rfork
echo 1 >/tmp/$name/attach0
echo 1 >/tmp/$name/trihubs
if (! ~ $noshell yes){
	echo placing rc inside iosrv session in 5 seconds
	mount -c /srv/$name /n/$name
	sleep 1
	echo h0s0 >/n/$name/ctl/data
	echo h1s0 >/n/$name/ctl/data
	echo h2s0 >/n/$name/ctl/data
	sleep 2
	echo h0o0 >/n/$name/ctl/data
	sleep 3
	oldprompt = $prompt
	prompt =(io%' ')
	rc -i </n/$name/H0out0/data1 >/n/$name/H1in0/data >[2]/n/$name/H2in0/data &
	prompt = $oldprompt
	rfork
}
if (~ $noshell yes){
	echo executing $xcmd with redirections from $name pipes
	mount -c /srv/$name /n/$name
	sleep 1
	echo h0s0 >/n/$name/ctl/data
	echo h1s0 >/n/$name/ctl/data
	echo h2s0 >/n/$name/ctl/data
	sleep 2
	echo h0o0 >/n/$name/ctl/data
	sleep 2
	$xcmd </n/$name/H0out0/data1 >/n/$name/H1in0/data >[2]/n/$name/H2in0/data &
	rfork
}
if (~ $interactive yes){
	echo 'interactive control mode: displaying log and attaching to ctl input'
	echo scroll >/mnt/wsys/wctl >[2]/dev/null &
	cat >/tmp/$name/ctl/data
}
if (~ $attach yes){
	echo connecting to io pipes as client
	rfork 
	io $name $*
}	
exit


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.