1 /**[txh]********************************************************************
3 Copyright (c) 2004 by Salvador E. Tropea.
4 Covered by the GPL license.
7 Example/test of pseudo terminals.
8 Note: Contributed by Greg Watson (gwatson lanl gov)
10 ***************************************************************************/
15 void cb_console(const char *str, void *data)
17 printf("CONSOLE> %s\n",str);
20 /* Note that unlike what's documented in gdb docs it isn't usable. */
21 void cb_target(const char *str, void *data)
23 printf("TARGET> %s\n",str);
26 void cb_log(const char *str, void *data)
28 printf("LOG> %s\n",str);
31 void cb_to(const char *str, void *data)
36 void cb_from(const char *str, void *data)
38 printf("<< %s\n",str);
41 void cb_async(mi_output *o, void *data)
46 int main(int argc, char *argv[])
53 /* Connect to gdb child. */
57 printf("Connect failed\n");
60 printf("Connected to gdb!\n");
62 /* Set all callbacks. */
63 mi_set_console_cb(h,cb_console,NULL);
64 mi_set_target_cb(h,cb_target,NULL);
65 mi_set_log_cb(h,cb_log,NULL);
66 mi_set_async_cb(h,cb_async,NULL);
67 mi_set_to_gdb_cb(h,cb_to,NULL);
68 mi_set_from_gdb_cb(h,cb_from,NULL);
70 /* Look for a free pseudo terminal. */
71 pty=gmi_look_for_free_pty();
74 printf("Error opening pseudo terminal.\n");
78 printf("Free pty slave = %s, master on %d\n",pty->slave,pty->master);
80 /* Tell gdb to attach the terminal. */
81 if (!gmi_target_terminal(h,pty->slave))
83 printf("Error selecting target terminal\n");
88 /* Set the name of the child and the command line aguments. */
89 if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
91 printf("Error setting exec y args\n");
96 /* Run the program. */
99 printf("Error in run!\n");
107 FD_SET(pty->master,&rfds);
110 if (select(pty->master+1,&rfds,NULL,NULL,NULL)<0)
117 if (FD_ISSET(pty->master,&rfds))
119 int n=read(pty->master,buf,BUFSIZ);
125 if (FD_ISSET(0,&rfds))
127 int n=read(0,buf,BUFSIZ);
130 write(pty->master,buf,n);
136 /* Close the connection. */