home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / serial.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  3.6 KB  |  191 lines

  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 (scb->ops->open(scb, name))
  68.     {
  69.       free (scb);
  70.       return NULL;
  71.     }
  72.  
  73.   return scb;
  74. }
  75.  
  76. void
  77. serial_close(scb)
  78.      serial_t scb;
  79. {
  80.   scb->ops->close(scb);
  81.  
  82.   free(scb);
  83. }
  84.  
  85. #if 0
  86. /* Connect the user directly to the remote system.  This command acts just like
  87.    the 'cu' or 'tip' command.  Use <CR>~. or <CR>~^D to break out.  */
  88.  
  89. static void
  90. cleanup_tty(ttystate)
  91.      struct ttystate ttystate;
  92. {
  93.   printf("\r\n[Exiting connect mode]\r\n");
  94.   serial_restore(0, &ttystate);
  95. }
  96.  
  97. static void
  98. connect_command (args, fromtty)
  99.      char    *args;
  100.      int    fromtty;
  101. {
  102.   fd_set readfds;
  103.   int numfds;
  104.   int c;
  105.   char cur_esc = 0;
  106.   static struct ttystate ttystate;
  107.  
  108.   dont_repeat();
  109.  
  110.   if (desc < 0)
  111.     error("target not open.");
  112.   
  113.   if (args)
  114.     fprintf("This command takes no args.  They have been ignored.\n");
  115.     
  116.   printf("[Entering connect mode.  Use ~. or ~^D to escape]\n");
  117.  
  118.   serial_raw(0, &ttystate);
  119.  
  120.   make_cleanup(cleanup_tty, &ttystate);
  121.  
  122.   FD_ZERO(&readfds);
  123.  
  124.   while (1)
  125.     {
  126.       do
  127.     {
  128.       FD_SET(0, &readfds);
  129.       FD_SET(desc, &readfds);
  130.       numfds = select(sizeof(readfds)*8, &readfds, 0, 0, 0);
  131.     }
  132.       while (numfds == 0);
  133.  
  134.       if (numfds < 0)
  135.     perror_with_name("select");
  136.  
  137.       if (FD_ISSET(0, &readfds))
  138.     {            /* tty input, send to stdebug */
  139.       char cx;
  140.  
  141.       c = serial_readchar(-1);
  142.       if (c < 0)
  143.         perror_with_name("connect");
  144.  
  145.       cx = c;
  146.       serial_write(&cx, 1);
  147.       switch (cur_esc)
  148.         {
  149.         case 0:
  150.           if (c == '\r')
  151.         cur_esc = c;
  152.           break;
  153.         case '\r':
  154.           if (c == '~')
  155.         cur_esc = c;
  156.           else
  157.         cur_esc = 0;
  158.           break;
  159.         case '~':
  160.           if (c == '.' || c == '\004')
  161.         return;
  162.           else
  163.         cur_esc = 0;
  164.         }
  165.     }
  166.  
  167.       if (FD_ISSET(desc, &readfds))
  168.     {
  169.       while (1)
  170.         {
  171.           c = serial_readchar(-1);
  172.           if (c < 0)
  173.         break;
  174.           putchar(c);
  175.         }
  176.       fflush(stdout);
  177.     }
  178.     }
  179. }
  180. #endif
  181.  
  182. #if 0
  183. void
  184. _initialize_serial ()
  185. {
  186.   add_com ("connect", class_obscure, connect_command,
  187.        "Connect the terminal directly up to the command monitor.\n\
  188. Use <CR>~. or <CR>~^D to break out.");
  189. }
  190. #endif
  191.