h57693
s 00014/00002/00236
d D 1.17 90/06/08 09:29:05 opcode 17 16
c -r and -b command line args
e
s 00009/00005/00229
d D 1.16 89/08/21 15:12:23 opcode 16 15
c linted
e
s 00015/00006/00219
d D 1.15 89/08/14 15:25:18 opcode 15 14
c added setlinebuf for rs232_debugfile
e
s 00041/00037/00184
d D 1.14 89/08/14 10:49:07 chrisb 14 13
c changed arguements
e
s 00001/00002/00220
d D 1.13 89/08/10 10:35:50 opcode 13 12
c switch init order so that tty settings saved before raw mode set
e
s 00001/00000/00221
d D 1.12 89/08/08 10:31:01 chrisb 12 11
c set pktmode = 1
e
s 00000/00006/00221
d D 1.11 89/08/07 18:21:02 chrisb 11 10
c comments
e
s 00001/00001/00226
d D 1.10 89/08/01 11:15:45 opcode 10 9
c deleted line to logout on HUP
e
s 00000/00016/00227
d D 1.9 89/07/14 15:32:42 opcode 9 8
c removed open kludge to match server open file descriptors
e
s 00012/00003/00231
d D 1.8 89/07/12 10:47:01 opcode 8 7
c initialize version, check return on rs232init()
e
s 00011/00002/00223
d D 1.7 89/07/05 11:00:57 opcode 7 6
c added pktdump
e
s 00037/00017/00188
d D 1.6 89/03/29 11:30:48 opcode 6 5
c deals with SIGHUP
e
s 00135/00004/00070
d D 1.5 89/03/29 09:55:39 opcode 5 4
c puts WINDOW_TTYPARMS in env. for psterm
e
s 00007/00001/00067
d D 1.4 89/03/28 17:07:37 opcode 4 3
c ignore SIGPIPE and SIGFPE
e
s 00017/00024/00051
d D 1.3 89/01/26 14:52:59 chrisb 3 2
c added -i and brought up to 1.1 version
e
s 00004/00000/00071
d D 1.2 88/12/21 11:51:52 chrisb 2 1
c added sccs keyword ids
e
s 00071/00000/00000
d D 1.1 88/12/21 11:28:17 chrisb 1 0
c date and time created 88/12/21 11:28:17 by chrisb
e
u
U
f e 0
t
T
I 2
#ifndef	lint
static char sccsid[] = "%Z%%M% %I% %E%";
#endif

I 15
/*
 * Copyright (c) 1989 by Sun Microsystems, Inc.
 */

E 15
E 2
I 1
#include "cbratp.h"
D 16
#include <sys/time.h>
E 16
I 3
#include <fcntl.h>
I 4
#include <signal.h>
I 5
#include <stdio.h>
#include <sgtty.h>
#include <sys/time.h>
I 16
#include <sys/resource.h>
#include <sys/time.h>
E 16
#include <sys/file.h>
#include <sys/wait.h>
E 5
E 4
E 3

I 8
#define	HELPER_VERSION	1
E 8
I 5
int news_pid;

D 8

E 8
E 5
usage()
{
D 6
    (void) fprintf(stderr, "usage: %s [-tty#] [-d]\n", myname);
E 6
I 6
D 7
    (void) fprintf(stderr, "usage: %s [-tty#] [-d] [-i] [-s]\n", myname);
E 7
I 7
D 14
    (void) fprintf(stderr, "usage: %s [-tty#] [-d] [-i] [-p|s]\n", myname);
E 7
    (void) fprintf(stderr, "       -tty#: set default tty\n");
    (void) fprintf(stderr, "       -d: debug mode\n");
E 14
I 14
    (void) fprintf(stderr, "usage: %s [-f ttydevice] ", myname);
I 17
    (void) fprintf(stderr, "[-b baudrate] ");
E 17
#ifdef	DEBUG
    (void) fprintf(stderr, "[-d debugfilename] ");
D 15
#endif	DEBUG
E 15
I 15
#endif	/*DEBUG*/
I 17
    (void) fprintf(stderr, "[-r] ");
E 17
E 15
    (void) fprintf(stderr, "[-s statfilename] [-i]\n");
I 17
    (void) fprintf(stderr, "       -b: set baudrate\n");
E 17
    (void) fprintf(stderr, "       -f: set tty device\n");
#ifdef	DEBUG
    (void) fprintf(stderr, "       -d: debug mode, output to file\n");
D 15
#endif	DEBUG
E 15
I 15
#endif	/*DEBUG*/
I 17
    (void) fprintf(stderr, "       -r: set RTS/CTS flow control\n");
E 17
E 15
    (void) fprintf(stderr, "       -s: generate stats, output to file\n");
E 14
    (void) fprintf(stderr, "       -i: immediate startup\n");
I 7
D 14
    (void) fprintf(stderr, "       -p: generate pkt dump file\n");
E 7
    (void) fprintf(stderr, "       -s: generate stats file\n");
E 14
E 6
    exit(1);
}

I 6

/* This routine interferes with load in dload.c.  Load currently blocks
   SIGCHLD this interaction may need to be fixed someday.
 */
static void
reaper()
{
    union wait  status;
D 16
    register    pid;
E 16
I 16
#ifdef  DEBUG
    int pid;
E 16

    while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0) {
D 16
#ifdef  DEBUG
E 16
D 15
        (void) fprintf(stderr, "Reaped %d, rv=0%o\n", pid, status.w_retcode);
#endif  DEBUG
E 15
I 15
        (void) fprintf(rs232_debugfile, "Reaped %d, retcode=0%o\n",
						pid, status.w_retcode);
D 16
#endif	/*DEBUG*/
E 16
E 15
    }
I 16
#else
    while (wait3(&status, WNOHANG, (struct rusage *) 0) > 0)
	;
#endif	/*DEBUG*/
E 16
}

static void
sighup_handler()
{
    register int i;
    
    for (i=getdtablesize()-1; i>0; i--)
	(void) close(i);
    sleep(3);
    reaper();
D 10
    lseek(0, -16L, 0);	/* will logout */
E 10
I 10
    /*lseek(0, -16L, 0);	/* will logout */
E 10
    exit(0);
}

E 6
main(argc, argv)
    int	argc;
    char **argv;
{
    int i;
I 3
    int iflag = 0;
E 3
D 14
    char tty[20];
I 7
    extern FILE *pktdump;
E 14
I 14
    char *tty = "/dev/tty";
I 17
    int baudrate = 0;
    int rtscts = 0;
E 17
    extern char	*optarg;
    extern int	optind;
I 15
    extern char	*strrchr();
E 15
E 14
E 7

D 14
    myname = *argv;
    strcpy(tty, "/dev/tty");
    for (i = 1; i < argc; i++) {
	if (!strncmp(argv[i], "-tty", 4)) {
	    strcat(tty, argv[i] + 4);
	}
	else if (!strncmp(argv[i], "-d", 2)) {
	    rs232_debug = 1;
	}
I 3
	else if (!strncmp(argv[i], "-i", 2)) {
	    iflag = 1;
	}
	else if (!strncmp(argv[i], "-s", 2)) {
I 7
	    if (rs232_stats != NULL || pktdump != NULL) 
		usage(); 
E 7
	    rs232_stats = fopen("statfile", "w");
	}
I 7
	else if (!strncmp(argv[i], "-p", 2)) {
	    if (rs232_stats != NULL || pktdump != NULL)
		usage();
	    pktdump = fopen("pktdump", "w");
	}
E 7
E 3
	else {
E 14
I 14
    if ((myname = strrchr(*argv, '/')) == NULL)
	myname = *argv;
    else
	myname++;
#ifdef	DEBUG
    rs232_debugfile = stdout;
D 15
#endif	DEBUG
E 15
I 15
#endif	/*DEBUG*/
E 15
D 17
    while ((i = getopt(argc, argv, "d:f:s:i")) != -1)
E 17
I 17
    while ((i = getopt(argc, argv, "b:d:f:s:ir")) != -1)
E 17
	switch (i) {
I 17
	case 'b':
	    baudrate = atoi(optarg);
	    break;
	case 'r':
	    rtscts = 1;
	    break;
E 17
	case 'i':
	    iflag++;
	    break;
	case 's':
	    rs232_stats = fopen(optarg, "w");
	    break;
#ifdef	DEBUG
	case 'd':
	    if (rs232_debugfile = fopen(optarg, "w"))
		rs232_debug++;
	    break;
D 15
#endif	DEBUG
E 15
I 15
#endif	/*DEBUG*/
E 15
	case 'f':
	    tty = optarg;
	    break;
	case '?':
E 14
	    usage();
	}
D 14
    }
E 14

I 15
#ifdef	DEBUG
    (void) setlinebuf(rs232_debugfile);
#endif	/*DEBUG*/
E 15
I 13
    (void) initialize_helper(tty);
E 13
I 5
D 8
    initialize_helper(tty);
E 5
    rs232init(tty);
E 8
I 8
D 17
    if (!rs232init(tty)) {
E 17
I 17
    if (!rs232init(tty, baudrate, rtscts)) {
E 17
	(void) fprintf(stderr, "can't init %s\n", tty);
	exit(1);
    }
E 8
I 5
D 13

I 8
    (void) initialize_helper(tty);
E 13
D 9

E 8
    /*
     * Temporary? kludge.
     * On the 3/50 Nut we need to open file descriptors locally for
     * keyboard, mouse, framebuffer, and the rs232 line.  These turn
     * out to be 6, 4, 5, and 3, respectively.  In order not to
     * have conflicting fds locally and remotely on the Nut, we
     * will open files here until we get up to fd 6.  After that,
     * fds will match both locally and remotely.
     *
     * There's another one for rs232file now also. 5/4/88
     */
E 5
I 3
D 7
    while ((i = open("/dev/null", O_RDONLY)) < 8)
E 7
I 7
    while ((i = open("/dev/null", O_RDONLY)) < 9)
E 7
	;
E 3
D 5
    news_pid = getpid();
    (void) gethostname(my_hostname, sizeof my_hostname);
    my_hostname[sizeof my_hostname - 1] = 0;
E 5
I 5

E 9
I 8
    version.version = HELPER_VERSION;
D 9

E 9
E 8
E 5
    initpktmode();
    rs232rawemit(213);
    rs232flush();
I 8

E 8
D 3
    /*
     * Temporary? kludge.
     * On the 3/50 Nut we need to open file descriptors locally for
     * keyboard, mouse, framebuffer, and the rs232 line.  These turn
     * out to be 6, 4, 5, and 3, respectively.  In order not to
     * have conflicting fds locally and remotely on the Nut, we
     * will open files here until we get up to fd 6.  After that,
     * fds will match both locally and remotely.
     *
     * There's another one for rs232file now also. 5/4/88
     */
    for (;;) {
	int fd;

	fd = open("/dev/null", 0);
	if (fd >= 7)
	    break;
    }
    while ((i = rs232rawkey()) != 231) {
	if (i < 0)
	    rs232ready();
	else
	    printf("(%c)%x\n", i, i);
    }
E 3
I 3
D 4
    if (iflag == 0)
E 4
I 4
    if ((int) signal(SIGPIPE, SIG_IGN) < 0)
	perror("signal: SIGPIPE");
    if ((int) signal(SIGFPE, SIG_IGN) < 0)
	perror("signal: SIGFPE");
I 6
    if ((int) signal(SIGHUP, sighup_handler) < 0)
	perror("signal: SIGHUP");
I 8

E 8
E 6
    if (iflag == 0) {
E 4
D 14
	while ((i = rs232rawkey()) != 231) {
	    if (i < 0)
		rs232ready();
	    else
		printf("(%c)%x\n", i, i);
	}
E 14
I 14
	do {
D 16
	    rs232ready();
E 16
I 16
	    (void) rs232ready();
E 16
	} while ((i = rs232rawkey()) != 231);
E 14
I 4
    }
I 12
    pkt_mode = 1;
E 12
E 4
E 3
    for (;;)
	wakeup_io();
}

I 5
D 6

/* This routine interferes with load in dload.c.  Load currently blocks
   SIGCHLD this interaction may need to be fixed someday.
 */
static void
reaper()
{
    union wait  status;
    register    pid;

    while ((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0) {
#ifdef  DEBUG
        (void) fprintf(stderr, "Reaped %d, rv=0%o\n", pid, status.w_retcode);
#endif  DEBUG
    }
}
E 6


initialize_helper(tty)
    char *tty;
{
    int tty_fd;

    (void) signal (SIGCHLD, reaper);
    news_pid = getpid();
    (void) gethostname(my_hostname, sizeof my_hostname);
    my_hostname[sizeof my_hostname - 1] = 0;

    /* Setup tty parameters for all terminal emulators that will start */
    tty_fd = open(tty, O_RDWR, 0);
    if (tty_fd < 0)
        (void) ttysw_saveparms(2);       /* Try stderr */
    else {
        (void) ttysw_saveparms(tty_fd);
        (void) close(tty_fd);
    }
}


/* ttysw_saveparms and we_setptyparms have been copied from the suntool libaray: * "@(#)ttysw_stty.c 10.5 86/09/03 Copyr 1983 Sun Micro"; */
 
/*
 * Ttysw parameter setting mechanism using given tty settings.
 */
 
/*
 * Determine ttyfd tty settings and cache in environment.
 */
ttysw_saveparms(ttyfd)
    int         ttyfd;
{
    int         ldisc,
                localmodes;
    struct sgttyb mode;
    struct tchars tchars;
    struct ltchars ltchars;

    /*                      
     * Get line discipline.
     */
    (void) ioctl(ttyfd, TIOCGETD, &ldisc);
    /* 
     * Get tty parameters
     */  
    (void) ioctl(ttyfd, TIOCGETP, &mode);
    /*   
     * Get local modes
     */  
    (void) ioctl(ttyfd, TIOCLGET, &localmodes);
    /*
     * Get terminal characters
     */  
    (void) ioctl(ttyfd, TIOCGETC, &tchars);
    /*
     * Get local special characters
     */  
    (void) ioctl(ttyfd, TIOCGLTC, &ltchars);
    /*
     * Write environment variable
     */  
    (void) we_setptyparms(ldisc, localmodes, &mode, &tchars, &ltchars);
}

#define WE_TTYPARMS     "WINDOW_TTYPARMS"
#define WE_TTYPARMSLEN  120

/*
 * Save tty settings in environment.
 */
we_setptyparms(ldisc, localmodes, mode, tchars, ltchars)
    int         ldisc,
                localmodes;
    struct sgttyb *mode;
    struct tchars *tchars;
    struct ltchars *ltchars;
{
    char        str[WE_TTYPARMSLEN];

    str[0] = '\0';
    /*
     * %c cannot be used to write the character valued fields because they
     * often have a value of \0.
     */  
    (void) sprintf(str,
                "%D,%D,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
               ldisc, localmodes, mode->sg_ispeed, mode->sg_ospeed,
               mode->sg_erase, mode->sg_kill, mode->sg_flags,
               tchars->t_intrc, tchars->t_quitc, tchars->t_startc,
               tchars->t_stopc, tchars->t_eofc, tchars->t_brkc,
               ltchars->t_suspc, ltchars->t_dsuspc, ltchars->t_rprntc,
               ltchars->t_flushc, ltchars->t_werasc, ltchars->t_lnextc);
    (void) setenv(WE_TTYPARMS, str);
}
I 8
D 11

E 8
E 5
wakeup_io()
{
    reader_proc();
    writer_proc();
}
E 11
D 5

E 5
E 1
