4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.6 2008/02/14 00:57:58 james
14 * *** empty log message ***
16 * Revision 1.5 2008/02/13 18:05:06 james
17 * *** empty log message ***
19 * Revision 1.4 2008/02/13 16:57:29 james
20 * *** empty log message ***
22 * Revision 1.3 2008/02/13 09:12:21 james
23 * *** empty log message ***
25 * Revision 1.2 2008/02/13 01:08:18 james
26 * *** empty log message ***
28 * Revision 1.1 2008/02/12 22:36:46 james
29 * *** empty log message ***
31 * Revision 1.1 2008/02/09 15:47:28 james
32 * *** empty log message ***
34 * Revision 1.2 2008/02/07 11:11:14 staffcvs
35 * *** empty log message ***
37 * Revision 1.1 2008/02/07 01:02:52 james
38 * *** empty log message ***
40 * Revision 1.3 2008/02/06 17:53:28 james
41 * *** empty log message ***
43 * Revision 1.2 2008/02/04 02:05:06 james
44 * *** empty log message ***
46 * Revision 1.1 2008/02/04 01:32:39 james
47 * *** empty log message ***
54 typedef struct TERMINAL_struct
57 struct termios orig_termios;
58 struct TERMINAL_struct *next;
62 static TERMINAL *terminal_list = NULL;
68 terminal_close (TTY * _t)
73 TERMINAL *t = (TERMINAL *) _t;
74 TERMINAL **ptr = &terminal_list;
79 /* Take out of cleanup list */
80 while (*ptr && (*ptr != t))
81 ptr = &((*ptr)->next);
86 tcsetattr (t->wfd, TCSANOW, &t->orig_termios);
88 set_nonblocking (t->wfd);
91 t->xmit (_t, "\033[r", 3);
92 t->xmit (_t, "\033[0m", 4);
93 i = sprintf (buf, "\033[%d;%dH", CRT_ROWS + 1, 1);
95 t->xmit (_t, "\033[J", 3);
97 set_blocking (t->rfd);
98 set_blocking (t->wfd);
105 terminal_atexit (void)
107 while (terminal_list)
108 terminal_close ((TTY *) terminal_list);
126 terminal_getsize (TTY * _t)
128 TERMINAL *t = (TERMINAL *) _t;
129 struct winsize sz = { 0 };
134 if (ioctl (t->wfd, TIOCGWINSZ, &sz))
136 t->size.x = CRT_COLS;
137 t->size.y = CRT_ROWS;
141 t->size.x = sz.ws_col;
142 t->size.y = sz.ws_row;
148 terminal_dispatch (void)
153 if (!terminal_winches)
156 terminal_winches = 0;
158 for (t = terminal_list; t; t = t->next)
159 terminal_getsize ((TTY *) t);
165 terminal_read (TTY * _t, void *buf, int len)
167 TERMINAL *t = (TERMINAL *) _t;
170 terminal_dispatch ();
171 set_nonblocking (t->rfd);
176 red = wrap_read (t->rfd, buf, len);
194 terminal_write (TTY * _t, void *buf, int len)
197 TERMINAL *t = (TERMINAL *) _t;
199 terminal_dispatch ();
201 set_blocking (t->wfd);
206 writ = wrap_write (t->wfd, buf, len);
225 terminal_register_handlers (void)
227 struct sigaction sa = { 0 };
229 sa.sa_handler = sigwinch;
230 sa.sa_flags = SA_RESTART;
231 sigaction (SIGWINCH, &sa, NULL);
233 sa.sa_handler = sigint;
234 sa.sa_flags = SA_RESTART;
235 sigaction (SIGINT, &sa, NULL);
239 terminal_open (int rfd, int wfd)
244 struct termios termios;
246 t = (TERMINAL *) malloc (sizeof (TERMINAL));
251 tcgetattr (wfd, &t->orig_termios);
253 t->next = terminal_list;
256 tcgetattr (wfd, &termios);
258 set_nonblocking (rfd);
259 set_nonblocking (wfd);
262 cfmakeraw (&termios);
263 //raw_termios (&termios);
265 tcsetattr (wfd, TCSANOW, &termios);
267 t->recv = terminal_read;
268 t->xmit = terminal_write;
269 t->close = terminal_close;
272 terminal_getsize ((TTY *) t);