#!/bin/sh set -x ################################################################################ # USAGE=' This script tries to build a Fortran 2003, Fortran 90, and FORTRAN 77 version, looking around for common combinations of compilers. When it is done, try some of the executables and see if any of them works. This is a quick and dirty way to see what might work The log will be in getkey.log ' # ################################################################################ CLEANUP(){ rm -f getkey.exe getkey.o m_getkey.mod getkey # clean up rm -f f90.o f77.o M_GETKEY.mod echo ================================================================================ } ################################################################################ RUN(){ CLEANUP set -x $CC $COPTS -c getkey.c $FC $FOPTS $SOURCE getkey.o -o getkey+${CC}+${FC}+$NAME # demonstrate non-interactive behavior echo 'abcdefghijklmnopqrstuvwxyz'|./getkey+${CC}+${FC}+$NAME # demonstrate interactive behavior #echo 'Running interactively -- strike "q" key to quit or if things look stuck' #./getkey+${CC}+${FC}+${NAME} } ################################################################################ RUNF03(){ echo "$CC + $FC and ISO_C_BINDING and f03 test program" SOURCE=f2003.f90 NAME=f03 RUN } ################################################################################ RUNFC(){ echo "$CC + $FC and guess at C/F interface and f90 test program" SOURCE=f90.f90 NAME=f90 RUN } ################################################################################ RUNF77(){ echo "$CC + $FC and guess at C/F interface and f77 test program" SOURCE=f77.f NAME=f77 RUN } ################################################################################ RUNALL(){ RUNF03 RUNFC RUNF77 } ################################################################################ export CC FC FOPTS COPTS ################################################################################ cat > getkey.c <<\EOF /* * @(#) Driver for reading a character from keyboard in raw I/O mode */ #include #include #include #include #include /*--------------------------*/ #ifdef BSD #include #else /*--------------------------*/ #ifdef Linux #define G77 #endif #ifdef CYGWIN #define G77 #endif #ifdef G77 #include #else #include #endif /*--------------------------*/ #endif #include /*V13 #include */ /*V13 #include */ /******************************************************************************/ /* return the next key typed in hot (raw I/O) mode. */ char getkeyC(void) { #ifdef BSD struct sgttyb oldtty, newtty; char c; ioctl(0, TIOCGETP, &oldtty); newtty = oldtty; newtty.sg_flags = RAW; ioctl(0, TIOCSETP, &newtty); read(0, &c, 1); ioctl(0, TIOCSETP, &oldtty); #else struct termio oldtty, newtty; /*V13 struct termios oldtty, newtty; */ char c; ioctl(0, TCGETA, &oldtty); /*V13 tcgetattr(0, &oldtty); */ newtty = oldtty; newtty.c_iflag = BRKINT | IXON | ISTRIP; newtty.c_lflag = 0; newtty.c_cc[VEOF] = 1; ioctl(0, TCSETA, &newtty); /*V13 tcsetattr(0, TCSANOW, &newtty); */ read(0, &c, 1); ioctl(0, TCSETA, &oldtty); /*V13 tcsetattr(0, TCSANOW, &oldtty); */ #endif /* fprintf(stderr,"C:c=%c\n",c); */ /* fflush(stdout); */ return(c); } /******************************************************************************/ /* Commonly, a C routine called name_ can be called from Fortran as name; plus less-common ones */ int getkey4f_(void) { return(getkeyC()); } int _getkey4f(void) { return(getkeyC()); } int getkey4f(void) { return(getkeyC()); } int GETKEY4F(void) { return(getkeyC()); } /******************************************************************************/ #ifdef TESTPRG /* THE REMAINING LINES ARE A TEST PROGRAM. REMOVE THEM AFTER TESTING THE ROUTINE */ /* read keys in hot (raw I/O) mode until letter q is hit */ main(){ char keyvalue; keyvalue=0; fprintf(stdout,"press keys ('q' to quit)\n"); fflush(stdout); fflush(stdin); while(keyvalue!='q'){ keyvalue=getkeyC(); /*keyvalue=getkey_();*/ fprintf(stdout,"C:KEY=%c %d\n",keyvalue,keyvalue); } } #endif EOF ################################################################################ cat > f2003.f90 <<\EOF !=======================================================================-------- !()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() !=======================================================================-------- ! These routines are available for general use. I ask that you send me ! interesting alterations that are available for public use; and that you ! include a note indicating the original author -- John S. Urban ! Last updated May 5th, 2009 !=======================================================================-------- !()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() !=======================================================================-------- ! make Fortran/C interface for C routine getkey(3C) module M_getkey use iso_c_binding implicit none public interface function getkey() bind(c, name='getkeyC') use iso_c_binding implicit none character(kind=c_char) :: getkey end function getkey end interface end module M_getkey !=======================================================================-------- !()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() !=======================================================================-------- !------------------------------------------------------------------------------- program test_getkey use M_getkey character :: A integer :: icount icount=0 write(*,*)'begin striking keys to demonstrate interactive raw I/O mode' write(*,*)'q to quit; up to 40 characters' do A=getkey() icount=icount+1 write(*,*)icount,' f03:key=',A,'->',ichar(A) !flush(6) if(A.eq.'q')stop if(icount.gt.40)stop enddo end program test_getkey EOF ################################################################################ cat > f90.f90 <<\EOF !=======================================================================-------- !()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() !=======================================================================-------- ! These routines are available for general use. I ask that you send me ! interesting alterations that are available for public use; and that you ! include a note indicating the original author -- John S. Urban ! Last updated May 5th, 2009 !=======================================================================-------- !()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() !=======================================================================-------- program test_getkey ! test program for debugging getkey(3F) and getkey(3C) character :: A character :: GETKEY icount=0 write(*,*)'begin striking keys to demonstrate interactive raw I/O mode' write(*,*)'q to quit; stops after 40 characters' do A=getkey() icount=icount+1 write(*,*)icount,' f90:key=',A,'->',ichar(A) if(icount.gt.40)stop if(A.eq.'q')stop enddo end program test_getkey character function getkey() ! call the C routine and convert the integer to a character integer getkey4F getkey=char(getkey4F()) !flush(6) ! usually not required, extension end function getkey EOF ################################################################################ cat > f77.f <<\EOF C=======================================================================-------- C()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() C=======================================================================-------- C These routines are available for general use. I ask that you send me C interesting alterations that are available for public use; and that you C include a note indicating the original author -- John S. Urban C Last updated May 5th, 2009 C=======================================================================-------- C()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()() C=======================================================================-------- program test_getkey C test program for debugging getkey(3F) and getkey(3C) character*(1) A,GETKEY write(*,*)'begin striking keys to test interactive raw I/O mode' write(*,*)'q to quit; stops after 40 characters' do 10 i10=1,40 A=getkey() write(*,*)i10,' f77:key=',A,'->',ichar(A) if(i10.gt.40)stop if(A.eq.'q')stop 10 continue end character function getkey() C call the C routine and convert the integer to a character integer getkey4F getkey=char(getkey4F()) C usually not required, extension !call flush(6) return end EOF ################################################################################ ( exec 2>&1 ################################################################################ cat <<\EOF This script may need tweeked. It tries a few of the most common things that work, and can be handy at giving you a quick idea of the best method to pursue EOF echo 'TRY C STANDALONE' ############### rm -f ./getkey; cc -DTESTPRG getkey.c -o getkey if [ -x ./getkey ] then SWITCH= echo 'abcdq'|./getkey fi ############### rm -f ./getkey; cc -DTESTPRG -DLinux getkey.c -o getkey if [ -x ./getkey ] then SWITCH='-DBSD' echo 'abcdq'|./getkey fi ############### rm -f ./getkey; cc -DTESTPRG -DBSD getkey.c -o getkey if [ -x ./getkey ] then SWITCH='-DLinux' echo 'abcdq'|./getkey fi ############### echo "SWITCH $SWITCH" ################################################################################ set +x echo 'TRY FORTRAN' # look around for some compilers [ -f "`which ifort 2>/dev/null`" ] && FC='ifort' FOPTS='-warn all' CC="icc" COPTS="$SWITCH" RUNALL [ -f "`which g95 2>/dev/null`" ] && FC='g95' FOPTS='-Wall' CC="gcc" COPTS="$SWITCH" RUNALL [ -f "`which f90 2>/dev/null`" ] && FC='f90' FOPTS= CC="cc" COPTS="$SWITCH" RUNALL [ -f "`which f95 2>/dev/null`" ] && FC='f95' FOPTS= CC="cc" COPTS="$SWITCH" RUNALL [ -f "`which f77 2>/dev/null`" ] && FC='f77' FOPTS= CC="cc" COPTS="$SWITCH" RUNF77 [ -f "`which g77 2>/dev/null`" ] && FC='g77' FOPTS='-fno-backslash' CC="gcc" COPTS="$SWITCH" RUNF77 [ -f "`which gfortran 2>/dev/null`" ] && FC='gfortran' FOPTS= CC="gcc" COPTS="$SWITCH" RUNALL [ -f "`which fort77 2>/dev/null`" ] && FC='fort77' FOPTS= CC="cc" COPTS="$SWITCH" RUN77 [ -f "`which xlf 2>/dev/null`" ] && FC='xlf' FOPTS='-qextname' CC="cc" COPTS="$SWITCH" RUNALL [ -f "`which path95 2>/dev/null`" ] && FC='path95' FOPTS= CC="cc" COPTS="$SWITCH" RUNALL [ -f "`which f95 2>/dev/null`" ] && FC='f95' FOPTS= CC="cc" COPTS="$SWITCH" RUNALL ################################################################################ CLEANUP ls -ltrasd getkey+* )|tee getkey.log echo "$USAGE" exit #[ -f "`which gfortran &2>/dev/null`" ] && FC='gfortran -I. -J. -static -Wuninitialized -g -O ' CC='gcc' RUNALL #[ -f "`which g95 &2>/dev/null`" ] && FC='g95 -I. -fmod=. -Wall -O3 ' CC='gcc' RUNALL #[ -f "`which xlf &2>/dev/null`" ] && FC='xlf -qextname' CC=' ' RUNALL #[ -f "`which pathf95 &2>/dev/null`" ] && FC='pathFC -I. -module .' CC=' ' RUNALL #[ -f "`which f95 &2>/dev/null`" ] && FC='f95 -I. -fmod=. ' CC=' ' RUNALL #[ -f "`which f95 &2>/dev/null`" ] && FC='f95 -I. -module .' CC=' ' RUNALL #[ -f "`which ifort &2>/dev/null`" ] && FC='ifort -I . -module . -traceback -warn all' CC='icc' RUNALL