Use a Pseudo Terminal to interact with Shell in Linux
-- Lihn1987
Gdb is always used for program debugging in the company recently. As the debugging level increases, the omnipotent printf becomes increasingly useless. If you can't do it, you will use gdb, I learned something about gdb and found that gdb is indeed quite powerful, but it is much more convenient than vc AND OTHER INTEGRATED environments... So an idea was born slowly,
Visualized gdb! I used to write something similar on the Internet, but I didn't want to write anything about it. I plan to use gtk for the interface. The interface has been designed with the glade tool, then, use a Pseudo Terminal to interact with the shell, which is equivalent to embedding a Pseudo Terminal in a graphic program. I have never used a Pseudo Terminal before. Today I just checked the materials and wrote a program symbolic. You can read the output of GDB and input it like gdb. The program is as follows:
[Code]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <linux/limits. h>
# Include <pty. h>
Void do_trans (int pty );
Int main (int argc, char * argv [])
{
Int pty, slave;
Char pty_name [PATH_MAX];
Int ret;
Pid_t child;
Ret = openpty (& pty, & slave, pty_name, NULL, NULL );
If (ret =-1)
{
Perror ("openpty ");
Exit (EXIT_FAILURE );
}
Child = fork ();
If (child =-1)
{
Perror ("fork ");
Exit (EXIT_FAILURE );
}
Else if (child = 0)
{
Close (pty );
Login_tty (slave );
Execl ("/usr/bin/GDB", "GDB", null );
}
Close (slave );
Do_trans (Pty );
Exit (exit_success );
}
Void do_trans (int pty)
{
Char buffer [800] = {0 };
Int ret;
Write (Pty, "L/N", 2); // symbolic input of nonstandard data, as to check whether the interaction is successful
While (ret = read (Pty, buffer, sizeof (buffer)-1)
{
Buffer [ret-1] = 0; // read is not able to read the string Terminator, you need to add it yourself, otherwise printf will fail
If (Ret <= 0)
{
Break;
}
Fflush (stdout); // this step is very important. STD usually needs this function to refresh data in the storage area.
Printf ("% s", buffer); // print the result
}
Return;
}
[/Code]