Skip to main content
Added an explanation of what is happening on the Lua side.
Source Link

So I'm trying to connect to a (Lua) debugger embedded in a program by redirecting the I/O. Currently I create a pair of FIFOs for the read and write streams and connect to them using cat /tmp/dbg_write & cat > /tmp/dbg_read. This is workable and pretty straightforward, but if you don't exit everything just right you have to go back and kill the background cat command. I figured it was time for a learning experience since there seems like there must be a better way. I'm totally stumped though.

Internally I'm just overriding the debugger's I/O functions to use FIFOs, since stdio is a no-go in this case. As far as I know, there is no way to do bidirectional I/O with the standard Lua APIs like sockets, and pulling in native libs is a no-go since it's embedded.

local dbg = require("debugger")

local READ_PIPE, WRITE_PIPE = "/tmp/dbg_read", "/tmp/dbg_write"
os.execute(string.format("mkfifo %s %s", READ_PIPE, WRITE_PIPE))
local input, output = io.open(READ_PIPE, "r"), io.open(WRITE_PIPE, "w")

function dbg.read(prompt)
    dbg.write(prompt)
    return input:read()
end

function dbg.write(str)
    output:write(str)
    output:flush()
end

I also tried:

  • using popen("netcat -lU") to create a Unix domain socket, but pipes are unidirectional. Derp.
  • Using an existing pty. Seems promising except I don't know how to create one separate from a shell that wants to read from it.
  • Using screen to... somehow create a tty session I can interact with. (no idea...)
  • Finding an existing utility that can copy from a file/pipe to stdout while also copying from stdin to another.

Anything obvious that I'm missing?

So I'm trying to connect to a (Lua) debugger embedded in a program by redirecting the I/O. Currently I create a pair of FIFOs for the read and write streams and connect to them using cat /tmp/dbg_write & cat > /tmp/dbg_read. This is workable and pretty straightforward, but if you don't exit everything just right you have to go back and kill the background cat command. I figured it was time for a learning experience since there seems like there must be a better way. I'm totally stumped though.

I also tried:

  • using popen("netcat -lU") to create a Unix domain socket, but pipes are unidirectional. Derp.
  • Using an existing pty. Seems promising except I don't know how to create one separate from a shell that wants to read from it.
  • Using screen to... somehow create a tty session I can interact with. (no idea...)
  • Finding an existing utility that can copy from a file/pipe to stdout while also copying from stdin to another.

Anything obvious that I'm missing?

So I'm trying to connect to a (Lua) debugger embedded in a program by redirecting the I/O. Currently I create a pair of FIFOs for the read and write streams and connect to them using cat /tmp/dbg_write & cat > /tmp/dbg_read. This is workable and pretty straightforward, but if you don't exit everything just right you have to go back and kill the background cat command. I figured it was time for a learning experience since there seems like there must be a better way. I'm totally stumped though.

Internally I'm just overriding the debugger's I/O functions to use FIFOs, since stdio is a no-go in this case. As far as I know, there is no way to do bidirectional I/O with the standard Lua APIs like sockets, and pulling in native libs is a no-go since it's embedded.

local dbg = require("debugger")

local READ_PIPE, WRITE_PIPE = "/tmp/dbg_read", "/tmp/dbg_write"
os.execute(string.format("mkfifo %s %s", READ_PIPE, WRITE_PIPE))
local input, output = io.open(READ_PIPE, "r"), io.open(WRITE_PIPE, "w")

function dbg.read(prompt)
    dbg.write(prompt)
    return input:read()
end

function dbg.write(str)
    output:write(str)
    output:flush()
end

I also tried:

  • using popen("netcat -lU") to create a Unix domain socket, but pipes are unidirectional. Derp.
  • Using an existing pty. Seems promising except I don't know how to create one separate from a shell that wants to read from it.
  • Using screen to... somehow create a tty session I can interact with. (no idea...)
  • Finding an existing utility that can copy from a file/pipe to stdout while also copying from stdin to another.

Anything obvious that I'm missing?

edited tags
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Source Link

Command to create/connect to an I/O stream?

So I'm trying to connect to a (Lua) debugger embedded in a program by redirecting the I/O. Currently I create a pair of FIFOs for the read and write streams and connect to them using cat /tmp/dbg_write & cat > /tmp/dbg_read. This is workable and pretty straightforward, but if you don't exit everything just right you have to go back and kill the background cat command. I figured it was time for a learning experience since there seems like there must be a better way. I'm totally stumped though.

I also tried:

  • using popen("netcat -lU") to create a Unix domain socket, but pipes are unidirectional. Derp.
  • Using an existing pty. Seems promising except I don't know how to create one separate from a shell that wants to read from it.
  • Using screen to... somehow create a tty session I can interact with. (no idea...)
  • Finding an existing utility that can copy from a file/pipe to stdout while also copying from stdin to another.

Anything obvious that I'm missing?