Use a Pseudo Terminal to interact with shell in linux

Source: Internet
Author: User

 

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.