🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: vmsnet.sources
Article #171

Control-C handler for Fortran, part 01/01

#171
From: Ed Wilts
Date: Wed, 27 Jan 1993 21:50
358 lines
13438 bytes
Submitted-by: ewilts@galaxy.gov.bc.ca (Ed Wilts)
Posting-number: Volume 4, Issue 54
Archive-name: fortran_control-c_handler/part01

This archive consists of two Fortran modules to simply Control-C handling, one
of which is merely a test program.

By calling the Control C handler, a high-level application can easily detect if
a ^C has been issued by the terminal and take appropriate action.

Note that I am not the original author and am only making it availalbe since
somebody asked...

$! ------------------ CUT HERE -----------------------
$ v='f$verify(f$trnlnm("SHARE_UNPACK_VERIFY"))'
$!
$! This archive created by VMS_SHARE Version 8.1
$!   On 12-JAN-1993 08:15:23.86   By user EWILTS       (Ed Wilts)
$!
$! The VMS_SHARE software that created this archive
$!    was written by  Andy Harper, Kings College London UK
$!    -- September 1992
$!
$! Credit is due to these people for their original ideas:
$!    James Gray, Michael Bednarek 
$!
$! TO UNPACK THIS SHARE FILE, CONCATENATE ALL PARTS IN ORDER
$! AND EXECUTE AS A COMMAND PROCEDURE  (  @name  )
$!
$! THE FOLLOWING FILE(S) WILL BE CREATED AFTER UNPACKING:
$!       1. CONTROL_C_TRAPS.FOR;1
$!       2. TEST_CONTROL_C.FOR;1
$!
$set="set"
$set symbol/scope=(nolocal,noglobal)
$f=f$parse("SHARE_UNPACK_TEMP","SYS$SCRATCH:."+f$getjpi("","PID"))
$e="write sys$error  ""%UNPACK"", "
$w="write sys$output ""%UNPACK"", "
$ if .not. f$trnlnm("SHARE_UNPACK_LOG") then $ w = "!"
$ ve=f$getsyi("version")
$ if ve-f$extract(0,1,ve) .ges. "4.4" then $ goto start
$ e "-E-OLDVER, Must run at least VMS 4.4"
$ v=f$verify(v)
$ exit 44
$unpack: subroutine ! P1=filename, P2=checksum, P3=attributes
$ if f$search(P1) .eqs. "" then $ goto file_absent
$ e "-W-EXISTS, File ''P1' exists. Skipped."
$ delete 'f'*
$ exit
$file_absent:
$ if f$parse(P1) .nes. "" then $ goto dirok
$ dn=f$parse(P1,,,"DIRECTORY")
$ w "-I-CREDIR, Creating directory ''dn'"
$ create/dir 'dn'
$ if $status then $ goto dirok
$ e "-E-CREDIRFAIL, Unable to create ''dn' File skipped."
$ delete 'f'*
$ exit
$dirok:
$ w "-I-UNPACK, Unpacking file ''P1'"
$ n=P1
$ if P3 .nes. "" then $ n=f
$ if .not. f$verify() then $ define/user sys$output nl:
$ EDIT/TPU/NOSEC/NODIS/COM=SYS$INPUT 'f'/OUT='n'
PROCEDURE GetHex LOCAL x1,x2;x1:=INDEX(t,ERASE_CHARACTER(1))-1;x2:=INDEX(t,
ERASE_CHARACTER(1))-1;RETURN 16*x1+x2;ENDPROCEDURE;
PROCEDURE SkipPartsep LOOP EXITIF INDEX(ERASE_LINE,"-+-+-+-+-+-+-+-+")=1;
ENDLOOP;ENDPROCEDURE;
PROCEDURE ExpandChar CASE CURRENT_CHARACTER FROM ' ' TO 'z' ["`"]
:ERASE_CHARACTER(1);COPY_TEXT(ASCII(GetHex));[" "]:ERASE_CHARACTER(1);[
OUTRANGE,INRANGE]:MOVE_HORIZONTAL(1);ENDCASE;ENDPROCEDURE;
PROCEDURE ProcessLine s:=ERASE_CHARACTER(1);LOOP EXITIF CURRENT_OFFSET>=LENGTH(
CURRENT_LINE);ExpandChar;ENDLOOP;IF s="V" THEN APPEND_LINE;ENDIF;ENDPROCEDURE;
PROCEDURE AdvanceLine MOVE_HORIZONTAL(-CURRENT_OFFSET);MOVE_VERTICAL(1);
ENDPROCEDURE;PROCEDURE Decode POSITION(BEGINNING_OF(b));LOOP EXITIF MARK(NONE)=
END_OF(b);IF INDEX(CURRENT_LINE,"+-+-+-+-+-+-+-+-")=1 THEN SkipPartSep;
ELSE ProcessLine;AdvanceLine;ENDIF;ENDLOOP;ENDPROCEDURE;SET(FACILITY_NAME,
"UNPACK");SET(SUCCESS,OFF);SET(INFORMATIONAL,OFF);t:="0123456789ABCDEF";f:=
GET_INFO(COMMAND_LINE,"file_name");b:=CREATE_BUFFER(f,f);Decode;WRITE_FILE(b,
GET_INFO(COMMAND_LINE,"output_file"));QUIT;
$ if p3 .eqs. "" then $ goto dl
$ open/write fdl &f
$ write fdl "RECORD"
$ write fdl P3
$ close fdl
$ w "-I-CONVRFM, Converting record format to ", P3
$ convert/fdl=&f &f-1 &P1
$dl: delete 'f'*
$ if P2 .eqs. "" then $ goto ckskip
$ checksum 'P1'
$ if checksum$checksum .nes. P2 then $ -
  e "-E-CHKSMFAIL, Checksum of ''P1' failed."
$ exit
$ckskip: e "-W-CHKSUMSKIP, checksum validation unavailable for ''P1'"
$ endsubroutine
$start:
$!
$ create 'f'
XCLast`20Modified:`20`204-JAN-1990`2010:12:56`20by`20WILTS`20:`20CONTROL_C_TRAP
VS.FOR
Xc****************************************************************************
Xc`20Control_c.for`20--`20FJM`20--`2011/14/85
Xc****************************************************************************
Xc
Xc`20Modified`20History:
Xc
Xc`0911/11/88`20--`20FJM`20--`20Insure`20that`20multiple`20calls`20to`20enable_
Vcontrol_C_asts,
Xc`09`09`09`20`20`20with`20or`20without`20intervening`20call`20to
Xc`09`09`09`20`20`20disable_control_C_asts,`20leave`20routine
Xc`09`09`09`20`20`20Control_c_was_seen`20in`20the`20.FALSE.`20state.
Xc
Xc****************************************************************************
Xc
Xc`20These`20routines`20allow`20a`20user`20to`20enable`20a`20device`20to`20resp
Vond`20to`20control-c
Xc`20asts.`20To`20do`20this`20the`20user`20calls:
Xc
Xc`09`09status`20=`20enable_control_c_asts(device)
Xc
Xc`20where`20"device"`20is`20the`20device`20the`20control_c`20interrupt`20decte
Vction`20is`20to`20be
Xc`20enabled`20for.`20If`20the`20device`20string`20is`20blank,`20or`20the`20dev
Vice`20is`20not`20a`20terminal
Xc`20device`20(ie.`20a`20CRT,`20Hardcopy`20terminal,`20etc.)`20then`20the`20rou
Vtine`20assumes`20that
Xc`20the`20call`20was`20made`20in`20batch`20or`20detached`20mode,`20and`20does
V`20not`20establish`20an
Xc`20ast`20handler.`20In`20this`20case`20the`20status`20returned`20is`20SS$_NOR
VMAL.`20In`20all`20other
Xc`20cases,`20control_c`20ast's`20are`20enabled,`20and`20the`20status`20returne
Vd`20is`20that`20of
Xc`20the`20system`20calls`20to`20establish`20the`20ast.
Xc
Xc`20To`20detect`20the`20presence`20of`20a`20typed`20Control_c,`20the`20user
V`20calls`20the`20function
Xc
Xc`09control_c_was_seen()
Xc
Xc`20This`20function`20returns`20.true.`20if`20a`20control_c`20was`20typed`20si
Vnce`20the`20last`20time
Xc`20control_c_was_seen`20was`20called.
Xc
Xc`20Example:
Xc
Xc`09`09if(`20control_c_was_seen()`20)`20then
Xc`09`09`09`09.
Xc`09`09`09`09.
Xc`09`09`09`09.
Xc`09`09`20`20`09`20`20`20`20`20end`20if
Xc
Xc`20Calling`20Enable_Control_c_Asts`20also`20has`20the`20effect`20of`20clearin
Vg`20the
Xc`20Control_c_Was_Seen`20Flag.`20Thus`20a`20control_c_Was_Seen`20call`20is`20g
Vuaranteed`20to
Xc`20return`20.TRUE.`20only`20if`20the`20control_c`20occurs`20between`20after
V`20the`20last`20time
Xc`20the`20Enable_Control_c_Asts`20routine`20is`20called.`20Note`20that`20in
V`20general,`20through,
Xc`20the`20intent`20is`20to`20call`20the`20Enable_Control_c_Ast`20routine`20onl
Vy`20once,`20at`20the
Xc`20beginning`20of`20the`20program.
Xc
Xc`20To`20stop`20the`20effect`20of`20the`20control_c`20interrupt,`20and`20remov
Ve`20the`20ast`20handler,
Xc`20the`20user`20may`20call`20the`20routine:
Xc
Xc`09`09status`20=`20disable_control_c_asts()
Xc
Xc`20Note`20that`20these`20routines`20are`20non-deterministic`20in`20that`20if
V`20more`20than`20one
Xc`20control_c`20is`20typed`20before`20"control_c_was_seen"`20is`20called,`20th
Ve`20"extra"
Xc`20control_c's`20are`20thrown`20out,`20not`20queued`20for`20delivery.`20Also
V`20note`20that`20the
Xc`20routines`20only`20support`20one`20device`20at`20a`20time,`20thus`20the`20r
Voutine
Xc`20"enable_control_c_asts"`20may`20only`20be`20called`20once`20within`20a`20p
Vrogram
Xc`20(unless`20the`20user`20calls`20disable_control_c_asts).`20If`20called`20mo
Vre`20than
Xc`20once`20without`20calling`20disable_control_c_asts,`20no`20action`20is`20ta
Vken,
Xc`20other`20then`20the`20above`20mentioned`20clearing`20of`20the`20control_c_w
Vas_Seen`20flag,
Xc`20and`20the`20status`20code`20of`20ss$_normal`20is`20returned`20(as`20contro
Vl_c`20ast's`20are
Xc`20still`20enabled).
Xc
Xc****************************************************************************
X`20
X`09integer*4`20function`20`20Enable_control_c_asts(device)
Xc
X`09implicit`09none
X`20
X`09character*(*)`09device
X`09integer*4`09i,`20length,`20status,`20devtype
X`09integer*4`09channel`09`09/0/
X`09integer*4`20`09sys$qiow,`20sys$assign,`20sys$dassgn
X`09integer*4`20`09lib$getdvi
X`09integer*4`20`09reenable_control_c_ast
X`09integer*4`20`09disable_control_c_asts
X`09integer*4`20`09control_c_was_seen
X`20
X`09external`09dc$_term
X`09external`09ss$_normal,`20ss$_abort
X`09external`09dvi$_devclass
X`09external`09io$_setmode
X`09external`09io$m_ctrlcast
X`09external`09control_c_ast_handler
X`20
X`09save`09`09channel
X`20
Xc`20Insure`20the`20routine`20Control_c_Was_Seen`20is`20in`20a`20known`20state
V`20(.false.)
Xc`20by`20eating`20any`20value`20it`20currently`20has.
X`20
X`09status`20=`20Control_c_Was_Seen()
X`20
Xc`20If`20we`20have`20already`20established`20a`20control_c`20handler,`20channe
Vl`20will`20be
Xc`20non-zero,`20so`20do`20nothing`20more`20than`20return.
X`20
X`09enable_control_c_asts`20=`20%loc(ss$_normal)
X`09if(`20channel`20.ne.`200)`20then
X`09`20`20return
X`09`20`20end`20if
X`20
Xc`20return`20with`20no`20action`20if`20the`20device`20string`20is`20blank,`20o
Vr`20if`20the
Xc`20device`20is`20not`20a`20terminal.`20This`20allows`20programs`20to`20run
V`20okay`20from
Xc`20batch`20mode`20and`20detached`20mode.
X`20
X`09do`20i`20=`20len(device),1,-1
X`09`20`20if(device(i:i).gt.'`20')go`20to`2010
X`09`20`20end`20do
X`09return
X10`09length`20=`20i
X`20
X`09status`20=`20lib$getdvi(%loc(dvi$_devclass),,device(1:length),devtype)
X`09if(`20.not.`20status`20)`20then
X`09`20`20enable_control_c_asts`20=`20status
X`09`20`20return
X`09`20`20end`20if
X`20
X`09if(`20devtype`20.ne.`20%loc(dc$_term))`20return
X`20
Xc`20if`20we`20got`20here,`20we`20have`20a`20terminal`20device.`20Assign`20the
V`20channel
X`20
X`09status`20=`20sys$assign(device(1:length),channel,,)
X`09if`20(.not.status)`20then
X`09`20`20enable_control_c_asts`20=`20status
X`09`20`20return
X`09`20`20end`20if
X`20
Xc`20and`20establish`20the`20interrupt`20handler.
X`20
X`09status`20=`20sys$qiow(%val(0),%val(channel),
X`091`09`20`20%val(%loc(io$_setmode)`20.or.`20%loc(io$m_ctrlcast)),
X`092`09`20`20,,,control_c_ast_handler,,,,,)
X`09enable_control_c_asts`20=`20status
X`09return
X`20
Xc`20This`20is`20the`20entry`20point`20for`20removing`20the`20control_c`20asts
X`20
X`09entry`09disable_control_c_asts
X`20
X`09disable_control_c_asts`20=`20sys$dassgn(%val(channel))
X`09channel`20=`200
X`20
Xc`20`20`20`20`20call`20the`20detection`20routine.`20This`20insures`20that`20co
Vntrol_c_was_seen
Xc`20`20`20`20`20is`20left`20in`20the`20.false.`20state,`20if`20the`20user`20ca
Vlls`20it`20again`20after`20this.
X`20
X`09status`20=`20control_c_was_seen()
X`09return
X`20
Xc`20this`20is`20the`20entry`20point`20which`20re_enables`20the`20control_c`20a
Vsts
Xc`20when`20it`20is`20read`20out.`20It`20is`20needed`20here`20since`20FORTRAN
V`20requires`20that
Xc`20no`20external`20reference`20can`20be`20made`20to`20"control_c_ast_handler"
V
Xc`20from`20within`20that`20subroutine.`20(ie.`20the`20compiler`20thinks`20it
V`20is`20detecting
Xc`20recursion).`20Thus`20we`20trick`20it`20by`20indirection.`20(It`20also`20sa
Vves`20having
Xc`20to`20pass`20"channel"`20around`20anyway.
X`20
X`09entry`20reenable_control_c_ast
X`09status`20=`20sys$qiow(%val(0),%val(channel),
X`091`09`20`20%val(%loc(io$_setmode)`20.or.`20%loc(io$m_ctrlcast)),
X`092`09`20`20,,,control_c_ast_handler,,,,,)
X`09if(`20.not.`20status)`20call`20lib$signal(%val(status))
X`09reenable_control_c_ast`20=`20status
X`09return
X`20
X`09end
X`20
Xc`20function`20control_c_was_seen()
Xc
Xc`20This`20function`20is`20called`20by`20the`20user`20to`20test`20if`20a`20con
Vtrol_c`20asts
Xc`20has`20occured`20since`20the`20last`20call`20to`20control_c_was_seen.`20The
V`20routine
Xc`20returns`20.true.`20or`20.false.`20as`20appropriate,`20and`20then`20re-enab
Vles`20the`20control-y
Xc`20asts.
X`20
X`09logical*4`20function`20control_c_was_seen
X`20
X`09logical*4`09was_seen`09/.false./
X`09volatile`09was_seen
X`09save`09`09was_seen
X`20
X`09control_c_was_seen`20=`20was_seen
X`09if(`20was_seen`20)`20then
X`09`20`20was_seen`20=`20.false.
X`09`20`20end`20if
X`09return
X`20
Xc`20This`20entry`20point`20is`20the`20control_c`20asts`20handler
X`20
X`09entry`20control_c_ast_handler
X`20
X`09call`20reenable_control_c_ast
X`09was_seen`20=`20.true.
X`09return
X`09end
$ call unpack CONTROL_C_TRAPS.FOR;1 1549371480 ""
$!
$ create 'f'
XCLast`20Modified:`20`204-JAN-1990`2010:26:55`20by`20WILTS`20:`20TEST.FOR
X`09logical`20`09loop_flag/.true./,
X`091`09`09control_c_was_seen
X
X`09call`20enable_control_c_asts('tt')
X
X`09do`20while`20(loop_flag)
X`09`09type`20*,'looping....'
X`09`09if`20(control_c_was_seen())`20then`20
X`09`09`09loop_flag`20=`20.false.
X`09`09`09type`20*,'setting`20loop_flag`20false'`09
X`09`09endif
X`09end`20do
X
X`09call`20exit
X`09end
$ call unpack TEST_CONTROL_C.FOR;1 739779678 ""
$ v=f$verify(v)
$ exit

-- 
Ed Wilts, BC Systems, 4000 Seymour Place, Victoria, B.C., Canada, V8X 4S8
EWilts@Galaxy.Gov.BC.CA     Office:  (604) 389-3430   Fax: (604) 389-3412
Technical Details
Message-ID: <10360095@MVB.SAIC.COM>
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!spool.mu.edu!howland.reston.ans.net!usc!sol.ctr.columbia.edu!hamblin.math.byu.edu!arizona.edu!mvb.saic.com!vmsnet-sources

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!agate!spool.mu.edu!howland.reston.ans.net!usc!sol.ctr.columbia.edu!hamblin.math.byu.edu!arizona.edu!mvb.saic.com!vmsnet-sources
Newsgroups: vmsnet.sources
Subject: Control-C handler for Fortran, part 01/01
Message-ID: <10360095@MVB.SAIC.COM>
From: ewilts@galaxy.gov.bc.ca (Ed Wilts)
Date: Wed, 27 Jan 1993 21:50:04 GMT
Followup-To: vmsnet.sources.d
Organization: BC Systems Corporation
Summary: ^C enable/disable/trap
Approved: Mark.Berryman@Mvb.Saic.Com
Lines: 358