* SAVELST
* SAVE.LIST command
* Copyright (c) 2001 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:
*
*    SAVE.LIST [list.name] [FROM list.number]
*
* list.name   = name under which to save. Defaults to list.number
*
* list.number = list to save. Defaults to 0.
*
* @SYSTEM.RETURN.CODE
*    +ve Successful - number of items saved
*    -ve Error code
*
* END-DESCRIPTION
*
* START-CODE


$internal
program $savelist
$catalog $SAVELST

$include err.h
$include keys.h

$include syscom.h
$include parser.h


   parser = "!PARSER"

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

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

   list.no = 0
   list.name = ""

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

   * Get list name

   call @parser(parser$get.token, token.type, list.name, keyword)
   if (token.type = PARSER$END) or (keyword = KW$FROM) then
      stop sysmsg(3241) ;* Target list name required
   end

   * Get list number if present

   call @parser(parser$get.token, token.type, token, keyword)
   if keyword = KW$FROM then
      call @parser(parser$get.token, token.type, token, keyword)
      if (token.type = PARSER$END) or not(num(token)) then
         stop sysmsg(3271) ;* Select list number missing or invalid
      end

      list.no = token + 0
      if list.no > HIGH.USER.SELECT then
         stop sysmsg(3271) ;* Select list number missing or invalid
      end

      call @parser(parser$get.token, token.type, token, keyword)
   end

   * Check no further information in command

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

   * Use default list name if not supplied

   if list.name = '' then list.name = list.no :  ""

   if not(selectinfo(list.no, SL$ACTIVE)) then
      stop sysmsg(3260, list.no) ;* Select list xx is not active
   end

* ---------------  Step 2 -  Create the save list file if necessary

   open "$SAVEDLISTS" to saved.lists.file else
      @system.return.code = -status()
      
      if status() # ER$NVR then
         stop sysmsg(3248, status()) ;* Error xx opening $SAVEDLISTS
      end

      create.file "$SVLISTS" dynamic on error
         stop sysmsg(3249, status()) ;* Error xx creating $SAVEDLISTS
      end

      voc.rec = "F" : @fm : "$SVLISTS"
      recordlocku @voc, "$SAVEDLISTS"
      write voc.rec to @voc, "$SAVEDLISTS"

      display sysmsg(3250) ;* Created $SAVEDLISTS

      open "$SAVEDLISTS" to saved.lists.file else
         stop sysmsg(3248, status()) ;* Error xx opening $SAVEDLISTS
      end
   end

* ---------------  Step 3 -  Save the select list

   readlist list.rec from list.no else null

   recordlocku saved.lists.file, list.name
   write list.rec to saved.lists.file, list.name on error
      @system.return.code = -status()
      stop sysmsg(3272, status()) ;* Error xx writing saved list record
   end

   num.items = dcount(list.rec, @fm)
   display sysmsg(3266, num.items, list.name) ;* xx records saved to select list 'xx'

   @system.return.code = num.items

   return
end

* END-CODE
