10	%title "Example"
20	%include "rmslib:rmsdefs.bas"

	! This program is an example of using RMS Interface 
	! with Basic.

	! This program will create an indexed file named
	! Example.Dat.  It will let the user enter information
	! at the terminal and will write the information into
	! the file.   When the user enters control-z, the
	! program will print out the lines entered in alphabetical
	! order (based on the first 8 characters), and exit.

	common  long term,  long indexed_file, 		&
		long ioflag, long inlength,        	&
		file_rec  term_buf

	declare integer function odd( integer )
	declare long constant 	  RMS$_EOF       =x"0001827A"L
	external long function input_count
	external long constant file_error

	! Define the I/O buffer for this program
	record file_rec
	   byte  buf (60)
	   end record file_rec

	! main program 

	! Define the key for the file 

	call rms_keydef( ifi_( indexed_file ), ref_(0%), siz_(8%), &
		dtp_("stg"), flg_("dup")   )
		

	! Define the FAB 

	! Put the name of the file in a character string (a necessity
	! for using RMS Interface with Basic).

	file_name$="example.dat"
	call rms_fabdef ( ifi_( indexed_file ), fnm_(file_name$),    &
			  org_("idx"), rat_("cr"), fop_("mxv"),      &
			  fac_("put,get")   			     )


	! Create the file. Specify protection and Expiration
	! date and time.					 

	call rms_create( ifi_( indexed_file ),  		   &
		     xabpro_( pro_( "s:r,o:rwed,g,w" )),           &
		     xabdat_( edt_( "30-JUL-1983" ),		   &
			      bdt_( "21-JUN-1983" )),		   &
		     err_(file_error by value)  		   )

	! Open sys$input/sys$output (the terminal)

	call rms_open( ifi_( term ), fnm_("tt"),		   &
		  fac_("put,get"), rat_("cr"),			   &
		  err_(file_error by value)			   )

	! Tell the user what to do 

	print " Enter lines of text, each at least 8 characters long. "
	print " Type control-z when you are done ..."

	! Fill up the indexed file with term input 

	ioflag=1%
	while odd(ioflag)
	    ! Read from terminal
	    ioflag = rms_getseq( term, term_buf, 60%)
	    ! Write the record to disk file if terminal read
	    ! was successful.					 
	    if odd(ioflag) then
	        call rms_put( ifi_( indexed_file ), rbf_(term_buf),    &
			 rsz_( input_count( term ) ), rac_("key"),     &
		  	 err_(file_error by value)		       )
	    else
	       if ioflag = rms$_eof then
		   gosub dump_the_file
	       else
		   call print_file_info(term)
	           end if
	       end if
	    next
	    call sys$exit( 1% by value)

	! local subroutine to rewind the file Example.dat, then sequentially
	! read records from it and print them on the terminal.

 dump_the_file:
	 declare long input_status
	 ! Rewind the indexed file
	 call rms_rewind ( ifi_( indexed_file ), krf_(0%),	&
		  	   err_(file_error by value)		)
	 ! Tell user sorted output is coming.
	  call rms_putseq( term, "Sorted Output..." by ref, 16%)
	  input_status = 1%
	 ! Read records from indexed file and print on terminal
	  while odd(input_status)
	      input_status = rms_getseq( indexed_file, term_buf, 60%)
	      inlength = input_count( indexed_file )
	      if inlength > 0 then
		call rms_put( ifi_(term), rbf_(term_buf), rsz_(inlength) )
		end if
	     next
	  return


42	 ! Logical function to tell if integer is odd.
 	def odd (integer i)
	if i / 2% * 2% = i then
	  odd = 0%
	else
	  odd = -1%
	end if
	end def
 
50	end	! Of main program


100
	! Function to return the number of input characters last
	! obtained from reading a particular file.

	function integer input_count by ref( long file_variable  ) 
	declare long num_chars_read
	  ! Utilizing the file information subroutine RMS_INFO,
	  ! Get "RSZ" from the RAB of file that did the read
	  call rms_info( file_variable, "rsz", num_chars_read)
	  input_count = num_chars_read
	  end function


200
	   ! Use a short procedure to print file information and
	   ! exit if there is a file error

	  sub file_error ( long	file_id, long ioflag)
	    call print_file_info( file_id )
	    call sys$exit( ioflag by value)
	    end sub
