[comp.lang.fortran] character function subprogram

wang@math.ufl.edu (04/08/91)

Can anyboby explain to me why the following FORTRAN character function
subprogram works under VAX/VMS, but doesn't work under unix (SunOS 4.1.1,
Sun Fortran 1.3)? 

      CHARACTER*8 cursor,cls*4
      cls = CHAR(27)//'[2J'
      i   = 5
      j   = 35
      PRINT *,cls,cursor(i,j),'I am here!'
      END

      CHARACTER FUNCTION cursor*8(row,col)
      CHARACTER*1 esc,c1*2,c2*2
      INTEGER row,col
      esc = CHAR(27)
      WRITE(c1,'(i2.2)') row
      WRITE(c2,'(i2.2)') col
      cursor = esc//'['//c1//';'//c2//'H'
      RETURN
      END

As you can see, this character function routine just returns the
character string for a cursor's position on the screen according to
its two arguments. The above program works just fine on VAX/VMS
machines but crashes on UNIX machines (specifically, SunSparc
workstations). The error messages I got when I ran it on a SunSparc
workstation are listed below:

iio: [-1] end of file
lately: writing sequential formatted internal IO
part of last string: |
IOT trap

Can you offer a sulotion to fix the problem?

Thanks a lot in advance!

wang@math.ufl.edu (04/08/91)

I guess my character function subprogram is ok, but I cannot just call
it directly in the PRINT statement (under Unix, of course). For
example, it would work if my main program is writen like this:

	CHARACTER*8 cursor,string,cls*4
	cls = CHAR(27)//'[2J'
	i   = 5
	j   = 35
	string = cursor(i,j)
	PRINT *,cls,string,'I am here!'
	END

This would, in my opinion, miss the point of conveniency of function
subprogram. My question is this: Why can't we call "cursor" directly
in the PRINT statement under Sun Fortran?

system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson)) (04/11/91)

In article <1991Apr7.211058.25661@math.ufl.edu> wang@math.ufl.edu writes:
>This would, in my opinion, miss the point of conveniency of function
>subprogram. My question is this: Why can't we call "cursor" directly
>in the PRINT statement under Sun Fortran?

This is mentioned in some FORTRAN manuals as a limitation - you are in
effect trying to use the I/O libraries recursively in the original
program, and some I/O libraries are not re-entrant in this way.
Of course, FORTRAN-77 doesn't have to provide recursion.
-- 
Mike Peterson, System Administrator, U/Toronto Department of Chemistry
E-mail: system@alchemy.chem.utoronto.ca
Tel: (416) 978-7094                  Fax: (416) 978-8775

vsnyder@jato.jpl.nasa.gov (Van Snyder) (04/12/91)

In article <1991Apr11.162324.9004@alchemy.chem.utoronto.ca> system@alchemy.chem.utoronto.ca (System Admin (Mike Peterson)) writes:
>In article <1991Apr7.211058.25661@math.ufl.edu> wang@math.ufl.edu writes:
>>This would, in my opinion, miss the point of conveniency of function
>>subprogram. My question is this: Why can't we call "cursor" directly
>>in the PRINT statement under Sun Fortran?
>
>This is mentioned in some FORTRAN manuals as a limitation - you are in
>effect trying to use the I/O libraries recursively in the original
>program, and some I/O libraries are not re-entrant in this way.
>Of course, FORTRAN-77 doesn't have to provide recursion.

It also violates section 12.11 of the standard: "A function must not be
referenced within an expression appearing anywhere in an input/output list
if such a reference causes an input/output statement to be executed."

-- 
vsnyder@jato.Jpl.Nasa.Gov
ames!elroy!jato!vsnyder
vsnyder@jato.uucp

trevor@ccc.govt.nz (04/16/91)

In article <1991Apr7.200854.25446@math.ufl.edu>, wang@math.ufl.edu writes:
> Can anyboby explain to me why the following FORTRAN character function
> subprogram works under VAX/VMS, but doesn't work under unix (SunOS 4.1.1,
> Sun Fortran 1.3)? 
> 
>       CHARACTER*8 cursor,cls*4
>       cls = CHAR(27)//'[2J'
>       i   = 5
>       j   = 35
>       PRINT *,cls,cursor(i,j),'I am here!'
>       END
> 
>       CHARACTER FUNCTION cursor*8(row,col)
>       CHARACTER*1 esc,c1*2,c2*2
>       INTEGER row,col
>       esc = CHAR(27)
>       WRITE(c1,'(i2.2)') row
>       WRITE(c2,'(i2.2)') col
>       cursor = esc//'['//c1//';'//c2//'H'
>       RETURN
>       END
> 
> (Unix messages deleted...)
> 

I think the main problem is that direct screen addressing; direct cursor
positioning are system-dependent: even worse, they are peripheral-dependent.
The program quoted writes an output string which contains an ANSI
device-control escape sequence. It should work fine if sent to a DEC VT100,
VT200, VT300 terminal (or any other VDU which emulates a DEC ANSI CRT or 
accepts ANSI device control) by a system which passes on the escape sequence
uncorrupted, but it won't work right on a VT52 or a printer, I think the screen
control sequences for HP terminals were different (but it's a looong time since
I programmed for one) and I'd be surprised if it did work under Unix on a Sun.
Direct cursor addressing isn't standard Fortran. (Fortran carriage-controls for
printers IS standard, but we have one printer that doesn't recognise them...)

I do know you can't use any sort of direct cursor addressing when writing for 
GEM; you have to use the appropriate GEM VDI or AES call.

I know this doesn't HELP much, but I hope it might explain a little.

Trevor Ingham, Systems Programmer, Christchurch City Council, New Zealand.
trevor@ccc.govt.nz

trevor@ccc.govt.nz (04/17/91)

In article <1991Apr15.140403.210@ccc.govt.nz>, trevor@ccc.govt.nz writes:
> In article <1991Apr7.200854.25446@math.ufl.edu>, wang@math.ufl.edu writes:
>> Can anyboby explain to me why the following FORTRAN character function
>> subprogram works under VAX/VMS, but doesn't work under unix (SunOS 4.1.1,
>> Sun Fortran 1.3)? 
>> 
>>       CHARACTER*8 cursor,cls*4
>>       cls = CHAR(27)//'[2J'
>>       i   = 5
>>       j   = 35
>>       PRINT *,cls,cursor(i,j),'I am here!'
>>       END
>> 
>>       CHARACTER FUNCTION cursor*8(row,col)
>>       CHARACTER*1 esc,c1*2,c2*2
>>       INTEGER row,col
>>       esc = CHAR(27)
>>       WRITE(c1,'(i2.2)') row
>>       WRITE(c2,'(i2.2)') col
>>       cursor = esc//'['//c1//';'//c2//'H'
>>       RETURN
>>       END
>> 
>> (Unix messages deleted...)
>> 
> 
> I think the main problem is that direct screen addressing; direct cursor
> positioning are system-dependent: even worse, they are peripheral-dependent.
> The program quoted writes an output string which contains an ANSI
> device-control escape sequence. It should work fine if sent to a DEC VT100,
> VT200, VT300 terminal (or any other VDU which emulates a DEC ANSI CRT or 
> accepts ANSI device control) by a system which passes on the escape sequence
> uncorrupted, but it won't work right on a VT52 or a printer, I think the 
> screen control sequences for HP terminals were different (but it's a looong 
> time since I programmed for one) and I'd be surprised if it did work under 
> Unix on a Sun.
> Direct cursor addressing isn't standard Fortran. (Fortran carriage-controls 
> for printers IS standard, but we have one printer that doesn't recognise 
> them...)
>

Sorry. I'd missed the function reference in the PRINT statement (I read it
as an array reference - oops). Thank you to those of you who have been patient
enough to point out the error of my ways.

I think what I was talking about was:
 
       CHARACTER*8 cursor,cls*4
       character*8 string
       cls = CHAR(27)//'[2J'
       i   = 5
       j   = 35
       string = cursor(i,j)
       PRINT *,cls,string,'I am here!'
       END
 
       CHARACTER FUNCTION cursor*8(row,col)
       CHARACTER*1 esc,c1*2,c2*2
       INTEGER row,col
       esc = CHAR(27)
       WRITE(c1,'(i2.2)') row
       WRITE(c2,'(i2.2)') col
       cursor = esc//'['//c1//';'//c2//'H'
       RETURN
       END
			which just leaves the (system-dependent) device
controls to be fixed. 

I *will* try to pay closer attention in future...

 Trevor Ingham, Systems Programmer, Christchurch City Council, New Zealand.
 trevor@ccc.govt.nz