* LISTSRVR
* LISTSRVR - LIST.SERVERS command.
* Copyright (c) 2004 Ladybridge Systems, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 
* Ladybridge Systems can be contacted via the www.openqm.com web site.
* 
* START-HISTORY:
* 13 Oct 04  2.0-5 Use message handler.
* 16 Sep 04  2.0-1 OpenQM launch. Earlier history details suppressed.
* END-HISTORY
*
* START-DESCRIPTION:
*
* END-DESCRIPTION
*
* START-CODE

$internal
program listsrvr
$catalogue $LSTSRVR

$include parser.h
$include err.h
$include int$keys.h

   parser = "!PARSER"
   prompt ''

   @system.return.code = -ER$ARGS     ;* Preset for argument errors

   if not(kernel(K$ADMINISTRATOR,-1)) then
      stop sysmsg(2001) ;* Command requires administrator privileges
   end

   call @parser(parser$reset, 0, @sentence, 0)
   call @parser(parser$get.token, token.type, token, keyword) ;* Verb

   call @parser(parser$get.token, token.type, token, keyword)
   if token.type # PARSER$END then
      stop sysmsg(2018, token) ;* Unexpected token (xx)
   end

   * Read the config file

   openseq kernel(K$INIPATH,0) to ini.f else
      @system.return.code = -status()
      stop sysmsg(3308) ;* Unable to open configuration file
   end

   servers = ''
   ip.addresses = ''
   ports = ''
   user.names = ''

   section = ''
   loop
      readseq line from ini.f else exit

      if trim(line) = '' then continue   ;* Skip blanks, reinserting correctly

      if line[1,1] = '[' then
         section = upcase(field(line[2,999], ']', 1))
      end else if section = 'QMNET' then
         server.name = field(line, '=', 1)
         locate server.name in servers<1> by 'AL' setting pos else
            ins server.name before servers<pos>

            data = field(line, '=', 2)
            ip.addr = field(data, ',', 1)
            if index(ip.addr, ':', 1) then
               port = field(ip.addr, ':', 2)
               ip.addr = field(ip.addr, ':', 1)
            end else
               port = 'Default'
            end

            ins ip.addr before ip.addresses<pos>
            ins port before ports<pos>

            ins field(data, ',', 2) before user.names<pos>
         end
      end
   repeat

   closeseq ini.f

   * Now list the sorted results

   n = dcount(servers, @fm)
   if n = 0 then
      crt sysmsg(3313) ;* There are no QMNet servers defined on this system
   end else
      crt sysmsg(3314)
      * Server..........  IP address..............  Port...  User name...........
      for i = 1 to n
         crt fmt(servers<i>, '16L') : '  ' :
         crt fmt(ip.addresses<i>, '24L') : '  ' :
         crt fmt(ports<i>, '7L') : '  ' :
         crt user.names<i>
      next i
   end

   @system.return.code = 0
end

* END-CODE
