home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / emacs-18.59src.lha / emacs-18.59 / src / amiga_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  6.3 KB  |  306 lines

  1. #include "config.h"
  2. #undef NULL
  3. #include "lisp.h"
  4. #include "termchar.h"
  5.  
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <internal/files.h>
  9. #include <internal/vars.h>
  10.  
  11. #undef LONGBITS
  12.  
  13. #include <exec/types.h>
  14. #include <dos/dos.h>
  15. #include <proto/exec.h>
  16.  
  17. #include "amiga.h"
  18. #include "termhooks.h"
  19.  
  20. static int term_initialised;
  21. ULONG inputsig;
  22.  
  23. /* A few tty system dependent routines unused on the Amiga */
  24.  
  25. setpgrp_of_tty(int pid) {}
  26. init_sigio() {}
  27. reset_sigio() {}
  28. request_sigio() {}
  29. unrequest_sigio() {}
  30.  
  31. /* Return nonzero if safe to use tabs in output.
  32.    At the time this is called, init_sys_modes has not been done yet.  */
  33.  
  34. tabs_safe_p()
  35. {
  36.   if (noninteractive)
  37.     return 1;
  38.  
  39.   return 0;            /* Not safe on Amiga !? */
  40. }
  41.  
  42. /* Get terminal size from system.
  43.    Store number of lines into *heightp and width into *widthp.
  44.    If zero or a negative number is stored, the value is not valid.  */
  45.  
  46. get_screen_size (widthp, heightp)
  47.      int *widthp, *heightp;
  48. {
  49.     if (term_initialised && !inhibit_window_system)
  50.     get_window_size(widthp, heightp);
  51.     else /* We don't known what size the terminal is */
  52.     {
  53.     *widthp = 0;
  54.     *heightp = 0;
  55.     }
  56. }
  57.  
  58. init_baud_rate ()
  59. {
  60.   if (noninteractive || !term_initialised) baud_rate = 1200;
  61.   else if (!inhibit_window_system) baud_rate = 38400;
  62.   else baud_rate = serial_baud_rate();
  63. }
  64.  
  65. void check_intuition ()
  66. {
  67.     if (noninteractive || inhibit_window_system)
  68.         error ("You aren't using a window.");
  69. }
  70.  
  71. #define TTYBUFSIZE 256        /* Same size as kbd_buffer */
  72. static char ttybuf[TTYBUFSIZE];
  73. static int tty_count;
  74. #define TTYPUT(c) { if (tty_count < TTYBUFSIZE) ttybuf[tty_count++] = c; }
  75.  
  76. static int interrupt_char;
  77.  
  78. void enque(unsigned int c, int meta)
  79. /* place input keys in keyboard buffer
  80.    If high bit is set, precede character with ^Q (hack).
  81.    If meta is true, set high bit.
  82.    If both the high bit & meta are true, we have a problem. Ignore it.
  83.    If c == AMIGASEQ (256) enqueue the amiga sequence introducer (C-x C-^)
  84. */
  85. {
  86.   /* Hack CSI to be AMIGASEQ (to allow defining function keys, etc) */
  87.   if (c == 0233 || c == AMIGASEQ)
  88.     {
  89.       TTYPUT('x' & 037);
  90.       TTYPUT('^' & 037);
  91.     }
  92.   else if (c >= 0200)    /* Special character, precede with ^Q */
  93.     {
  94.       TTYPUT('q' & 037);
  95.       TTYPUT(c);
  96.     }
  97.   else
  98.     {
  99.       if (meta) c |= 0200;
  100.       if (c == interrupt_char) Signal(_us, SIGBREAKF_CTRL_C);
  101.       else TTYPUT(c);
  102.     }
  103. }
  104.  
  105. int get_ttycount(void)
  106. {
  107.   return tty_count;
  108. }
  109.  
  110. init_sys_modes ()
  111. {
  112.   extern int quit_char;
  113.  
  114.   if (noninteractive)
  115.     return;
  116.  
  117.   if (inhibit_window_system) clear_screen();
  118.  
  119.   interrupt_char = quit_char;
  120.   if (!inhibit_window_system) setup_intchar(interrupt_char);
  121. }
  122.  
  123. reset_sys_modes ()
  124. {
  125.   if (noninteractive)
  126.     {
  127.       fflush (stdout);
  128.       return;
  129.     }
  130.   move_cursor (screen_height - 1, 0);
  131.   clear_end_of_line (screen_width);
  132.   /* clear_end_of_line may move the cursor */
  133.   move_cursor (screen_height - 1, 0);
  134. }
  135.  
  136. void amiga_consume_input(void)
  137. {
  138.   extern int this_command_key_count;
  139.   int force = this_command_key_count == 0;
  140.   /* If force is TRUE & some non-keyboard (eg mouse events) input is pending,
  141.      insert the appropriate magic sequence in the input stream */
  142.  
  143.   if (term_initialised)
  144.     {
  145.       if (!inhibit_window_system) check_window(force);
  146.       else check_serial(force);
  147.       check_arexx(force, TRUE);
  148.     }
  149. }
  150.  
  151. discard_tty_input ()
  152. {
  153.   if (noninteractive)
  154.     return;
  155.  
  156.   amiga_consume_input();
  157.   tty_count = 0;
  158.   chkabort();
  159. }
  160.  
  161. /* Code for the fd describing the emacs input (terminal or window) */
  162.  
  163. static ULONG __regargs ttyin_select_start(void *userinfo, int rd, int wr)
  164. {
  165.   if (!inhibit_window_system) force_window();
  166.  
  167.   return tty_count ? -1 : inputsig;
  168. }
  169.  
  170. static void __regargs ttyin_select_poll(void *userinfo, int *rd, int *wr)
  171. {
  172.   amiga_consume_input();
  173.   if (!tty_count) *rd = 0;
  174. }
  175.  
  176. static int __regargs ttyin_read(void *userinfo, void *buffer, unsigned int length)
  177. {
  178.   amiga_consume_input();
  179.   if (length > tty_count) length = tty_count;
  180.   memcpy(buffer, ttybuf, length);
  181.   tty_count -= length;
  182.   if (tty_count) memmove(ttybuf, ttybuf + length, tty_count - length);
  183.  
  184.   return (int)length;
  185. }
  186.  
  187. static int __regargs ttyin_write(void *userinfo, void *buffer, unsigned int length)
  188. {
  189.   errno = EACCES;
  190.   return -1;
  191. }
  192.  
  193. static int __regargs ttyin_lseek(void *userinfo, long rpos, int mode)
  194. {
  195.   errno = ESPIPE;
  196.   return -1;
  197. }
  198.  
  199. static int __regargs ttyin_close(void *userinfo, int internal)
  200. {
  201.   return 0;
  202. }
  203.  
  204. static int __regargs ttyin_ioctl(void *userinfo, int request, void *data)
  205. {
  206.   errno = EINVAL;
  207.   return -1;
  208. }
  209.  
  210. #define CBUFSIZE 1024
  211. #undef fwrite
  212. #undef fflush
  213.  
  214. char cbuffer[CBUFSIZE + 16], *cbuffer_pos;
  215.  
  216. int emacs_fflush(FILE *f)
  217. {
  218.     if (noninteractive || f != stdout) return fflush(f);
  219.     else
  220.     {
  221.     int len;
  222.  
  223.     len = cbuffer_pos - cbuffer;
  224.     if (term_initialised)
  225.         if (!inhibit_window_system) screen_puts(cbuffer, len);
  226.         else serial_puts(cbuffer, len);
  227.     if (termscript) fwrite (cbuffer, 1, len, termscript);
  228.     cbuffer_pos = cbuffer;
  229.  
  230.     return 0;
  231.     }
  232. }
  233.  
  234. void emacs_putchar(int c)
  235. {
  236.     if (cbuffer_pos >= cbuffer + CBUFSIZE) emacs_fflush(stdout);
  237.     *cbuffer_pos++ = c;
  238. }
  239.  
  240. void emacs_output(char *str, int size)
  241. {
  242.     if (cbuffer_pos + size > cbuffer + CBUFSIZE) emacs_fflush(stdout);
  243.     if (size > CBUFSIZE)
  244.     {
  245.     if (term_initialised)
  246.         if (!inhibit_window_system) screen_puts(str, size);
  247.         else serial_puts(str, size);
  248.     }
  249.     else
  250.     {
  251.     memcpy(cbuffer_pos, str, size);
  252.     cbuffer_pos += size;
  253.     }
  254. }
  255.  
  256. void emacs_fwrite(char *str, unsigned int nblocks, unsigned int len, FILE *f)
  257. {
  258.     if (noninteractive || f != stdout) fwrite (str, nblocks, len, f);
  259.     else
  260.     {
  261.     unsigned int size;
  262.  
  263.     if (nblocks == 1) size = len; /* Emacs always uses 1 "block" */
  264.     else size = nblocks * len;
  265.  
  266.     emacs_output(str, size);
  267.     }
  268. }
  269.  
  270. void syms_of_amiga_tty(void)
  271. {
  272.   syms_of_amiga_screen();
  273.   syms_of_amiga_rexx();
  274. }
  275.  
  276. void init_amiga_tty()
  277. {
  278.   inputsig = 0;
  279.   term_initialised = FALSE;
  280.   init_amiga_rexx();
  281. }
  282.  
  283. void cleanup_amiga_tty()
  284. {
  285.   cleanup_amiga_rexx();
  286.   cleanup_amiga_serial();
  287.   cleanup_amiga_screen();
  288. }
  289.  
  290. void early_amiga_tty()
  291. {
  292.   cbuffer_pos = cbuffer;
  293.   tty_count = 0;
  294. }
  295.  
  296. void amiga_term_open(void)
  297. {
  298.   inhibit_window_system ? init_amiga_serial() : init_amiga_screen();
  299.   close(0);
  300.   if (_alloc_fd((void *)1, FI_READ, ttyin_select_start, ttyin_select_poll, ttyin_read,
  301.         ttyin_write, ttyin_lseek, ttyin_close, ttyin_ioctl) == 0)
  302.     term_initialised = TRUE;
  303.   else _fail("Failed to initialise I/O, no memory ?");
  304. }
  305.  
  306.