070a3b5553015f3b66ee09d20803cd7cf163a097
[gdbmicli.git] / examples / pty_test.c
1 /**[txh]********************************************************************
2
3   Copyright (c) 2004 by Salvador E. Tropea.
4   Covered by the GPL license.
5
6   Comment:
7   Example/test of pseudo terminals.
8   Note: Contributed by Greg Watson (gwatson lanl gov)
9
10 ***************************************************************************/
11
12 #include <stdio.h>
13 #include "mi_gdb.h"
14
15 void cb_console(const char *str, void *data)
16 {
17  printf("CONSOLE> %s\n",str);
18 }
19
20 /* Note that unlike what's documented in gdb docs it isn't usable. */
21 void cb_target(const char *str, void *data)
22 {
23  printf("TARGET> %s\n",str);
24 }
25
26 void cb_log(const char *str, void *data)
27 {
28  printf("LOG> %s\n",str);
29 }
30
31 void cb_to(const char *str, void *data)
32 {
33  printf(">> %s",str);
34 }
35
36 void cb_from(const char *str, void *data)
37 {
38  printf("<< %s\n",str);
39 }
40
41 void cb_async(mi_output *o, void *data)
42 {
43  printf("ASYNC\n");
44 }
45
46 int main(int argc, char *argv[])
47 {
48  mi_pty *pty=NULL;
49  mi_h *h;
50  fd_set rfds;
51  char buf[BUFSIZ];
52
53  /* Connect to gdb child. */
54  h=mi_connect_local();
55  if (!h)
56    {
57     printf("Connect failed\n");
58     return 1;
59    }
60  printf("Connected to gdb!\n");
61
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);
69
70  /* Look for a free pseudo terminal. */
71  pty=gmi_look_for_free_pty();
72  if (!pty) 
73    {
74     printf("Error opening pseudo terminal.\n");
75     return 1;
76    }
77
78  printf("Free pty slave = %s, master on %d\n",pty->slave,pty->master);
79
80  /* Tell gdb to attach the terminal. */
81  if (!gmi_target_terminal(h,pty->slave))
82    {
83     printf("Error selecting target terminal\n");
84     mi_disconnect(h);
85     return 1;
86    }
87
88  /* Set the name of the child and the command line aguments. */
89  if (!gmi_set_exec(h,"./test_target","prb1 2 prb3"))
90    {
91     printf("Error setting exec y args\n");
92     mi_disconnect(h);
93     return 1;
94    }
95
96  /* Run the program. */
97  if (!gmi_exec_run(h))
98    {
99     printf("Error in run!\n");
100     mi_disconnect(h);
101     return 1;
102    }
103
104  for (;;)
105    {
106     FD_ZERO(&rfds);
107     FD_SET(pty->master,&rfds);
108     FD_SET(0,&rfds);
109     
110     if (select(pty->master+1,&rfds,NULL,NULL,NULL)<0)
111       {
112        perror("select");
113        mi_disconnect(h);
114        return 1;
115       }
116
117     if (FD_ISSET(pty->master,&rfds))
118       {
119        int n=read(pty->master,buf,BUFSIZ);
120        if (n<=0)
121           break;
122        write(1,buf,n);
123       }
124
125     if (FD_ISSET(0,&rfds))
126       {
127        int n=read(0,buf,BUFSIZ);
128        if (n<=0)
129           break;
130        write(pty->master,buf,n);
131       }
132    }
133
134  /* Exit from gdb. */
135  gmi_gdb_exit(h);
136  /* Close the connection. */
137  mi_disconnect(h);
138  gmi_end_pty(pty);
139
140  return 0;
141 }