%include "INet:Common_Formats.Inc"

%externalpredicatespec FS lookup(%string(31) what, %integername value)

%systemroutinespec phex(%integer x)

%externalroutine print INet address(%integer addr)
   write(addr >> 24 & 255, 0);  print symbol('.')
   write(addr >> 16 & 255, 0);  print symbol('.')
   write(addr >>  8 & 255, 0);  print symbol('.')
   write(addr       & 255, 0)
%end

%owninteger in use = 0

%routine show one(%record(TCB fm)%name TCB, %integer slot)
   %integer now, offset
      %return %if TCB_local port = 0
      in use = in use + 1
      !%if TCB_local port = 0 %start
      !   printstring("Slot ");  write(slot, 0);  printstring(": ")
      !   printstring("*free*");  newline
      !   %return
      !%finish
      printstring("Slot ");  write(slot, 0);  printstring(": ")
      print inet address(TCB_remote address);  print symbol('.')
      write(TCB_remote port, 0);  printstring(" <--> ")
      write(TCB_local port, 0);  newline
      printstring("State ");  printstring(TCP state name(TCB_state))
      printstring(", from ");  printstring(TCP state name(TCB_previous state))
    ! now = msecs timestamp
    ! offset = now - TCB_state change stamp
    ! printstring(", changed ")
    ! %if offset >= 0 %start
    !    write(offset, 0);  printstring(" msec")
    !    print symbol('s') %if offset # 1
    ! %else
    !    printstring("ages")
    ! %finish
    ! printstring(" ago")
      newline
      printstring("Seq: ");  phex(TCB_snd nxt)
      printstring(", ISS: ");  phex(TCB_iss)
      printstring(", sent: ");  write(TCB_snd nxt - TCB_iss, 0)
      printstring(", window: ");  write(TCB_snd wnd, 0);  newline
      printstring("Ack: ");  phex(TCB_rcv nxt)
      printstring(", IRS: ");  phex(TCB_irs)
      printstring(", received: ");  write(TCB_rcv nxt - TCB_irs, 0)
      printstring(", window: ");  write(TCB_rcv wnd, 0);  newline
%end

%begin
   %record(TCB table fm)%name TCB table
   %integer status, i, n
      %if FS lookup(TCB table name, i) %start
         TCB table == record(i)
         n = 32
      %else
         printstring("No TCB table?");  newline
         %stop
      %finish
      show one(TCB table_TCB(i), i) %for i = 1, 1, n
      write(in use, 0);  printstring(" TCB")
      print symbol('s') %if in use # 1
      printstring(" in use");  newline
%end %of %program
