1

I'm trying to build a small utility in C to be used through web extension native messaging, following this MDN document. I have the extension side completed but am having some trouble on the application side. The examples are in Python and node.js and I need to use C.

I'd like to do what this poster did in C# described in this SO question.

Also, in this SO question, the poster's C program is being opened by Java and passed some data. It fits what I'm trying to do, if the Java side is performed by the extension.

My question is that I don't understand how stdin "knows" to get the input from the extension's message rather than the keyboard, for example. In the C code, in the statement fscanf(stdin, "%s", buff); from the last link above, how is stdin associated with the Java program, or does it not need to be and will read whatever source of input it receives?

If that is the case, and the communication port is set up such that the native application remains open for multiple messages from the extension, do you just repeat the fscanf or fread after a message is processed and the C program will wait for new input from any source again?

Lastly, if fread is always used, will that guarantee that stdin won't read input from any source other than the extension when it remains open in between messages?

Thank you.

Gary
  • 2,393
  • 12
  • 31
  • note: it is not necessary to answer "how `stdin` knows" question in order to answer "how to use stdin": you just read from stdin/write to stdin [as we've discussed here](https://stackoverflow.com/questions/64399326/how-to-get-stdin-in-c-to-read-external-data-from-another-program?noredirect=1&lq=1) until eof (until fread returns less than 4 bytes while you read the size of the next message). [Ping-pong example shows the protocol](https://github.com/SphinxKnight/webextensions-examples/blob/master/native-messaging/app/ping_pong.py) – jfs Oct 17 '20 at 09:16
  • 1
    Here's an example of how redirection can be implemented in terms of POSIX dup2() call https://gist.github.com/zed/7835043 it is not necessary to use fread/fwrite successfully. – jfs Oct 17 '20 at 09:18
  • Thanks. By "how" I meant through what code because I didn't see any in these examples or in my texts except when a file is opened. I think the answer to my question is that, (quoting a text book now) "`stdin` points to an input stream that is the 'normal input' to the program." That is what I was missing. If the program is executed from the runtime command line, the keyboard will be the normal input stream; if the browser or a Java program executes it, they will be the normal input. That is what I was not understanding and was expecting to progammatically set stdin to a desired input stream. – Gary Oct 18 '20 at 01:51
  • @jfs I wrote this question with the intent of deleting the one that was closed but since you referenced that here, I coud leave it. Or, perhaps, I should delete both because this is likely of no use to anyone else. – Gary Oct 18 '20 at 01:52
  • 1
    here's [Native messaging "ping_pong" example in C](https://gist.github.com/zed/4459378be67a4b37f53430e0703cb700) – jfs Oct 18 '20 at 06:36
  • @jfs Thank you for the example in C. It is a great help to me. I thought that it wouldn't be difficult for me to build one after understanding the source of stdin but, after looking at your example, I realize it would have been far more challenging for me; and it saves me a great deal of time and headache in trying to get started. I appreciate it. It is working in my extension right now. I'd accept it as the answer but it is only in a comment right now. – Gary Oct 19 '20 at 02:42
  • 1
    the code worked when I tested it but the code is not an answer. You could post the answer in terms that you know (think, what answer might be useful to a future visitor who clicked on the title of the question in a search) – jfs Oct 19 '20 at 15:41

0 Answers0