initial commit of new dgamelaunch to CVS

git-svn-id: svn://katsu.triplehelix.org/dgamelaunch/trunk@1 db0b04b0-f4d1-0310-9a6d-de3e77497b0e
This commit is contained in:
Joshua Kwan 2003-12-30 21:30:32 +00:00
commit f626f44b02
17 changed files with 7607 additions and 0 deletions

9
Bugs Normal file
View File

@ -0,0 +1,9 @@
* Only the first 15 games in progress are shown on the games in progress
screen.
* Since virus kept calling alarm() on itself, I removed SIGALRM handling
altogether, which breaks occasional refresh of the bottom status line.
I don't consider this a huge bug, but if one day I, or someone else,
gets really bored, the full solution to this would be to bring back
the virus signal handling, and just clear the alarm calls before exiting
and going back to dgamelaunch code (or clear the alarm() handlers).

339
COPYING Normal file
View File

@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 2001-2003 M. Drew Streib
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.

119
Changelog Normal file
View File

@ -0,0 +1,119 @@
1.3.10.1 (2003/12/27)
* Merry Christmas, dtype...
* Major cleanups - remove most uses of strcpy() and strcat(), except
in virus.c, replace with snprintf.
* Add support for mailing users while they are playing.
[nh343-simple_mail.diff is needed]
* Rip out some crazy getopt code that was causing virus to go crazy
after a ":q!" Since dgamelaunch only passes up to argv[1] to virus
*ever*, just make it use argv[1] and panic if argc < 2, which should
never happen anyway.
* Add a confirmation to the change password screen so people like me
don't change their passwords by accident all the time.
* Removed strl{cat,cpy}.c, as they're not used by ttyrec even though
they are included.
* Remove many unused variables and make some functions void because
they don't return anything.
* Add some function prototypes in dgamelaunch.c to quell warnings.
* Make some vars unsigned to quell warnings.
* 'capital letter. experimental' section made irrelevant by use of
tolower(3) around getch()
* Change big conditional block to use a switch.
* Replace a global with a static variable within the function.
1.3.10 (2003/10/22)
* Added a mode flag to the open call for inprogress lock files.
(jilles)
1.3.9 (2003/09/06)
* Fixed the bug where editing options file wouldn't work on same
session as registration.
* Changed ttyplay timeout to 20 minutes with a better error msg.
* Made a zero length response on registration just dump you
back to the main menu.
1.3.7 (2003/09/06)
* Fixed the infamous hlen bug! I was rewinding to the beginning
of payload, not header.
1.3.5 (2003/09/05)
* Returned SIGWINCH to defaults after exiting virus. This was
probably the screen resize crash bug. (maybe?)
1.3.1 (2003/08/30)
* First shot at an engine to strip out graphics sets. It is a lot
harder than I would have thought, but doable at a 99% or so
success rate, I think, which should at least allow people to
view games in other graphics sets somewhat.
1.2.22 (2003/08/29)
* Expanded README
* Hope to have caught the error in a partial read of the error.
1.2.21 (2003/08/28)
* Added a README since I noticed people were downloading this.
1.2.20 (2003/08/24)
* Added additional check on payload length. Just a sanity thing.
1.2.19 (2003/08/24)
* Fixed (hopefully) that nasty bug that occurs when a partial read
after header read returned, and caused the next header read to
basically read random data (and thus a random length).
1.2.18 (2003/08/14)
* After determining select() on files sucks ass, I've just reduced
the polling interval on the ttyplay engine to 100ms, and given
up on an otherwise proper select call. Humans shouldn't notice
the latency, but I always will... grrr...
1.2.17 (2003/08/14)
* Put in the 50ms delay from header read until payload read during
preads in order to hopefully hack/wait around the race condition
99% of the time.
* Added a sleep(10) after receiving SIGHUP until sending one
to the child process in the mild hopes that this gets around
whatever condition is causing it not to receive and handle
the SIGHUP now.
1.2.16 (2003/08/14)
* All code is now run through indent(1) for cleanliness
* Game playback in progress now finds the last term reset and plays
forward from the last header before that reset.
* Some effort has been made to trap SIGHUP better and pass that
along to child processes as well. This still needs work for
nethack itself, but dgamelaunch is clean so far.
1.2.6 (2003/08/08)
* Viewing games in progress now works pretty well.
1.2.0beta2 (2003/08/07)
* Added initial ttyrec integration, although everything is very
broken at this point. Once I learn more about tty libraries than
I ever want to know, maybe everything will work again.
1.1.6 (2003/08/06)
* The last few revs included minor bugfixes found in production use,
changed some text in the program, and 1.1.6 is some minor packaging
changes for the purposes of a release to the public of the source
code.
1.1.1 (2003/08/02)
* Introduced virus (vi editor) code, and provided a new option to edit
personal nethack rc files.
1.0.0 (2003/08/01)
* Since 0.2.3 has been in production for over a year and a half, it
is hereby dubbed 1.0.0.
0.2.2 (2001/11/11)
* added change password feature. also is the way to get password crypt()ed
0.2.1 (2001/11/11)
* moved more of the crucial config into defines
* removed the echo of password on login
0.2.0 (2001/11/11)
* added crypt() support for the password file. Now the program first
checks for the crypt() password, then the plaintext one for legacy
and for ease of admins changing passwords.

21
Makefile Normal file
View File

@ -0,0 +1,21 @@
CC = gcc
LDFLAGS =
CFLAGS = -g3 -O0 -Wall -W -Wno-unused-parameter
SRCS = virus.c ttyrec.c dgamelaunch.c io.c ttyplay.c stripgfx.c
OBJS = $(SRCS:.c=.o)
LIBS = -lncurses -lcrypt
all: dgamelaunch
dgamelaunch: $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS) $(LIBS)
clean:
rm -f dgamelaunch
rm -f *.o
install:
cp dgamelaunch /opt/nethack/nethack.dtype.org/
indent:
indent -ts2 *.c *.h
rm *~
release: clean indent

82
README Normal file
View File

@ -0,0 +1,82 @@
In short, there isn't an easy 'make install' option for this, as it
runs in a chroot environment and is somewhat specific to a server.
It should compile on most modern Linux distros, although there are
a couple things which do hinder total portability.
That said, it shouldn't be too hard to figure out most of the defines
in dgamelaunch.(c|h), and I'm more than happy to help out with any
questions.
Me: M. Drew Streib <dtype@dtype.org>
Mailing list: http://alt.org/mailman/listinfo/nethack/
BASIC SETUP INSTRUCTIONS
===========================
1) Setup a chroot area, with a bin directory, a var directory, and a
'dgldir' directory. You'll also need 'etc' and 'etc/terminfo' for
curses.
2) If using the #define's as an example, setup 'rcfiles', 'ttyrec',
and 'inprogress' directories, in the dgldir. Also touch the files
'dgl-login' and 'dgl-lock' in the main chroot directory.
Your directories/files should now look like this:
/chrootdir/bin/
/chrootdir/var/
/chrootdir/etc/
/chrootdir/etc/terminfo/
/chrootdir/dgldir/
/chrootdir/dgldir/ttyrec/
/chrootdir/dgldir/inprogress/
/chrootdir/dgldir/rcfiles/
/chrootdir/dgl-login (empty)
/chrootdir/dgl-lock (empty)
They should all be owned by the user you intend to run this as.
2) In the #define's in dgamelaunch.c and dgamelaunch.h, change the
values for those directories, sort of using my own directory setup
as an example.
3) Compile nethack, and basically tell it that /var is its playground,
since it will be in the chroot environment. There should be no need
for special nethack compilation instructions, but it will need to be
installed to the chroot environment. A static compile will ensure that
it can run without any libraries, or you can of course place libraries
in the environment.
For security reasons, I'd avoid putting any shells, etc, in this environment
though, of course. Just whatever needs to be exec'd, which is namely,
nethack.
4) Be sure to set the user id's in the #define's of dgamelaunch to
the owner of the chroot directory, so nethack and dgamelaunch have
access to their files. This user needs a lot of write access, but
you can leave the executables owned by someone else.
5) Either setup dgamelaunch as the shell for a single login, in which
case it must be suid root (don't worry. It sheds privs right after the
immediate chroot), or set it up as an inetd service. I use the following
lines for my xinetd.conf.
service telnet
{
socket_type = stream
protocol = tcp
wait = no
user = root
server = /usr/sbin/in.telnetd
server_args = -L /opt/nethack/nethack.dtype.org/dgamelaunch
rlimit_cpu = 3600
bind = 64.71.163.206
}
6) Populate /chrootdir/etc/terminfo with terminfo files. I think that most
modern ncurses will default to terminfo when it is available. Mine seemed
to. This also makes it easy to add term types on the fly.
7) You can test your compilation of dgamelaunch by simply running
it as root, just as a shell (with suid) or inetd might do.

1097
dgamelaunch.c Normal file

File diff suppressed because it is too large Load Diff

27
dgamelaunch.h Normal file
View File

@ -0,0 +1,27 @@
/* IMPORTANT defines */
#ifndef __DGAMELAUNCH_H
#define __DGAMELAUNCH_H
struct dg_user
{
char* username;
char* email;
char* env;
char* password;
int flags;
};
#define SHED_UID 1031 /* the uid to shed privs to */
#define SHED_GID 1031 /* the gid to shed privs to */
#define MAXUSERS 64000 /* solves some preallocation issues. */
#define LOC_CHROOT "/var/lib/dgamelaunch/"
#define LOC_NETHACK "/bin/nethack"
#define LOC_DGLDIR "/dgldir/rcfiles/"
#define LOC_TTYRECDIR "/dgldir/ttyrec/"
#define LOC_INPROGRESSDIR "/dgldir/inprogress/"
#define LOC_SPOOLDIR "/var/mail"
#define LOC_CANNED "/dgl-default-rcfile"
#endif

20
dgl-default-rcfile Normal file
View File

@ -0,0 +1,20 @@
# This is your personal nethackrc file. It is in the same format as default
# nethack options files. Optionally uncomment the following lines as a
# starter. This editor is vi-like. Type ESC a couple times, then ':q!'
# (without quotes) to exit if you get stuck.
#
# See the following link for more options information:
# http://www.nethack.org/v341/Guidebook.html#_TOCentry_40
#
# OPTIONS=showexp,showscore,time
# OPTIONS=!autopickup,color
# OPTIONS=catname:catfoo,dogname:dogfoo,horsename:horsefoo
#
# The following lines are necessary if you want to use the menucolors option.
# OPTIONS=menucolors
# MENUCOLOR=" blessed "=green
# MENUCOLOR=" holy "=green
# MENUCOLOR=" uncursed "=yellow
# MENUCOLOR=" cursed "=red
# MENUCOLOR=" unholy "=red
# MENUCOLOR=" cursed .* (being worn)"=orange&underline

146
io.c Normal file
View File

@ -0,0 +1,146 @@
/*
* Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include "ttyrec.h"
#define SWAP_ENDIAN(val) ((unsigned int) ( \
(((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
(((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \
(((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \
(((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
static int
is_little_endian ()
{
static int retval = -1;
if (retval == -1)
{
int n = 1;
char *p = (char *) &n;
char x[] = { 1, 0, 0, 0 };
assert (sizeof (int) == 4);
if (memcmp (p, x, 4) == 0)
{
retval = 1;
}
else
{
retval = 0;
}
}
return retval;
}
static int
convert_to_little_endian (int x)
{
if (is_little_endian ())
{
return x;
}
else
{
return SWAP_ENDIAN (x);
}
}
int
read_header (FILE * fp, Header * h)
{
int buf[3];
int numread;
long offset;
offset = ftell (fp);
numread = fread (buf, sizeof (int), 3, fp);
/* odd but possible race condition when full header isn't read because
* it isn't there yet. */
if (numread < 3)
{
fseek (fp, offset, SEEK_SET);
return 0;
}
h->tv.tv_sec = convert_to_little_endian (buf[0]);
h->tv.tv_usec = convert_to_little_endian (buf[1]);
h->len = convert_to_little_endian (buf[2]);
return 1;
}
int
write_header (FILE * fp, Header * h)
{
int buf[3];
buf[0] = convert_to_little_endian (h->tv.tv_sec);
buf[1] = convert_to_little_endian (h->tv.tv_usec);
buf[2] = convert_to_little_endian (h->len);
if (fwrite (buf, sizeof (int), 3, fp) == 0)
{
return 0;
}
return 1;
}
static char *progname = "";
void
set_progname (const char *name)
{
progname = strdup (name);
}
FILE *
efopen (const char *path, const char *mode)
{
FILE *fp = fopen (path, mode);
if (fp == NULL)
{
fprintf (stderr, "%s: %s: %s\n", progname, path, strerror (errno));
exit (EXIT_FAILURE);
}
return fp;
}

10
io.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef __TTYREC_IO_H__
#define __TTYREC_IO_H__
#include "ttyrec.h"
int read_header (FILE * fp, Header * h);
int write_header (FILE * fp, Header * h);
FILE *efopen (const char *path, const char *mode);
#endif

43
last_char_is.c Normal file
View File

@ -0,0 +1,43 @@
/*
* busybox library eXtended function
*
* Copyright (C) 2001 Larry Doolittle, <ldoolitt@recycle.lbl.gov>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <string.h>
/* Find out if the last character of a string matches the one given Don't
* underrun the buffer if the string length is 0. Also avoids a possible
* space-hogging inline of strlen() per usage.
*/
char *
last_char_is (const char *s, int c)
{
char *sret;
if (!s)
return NULL;
sret = (char *) s + strlen (s) - 1;
if (sret >= s && *sret == c)
{
return sret;
}
else
{
return NULL;
}
}

394
stripgfx.c Normal file
View File

@ -0,0 +1,394 @@
/* Parts taken from drawing.c, nethack source */
/* Copyright (c) NetHack Development Team 1992. */
/* NetHack may be freely redistributed. See license for details. */
#include <string.h>
#include "stripgfx.h"
#define MAXPCHARS 92
unsigned char gfx_map[256];
unsigned int state = 0;
static unsigned char no_graphics[MAXPCHARS] = {
' ', /* 0 */
/* stone */
'|', /* vwall */
'-', /* hwall */
'-', /* tlcorn */
'-', /* trcorn */
'-', /* blcorn */
'-', /* brcorn */
'-', /* crwall */
'-', /* tuwall */
'-', /* tdwall */
'|', /* 10 */
/* tlwall */
'|', /* trwall */
'.', /* ndoor */
'-', /* vodoor */
'|', /* hodoor */
'+', /* vcdoor */
'+', /* hcdoor */
'#', /* bars */
'#', /* tree */
'.', /* room */
'#', /* 20 */
/* dark corr */
'#', /* lit corr */
'<', /* upstair */
'>', /* dnstair */
'<', /* upladder */
'>', /* dnladder */
'_', /* altar */
'|', /* grave */
'\\', /* throne */
'#', /* sink */
'{', /* 30 */
/* fountain */
'}', /* pool */
'.', /* ice */
'}', /* lava */
'.', /* vodbridge */
'.', /* hodbridge */
'#', /* vcdbridge */
'#', /* hcdbridge */
' ', /* open air */
'#', /* [part of] a cloud */
'}', /* 40 */
/* under water */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* 50 */
/* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'^', /* trap */
'"', /* web */
'^', /* trap */
'^', /* 60 */
/* trap */
'^', /* trap */
'^', /* trap */
'|', /* vbeam */
'-', /* hbeam */
'\\', /* lslant */
'/', /* rslant */
'*', /* dig beam */
'!', /* camera flash beam */
')', /* boomerang open left */
'(', /* 70 */
/* boomerang open right */
'0', /* 4 magic shield symbols */
'#',
'@',
'*',
'/', /* swallow top left */
'-', /* swallow top center */
'\\', /* swallow top right */
'|', /* swallow middle left */
'|', /* swallow middle right */
'\\', /* 80 */
/* swallow bottom left */
'-', /* swallow bottom center */
'/', /* swallow bottom right */
'/', /* explosion top left */
'-', /* explosion top center */
'\\', /* explosion top right */
'|', /* explosion middle left */
' ', /* explosion middle center */
'|', /* explosion middle right */
'\\', /* explosion bottom left */
'-', /* 90 */
/* explosion bottom center */
'/' /* explosion bottom right */
};
static unsigned char ibm_graphics[MAXPCHARS] = {
/* 0*/ 0x00,
0xb3, /* : meta-3, vertical rule */
0xc4, /* : meta-D, horizontal rule */
0xda, /* : meta-Z, top left corner */
0xbf, /* : meta-?, top right corner */
0xc0, /* : meta-@, bottom left */
0xd9, /* : meta-Y, bottom right */
0xc5, /* : meta-E, cross */
0xc1, /* : meta-A, T up */
0xc2, /* : meta-B, T down */
/*10 */ 0xb4,
/* : meta-4, T left */
0xc3, /* : meta-C, T right */
0xfa, /* : meta-z, centered dot */
0xfe, /* : meta-~, small centered square */
0xfe, /* : meta-~, small centered square */
0x00,
0x00,
240, /* : equivalence symbol */
241, /* : plus or minus symbol */
0xfa, /* : meta-z, centered dot */
/*20 */ 0xb0,
/* : meta-0, light shading */
0xb1, /* : meta-1, medium shading */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/*30 */ 0xf4,
/* : meta-t, integral top half */
0xf7, /* : meta-w, approx. equals */
0xfa, /* : meta-z, centered dot */
0xf7, /* : meta-w, approx. equals */
0xfa, /* : meta-z, centered dot */
0xfa, /* : meta-z, centered dot */
0x00,
0x00,
0x00,
0x00,
/*40 */ 0xf7,
/* : meta-w, approx. equals */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/*50*/ 0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/*60*/ 0x00,
0x00,
0x00,
0xb3, /* : meta-3, vertical rule */
0xc4, /* : meta-D, horizontal rule */
0x00,
0x00,
0x00,
0x00,
0x00,
/*70*/ 0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xb3, /* : meta-3, vertical rule */
0xb3, /* : meta-3, vertical rule */
/*80*/ 0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xb3, /* : meta-3, vertical rule */
0x00,
0xb3, /* : meta-3, vertical rule */
0x00,
/*90*/ 0x00,
0x00
};
static unsigned char dec_graphics[MAXPCHARS] = {
/* 0*/ 0x00,
0xf8, /* : meta-x, vertical rule */
0xf1, /* : meta-q, horizontal rule */
0xec, /* : meta-l, top left corner */
0xeb, /* : meta-k, top right corner */
0xed, /* : meta-m, bottom left */
0xea, /* : meta-j, bottom right */
0xee, /* : meta-n, cross */
0xf6, /* : meta-v, T up */
0xf7, /* : meta-w, T down */
/*10 */ 0xf5,
/* : meta-u, T left */
0xf4, /* : meta-t, T right */
0xfe, /* : meta-~, centered dot */
0xe1, /* : meta-a, solid block */
0xe1, /* : meta-a, solid block */
0x00,
0x00,
0xfb, /* : meta-{, small pi */
0xe7, /* : meta-g, plus-or-minus */
0xfe, /* : meta-~, centered dot */
/*20*/ 0x00,
0x00,
0x00,
0x00,
0xf9, /* : meta-y, greater-than-or-equals */
0xfa, /* : meta-z, less-than-or-equals */
0x00, /* 0xc3, \E)3: meta-C, dagger */
0x00,
0x00,
0x00,
/*30 */ 0x00,
/* 0xdb, \E)3: meta-[, integral top half */
0xe0, /* : meta-\, diamond */
0xfe, /* : meta-~, centered dot */
0xe0, /* : meta-\, diamond */
0xfe, /* : meta-~, centered dot */
0xfe, /* : meta-~, centered dot */
0x00,
0x00,
0x00,
0x00,
/*40 */ 0xe0,
/* : meta-\, diamond */
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
/*50*/ 0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00, /* 0xbd, \E)3: meta-=, int'l currency */
0x00,
/*60*/ 0x00,
0x00,
0x00,
0xf8, /* : meta-x, vertical rule */
0xf1, /* : meta-q, horizontal rule */
0x00,
0x00,
0x00,
0x00,
0x00,
/*70*/ 0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xef, /* : meta-o, high horizontal line */
0x00,
0xf8, /* : meta-x, vertical rule */
0xf8, /* : meta-x, vertical rule */
/*80*/ 0x00,
0xf3, /* : meta-s, low horizontal line */
0x00,
0x00,
0xef, /* : meta-o, high horizontal line */
0x00,
0xf8, /* : meta-x, vertical rule */
0x00,
0xf8, /* : meta-x, vertical rule */
0x00,
/*90 */ 0xf3,
/* : meta-s, low horizontal line */
0x00
};
static unsigned char IBM_r_oc_syms[18] = { /* a la EPYX Rogue */
/* 0*/ '\0',
0x00,
0x18, /* weapon: up arrow */
/* 0x0a, */ 0x00,
/* armor: Vert rect with o */
/* 0x09, */ 0x00,
/* ring: circle with arrow */
/* 5 */ 0x0c,
/* amulet: "female" symbol */
0x00,
0x05, /* food: club (as in cards) */
0xad, /* potion: upside down '!' */
0x0e, /* scroll: musical note */
/*10*/ 0x00,
0xe7, /* wand: greek tau */
0x0f, /* gold: yes it's the same as gems */
0x0f, /* gems: fancy '*' */
0x00,
/*15*/ 0x00,
0x00,
0x00
};
void
populate_gfx_array (int gfxset)
{
int i;
memset (gfx_map, 0, 256);
if (gfxset == NO_GRAPHICS)
return;
for (i = 0; i < MAXPCHARS; i++)
{
if ((gfxset == DEC_GRAPHICS) && (dec_graphics[i])
&& !(gfx_map[dec_graphics[i]]))
gfx_map[dec_graphics[i] - 128] = no_graphics[i];
if ((gfxset == IBM_GRAPHICS) && (ibm_graphics[i]))
gfx_map[ibm_graphics[i] - 128] = no_graphics[i];
}
/*
endwin();
for (i=0;i<256;i++) {
printf("%X:%X ",i,gfx_map[i]);
}
exit(1); */
}
unsigned char
strip_gfx (unsigned char inchar)
{
if ((inchar == 0x0E) && (state == 0))
{
state = 1;
return 0x00;
}
if ((inchar == 0x0F) && (state == 1))
{
state = 0;
return inchar;
}
if ((inchar == 0x1B) && (state == 1))
{
state = 0;
return inchar;
}
if (gfx_map[inchar] && (state == 1))
{
return gfx_map[inchar];
}
return inchar;
}

7
stripgfx.h Normal file
View File

@ -0,0 +1,7 @@
#define NO_GRAPHICS 1
#define DEC_GRAPHICS 2
#define IBM_GRAPHICS 3
void populate_gfx_array (int gfxset);
unsigned char strip_gfx (unsigned char inchar);

405
ttyplay.c Normal file
View File

@ -0,0 +1,405 @@
/*
* Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <termios.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <curses.h>
#include "ttyrec.h"
#include "io.h"
#include "stripgfx.h"
extern int caught_sighup;
off_t seek_offset_clrscr;
int bstripgfx;
typedef double (*WaitFunc) (struct timeval prev,
struct timeval cur, double speed);
typedef int (*ReadFunc) (FILE * fp, Header * h, char **buf, int pread);
typedef void (*WriteFunc) (char *buf, int len);
typedef void (*ProcessFunc) (FILE * fp, double speed,
ReadFunc read_func, WaitFunc wait_func);
struct timeval
timeval_diff (struct timeval tv1, struct timeval tv2)
{
struct timeval diff;
diff.tv_sec = tv2.tv_sec - tv1.tv_sec;
diff.tv_usec = tv2.tv_usec - tv1.tv_usec;
if (diff.tv_usec < 0)
{
diff.tv_sec--;
diff.tv_usec += 1000000;
}
return diff;
}
struct timeval
timeval_div (struct timeval tv1, double n)
{
double x = ((double) tv1.tv_sec + (double) tv1.tv_usec / 1000000.0) / n;
struct timeval div;
div.tv_sec = (int) x;
div.tv_usec = (x - (int) x) * 1000000;
return div;
}
double
ttywait (struct timeval prev, struct timeval cur, double speed)
{
struct timeval diff = timeval_diff (prev, cur);
fd_set readfs;
assert (speed != 0);
diff = timeval_div (diff, speed);
FD_SET (STDIN_FILENO, &readfs);
select (1, &readfs, NULL, NULL, &diff); /* skip if a user hits any key */
if (FD_ISSET (0, &readfs))
{ /* a user hits a character? */
char c;
read (STDIN_FILENO, &c, 1); /* drain the character */
switch (c)
{
case '+':
case 'f':
speed *= 2;
break;
case '-':
case 's':
speed /= 2;
break;
case '1':
speed = 1.0;
break;
}
}
return speed;
}
double
ttynowait (struct timeval prev, struct timeval cur, double speed)
{
return 0; /* Speed isn't important. */
}
int
ttyread (FILE * fp, Header * h, char **buf, int pread)
{
long offset;
/* do this BEFORE header read, hlen bug */
offset = ftell (fp);
if (read_header (fp, h) == 0)
{
return 0;
}
/* length should never be longer than one BUFSIZ */
if (h->len > BUFSIZ)
{
perror ("hlen");
exit (1);
}
*buf = malloc (h->len);
if (*buf == NULL)
{
perror ("malloc");
exit (1);
}
if (fread (*buf, 1, h->len, fp) != h->len)
{
fseek (fp, offset, SEEK_SET);
return 0;
}
return 1;
}
int
ttypread (FILE * fp, Header * h, char **buf, int pread)
{
int counter = 0;
fd_set readfs;
struct timeval zerotime;
zerotime.tv_sec = 0;
zerotime.tv_usec = 0;
/*
* Read persistently just like tail -f.
*/
while (ttyread (fp, h, buf, 1) == 0)
{
struct timeval w = { 0, 100000 };
select (0, NULL, NULL, NULL, &w);
clearerr (fp);
if (counter++ > (20 * 60 * 10))
{
endwin ();
printf ("Exiting due to 20 minutes of inactivity.\n");
exit (2);
return 0;
}
/* look for keypresses here. as good a place as any */
FD_SET (STDIN_FILENO, &readfs);
select (1, &readfs, NULL, NULL, &zerotime);
if (FD_ISSET (0, &readfs))
{ /* a user hits a character? */
char c;
read (STDIN_FILENO, &c, 1); /* drain the character */
switch (c)
{
case 'q':
return 0;
break;
}
}
}
return 1;
}
void
ttywrite (char *buf, int len)
{
int i;
if (bstripgfx)
{
for (i = 0; i < len; i++)
{
buf[i] = strip_gfx (buf[i]);
}
}
fwrite (buf, 1, len, stdout);
}
void
ttynowrite (char *buf, int len)
{
/* do nothing */
}
void
ttyplay (FILE * fp, double speed, ReadFunc read_func,
WriteFunc write_func, WaitFunc wait_func, off_t offset)
{
int first_time = 1;
struct timeval prev;
setbuf (stdout, NULL);
setbuf (fp, NULL);
/* for dtype's attempt to get the last clrscr and playback from there */
if (offset)
{
lseek (fileno (fp), offset, SEEK_SET);
}
while (1)
{
char *buf;
Header h;
if (read_func (fp, &h, &buf, 0) == 0)
{
break;
}
if (!first_time)
{
speed = wait_func (prev, h.tv, speed);
}
first_time = 0;
write_func (buf, h.len);
prev = h.tv;
free (buf);
}
}
void
set_seek_offset_clrscr (FILE * fp)
{
off_t raw_seek_offset = 0;
char *buf;
struct stat mystat;
int state = 0;
int i;
int bytesread;
lseek (fileno (fp), 0, SEEK_SET);
fstat (fileno (fp), &mystat);
buf = malloc (mystat.st_size);
bytesread = read (fileno (fp), buf, mystat.st_size);
/* one byte at at time sucks, but is a simple hack for the temp
* being to avoid looking for wraparounds */
for (i = 0; i < bytesread; i++)
{
if (buf[i] == 0x1b)
{
state = 1;
}
else if ((buf[i] == 0x5b) && (state == 1))
{
state = 2;
}
else if ((buf[i] == 0x32) && (state == 2))
{
state = 3;
}
else if ((buf[i] == 0x4a) && ((state == 2) || (state == 3)))
{
state = 4;
}
else
{
state = 0;
}
if (state == 4)
{
raw_seek_offset = i - 2;
}
}
free (buf);
/* now find last filepos that is less than seek offset */
lseek (fileno (fp), 0, SEEK_SET);
while (1)
{
char *buf;
Header h;
if (ttyread (fp, &h, &buf, 0) == 0)
{
break;
}
if (lseek (fileno (fp), 0, SEEK_CUR) < raw_seek_offset)
{
seek_offset_clrscr = lseek (fileno (fp), 0, SEEK_CUR);
}
free (buf);
}
}
void
ttyskipall (FILE * fp)
{
/*
* Skip all records.
*/
ttyplay (fp, 0, ttyread, ttynowrite, ttynowait, 0);
}
void
ttyplayback (FILE * fp, double speed, ReadFunc read_func, WaitFunc wait_func)
{
ttyplay (fp, speed, ttyread, ttywrite, wait_func, 0);
}
void
ttypeek (FILE * fp, double speed, ReadFunc read_func, WaitFunc wait_func)
{
ttyskipall (fp);
set_seek_offset_clrscr (fp);
if (seek_offset_clrscr)
{
ttyplay (fp, 0, ttyread, ttywrite, ttynowait, seek_offset_clrscr);
}
ttyplay (fp, speed, ttypread, ttywrite, ttynowait, 0);
}
void
usage (void)
{
printf ("Usage: ttyplay [OPTION] [FILE]\n");
printf (" -s SPEED Set speed to SPEED [1.0]\n");
printf (" -n No wait mode\n");
printf (" -p Peek another person's ttyrecord\n");
exit (EXIT_FAILURE);
}
int
ttyplay_main (char *ttyfile, int mode, int rstripgfx)
{
double speed = 1.0;
ReadFunc read_func = ttyread;
WaitFunc wait_func = ttywait;
ProcessFunc process = ttyplayback;
FILE *input = stdin;
struct termios old, new;
/* strip graphics mode flag */
bstripgfx = rstripgfx;
if (bstripgfx)
populate_gfx_array (DEC_GRAPHICS);
seek_offset_clrscr = 0;
if (mode == 1)
process = ttypeek;
input = efopen (ttyfile, "r");
tcgetattr (0, &old); /* Get current terminal state */
new = old; /* Make a copy */
new.c_lflag &= ~(ICANON | ECHO | ECHONL); /* unbuffered, no echo */
tcsetattr (0, TCSANOW, &new); /* Make it current */
process (input, speed, read_func, wait_func);
tcsetattr (0, TCSANOW, &old); /* Return terminal state */
return 0;
}

386
ttyrec.c Normal file
View File

@ -0,0 +1,386 @@
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
* - added Native Language Support
*/
/* 2000-12-27 Satoru Takabayashi <satoru@namazu.org>
* - modify `script' to create `ttyrec'.
*/
/*
* script
*/
#include "dgamelaunch.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <time.h>
#include <sys/file.h>
#include <sys/signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stropts.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
#include "ttyrec.h"
#include "io.h"
#define HAVE_inet_aton
#define HAVE_scsi_h
#define HAVE_kd_h
#define _(FOO) FOO
#ifdef HAVE_openpty
#include <libutil.h>
#endif
#define CDEL _POSIX_VDISABLE
extern char ttyrec_filename[100];
extern int caught_sighup;
extern int pid_game;
void done (void);
void fail (void);
void fixtty (void);
void getslave (void);
void doinput (void);
void dooutput (void);
void doshell (char *);
FILE *fscript;
int master;
int slave;
int child;
int subchild;
struct termios tt;
struct winsize win;
int lb;
int l;
int aflg;
int uflg;
int
ttyrec_main (char *username)
{
void finish ();
char dirname[100];
/* create if it doesn't exist */
mkdir (dirname, 0755);
snprintf (dirname, 100, "/%s%s%s", LOC_TTYRECDIR, username,
ttyrec_filename);
if ((fscript = fopen (dirname, "w")) == NULL)
{
perror (dirname);
fail ();
}
setbuf (fscript, NULL);
fixtty ();
(void) signal (SIGCHLD, finish);
child = fork ();
if (child < 0)
{
perror ("fork");
fail ();
}
if (child == 0)
{
subchild = child = fork ();
if (child < 0)
{
perror ("fork");
fail ();
}
if (child)
{
close (slave);
pid_game = child;
dooutput ();
}
else
doshell (username);
}
doinput ();
return 0;
}
void
doinput ()
{
register int cc;
char ibuf[BUFSIZ];
(void) fclose (fscript);
while ((cc = read (0, ibuf, BUFSIZ)) > 0)
(void) write (master, ibuf, cc);
done ();
}
#include <sys/wait.h>
void
finish ()
{
#if defined(SVR4)
int status;
#else /* !SVR4 */
union wait status;
#endif /* !SVR4 */
register int pid;
register int die = 0;
while ((pid = wait3 ((int *) &status, WNOHANG, 0)) > 0)
if (pid == child)
die = 1;
if (die)
done ();
}
struct linebuf
{
char str[BUFSIZ + 1]; /* + 1 for an additional NULL character. */
int len;
};
void
check_line (const char *line)
{
static int uuencode_mode = 0;
static FILE *uudecode;
if (uuencode_mode == 1)
{
fprintf (uudecode, "%s", line);
if (strcmp (line, "end\n") == 0)
{
pclose (uudecode);
uuencode_mode = 0;
}
}
else
{
int dummy;
char dummy2[BUFSIZ];
if (sscanf (line, "begin %o %s", &dummy, dummy2) == 2)
{
/*
* uuencode line found!
*/
uudecode = popen ("uudecode", "w");
fprintf (uudecode, "%s", line);
uuencode_mode = 1;
}
}
}
void
check_output (const char *str, int len)
{
static struct linebuf lbuf = { "", 0 };
int i;
for (i = 0; i < len; i++)
{
if (lbuf.len < BUFSIZ)
{
lbuf.str[lbuf.len] = str[i];
if (lbuf.str[lbuf.len] == '\r')
{
lbuf.str[lbuf.len] = '\n';
}
lbuf.len++;
if (lbuf.str[lbuf.len - 1] == '\n')
{
if (lbuf.len > 1)
{ /* skip a blank line. */
lbuf.str[lbuf.len] = '\0';
check_line (lbuf.str);
}
lbuf.len = 0;
}
}
else
{ /* buffer overflow */
lbuf.len = 0;
}
}
}
void
dooutput ()
{
int cc;
time_t tvec, time ();
char obuf[BUFSIZ], *ctime ();
setbuf (stdout, NULL);
(void) close (0);
tvec = time ((time_t *) NULL);
for (;;)
{
Header h;
cc = read (master, obuf, BUFSIZ);
if (cc <= 0)
break;
if (uflg)
check_output (obuf, cc);
h.len = cc;
gettimeofday (&h.tv, NULL);
(void) write (1, obuf, cc);
(void) write_header (fscript, &h);
(void) fwrite (obuf, 1, cc, fscript);
}
done ();
}
void
doshell (char *username)
{
char *argv1 = LOC_NETHACK;
char *argv2 = "-u";
char *myargv[10];
getslave ();
(void) close (master);
(void) fclose (fscript);
(void) dup2 (slave, 0);
(void) dup2 (slave, 1);
(void) dup2 (slave, 2);
(void) close (slave);
myargv[0] = argv1;
myargv[1] = argv2;
myargv[2] = username;
myargv[3] = 0;
execvp (LOC_NETHACK, myargv);
fail ();
}
void
fixtty ()
{
struct termios rtt;
rtt = tt;
rtt.c_iflag = 0;
rtt.c_lflag &= ~(ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK | ECHONL);
rtt.c_oflag = OPOST;
rtt.c_cc[VINTR] = CDEL;
rtt.c_cc[VQUIT] = CDEL;
rtt.c_cc[VERASE] = CDEL;
rtt.c_cc[VKILL] = CDEL;
rtt.c_cc[VEOF] = 1;
rtt.c_cc[VEOL] = 0;
(void) tcsetattr (0, TCSAFLUSH, &rtt);
}
void
fail ()
{
(void) kill (0, SIGTERM);
done ();
}
void
done ()
{
time_t tvec, time ();
char *ctime ();
if (subchild)
{
tvec = time ((time_t *) NULL);
(void) fclose (fscript);
(void) close (master);
}
else
{
(void) tcsetattr (0, TCSAFLUSH, &tt);
}
exit (0);
}
void
getslave ()
{
(void) setsid ();
/* grantpt( master);
unlockpt(master);
if ((slave = open((const char *)ptsname(master), O_RDWR)) < 0) {
perror((const char *)ptsname(master));
fail();
perror("open(fd, O_RDWR)");
fail();
} */
if (isastream (slave))
{
if (ioctl (slave, I_PUSH, "ptem") < 0)
{
perror ("ioctl(fd, I_PUSH, ptem)");
fail ();
}
if (ioctl (slave, I_PUSH, "ldterm") < 0)
{
perror ("ioctl(fd, I_PUSH, ldterm)");
fail ();
}
#ifndef _HPUX_SOURCE
if (ioctl (slave, I_PUSH, "ttcompat") < 0)
{
perror ("ioctl(fd, I_PUSH, ttcompat)");
fail ();
}
#endif
(void) ioctl (0, TIOCGWINSZ, (char *) &win);
}
}

15
ttyrec.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __TTYREC_H__
#define __TTYREC_H__
#include <sys/time.h>
#include <sys/types.h>
typedef struct header
{
struct timeval tv;
size_t len;
}
Header;
#endif

4487
virus.c Normal file

File diff suppressed because it is too large Load Diff