vuser - a virtual user program

Jeff Lawhorn jeffl at berick.uucp
Sat Dec 30 06:17:12 AEST 1989


The attached shar contains the source to the vuser program.  It
is a program that records the user input, and the program
responses so that the session can be replayed at a future time.

I originally wrote it to test the uipc library for the unix pc.
I then used it to playback a fairly long interactive session
several times concurrently to see what kind of load the machine
could take.

Vuser is known to run on the UnixPC (with the uipc
library/driver) and on Sun 3s running SunOS 3.5 under both X and
Suntools. 

Vuser is covered by the GPL (see the file COPYING for details).

----- Cut Here -----
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	COPYING
#	Makefile
#	README
#	TODO
#	havefun.c
#	main.c
#	pty.c
#	slave.c
#	tty.c
# This archive created: Fri Dec 29 11:06:30 1989
export PATH; PATH=/bin:$PATH
echo shar: extracting "'COPYING'" '(12488 characters)'
if test -f 'COPYING'
then
	echo shar: will not over-write existing file "'COPYING'"
else
sed 's/^X//' << \SHAR_EOF > 'COPYING'
X
X		    GNU GENERAL PUBLIC LICENSE
X		     Version 1, February 1989
X
X Copyright (C) 1989 Free Software Foundation, Inc.
X                    675 Mass Ave, Cambridge, MA 02139, USA
X Everyone is permitted to copy and distribute verbatim copies
X of this license document, but changing it is not allowed.
X
X			    Preamble
X
X  The license agreements of most software companies try to keep users
Xat the mercy of those companies.  By contrast, our General Public
XLicense is intended to guarantee your freedom to share and change free
Xsoftware--to make sure the software is free for all its users.  The
XGeneral Public License applies to the Free Software Foundation's
Xsoftware and to any other program whose authors commit to using it.
XYou can use it for your programs, too.
X
X  When we speak of free software, we are referring to freedom, not
Xprice.  Specifically, the General Public License is designed to make
Xsure that you have the freedom to give away or sell copies of free
Xsoftware, that you receive source code or can get it if you want it,
Xthat you can change the software or use pieces of it in new free
Xprograms; and that you know you can do these things.
X
X  To protect your rights, we need to make restrictions that forbid
Xanyone to deny you these rights or to ask you to surrender the rights.
XThese restrictions translate to certain responsibilities for you if you
Xdistribute copies of the software, or if you modify it.
X
X  For example, if you distribute copies of a such a program, whether
Xgratis or for a fee, you must give the recipients all the rights that
Xyou have.  You must make sure that they, too, receive or can get the
Xsource code.  And you must tell them their rights.
X
X  We protect your rights with two steps: (1) copyright the software, and
X(2) offer you this license which gives you legal permission to copy,
Xdistribute and/or modify the software.
X
X  Also, for each author's protection and ours, we want to make certain
Xthat everyone understands that there is no warranty for this free
Xsoftware.  If the software is modified by someone else and passed on, we
Xwant its recipients to know that what they have is not the original, so
Xthat any problems introduced by others will not reflect on the original
Xauthors' reputations.
X
X  The precise terms and conditions for copying, distribution and
Xmodification follow.
X
X		    GNU GENERAL PUBLIC LICENSE
X   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
X
X  0. This License Agreement applies to any program or other work which
Xcontains a notice placed by the copyright holder saying it may be
Xdistributed under the terms of this General Public License.  The
X"Program", below, refers to any such program or work, and a "work based
Xon the Program" means either the Program or any work containing the
XProgram or a portion of it, either verbatim or with modifications.  Each
Xlicensee is addressed as "you".
X
X  1. You may copy and distribute verbatim copies of the Program's source
Xcode as you receive it, in any medium, provided that you conspicuously and
Xappropriately publish on each copy an appropriate copyright notice and
Xdisclaimer of warranty; keep intact all the notices that refer to this
XGeneral Public License and to the absence of any warranty; and give any
Xother recipients of the Program a copy of this General Public License
Xalong with the Program.  You may charge a fee for the physical act of
Xtransferring a copy.
X
X  2. You may modify your copy or copies of the Program or any portion of
Xit, and copy and distribute such modifications under the terms of Paragraph
X1 above, provided that you also do the following:
X
X    a) cause the modified files to carry prominent notices stating that
X    you changed the files and the date of any change; and
X
X    b) cause the whole of any work that you distribute or publish, that
X    in whole or in part contains the Program or any part thereof, either
X    with or without modifications, to be licensed at no charge to all
X    third parties under the terms of this General Public License (except
X    that you may choose to grant warranty protection to some or all
X    third parties, at your option).
X
X    c) If the modified program normally reads commands interactively when
X    run, you must cause it, when started running for such interactive use
X    in the simplest and most usual way, to print or display an
X    announcement including an appropriate copyright notice and a notice
X    that there is no warranty (or else, saying that you provide a
X    warranty) and that users may redistribute the program under these
X    conditions, and telling the user how to view a copy of this General
X    Public License.
X
X    d) You may charge a fee for the physical act of transferring a
X    copy, and you may at your option offer warranty protection in
X    exchange for a fee.
X
XMere aggregation of another independent work with the Program (or its
Xderivative) on a volume of a storage or distribution medium does not bring
Xthe other work under the scope of these terms.
X
X  3. You may copy and distribute the Program (or a portion or derivative of
Xit, under Paragraph 2) in object code or executable form under the terms of
XParagraphs 1 and 2 above provided that you also do one of the following:
X
X    a) accompany it with the complete corresponding machine-readable
X    source code, which must be distributed under the terms of
X    Paragraphs 1 and 2 above; or,
X
X    b) accompany it with a written offer, valid for at least three
X    years, to give any third party free (except for a nominal charge
X    for the cost of distribution) a complete machine-readable copy of the
X    corresponding source code, to be distributed under the terms of
X    Paragraphs 1 and 2 above; or,
X
X    c) accompany it with the information you received as to where the
X    corresponding source code may be obtained.  (This alternative is
X    allowed only for noncommercial distribution and only if you
X    received the program in object code or executable form alone.)
X
XSource code for a work means the preferred form of the work for making
Xmodifications to it.  For an executable file, complete source code means
Xall the source code for all modules it contains; but, as a special
Xexception, it need not include source code for modules which are standard
Xlibraries that accompany the operating system on which the executable
Xfile runs, or for standard header files or definitions files that
Xaccompany that operating system.
X
X  4. You may not copy, modify, sublicense, distribute or transfer the
XProgram except as expressly provided under this General Public License.
XAny attempt otherwise to copy, modify, sublicense, distribute or transfer
Xthe Program is void, and will automatically terminate your rights to use
Xthe Program under this License.  However, parties who have received
Xcopies, or rights to use copies, from you under this General Public
XLicense will not have their licenses terminated so long as such parties
Xremain in full compliance.
X
X  5. By copying, distributing or modifying the Program (or any work based
Xon the Program) you indicate your acceptance of this license to do so,
Xand all its terms and conditions.
X
X  6. Each time you redistribute the Program (or any work based on the
XProgram), the recipient automatically receives a license from the original
Xlicensor to copy, distribute or modify the Program subject to these
Xterms and conditions.  You may not impose any further restrictions on the
Xrecipients' exercise of the rights granted herein.
X
X  7. The Free Software Foundation may publish revised and/or new versions
Xof the General Public License from time to time.  Such new versions will
Xbe similar in spirit to the present version, but may differ in detail to
Xaddress new problems or concerns.
X
XEach version is given a distinguishing version number.  If the Program
Xspecifies a version number of the license which applies to it and "any
Xlater version", you have the option of following the terms and conditions
Xeither of that version or of any later version published by the Free
XSoftware Foundation.  If the Program does not specify a version number of
Xthe license, you may choose any version ever published by the Free Software
XFoundation.
X
X  8. If you wish to incorporate parts of the Program into other free
Xprograms whose distribution conditions are different, write to the author
Xto ask for permission.  For software which is copyrighted by the Free
XSoftware Foundation, write to the Free Software Foundation; we sometimes
Xmake exceptions for this.  Our decision will be guided by the two goals
Xof preserving the free status of all derivatives of our free software and
Xof promoting the sharing and reuse of software generally.
X
X			    NO WARRANTY
X
X  9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
XOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
XPROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
XOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
XMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
XTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
XPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
XREPAIR OR CORRECTION.
X
X  10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
XWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
XREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
XINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
XOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
XTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
XYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
XPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
XPOSSIBILITY OF SUCH DAMAGES.
X
X		     END OF TERMS AND CONDITIONS
X
X	Appendix: How to Apply These Terms to Your New Programs
X
X  If you develop a new program, and you want it to be of the greatest
Xpossible use to humanity, the best way to achieve this is to make it
Xfree software which everyone can redistribute and change under these
Xterms.
X
X  To do so, attach the following notices to the program.  It is safest to
Xattach them to the start of each source file to most effectively convey
Xthe exclusion of warranty; and each file should have at least the
X"copyright" line and a pointer to where the full notice is found.
X
X    <one line to give the program's name and a brief idea of what it does.>
X    Copyright (C) 19yy  <name of author>
X
X    This program is free software; you can redistribute it and/or modify
X    it under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your option)
X    any later version.
X
X    This program is distributed in the hope that it will be useful,
X    but WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X    GNU General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with this program; if not, write to the Free Software
X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
X
XAlso add information on how to contact you by electronic and paper mail.
X
XIf the program is interactive, make it output a short notice like this
Xwhen it starts in an interactive mode:
X
X    Gnomovision version 69, Copyright (C) 19xx name of author
X    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
X    This is free software, and you are welcome to redistribute it
X    under certain conditions; type `show c' for details.
X
XThe hypothetical commands `show w' and `show c' should show the
Xappropriate parts of the General Public License.  Of course, the
Xcommands you use may be called something other than `show w' and `show
Xc'; they could even be mouse-clicks or menu items--whatever suits your
Xprogram.
X
XYou should also get your employer (if you work as a programmer) or your
Xschool, if any, to sign a "copyright disclaimer" for the program, if
Xnecessary.  Here a sample; alter the names:
X
X  Yoyodyne, Inc., hereby disclaims all copyright interest in the
X  program `Gnomovision' (a program to direct compilers to make passes
X  at assemblers) written by James Hacker.
X
X  <signature of Ty Coon>, 1 April 1989
X  Ty Coon, President of Vice
X
XThat's all there is to it!
SHAR_EOF
if test 12488 -ne "`wc -c < 'COPYING'`"
then
	echo shar: error transmitting "'COPYING'" '(should have been 12488 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'Makefile'" '(1405 characters)'
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile'
X# Makefile - This file contains the make rules for the vuser program.
X#
X#    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X#
X#    This file is part of vuser.
X#
X#    vuser is free software; you can redistribute it and/or modify it
X#    under the terms of the GNU General Public License as published by
X#    the Free Software Foundation; either version 1, or (at your
X#    option) any later version.
X#
X#    vuser is distributed in the hope that it will be useful, but
X#    WITHOUT ANY WARRANTY; without even the implied warranty of
X#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X#    General Public License for more details.
X#
X#    You should have received a copy of the GNU General Public License
X#    along with GNU CC; see the file COPYING.  If not, write to the
X#    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X
X# $Id: Makefile,v 1.1 89/12/15 21:31:56 jeffl Exp $
X
X#
X# Define SYSV when compiling on System V.
X# Define TIMEOUT_IS_LONG if your timeout value in select is a long
X#    rather than a struct timeval.
X
X# CFLAGS = -O -DSYSV -DTIMEOUT_IS_LONG
XCFLAGS = -g
X
X# some of us use smart C compilers.
XCC=gcc
X
X# add any machine specific libraries you need to link to get select.
X# LIBS = -luipc
XLIBS =
X
XCFILES = main.c pty.c tty.c slave.c havefun.c
XOFILES = main.o pty.o tty.o slave.o havefun.o
X
Xvuser: $(OFILES)
X	$(CC) -o vuser $(OFILES) $(LIBS)
SHAR_EOF
if test 1405 -ne "`wc -c < 'Makefile'`"
then
	echo shar: error transmitting "'Makefile'" '(should have been 1405 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'README'" '(1942 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
sed 's/^X//' << \SHAR_EOF > 'README'
X
X	The vuser program is intended to be used to replay
Xinteractive sessions.  It does this by recording the users key
Xstrokes and the systems responses, and then replaying the
Xsequences at a later time.
X
Xvuser understands the following options:
X
X	-p num	Set the recorded prompt length to num characters.
X		Default is 10.
X
X	-t num	Send the non-interactive timeout to be num seconds.
X		If the expected prompt is not seen in that
X		amount of time, vuser will procced as if the
X		prompt had been seen.  Setting the timeout to 0
X		will cause vuser to wait forever for a prompt.
X		Default is 30 seconds.
X
X	-s name	Generate a script file to the file named name for
X		future playback.  The default is not to generate
X		a script file.
X
X	-r name Record all characters sent to the standard output
X		in the file named name.  The default is not to
X		record the characters sent to the standard output.
X
X	-e	Use the rest of the command line as the command 
X		to run rather than the program specified by the
X		environment variable SHELL.  If SHELL is not set,
X		then the default is /bin/sh.
X
X	-l user host	Shorthand for ``-e rlogin host -l user''.
X
XIf either -e or -l is specified, it must be the last option on
Xthe command line.
X
X	To replay a session at a later time, run vuser with the
Xsame command line options as for the interactive session, but do
Xnot specify -s, and redirect the input from the script file that
Xwas generated during the interactive session.
X
X
XExamples:
X
XTo run vuser interactively and generate a script file for later
Xreplaying, you would use a command similar to:
X
X	vuser -s script.in -e bash
X
XThis will run the program bash, recording what the user types,
Xand what the program sends back.  When bash terminates, vuser
Xwill also terminate.  To replay the session at a later time you
Xwould use the command:
X
X	vuser -e bash <script.in
X
X
XComments/Sugestions/Enhancements should be sent to jeffl at berick.uucp
Xor ...!ucsd!sdsu!berick!jeffl.
X
SHAR_EOF
if test 1942 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 1942 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'TODO'" '(353 characters)'
if test -f 'TODO'
then
	echo shar: will not over-write existing file "'TODO'"
else
sed 's/^X//' << \SHAR_EOF > 'TODO'
XThings to be done to vuser:
X
X	Fix to work properly with csh.  It currently only works
X	with bash, sh, and ksh.  I do not use csh so this has
X	been a low priority.
X
X	Improve the signal handling.  It is currently very poor.
X
X	Write reasonable documentation including a man page and a
X	texinfo file.
X
X	Clean up the code that handles the escape characters.
SHAR_EOF
if test 353 -ne "`wc -c < 'TODO'`"
then
	echo shar: error transmitting "'TODO'" '(should have been 353 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'havefun.c'" '(7960 characters)'
if test -f 'havefun.c'
then
	echo shar: will not over-write existing file "'havefun.c'"
else
sed 's/^X//' << \SHAR_EOF > 'havefun.c'
X/* havefun.c - This module contains the code to do the main i/o handling.
X
X    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X
X    This file is part of vuser.
X
X    vuser is free software; you can redistribute it and/or modify it
X    under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your
X    option) any later version.
X
X    vuser is distributed in the hope that it will be useful, but
X    WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X    General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with GNU CC; see the file COPYING.  If not, write to the
X    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X*/
X
X#if !defined(lint)
Xstatic char SCCSid[] = "$Id: havefun.c,v 1.1 89/12/15 21:28:52 jeffl Exp $";
X#endif
X
X#include <stdio.h>
X#include <ctype.h>
X#include <signal.h>
X
X#if defined(__STDC__)
Xextern void KillSlave();
Xextern int DeadSlave();
Xvoid Send2MasterPty(char *, int);
Xvoid Send2stdout(char *, int);
X#else
Xextern void KillSlave();
Xextern int DeadSlave();
Xvoid Send2MasterPty();
Xvoid Send2stdout();
X#endif
X
Xextern FILE *ScriptFile;
Xextern FILE *RecordFile;
Xextern int MasterPty;
Xextern int Interactive;
X
Xchar PromptBuf[4096];
Xchar InComingPrompt[8192];
Xchar PromptLookedFor[4096];
Xint PromptSize = 10;
Xint PromptCount = 0;
Xint ICPCount = 0;
Xint ICPLength = 0;
Xchar *CurrPromptPos = PromptBuf - 1;
Xchar EscapeChar = '@';
X#if defined(TIMEOUT_IS_LONG)
Xlong TimeOut = 30;
X#else
X#include <sys/time.h>
Xstruct timeval TimeOut = {30, 0};
X#endif
X
Xvoid Send2ScriptFile(curr, start, stop, size)
Xchar *curr, *start, *stop;
Xint size;
X{
X    if(curr > stop)
X	curr = start + 1;
X    while(size--) {
X	if(isprint(*curr) && *curr != '\\')
X	    putc(*curr, ScriptFile);
X	else
X	    switch(*curr) {
X		case '\n': fputs("\\n", ScriptFile); break;
X		case '\r': fputs("\\r", ScriptFile); break;
X		case '\t': fputs("\\t", ScriptFile); break;
X		case '\f': fputs("\\f", ScriptFile); break;
X		case '\\': fputs("\\\\", ScriptFile); break;
X		default: fprintf(ScriptFile, "\\0%03o", *curr); break;
X	    }
X	if(++curr > stop)
X	    curr = start;
X    }
X    putc('\n', ScriptFile);
X    return;
X}
X
Xint GetOneChar()
X{
X    char chr = 0;
X
X    if(Interactive) {
X	write(1, "@", 1);
X	read(0, &chr, 1);
X	if(chr != EscapeChar)
X	    write(1, &chr, 1);
X	return((int)chr);
X    }
X    return(getchar());
X}
X
Xint DoEscape(inBuf, cnt)
Xchar *inBuf;
Xint cnt;
X{
X    int gotChar = -1;
X    char buf[4096];
X
X    if(!cnt)
X	gotChar = GetOneChar();
X    else {
X	gotChar = *inBuf++;
X	--cnt;
X    }
X    if(gotChar == EscapeChar) {
X	if(ScriptFile) {
X	    fseek(ScriptFile, -1, 1);
X	    putc(EscapeChar, ScriptFile);
X	    putc('\n', ScriptFile);
X	}
X	if(!Interactive)
X	    write(1, &gotChar, 1);
X	return(0);
X    }
X    if(Interactive)
X	Tty2ShellMode();
X    if(gotChar == '!') {
X	if(cnt)
X	    strcpy(buf, inBuf);
X	else
X	    gets(buf);
X	if(ScriptFile) {
X	    fseek(ScriptFile, -1, 1);
X	    putc('!', ScriptFile);
X	    fputs(buf, ScriptFile);
X	}
X	if(!Interactive) {
X	    write(1, "@!", 2);
X	    write(1, buf, strlen(buf));
X	    write(1, "\n", 1);
X	}
X	signal(SIGCLD, SIG_DFL);
X	system(buf);
X	signal(SIGCLD, DeadSlave);
X    } else if(gotChar == 't' || gotChar == 'T') {
X	gets(buf);
X	if(ScriptFile) {
X	    fseek(ScriptFile, -1, 1);
X	    putc('t', ScriptFile);
X	    fputs(buf, ScriptFile);
X	}
X	if(!Interactive) {
X	    write(1, "@t", 2);
X	    write(1, buf, strlen(buf));
X	    write(1, "\n", 1);
X	}
X#if defined(TIMEOUT_IS_LONG)
X	TimeOut = atol(buf);
X#else
X	TimeOut.tv_sec = atol(buf);
X#endif
X    } else if(gotChar == 'q' || gotChar == 'Q') {
X	gets(buf);
X	if(ScriptFile) {
X	    fseek(ScriptFile, -1, 1);
X	    putc('q', ScriptFile);
X	    fputs(buf, ScriptFile);
X	}
X	if(!Interactive) {
X	    write(1, "@q", 2);
X	    write(1, buf, strlen(buf));
X	    write(1, "\n", 1);
X	}
X	PromptSize = atoi(buf);
X    }
X    Tty2ProgramMode();
X    return(1);
X}
X
Xvoid SendResponse();
X
Xvoid GetNextPrompt()
X{
X    int chr = EOF;
X
X    ICPLength = 0;
X    while((chr = getchar()) != EOF) {
X	switch(chr) {
X	    case '\n': {
X		if(!ICPLength)
X		    SendResponse();
X		return;
X	    }
X	    case '\\': {
X		switch(chr = getchar()) {
X		    case '0': {
X			chr = ((getchar() - '0') * 64);
X			chr += ((getchar() - '0') * 8);
X			chr += (getchar() - '0');
X			break;
X		    }
X		    case 'n': chr = '\n'; break;
X		    case 'r': chr = '\r'; break;
X		    case 't': chr = '\t'; break;
X		    case 'f': chr = '\f'; break;
X		    case '\\': break;
X		}
X		break;
X	    }
X	}
X	PromptLookedFor[ICPLength++] = chr;
X    }
X    return;
X}
X
Xvoid SendResponse()
X{
X    int chr = EOF;
X    int sendBufCount = 0;
X    char sendBuf[4096];
X
X    while((chr = getchar()) != EOF) {
X	switch(chr) {
X	    case '\n': {
X		Send2MasterPty(sendBuf, sendBufCount);
X		GetNextPrompt();
X		return;
X	    }
X	    case '\\': {
X		switch(chr = getchar()) {
X		    case '0': {
X			chr = ((getchar() - '0') * 64);
X			chr += ((getchar() - '0') * 8);
X			chr += (getchar() - '0');
X			break;
X		    }
X		    case 'n': chr = '\n'; break;
X		    case 'r': chr = '\r'; break;
X		    case 't': chr = '\t'; break;
X		    case 'f': chr = '\f'; break;
X		    case '\\': break;
X		}
X		break;
X	    }
X	}
X	if(chr == EscapeChar)
X	    if(DoEscape(NULL, 0))
X		continue;
X	sendBuf[sendBufCount++] = chr;
X    }
X    return;
X}
X
Xint PromptCompare(ptr1, ptr2, cnt)
Xchar *ptr1, *ptr2;
Xint cnt;
X{
X    while(cnt-- && *ptr1++ == *ptr2++);
X    if(!cnt)
X	return(0);
X    return(*--ptr1 - *--ptr2);
X}
X
Xvoid DoPromptStuff(buf, cnt)
Xchar *buf;
Xint cnt;
X{
X    while(cnt--) {
X	InComingPrompt[ICPCount++] = *buf++;
X	if(ICPCount >= ICPLength) {
X	    if(ICPCount > 8192) {
X		memcpy(InComingPrompt, InComingPrompt + ICPCount - ICPLength,
X		       ICPLength);
X		ICPCount = ICPLength;
X	    }
X	    if(!PromptCompare(InComingPrompt + ICPCount - ICPLength,
X			      PromptLookedFor, ICPLength)) {
X		SendResponse();
X		ICPCount = 0;
X		continue;
X	    }
X	}
X    }
X    return;
X}
X
Xvoid Send2stdout(buf, cnt)
Xchar *buf;
Xint cnt;
X{
X    int savedCnt = 0;
X    
X    if(ScriptFile)
X	for(; savedCnt < cnt; ++savedCnt) {
X	    if(++CurrPromptPos > PromptBuf + PromptSize)
X		CurrPromptPos = PromptBuf;
X	    *CurrPromptPos = *(buf + savedCnt);
X	    if(PromptCount++ >= PromptSize)
X		PromptCount = PromptSize;
X	}
X    write(1, buf, cnt);
X    if(!Interactive)
X	DoPromptStuff(buf, cnt);
X    if(RecordFile)
X	fwrite(buf, 1, cnt, RecordFile);
X    return;
X}
X
Xvoid Send2MasterPty(buf, cnt)
Xchar *buf;
Xint cnt;
X{
X    static int lastCharSent = -1;
X
X    if(ScriptFile) {
X	if(cnt == 1 && *buf != EscapeChar && PromptCount > 0
X	   && PromptBuf[PromptCount - 1] == lastCharSent) {
X	    fseek(ScriptFile, -1, 1);
X	    Send2ScriptFile(buf, buf, buf + 1, 1);
X	} else {
X	    Send2ScriptFile(PromptCount >= PromptSize ?
X			    CurrPromptPos+1:PromptBuf,
X			    PromptBuf, PromptBuf + PromptSize, PromptCount);
X	    CurrPromptPos = PromptBuf - 1;
X	    PromptCount = 0;
X	    Send2ScriptFile(buf, buf, buf + cnt, cnt);
X	}
X	lastCharSent = buf[cnt - 1];
X    }
X    if(Interactive) {
X	int pos = 0;
X	for(; pos < cnt; ++pos)
X	    if(buf[pos] == EscapeChar) {
X		write(MasterPty, buf, pos);
X		if(DoEscape(buf + pos + 1, cnt - pos - 1))
X		    return;
X	    }
X    }
X    write(MasterPty, buf, cnt);
X    return;
X}
X
Xvoid HaveFun()
X{
X    int readMask = 0, cnt = -1;
X    char buf[4096];
X
X    if(!Interactive)
X	GetNextPrompt();
X    while(1) {
X	readMask = (1 << MasterPty) | Interactive;
X	if(select(32, &readMask, (int *)NULL, (int *)NULL, &TimeOut) < 0) {
X	    perror("on select: ");
X	    KillSlave();
X	}
X	if(readMask & (1 << MasterPty)) {
X	    if((cnt = read(MasterPty, buf, sizeof(buf))) < 0) {
X		perror("read of master pty: ");
X		KillSlave();
X	    }
X	    Send2stdout(buf, cnt);
X	} else if(readMask & 1) {
X	    if((cnt = read(0, buf, sizeof(buf))) < 0) {
X		perror("read of stdin: ");
X		KillSlave();
X	    }
X	    Send2MasterPty(buf, cnt);
X	} else if(!Interactive)
X	    SendResponse();
X    }
X}
SHAR_EOF
if test 7960 -ne "`wc -c < 'havefun.c'`"
then
	echo shar: error transmitting "'havefun.c'" '(should have been 7960 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'main.c'" '(2512 characters)'
if test -f 'main.c'
then
	echo shar: will not over-write existing file "'main.c'"
else
sed 's/^X//' << \SHAR_EOF > 'main.c'
X/* main.c - This module contains the startup code for the vuser program.
X
X    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X
X    This file is part of vuser.
X
X    vuser is free software; you can redistribute it and/or modify it
X    under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your
X    option) any later version.
X
X    vuser is distributed in the hope that it will be useful, but
X    WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X    General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with GNU CC; see the file COPYING.  If not, write to the
X    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X*/
X
X#if !defined(lint)
Xstatic char SCCSid[] = "$Id: main.c,v 1.1 89/12/15 21:30:53 jeffl Exp $";
X#endif
X
X#include <stdio.h>
X#include <signal.h>
X
X#if defined(__STDC__)
Xextern void Tty2ProgramMode();
Xextern void GetPty();
Xextern void StartSlave();
Xextern void HaveFun();
X#else
Xextern void Tty2ProgramMode();
Xextern void GetPty();
Xextern void StartSlave();
Xextern void HaveFun();
X#endif
X
Xextern char *optarg;
Xextern int optind;
X
Xextern int PromptSize;
Xextern char **Program2Run;
Xextern char *RloginProgram[];
X
X#if defined(TIMEOUT_IS_LONG)
Xextern long TimeOut;
X#else
X#include <sys/time.h>
Xextern struct timeval TimeOut;
X#endif
X
XFILE *ScriptFile = (FILE *)NULL;
XFILE *RecordFile = (FILE *)NULL;
X
Xmain(argc, argv)
Xint argc;
Xchar *argv[];
X{
X    loop: switch(getopt(argc, argv, "s:p:t:e:l:r:")) {
X	case EOF: break;
X	case 's': {
X	    if(!(ScriptFile = fopen(optarg, "w"))) {
X		perror(optarg);
X		exit(1);
X	    }
X	    goto loop;
X	}
X	case 'p': {
X	    if((PromptSize = atoi(optarg)) > 4096) {
X		fprintf(stderr, "Prompt size is limited to 4096.\n");
X		PromptSize = 4096;
X	    }
X	    goto loop;
X	}
X#if defined(TIMEOUT_IS_LONG)
X	case 't': TimeOut = atol(optarg); goto loop;
X#else
X	case 't': TimeOut.tv_sec = atol(optarg); goto loop;
X#endif
X	case 'e': Program2Run = &argv[optind - 1]; break;
X	case 'l': {
X	    RloginProgram[2] = argv[optind];
X	    RloginProgram[4] = argv[optind - 1];
X	    break;
X	}
X	case 'r': {
X	    if(!(RecordFile = fopen(optarg, "w"))) {
X		perror(optarg);
X		exit(1);
X	    }
X	    goto loop;
X	}
X	default: exit(1);
X    }
X    setbuf(stdin, (char *)NULL);
X    signal(SIGINT, SIG_IGN);
X    Tty2ProgramMode();
X    GetPty();
X    StartSlave();
X    HaveFun();
X}
SHAR_EOF
if test 2512 -ne "`wc -c < 'main.c'`"
then
	echo shar: error transmitting "'main.c'" '(should have been 2512 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'pty.c'" '(2284 characters)'
if test -f 'pty.c'
then
	echo shar: will not over-write existing file "'pty.c'"
else
sed 's/^X//' << \SHAR_EOF > 'pty.c'
X/* pty.c - This module contains the code to handle the pty stuff.
X
X    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X
X    This file is part of vuser.
X
X    vuser is free software; you can redistribute it and/or modify it
X    under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your
X    option) any later version.
X
X    vuser is distributed in the hope that it will be useful, but
X    WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X    General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with GNU CC; see the file COPYING.  If not, write to the
X    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X*/
X
X#if !defined(lint)
Xstatic char SCCSid[] = "$Id: pty.c,v 1.1 89/12/15 21:31:03 jeffl Exp $";
X#endif
X
X#include <stdio.h>
X#include <fcntl.h>
X
X#if defined(__STDC__)
Xextern void Tty2ShellMode();
X#else
Xextern void Tty2ShellMode();
X#endif
X
X#if defined(SYSV)
X#include <termio.h>
Xstruct termio ShellTtyMode;
X#else
X#include <sgtty.h>
Xstruct sgttyb ShellTtyMode;
X#endif
X
Xint MasterPty = -1, SlavePty = -1;
X
Xvoid Pty2ShellMode()
X{
X#if defined(SYSV)
X    ioctl(SlavePty, TCSETA, &ShellTtyMode);
X#else
X    ioctl(SlavePty, TIOCSETP, &ShellTtyMode);
X#endif
X    return;
X}
X
Xvoid GetPty()
X{
X    int line = 'p', node = 0;
X    char tmp[32];
X
X    strcpy(tmp, "/dev/ptyxx");
X    for(; line <= 'q'; ++line)
X	for(node = 0; node < 16; ++node) {
X	    tmp[8] = (char)line;
X	    tmp[9] = (char)((node > 9 ? (node + 'a' - 10) : (node + '0')));
X	    if((MasterPty = open(tmp, O_RDWR|O_NDELAY)) >= 0) {
X		tmp[5] = 't';
X		if((SlavePty = open(tmp, O_RDWR)) < 0) {
X		    perror("on pty: ");
X		    Tty2ShellMode();
X		    exit(1);
X		}
X		Pty2ShellMode();
X		return;
X	    }
X	}
X    fprintf(stderr, "No ptys available, try later.\n");
X    Tty2ShellMode();
X    exit(1);
X}
X
Xvoid SlavePty2Stdio()
X{
X    int cnt = 3;
X#if !defined(_NFILE)
X    int maxFiles = getdtablesize();
X#else
X    int maxFiles = _NFILE;
X#endif
X    
X    close(0);
X    dup(SlavePty);
X    close(1);
X    dup(SlavePty);
X    close(2);
X    dup(SlavePty);
X    for(; cnt < maxFiles; ++cnt)
X	close(cnt);
X    return;
X}
SHAR_EOF
if test 2284 -ne "`wc -c < 'pty.c'`"
then
	echo shar: error transmitting "'pty.c'" '(should have been 2284 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'slave.c'" '(2607 characters)'
if test -f 'slave.c'
then
	echo shar: will not over-write existing file "'slave.c'"
else
sed 's/^X//' << \SHAR_EOF > 'slave.c'
X/* slave.c - This module contains the code to handle processing the slave
X   process.
X
X    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X
X    This file is part of vuser.
X
X    vuser is free software; you can redistribute it and/or modify it
X    under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your
X    option) any later version.
X
X    vuser is distributed in the hope that it will be useful, but
X    WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X    General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with GNU CC; see the file COPYING.  If not, write to the
X    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X*/
X
X#if !defined(lint)
Xstatic char SCCSid[] = "$Id: slave.c,v 1.1 89/12/15 21:31:13 jeffl Exp $";
X#endif
X
X#include <stdio.h>
X#include <signal.h>
X#include <sys/wait.h>
X
X#if defined(__STDC__)
Xextern void Tty2ShellMode();
Xextern void SlavePty2Stdio();
Xextern char *getenv(char *);
X#else
Xextern void Tty2ShellMode();
Xextern void SlavePty2Stdio();
Xextern char *getenv();
X#endif
X
Xint SlavePid = 0;
Xchar **Program2Run = (char **)NULL;
Xchar *RloginProgram[] = {
X    "rlogin",
X    "rlogin",
X    NULL,
X    "-l",
X    NULL,
X    NULL,
X};
X
Xvoid RunSlaveProgram()
X{
X    char *ptr = (char *)NULL;
X    char tmp[1024];
X
X    if(RloginProgram[2]) {
X	execlp(RloginProgram[0], RloginProgram[1], RloginProgram[2],
X	       RloginProgram[3], RloginProgram[4], RloginProgram[5]);
X	perror("rlogin: ");
X	Tty2ShellMode();
X	exit(1);
X    }
X    if(Program2Run) {
X	execvp(Program2Run[0], Program2Run);
X	perror(Program2Run[0]);
X	Tty2ShellMode();
X	exit(1);
X    }
X    if(!(ptr = getenv("SHELL")))
X	strcpy(tmp, "/bin/sh");
X    else
X	strcpy(tmp, ptr);
X    execlp(tmp, tmp, (char *)0);
X    perror("starting slave: ");
X    Tty2ShellMode();
X    exit(1);
X}
X
Xvoid KillSlave()
X{
X    kill(SlavePid, SIGTERM);
X    sleep(15);
X    kill(SlavePid, SIGKILL);
X    sleep(2);
X    Tty2ShellMode();
X    exit(1);
X}
X
XDeadSlave()
X{
X    union wait status;
X
X    if(wait(&status) != SlavePid) {
X	signal(SIGCLD, DeadSlave);
X	fprintf(stderr, "Someone else's child just killed me!!!\n");
X	fprintf(stderr, "Shutting down.\n");
X	KillSlave();
X    }
X    Tty2ShellMode();
X    exit(0);
X}
X
Xvoid StartSlave()
X{
X    signal(SIGCLD, DeadSlave);
X    if((SlavePid = fork()) < 0) {
X	perror("on fork: ");
X	Tty2ShellMode();
X	exit(1);
X    }
X    if(!SlavePid) {
X	SlavePty2Stdio();
X	RunSlaveProgram();
X    }
X    return;
X}
SHAR_EOF
if test 2607 -ne "`wc -c < 'slave.c'`"
then
	echo shar: error transmitting "'slave.c'" '(should have been 2607 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'tty.c'" '(2415 characters)'
if test -f 'tty.c'
then
	echo shar: will not over-write existing file "'tty.c'"
else
sed 's/^X//' << \SHAR_EOF > 'tty.c'
X/* tty.c - This module contains the code to manipulate the tty.
X
X    Copyright 1989 by Jeffrey F. Lawhorn  (jeffl at berick.uucp)
X
X    This file is part of vuser.
X
X    vuser is free software; you can redistribute it and/or modify it
X    under the terms of the GNU General Public License as published by
X    the Free Software Foundation; either version 1, or (at your
X    option) any later version.
X
X    vuser is distributed in the hope that it will be useful, but
X    WITHOUT ANY WARRANTY; without even the implied warranty of
X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
X    General Public License for more details.
X
X    You should have received a copy of the GNU General Public License
X    along with GNU CC; see the file COPYING.  If not, write to the
X    Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X*/
X
X#if !defined(lint)
Xstatic char SCCSid[] = "$Id: tty.c,v 1.1 89/12/15 21:31:24 jeffl Exp $";
X#endif
X
X#include <errno.h>
X#include <fcntl.h>
X
X#if defined(SYSV)
X#include <termio.h>
Xstruct termio ShellTtyMode, ProgramTtyMode;
X#else
X#include <sgtty.h>
Xstruct sgttyb ShellTtyMode, ProgramTtyMode;
X#endif
X
Xint Interactive = 1;
X
Xvoid Tty2ProgramMode()
X{
X    static int savedShellMode = 0;
X    int retVal = 0, ttyFile = -1;
X
X    if(!savedShellMode) {
X	if(!(ttyFile = open("/dev/tty", O_RDWR))) {
X	    perror("opening /dev/tty: ");
X	    exit(1);
X	}
X#if defined(SYSV)
X	retVal = ioctl(0, TCGETA, &ShellTtyMode);
X#else
X	retVal = ioctl(0, TIOCGETP, &ShellTtyMode);
X#endif
X	if(retVal < 0 && errno == ENOTTY)
X	    Interactive = 0;
X	else if(retVal < 0){
X	    perror("getting tty modes: ");
X	    exit(1);
X	}
X#if defined(SYSV)
X	retVal = ioctl(ttyFile, TCGETA, &ShellTtyMode);
X#else
X	retVal = ioctl(ttyFile, TIOCGETP, &ShellTtyMode);
X#endif
X	close(ttyFile);
X	savedShellMode = 1;
X    }
X#if defined(SYSV)
X    ioctl(0, TCGETA, &ProgramTtyMode);
X    ProgramTtyMode.c_oflag = 0;
X    ProgramTtyMode.c_iflag = 0;
X    ProgramTtyMode.c_lflag &= ~(ICANON|ECHO);
X    ProgramTtyMode.c_cc[4] = 1;
X    ProgramTtyMode.c_cc[5] = 0;
X    ioctl(0, TCSETA, &ProgramTtyMode);
X#else
X    ioctl(0, TIOCGETP, &ProgramTtyMode);
X    ProgramTtyMode.sg_flags |= RAW;
X    ProgramTtyMode.sg_flags &= ~(ECHO);
X    ioctl(0, TIOCSETP, &ProgramTtyMode);
X#endif
X    return;
X}
X
Xvoid Tty2ShellMode()
X{
X#if defined(SYSV)
X    ioctl(0, TCSETA, &ShellTtyMode);
X#else
X    ioctl(0, TIOCSETP, &ShellTtyMode);
X#endif
X    return;
X}
SHAR_EOF
if test 2415 -ne "`wc -c < 'tty.c'`"
then
	echo shar: error transmitting "'tty.c'" '(should have been 2415 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0
--

Jeff Lawhorn                 I know I had a pithy quote sitting 
jeffl at berick.uucp            around here somewhere...
ucsd!sdsu!berick!jeffl       
-- 

Jeff Lawhorn                 I know I had a pithy quote sitting 
jeffl at berick.uucp            around here somewhere...
ucsd!sdsu!berick!jeffl       



More information about the Alt.sources mailing list