As in linux pty \ pts, open a pair and do not write code for this. Just connect cat to one side, hook my applet to the other side.

The program should work with a com port, but for now I want to send a pseudo-terminal to it, so that I can throw test data with my hands.

I tried socat, but there it is not clear how to access pts from the outside and which terminal was allocated. There is a pty in mana, but his applications there are a little wrong.

Pipe and fifo do not suit me. Pipe due to the fact that O_NOCTTY, and fifo one-sided.

A more detailed recipe for socat can someone tell me.

The essence of what I want: in one terminal, run cat <> /dev/master-pseudotty-device or picocom /dev/master-pseudotty-device , in the second ./moya_proga /dev/slave-pseudotty-device .

  • Your question is not entirely clear: cat | prog cat | prog , and what have the terminals? "As in linux pty \ pts and not to write code for this" - and what about linux? - 0xdb
  • If the program has no arguments, then the script -c ./your-prog does it - avp
  • man socat will help. Moreover, it can then be left to work with serial ports and remote connections. - 0andriy
  • @ 0xdb, the pipe will not go. I want a separate terminal - eri
  • @ 0andriy sokat tried, but mans did not really help me. Sokat opens the TCP, but I don’t know how he can understand which of his is eri

1 answer 1

For half an hour googling did not find anything ready (probably I don’t know how to search).

Try

 // compile with -lutil #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <termios.h> #include <unistd.h> #include <pty.h> int main (int ac, char* av[]) { char ptys_name[256]; fd_set rfd; struct termios tt; int master, slave; int n, nfds, cc; if (tcgetattr (0, &tt) < 0) exit((perror("Can't get stdin tcgetattr"), 1)); cfmakeraw (&tt); if (openpty (&master, &slave, ptys_name, &tt, 0) < 0) exit((perror("Can't open pty"), 1)); puts(ptys_name); // tcsetattr(0, TCSANOW, &tt); // tty (stdin) RAW-mode (not debugged) nfds = master + 1; for (;;) { char buf[BUFSIZ]; FD_ZERO(&rfd); FD_SET(master, &rfd); FD_SET(0, &rfd); if ((n = select(nfds, &rfd, 0, 0, NULL)) < 0) if (errno == EINTR) continue; else exit((perror("select"), 1)); if (n > 0) { if (FD_ISSET(master, &rfd)) if ((cc = read(master, buf, sizeof(buf))) > 0) (void) write(1, buf, cc); // echo to stdout if (FD_ISSET(0, &rfd)) if ((cc = read(0, buf, sizeof(buf))) > 0) if (write(master, buf, cc) < 0) fputs("Can't write to slave\n", stderr); } } return 0; } 

The name of the slave that the program prints is passed to the parameter with its own. They open it on read-write (or in different files, as it is more convenient) and use it as a terminal.

This program copies stdin from the window to the slave, you read these characters. And what you write in this slave it prints in the window.

Work in RAW mode is not debugged (somehow see for yourself)

  • thanks for the example - eri
  • @eri, I tried to do exactly what you need, i.e. a couple of pty / tty in bsd-style (and not ptmx / pts as in response), but it didn’t work in my ubunt (if interested, I asked a question ) - avp
  • @eri, within the framework of what is available (ptmx / pts) it seems you can do such a thing. In the program, in response, we make 2 pairs of master slaves, type the names of the slaves, and in the loop do the proxy. Those. what we read from one master we write in the second, and from the second to the first. Then your program gets a name in the argument, for example, slave1 and works with it as with a terminal (its stdin-stdout remains connected to the window). In a couple of other windows, run cat slave2 (it will print the output of your program in slave1 ) and cat >slave2 (here you fill in the input that slave1 to slave1 in your prog). - avp
  • If this simulation suits you, then I can write (but I don’t promise urgency ...) - avp
  • I play with libuv for now ..) - eri