* Makefile.in (SFILES OBS): Add serial.[co] & ser-hardwire.[co].
[external/binutils.git] / gdb / serial.c
1 /* Generic serial interface routines
2    Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "serial.h"
22
23 /* Open up a device or a network socket, depending upon the syntax of NAME. */
24
25 static struct serial_ops *serial_ops_list = NULL;
26
27 static struct serial_ops *
28 serial_interface_lookup (name)
29      char *name;
30 {
31   struct serial_ops *ops;
32
33   for (ops = serial_ops_list; ops; ops = ops->next)
34     if (strcmp (name, ops->name) == 0)
35       return ops;
36
37   return NULL;
38 }
39
40 void
41 serial_add_interface(optable)
42      struct serial_ops *optable;
43 {
44   optable->next = serial_ops_list;
45   serial_ops_list = optable;
46 }
47
48 serial_t
49 serial_open(name)
50      const char *name;
51 {
52   serial_t scb;
53   struct serial_ops *ops;
54
55   ops = serial_interface_lookup ("hardwire");
56
57   if (!ops)
58     return NULL;
59
60   scb = (serial_t)xmalloc (sizeof (struct _serial_t));
61
62   scb->ops = ops;
63
64   scb->bufcnt = 0;
65   scb->bufp = scb->buf;
66
67   if (SERIAL_OPEN (scb, name))
68     {
69       free (scb);
70       return NULL;
71     }
72
73   return scb;
74 }
75
76 #if 0
77 /* Connect the user directly to the remote system.  This command acts just like
78    the 'cu' or 'tip' command.  Use <CR>~. or <CR>~^D to break out.  */
79
80 static void
81 cleanup_tty(ttystate)
82      struct ttystate ttystate;
83 {
84   printf("\r\n[Exiting connect mode]\r\n");
85   serial_restore(0, &ttystate);
86 }
87
88 static void
89 connect_command (args, fromtty)
90      char       *args;
91      int        fromtty;
92 {
93   fd_set readfds;
94   int numfds;
95   int c;
96   char cur_esc = 0;
97   static struct ttystate ttystate;
98
99   dont_repeat();
100
101   if (desc < 0)
102     error("target not open.");
103   
104   if (args)
105     fprintf("This command takes no args.  They have been ignored.\n");
106         
107   printf("[Entering connect mode.  Use ~. or ~^D to escape]\n");
108
109   serial_raw(0, &ttystate);
110
111   make_cleanup(cleanup_tty, &ttystate);
112
113   FD_ZERO(&readfds);
114
115   while (1)
116     {
117       do
118         {
119           FD_SET(0, &readfds);
120           FD_SET(desc, &readfds);
121           numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
122         }
123       while (numfds == 0);
124
125       if (numfds < 0)
126         perror_with_name("select");
127
128       if (FD_ISSET(0, &readfds))
129         {                       /* tty input, send to stdebug */
130           char cx;
131
132           c = serial_readchar(-1);
133           if (c < 0)
134             perror_with_name("connect");
135
136           cx = c;
137           serial_write(&cx, 1);
138           switch (cur_esc)
139             {
140             case 0:
141               if (c == '\r')
142                 cur_esc = c;
143               break;
144             case '\r':
145               if (c == '~')
146                 cur_esc = c;
147               else
148                 cur_esc = 0;
149               break;
150             case '~':
151               if (c == '.' || c == '\004')
152                 return;
153               else
154                 cur_esc = 0;
155             }
156         }
157
158       if (FD_ISSET(desc, &readfds))
159         {
160           while (1)
161             {
162               c = serial_readchar(-1);
163               if (c < 0)
164                 break;
165               putchar(c);
166             }
167           fflush(stdout);
168         }
169     }
170 }
171 #endif
172
173 #if 0
174 void
175 _initialize_serial ()
176 {
177   add_com ("connect", class_obscure, connect_command,
178            "Connect the terminal directly up to the command monitor.\n\
179 Use <CR>~. or <CR>~^D to break out.");
180 }
181 #endif