[comp.os.minix] Terminal Program

jdeitch@pnet01.cts.com (Jim Deitch) (04/23/89)

A while back a terminal program for Minix came through the news group.  Has
anybody gotten it working, and if so, could you please mail it to me?

Thanks,
 Jim


UUCP: {nosc ucsd hplabs!hp-sdd}!crash!pnet01!jdeitch
ARPA: crash!pnet01!jdeitch@nosc.mil
INET: jdeitch@pnet01.cts.com

stailey@iris613.gsfc.nasa.gov (Ken Stailey) (04/23/89)

It was mine.  I through it together in an enormous hurry but the code it
somewhat structured.  I still use it because it's pretty fast with 1K buffers
but there must be a bug in my serial driver because the reading child proc
takes a dive after about 15 min of continuous operation.  Re-running it does
not help.  Mostly it was a responce to the binary only postings of "cu".

Here it is:

This is a much improved version of the fast terminal program posted on
Tue Mar 7.  Gone is quite a bit of junk that would never work right because
of handshaking problems using stdio on /dev/tty1.  In its place is a cu-like
command set and quite a bit more robustness.
						Ken

--------------- cut here ------------------------ cut here -----------------
echo x - makefile
sed "s/^X//" > makefile << '/'
Xall:	term write_tty read_tty
X
X
Xterm:	term.o err.o 
X	$(CC) -o term term.o err.o
X
Xwrite_tty:	write_tty.o do_shell.o
X	$(CC) -o write_tty write_tty.o do_shell.o
X
Xread_tty:	read_tty.o
X	$(CC) -o read_tty read_tty.o
X
Xwrite_tty.o:	write_tty.c const.h
X	$(CC) -c -O write_tty.c
X
Xread_tty.o:	read_tty.c const.h
X	$(CC) -c -O read_tty.c
X
Xterm.o:	term.c
X	$(CC) -c -O term.c
X
Xerr.o:	err.c
X	$(CC) -c -O err.c
X
Xdo_shell.o: do_shell.c
X	$(CC) -c -O do_shell.c
/
echo x - const.h
sed "s/^X//" > const.h << '/'
X#define TRUE 1
X#define FALSE 0
X
X#define ERR -1
/
echo x - do_shell.c
sed "s/^X//" > do_shell.c << '/'
Xdo_shell()
X{
X}
/
echo x - err.c
sed "s/^X//" > err.c << '/'
X#include <stdio.h>
X
Xextern int errno;
Xextern char *sys_errlist[];
Xextern char *i_am;
X
Xerr(s)
Xchar *s;
X{
X  fprintf(stderr, "%s failed to %s because %s (errno %d).\n",
X	i_am, s, sys_errlist[errno], errno);
X
X  exit(-1);
X}
/
echo x - read_tty.c
sed "s/^X//" > read_tty.c << '/'
X/* get chars from rs232 and display them */
X
X#include <stdio.h>
X#include <signal.h>
X#include "const.h"
X
Xcleanup()
X{
X  fprintf(stderr, "reader stopped\n");
X  fflush(stderr);
X
X  exit(0);
X}
X
X
Xchar err_buf[BUFSIZ];
X
Xmain()
X{
X  char chrs[1024];
X
X  signal(SIGINT,  cleanup);
X  signal(SIGQUIT, cleanup);
X  signal(SIGTERM, cleanup);
X
X  setbuf(stderr, err_buf);
X
X  fprintf(stderr, "reader started\n");
X  fflush(stderr);
X
X  while (TRUE) {
X	write(1, chrs, read(0, chrs, 10));
X  }
X}
/
echo x - term.c
sed "s/^X//" > term.c << '/'
X/* minix doesn't suport open(path, oflag, mode) so no NODELAY		*/
X
X/* my work around is to run term as 2 procs one that reads /dev/tty1	*/
X/* and one that writes to it.  Now if we block for reading it doesn't	*/
X/* matter.								*/
X
X#include <signal.h>
X#include <sgtty.h>
X#include <fcntl.h>
X#include <stdio.h>
X#include "const.h"
X
Xchar *i_am = "term"; /* name used in err() */
Xextern void err();   /* error printing routine */
X
Xint rd_pid, wr_pid;
X
Xcleanup()
X{
X  system("stty -raw echo");
X  system("stty -raw echo </dev/tty1");
X
X  kill(rd_pid, SIGTERM);
X  kill(wr_pid, SIGTERM);
X
X  exit(0);
X}
X
Xmain()
X{
X  int rd_pid, wr_pid;
X  int done, status;
X
X  signal(SIGINT, cleanup);
X  signal(SIGQUIT, cleanup);
X
X  system("stty raw -echo");
X  system("stty raw -echo </dev/tty1");
X
X/* launch writer process */
X  if (wr_pid = fork()) {
X	if (wr_pid == -1)
X		err("fork tty writer");
X  } else {
X	freopen("/dev/tty1", "w", stdout);
X	execl("write_tty", "write_tty", (char *)0);
X  }
X
X/* launch reader process */
X  if (rd_pid = fork()) {
X	if (rd_pid == -1)
X		err("fork tty reader");
X  } else {
X  	if (freopen("/dev/tty1", "r", stdin) == ERR)
X		err("open rs232 port for reading");
X
X  	if (ioctl(0, TIOCSTART, 0) == ERR)
X		err("reset /dev/tty1");
X
X	execl("read_tty", "read_tty", (char *)0);
X  }
X
X  done = wait(&status);
X  cleanup();
X}
X
/
echo x - write_tty.c
sed "s/^X//" > write_tty.c << '/'
X/* get chars from keyboard and put them to rs232 */
X
X#include <stdio.h>
X#include <fcntl.h>
X#include <signal.h>
X#include "const.h"
X
Xchar err_buf[BUFSIZ];
X
Xcleanup()
X{
X  fprintf(stderr, "writer stopped\n");
X  fflush(stderr);
X
X  exit(0);
X}
X
Xmain()
X{
X  char ch;
X  char was_newline = TRUE;
X
X  signal(SIGINT,  SIG_IGN);
X  signal(SIGQUIT, SIG_IGN);
X  signal(SIGTERM, cleanup);
X
X  setbuf(stderr, err_buf);
X
X  fprintf(stderr, "writer started\n");
X  fflush(stderr);
X
X  while (TRUE) {
X	read(0, &ch, 1);
X
X	if (was_newline) {
X		if (ch == '~') {
X			read(0, &ch, 1);
X
X			switch (ch) {
X
X			case '.':
X				cleanup();  /* go home! */
X
X			case '!':
X				do_shell();
X			break;
X
X			case '~':
X			break;
X
X			default:
X				fprintf(stderr, "invalid command--use\
X \"~~\" to start a line with \"~\"\n");
X			}
X		}
X	}			
X
X	if (ch == '\r')
X		was_newline = TRUE;
X	else
X		was_newline = FALSE;
X
X	write(1, &ch, 1);
X  }
X}
/
*========================================================================*
{  what opinions?!? what disclaimer !?!?!        ken@all.over.the.place  }
*========================================================================*