!
! this is dump, which takes fortran unformatted data files and 
!	displays them in some sort of format useful to the user.
!	It handles variable or fixed length records.
!
	subroutine dump(i_start,i_end)

	parameter none  = 0,
     1		  first = 1,
     2		  last  = 2,
     3	          only  = 3

 	logical overflow
	common /too_large/ overflow,large_length,iaddress

	logical hit_end_of_file
	common /editor/ hit_end_of_file

	character*255 macro_definition,macro_name
	common /mine/ macro_definition,macro_name
	character*9 record_type
	character*80 in_string,real_file_name,temp
	integer std_seq_get,parse_string,record_size
	external std_rfa_useropen,rms$_eof,ss$_normal,lib$_syntaxerr
	integer*2 length,size
	character*1 zero,space
	data space /' '/
	data zero /0/
	data size/32768/
	character*63 file_name
	
	common /files/ file_name

	byte buffer(32768),edit_buffer(32768)
	common /buf/ buffer,edit_buffer,length_total

	include 'flags.inc'

        byte device_type
        parameter FT1 = 16
        parameter FT8 = 23
	data logical_unit/3/

	integer terminal_type,vt100_type
	data vt100_type /2/
	integer*2 display_type

	logical attention_flag,display_message
	common /control_c/ attention_flag,display_message
	logical reverse_on
	common /rflag/ reverse_on

	attention_flag=.false.

	if (i_start.gt.i_end) then
		type 4242
 4242		format(/' Are you trying to be funny???'/)
	 	return
	 endif

	call check_file

	fixed_length=.false.

	open(unit=logical_unit,name=file_name,type='old',
     1		form='unformatted',readonly,useropen=std_rfa_useropen,
     2		err=9100,shared)
	inquire(logical_unit,recl=record_size)
	inquire(logical_unit,recordtype=record_type)
	inquire(logical_unit,name=real_file_name)
	ireal_length=index(real_file_name,space)-1
	if (record_type.eq.'FIXED') fixed_length=.TRUE.
	if (fixed_length) then
		length_of_header=0
 	 else
		length_of_header=2
	 endif
	close(unit=logical_unit)
!
! change record size to get correct number of longwords
!
	if (.not. fixed_length) record_size=record_size+1
!
! added reopen of file because of large fixed length records cause
! program to die 2/22/82.
!
	open(unit=logical_unit,name=file_name,type='old',
     1		form='unformatted',readonly,useropen=std_rfa_useropen,
     2		recl=record_size,recordtype=record_type,shared)
 3	format(a1)
!
! if only doing length summary forget macro definition
!
	if (.not. (display_on .or. print_on)) goto 22
	ilength=index(macro_definition,space)-1
	if (ilength.le.0) then
		type 2010
 2010		format(' No macro definition to use for dump'/)
		return
	 endif
	istat=parse_string(macro_definition(1:ilength),jlength)
	if (istat.eq.%loc(lib$_syntaxerr)) then
		type 2015,macro_definition(1:ilength)
 2015		format(/' Error parsing macro definition:'/
     1		x,a/<ilength-jlength+1>x,'^')
		return
	 endif
	if (.not. istat) call lib$stop(%val(istat))
	if (print_on) then
		type 2060
 2060		format(/' Output will be in file DUMP.OUT'/)
		open(unit=7,name='dump.out',type='new')
	 endif

 22	hit_end_of_file=.false.
	no_rec=0

	if (full_screen) then
		call lib$screen_info(terminal_type,device_type)
                if (jiand(terminal_type,vt100_type).ne.vt100_type .and.
     1                  (device_type.lt.FT1.or.device_type.gt.FT8)) then
     			full_screen=.false.
			type 2
 2			format(/' Wrong terminal type for full screen'/)
		 endif
	 endif
!
! if full screen selected and the terminal is a vt100 series, set up
!	scrolling region, labels, etc.
!
	if (full_screen) then
		call lib$erase_page(1,1)
		display_type=2
		temp='  File:  '//real_file_name(1:ireal_length)
		call lib$put_screen(temp,1,1,display_type)
		encode(80,4005,temp)
 4005		format('Record #    Entries   ',18x,'Contents')
		display_type=0
		call lib$put_screen(temp,3,1)
		encode(80,4010,temp)
 4010		format(8('-'),4x,7('-'),21x,8('-'))
		call lib$put_screen(temp,4,1)
		call lib$set_scroll(6,24)
		temp=' '
		call lib$put_screen(temp,6,1)
	 endif

 5	continue
	call get_record(logical_unit,buffer,size,length_total,
     1			istat,fixed_length)
	if (istat.eq.%loc(rms$_eof)) hit_end_of_file=.true.
	if (istat.eq.%loc(rms$_eof)) goto 9000
	no_rec=no_rec+1
	if (no_rec.lt.i_start) goto 1000
	if (overflow) then
		if (summary_on) type 10,no_rec,large_length
	 else
		if (summary_on) type 10,no_rec,length_total
	 endif
 10	format(' record #',i6,' is ',i7,
     1		' bytes long.')	
	if (print_on) then
		if (overflow) then
			iprint_size=large_length
		 else
			iprint_size=length_total
		 endif
		write(7,15) real_file_name(1:ireal_length),no_rec,
     1			    iprint_size
 15		format(//,' dump of ',a<ireal_length>,' record #',i6,
     1			  ' size (bytes) = ',i7//' record entries',
     2		          38x,'contents'/)
	 endif
	if (.not. (display_on .or. print_on)) goto 900
!
! if we need reversal, do so
!
	if (reverse_on) then
		if (overflow) then
			len1=large_length/2
			call std_swap_bytes(len1,%val(iaddress))
		 else
			len1=length_total/2
			call std_swap_bytes(len1,buffer)
		 endif
	 endif
!
! use overflow buffer if needed
!
	if (overflow) then
		call special(%val(iaddress),large_length,no_rec,
     1				real_file_name,ireal_length)
	 else
	    	call special(buffer,length_total,no_rec,
     1				real_file_name,ireal_length)
	 endif
!
! release overflow buffer if used
!
 900	if (overflow) then
		istat=lib$free_vm(large_length,iaddress)
		if (.not. istat) then
			type 910
 910			format(/' Error freeing virtual memory'/)
		 endif
		iaddress=0
		large_length=0
	 endif
 	if (attention_flag) goto 9000
	if (no_rec.ge.i_end) goto 9000
 1000	length_total=0
	i_here=0
	goto 5
 9000	rewind logical_unit
	
 9100	if (full_screen) then
		call lib$set_scroll(1,24)
		type 9110
 9110		format(' ')
		call lib$set_cursor(24,1)
	 endif
	return
	end
