From: james Date: Wed, 13 Feb 2008 01:08:18 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=sympathy.git;a=commitdiff_plain;h=89fa7c69158bf37823875c61345024308b156772 *** empty log message *** --- diff --git a/src/ansi.c b/src/ansi.c index c501ab9..b6f30f9 100644 --- a/src/ansi.c +++ b/src/ansi.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.17 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.16 2008/02/07 13:22:51 james * *** empty log message *** * @@ -80,50 +83,6 @@ set_blocking (int fd) } -int -ansi_read (ANSI * a, void *buf, int n) -{ - int red; - - set_nonblocking (a->fd); - red = read (a->fd, buf, n); - - if (!red) - return -1; - - if ((red == -1) && (errno == EAGAIN)) - { - return 0; - } - - return red; -} - -void -ansi_write (ANSI * a, char *buf, int n) -{ - set_blocking (a->fd); - write (a->fd, buf, n); -} - -void -ansi_getsize (ANSI * a) -{ - struct winsize sz = { 0 }; - if (ioctl (a->fd, TIOCGWINSZ, &sz)) - { - a->size.x = CRT_COLS; - a->size.y = CRT_ROWS; - } - else - { - a->size.x = sz.ws_col; - a->size.y = sz.ws_row; - } - -} - - void ansi_move (ANSI * a, CRT_Pos p) { @@ -144,70 +103,70 @@ ansi_move (ANSI * a, CRT_Pos p) { if (dx == 1) { - ansi_write (a, "\033[C", 3); + a->terminal->xmit (a->terminal, "\033[C", 3); } else if (dx == -1) { - ansi_write (a, "\033[D", 3); + a->terminal->xmit (a->terminal, "\033[D", 3); } else { n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } } else if (!dx) { if (dy == -1) { - ansi_write (a, "\033[A", 3); + a->terminal->xmit (a->terminal, "\033[A", 3); } else if (dy == 1) { - ansi_write (a, "\033[B", 3); + a->terminal->xmit (a->terminal, "\033[B", 3); } else if (dy < 0) { n = snprintf (buf, sizeof (buf), "\033[%dA", -dy); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } else { n = snprintf (buf, sizeof (buf), "\033[%dB", dy); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } } else if (!p.x) { if (dy == 1) { - ansi_write (a, "\033[E", 3); + a->terminal->xmit (a->terminal, "\033[E", 3); } else if (dy == -1) { - ansi_write (a, "\033[F", 3); + a->terminal->xmit (a->terminal, "\033[F", 3); } else if (dy > 0) { n = snprintf (buf, sizeof (buf), "\033[%dE", dy); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } else { n = snprintf (buf, sizeof (buf), "\033[%dF", -dy); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } } else { n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } } else { n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1); - ansi_write (a, buf, n); + a->terminal->xmit (a->terminal, buf, n); } a->pos = p; @@ -222,11 +181,11 @@ ansi_showhide_cursor (ANSI * a, int hide) if (hide) { - ansi_write (a, "\033[?25l", 6); + a->terminal->xmit (a->terminal, "\033[?25l", 6); } else { - ansi_write (a, "\033[?25h", 6); + a->terminal->xmit (a->terminal, "\033[?25h", 6); } a->hide_cursor = hide; @@ -236,7 +195,7 @@ ansi_showhide_cursor (ANSI * a, int hide) void ansi_force_attr_normal (ANSI * a) { - ansi_write (a, "\033[0m", 4); + a->terminal->xmit (a->terminal, "\033[0m", 4); a->attr = CRT_ATTR_NORMAL; a->color = ANSI_INVAL; } @@ -277,7 +236,7 @@ ansi_set_color (ANSI * a, int color) fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color); #endif - ansi_write (a, buf, i); + a->terminal->xmit (a->terminal, buf, i); a->color = color; } } @@ -306,34 +265,34 @@ ansi_set_attr (ANSI * a, int attr) { if (attr & CRT_ATTR_UNDERLINE) { - ansi_write (a, "\033[4m", 4); + a->terminal->xmit (a->terminal, "\033[4m", 4); } else { - ansi_write (a, "\033[24m", 5); + a->terminal->xmit (a->terminal, "\033[24m", 5); } } if (dif & CRT_ATTR_REVERSE) { if (attr & CRT_ATTR_REVERSE) { - ansi_write (a, "\033[7m", 4); + a->terminal->xmit (a->terminal, "\033[7m", 4); } else { - ansi_write (a, "\033[27m", 5); + a->terminal->xmit (a->terminal, "\033[27m", 5); } } if (dif & CRT_ATTR_BOLD) { if (attr & CRT_ATTR_BOLD) { - ansi_write (a, "\033[1m", 4); + a->terminal->xmit (a->terminal, "\033[1m", 4); } else { - ansi_write (a, "\033[21m", 5); - ansi_write (a, "\033[22m", 5); + a->terminal->xmit (a->terminal, "\033[21m", 5); + a->terminal->xmit (a->terminal, "\033[22m", 5); } } @@ -353,7 +312,7 @@ ansi_render (ANSI * a, CRT_CA ca) ansi_set_attr (a, ca.attr); ansi_set_color (a, ca.color); - ansi_write (a, &ca.chr, 1); + a->terminal->xmit (a->terminal, &ca.chr, 1); a->pos.x++; @@ -375,11 +334,12 @@ ansi_cls (ANSI * a) ansi_force_attr_normal (a); ansi_set_color (a, CRT_COLOR_NORMAL); ansi_move (a, p); - ansi_write (a, "\033[2J", 4); + a->terminal->xmit (a->terminal, "\033[2J", 4); /*different emulators leave cursor in different places after cls differently*/ a->pos.x = ANSI_INVAL; } +#if 0 int ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e) { @@ -396,11 +356,11 @@ ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e) ansi_showhide_cursor (a, 1); i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1); - ansi_write (a, buf, i); + a->terminal->xmit (a->terminal, buf, i); i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0); - ansi_write (a, buf, i); - ansi_write (a, "\033D", 2); - ansi_write (a, "\033[r", 3); + a->terminal->xmit (a->terminal, buf, i); + a->terminal->xmit (a->terminal, "\033D", 2); + a->terminal->xmit (a->terminal, "\033[r", 3); s.y = e.y; crt_erase (&a->crt, s, e, 1); @@ -461,7 +421,7 @@ ansi_spot_scroll (ANSI * a, CRT * c) return; } - +#endif void @@ -471,7 +431,8 @@ ansi_draw (ANSI * a, CRT * c) int o; int hidden_cursor = 0; - ansi_spot_scroll (a, c); + if (crt_pos_cmp(a->terminal.size,a->size)) { + for (p.y = 0; p.y < CRT_ROWS; ++p.y) { @@ -505,7 +466,7 @@ ansi_draw (ANSI * a, CRT * c) ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED)); ansi_move (a, p); - ansi_write (a, msg, sizeof (msg)); + a->terminal->xmit (a->terminal, msg, sizeof (msg)); a->pos.x = ANSI_INVAL; } @@ -527,8 +488,11 @@ void ansi_reset (ANSI * a) { // FIXME: -- echos back crap? -// ansi_write (a, "\033[c", 3); - ansi_getsize (a); +// a->terminal->xmit (a->terminal, "\033[c", 3); + + terminal_getsize(a->terminal); + + a->size=a->terminal->size; a->pos.x = ANSI_INVAL; a->hide_cursor = ANSI_INVAL; @@ -536,21 +500,21 @@ ansi_reset (ANSI * a) crt_reset (&a->crt); ansi_cls (a); - ansi_write (a, "\033=", 2); - ansi_write (a, "\033[?6l", 5); - ansi_write (a, "\033[r", 3); + a->terminal->xmit (a->terminal, "\033=", 2); + a->terminal->xmit (a->terminal, "\033[?6l", 5); + a->terminal->xmit (a->terminal, "\033[r", 3); ansi_draw (a, &a->crt); } void -ansi_flush_escape (ANSI * a, VT102 * v, TTY * t) +ansi_flush_escape (ANSI * a, Context *c) { ANSI_Parser *p = &a->parser; int i; for (i = 0; i < p->escape_ptr; ++i) { - vt102_send (v, p->escape_buf[i], t); + vt102_send (c->v, p->escape_buf[i], c->t); } p->escape_ptr = 0; @@ -558,26 +522,26 @@ ansi_flush_escape (ANSI * a, VT102 * v, TTY * t) } void -ansi_parse_deckey (ANSI * a, VT102 * v, TTY * t) +ansi_parse_deckey (ANSI * a, Context *c) { ANSI_Parser *p = &a->parser; if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O')) { - ansi_flush_escape (a, v, t); + ansi_flush_escape (a, c); return; } if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z')) { - vt102_send (v, KEY_UP + (p->escape_buf[2] - 'A'), t); + vt102_send (c->v, KEY_UP + (p->escape_buf[2] - 'A'), c->t); } else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z')) { - vt102_send (v, KEY_154 + (p->escape_buf[2] - 'a'), t); + vt102_send (c->v, KEY_154 + (p->escape_buf[2] - 'a'), c->t); } else { - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); return; } p->in_escape = 0; @@ -585,22 +549,22 @@ ansi_parse_deckey (ANSI * a, VT102 * v, TTY * t) } void -ansi_parse_ansikey (ANSI * a, VT102 * v, TTY * t) +ansi_parse_ansikey (ANSI * a,Context *c) { ANSI_Parser *p = &a->parser; if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~')) { - ansi_flush_escape (a, v, t); + ansi_flush_escape (a, c); return; } if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9')) { - vt102_send (v, KEY_180 + (p->escape_buf[2] - '0'), t); + vt102_send (c->v, KEY_180 + (p->escape_buf[2] - '0'), c->t); } else { - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); return; } @@ -611,7 +575,7 @@ ansi_parse_ansikey (ANSI * a, VT102 * v, TTY * t) void -ansi_parse_escape (ANSI * a, VT102 * v, TTY * t) +ansi_parse_escape (ANSI * a,Context *c) { ANSI_Parser *p = &a->parser; switch (p->escape_ptr) @@ -626,41 +590,41 @@ ansi_parse_escape (ANSI * a, VT102 * v, TTY * t) case 'O': break; default: - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); } break; case 3: switch (p->escape_buf[1]) { case 'O': - ansi_parse_deckey (a, v, t); + ansi_parse_deckey (a, c); break; case '[': if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z')) - ansi_parse_deckey (a, v, t); + ansi_parse_deckey (a,c); break; default: - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); } break; case 4: switch (p->escape_buf[1]) { case '[': - ansi_parse_ansikey (a, v, t); + ansi_parse_ansikey (a, c); break; default: - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); } break; case 5: - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); } } void -ansi_check_escape (ANSI * a, VT102 * v, TTY * t) +ansi_check_escape (ANSI * a, Context *c) { ANSI_Parser *p = &a->parser; struct timeval now, diff; @@ -678,24 +642,24 @@ ansi_check_escape (ANSI * a, VT102 * v, TTY * t) /*Time up? */ if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT)) - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); } void -ansi_parse_char (ANSI * a, int c, VT102 * v, TTY * t) +ansi_parse_char (ANSI * a,Context *c, int ch) { ANSI_Parser *p = &a->parser; /*See if it's time to flush the escape*/ - ansi_check_escape (a, v, t); + ansi_check_escape (a, c); - if (c == 033) + if (ch == 033) { if (p->in_escape) - ansi_flush_escape (a, v, t); + ansi_flush_escape (a,c); p->in_escape++; p->escape_ptr = 0; @@ -704,33 +668,33 @@ ansi_parse_char (ANSI * a, int c, VT102 * v, TTY * t) if (p->in_escape) { - p->escape_buf[p->escape_ptr++] = c; - ansi_parse_escape (a, v, t); + p->escape_buf[p->escape_ptr++] = ch; + ansi_parse_escape (a,c); } else { - vt102_send (v, c, t); + vt102_send (c->v, ch, c->t); } } void -ansi_parse (ANSI * a, char *buf, int len, VT102 * v, TTY * t) +ansi_parse (ANSI * a,Context *c, char *buf, int len) { while (len--) - ansi_parse_char (a, *(buf++), v, t); + ansi_parse_char (a,c, *(buf++)); } int -ansi_dispatch (ANSI * a, VT102 * v, TTY * t) +ansi_dispatch (ANSI * a, Context *c) { char buf[1024]; int red; - ansi_check_escape (a, v, t); + ansi_check_escape (a, c); - red = ansi_read (a, buf, sizeof (buf)); + red = a->terminal->recv(a->terminal,buf,sizeof(buf)); if (red <= 0) return red; @@ -745,7 +709,7 @@ ansi_dispatch (ANSI * a, VT102 * v, TTY * t) #endif - ansi_parse (a, buf, red, v, t); + ansi_parse (a,c, buf, red ); return 0; } diff --git a/src/ansi.h b/src/ansi.h index e88f076..1368ada 100644 --- a/src/ansi.h +++ b/src/ansi.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.6 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.5 2008/02/07 12:16:04 james * *** empty log message *** * @@ -47,7 +50,7 @@ typedef struct typedef struct { - int fd; + TTY *terminal; CRT crt; CRT_Pos pos; diff --git a/src/crt.h b/src/crt.h index da5922c..addaed8 100644 --- a/src/crt.h +++ b/src/crt.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.7 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.6 2008/02/07 13:22:51 james * *** empty log message *** * @@ -111,4 +114,10 @@ crt_ca_cmp (CRT_CA a, CRT_CA b) return memcmp (&a, &b, sizeof (a)); } +static inline +crt_pos_cmp (CRT_POS a, CRT_POS b) +{ + return memcmp (&a, &b, sizeof (a)); +} + #endif /* __CRT_H__ */ diff --git a/src/libsympathy.c b/src/libsympathy.c index e91d08a..7e6d78b 100644 --- a/src/libsympathy.c +++ b/src/libsympathy.c @@ -11,6 +11,9 @@ static char rcsid[] = /* * $Log$ + * Revision 1.15 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.14 2008/02/12 22:36:46 james * *** empty log message *** * @@ -58,21 +61,6 @@ static char rcsid[] = #include "project.h" -struct termios old = { 0 }; -static int had_winch = 0; - -static void -quit (int not) -{ - tcsetattr (0, TCSANOW, &old); - exit (1); -} - -static void -winch (int not) -{ - had_winch++; -} void @@ -80,36 +68,29 @@ testy (void) { struct termios raw = { 0 }; ANSI a = { 0 }; + Context c; + +#if 0 fd_set rfd; int fd; char c; + TTY *t; VT102 *v; History *h; int i; +#endif + - signal (SIGINT, quit); - { - struct sigaction sa = { 0 }; - - sa.sa_handler = winch; - sa.sa_flags = SA_RESTART; - sigaction (SIGWINCH, &sa, NULL); - } - + ansi_reset (&a); - tcgetattr (0, &old); - tcgetattr (0, &raw); - cfmakeraw (&raw); - tcsetattr (0, TCSANOW, &raw); - a.fd = 0; - ansi_reset (&a); + c.t = tty_new_test (); + c.v = vt102_new (); + a.terminal=terminal_open(0,1); - t = tty_new_test (); - v = vt102_new (); FD_ZERO (&rfd); for (;;) diff --git a/src/prototypes.h b/src/prototypes.h index 8468fb6..17027e1 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -1,7 +1,4 @@ /* ansi.c */ -int ansi_read(ANSI *a, void *buf, int n); -void ansi_write(ANSI *a, char *buf, int n); -void ansi_getsize(ANSI *a); void ansi_move(ANSI *a, CRT_Pos p); void ansi_showhide_cursor(ANSI *a, int hide); void ansi_force_attr_normal(ANSI *a); @@ -14,14 +11,14 @@ void ansi_spot_scroll_up(ANSI *a, CRT *c); void ansi_spot_scroll(ANSI *a, CRT *c); void ansi_draw(ANSI *a, CRT *c); void ansi_reset(ANSI *a); -void ansi_flush_escape(ANSI *a, VT102 *v, TTY *t); -void ansi_parse_deckey(ANSI *a, VT102 *v, TTY *t); -void ansi_parse_ansikey(ANSI *a, VT102 *v, TTY *t); -void ansi_parse_escape(ANSI *a, VT102 *v, TTY *t); -void ansi_check_escape(ANSI *a, VT102 *v, TTY *t); -void ansi_parse_char(ANSI *a, int c, VT102 *v, TTY *t); -void ansi_parse(ANSI *a, char *buf, int len, VT102 *v, TTY *t); -int ansi_dispatch(ANSI *a, VT102 *v, TTY *t); +void ansi_flush_escape(ANSI *a, Context *c); +void ansi_parse_deckey(ANSI *a, Context *c); +void ansi_parse_ansikey(ANSI *a, Context *c); +void ansi_parse_escape(ANSI *a, Context *c); +void ansi_check_escape(ANSI *a, Context *c); +void ansi_parse_char(ANSI *a, Context *c, int ch); +void ansi_parse(ANSI *a, Context *c, char *buf, int len); +int ansi_dispatch(ANSI *a, Context *c); /* crt.c */ void crt_erase(CRT *c, CRT_Pos s, CRT_Pos e, int ea); void crt_cls(CRT *c); @@ -83,5 +80,4 @@ int ring_read(Ring *r, void *b, int n); int ring_write(Ring *r, void *b, int n); Ring *ring_new(int n); /* ptty.c */ -void ptty_close(TTY *_t); TTY *ptty_open(char *path, char *argv[]); diff --git a/src/terminal.c b/src/terminal.c index a4cd31b..f0472a1 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.2 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.1 2008/02/12 22:36:46 james * *** empty log message *** * @@ -45,6 +48,8 @@ typedef struct TERMINAL_struct static TERMINAL terminal_list=NULL; +int terminal_winches; + static void @@ -71,6 +76,60 @@ terminal_close (TTY * _t) } +void terminal_atexit(void) +{ +while (terminal_list) + terminal_close(terminal_list); +} + +static void sigint(int dummy) +{ +terminal_atexit(); +exit(-1); +} + +static void +sigwinch (int not) +{ + terminal_winches++; +} + + +void +terminal_getsize (TTY *_t) +{ +TERMINAL *t=(TTY *) _t; + struct winsize sz = { 0 }; + +if (!t) return; + +if (ioctl (a->wfd, TIOCGWINSZ, &sz)) + { + t->size.x = CRT_COLS; + t->size.y = CRT_ROWS; + } + else + { + t->size.x = sz.ws_col; + t->size.y = sz.ws_row; + } +} + + +void terminal_dispatch(void) +{ +TERMINAL *t; + + +if (!terminal_winches) return; + +terminal_winches=0; + +for (t=terminal_list;t;t=t->next) +terminal_getsize(t); + +} + static int terminal_read (TTY * _t, void *buf, int len) @@ -78,6 +137,8 @@ terminal_read (TTY * _t, void *buf, int len) TERMINAL *t = (TERMINAL *) _t; int red, done = 0; + terminal_dispatch(); + do { @@ -104,6 +165,8 @@ terminal_write (TTY * _t, void *buf, int len) int writ, done = 0; TERMINAL *t = (TERMINAL *) _t; + terminal_dispatch(); + do { @@ -123,6 +186,20 @@ terminal_write (TTY * _t, void *buf, int len) return done; } + +void terminal_register_handlers(void) +{ + struct sigaction sa = { 0 }; + + sa.sa_handler = sigwinch; + sa.sa_flags = SA_RESTART; + sigaction (SIGWINCH, &sa, NULL); + + sa.sa_handler = sigint; + sa.sa_flags = SA_RESTART; + sigaction (SIGINT, &sa, NULL); +} + TTY * terminal_open (int rfd,int wfd) { @@ -156,34 +233,8 @@ terminal_open (int rfd,int wfd) t->close = terminal_close; - return (TTY *) t; -} + terminal_getsize((TTY*) t); -void -terminal_getsize (TTY *_t,CRT_POS *pos) -{ -TERMINAL *t=(TTY *) _t; - struct winsize sz = { 0 }; - -if ((!t) || (!pos)) return; - -if (ioctl (a->wfd, TIOCGWINSZ, &sz)) - { - pos->x = CRT_COLS; - pos->y = CRT_ROWS; - } - else - { - pos->x = sz.ws_col; - pos->y = sz.ws_row; - } -} - - -void terminal_atexit(void) -{ -while (terminal_list) - terminal_close(terminal_list); + return (TTY *) t; } - diff --git a/src/tty.h b/src/tty.h index 51634ed..76e2803 100644 --- a/src/tty.h +++ b/src/tty.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.4 2008/02/13 01:08:18 james + * *** empty log message *** + * * Revision 1.3 2008/02/09 15:47:28 james * *** empty log message *** * @@ -28,6 +31,7 @@ #define TTY_SIGNATURE \ char name[1024]; \ + CRT_POS size; \ void (*close)(struct TTY_struct *); \ int (*recv)(struct TTY_struct *,void *buf,int len); \ int (*xmit)(struct TTY_struct *,void *buf,int len); \ diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..e8b315e --- /dev/null +++ b/src/util.c @@ -0,0 +1,108 @@ +/* + * util.c: + * + * Copyright (c) 2008 James McKenzie , + * All rights reserved. + * + */ + +static char rcsid[] = "$Id$"; + +/* + * $Log$ + * Revision 1.1 2008/02/13 01:08:38 james + * *** empty log message *** + * + */ + +#include "project.h" + +int +wrap_read (int fd, void *buf, int len) +{ + int red; + + red = read (fd, buf, len); + if (!red) + return -1; + + if ((red < 0) && (errno == EAGAIN)) + red = 0; + + return red; +} + +int +wrap_write (int fd, void *buf, int len) +{ + int writ; + + writ = write (fd, buf, len); + if (!writ) + return -1; + + if ((writ < 0) && (errno == -EAGAIN)) + writ = 0; + + return writ; +} + + +void +set_nonblocking (int fd) +{ + long arg; + arg = fcntl (fd, F_GETFL, arg); + arg |= O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + +void +set_blocking (int fd) +{ + long arg; + arg = fcntl (fd, F_GETFL, arg); + arg &= ~O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + +void raw_termios(struct termios *termios) +{ + + termios->c_iflag = ICRNL | IXON; + termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0; + termios->c_lflag = + ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE; + + termios->c_cc[VINTR] = 003; + termios->c_cc[VQUIT] = 034; + termios->c_cc[VERASE] = 0177; + termios->c_cc[VKILL] = 025; + termios->c_cc[VEOF] = 004; + termios->c_cc[VEOL] = 0; + termios->c_cc[VEOL2] = 0; + termios->c_cc[VSTART] = 021; + termios->c_cc[VSTOP] = 023; + termios->c_cc[VSUSP] = 032; + termios->c_cc[VLNEXT] = 026; + termios->c_cc[VWERASE] = 027; + termios->c_cc[VREPRINT] = 022; + termios->c_cc[VDISCARD] = 017; + +} + +void +default_termios (struct termios *termios) +{ + + memset (termios, 0, sizeof (termios)); + + raw_termios(termios); + + termios->c_cflag = CS8 | CREAD | CLOCAL; + + cfsetispeed (termios, B9600); + cfsetospeed (termios, B9600); +} + +