1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
6 Module: pseudo terminal
8 Helper to find a free pseudo terminal. Use this if you need to manage
9 input *and* output to the target process. If you just need output then
10 define a handler for target output stream records (assuming that this
11 is working for your particular version of gdb).
14 mi_pty *pty = gmi_look_for_free_pty();
15 if (pty) gmi_target_terminal(mih, pty->slave);
17 * reading from pty->master will get stdout from target *
18 * writing to pty->master will send to target stdin *
20 Note: Contributed by Greg Watson (gwatson lanl gov)
22 ***************************************************************************/
29 #include <sys/ioctl.h>
33 /**[txh]********************************************************************
36 Look for a free and usable pseudo terminal. Low level, use
37 @x{gmi_look_for_free_pty}.
39 Return: A file descriptor connected to the master pty and the name of the slave device, or <0 on error.
41 ***************************************************************************/
47 int mi_look_for_free_pty(int *master, char **slave)
51 static char name[BUFSIZ];
53 if (openpty(&fdmaster,&fdslave,name,NULL,NULL)<0)
56 (void)close(fdslave); /* this will be reopened by gdb */
63 #elif defined(__linux__)
65 int mi_look_for_free_pty(int *master, char **slave)
67 if ((*master=open("/dev/ptmx",O_RDWR))<0)
69 if (grantpt(*master)<0 || unlockpt(*master)<0)
71 *slave = ptsname(*master);
76 #else /* undefined o/s */
78 int mi_look_for_free_pty(int *master, char **slave)
84 /**[txh]********************************************************************
87 Look for a free and usable pseudo terminal to be used by the child.
89 Return: A new mi_pty structure, you can use gmi_end_pty to
92 ***************************************************************************/
94 mi_pty *gmi_look_for_free_pty()
98 int pty=mi_look_for_free_pty(&master,&slave);
103 res=(mi_pty *)malloc(sizeof(mi_pty));
106 res->slave=strdup(slave);
111 void mi_free_pty(mi_pty *p)
119 /**[txh]********************************************************************
122 Closes the pseudo termial master and releases the allocated memory.
124 ***************************************************************************/
126 void gmi_end_pty(mi_pty *p)