daelious
daelious

Reputation: 13

Embedded Lua in C take input from STDIN

I'm writing a C app that has an embedded lua script in it. The Lua script takes input from STDIN. So when I'm running the script from the shell it's like thus:

lua myscript.lua < datafile*

How do I accomplish this from inside the C code?

Thank you.

Upvotes: 1

Views: 569

Answers (2)

Alexander Gladysh
Alexander Gladysh

Reputation: 41403

Have you tried to just run your script unmodified (i.e. use io.stdin etc.)? Unless you're doing something fancy on C side, it should work out of the box.

Upvotes: 0

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

Use the dup2(2) system call on descriptor 0 (stdin) and on the descriptor returned by open(2) on datafile:

int fd = open("datafile", O_RDONLY);
dup2(fd, 0);
/* reading from stdin now is in fact reading from datafile */

Of course, you need some error-checking in a real-world program.

To implement the behaviour of wildcarding, you may want to look at the readdir(3) library function.

Upvotes: 1

Related Questions