	[inherit('rmsprocs.pen')] 
	program	example  (output);

    (* 
	This program is an Example of using RMS Interface 
	with Pascal.

	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.

     *)

	const 	  RMS$_EOF       =%x'0001827A';       

	type buffer = packed array [1..60] of char;

	var terminal	  : [static]integer := 0;	(* file variable *)
	    indexed_file  : [static]integer := 0;	(* file variable *)
	    ioflag   : integer := 1;
	    inlength : integer;
	    term_buf : buffer;


	(* Function to retrieve the # chars obtained from a read,
	   using the RMS Interface routine RMS_INFO
	  						        *)
	function input_count( file_variable : integer ) : integer;
	var num_chars_read : integer;
	begin
	    (* Get 'RSZ' from the RAB of file that did the read *)
	  rms_info( file_variable, 'rsz', num_chars_read);
	  input_count := num_chars_read
	end;
	

	(* Use a short procedure to print file information and
	   exit if there is a file error
								*)
	[asynchronous,unbound ]procedure file_error (
	   file_id, ioflag : [unsafe]integer  );
	  begin
	    print_file_info( file_id );
	    halt;
	  end;


    (*  Procedure to rewind the file Example.dat, then sequentially
	read records from it and print them on the terminal.
									*)
	procedure dump_the_file;
	var input_status, inlength : integer;
	begin
	  rms_rewind ( ifi_( indexed_file ), krf_(0), err_( file_error ) );
	  rms_putseq( terminal, 'Sorted Output...', 16);
	  input_status := 1;
	  while odd(input_status) do
	    begin
	      input_status := rms_getseq( indexed_file, term_buf, 60);
	      inlength := input_count( indexed_file );
	      if (inlength) > 0 then
		rms_put( ifi_(terminal), rbf_(term_buf), rsz_(inlength) );
	    end;
	end;
	  

    begin	(* main program *)

		(* Define the key for the file *)

	rms_keydef( ifi_( indexed_file ), ref_(0), siz_(8), dtp_('stg'),
		    flg_('dup')   );
		

	(* Define the FAB *)

	rms_fabdef ( ifi_( indexed_file ), fnm_('Example.dat'), org_('idx'),
		     rat_('cr'), fop_('mxv'), fac_('put,get')   );


	(* Create the file. Specify protection and Expiration
	   date and time.					 *)

	 rms_create( ifi_( indexed_file ), err_( file_error),
		     xabpro_( pro_( 's:r,o:rwed,g,w' )),
		     xabdat_( edt_( '30-JUN-1983' ))      );
	

	(* Open sys$input *)

	rms_open( ifi_( terminal ), fnm_('tt'),
		  fac_('put,get'),err_( file_error ), rat_('cr')  );

	(* Tell the user what to do *)

	writeln (' Enter lines of text, each at least 8 characters long. ');
	writeln (' Type control-z when you are done ...');


	(* Fill up the indexed file with terminal input *)

	while odd(ioflag) do
	  begin
	    ioflag := rms_getseq( terminal, term_buf, 60);

	    (* Write the record to disk file if terminal read
	       was successful.					 *)
	    if odd (ioflag) then
	        rms_put( ifi_( indexed_file ), rbf_(term_buf), 
			 rsz_( input_count( terminal ) ),
			 rac_('key'),  err_( file_error )  )
	    else
	       if ioflag = rms$_eof then
		   dump_the_file
	       else
		   file_error( terminal, ioflag );
	  end;

     end.

	
