Inside COBOL #45
Well I didn’t have time, or opportunity to compile any interesting COBOL type items from HP World, hopefully I will have something by next issue. So this month I bring to you a little information on programmatically using operating system variables. You probably/hopefully know how to use the OS variables, i.e., SHOWVAR myvar, or SETVAR,myvar, or DELETEVAR myvar. However, what if you want to access these babies from your program?
OS Variables are a logical extension to the old JCW’s, but JCW’s could only contain integer data. Variables can contain integer, string, and boolean data, which makes for a pretty versatile vehicle for getting input into your program, or putting data out.
First there are essentially three intrinsics you need to be concerned with, they are HPCIDELETEVAR, HPCIGETVAR, HPCIPUTVAR. This gives you the same functionality as you do from the command interpreter. To retrieve variables we specify a type of 1 for integer, 2 for string and 3 for boolean. Take a look at figure 1 and you will see a small example of a macro that will try to return both an integer and string value for every variable it returns. Later you will see the use of the GETVAR macro, and how we use it to get and store various string and integer data.
Booleans are another issue however, and the HPTYPEAHEAD variable is a good one to use as an example. In figure 2 we want to see if the HPTYPEAHEAD variable is on, since it is a boolean it will return a value of 1 if it is true. If it is true, then I want to set it to false, so I use HPCIPUTVAR with HPTYPEAHEAD set to 0, and that’s it.
Another way to do the SET and DELETE is to just use the COMMAND or HPCICOMMAND intrinsic and pass “SETVAR HPTYPEAHEAD,FALSE” or “DELETEVAR MYVAR”. Sometimes this is much easier than going through and setting up the whole environment for calling the intrinsics. I typically only use the HPCIGETVAR, the rest I use the HPCICOMMAND intrinsic now, because I always have that macro set up in my code.
Ok, so after 4 years of saying I am out of ideas for this column, and only missing 1 issue, I really really am running out of tips and information. So anyone with any clever ideas, like how to use NETIPC from COBOL, or something like that, pony up and share with the rest of us.
Figure 1 01 VAR-NAME PIC X(40) VALUE SPACES. 01 VAR-INT PIC S9(9) COMP VALUE 0. 01 VAR-STRING PIC X(255) VALUE SPACES. * 01 VAR-STATUS. 03 VS-1 PIC S9(4) COMP VALUE 0. 03 VS-2 PIC S9(4) COMP VALUE 0. * $DEFINE %GETVAR= MOVE SPACES TO VAR-STRING MOVE ZEROES TO VAR-INT MOVE !1 TO VAR-NAME CALL INTRINSIC "HPCIGETVAR" USING VAR-NAME, VAR-STATUS, 1, VAR-INT, 2, VAR-STRING, 0. IF VS-1 <> 0 DISPLAY 'Error ' VS-1 ' in HPCIGETVAR' DISPLAY 'Failed on variable ' VAR-NAME CALL INTRINSIC 'QUIT' USING \21\ END-IF# A2000-GET-VARS. %GETVAR("REC"#). COMPUTE TAPE-LEN = VAR-INT * -1. %GETVAR("BLK"#). MOVE VAR-INT TO TAPE-BLK. COMPUTE TAPE-LEN = TAPE-LEN * TAPE-BLK. %GETVAR("A_OR_E"#). MOVE VAR-STRING(1:1) TO A-OR-E. %GETVAR("DISKFILE"#). MOVE VAR-STRING(1:36) TO DISK-FILE. A2000-EXIT. EXIT. * Figure 2 01 SPECTRUM-VARS. 03 VAR-NAME PIC X(11) VALUE "HPTYPEAHEAD". 03 VAR-STATUS PIC S9(9) COMP VALUE 0. 03 VAR-ITEM-NUM PIC S9(9) COMP VALUE 3. 03 VAR-ITEM PIC S9(9) COMP VALUE 0. CALL INTRINSIC "HPCIGETVAR" USING VAR-NAME, VAR-STATUS, VAR-ITEM-NUM, VAR-ITEM. IF VAR-ITEM = 1 THEN MOVE 0 TO VAR-ITEM CALL INTRINSIC "HPCIPUTVAR" USING VAR-NAME, VAR-STATUS, VAR-ITEM-NUM, VAR-ITEM.