From 6928bcea1545df58c3cda2fecd0d90a27aeee9fe Mon Sep 17 00:00:00 2001 From: james <> Date: Thu, 7 Feb 2008 01:05:06 +0000 Subject: *** empty log message *** --- src/Makefile.am | 5 +- src/ansi.c | 5 ++ src/prototypes.h | 132 +++++++++++++++++----------------- src/testtty.c | 85 ---------------------- src/tty.c | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 289 insertions(+), 153 deletions(-) delete mode 100644 src/testtty.c create mode 100644 src/tty.c diff --git a/src/Makefile.am b/src/Makefile.am index a163fd0..6cfd221 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,9 @@ # $Id$ # # $Log$ +# Revision 1.4 2008/02/07 01:04:16 james +# *** empty log message *** +# # Revision 1.3 2008/02/07 00:39:13 james # *** empty log message *** # @@ -23,7 +26,7 @@ INCLUDES = -SRCS= ansi.c crt.c html.c libsympathy.c render.c testtty.c version.c vt102.c tty.c +SRCS= ansi.c crt.c html.c libsympathy.c render.c version.c vt102.c tty.c CPROTO=cproto SYMPATHYSRCS=${SRCS} diff --git a/src/ansi.c b/src/ansi.c index cf92e71..2154413 100644 --- a/src/ansi.c +++ b/src/ansi.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.10 2008/02/07 01:02:52 james + * *** empty log message *** + * * Revision 1.9 2008/02/07 00:43:27 james * *** empty log message *** * @@ -579,8 +582,10 @@ ansi_dispatch (ANSI * a, VT102 * v) if (red <= 0) return red; +#if 0 if (*buf == 3) return -1; +#endif ansi_parse (a, buf, red, v); diff --git a/src/prototypes.h b/src/prototypes.h index 9418624..c75ddbc 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -1,76 +1,74 @@ /* 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); -void ansi_set_attr (ANSI * a, int attr); -void ansi_render (ANSI * a, CRT_CA ca); -void ansi_cls (ANSI * a); -void ansi_draw (ANSI * a, CRT * c); -void ansi_reset (ANSI * a); -void ansi_flush_escape (ANSI * a, VT102 * v); -void ansi_parse_deckey (ANSI * a, VT102 * v); -void ansi_parse_ansikey (ANSI * a, VT102 * v); -void ansi_parse_escape (ANSI * a, VT102 * v); -void ansi_check_escape (ANSI * a, VT102 * v); -void ansi_parse_char (ANSI * a, int c, VT102 * v); -void ansi_parse (ANSI * a, char *buf, int len, VT102 * v); -int ansi_dispatch (ANSI * a, VT102 * v); +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); +void ansi_set_attr(ANSI *a, int attr); +void ansi_render(ANSI *a, CRT_CA ca); +void ansi_cls(ANSI *a); +void ansi_draw(ANSI *a, CRT *c); +void ansi_reset(ANSI *a); +void ansi_flush_escape(ANSI *a, VT102 *v); +void ansi_parse_deckey(ANSI *a, VT102 *v); +void ansi_parse_ansikey(ANSI *a, VT102 *v); +void ansi_parse_escape(ANSI *a, VT102 *v); +void ansi_check_escape(ANSI *a, VT102 *v); +void ansi_parse_char(ANSI *a, int c, VT102 *v); +void ansi_parse(ANSI *a, char *buf, int len, VT102 *v); +int ansi_dispatch(ANSI *a, VT102 *v); /* crt.c */ -void crt_erase (CRT * c, CRT_Pos s, CRT_Pos e, int ea); -void crt_cls (CRT * c); -void crt_scroll_up (CRT * c, CRT_Pos s, CRT_Pos e, int ea); -void crt_scroll_down (CRT * c, CRT_Pos s, CRT_Pos e, int ea); -void crt_reset (CRT * c); -void crt_insert (CRT * c, CRT_CA ca); +void crt_erase(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_cls(CRT *c); +void crt_scroll_up(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_scroll_down(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_reset(CRT *c); +void crt_insert(CRT *c, CRT_CA ca); /* html.c */ -void html_entity (FILE * f, int c); -void html_render (FILE * f, CRT_CA c); -void html_draw (FILE * f, CRT * c); +void html_entity(FILE *f, int c); +void html_render(FILE *f, CRT_CA c); +void html_draw(FILE *f, CRT *c); /* libsympathy.c */ struct termios old; -void testy (void); +void testy(void); /* render.c */ -/* testtty.c */ -int open_fd_to_bash (void); /* version.c */ /* vt102.c */ -void vt102_log_line (VT102 * v, int line); -void vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br); -void vt102_cursor_normalize (VT102 * v); -void vt102_cursor_carriage_return (VT102 * v); -void vt102_cursor_advance_line (VT102 * v); -void vt102_cursor_advance (VT102 * v); -void vt102_do_pending_wrap (VT102 * v); -void vt102_cursor_retard (VT102 * v); -void vt102_reset_tabs (VT102 * v); -void vt102_cursor_advance_tab (VT102 * v); -int vt102_cursor_home (VT102 * v); -int vt102_cursor_absolute (VT102 * v, int x, int y); -int vt102_cursor_relative (VT102 * v, int x, int y); -void vt102_delete_from_line (VT102 * v, CRT_Pos p); -void vt102_insert_into_line (VT102 * v, CRT_Pos p); -void vt102_change_mode (VT102 * v, int private, char *ns, int set); -void vt102_parse_mode_string (VT102 * v, char *buf, int len); -void vt102_change_attr (VT102 * v, char *na); -void vt102_parse_attr_string (VT102 * v, char *buf, int len); -void vt102_save_state (VT102 * v); -void vt102_restore_state (VT102 * v); -void vt102_parse_esc (VT102 * v, int c); -void vt102_parse_csi (VT102 * v, char *buf, int len); -void vt102_status_line (VT102 * v, char *str); -void vt102_parse_char (VT102 * v, int c); -int vt102_parse (VT102 * v, char *buf, int len); -void vt102_parser_reset (VT102_parser * p); -void vt102_send (VT102 * v, uint8_t key); -void vt102_reset (VT102 * v); -int vt102_dispatch (VT102 * v); -VT102 *vt102_new (TTY * t); -void vt102_free (VT102 * v); +void vt102_log_line(VT102 *v, int line); +void vt102_clip_cursor(VT102 *v, CRT_Pos tl, CRT_Pos br); +void vt102_cursor_normalize(VT102 *v); +void vt102_cursor_carriage_return(VT102 *v); +void vt102_cursor_advance_line(VT102 *v); +void vt102_cursor_advance(VT102 *v); +void vt102_do_pending_wrap(VT102 *v); +void vt102_cursor_retard(VT102 *v); +void vt102_reset_tabs(VT102 *v); +void vt102_cursor_advance_tab(VT102 *v); +int vt102_cursor_home(VT102 *v); +int vt102_cursor_absolute(VT102 *v, int x, int y); +int vt102_cursor_relative(VT102 *v, int x, int y); +void vt102_delete_from_line(VT102 *v, CRT_Pos p); +void vt102_insert_into_line(VT102 *v, CRT_Pos p); +void vt102_change_mode(VT102 *v, int private, char *ns, int set); +void vt102_parse_mode_string(VT102 *v, char *buf, int len); +void vt102_change_attr(VT102 *v, char *na); +void vt102_parse_attr_string(VT102 *v, char *buf, int len); +void vt102_save_state(VT102 *v); +void vt102_restore_state(VT102 *v); +void vt102_parse_esc(VT102 *v, int c); +void vt102_parse_csi(VT102 *v, char *buf, int len); +void vt102_status_line(VT102 *v, char *str); +void vt102_parse_char(VT102 *v, int c); +int vt102_parse(VT102 *v, char *buf, int len); +void vt102_parser_reset(VT102_parser *p); +void vt102_send(VT102 *v, uint8_t key); +void vt102_reset(VT102 *v); +int vt102_dispatch(VT102 *v); +VT102 *vt102_new(TTY *t); +void vt102_free(VT102 *v); /* tty.c */ -TTY *tty_new_test (void); -int tty_read (TTY * t, void *buf, int len); -int tty_write (TTY * t, void *buf, int len); -void tty_free (TTY * t); +TTY *tty_new_test(void); +int tty_read(TTY *t, void *buf, int len); +int tty_write(TTY *t, void *buf, int len); +void tty_free(TTY *t); diff --git a/src/testtty.c b/src/testtty.c deleted file mode 100644 index 11d3ac2..0000000 --- a/src/testtty.c +++ /dev/null @@ -1,85 +0,0 @@ -/* - * testtty.c: - * - * Copyright (c) 2008 James McKenzie , - * All rights reserved. - * - */ - -static char rcsid[] = "$Id$"; - -/* - * $Log$ - * Revision 1.3 2008/02/06 17:53:28 james - * *** empty log message *** - * - * Revision 1.2 2008/02/04 02:05:06 james - * *** empty log message *** - * - * Revision 1.1 2008/02/04 01:32:39 james - * *** empty log message *** - * - */ - -#include "project.h" - -static void -default_termios (struct termios *termios) -{ - - memset (termios, 0, sizeof (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; - - termios->c_cflag = CS8 | CREAD | CLOCAL; - - cfsetispeed (termios, B9600); - cfsetospeed (termios, B9600); -} - - -int -open_fd_to_bash (void) /*thump */ -{ - pid_t child; - int fd; - struct winsize winsize = { 0 }; - struct termios termios; - - default_termios (&termios); - - winsize.ws_row = VT102_ROWS; - winsize.ws_col = VT102_COLS; - - child = forkpty (&fd, NULL, &termios, &winsize); - - switch (child) - { - case -1: /*boo hiss */ - return -1; - case 0: /*waaah */ - setenv ("TERM", "vt102", 1); - execl ("/bin/sh", "-", (char *) 0); - _exit (-1); - } - - return fd; -} diff --git a/src/tty.c b/src/tty.c new file mode 100644 index 0000000..adee8bf --- /dev/null +++ b/src/tty.c @@ -0,0 +1,215 @@ +/* + * testtty.c: + * + * Copyright (c) 2008 James McKenzie , + * All rights reserved. + * + */ + +static char rcsid[] = "$Id$"; + +/* + * $Log$ + * Revision 1.1 2008/02/07 01:02:52 james + * *** empty log message *** + * + * Revision 1.3 2008/02/06 17:53:28 james + * *** empty log message *** + * + * Revision 1.2 2008/02/04 02:05:06 james + * *** empty log message *** + * + * Revision 1.1 2008/02/04 01:32:39 james + * *** empty log message *** + * + */ + +#include "project.h" + +static void +set_nonblocking (int fd) +{ + long arg; + arg = fcntl (fd, F_GETFL, arg); + arg |= O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + +static void +set_blocking (int fd) +{ + long arg; + arg = fcntl (fd, F_GETFL, arg); + arg &= ~O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + +static void +default_termios (struct termios *termios) +{ + + memset (termios, 0, sizeof (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; + + termios->c_cflag = CS8 | CREAD | CLOCAL; + + cfsetispeed (termios, B9600); + cfsetospeed (termios, B9600); +} + + +static int +open_fd_to_bash (void) /*thump */ +{ + pid_t child; + int fd; + struct winsize winsize = { 0 }; + struct termios termios; + + default_termios (&termios); + + winsize.ws_row = VT102_ROWS; + winsize.ws_col = VT102_COLS; + + child = forkpty (&fd, NULL, &termios, &winsize); + + switch (child) + { + case -1: /*boo hiss */ + return -1; + case 0: /*waaah */ + setenv ("TERM", "vt102", 1); + execl ("/bin/sh", "-", (char *) 0); + _exit (-1); + } + + return fd; +} + + +TTY * +tty_new_test (void) +{ + TTY *t; + t = (TTY *) malloc (sizeof (TTY)); + t->fd = open_fd_to_bash (); + + set_nonblocking (t->fd); + + return t; +} + +static 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; +} + +static 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; +} + +int +tty_read (TTY * t, void *buf, int len) +{ + int red, done = 0; + + + do + { + + red = wrap_read (t->fd, buf, len); + if (red < 0) + return -1; + if (!red) + return done; + + buf += red; + len -= red; + done += red; + } + while (len); + + + return done; +} + + +int +tty_write (TTY * t, void *buf, int len) +{ + int writ, done = 0; + +#if 0 + { + int i; + uint8_t *p = buf; + for (i = 0; i < len; ++i) + fprintf (stderr, "vw: %03o %c\n", p[i], p[i] > 31 ? p[i] : 32); + } +#endif + + do + { + + writ = wrap_write (t->fd, buf, len); + if (writ < 0) + return -1; + if (!writ) + sleep (1); + + buf += writ; + len -= writ; + done += writ; + } + while (len); + + + return done; +} + +void +tty_free (TTY * t) +{ + close (t->fd); + free (t); +} -- cgit v1.2.3