Inside COBOL #53 (Control Y trap processing)
by
Shawn Gordon
President
The Kompany

You ever notice how you can use the +Y key combination to do interrupt processing in many programs such as Query, MPEX, and Suprtool? Have you ever wondered how they do it? I did, so many years ago I learned just enough SPL to write a control Y trap, because in my ignorance, I didn’t think it could be done in COBOL, and I am embarrassed to say that I never noticed the information in the COBOL manual.

Our tip this month comes by way of Tim Ericson at Denkor Dental Management Corp. (the NewsWire hat is in the mail). I would like to thank Tim, and Tim would like to thank the HP COBOL II/XL Programmer’s Guide, pages 4-55 and 4-56. See figure 1 for an example of the code that arms, and handles the +Y trap.

Ok, let’s go over what is going to happen here. Tim’s comments at the top of the code tell you how to use the program, but let’s talk about what may not be so obvious. First the call to HPMYPROGRAM will return the fully qualified MPE file name of the program that is calling it, which we will need for our next call to HPGETPROCPLABEL. This intrinsic will search for the named procedure label, in our case the entry for “CONTROL-Y-TRAP” to use as the interrupt address. It starts searching from the named program until it finds the procdure label, and returns the address into our variable PLABEL. We then call XCONTRAP, which is the actual trap handler. Here we pass the PLABEL of our interrupt, and the intrinsic will return the procedure label of the current address in the code.

This subroutine should only have to be called once from your main program. Once the trap is enabled, any time the user hits +Y is hit, you will end up calling the “CONTROL-Y-TRAP” entry point and displaying the information in that routine. It is up to your calling process to decide what to do with this information. The entry point will also re-arm the trap with the call to RESETCONTROL.

Some good applications of a control Y trap is when you are serially reading large amounts of data from a data set or a flat file. If you are keeping counters then the user can easily tell what their progress is, and dump out if desired. This can be a handy debugging tool as well if you use it correctly.

Well that wraps it up for this month, next month I am finally going to get to COBOL macros like I’ve been promising for months. Keep those cards and letters coming.

Figure 1

    $PAGE
   $CONTROL BOUNDS, DYNAMIC, LIST, POST85, SOURCE 

  **************************************************************** 
  *                                                              * 
  *  CONTROLY.SOURCE            11/15/94          T. ERICSON     * 
  *                                                              * 
  *  THIS SUBPROGRAM ARMS A CONTROL-Y TRAP, AND MUST BE CALLED   * 
  *  FROM A MAIN PROGRAM.  THE FOLLOWING CODE MUST BE INCLUDED   * 
  *  IN THE MAIN PROGRAM:                                        * 
  *                                                              * 
  *           .                                                  * 
  *           .                                                  * 
  *           .                                                  * 
  *   WORKING-STORAGE SECTION.                                   * 
  *                                                              * 
  *   01  CONTROL-Y    EXTERNAL    PIC X.                        * 
  *       88  CONTROL-Y-HIT        VALUE "Y".                    * 
  *       88  CONTROL-Y-OFF        VALUE "N".                    * 
  *                                                              * 
  *           .                                                  * 
  *           .                                                  * 
  *           .                                                  * 
  *   PROCEDURE DIVISION.                                        * 
  *                                                              * 
  *       CALL "ARM-CONTROL-Y".                                  * 
  *                                                              * 
  *           .                                                  * 
  *           .                                                  * 
  *           .                                                  * 
  *   LOOP-POINT.                                                * 
  *                                                              * 
  *           .                                                  * 
  *           .                                                  * 
  *           .                                                  * 
  *       IF CONTROL-Y-HIT                                       * 
  *           DISPLAY "DO YOU WISH TO CONTINUE? "                * 
  *               WITH NO ADVANCING                              * 
  *           ACCEPT ANSWER                                      * 
  *           IF ANSWER = "N"                                    * 
  *               STOP RUN                                       * 
  *           END-IF                                             * 
  *           SET CONTROL-Y-OFF TO TRUE.                         * 
  *                                                              * 
  *       GO TO LOOP-POINT.                                      * 
  *           .                                                  * 
  *           .                                                  * 
  *           .                                                  * 
  *                                                              * 
  *  COMPILE AND LINK THE TWO SOURCE FILES TOGETHER LIKE THIS:   * 
  *                                                              * 
  *   :COB85XL MAINSOURCE, USLMAIN                               * 
  *   :COB85XL CONTROLY.SOURCE, USLSUB                           * 
  *   :LINK FROM=USLMAIN, USLSUB; TO=PROGRAMFILE                 * 
  *                                                              * 
  **************************************************************** 

 $PAGE
   IDENTIFICATION DIVISION.
   PROGRAM-ID. ARM-CONTROL-Y.
   DATA DIVISION.
   WORKING-STORAGE SECTION.

   01  PROCNAME              PIC  X(20) VALUE "!control_y_trap!". 
   01  PLABEL                PIC S9(09) COMP.
   01  OLDPLABEL             PIC S9(09) COMP. 
   01  PROGFILE              PIC  X(40).
   01  CONTROL-Y   EXTERNAL  PIC  X.
       88  CONTROL-Y-HIT     VALUE "Y". 
       88  CONTROL-Y-OFF     VALUE "N". 8.2
   PROCEDURE DIVISION.
   01-START.

       CALL INTRINSIC "HPMYPROGRAM" 
            USING PROGFILE.
       CALL INTRINSIC "HPGETPROCPLABEL"
            USING PROCNAME PLABEL \\ PROGFILE. 
       CALL INTRINSIC "XCONTRAP"
            USING PLABEL OLDPLABEL. 
       EXIT PROGRAM.

  ENTRY "CONTROL-Y-TRAP".
       DISPLAY "< CONTROL Y >".
       DISPLAY " ".
       SET CONTROL-Y-HIT TO TRUE.

       CALL INTRINSIC "RESETCONTROL".