      $set ans85 comp noqual noalter noosvs vsc2
      ******************************************************************
      /
       identification division.

      * this program handles the viewing of the contents of a relative
      * file. The screen has a visible size of "visible-size". The size
      * of the file array in the data block is "file-array-size". For
      * this program to work correctly the file-array-size must be
      * greater than the visible-size.

      * You are provided with test file "dsdemo5.fle" which illustrates
      * the use of this program. It may be of interest to note that the
      * record size of this test file is exactly the same as the record
      * size of any of the error message files you create. As an
      * exercise you may wish to use this program to look at one of your
      * own error message files. (Beware however that the error message
      * files have a dummy system record at the start, so that your
      * error numbers will always be 1 greater than the relative record
      * key.

       environment division.

       input-output section.
       file-control.
           select test-file assign "DSDEMO5.FLE"
           organization is relative
           access mode is dynamic
           relative key is rel-key
           file status is file-status.

       data division.

       file section.
       fd  test-file.
       01  test-file-record            pic x(76).

       working-storage section.

      *
      * local proc numbers
      *
       78  data-up-proc                value 144.
       78  data-down-proc              value 145.

      *
      * run time and screen set name constants
      *
       78  dialog-system               value "DSRUN".
       78  screen-set-name             value "DSDEMO5".

      *
      * constants
      *
      * visible-size is one less than the size of the visible portion
      * of the array on the screen. This is to allow pageing of data to
      * still leave one of the previous records visible on the screen

       78  file-array-size             value 52.
       78  visible-size                value 17.

      *
      * relative record variables
      *
       77  rel-key                      pic 9(4) comp.
       77  rec-limit                    pic 9(4) comp.
       77  actual-amount-read           pic 9(4) comp.

      *
      * temporary indexes used in array manipulations
      *
       77  temp-ind-1                   pic s9(4) comp.
       77  temp-ind-2                   pic s9(4) comp.

       01  file-status                 pic x(2).
           88  successful-io           value "00".
           88  end-of-file             value "10" "46".
      *
      * buffer used to store extra records when paging beyond the
      * current limits of the array.
      *
       01  paging-buffer.
           03  paging-buffer-item  occurs visible-size.
               05  buffer-rnumber      pic 9(3).
               05  buffer-rcontents    pic x(76).

       copy "DS-CNTRL.MF".
       copy "DSDEMO5.CPB".

      ******************************************************************
      *                                                                *
      * procedure division                                             *
      *                                                                *
      ******************************************************************
       procedure division.
       main-process section.
           perform initialise-demo-handler
           perform process-file-list until dsdemo5-esc-flag-true
           perform terminate-process.
      ******************************************************************
       initialise-demo-handler section.
           initialize dsdemo5-data-block
           move dsdemo5-data-block-version-no to
                                              ds-data-block-version-no
           move dsdemo5-version-no to ds-version-no
           open input test-file
           perform load-array-from-file
           if rec-limit < file-array-size
               move rec-limit to dsdemo5-line-lim
           else
               move file-array-size to dsdemo5-line-lim
           end-if
           initialize ds-input-fields
           move ds-new-set to ds-control
           move screen-set-name to ds-set-name
           perform call-dialog-system
           move ds-continue to ds-control.
      ******************************************************************
       process-file-list section.
           if dsdemo5-up-flag-true
               perform adjust-array-up
           else
               if dsdemo5-dn-flag-true
                   perform adjust-array-down
               end-if
           end-if
           perform clear-flags
           perform call-dialog-system.
      ******************************************************************
       terminate-process section.
           close test-file
           stop run.
      ******************************************************************
       adjust-array-up section.
           move dsdemo5-rnumber(1) to rel-key
           perform read-previous-page
           if actual-amount-read > zero
               perform shuffle-array-down
               move actual-amount-read to dsdemo5-adj-cnt
               move data-down-proc to ds-proc-no
           end-if.
      ******************************************************************
       adjust-array-down section.
           move dsdemo5-rnumber(file-array-size) to rel-key
           perform read-next-page
           if actual-amount-read > zero
               perform shuffle-array-up
               move actual-amount-read to dsdemo5-adj-cnt
               move data-up-proc to ds-proc-no
           end-if.
      ******************************************************************
       read-previous-page section.
      * this section attempts to read backwards through the file up to
      * visible-size records
           move zero to actual-amount-read
           subtract 1 from rel-key
           if rel-key not = 0
               move visible-size to temp-ind-1
               perform read-test-file
               perform until rel-key = 0 or
                                     actual-amount-read = visible-size
                   if successful-io
                       add 1 to actual-amount-read
                       move rel-key to buffer-rnumber(temp-ind-1)
                       move test-file-record to
                                         buffer-rcontents(temp-ind-1)
                       subtract 1 from temp-ind-1
                   end-if
                   subtract 1 from rel-key
                   perform read-test-file
               end-perform
           end-if.
      ******************************************************************
       read-next-page section.
      * this section attempts to read forwards through the file up to
      * visible-size records
           move zero to actual-amount-read
           perform read-test-file
           perform read-next-record
           perform until end-of-file or actual-amount-read =
                                                         visible-size
               if successful-io
                   add 1 to actual-amount-read
                   move rel-key to buffer-rnumber(actual-amount-read)
                   move test-file-record to
                                    buffer-rcontents(actual-amount-read)
               end-if
               perform read-next-record
           end-perform.
      ******************************************************************
       load-array-from-file section.
      * this section does the initial load of as much of the file as it
      * can get into the array
           move 1 to rec-limit
           move 1 to rel-key
           perform read-test-file
           perform until not successful-io or rec-limit >
                                                      file-array-size
               if successful-io
                   move rel-key to dsdemo5-rnumber(rec-limit)
                   move test-file-record to dsdemo5-rcontent(rec-limit)
                   add 1 to rec-limit
               end-if
               perform read-next-record
           end-perform
           subtract 1 from rec-limit.
      ******************************************************************
       shuffle-array-up section.
           move actual-amount-read to temp-ind-1
           add 1 to temp-ind-1
           move 1 to temp-ind-2
           perform until temp-ind-1 > file-array-size
               move dsdemo5-group-item-001(temp-ind-1)
                                  to dsdemo5-group-item-001(temp-ind-2)
               add 1 TO temp-ind-1
               add 1 TO temp-ind-2
           end-perform
           move 1 to temp-ind-1
           move file-array-size to temp-ind-2
           subtract actual-amount-read from temp-ind-2
           add 1 to temp-ind-2
           perform until temp-ind-2 > file-array-size
               move paging-buffer-item(temp-ind-1)
                                  to dsdemo5-group-item-001(temp-ind-2)
               add 1 TO temp-ind-1
               add 1 TO temp-ind-2
           end-perform.
      ******************************************************************
       shuffle-array-down section.
           move file-array-size to temp-ind-1
           subtract actual-amount-read from temp-ind-1
           move file-array-size to temp-ind-2
           perform until temp-ind-2 = actual-amount-read
               move dsdemo5-group-item-001(temp-ind-1)
                                  to dsdemo5-group-item-001(temp-ind-2)
               subtract 1 from temp-ind-1
               subtract 1 from temp-ind-2
           end-perform

           move visible-size to temp-ind-1
           move actual-amount-read to temp-ind-2
           perform until temp-ind-2 < 1
               move paging-buffer-item(temp-ind-1)
                                  to dsdemo5-group-item-001(temp-ind-2)
               subtract 1 from temp-ind-1
               subtract 1 from temp-ind-2
           end-perform.
      ******************************************************************
       clear-flags section.
           move 0 to dsdemo5-esc-flag
           move 0 to dsdemo5-up-flag
           move 0 to dsdemo5-dn-flag.
      ******************************************************************
       read-test-file section.
           read test-file.
      ******************************************************************
       read-next-record section.
           read test-file next.
      ******************************************************************
       call-dialog-system section.
           call dialog-system using ds-control-block,
                                    dsdemo5-data-block
           if ds-system-error-no > ZERO
               perform terminate-process
           end-if.

      ******************************************************************
