home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / uxtty.c < prev    next >
C/C++ Source or Header  |  1999-01-02  |  5KB  |  185 lines

  1. /* -*-C-*-
  2.  
  3. $Id: uxtty.c,v 1.10 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1990-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "ux.h"
  23. #include "ostty.h"
  24. #include "osenv.h"
  25. #include "uxio.h"
  26. #include "uxterm.h"
  27.  
  28. /* Standard Input and Output */
  29.  
  30. static Tchannel input_channel;
  31. static Tchannel output_channel;
  32. static int tty_x_size;
  33. static int tty_y_size;
  34. static CONST char * tty_command_beep;
  35. static CONST char * tty_command_clear;
  36.  
  37. Tchannel
  38. DEFUN_VOID (OS_tty_input_channel)
  39. {
  40.   return (input_channel);
  41. }
  42.  
  43. Tchannel
  44. DEFUN_VOID (OS_tty_output_channel)
  45. {
  46.   return (output_channel);
  47. }
  48.  
  49. unsigned int
  50. DEFUN_VOID (OS_tty_x_size)
  51. {
  52.   return (tty_x_size);
  53. }
  54.  
  55. unsigned int
  56. DEFUN_VOID (OS_tty_y_size)
  57. {
  58.   return (tty_y_size);
  59. }
  60.  
  61. CONST char *
  62. DEFUN_VOID (OS_tty_command_beep)
  63. {
  64.   return (tty_command_beep);
  65. }
  66.  
  67. CONST char *
  68. DEFUN_VOID (OS_tty_command_clear)
  69. {
  70.   return (tty_command_clear);
  71. }
  72.  
  73. #ifndef TERMCAP_BUFFER_SIZE
  74. #define TERMCAP_BUFFER_SIZE 2048
  75. #endif
  76.  
  77. #ifndef DEFAULT_TTY_X_SIZE
  78. #define DEFAULT_TTY_X_SIZE 80
  79. #endif
  80.  
  81. #ifndef DEFAULT_TTY_Y_SIZE
  82. #define DEFAULT_TTY_Y_SIZE 24
  83. #endif
  84.  
  85. /* Define the 4.3 names in terms of the Sun names
  86.    if the latter exist and the former do not.  */
  87. #ifdef TIOCGSIZE
  88. #ifndef TIOCGWINSZ
  89. #define TIOCGWINSZ TIOCGSIZE
  90. #define winsize ttysize
  91. #define ws_row ts_lines
  92. #define ws_col ts_cols
  93. #endif /* not TIOCGWINSZ */
  94. #endif /* TIOCGSIZE */
  95.  
  96. static char tputs_output [TERMCAP_BUFFER_SIZE];
  97. static char * tputs_output_scan;
  98.  
  99. static void
  100. DEFUN (tputs_write_char, (c), char c)
  101. {
  102.   (*tputs_output_scan++) = c;
  103. }
  104.  
  105. void
  106. DEFUN_VOID (UX_initialize_tty)
  107. {
  108.   extern int EXFUN (atoi, (CONST char *));
  109.   extern Tchannel EXFUN (OS_open_fd, (int fd));
  110.   input_channel = (OS_open_fd (STDIN_FILENO));
  111.   (CHANNEL_INTERNAL (input_channel)) = 1;
  112.   output_channel = (OS_open_fd (STDOUT_FILENO));
  113.   (CHANNEL_INTERNAL (output_channel)) = 1;
  114.   tty_x_size = (-1);
  115.   tty_y_size = (-1);
  116.   tty_command_beep = ALERT_STRING;
  117.   tty_command_clear = 0;
  118.   /* Figure out the size of the terminal.  First ask the operating
  119.      system, if it has an appropriate system call.  Then try the
  120.      environment variables COLUMNS and LINES.  Then try termcap.
  121.      Finally, use the default.  */
  122. #ifdef TIOCGWINSZ
  123.   {
  124.     struct winsize size;
  125.     if ((UX_ioctl (STDOUT_FILENO, TIOCGWINSZ, (&size))) >= 0)
  126.       {
  127.     tty_x_size = (size . ws_col);
  128.     tty_y_size = (size . ws_row);
  129.       }
  130.   }
  131. #endif /* TIOCGWINSZ */
  132.   if ((tty_x_size <= 0) || (tty_y_size <= 0))
  133.     {
  134.       CONST char * columns = (UX_getenv ("COLUMNS"));
  135.       CONST char * lines = (UX_getenv ("LINES"));
  136.       if ((columns != 0) && (lines != 0))
  137.     {
  138.       int x = (atoi (columns));
  139.       int y = (atoi (lines));
  140.       if ((x > 0) && (y > 0))
  141.         {
  142.           tty_x_size = x;
  143.           tty_y_size = y;
  144.         }
  145.     }
  146.     }
  147.   tputs_output_scan = tputs_output;
  148.   {
  149.     extern int EXFUN (tgetent, (PTR, CONST char *));
  150.     extern int EXFUN (tgetnum, (CONST char *));
  151.     extern CONST char * EXFUN (tgetstr, (CONST char *, char **));
  152.     static char tgetstr_buffer [TERMCAP_BUFFER_SIZE];
  153.     char termcap_buffer [TERMCAP_BUFFER_SIZE];
  154.     char * tbp = tgetstr_buffer;
  155.     CONST char * term;
  156.     if ((isatty (STDOUT_FILENO))
  157.     && (!option_emacs_subprocess)
  158.     && ((term = (getenv ("TERM"))) != 0)
  159.     && ((tgetent (termcap_buffer, term)) > 0))
  160.       {
  161.     if ((tty_x_size <= 0) || (tty_y_size <= 0))
  162.       {
  163.         tty_x_size = (tgetnum ("co"));
  164.         tty_y_size = (tgetnum ("li"));
  165.       }
  166.     tty_command_clear = (tgetstr ("cl", (&tbp)));
  167.       }
  168.   }
  169.   if ((tty_x_size <= 0) || (tty_y_size <= 0))
  170.     {
  171.       tty_x_size = DEFAULT_TTY_X_SIZE;
  172.       tty_y_size = DEFAULT_TTY_Y_SIZE;
  173.     }
  174.   if (tty_command_clear == 0)
  175.     tty_command_clear = "\f";
  176.   else
  177.     {
  178.       extern void EXFUN (tputs, (CONST char *, int, void (*) (char)));
  179.       char * command = tputs_output_scan;
  180.       tputs (tty_command_clear, tty_y_size, tputs_write_char);
  181.       (*tputs_output_scan++) = '\0';
  182.       tty_command_clear = command;
  183.     }
  184. }
  185.