#========== miscellaneous support for all programs ==========
   include std.def
# alldig - return YES if str is all digits
   integer function alldig (str)
   integer i
   character str(ARB)
   alldig = NO
   if (str(1) == EOS)
      return
   for (i=1; str(i) ~= EOS; i=i+1)
      if (str(i) < '0' | str(i) > '9')
         return
   alldig = YES
   return
   end
# ctoi - convert string at in(i) to integer, increment i
   integer function ctoi(in, i)
   character in(ARB)
   integer index
   integer d, i
#   string digits "0123456789"
   character digits(11)
   data digits(1) /'0'/
   data digits(2) /'1'/
   data digits(3) /'2'/
   data digits(4) /'3'/
   data digits(5) /'4'/
   data digits(6) /'5'/
   data digits(7) /'6'/
   data digits(8) /'7'/
   data digits(9) /'8'/
   data digits(10) /'9'/
   data digits(11) /EOS/
   while (in(i) == ' ' | in(i) == TAB)
      i = i + 1
   for (ctoi = 0; in(i) ~= EOS; i = i + 1) {
      d = index(digits, in(i))
      if (d == 0)      # non-digit
         break
      ctoi = 10 * ctoi + d - 1
      }
   return
   end
# concat - concatenate two strings
   integer function concat(s1,s2,lim)
   character s1(ARB),s2(ARB)
   integer lim, i, length, l
   l = length(s1)
   for (i=l+1; i<lim & s2(i-l) ~= EOS; i=i+1) s1(i) = s2(i-l)
   s1(i) = EOS
   concat = i-1
   return
   end
# equal - compare str1 to str2; return YES if equal, NO if not
   integer function equal(str1, str2)
   character str1(ARB), str2(ARB)
   integer i
   for (i = 1; str1(i) == str2(i); i = i + 1)
      if (str1(i) == EOS) {
         equal = YES
         return
         }
   equal = NO
   return
   end
# index - find character  c  in string  str
   integer function index(str, c)
   character c, str(ARB)
   for (index = 1; str(index) ~= EOS; index = index + 1)
      if (str(index) == c)
         return
   index = 0
   return
   end
# itoc - convert integer  int  to char string in  str
define(abs,iabs)
   integer function itoc(int, str, size)
   integer abs, mod
   integer d, i, int, intval, j, k, size
   character str(ARB)
#   string digits "0123456789"
   character digits(11)
   data digits(1) /'0'/
   data digits(2) /'1'/
   data digits(3) /'2'/
   data digits(4) /'3'/
   data digits(5) /'4'/
   data digits(6) /'5'/
   data digits(7) /'6'/
   data digits(8) /'7'/
   data digits(9) /'8'/
   data digits(10) /'9'/
   data digits(11) /EOS/
   intval = abs(int)
   str(1) = EOS
   i = 1
   repeat {            # generate digits
      i = i + 1
      d = mod(intval, 10)
      str(i) = digits(d+1)
      intval = intval / 10
      } until (intval == 0 | i >= size)
   if (int < 0 & i < size) {      # then sign
      i = i + 1
      str(i) = '-'
      }
   itoc = i - 1
   for (j = 1; j < i; j = j + 1) {   # then reverse
      k = str(i)
      str(i) = str(j)
      str(j) = k
      i = i - 1
      }
   return
   end
# length - compute length of string
   integer function length(str)
   character str(ARB)  #<wpw>      integer->character
   for (length = 0; str(length+1) ~= EOS; length = length + 1)
      ;
   return
   end
# scopy - copy string at from(i) to to(j)
   subroutine scopy(from, i, to, j)
   character from(ARB), to(ARB)
   integer i, j, k1, k2
   k2 = j
   for (k1 = i; from(k1) ~= EOS; k1 = k1 + 1) {
      to(k2) = from(k1)
      k2 = k2 + 1
      }
   to(k2) = EOS
   return
   end
# type - determine type of character
   character function type(c)
   character c
   character ctype(-128:127)
 # A-Z and a-z are alphabetic;
 # 0-9 are digits;
   data ctype/128*0,48*0,10*DIGIT,7*0,26*LETTER,
              6*0,26*LETTER,5*0/
   type = ctype(c)
   if (type == 0) type = c
#$   if( c >= '0' & c <= '9' )
#$      type = DIGIT
#$   else if( c >= 'a' & c <= 'z' )
#$      type = LETTER
#$   else if( c >= 'A' & c <= 'Z' )
#$      type = LETTER
#$   else
#$      type = c
   return
   end
# fold - fold all letters to upper case
 subroutine fold (token)
 character token(ARB)
 
call upper(token)
 return
 end
 
# upper - fold all alphas to upper case
   subroutine upper (token)
   character token(ARB)
   integer i
   for (i=1; token(i) ~= EOS; i=i+1)
      if (token(i) >= 'a' & token(i) <= 'z')
         token(i) = token(i) - 'a' + 'A'
   return
   end
 
# uniqm - find command string which matches input string uniquely
   subroutine uniqm(in,cmdstr,code,result)
   character in(ARB), cmdstr(ARB)
   integer code, result, i
   if (in(1) == EOS & cmdstr(1) ~= EOS) return #null string must match exactly
   for (i = 1; in(i) ~= EOS; i = i+1)
      if (in(i) ~= cmdstr(i)) return
   if (result ~= 0) result = ERR
   else if (code >= 0) result = code
   else if (cmdstr(i) == EOS) result = -code # if code neg., must match exactly
   return
   end
# pmatch - return YES if str1 is a partial match to str2
   integer function pmatch(str1, str2)
   character str1(ARB), str2(ARB)
   integer c
   c = 0
   call uniqm(str1, str2, 1, c)
   if (c == 0) pmatch = NO
   else pmatch = YES
   return
   end
