HP 3000 Manuals

EXTERNAL Data Items and Files [ HP COBOL II/XL Programmer's Guide ] MPE/iX 5.0 Documentation


HP COBOL II/XL Programmer's Guide

EXTERNAL Data Items and Files 

EXTERNAL data items and files can be shared by two or more programs (for
the purpose of this discussion, a subprogram is also a program).  For
programs to share an EXTERNAL item:

   *   Each program must declare the EXTERNAL item (data item or file).

   *   Each program must give the EXTERNAL item exactly the same name,
       because the Link Editor matches it to itself in all programs by
       its external name (see "External Naming Convention").

   *   Programs that share the EXTERNAL item must either be linked
       together or reside in the same object module in an executable
       library.  (To put programs in the same object module in an
       executable library, use the MERGE option of the Link Editor
       command ADDXL).

In the case of shared EXTERNAL records, there is an easy way to ensure
that their names and the names of the items within them are exactly the
same in the programs that share them:  put their declarations in a COPY
library and copy the library into each program with the COPY statement.

For an EXTERNAL record, the compiler generates one external name.  To
find the actual location of an EXTERNAL record, consult the Link Map (see
Chapter 7 ).

The compiler generates two external names for an EXTERNAL file--one for
the FD name (based on the file name) and one for the record area (which
is the file name with "_buffer_" appended to it).

Uses for EXTERNAL data items and files are:

   *   To allow a main program and separately compiled subprograms to
       share files and data (nested programs can share files and data
       using the GLOBAL clause).

   *   To pass parameters between programs without the USING phrase.

       Programs that change data other than that passed through the USING
       phrase have side effects, though, so be very careful.

   *   To reduce program file size.

       EXTERNAL items are stored in space allocated at run time, while
       internal items are stored in the program file.  Therefore, you can
       significantly reduce program file size by declaring records that
       contain huge arrays EXTERNAL.


NOTE If a file is declared EXTERNAL in one program, and is opened in another program that uses the EXCLUSIVE statement, then the first program must declare the file with L in the ASSIGN clause. This enables dynamic locking. Similarly, if a file is declared EXTERNAL in a program that does not write to it, it must declare it with CCTL in the ASSIGN clause to enable CCTL.
Example This example shows how to invoke CONTROL Y traps from COBOL. The main program executes a loop until CONTROL Y is pressed. The subprogram arms the CONTROL Y trap to execute its secondary entry point when CONTROL Y is pressed. The main program and subprogram communicate through EXTERNAL data items. The following is the main program: 001000 IDENTIFICATION DIVISION. 001100 PROGRAM-ID. CONTROL-Y-TEST. 001200 ENVIRONMENT DIVISION. 001300 CONFIGURATION SECTION. 001400 SPECIAL-NAMES. 001600 SYMBOLIC CHARACTERS BELL IS 8. 001700 DATA DIVISION. 001800 WORKING-STORAGE SECTION. 001900 1 TOTAL PIC S9(9) COMP VALUE 0. 002100 1 CONTROL-Y EXTERNAL PIC X. 002200 88 CONTROL-Y-HIT VALUE "Y". 002250 88 CONTROL-Y-OFF VALUE "N". 002600 1 LOTS-OF-STUFF EXTERNAL. 002700 5 PIC X(40).[REV BEG] 002300 PROCEDURE DIVISION. 002350 P1.[REV END] 002400 SET CONTROL-Y-OFF TO TRUE. 002500 MOVE ALL "*" TO LOTS-OF-STUFF. 002600 CALL "ARM-CONTROL-Y". 002700 LOOP. 002800 DISPLAY "HI" WITH NO ADVANCING. 002900 ADD 1 TO TOTAL. 003000 IF NOT CONTROL-Y-HIT GO LOOP. 003100* 003200 DISPLAY BELL. 003300 DISPLAY "control-y was hit after " TOTAL " times". 003400 DISPLAY LOTS-OF-STUFF. 003500 SET CONTROL-Y-OFF TO TRUE. 003600 MOVE 0 TO TOTAL. 003700 GO TO LOOP. The following is the subprogram: 000100$CONTROL DYNAMIC 001200 IDENTIFICATION DIVISION. 001300 PROGRAM-ID. ARM-CONTROL-Y. 001800 DATA DIVISION. 001900 WORKING-STORAGE SECTION. 002000 1 TOTAL PIC S9(9) COMP VALUE 0. 002100 1 PROCNAME PIC X(20) VALUE "!control_y_trap!". 002200 1 PLABEL PIC S9(9) COMP. 002300 1 OLDPLABEL PIC S9(9) COMP. 002400 1 PROGFILE PIC X(40). 002500* another way to pass data 002510 1 CONTROL-Y EXTERNAL PIC X. 002520 88 CONTROL-Y-HIT VALUE "Y". 002530 88 CONTROL-Y-OFF VALUE "N". 002600 1 LOTS-OF-STUFF EXTERNAL. 002700 5 PIC X(40). 002800 PROCEDURE DIVISION. 002850 P1. 002900* get plabel from hpgetprocplabel 003000 CALL INTRINSIC "HPMYPROGRAM" USING PROGFILE. 003100 CALL INTRINSIC "HPGETPROCPLABEL" USING PROCNAME PLABEL \\ 003200 PROGFILE. 003300* call xcontrap 003400 CALL INTRINSIC "XCONTRAP" USING PLABEL OLDPLABEL. 003500 EXIT PROGRAM. 003600* 003700 ENTRY "CONTROL-Y-TRAP". 003800 SET CONTROL-Y-HIT TO TRUE. 003900 MOVE "Trap routine reenabled again" TO LOTS-OF-STUFF. 004000* reenable for next time 004100 CALL INTRINSIC "RESETCONTROL". EXTERNAL Items and FORTRAN The Link Editor matches FORTRAN named common blocks and COBOL EXTERNAL records by name (one FORTRAN named common block matches one COBOL EXTERNAL record). If the FORTRAN named common block declares more than one variable, it is your responsibility to align the elementary items of the COBOL record along the proper boundaries. You may have to specify unused bytes with FILLER. Remember that COBOL aligns all USAGE BINARY SYNCHRONIZED data items along 32-bit boundaries, regardless of data item size (unless you compile the program with the SYNC16 control option, in which case these data items are 16-bit-aligned). EXTERNAL Items and Pascal The Link Editor matches Pascal variables declared in the outer block and COBOL EXTERNAL records. The Pascal program must be compiled with[REV BEG] the EXTERNAL, GLOBAL, or GLOBAL and EXTERNAL options.[REV END] Every variable in the outer block of the Pascal program must have a matching COBOL EXTERNAL record[REV BEG] if the Pascal EXTERNAL option is used.[REV END] This applies only between Pascal and COBOL. [REV BEG] EXTERNAL Items and C The Link Editor matches C global variables and COBOL EXTERNAL records.[REV END] [REV BEG] Sharing EXTERNAL Items When two COBOL programs share EXTERNAL items, one program can declare some EXTERNAL items that the other program does not. When a COBOL and a FORTRAN (or C) program share EXTERNAL items, the COBOL program can declare EXTERNAL records that do not correspond to FORTRAN named common blocks (or C globals) or vice versa.[REV END] COBOL, FORTRAN, and Pascal Example This COBOL main program, FORTRAN subprogram, and Pascal subprogram can pass information to each other through shared EXTERNAL items. The following is the COBOL main program with EXTERNAL records: 001000 IDENTIFICATION DIVISION. 001100 PROGRAM-ID. COBEXT. 001200 DATA DIVISION. 001300 WORKING-STORAGE SECTION. 001400 01 A EXTERNAL. 001500 05 I PIC S9(9) BINARY. 001600 05 J PIC S9(4) BINARY. 001610 05 FILLER PIC XX. 001620 05 K PIC S9(9) BINARY. 001700 PROCEDURE DIVISION. 001800 P1. 001900 CALL "PAS". 002000 MOVE -7 TO I. 002000 MOVE -8 TO J. 002000 MOVE -9 TO K. 002100 CALL "FTN". 002200 DISPLAY "I=" I ", J=" J ", K=" K. The following is the FORTRAN subprogram with named common block: SUBROUTINE FTN COMMON /A/ I,J,K INTEGER*4 I,K INTEGER*2 J WRITE(6,*) I,J,K END The following is the Pascal subprogram. It is compiled with the EXTERNAL option: $EXTERNAL,SUBPROGRAM$ PROGRAM STUFF; TYPE COM = RECORD I: INTEGER; J: SHORTINT; K: INTEGER; END; VAR A : COM; PROCEDURE FTN; EXTERNAL FTN77; PROCEDURE PAS; BEGIN A.I:=5; A.J:=-5; A.K:=6; FTN; END; BEGIN END.


MPE/iX 5.0 Documentation