* LISTRDU
* LIST.READU command
* Copyright (c) 2005 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:
* 09 Aug 05  2.2-7 New layout for report.
* 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:
*
*    LIST.READU [user.no] [NO.PAGE] [LPTR [n]] [WAIT]
*
* END-DESCRIPTION
*
* START-CODE


$internal
program $list.readu
$catalog $listrdu

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

   parser = "!PARSER"

   @system.return.code = -ER$ARGS      ;* Preset for command format errors
   lptr = -1
   show.waiters = @false
   detail = @false
   user = 0

* ---------------  Step 1 -  Parse command

   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 matches "1N0N"  then
      user = token + 0
      call @parser(parser$get.token, token.type, token, keyword)
   end

   * Look for options

   loop
   while token.type # PARSER$END
      begin case
         case keyword = KW$DETAIL
            detail = @true

         case keyword = KW$LPTR
            lptr = 0
            call @parser(parser$get.token, token.type, token, keyword)
            if not(token matches "1N0N") then continue
            lptr = token + 0
            if lptr > LPTR$HIGH.PRINT.UNIT then
               stop sysmsg(2053) ;* Invalid print unit number
            end

         case keyword = KW$NO.PAGE
            s = @(0,0)

         case keyword = KW$WAIT
            show.waiters = @true

         case 1
            stop sysmsg(2018, token) ;* Unexpected token (xx)
      end case

      call @parser(parser$get.token, token.type, token, keyword)
   repeat
   
   @system.return.code = 0

* ---------------  Step 2  -  Get list of locks

   locks = getlocks('', user)

* ---------------  Step 3  -  Report
* file_id VM pathname VM userno VM type VM id VM username FM ...
* Multiple locks in a single file are not grouped together

   if lptr = 0 then printer on

   if detail then
      print on lptr sysmsg(6453, locks<1,1>, locks<1,2>, locks<1,3>)
            * Record lock limit (NUMLOCKS) = %1, Current = %2, Peak = %3
   end
   del locks<1>


* 0        1         2         3         4         5         6         7         8
* 12345678901234567890123456789012345678901234567890123456789012345678901234567890
* User File Path........................... Type Id..............................
* 1234 1234 31xxxxxxxxxxxxxxxxxxxxxxxxxxxxx FX
* 1234 1234 31xxxxxxxxxxxxxxxxxxxxxxxxxxxxx SX
* 1234 1234 31xxxxxxxxxxxxxxxxxxxxxxxxxxxxx RU   32xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* 1234 1234 31xxxxxxxxxxxxxxxxxxxxxxxxxxxxx RL   32xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
* 1234 1234 31xxxxxxxxxxxxxxxxxxxxxxxxxxxxx WAIT 32xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

   lock.reported = @false
   num.locks = dcount(locks, @fm)
   for i = 1 to num.locks
      s = locks<i>
      file.id = s<1,1>
      path = fmt(s<1,2>, '31T')
      user.no = s<1,3>
      type = s<1,4>
      id = fmt(s<1,5>, '32T')

      if user and user.no # user then continue
      if type = 'WAIT' and not(show.waiters) then continue

      if not(lock.reported) then
         print on lptr 'User File Path........................... Type Id..............................'
         lock.reported = @true
      end

      line = space(79)
      line[1,4] = fmt(user.no, '4R')
      line[6,4] = fmt(file.id, '4R')
      line[11,31] = remove(path, more.path)
      line[43,4] = type
      line[48,32] = remove(id, more.id)
      print on lptr line

      loop
      while more.path or more.id
         line = space(79)
         line[11,31] = remove(path, more.path)
         line[48,32] = remove(id, more.id)
         print on lptr line
      repeat
   next i

   if not(lock.reported) then
      if user = 0 then
         print on lptr sysmsg(6450) ;* There are no active file, read or update locks held by any user
      end else
         print on lptr sysmsg(6451, user) ;* There are no active file, read or update locks held by user xx
      end
   end

   if lptr = 0 then printer off

   return
end

* END-CODE
