!************************************************************************
!*                                                                      *
!* Program to munge text name-server table NS.TXT to binary form NS.DAT *
!* NS.DAT is used by INVENTY to produce APM inventory and by WHATIS.    *
!*                                                                      *
!*                  Version 1.3    4 May 1988                           *
!*                                                                      *
!************************************************************************

!8/12/87: Use char 1 of NS.TXT as a server (A,B,C,D,M) and generates
!1/5/88: Add Inet addresses and names
! modified NS recordformat (inon and room merged, new bootfs field)
include  "inc:util.imp"
begin 
include  "nsdefs.inc"
include  "ie:sysdep.inc"
integer  i, l, j
constinteger  max = 16_56
string  (255) s, comment
record  (ns lfm) lf
recordformat  f(string  (4) add, inet, case, string  (8) sname,
                byte  io, rm, bootfs, 
                string  (31) iname, lname, string  (18) bpmap)
record  (f) array  t(0:max)

routine  printfield(string  (255) s, integer  l)
   printstring (s)
   spaces(l-length(s)) if  l > length(s)
end 


routine  reads(string  (*) name  s, integer  l)
   integer  i, c
   s = ""
   if  l > 0 start 
      for  i = 1, 1, l cycle 
         readsymbol(c)
         s = s.tostring(c)
      repeat 
   else 
      l = -l
      cycle 
         readsymbol(c)
         exit  if  c < ' ' or  length(s) = l
         s = s.tostring(c)
      repeat 
   finish 
   s = s." " while  length(s) < l
   length(s) = l
end 

s = cliparam; s = "ns.txt" if  s = ""
open input(1, s); selectinput(1)
readline(comment) ;!Date and update info.
printline(comment)

for  i = 0, 1, max cycle 
   readsymbol(t(i)_bootfs); skipsymbol; skipsymbol; skipsymbol
   reads(t(i)_inet, 2); skipsymbol
   reads(t(i)_case, 3); skipsymbol
   reads(t(i)_sname, 8); skipsymbol; reads(t(i)_add, 4); skipsymbol
   reads(t(i)_bpmap, 18); skipsymbol
   readsymbol(t(i)_io); readsymbol(t(i)_rm); skipsymbol; 
   reads(t(i)_iname, 12); reads(t(i)_lname, -31)
   if  t(i)_io = 'A' start 
     t(i)_io = ns at
   elseif  t(i)_io = 'O'
     t(i)_io = ns on
   elseif  t(i)_io = 'I'
     t(i)_io = ns in
   else 
     t(i)_io = ns blank
   finish 

   if  t(i)_rm = 'T' start 
     t(i)_rm = ns the
   elseif  t(i)_rm = 'R'
     t(i)_rm = ns room
   else 
     t(i)_rm = ns blank
   finish 
repeat 

!Now write out the file
open output(1, "ns.dat"); selectoutput(1)
!Put all the short names as a quick reference at the front of the file
!(128 * 8 bytes)
for  i = 0, 1, max cycle 
   printstring(t(i)_sname)
repeat 

!Then write out all the long records (128 * 64 bytes)
for  i = 0, 1, max cycle 
   lf = 0
   lf_inonrm = (t(i)_io)<<4+(t(i)_rm)&15                  { 1: IN/ON/AT}
   lf_bootfs = t(i)_bootfs                                { 1: Boot filestore}
   if  t(i)_case = "???" then  lf_case = 0 else  lf_case = stoi(t(i)_case)
   if  t(i)_inet = "?" then  lf_inet=0 else  lf_inet = stoi(t(i)_inet)
   bulk move(7,  charno(t(i)_iname, 1), lf_iname(1))      { 7: Inet name}
   bulk move(31, charno(t(i)_lname, 1), lf_lname(1))      {31: long name}
   bulk move(4,  charno(t(i)_add, 1),   lf_add(1))        { 4: Location}
   bulk move(18, charno(t(i)_bpmap, 1), lf_bpmap(1))      {18: Peripheral list}
   !                                        Total         {64}
   for  j = 0, 1, sizeof(lf)-1 cycle 
      printsymbol(byteinteger(addr(lf)+j))
   repeat 
repeat 
printsymbol(length(comment)); printstring(comment); spaces(255-length(comment))
endofprogram