Lua: io.read() doesn't work on long strings

The following command freezes on a string of length about 2000 chars:

lua -e 'print(io.read())'

Any idea why?

PS: Lua version 5.1.5

Upvotes: 3

Views: 464

Answers (2)

lhf
lhf

Reputation: 72312

This is most probably a limitation of terminal IO in the underlying operating system. It is enforced before it gets to the Lua limit.

Try entering a long line in cat > /dev/null.

Upvotes: 4

Yu Hao
Yu Hao

Reputation: 122383

When called without arguments, io.read uses the default format "*l", which reads the next line. The underlying buffer size is C's BUFSIZ, it seems that your input string has exceeded that limit.

Change it to io.read("*a") to read the whole input. Remember to send EOF in the end.

Upvotes: 2

Related Questions