4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.3 2008/02/13 09:12:21 james
14 * *** empty log message ***
16 * Revision 1.2 2008/02/13 01:08:18 james
17 * *** empty log message ***
19 * Revision 1.1 2008/02/12 22:36:46 james
20 * *** empty log message ***
22 * Revision 1.1 2008/02/09 15:47:28 james
23 * *** empty log message ***
25 * Revision 1.2 2008/02/07 11:11:14 staffcvs
26 * *** empty log message ***
28 * Revision 1.1 2008/02/07 01:02:52 james
29 * *** empty log message ***
31 * Revision 1.3 2008/02/06 17:53:28 james
32 * *** empty log message ***
34 * Revision 1.2 2008/02/04 02:05:06 james
35 * *** empty log message ***
37 * Revision 1.1 2008/02/04 01:32:39 james
38 * *** empty log message ***
45 typedef struct TERMINAL_struct
48 struct termios orig_termios;
49 struct TERMINAL_struct *next;
53 static TERMINAL *terminal_list = NULL;
59 terminal_close (TTY * _t)
61 TERMINAL *t = (TERMINAL *) _t;
62 TERMINAL **ptr = &terminal_list;
67 /* Take out of cleanup list */
68 while (*ptr && (*ptr != t))
69 ptr = &((*ptr)->next);
74 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
76 set_blocking (t->rfd);
77 set_blocking (t->wfd);
84 terminal_atexit (void)
87 terminal_close ((TTY *) terminal_list);
105 terminal_getsize (TTY * _t)
107 TERMINAL *t = (TERMINAL *) _t;
108 struct winsize sz = { 0 };
113 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
115 t->size.x = CRT_COLS;
116 t->size.y = CRT_ROWS;
120 t->size.x = sz.ws_col;
121 t->size.y = sz.ws_row;
127 terminal_dispatch (void)
132 if (!terminal_winches)
135 terminal_winches = 0;
137 for (t = terminal_list; t; t = t->next)
138 terminal_getsize (t);
144 terminal_read (TTY * _t, void *buf, int len)
146 TERMINAL *t = (TERMINAL *) _t;
149 terminal_dispatch ();
154 red = wrap_read (t->rfd, buf, len);
172 terminal_write (TTY * _t, void *buf, int len)
175 TERMINAL *t = (TERMINAL *) _t;
177 terminal_dispatch ();
182 writ = wrap_write (t->wfd, buf, len);
200 terminal_register_handlers (void)
202 struct sigaction sa = { 0 };
204 sa.sa_handler = sigwinch;
205 sa.sa_flags = SA_RESTART;
206 sigaction (SIGWINCH, &sa, NULL);
208 sa.sa_handler = sigint;
209 sa.sa_flags = SA_RESTART;
210 sigaction (SIGINT, &sa, NULL);
214 terminal_open (int rfd, int wfd)
219 struct termios termios;
221 t = (TERMINAL *) malloc (sizeof (TERMINAL));
226 tcgetattr (wfd, &t->orig_termios);
228 t->next = terminal_list;
231 tcgetattr (wfd, &termios);
233 set_nonblocking (rfd);
234 set_nonblocking (wfd);
237 raw_termios (&termios);
239 tcsetattr (wfd, TCSANOW, &termios);
241 t->recv = terminal_read;
242 t->xmit = terminal_write;
243 t->close = terminal_close;
246 terminal_getsize ((TTY *) t);