Copybook to Code

Generating Code with HostBridge

When I asked Scott Glenn to develop the CommareaProgram object for HB.js eight years ago it seemed like it was going to be a pretty simple implementation.  Just wrapper the CICS EXEC LINK command with JavaScript like object syntax.  Make sure we can pass and receive a commarea buffer back and forth.  I mean it’s just PROGRAM, COMMAREA and LENGTH right?  Oops, forgot about SYNCONRETURN, SYSID and TRANSID.  Wait what is this DATALENGTH (how is that different than LENGTH), INPUTMSG (cannot be used with DATALENGTH, huh?).  Then CICS TS 4.1 came out, oh no – channels and containers (put, delete, query, put64, get64, . . . and lots and lots of parameters).  But all of this was really easy, it was all just SMOP, a Simple Matter of Programming (thank you Dan Speer), and I didn’t have to do any of it.

Scott finished the object (“Finally!!  What took so long it was just a simple link!”) and passed it to me for testing.  I used my massive COBOL programming skills to call John Bachiochi (I find it interesting that auto-correct suggests Hibachi for John’s last name, I will suggest that he change it) and ask him to create me a COBOL program that I could invoke from HB.js.  I wanted a program that would test all of the common COBOL data formats our customers would be likely to encounter, I mean DISPLAY (character) and COMP-3 (packed decimal), right?  John wrote the program and sent me the copybook.  John being John, the copybook was very very long.  When I asked John why it was so long he said that these were the formats we needed to support.  Beside DISPLAY and COMP-3 there was COMP (I guess that is COMP-1), COMP-2, COMP-5 (huh??), ZONED (wait what happened to COMP-4, never mind I don’t want to know).  There were dozens of different of tests for each data type.  Differences in length, scale and precision (I didn’t know the difference either, didn’t want to look stupid so I just nodded my head and googled it later), signed and unsigned, etc.

Editor’s Note: As I am sure you have noticed James likes to use parenthesis to an excess.  We have edited and remove them from his blog posts on multiple occasions, but as he is the administrator of the system and has threaten to revoke all of our access, we have decided to compromise and let him use all of the parenthesis he chooses (and I choose to add some right here), but we take no responsibility for the content of James’ blog.

James’ Note: Ha! or (Ha! Nerd victory.)

All of these different data types had to be correctly converted bidirectionally into and out of the buffer that would be the COMMAREA used by the program.  Writing JavaScript to do these conversions would be relatively easy, but there were a couple of issues, the primary being performance.  It is possible, maybe even common that there would be thousands of these data conversions done in each web service call.  Performance is always a major concern in the code we write, native code or JavaScript.  Another issue in doing the conversion in JavaScript is that I had no clue how to convert any data types (okay, I could do DISPLAY, maybe COMP-1/binary, but that was it).  I mean really who knew that -172.031 would become x’f1f7f2f0f3f1d0′ for zoned or x’1720310d’ for packed decimal (I tried just converting numbers to strings and adding a “0d” for packed decimal and surprisingly it didn’t work).  So I went back to Scott and told him we needed a bunch of native methods to convert into and out of all of these data formats for performance reasons, he bought it.

So now I had all of the tools necessary to a build JavaScript object that would a describe a COBOL copybook.  I sat down with John’s copybook and the new conversion methods and began to code up the JavaScript object.  After about 15 minutes (pretty sure a 10 minute break is required by law for every 10 minutes worked, I worked 15 minutes because I am an overachiever) I took a break to calculate how long it would take me to create the JavaScript object by hand coding it and determined it would be about 9 months.  I hadn’t worked that long on a single project since Project DEEP FREEZE (it’s a government thing, I can’t talk about it).  I needed something that would read the copybook and generate the code that would complete within my attention span.  Briefly I thought about writing something to read the copybook and generate the code, I quickly determined that was a nonstarter.  So I went to my friend google.  I found a open source program called CB2XML that would convert a COBOL copybook to XML.  Perfect, if there is one thing I know how to do it is to process XML using HB.js.  The only problem is that the program is written in Java and HB.js doesn’t know how to invoke Java. So I went back to Scott and told him that a customer (yes I too, can be a customer of HostBridge) needed to call a Java program from HB.js, he bought it (I was caught when I also asked him to modify the Java program to pass it’s data via Channel and Container).

Now I had a HB.js Java API and a working CB2XML program.  The rest would be easy . . .  or not.


To be continued in my next post. . .

One thought on “Copybook to Code

  1. Interesting,
    not sure you need to actually call cb2xml from the java script to get the cobol Data-Definitions. The idea is you use cb2xml to convert a Cobol-Copybook into an equivalent “Xml-Copybook”. This conversion could be done as a batch process. You could then just load the Converted “Xml” copybook.

    With regards Cobol Types

    Comp – Big endian Integer (can be 2,4, 8 bytes long)
    Comp-1 – 4 Byte Floating point (float c/java) – rarely used in Cobol
    comp-2 – 8 Byte Floating point (double c/java) – rarely used in Cobol
    comp-3 – Packed Decimal
    comp-5 – Big endian Integer (can be 2,4, 8 bytes long)

    I have written conversion routines for comp, comp-3, comp-5 and zoned decimal in Java (JRecord project)

    Good luck

    Bruce (Maintainer cb2xml project)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s