Passing binary data to a command line tool

I’ve got a command line tool, I’d like to wrap in a Xojo GUI. But it’s weird… It takes binary data from stdin, processes it and puts it in stdout.

My Xojo app creates the binary data in the format it expects and I can test this, but writing the data to disk and using “cat <pathToFile/> | <pathToTool/>” and it writes the processed data to stdout.

However I really want to avoid writing the data to disk first and then using cat to load it up. But I seem unable to get it to work.

shell.execute "<pathToTool/> <binaryData>"

makes the tool crash. Writing the data to the shell first and then calling the tool fails, so does launching the tool and then writing the data.

shell.write <binaryData>
shell.execute <pathToTool/>

Setting the mode of the shell to 2, just sits there and doesn’t complete (I’d also like to avoid this as it needs to be called in a function).

I’ve also tried enabling caninomical and sending chr( 4 ) after sending my binary data, and it still just sits there.

There has to be something I’m missing, I’ve never tried sending binary data to a command line tool. Ideas?

Use something like hex or base64 encoding on the binary data. Unprintable characters can do bad things on command lines.

1 Like

Unfortunately I didn’t write the command line tool, so I have no control over what it takes as an input.

If I cat "<pathToFile/>" it displays the RAW binary data, so I am assuming that it takes the RAW unencoded binary data.

So there’s 3 questions I have for people with more experience than I.

  1. Do I use write before I execute the tool?
  2. Do I use write after I executed the tool?
  3. How do I signify the end of data? Sending chr(4) (even after enabling canonical) doesn’t seem to help.

Much obliged to anyone who can assist here.

Another idea: you get or make an intermediate tool that decodes, say, hex into binary and back. Your call would end up looking like this:

bin --from-hex=123456 | your_tool | bin --from-bin

You’d end up with hex that you can then decode.

Thanks @Kem_Tekinay and @Greg_O_Lone

I got it working by using a NSTask class that I wrote and adapting it to allow for writing to stdin.

For future reference, the data is written before the command line tool is executed, and end of data is signified by closing the NSFileHandle.

1 Like

Can a command line tool access the clipboard?

I don’t think so, at least not by default. You might be able to jimmy in AppKit and use it via declares.

You can also check NSTaskMBS or ShellMBS, which should allow to pass binary data just fine.
If needed, I can help there.