Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cobc without an input source file, but via a pipe?

Is it possible to execute COBOL code without providing input files? I'm using cobc.

I tried to pipe the code to the cobc process:

$ cat my-input.cbl | cobc
cobc: No input files

To compile a file and run it, I do:

cobc -x di.cbl -o a && ./a

Supposing I don't have a file, I have just the code somewhere (maybe in a variable, as string), can I pass it to cobc? It would be better than creating the file and then compile it and run it.

In my case, the user inputs the source code of a COBOL program and that's a string variable. Currently, I save the code in a file, I compile it and then I execute it. It would be nice if cobc would support piping the code snippet in stdin or even as cli option, and generate a binary file or even showing the result directly (not sure if that's a good or bad idea). node, for example, has the -p option: node -p 'console.log(1)' -- this will output 1\nundefined. g++ has this feature –

like image 228
Ionică Bizău Avatar asked Jul 29 '15 18:07

Ionică Bizău


1 Answers

It was not possible yesterday, but it is today, and I'll get the changes into the source tree as soon as the lead approves the change.

Two small changes to the compiler source and - can be used to indicate stdin.

prompt$ echo 'program-id. test. display "hello".' | cobc -free -frelax -x -o thing -
-: 1: Warning: PROCEDURE DIVISION header missing - assumed
prompt$ ls -l
-rwxrwxr-x. 1 btiffin btiffin 13192 Jul 31 17:27 thing
prompt$ ./thing
hello

This will then work for any operating system, and not just those with /dev/stdin support (which, for now, as paxdiablo wrote, is a very valid option with GNU/Linux and the like).

And as an aside; cobc can already be used as a POSIX interpreter.

#!/usr/local/bin/cobc -xvg
       identification division.
       program-id. SAMPLE.

       procedure division.
       display "scripted" end-display
       goback.

       end program SAMPLE.

with

prompt$ chmod +x interp.cob
prompt$ ./interp.cob

giving

Command line:   /usr/local/bin/cobc -xvg ./interp.cob 
Preprocessing:  ./interp.cob -> interp.i
Return status:  0
Parsing:        interp.i (./interp.cob)
Return status:  0
Translating:    interp.i -> interp.c (./interp.cob)
Executing:      gcc -std=gnu99 -c -I/usr/local/include -pipe -Wno-unused
                -fsigned-char -Wno-pointer-sign -g -o "/tmp/cob25113_0.o"
                "interp.c"
Return status:  0
Executing:      gcc -std=gnu99 -Wl,--export-dynamic -o "interp"
                "/tmp/cob25113_0.o" -L/usr/local/lib -lcob -lm -lgmp
                -lncursesw -ldb -ldl
Return status:  0

Sadly, that is just for the build. Execution requires an extra step.

prompt$ ./interp
scripted

There is no compiler option for "run the code now", so a -J (fixed form) and -j (free form) option will be added. Run job, becomes a new form of -x (create executable) but will also execute the binary at end of processing. Then we'll have access to cobc "scripts" in POSIX. Currently all that is allowed is build, and the run has to be a separate command.

And the inherent limitations that POSIX hashbang lines are limited to a single argument, so -j -free wouldn't work, nor would -j -Xref etc, but within limits, it'll allow for cobc scripts.

That's another one line change to the compiler, and 400 lines of option parsing and documentation. ;-)

Should be ready for testing in a few hours. Again, these changes will require approval from all the other compiler maintainers, but I don't see any reason for not allowing the improvements. Two commits, one for - as stdin, and -J -j for run job at end of compile.

like image 166
Brian Tiffin Avatar answered Oct 18 '22 16:10

Brian Tiffin