IBM Bob on z/OS with Enterprise COBOL: fix a program until it passes the test
In IBM Bob with COBOL and Podman on RHEL Bob fixed a COBOL program until it passed the test, with IBM COBOL for Linux in a container. And in IBM Bob Shell on z/OS: install it in USS and a first test I installed Bob Shell on my z/OS 3.1 (ADCD).
In this article I put the two together: the same program with errors and the same test, but on the mainframe, with IBM Enterprise COBOL for z/OS. Bob compiles, runs and fixes the program on z/OS, started from a job.
I start from what I installed in that article: Bob Shell in the zFS mounted on /u/bobshell, Node.js 22 and the API key in $HOME/.bob-api-key.
This is a lab idea: a way to try, in isolation, something that occurred to me. There are probably better or more complete ways to do it. The idea is to see that it works and that everyone adapts it to what they need, always taking into account what the IBM licenses allow.
Enterprise COBOL in USS: cob2
Enterprise COBOL for z/OS has a command to compile from z/OS UNIX: cob2. On my ADCD it is in the folder of version 6.4:
ls /usr/lpp/IBM/cobol/*/bin/cob2
First of all, I check that it compiles and runs a Hello World. As in the Bob Shell on z/OS article, I do everything with jobs: the first step writes the program to /u/bobshell/work, the second one writes the script and the third one runs it with BPXBATCH.
//COBHELLO JOB (ACCT),'COB2 TEST',CLASS=A,MSGCLASS=X,
// REGION=0M,TIME=1440
//* 1. Write the COBOL source to /u/bobshell/work
//WRITECBL EXEC PGM=IEBGENER
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'HELLO FROM ENTERPRISE COBOL'.
STOP RUN.
/*
//SYSUT2 DD PATH='/u/bobshell/work/hello.cbl',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 2. Write the script to /tmp
//WRITESH EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
export PATH=/usr/lpp/IBM/cobol/igyv6r4/bin:$PATH
cd /u/bobshell/work
cob2 -o hello hello.cbl
echo "cob2 return code: $?"
./hello
echo "hello return code: $?"
/*
//SYSUT2 DD PATH='/tmp/cobhello.sh',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 3. Run it
//RUNSH EXEC PGM=BPXBATCH,COND=(0,NE)
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//STDPARM DD *
SH /bin/sh /tmp/cobhello.sh
/*


Enterprise COBOL 6.4 compiles without warnings ("no statements flagged"), the program writes HELLO FROM ENTERPRISE COBOL and both return codes are 0. On the ADCD nothing else needs to be configured to use cob2.
The test on z/OS
I set up the same test as on RHEL, in the /u/bobshell/cobol folder: the program with the two errors, the expected result (expected.txt) and the pipeline (pipeline.sh), which now compiles with the z/OS cob2.
Here comes the first difference with Linux: the z/OS compiler needs the program in EBCDIC, but Bob runs on Node.js, which works in ASCII. In my first attempt I asked Bob to keep the program in EBCDIC: it spent the whole budget (1.01 Bobcoins and 31 tool calls) fighting with the conversion and left the program half done, with only the first line.
So I do it the other way round. I write the program with errors in EBCDIC (invoice-buggy.cbl, which also lets me repeat the test) and, from it, I create invoice.cbl in ASCII with iconv, tagged as ISO8859-1. Bob works with invoice.cbl, and the pipeline converts it to EBCDIC (invoice-ebcdic.cbl) just before compiling. expected.txt stays in EBCDIC, like the program output, and I tag it as IBM-1047 so that Bob reads it correctly.
Everything goes in one job. IEBGENER writes the files, and the last step runs the pipeline once: as the program has a compilation error, it has to end with return code 1. To repeat the test after Bob has fixed the program, just submit this job again: it creates invoice.cbl with the two errors again.
One detail: I first write the expected result to expected.in and then remove the trailing blanks of each line with sed. The lines that IEBGENER writes can be padded with blanks up to 80 columns, and then diff would fail the test even if the program were right.
//COBSETUP JOB (ACCT),'COBOL PIPELINE',CLASS=A,MSGCLASS=X,
// REGION=0M,TIME=1440
//* 1. Folder for the test
//MKDIR EXEC PGM=BPXBATCH
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//STDPARM DD *
SH mkdir -p /u/bobshell/cobol
/*
//* 2. COBOL program with two errors (EBCDIC master copy)
//WRITECBL EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. INVOICE.
000300 DATA DIVISION.
000400 WORKING-STORAGE SECTION.
000500 01 WS-LINES.
000600 05 FILLER PIC 9(5)V99 VALUE 1000.00.
000700 05 FILLER PIC V99 VALUE .21.
000800 05 FILLER PIC 9(5)V99 VALUE 200.00.
000900 05 FILLER PIC V99 VALUE .10.
001000 05 FILLER PIC 9(5)V99 VALUE 50.00.
001100 05 FILLER PIC V99 VALUE .04.
001200 01 WS-TABLE REDEFINES WS-LINES.
001300 05 WS-LINE OCCURS 3 TIMES.
001400 10 WS-NET PIC 9(5)V99.
001500 10 WS-RATE PIC V99.
001600 01 WS-I PIC 9.
001700 01 WS-VAT PIC 9(5)V99.
001800 01 WS-TOTAL-NET PIC 9(6)V99 VALUE 0.
001900 01 WS-TOTAL-VAT PIC 9(6)V99 VALUE 0.
002000 01 WS-TOTAL PIC 9(6)V99.
002100 01 WS-EDIT PIC ZZZ,ZZ9.99.
002200 PROCEDURE DIVISION.
002300 MAINLINE.
002400 PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I = 3
002500 COMPUTE WS-VAT = WS-NET(WS-I) * WS-RATE(WS-I)
002600 ADD WS-NET(WS-I) TO WS-TOTAL-NET
002700 ADD WS-VAT TO WS-TOTAL-VAR
002800 END-PERFORM
002900 COMPUTE WS-TOTAL = WS-TOTAL-NET + WS-TOTAL-VAT
003000 MOVE WS-TOTAL-NET TO WS-EDIT
003100 DISPLAY 'NET: ' WS-EDIT
003200 MOVE WS-TOTAL-VAT TO WS-EDIT
003300 DISPLAY 'VAT: ' WS-EDIT
003400 MOVE WS-TOTAL TO WS-EDIT
003500 DISPLAY 'TOTAL: ' WS-EDIT
003600 STOP RUN.
/*
//SYSUT2 DD PATH='/u/bobshell/cobol/invoice-buggy.cbl',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 3. Expected result
//WRITEEXP EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
NET: 1,250.00
VAT: 232.00
TOTAL: 1,482.00
/*
//SYSUT2 DD PATH='/u/bobshell/cobol/expected.in',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 4. Pipeline: convert, compile, run and compare
//WRITEPIP EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
#!/bin/sh
# Pipeline: compile, run and compare with the expected output
export PATH=/usr/lpp/IBM/cobol/igyv6r4/bin:$PATH
cd /u/bobshell/cobol
echo "=== 1. Compile ==="
# invoice.cbl is in ASCII: the compiler needs EBCDIC
iconv -f ISO8859-1 -t IBM-1047 invoice.cbl > invoice-ebcdic.cbl
if ! cob2 -o invoice invoice-ebcdic.cbl; then
echo "COMPILE FAILED"
exit 1
fi
echo "=== 2. Run ==="
./invoice > output.txt
RC=$?
cat output.txt
if [ $RC -ne 0 ]; then
echo "RUN FAILED (return code $RC)"
exit 3
fi
echo "=== 3. Test ==="
if ! diff expected.txt output.txt; then
echo "TEST FAILED"
exit 2
fi
echo "TEST OK"
/*
//SYSUT2 DD PATH='/u/bobshell/cobol/pipeline.sh',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR,SIXUSR),FILEDATA=TEXT
//* 5. Setup script
//WRITESH EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
cd /u/bobshell/cobol
sed 's/ *$//' expected.in > expected.txt
rm expected.in
chtag -tc IBM-1047 expected.txt
iconv -f IBM-1047 -t ISO8859-1 invoice-buggy.cbl > invoice.cbl
chtag -tc ISO8859-1 invoice.cbl
rm -f invoice invoice-ebcdic.cbl output.txt
sh pipeline.sh
echo "pipeline return code: $?"
ls -T invoice.cbl invoice-buggy.cbl expected.txt
/*
//SYSUT2 DD PATH='/tmp/cobsetup.sh',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 6. Run it
//RUNSH EXEC PGM=BPXBATCH,COND=(0,NE)
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//STDPARM DD *
SH /bin/sh /tmp/cobsetup.sh
/*


The pipeline fails at compilation with the same error as on RHEL: IGYPS2121-S ("WS-TOTAL-VAR" was not defined as a data-name). Enterprise COBOL ends with return code 12 and the pipeline with 1.
Bob fixes the program
I give Bob the same instructions as on RHEL, adapted to z/OS: I explain that invoice.cbl is in ASCII and that the pipeline converts it to EBCDIC before compiling, and I ask it to change only invoice.cbl.
As in the Bob Shell on z/OS article, I start it with a job and bob run: Bob runs the pipeline, reads the error, fixes the program and tests again, without asking for permission. When it finishes, the job runs the pipeline again, so I do not rely only on what Bob says. The ls -T at the end shows how the file ended up (whether it has a code set tag).
With bob run, Bob runs commands and changes files without asking for permission, and in this job it runs as IBMUSER, which is a superuser on the ADCD. That is why I give it limits (--max-cost and --max-turns). On a real system you should use a user with just the permissions it needs.
//COBBOB JOB (ACCT),'BOB FIXES COBOL',CLASS=A,MSGCLASS=X,
// REGION=0M,MEMLIMIT=NOLIMIT,TIME=1440
//* 1. Write the instructions for Bob
//WRITEP EXEC PGM=IEBGENER
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
The folder /u/bobshell/cobol has a COBOL program (invoice.cbl) that
calculates an invoice with three lines, each one with its own VAT
rate. To compile and test it with IBM Enterprise COBOL for z/OS, run
in the shell:
sh /u/bobshell/cobol/pipeline.sh; echo $?
invoice.cbl is an ASCII text file: the pipeline converts it to EBCDIC
(invoice-ebcdic.cbl), compiles it with cob2, runs the program and
compares its output with expected.txt. Return codes: 0 = test passed,
1 = compilation error, 2 = the output does not match expected.txt,
3 = the program ended with a return code other than 0.
Fix invoice.cbl until the return code is 0. Rules:
- Edit only invoice.cbl and keep it in ASCII. Do not change
expected.txt, pipeline.sh or invoice-ebcdic.cbl.
- Keep the COBOL fixed format (sequence numbers in columns 1-6,
area A from column 8 and area B from column 12).
- Explain each error you find before you fix it.
/*
//SYSUT2 DD PATH='/u/bobshell/work/cobprompt.txt',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 2. Write the script to /tmp
//WRITESH EXEC PGM=IEBGENER,COND=(0,NE)
//SYSPRINT DD SYSOUT=*
//SYSIN DD DUMMY
//SYSUT1 DD *
. /u/nodeJS/current/.env
export TMPDIR=/u/bobshell/tmp
export BOB_API_KEY="$(cat $HOME/.bob-api-key)"
BOBJS=/u/bobshell/npm/lib/node_modules/@ibm/bob/dist/bob.js
OPTS="--accept-license --max-cost 1 --max-turns 30"
PROMPT="$(cat /u/bobshell/work/cobprompt.txt)"
cd /u/bobshell/cobol
node --liftoff-only $BOBJS run $OPTS "$PROMPT" </dev/null
echo "bob run return code: $?"
echo "=== Check: run the pipeline again ==="
sh /u/bobshell/cobol/pipeline.sh
echo "pipeline return code: $?"
ls -T /u/bobshell/cobol/invoice.cbl
/*
//SYSUT2 DD PATH='/tmp/cobbob.sh',
// PATHOPTS=(OWRONLY,OCREAT,OTRUNC),
// PATHMODE=(SIRUSR,SIWUSR),FILEDATA=TEXT
//* 3. Run it
//RUNSH EXEC PGM=BPXBATCH,COND=(0,NE)
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//STDPARM DD *
SH /bin/sh /tmp/cobbob.sh
/*
The result

The job ends with RC=0000, and the RUNSH step, the one that starts Bob, takes less than a minute. Its output shows everything Bob does, step by step.

Bob starts by reading the files, such as invoice.cbl, and then runs the pipeline with the command I gave it.

Here EBCDIC shows up again: the compiler messages reach Bob as strange characters, because they are in EBCDIC. Bob notices it and looks for another way to read them: first it looks at pipeline.sh to see how the program is compiled.

Once it can read the errors, it finds both of them, the same ones as on RHEL: the misspelled name on line 27 (WS-TOTAL-VAR instead of WS-TOTAL-VAT) and the loop on line 24, which with UNTIL WS-I = 3 ends before processing the third invoice line. Before changing anything, it checks the numbers against the expected result.

It fixes invoice.cbl with apply_diff, its tool to change files, and runs the pipeline again.

At the end it explains the two errors and what it spent: 0.151 Bobcoins in 25.8 seconds, with 7 tool calls. In the first attempt, when I asked it to work in EBCDIC, it spent 1.01 Bobcoins and did not finish.

The check in the job confirms it: the pipeline, run again outside Bob, compiles without warnings, the program prints the expected amounts and the test ends with TEST OK and return code 0. And invoice.cbl is still in ASCII (ISO8859-1), as I asked. The /etc/ssl/certs warning is the same one that appeared when installing Bob Shell, and it does not affect the test.
On RHEL everything was ASCII and there was no need to think about it. On z/OS, the key has been to keep the two apart: Bob works with the program in ASCII, and the pipeline converts it to EBCDIC just before compiling it with Enterprise COBOL.