为一个emscripten HTML程序提供stdin? [英] Providing stdin to an emscripten HTML program?

查看:140
本文介绍了为一个emscripten HTML程序提供stdin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C程序,它通过命令行接受一个参数(一个char数组/字符串),并从stdin读取。我使用emscripten将其编译为JavaScript。这是成功的,我可以像使用node.js的普通C程序一样运行它:

I have a C program that takes one argument (a char array / string) via command line and also reads from stdin. I've compiled it to JavaScript using emscripten. This was successful and I can run it just like the normal C program using node.js:

emcc -O2 translate.c
node translate.js "foo" < bar.txt

如你所见,我提供字符串foo作为参数, bar.txt的内容为stdin。现在我希望这是一个自包含的HTML文件。

As you can see, I'm providing the string "foo" as an argument and the contents of bar.txt as stdin. Now I want this to be a self-contained HTML file.

通过将输出更改为HTML:

By changing the output to HTML:

emcc -O2 translate.c -o trans.html

I通过添加参数:['foo'], 提供参数到 var Module 中的定义。这按预期工作,程序正确接收参数。

I provide the argument by adding arguments: ['foo'], to the definitions in var Module. This works as expected, the program receives the argument correctly.

现在,如何为此程序提供stdin输入?我不需要动态地这样做。可以在HTML中的某处使用所需的stdin内容声明一个字符串。

Now, how do I provide the stdin input to this program? I don't need to do this dynamically. It would be fine to just declare a string somewhere in the HTML with the required stdin content.

刚刚找到了适合我的解决方案。在生成HTML的JS文件中,有一个默认的输入处理程序,当没有定义其他输入方法时, prompt()是用户。只需编辑变量结果或调用您自己的函数:

Just found a solution that works for me. In the JS file for the generated HTML, there is a default input handler which prompt()s the user when no other input method is defined. Just edit the variable result or call your own function:

} else if (typeof window != 'undefined' &&
    typeof window.prompt == 'function') {
    // Browser.

    // REPLACE THIS CODE:
    result = window.prompt('Input: ');  // returns null on cancel
    if (result !== null) {
        result += '\n';
    }


推荐答案

一种方法是使用Emscripten Filesystem API,例如通过调用 FS。在模块preRun函数中初始化,传递用于标准输入,输出和错误的自定义函数。

A way would be to use the Emscripten Filesystem API, for example by calling FS.init in the Module preRun function, passing custom functions to be used for standard input, output and error.

var Module = {
  preRun: function() {
    function stdin() {
      // Return ASCII code of character, or null if no input
    }

    function stdout(asciiCode) {
      // Do something with the asciiCode
    }

    function stderr(asciiCode) {
      // Do something with the asciiCode
    }

    FS.init(stdin, stdout, stderr);
  }
};

这些函数非常低级:它们每个处理一个字符作为ASCII码。如果您想要传入字符串,则必须自己迭代字符串的字符。我怀疑 charCodeAt 会有所帮助。要从stdout或stderr输出字符串,我怀疑 fromCharCode 会有所帮助。

The functions are quite low-level: they each deal with one character at a time as an ASCII code. If you have strings you want to pass in, you would have to iterate over the characters of the string yourself. I suspect charCodeAt would be helpful. To output strings from stdout or stderr, then I suspect fromCharCode would be helpful.

使用每个的示例(测试不太好!)实现如下。

Example (not very well tested!) implementations using each are below.

var input = "This is from the standard input\n";
var i = 0;
var Module = {
  preRun: function() {
    function stdin() {
      if (i < res.length) {
        var code = input.charCodeAt(i);
        ++i;
        return code;
      } else {
        return null;
      }
    }

    var stdoutBuffer = "";
    function stdout(code) {
      if (code === "\n".charCodeAt(0) && stdoutBuffer !== "") {
        console.log(stdoutBuffer);
        stdoutBufer = "";
      } else {
        stdoutBuffer += String.fromCharCode(code);
      }
    }

    var stderrBuffer = "";
    function stderr(code) {
      if (code === "\n".charCodeAt(0) && stderrBuffer !== "") {
        console.log(stderrBuffer);
        stderrBuffer = "";
      } else {
        stderrBuffer += String.fromCharCode(code);
      }
    }

    FS.init(stdin, stdout, stderr);
  }
};

这篇关于为一个emscripten HTML程序提供stdin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆