;-*-midas-*-

title ports-> print number of free net ports.
;Stolen from RMS's LOADP, and prettied up a bit.

a=1
b=2
c=3
d=4
e=5
f=6
g=7
h=10
i=11
j=12
k=13
l=14
m=15
n=16
p=17
t1=20
t2=21
chttyo==1

define syscal a,b
	.call [setz ? sixbit/a/ ? b ((setz))]
termin
DEFINE TCAL OP,ARGS
	.CALL [SETZ ? SIXBIT /OP/ ? ARGS ((SETZ))]
TERMIN
DEFINE TYPE CHNL,&STRING
	MOVEI T1,<.LENGTH STRING>
	MOVE T2,[440700,,[ASCII STRING]]
	TCAL SIOT,[%CLIMM,,CHNL ? T2 ? T1]
	  .LOSE %LSFIL
TERMIN



beg:	move p,[-lpdl,,pdl]
	.open chttyo,[.uao,,'tty]
	 .lose
	movei b,0		;B indexes which tty we are looking at.
	setzm nfstys		;Initialize counters: # of free stys
count:	syscal ttyget,[%climm,,400000(b) ;D gets TTYST var - negative for free tty.
			%clout,,a ? %clout,,a ? %clout,,d]
	 jrst print		;Error means we have run out of tty numbers.
	syscal ttyvar,[%climm,,400000(b) ? [sixbit/ttytyp/]
			%clout,,a]
	 .lose %lsfil
	trnn a,%tysty		;If it's a sty, is it free?
	 jrst notfre
	jumpge d,notfre
	aos nfstys
notfre:	aoja b,count		;ok, it isn't free, ignore it.

ascout:	hrli a,440700
ascou1:	ildb b,a
	jumpe b,cpopj
	.iot chttyo,b
	jrst ascou1

decout:	idivi a,10.
	hrlm b,(p)
	skipe a
	 pushj p,decout
	hlrz b,(p)
	addi b,"0
	.iot chttyo,b
cpopj:	popj p,

;Having collected the data, print it out.
print:	move a,nfstys
	cain a,0
	 jrst print0
	cain a,1
	 jrst print1
	movei a,[asciz/There are /]
	pushj p,ascout
	move a,nfstys
	pushj p,decout
	movei a,[asciz/ free net ports.
/]
	pushj p,ascout
	.logout 1,


print1:	movei a,[asciz/There is 1 free net port.
/]
	pushj p,ascout
	.logout 1,

print0:	movei a,[asciz/There are no free net ports.
/]
	pushj p,ascout
	.logout 1,


nfstys:	0

pat: patch:
	block 40
epatch:	-1

lpdl=10
pdl:	block lpdl

	end beg
