[unix-pc.sources] Larry Wall's patch v2.0-11

egray@fthood.UUCP (06/07/88)

Hello netlanders...

This is a *reposting* of Larry Wall's patch v2.0 source thru patchlevel 11.
Since most of the unix-pc.source sites don't routinely get comp.sources.unix
or comp.sources.bugs, I believe a repost is appropriate.  (If you think
otherwise, it's too late!)  This posting also contains a makefile that is
specific to the AT&T Unix PC 7300/3b1 called "7300make".

Emmet P. Gray				US Army, HQ III Corps & Fort Hood
...!ihnp4!uiucuxc!fthood!egray		Attn: AFZF-DE-ENV
					Directorate of Engineering & Housing
					Environmental Management Office
					Fort Hood, TX 76544-5057

-------------------------------------------------------------------------------
#! /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:
#	7300make
#	Configure
#	EXTERN.h
#	INTERN.h
#	MANIFEST
#	Makefile.SH
#	README
#	common.h
#	config.H
# This archive created: Tue Jun  7 11:32:42 1988
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting '7300make'" '(1048 characters)'
if test -f '7300make'
then
	echo shar: "will not over-write existing file '7300make'"
else
sed 's/^X//' << \SHAR_EOF > '7300make'
X#	modified makefile for Unix PC and shared libraries 
X
XLDFLAGS = -s
XBIN = /usr/local/bin
XMANDIR = /usr/man/man1
XSHAREDLIB = /lib/crt0s.o /lib/shlib.ifile
XLINT = lint -hax
X
XSRC = patch.c pch.c inp.c version.c util.c
XOBJ = patch.o pch.o inp.o version.o util.o
X
Xall:	patch
X
Xpatch:	$(OBJ)
X	$(LD) $(LDFLAGS) $(OBJ) $(SHAREDLIB) -o patch
X
Xinstall: patch
X	cp patch $(BIN)/patch
X	rm patch
X#	cp patch.man $(MANDIR)/patch.1
X
Xclean:
X	rm -f *.o core
X
Xlint:
X	$(LINT) $(SRC)
X
Xshar:
X	shar -a 7300make Configure EXTERN.h INTERN.h MANIFEST Makefile.SH \
X	README common.h config.H > patch_sh.1
X	shar -a config.h.SH inp.c inp.h malloc.c patch.c util.h version.c \
X	version.h > patch_sh.2
X	shar -a patch.man patchlevel.h pch.c pch.h util.c > patch_sh.3
X
X# assert.o is now in the shared library, yeah!
X#assert.o:
X#	ar x /lib/libc.a assert.o
X
Xpatch.o:	config.h common.h inp.h pch.h util.h version.h
Xpch.o:		config.h common.h pch.h util.h
Xinp.o:		config.h common.h inp.h util.h
Xutil.o:		config.h common.h util.h
Xversion.o:	config.h common.h version.h patchlevel.h util.h
SHAR_EOF
if test 1048 -ne "`wc -c < '7300make'`"
then
	echo shar: "error transmitting '7300make'" '(should have been 1048 characters)'
fi
fi
echo shar: "extracting 'Configure'" '(30911 characters)'
if test -f 'Configure'
then
	echo shar: "will not over-write existing file 'Configure'"
else
sed 's/^X//' << \SHAR_EOF > 'Configure'
X#! /bin/sh
X#
X# If these # comments don't work, trim them.  Don't worry about any other
X# shell scripts, Configure will trim # comments from them for you.
X#
X# (If you are trying to port this package to a machine without sh, I would
X# suggest you cut out the prototypical config.h from the end of Configure
X# and edit it to reflect your system.  Some packages may include samples
X# of config.h for certain machines, so you might look for one of those.)
X#
X# $Header: Configure,v 2.0.1.1 88/06/03 15:00:13 lwall Locked $
X#
X# Yes, you may rip this off to use in other distribution packages.
X# (Note: this Configure script was generated automatically.  Rather than
X# working with this copy of Configure, you may wish to get metaconfig.)
X
X: sanity checks
XPATH='.:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc'
Xexport PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh $0; kill $$)
X
Xif test ! -t 0; then
X    echo "Say 'sh Configure', not 'sh <Configure'"
X    exit 1
Xfi
X
X(alias) >/dev/null 2>&1 && \
X    echo "(I see you are using the Korn shell.  Some ksh's blow up on Configure," && \
X    echo "especially on exotic machines.  If yours does, try the Bourne shell instead.)"
X
Xif test ! -d ../UU; then
X    if test ! -d UU; then
X	mkdir UU
X    fi
X    cd UU
Xfi
X
Xcase "$1" in
X-d) shift; fastread='yes';;
Xesac
X
Xd_eunice=''
Xeunicefix=''
Xdefine=''
Xloclist=''
Xexpr=''
Xsed=''
Xecho=''
Xcat=''
Xrm=''
Xmv=''
Xcp=''
Xtail=''
Xtr=''
Xmkdir=''
Xsort=''
Xuniq=''
Xgrep=''
Xtrylist=''
Xtest=''
Xinews=''
Xegrep=''
Xmore=''
Xpg=''
XMcc=''
Xvi=''
Xmailx=''
Xmail=''
Xcpp=''
XLog=''
XHeader=''
Xbin=''
Xcc=''
Xcontains=''
Xcppstdin=''
Xcppminus=''
Xd_charsprf=''
Xd_flexfnam=''
Xd_index=''
Xd_voidsig=''
Xlibc=''
Xmansrc=''
Xmanext=''
Xmodels=''
Xsplit=''
Xsmall=''
Xmedium=''
Xlarge=''
Xhuge=''
Xccflags=''
Xldflags=''
Xn=''
Xc=''
Xpackage=''
Xregisters=''
Xreg1=''
Xreg2=''
Xreg3=''
Xreg4=''
Xreg5=''
Xreg6=''
Xreg7=''
Xreg8=''
Xreg9=''
Xreg10=''
Xreg11=''
Xreg12=''
Xreg13=''
Xreg14=''
Xreg15=''
Xreg16=''
Xspitshell=''
Xshsharp=''
Xsharpbang=''
Xstartsh=''
Xvoidflags=''
Xdefvoidused=''
XCONFIG=''
X
X: set package name
Xpackage=patch
X
Xecho " "
Xecho "Beginning of configuration questions for $package kit."
X: Eunice requires " " instead of "", can you believe it
Xecho " "
X
Xdefine='define'
Xundef='/*undef'
Xlibpth='/usr/lib /usr/local/lib /lib'
Xsmallmach='pdp11 i8086 z8000 i80286 iAPX286'
Xrmlist='kit[1-9]isdone kit[1-9][0-9]isdone'
Xtrap 'echo " "; rm -f $rmlist; exit 1' 1 2 3
X
X: We must find out about Eunice early
Xeunicefix=':'
Xif test -f /etc/unixtovms; then
X    eunicefix=/etc/unixtovms
Xfi
Xif test -f /etc/unixtovms.exe; then
X    eunicefix=/etc/unixtovms.exe
Xfi
X
Xattrlist="mc68000 sun gcos unix ibm gimpel interdata tss os mert pyr"
Xattrlist="$attrlist vax pdp11 i8086 z8000 u3b2 u3b5 u3b20 u3b200"
Xattrlist="$attrlist ns32000 ns16000 iAPX286 mc300 mc500 mc700 sparc"
Xattrlist="$attrlist nsc32000 sinix xenix venix posix ansi M_XENIX"
Xattrlist="$attrlist $mc68k __STDC__"
Xpth="/usr/ucb /bin /usr/bin /usr/local /usr/local/bin /usr/lbin /etc /usr/lib /lib"
Xd_newshome="../../NeWS"
Xdefvoidused=7
X
X: some greps do not return status, grrr.
Xecho "grimblepritz" >grimble
Xif grep blurfldyick grimble >/dev/null 2>&1 ; then
X    contains=contains
Xelif grep grimblepritz grimble >/dev/null 2>&1 ; then
X    contains=grep
Xelse
X    contains=contains
Xfi
Xrm -f grimble
X: the following should work in any shell
Xcase "$contains" in
Xcontains*)
X    echo " "
X    echo "AGH!  Grep doesn't return a status.  Attempting remedial action."
X    cat >contains <<'EOSS'
Xgrep "$1" "$2" >.greptmp && cat .greptmp && test -s .greptmp
XEOSS
Xchmod +x contains
Xesac
X
X: first determine how to suppress newline on echo command
Xecho "Checking echo to see how to suppress newlines..."
X(echo "hi there\c" ; echo " ") >.echotmp
Xif $contains c .echotmp >/dev/null 2>&1 ; then
X    echo "...using -n."
X    n='-n'
X    c=''
Xelse
X    cat <<'EOM'
X...using \c
XEOM
X    n=''
X    c='\c'
Xfi
Xecho $n "Type carriage return to continue.  Your cursor should be here-->$c"
Xread ans
Xrm -f .echotmp
X
X: now set up to do reads with possible shell escape and default assignment
Xcat <<EOSC >myread
Xcase "\$fastread" in
Xyes) ans=''; echo " " ;;
X*) ans='!';;
Xesac
Xwhile expr "X\$ans" : "X!" >/dev/null; do
X    read ans
X    case "\$ans" in
X    !)
X	sh
X	echo " "
X	echo $n "\$rp $c"
X	;;
X    !*)
X	set \`expr "X\$ans" : "X!\(.*\)\$"\`
X	sh -c "\$*"
X	echo " "
X	echo $n "\$rp $c"
X	;;
X    esac
Xdone
Xrp='Your answer:'
Xcase "\$ans" in
X'') ans="\$dflt";;
Xesac
XEOSC
X
X: general instructions
Xcat <<EOH
X 
XThis installation shell script will examine your system and ask you questions
Xto determine how the $package package should be installed.  If you get stuck
Xon a question, you may use a ! shell escape to start a subshell or execute
Xa command.  Many of the questions will have default answers in square
Xbrackets--typing carriage return will give you the default.
X
XOn some of the questions which ask for file or directory names you are
Xallowed to use the ~name construct to specify the login directory belonging
Xto "name", even if you don't have a shell which knows about that.  Questions
Xwhere this is allowed will be marked "(~name ok)".
X
XEOH
Xrp="[Type carriage return to continue]"
Xecho $n "$rp $c"
X. myread
Xcat <<EOH
X
XMuch effort has been expended to ensure that this shell script will run
Xon any Unix system.  If despite that it blows up on you, your best bet is
Xto edit Configure and run it again. Also, let me (lwall@jpl-devvax.jpl.nasa.gov)
Xknow how I blew it.  If you can't run Configure for some reason, you'll have
Xto generate a config.sh file by hand.
X
XThis installation script affects things in two ways: 1) it may do direct
Xvariable substitutions on some of the files included in this kit, and
X2) it builds a config.h file for inclusion in C programs.  You may edit
Xany of these files as the need arises after running this script.
X
XIf you make a mistake on a question, there is no easy way to back up to it
Xcurrently.  The easiest thing to do is to edit config.sh and rerun all the
XSH files.  Configure will offer to let you do this before it runs the SH files.
X
XEOH
Xrp="[Type carriage return to continue]"
Xecho $n "$rp $c"
X. myread
X
X: get old answers, if there is a config file out there
Xif test -f ../config.sh; then
X    echo " "
X    dflt=y
X    rp="I see a config.sh file.  Did Configure make it on THIS system? [$dflt]"
X    echo $n "$rp $c"
X    . myread
X    case "$ans" in
X    n*) echo "OK, I'll ignore it.";;
X    *)  echo "Fetching default answers from your old config.sh file..."
X	tmp="$n"
X	ans="$c"
X        . ../config.sh
X	n="$tmp"
X	c="$ans"
X	;;
X    esac
Xfi
X
X: find out where common programs are
Xecho " "
Xecho "Locating common programs..."
Xcat <<EOSC >loc
X$startsh
Xcase \$# in
X0) exit 1;;
Xesac
Xthing=\$1
Xshift
Xdflt=\$1
Xshift
Xfor dir in \$*; do
X    case "\$thing" in
X    .)
X	if test -d \$dir/\$thing; then
X	    echo \$dir
X	    exit 0
X	fi
X	;;
X    *)
X	if test -f \$dir/\$thing; then
X	    echo \$dir/\$thing
X	    exit 0
X	elif test -f \$dir/\$thing.exe; then
X	    : on Eunice apparently
X	    echo \$dir/\$thing
X	    exit 0
X	fi
X	;;
X    esac
Xdone
Xecho \$dflt
Xexit 1
XEOSC
Xchmod +x loc
X$eunicefix loc
Xloclist="
Xexpr
Xsed
Xecho
Xcat
Xrm
Xtr
Xgrep
X"
Xtrylist="
Xtest
XMcc
Xcpp
X"
Xfor file in $loclist; do
X    xxx=`loc $file $file $pth`
X    eval $file=$xxx
X    eval _$file=$xxx
X    case "$xxx" in
X    /*)
X	echo $file is in $xxx.
X	;;
X    *)
X	echo "I don't know where $file is.  I hope it's in everyone's PATH."
X	;;
X    esac
Xdone
Xecho " "
Xecho "Don't worry if any of the following aren't found..."
Xans=offhand
Xfor file in $trylist; do
X    xxx=`loc $file $file $pth`
X    eval $file=$xxx
X    eval _$file=$xxx
X    case "$xxx" in
X    /*)
X	echo $file is in $xxx.
X	;;
X    *)
X	echo "I don't see $file out there, $ans."
X	ans=either
X	;;
X    esac
Xdone
Xcase "$egrep" in
Xegrep)
X    echo "Substituting grep for egrep."
X    egrep=$grep
X    ;;
Xesac
Xcase "$test" in
Xtest)
X    echo "Hopefully test is built into your sh."
X    ;;
X/bin/test)
X    echo " "
X    dflt=n
X    rp="Is your "'"'"test"'"'" built into sh? [$dflt] (OK to guess)"
X    echo $n "$rp $c"
X    . myread
X    case "$ans" in
X    y*) test=test ;;
X    esac
X    ;;
X*)
X    test=test
X    ;;
Xesac
Xcase "$echo" in
Xecho)
X    echo "Hopefully echo is built into your sh."
X    ;;
X/bin/echo)
X    echo " "
X    echo "Checking compatibility between /bin/echo and builtin echo (if any)..."
X    $echo $n "hi there$c" >foo1
X    echo $n "hi there$c" >foo2
X    if cmp foo1 foo2 >/dev/null 2>&1; then
X	echo "They are compatible.  In fact, they may be identical."
X    else
X	case "$n" in
X	'-n') n='' c='\c' ans='\c' ;;
X	*) n='-n' c='' ans='-n' ;;
X	esac
X	cat <<FOO
XThey are not compatible!  You are probably running ksh on a non-USG system.
XI'll have to use /bin/echo instead of the builtin, since Bourne shell doesn't
Xhave echo built in and we may have to run some Bourne shell scripts.  That
Xmeans I'll have to use $ans to suppress newlines now.  Life is ridiculous.
X
XFOO
X	rp="Your cursor should be here-->"
X	$echo $n "$rp$c"
X	. myread
X    fi
X    $rm -f foo1 foo2
X    ;;
X*)
X    : cross your fingers
X    echo=echo
X    ;;
Xesac
Xrmlist="$rmlist loc"
X
X: get list of predefined functions in a handy place
Xecho " "
Xif test -f /lib/libc.a; then
X    echo "Your C library is in /lib/libc.a.  You're normal."
X    libc=/lib/libc.a
Xelse
X    ans=`loc libc.a blurfl/dyick $libpth`
X    if test ! -f $ans; then
X	ans=`loc clib blurfl/dyick $libpth`
X    fi
X    if test ! -f $ans; then
X	ans=`loc libc blurfl/dyick $libpth`
X    fi
X    if test -f $ans; then
X	echo "Your C library is in $ans, of all places."
X	libc=$ans
X    else
X	if test -f "$libc"; then
X	    echo "Your C library is in $libc, like you said before."
X	else
X	    cat <<EOM
X 
XI can't seem to find your C library.  I've looked in the following places:
X
X	$libpth
X
XNone of these seems to contain your C library.  What is the full name
XEOM
X	    dflt=None
X	    $echo $n "of your C library? $c"
X	    rp='C library full name?'
X	    . myread
X	    libc="$ans"
X	fi
X    fi
Xfi
Xecho " "
X$echo $n "Extracting names from $libc for later perusal...$c"
Xnm $libc 2>/dev/null | sed -n -e 's/^.* T _//p' -e 's/^.* T //p' > libc.list
Xif $contains '^printf$' libc.list >/dev/null 2>&1; then
X    echo "done"
Xelse
X    nm $libc 2>/dev/null | sed -n -e 's/^.* D _//p' -e 's/^.* D //p' > libc.list
X    if $contains '^printf$' libc.list >/dev/null 2>&1; then
X	echo "done"
X    else
X	echo " "
X	echo "nm didn't seem to work right."
X	echo "Trying ar instead..."
X	if ar t $libc | sed -e 's/\.o$//' > libc.list; then
X	    echo "Ok."
X	else
X	    echo "That didn't work either.  Giving up."
X	    exit 1
X	fi
X    fi
Xfi
Xrmlist="$rmlist libc.list"
X
X: make some quick guesses about what we are up against
Xecho " "
X$echo $n "Hmm...  $c"
Xif $contains SIGTSTP /usr/include/signal.h >/dev/null 2>&1 ; then
X    echo "Looks kind of like a BSD system, but we'll see..."
X    echo exit 0 >bsd
X    echo exit 1 >usg
X    echo exit 1 >v7
Xelif $contains '^fcntl$' libc.list >/dev/null 2>&1 ; then
X    echo "Looks kind of like a USG system, but we'll see..."
X    echo exit 1 >bsd
X    echo exit 0 >usg
X    echo exit 1 >v7
Xelse
X    echo "Looks kind of like a version 7 system, but we'll see..."
X    echo exit 1 >bsd
X    echo exit 1 >usg
X    echo exit 0 >v7
Xfi
Xif $contains '^vmssystem$' libc.list >/dev/null 2>&1 ; then
X    cat <<'EOI'
XThere is, however, a strange, musty smell in the air that reminds me of
Xsomething...hmm...yes...I've got it...there's a VMS nearby, or I'm a Blit.
XEOI
X    echo "exit 0" >eunice
X    eunicefix=unixtovms
X    d_eunice="$define"
X: it so happens the Eunice I know will not run shell scripts in Unix format
Xelse
X    echo " "
X    echo "Congratulations.  You aren't running Eunice."
X    eunicefix=':'
X    d_eunice="$undef"
X    echo "exit 1" >eunice
Xfi
Xif test -f /xenix; then
X    echo "Actually, this looks more like a XENIX system..."
X    echo "exit 0" >xenix
Xelse
X    echo " "
X    echo "It's not Xenix..."
X    echo "exit 1" >xenix
Xfi
Xchmod +x xenix
X$eunicefix xenix
Xif test -f /venix; then
X    echo "Actually, this looks more like a VENIX system..."
X    echo "exit 0" >venix
Xelse
X    echo " "
X    if xenix; then
X	: null
X    else
X	echo "Nor is it Venix..."
X    fi
X    echo "exit 1" >venix
Xfi
Xchmod +x bsd usg v7 eunice venix
X$eunicefix bsd usg v7 eunice venix
Xrmlist="$rmlist bsd usg v7 eunice venix xenix"
X
X: see if sh knows # comments
Xecho " "
Xecho "Checking your sh to see if it knows about # comments..."
Xif sh -c '#' >/dev/null 2>&1 ; then
X    echo "Your sh handles # comments correctly."
X    shsharp=true
X    spitshell=cat
X    echo " "
X    echo "Okay, let's see if #! works on this system..."
X    echo "#!/bin/echo hi" > try
X    $eunicefix try
X    chmod +x try
X    try > today
X    if $contains hi today >/dev/null 2>&1; then
X	echo "It does."
X	sharpbang='#!'
X    else
X	echo "#! /bin/echo hi" > try
X	$eunicefix try
X	chmod +x try
X	try > today
X	if test -s today; then
X	    echo "It does."
X	    sharpbang='#! '
X	else
X	    echo "It doesn't."
X	    sharpbang=': use '
X	fi
X    fi
Xelse
X    echo "Your sh doesn't grok # comments--I will strip them later on."
X    shsharp=false
X    echo "exec grep -v '^#'" >spitshell
X    chmod +x spitshell
X    $eunicefix spitshell
X    spitshell=`pwd`/spitshell
X    echo "I presume that if # doesn't work, #! won't work either!"
X    sharpbang=': use '
Xfi
X
X: figure out how to guarantee sh startup
Xecho " "
Xecho "Checking out how to guarantee sh startup..."
Xstartsh=$sharpbang'/bin/sh'
Xecho "Let's see if '$startsh' works..."
Xcat >try <<EOSS
X$startsh
Xset abc
Xtest "$?abc" != 1
XEOSS
X
Xchmod +x try
X$eunicefix try
Xif try; then
X    echo "Yup, it does."
Xelse
X    echo "Nope.  You may have to fix up the shell scripts to make sure sh runs them."
Xfi
Xrm -f try today
X
X: see if sprintf is declared as int or pointer to char
Xecho " "
Xcat >.ucbsprf.c <<'EOF'
Xmain() { char buf[10]; exit((unsigned long)sprintf(buf,"%s","foo") > 10L); }
XEOF
Xif cc .ucbsprf.c -o .ucbsprf >/dev/null 2>&1 && .ucbsprf; then
X    echo "Your sprintf() returns (int)."
X    d_charsprf="$undef"
Xelse
X    echo "Your sprintf() returns (char*)."
X    d_charsprf="$define"
Xfi
X/bin/rm -f .ucbsprf.c .ucbsprf
X
X: see if we can have long filenames
Xecho " "
Xrm -f 123456789abcde
Xif (echo hi >123456789abcdef) 2>/dev/null; then
X    : not version 8
X    if test -f 123456789abcde; then
X	echo 'You cannot have filenames longer than 14 characters.  Sigh.'
X	d_flexfnam="$undef"
X    else
X	echo 'You can have filenames longer than 14 characters.'
X	d_flexfnam="$define"
X    fi
Xelse
X    : version 8 probably
X    echo "You can't have filenames longer than 14 chars.  V8 can't even think about them!"
X    d_flexfnam="$undef"
Xfi 
X: index or strcpy
Xecho " "
Xcase "$d_index" in
Xn) dflt=n;;
X*) dflt=y;;
Xesac
Xif $contains '^index$' libc.list >/dev/null 2>&1 ; then
X    if $contains '^strchr$' libc.list >/dev/null 2>&1 ; then
X	echo "Your system has both index() and strchr().  Shall I use"
X	rp="index() rather than strchr()? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	case "$ans" in
X	    n*) d_index="$define" ;;
X	    *)  d_index="$undef" ;;
X	esac
X    else
X	d_index="$undef"
X	echo "index() found."
X    fi
Xelse
X    if $contains '^strchr$' libc.list >/dev/null 2>&1 ; then
X	d_index="$define"
X	echo "strchr() found."
X    else
X	echo "No index() or strchr() found!"
X	d_index="$undef"
X    fi
Xfi
X
X: see if signal is declared as pointer to function returning int or void
Xecho " "
Xif $contains 'void.*signal' /usr/include/signal.h >/dev/null 2>&1 ; then
X    echo "You have void (*signal())() instead of int."
X    d_voidsig="$define"
Xelse
X    echo "You have int (*signal())() instead of void."
X    d_voidsig="$undef"
Xfi
X
X: check for void type
Xecho " "
X$cat <<EOM
XChecking to see how well your C compiler groks the void type...
X
X  Support flag bits are:
X    1: basic void declarations.
X    2: arrays of pointers to functions returning void.
X    4: operations between pointers to and addresses of void functions.
X
XEOM
Xcase "$voidflags" in
X'')
X    $cat >try.c <<'EOCP'
X#if TRY & 1
Xvoid main() {
X#else
Xmain() {
X#endif
X	extern void *moo();
X	void *(*goo)();
X#if TRY & 2
X	void (*foo[10])();
X#endif
X
X#if TRY & 4
X	if(*goo == moo) {
X		exit(0);
X	}
X#endif
X	exit(0);
X}
XEOCP
X    if cc -S -DTRY=7 try.c >.out 2>&1 ; then
X	voidflags=7
X	echo "It appears to support void fully."
X	if $contains warning .out >/dev/null 2>&1; then
X	    echo "However, you might get some warnings that look like this:"
X	    $cat .out
X	fi
X    else
X	echo "Hmm, you compiler has some difficulty with void.  Checking further..."
X	if cc -S -DTRY=1 try.c >/dev/null 2>&1 ; then
X	    echo "It supports 1..."
X	    if cc -S -DTRY=3 try.c >/dev/null 2>&1 ; then
X		voidflags=3
X		echo "And it supports 2 but not 4."
X	    else
X		echo "It doesn't support 2..."
X		if cc -S -DTRY=3 try.c >/dev/null 2>&1 ; then
X		    voidflags=5
X		    echo "But it supports 4."
X		else
X		    voidflags=1
X		    echo "And it doesn't support 4."
X		fi
X	    fi
X	else
X	    echo "There is no support at all for void."
X	    voidflags=0
X	fi
X    fi
Xesac
Xdflt="$voidflags";
Xrp="Your void support flags add up to what? [$dflt]"
X$echo $n "$rp $c"
X. myread
Xvoidflags="$ans"
X$rm -f try.* .out
X
X: see how we invoke the C preprocessor
Xecho " "
Xecho "Now, how can we feed standard input to your C preprocessor..."
Xcat <<'EOT' >testcpp.c
X#define ABC abc
X#define XYZ xyz
XABC.XYZ
XEOT
Xecho 'Maybe "'$cpp'" will work...'
X$cpp <testcpp.c >testcpp.out 2>&1
Xif $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X    echo "Yup, it does."
X    cppstdin="$cpp"
X    cppminus='';
Xelse
X    echo 'Nope, maybe "'$cpp' -" will work...'
X    $cpp - <testcpp.c >testcpp.out 2>&1
X    if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X	echo "Yup, it does."
X	cppstdin="$cpp"
X	cppminus='-';
X    else
X	echo 'No such luck...maybe "cc -E" will work...'
X	cc -E <testcpp.c >testcpp.out 2>&1
X	if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X	    echo "It works!"
X	    cppstdin='cc -E'
X	    cppminus='';
X	else
X	    echo 'Nixed again...maybe "cc -E -" will work...'
X	    cc -E - <testcpp.c >testcpp.out 2>&1
X	    if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X		echo "Hooray, it works!  I was beginning to wonder."
X		cppstdin='cc -E'
X		cppminus='-';
X	    else
X		echo 'Nope...maybe "cc -P" will work...'
X		cc -P <testcpp.c >testcpp.out 2>&1
X		if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X		    echo "Yup, that does."
X		    cppstdin='cc -P'
X		    cppminus='';
X		else
X		    echo 'Nope...maybe "cc -P -" will work...'
X		    cc -P - <testcpp.c >testcpp.out 2>&1
X		    if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X			echo "Yup, that does."
X			cppstdin='cc -P'
X			cppminus='-';
X		    else
X			echo 'Hmm...perhaps you already told me...'
X			case "$cppstdin" in
X			'') ;;
X			*) $cppstdin $cppminus <testcpp.c >testcpp.out 2>&1;;
X			esac
X			if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X			    echo "Hooray, you did!  I was beginning to wonder."
X			else
X			    echo 'Uh-uh.  Time to get fancy...'
X			    echo 'Trying (cat >/tmp/$$.c; cc -E /tmp/$$.c; rm /tmp/$$.c)'
X			    cppstdin='(cat >/tmp/$$.c; cc -E /tmp/$$.c; rm /tmp/$$.c)'
X			    cppminus='';
X			    $cppstdin <testcpp.c >testcpp.out 2>&1
X			    if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X				echo "Eureka!."
X			    else
X				dflt=blurfl
X				$echo $n "No dice.  I can't find a C preprocessor.  Name one: $c"
X				rp='Name a C preprocessor:'
X				. myread
X				cppstdin="$ans"
X				$cppstdin <testcpp.c >testcpp.out 2>&1
X				if $contains 'abc.xyz' testcpp.out >/dev/null 2>&1 ; then
X				    echo "OK, that will do."
X				else
X				    echo "Sorry, I can't get that to work.  Go find one."
X				    exit 1
X				fi
X			    fi
X			fi
X		    fi
X		fi
X	    fi
X	fi
X    fi
Xfi
Xrm -f testcpp.c testcpp.out
X
X: get C preprocessor symbols handy
Xecho " "
Xecho $attrlist | $tr '[ - ]' '[\012-\012]' >Cppsym.know
X$cat <<EOSS >Cppsym
X$startsh
Xcase "\$1" in
X-l) list=true
X    shift
X    ;;
Xesac
Xunknown=''
Xcase "\$list\$#" in
X1|2)
X    for sym do
X	if $contains "^\$1$" Cppsym.true >/dev/null 2>&1; then
X	    exit 0
X	elif $contains "^\$1$" Cppsym.know >/dev/null 2>&1; then
X		:
X	else
X	    unknown="\$unknown \$sym"
X	fi
X    done
X    set X \$unknown
X    shift
X    ;;
Xesac
Xcase \$# in
X0) exit 1;;
Xesac
Xecho \$* | $tr '[ - ]' '[\012-\012]' | $sed -e 's/\(.*\)/\\
X#ifdef \1\\
Xexit 0; _ _ _ _\1\\	 \1\\
X#endif\\
X/' >/tmp/Cppsym\$\$
Xecho exit 1 >>/tmp/Cppsym\$\$
X$cppstdin $cppminus </tmp/Cppsym\$\$ >/tmp/Cppsym2\$\$
Xcase "\$list" in
Xtrue) awk 'NF > 5 {print substr(\$6,2,100)}' </tmp/Cppsym2\$\$ ;;
X*)
X    sh /tmp/Cppsym2\$\$
X    status=\$?
X    ;;
Xesac
X$rm -f /tmp/Cppsym\$\$ /tmp/Cppsym2\$\$
Xexit \$status
XEOSS
Xchmod +x Cppsym
X$eunicefix Cppsym
Xecho "Your C preprocessor defines the following symbols:"
XCppsym -l $attrlist >Cppsym.true
Xcat Cppsym.true
Xrmlist="$rmlist Cppsym Cppsym.know Cppsym.true"
X
X: see how many register declarations we want to use
Xcase "$registers" in
X'')
X    if Cppsym vax; then
X	dflt=6
X    elif Cppsym sun mc68000; then
X	dflt=10
X    elif Cppsym pyr; then
X	dflt=14
X    elif Cppsym ns32000 ns16000; then
X	dflt=5
X    elif Cppsym $smallmach; then
X	dflt=3
X    else
X	: if you have any other numbers for me, send them in
X	dflt=6
X    fi
X    ;;
X*)  dflt=$registers ;;
Xesac
Xcat <<EOM
X 
XDifferent C compilers on different machines pay attention to different
Xnumbers of register declarations.  About how many register declarations in
XEOM
X$echo $n "each routine does your C compiler pay attention to? (OK to guess) [$dflt] $c"
Xrp="# register declarations used? [$dflt]"
X. myread
Xregisters=$ans
Xreg1=''
Xawk "BEGIN { for (i=1; i<=16; i++) printf \"reg%d=''\n\", i}" </dev/null >.foo
X. .foo
Xawk "BEGIN { for (i=1; i<=$registers; i++) printf \"reg%d=register\n\", i}" \
X	</dev/null >.foo
X. .foo
Xrm -f .foo
X
X: preserve RCS keywords in files with variable substitution, grrr
XLog='$Log'
XHeader='$Header'
X
X: set up shell script to do ~ expansion
Xcat >filexp <<EOSS
X$startsh
X: expand filename
Xcase "\$1" in
X ~/*|~)
X    echo \$1 | $sed "s|~|\${HOME-\$LOGDIR}|"
X    ;;
X ~*)
X    if $test -f /bin/csh; then
X	/bin/csh -f -c "glob \$1"
X	echo ""
X    else
X	name=\`$expr x\$1 : '..\([^/]*\)'\`
X	dir=\`$sed -n -e "/^\${name}:/{s/^[^:]*:[^:]*:[^:]*:[^:]*:[^:]*:\([^:]*\).*"'\$'"/\1/" -e p -e q -e '}' </etc/passwd\`
X	if $test ! -d "\$dir"; then
X	    me=\`basename \$0\`
X	    echo "\$me: can't locate home directory for: \$name" >&2
X	    exit 1
X	fi
X	case "\$1" in
X	*/*)
X	    echo \$dir/\`$expr x\$1 : '..[^/]*/\(.*\)'\`
X	    ;;
X	*)
X	    echo \$dir
X	    ;;
X	esac
X    fi
X    ;;
X*)
X    echo \$1
X    ;;
Xesac
XEOSS
Xchmod +x filexp
X$eunicefix filexp
X
X: determine where public executables go
Xcase "$bin" in
X'')
X    dflt=`loc . /bin /usr/local/bin /usr/lbin /usr/local /usr/bin`
X    ;;
X*)  dflt="$bin"
X    ;;
Xesac
Xcont=true
Xwhile $test "$cont" ; do
X    echo " "
X    rp="Where do you want to put the public executables? [$dflt]"
X    $echo $n "$rp $c"
X    . myread
X    bin="$ans"
X    bin=`filexp $bin`
X    if test -d $bin; then
X	cont=''
X    else
X	dflt=n
X	rp="Directory $bin doesn't exist.  Use that name anyway? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	dflt=''
X	case "$ans" in
X	y*) cont='';;
X	esac
X    fi
Xdone
X
X: determine where manual pages go
Xcase "$mansrc" in
X'')
X    dflt=`loc . /usr/man/man1 /usr/man/mann /usr/man/local/man1 /usr/man/u_man/man1 /usr/man/man1`
X    ;;
X*)  dflt="$mansrc"
X    ;;
Xesac
Xcont=true
Xwhile $test "$cont" ; do
X    echo " "
X    rp="Where do the manual pages (source) go? [$dflt]"
X    $echo $n "$rp $c"
X    . myread
X    mansrc=`filexp "$ans"`
X    if test -d $mansrc; then
X	cont=''
X    else
X	dflt=n
X	rp="Directory $mansrc doesn't exist.  Use that name anyway? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	dflt=''
X	case "$ans" in
X	y*) cont='';;
X	esac
X    fi
Xdone
Xcase "$mansrc" in
X*l)
X    manext=l
X    ;;
X*n)
X    manext=n
X    ;;
X*C)
X    manext=C
X    ;;
X*)
X    manext=1
X    ;;
Xesac
X
X: see what memory models we can support
Xcase "$models" in
X'')
X    if Cppsym pdp11; then
X	dflt='unsplit split'
X    else
X	ans=`loc . X /lib/small /lib/large /usr/lib/small /usr/lib/large /lib/medium /usr/lib/medium /lib/huge`
X	case "$ans" in
X	X) dflt='none';;
X	*)  if $test -d /lib/small || $test -d /usr/lib/small; then
X		dflt='small'
X	    else
X		dflt=''
X	    fi
X	    if $test -d /lib/medium || $test -d /usr/lib/medium; then
X		dflt="$dflt medium"
X	    fi
X	    if $test -d /lib/large || $test -d /usr/lib/large; then
X		dflt="$dflt large"
X	    fi
X	    if $test -d /lib/huge || $test -d /usr/lib/huge; then
X		dflt="$dflt huge"
X	    fi
X	esac
X    fi
X    ;;
X*)  dflt="$models" ;;
Xesac
X$cat <<EOM
X 
XSome systems have different model sizes.  On most systems they are called
Xsmall, medium, large, and huge.  On the PDP11 they are called unsplit and
Xsplit.  If your system doesn't support different memory models, say "none".
XIf you wish to force everything to one memory model, say "none" here and
Xput the appropriate flags later when it asks you for other cc and ld flags.
XVenix systems may wish to put "none" and let the compiler figure things out.
X(In the following question multiple model names should be space separated.)
X
XEOM
Xrp="Which models are supported? [$dflt]"
X$echo $n "$rp $c"
X. myread
Xmodels="$ans"
X
Xcase "$models" in
Xnone)
X    small=''
X    medium=''
X    large=''
X    huge=''
X    unsplit=''
X    split=''
X    ;;
X*split)
X    case "$split" in
X    '') 
X	if $contains '-i' $mansrc/ld.1 >/dev/null 2>&1 || \
X	   $contains '-i' $mansrc/cc.1 >/dev/null 2>&1; then
X	    dflt='-i'
X	else
X	    dflt='none'
X	fi
X	;;
X    *) dflt="$split";;
X    esac
X    rp="What flag indicates separate I and D space? [$dflt]"
X    $echo $n "$rp $c"
X    . myread
X    case "$ans" in
X    none) ans='';;
X    esac
X    split="$ans"
X    unsplit=''
X    ;;
X*large*|*small*|*medium*|*huge*)
X    case "$model" in
X    *large*)
X	case "$large" in
X	'') dflt='-Ml';;
X	*) dflt="$large";;
X	esac
X	rp="What flag indicates large model? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	case "$ans" in
X	none) ans='';
X	esac
X	large="$ans"
X	;;
X    *) large='';;
X    esac
X    case "$model" in
X    *huge*)
X	case "$huge" in
X	'') dflt='-Mh';;
X	*) dflt="$huge";;
X	esac
X	rp="What flag indicates huge model? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	case "$ans" in
X	none) ans='';
X	esac
X	huge="$ans"
X	;;
X    *) huge="$large";;
X    esac
X    case "$model" in
X    *medium*)
X	case "$medium" in
X	'') dflt='-Mm';;
X	*) dflt="$medium";;
X	esac
X	rp="What flag indicates medium model? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	case "$ans" in
X	none) ans='';
X	esac
X	medium="$ans"
X	;;
X    *) medium="$large";;
X    esac
X    case "$model" in
X    *small*)
X	case "$small" in
X	'') dflt='none';;
X	*) dflt="$small";;
X	esac
X	rp="What flag indicates small model? [$dflt]"
X	$echo $n "$rp $c"
X	. myread
X	case "$ans" in
X	none) ans='';
X	esac
X	small="$ans"
X	;;
X    *) small='';;
X    esac
X    ;;
X*)
X    echo "Unrecognized memory models--you may have to edit Makefile.SH"
X    ;;
Xesac
X
Xcase "$ccflags" in
X'') dflt='none';;
X*) dflt="$ccflags";;
Xesac
Xecho " "
Xrp="Any additional cc flags? [$dflt]"
X$echo $n "$rp $c"
X. myread
Xcase "$ans" in
Xnone) ans='';
Xesac
Xccflags="$ans"
X
Xcase "$ldflags" in
X'') if venix; then
X	dflt='-i -z'
X    else
X	dflt='none'
X    fi
X    ;;
X*) dflt="$ldflags";;
Xesac
Xecho " "
Xrp="Any additional ld flags? [$dflt]"
X$echo $n "$rp $c"
X. myread
Xcase "$ans" in
Xnone) ans='';
Xesac
Xldflags="$ans"
X
X: see if we need a special compiler
Xecho " "
Xif usg; then
X    case "$cc" in
X    '')
X	case "$Mcc" in
X	/*) dflt='Mcc'
X	    ;;
X	*)
X	    case "$large" in
X	    -M*)
X		dflt='cc'
X		;;
X	    *)
X		if $contains '\-M' $mansrc/cc.1 >/dev/null 2>&1 ; then
X		    dflt='cc -M'
X		else
X		    dflt='cc'
X		fi
X		;;
X	    esac
X	    ;;
X	esac
X	;;
X    *)  dflt="$cc";;
X    esac
X    $cat <<'EOM'
X 
XOn some systems the default C compiler will not resolve multiple global
Xreferences that happen to have the same name.  On some such systems the
X"Mcc" command may be used to force these to be resolved.  On other systems
Xa "cc -M" command is required.  (Note that the -M flag on other systems
Xindicates a memory model to use!)  What command will force resolution on
XEOM
X    $echo $n "this system? [$dflt] $c"
X    rp="Command to resolve multiple refs? [$dflt]"
X    . myread
X    cc="$ans"
Xelse
X    echo "Not a USG system--assuming cc can resolve multiple definitions."
X    cc=cc
Xfi
X
Xecho " "
Xecho "End of configuration questions."
Xecho " "
X
X: create config.sh file
Xecho " "
Xif test -d ../UU; then
X    cd ..
Xfi
Xecho "Creating config.sh..."
X$spitshell <<EOT >config.sh
X$startsh
X# config.sh
X# This file was produced by running the Configure script.
X
Xd_eunice='$d_eunice'
Xeunicefix='$eunicefix'
Xdefine='$define'
Xloclist='$loclist'
Xexpr='$expr'
Xsed='$sed'
Xecho='$echo'
Xcat='$cat'
Xrm='$rm'
Xmv='$mv'
Xcp='$cp'
Xtail='$tail'
Xtr='$tr'
Xmkdir='$mkdir'
Xsort='$sort'
Xuniq='$uniq'
Xgrep='$grep'
Xtrylist='$trylist'
Xtest='$test'
Xinews='$inews'
Xegrep='$egrep'
Xmore='$more'
Xpg='$pg'
XMcc='$Mcc'
Xvi='$vi'
Xmailx='$mailx'
Xmail='$mail'
Xcpp='$cpp'
XLog='$Log'
XHeader='$Header'
Xbin='$bin'
Xcc='$cc'
Xcontains='$contains'
Xcppstdin='$cppstdin'
Xcppminus='$cppminus'
Xd_charsprf='$d_charsprf'
Xd_flexfnam='$d_flexfnam'
Xd_index='$d_index'
Xd_voidsig='$d_voidsig'
Xlibc='$libc'
Xmansrc='$mansrc'
Xmanext='$manext'
Xmodels='$models'
Xsplit='$split'
Xsmall='$small'
Xmedium='$medium'
Xlarge='$large'
Xhuge='$huge'
Xccflags='$ccflags'
Xldflags='$ldflags'
Xn='$n'
Xc='$c'
Xpackage='$package'
Xregisters='$registers'
Xreg1='$reg1'
Xreg2='$reg2'
Xreg3='$reg3'
Xreg4='$reg4'
Xreg5='$reg5'
Xreg6='$reg6'
Xreg7='$reg7'
Xreg8='$reg8'
Xreg9='$reg9'
Xreg10='$reg10'
Xreg11='$reg11'
Xreg12='$reg12'
Xreg13='$reg13'
Xreg14='$reg14'
Xreg15='$reg15'
Xreg16='$reg16'
Xspitshell='$spitshell'
Xshsharp='$shsharp'
Xsharpbang='$sharpbang'
Xstartsh='$startsh'
Xvoidflags='$voidflags'
Xdefvoidused='$defvoidused'
XCONFIG=true
XEOT
X 
XCONFIG=true
X
Xecho " "
Xdflt=''
Xfastread=''
Xecho "If you didn't make any mistakes, then just type a carriage return here."
Xrp="If you need to edit config.sh, do it as a shell escape here:"
X$echo $n "$rp $c"
X. UU/myread
Xcase "$ans" in
X'') ;;
X*) : in case they cannot read
X    eval $ans;;
Xesac
X. ./config.sh
X
Xecho " "
Xecho "Doing variable substitutions on .SH files..."
Xset x `awk '{print $1}' <MANIFEST | $grep '\.SH'`
Xshift
Xcase $# in
X0) set x *.SH; shift;;
Xesac
Xif test ! -f $1; then
X    shift
Xfi
Xfor file in $*; do
X    case "$file" in
X    */*)
X	dir=`$expr X$file : 'X\(.*\)/'`
X	file=`$expr X$file : 'X.*/\(.*\)'`
X	(cd $dir && . $file)
X	;;
X    *)
X	. $file
X	;;
X    esac
Xdone
Xif test -f config.h.SH; then
X    if test ! -f config.h; then
X	: oops, they left it out of MANIFEST, probably, so do it anyway.
X	. config.h.SH
X    fi
Xfi
X
Xif $contains '^depend:' Makefile >/dev/null 2>&1; then
X    dflt=n
X    $cat <<EOM
X
XNow you need to generate make dependencies by running "make depend".
XYou might prefer to run it in background: "make depend > makedepend.out &"
XIt can take a while, so you might not want to run it right now.
X
XEOM
X    rp="Run make depend now? [$dflt]"
X    $echo $n "$rp $c"
X    . UU/myread
X    case "$ans" in
X    y*) make depend
X	echo "Now you must run a make."
X	;;
X    *)  echo "You must run 'make depend' then 'make'."
X	;;
X    esac
Xelif test -f Makefile; then
X    echo " "
X    echo "Now you must run a make."
Xelse
X    echo "Done."
Xfi
X
X$rm -f kit*isdone
Xcd UU && $rm -f $rmlist
X: end of Configure
SHAR_EOF
if test 30911 -ne "`wc -c < 'Configure'`"
then
	echo shar: "error transmitting 'Configure'" '(should have been 30911 characters)'
fi
chmod +x 'Configure'
fi
echo shar: "extracting 'EXTERN.h'" '(241 characters)'
if test -f 'EXTERN.h'
then
	echo shar: "will not over-write existing file 'EXTERN.h'"
else
sed 's/^X//' << \SHAR_EOF > 'EXTERN.h'
X/* $Header: EXTERN.h,v 2.0 86/09/17 15:35:37 lwall Exp $
X *
X * $Log:	EXTERN.h,v $
X * Revision 2.0  86/09/17  15:35:37  lwall
X * Baseline for netwide release.
X * 
X */
X
X#undef EXT
X#define EXT extern
X
X#undef INIT
X#define INIT(x)
X
X#undef DOINIT
SHAR_EOF
if test 241 -ne "`wc -c < 'EXTERN.h'`"
then
	echo shar: "error transmitting 'EXTERN.h'" '(should have been 241 characters)'
fi
fi
echo shar: "extracting 'INTERN.h'" '(239 characters)'
if test -f 'INTERN.h'
then
	echo shar: "will not over-write existing file 'INTERN.h'"
else
sed 's/^X//' << \SHAR_EOF > 'INTERN.h'
X/* $Header: INTERN.h,v 2.0 86/09/17 15:35:58 lwall Exp $
X *
X * $Log:	INTERN.h,v $
X * Revision 2.0  86/09/17  15:35:58  lwall
X * Baseline for netwide release.
X * 
X */
X
X#undef EXT
X#define EXT
X
X#undef INIT
X#define INIT(x) = x
X
X#define DOINIT
SHAR_EOF
if test 239 -ne "`wc -c < 'INTERN.h'`"
then
	echo shar: "error transmitting 'INTERN.h'" '(should have been 239 characters)'
fi
fi
echo shar: "extracting 'MANIFEST'" '(1196 characters)'
if test -f 'MANIFEST'
then
	echo shar: "will not over-write existing file 'MANIFEST'"
else
sed 's/^X//' << \SHAR_EOF > 'MANIFEST'
XAfter all the patch kits are run you should have the following files:
X
XFilename		Kit Description
X--------		--- -----------
XConfigure                1  A shell script that installs everything system dependent.
XEXTERN.h                 1  Toggle .h files to look foreign.
XINTERN.h                 3  Toggle .h files to look domestic.
XMANIFEST                 3  This list of files.
XMakefile.SH              3  The makefile.
XREADME                   3  Installation instructions.
Xcommon.h                 3  Common definitions.
Xconfig.H                 3  Sample config.h, in case Configure won't run.
Xinp.c                    2  Input file abstract data type routines.
Xinp.h                    3  Public defs for above.
Xpatch.c                  2  The patch program.
Xpatch.man                2  Manual page for patch.
Xpatchlevel.h             3  The patch level of the patch program.
Xpch.c                    1  Patch abstract data type routines.
Xpch.h                    3  Public defs for above.
Xutil.c                   2  Utility routines.
Xutil.h                   1  Public defs for above.
Xversion.c                3  Version number routine.
Xversion.h                3  Public defs for above.
SHAR_EOF
if test 1196 -ne "`wc -c < 'MANIFEST'`"
then
	echo shar: "error transmitting 'MANIFEST'" '(should have been 1196 characters)'
fi
fi
echo shar: "extracting 'Makefile.SH'" '(2183 characters)'
if test -f 'Makefile.SH'
then
	echo shar: "will not over-write existing file 'Makefile.SH'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile.SH'
Xcase $CONFIG in
X    '') . config.sh ;;
Xesac
Xecho "Extracting Makefile (with variable substitutions)"
Xcat >Makefile <<!GROK!THIS!
X# $Header: Makefile.SH,v 2.0.1.1 88/06/03 15:00:48 lwall Locked $
X#
X# $Log:	Makefile.SH,v $
X# Revision 2.0.1.1  88/06/03  15:00:48  lwall
X# patch10: upgraded to match some new metaconfig stuff
X# 
X# Revision 2.0  86/09/17  15:36:15  lwall
X# Baseline for netwide release.
X# 
X# Revision 1.2  86/09/08  14:07:42  lwall
X# Split up patch.c.
X# 
X# Revision 1.1  86/08/01  20:18:35  lwall
X# Initial revision
X# 
X
XCC = $cc
Xbin = $bin
Xmansrc = $mansrc
Xmanext = $manext
XCFLAGS = $ccflags -O
XLDFLAGS = $ldflags
XSMALL = $small
XLARGE = $large $split
X
X!GROK!THIS!
Xcat >>Makefile <<'!NO!SUBS!'
X
Xpublic = patch
Xprivate = 
Xmanpages = patch.man
Xutil = Makefile
X
Xc = patch.c pch.c inp.c version.c util.c
X
Xobj = patch.o pch.o inp.o util.o version.o
X
Xlintflags = -phbvxac
X
Xaddedbyconf = Makefile.old bsd config.h config.sh eunice loc pdp11 usg v7
X
X# grrr
XSHELL = /bin/sh
X
X.c.o:
X	$(CC) -c $(CFLAGS) $(LARGE) $*.c
X
Xall: $(public) $(private) $(util)
X	touch all
X
Xpatch: $(obj)
X	$(CC) $(LDFLAGS) $(obj) $(libs) -o patch
X
X# won't work with csh
Xinstall: patch
X	export PATH || exit 1
X	- mv $(bin)/patch $(bin)/patch.old
X	- if test `pwd` != $(bin); then cp $(public) $(bin); fi
X	cd $(bin); chmod 755 $(public)
X	- if test `pwd` != $(mansrc); then \
Xfor page in $(manpages); do \
Xrm -f $(mansrc)/../cat$(manext)/`basename $$page .man`.$(manext); \
Xcp $$page $(mansrc)/`basename $$page .man`.$(manext); \
Xdone; \
Xfi
X
Xclean:
X	rm -f *.o *.orig core
X
Xrealclean:
X	rm -f patch *.o *.orig core $(addedbyconf)
X
X# The following lint has practically everything turned on.  Unfortunately,
X# you have to wade through a lot of mumbo jumbo that can't be suppressed.
X# If the source file has a /*NOSTRICT*/ somewhere, ignore the lint message
X# for that spot.
X
Xlint:
X	lint $(lintflags) $(defs) $(c) > patch.fuzz
X
Xpatch.o: config.h common.h patch.c inp.h pch.h util.h version.h
Xpch.o: config.h common.h pch.c pch.h util.h
Xinp.o: config.h common.h inp.c inp.h util.h
Xutil.o: config.h common.h util.c util.h
Xversion.o: config.h common.h version.c version.h patchlevel.h util.h
X
X!NO!SUBS!
X$eunicefix Makefile
SHAR_EOF
if test 2183 -ne "`wc -c < 'Makefile.SH'`"
then
	echo shar: "error transmitting 'Makefile.SH'" '(should have been 2183 characters)'
fi
fi
echo shar: "extracting 'README'" '(2963 characters)'
if test -f 'README'
then
	echo shar: "will not over-write existing file 'README'"
else
sed 's/^X//' << \SHAR_EOF > 'README'
X			Patch Kit, Version 2.0
X
X		    Copyright (c) 1986, Larry Wall
X
XYou may copy the patch kit in whole or in part as long as you don't try to
Xmake money off it, or pretend that you wrote it.
X--------------------------------------------------------------------------
X
XPlease read all the directions below before you proceed any further, and
Xthen follow them carefully.  Failure to do so may void your warranty. :-)
X
XAfter you have unpacked your kit, you should have all the files listed
Xin MANIFEST.
X
XInstallation
X
X1)  Run Configure.  This will figure out various things about your system.
X    Some things Configure will figure out for itself, other things it will
X    ask you about.  It will then proceed to make config.h, config.sh, and
X    Makefile.
X
X    You might possibly have to trim # comments from the front of Configure
X    if your sh doesn't handle them, but all other # comments will be taken
X    care of.
X
X    If you don't have sh, you'll have to rip the prototype of config.h out
X    of Configure and generate the defines by hand.
X
X2)  Glance through config.h to make sure system dependencies are correct.
X    Most of them should have been taken care of by running the Configure script.
X
X    If you have any additional changes to make to the C definitions, they
X    can be done in the Makefile, or in config.h.  Bear in mind that they may
X    get undone next time you run Configure.
X
X3)  make
X
X    This will attempt to make patch in the current directory.
X
X4)  make install
X
X    This will put patch into a public directory (normally /usr/local/bin).
X    It will also try to put the man pages in a reasonable place.  It will not
X    nroff the man page, however.
X
X5)  Read the manual entry before running patch.
X
X6)  IMPORTANT!  Help save the world!  Communicate any problems and
X    suggested patches to me, lwall@sdcrdcf.UUCP (Larry Wall), so we can
X    keep the world in sync.  If you have a problem, there's someone else
X    out there who either has had or will have the same problem.
X
X    If possible, send in patches such that the patch program will apply them.
X    Context diffs are the best, then normal diffs.  Don't send ed scripts--
X    I've probably changed my copy since the version you have.
X
X    Watch for patch patches in net.sources.bugs.  Patches will generally be
X    in a form usable by the patch program.  If you are just now bringing up
X    patch and aren't sure how many patches there are, write to me and I'll
X    send any you don't have.  Your current patch level is shown in patchlevel.h.
X
X
XNEW FEATURES IN THIS RELEASE
X
X(Correct) support for 4.3bsd-style context diffs.
XFiles can be created from scratch.
XYou can specify a fuzz-factor for context matching.
XYou can force patch to ask no questions.
XYou can specify how much of the leading pathname to strip off filenames.
XUses a Configure script for greater portability.
XYou are now asked if you want to apply a reversed patch.
XNo limit (apart from memory) on the size of hunks.
SHAR_EOF
if test 2963 -ne "`wc -c < 'README'`"
then
	echo shar: "error transmitting 'README'" '(should have been 2963 characters)'
fi
fi
echo shar: "extracting 'common.h'" '(3873 characters)'
if test -f 'common.h'
then
	echo shar: "will not over-write existing file 'common.h'"
else
sed 's/^X//' << \SHAR_EOF > 'common.h'
X/* $Header: common.h,v 2.0.1.1 88/06/03 15:01:56 lwall Locked $
X *
X * $Log:	common.h,v $
X * Revision 2.0.1.1  88/06/03  15:01:56  lwall
X * patch10: support for shorter extensions.
X * 
X * Revision 2.0  86/09/17  15:36:39  lwall
X * Baseline for netwide release.
X * 
X */
X
X#define DEBUGGING
X
X#include "config.h"
X
X/* shut lint up about the following when return value ignored */
X
X#define Signal (void)signal
X#define Unlink (void)unlink
X#define Lseek (void)lseek
X#define Fseek (void)fseek
X#define Fstat (void)fstat
X#define Pclose (void)pclose
X#define Close (void)close
X#define Fclose (void)fclose
X#define Fflush (void)fflush
X#define Sprintf (void)sprintf
X#define Mktemp (void)mktemp
X#define Strcpy (void)strcpy
X#define Strcat (void)strcat
X
X#include <stdio.h>
X#include <assert.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <ctype.h>
X#include <signal.h>
X
X/* constants */
X
X#define TRUE (1)
X#define FALSE (0)
X
X#define MAXHUNKSIZE 100000		/* is this enough lines? */
X#define INITHUNKMAX 125			/* initial dynamic allocation size */
X#define MAXLINELEN 1024
X#define BUFFERSIZE 1024
X#define SCCSPREFIX "s."
X#define GET "get -e %s"
X#define RCSSUFFIX ",v"
X#define CHECKOUT "co -l %s"
X
X#ifdef FLEXFILENAMES
X#define ORIGEXT ".orig"
X#define REJEXT ".rej"
X#else
X#define ORIGEXT "~"
X#define REJEXT "#"
X#endif
X
X/* handy definitions */
X
X#define Null(t) ((t)0)
X#define Nullch Null(char *)
X#define Nullfp Null(FILE *)
X#define Nulline Null(LINENUM)
X
X#define Ctl(ch) ((ch) & 037)
X
X#define strNE(s1,s2) (strcmp(s1, s2))
X#define strEQ(s1,s2) (!strcmp(s1, s2))
X#define strnNE(s1,s2,l) (strncmp(s1, s2, l))
X#define strnEQ(s1,s2,l) (!strncmp(s1, s2, l))
X
X/* typedefs */
X
Xtypedef char bool;
Xtypedef long LINENUM;			/* must be signed */
Xtypedef unsigned MEM;			/* what to feed malloc */
X
X/* globals */
X
XEXT int Argc;				/* guess */
XEXT char **Argv;
XEXT int Argc_last;			/* for restarting plan_b */
XEXT char **Argv_last;
X
XEXT struct stat filestat;		/* file statistics area */
XEXT int filemode INIT(0644);
X
XEXT char buf[MAXLINELEN];		/* general purpose buffer */
XEXT FILE *ofp INIT(Nullfp);		/* output file pointer */
XEXT FILE *rejfp INIT(Nullfp);		/* reject file pointer */
X
XEXT bool using_plan_a INIT(TRUE);	/* try to keep everything in memory */
XEXT bool out_of_mem INIT(FALSE);	/* ran out of memory in plan a */
X
X#define MAXFILEC 2
XEXT int filec INIT(0);			/* how many file arguments? */
XEXT char *filearg[MAXFILEC];
XEXT bool ok_to_create_file INIT(FALSE);
XEXT char *bestguess INIT(Nullch);	/* guess at correct filename */
X
XEXT char *outname INIT(Nullch);
XEXT char rejname[128];
X
XEXT char *origext INIT(Nullch);
XEXT char *origprae INIT(Nullch);
X
XEXT char TMPOUTNAME[] INIT("/tmp/patchoXXXXXX");
XEXT char TMPINNAME[] INIT("/tmp/patchiXXXXXX");	/* might want /usr/tmp here */
XEXT char TMPREJNAME[] INIT("/tmp/patchrXXXXXX");
XEXT char TMPPATNAME[] INIT("/tmp/patchpXXXXXX");
XEXT bool toutkeep INIT(FALSE);
XEXT bool trejkeep INIT(FALSE);
X
XEXT LINENUM last_offset INIT(0);
X#ifdef DEBUGGING
XEXT int debug INIT(0);
X#endif
XEXT LINENUM maxfuzz INIT(2);
XEXT bool force INIT(FALSE);
XEXT bool verbose INIT(TRUE);
XEXT bool reverse INIT(FALSE);
XEXT bool noreverse INIT(FALSE);
XEXT bool skip_rest_of_patch INIT(FALSE);
XEXT int strippath INIT(957);
XEXT bool canonicalize INIT(FALSE);
X
X#define CONTEXT_DIFF 1
X#define NORMAL_DIFF 2
X#define ED_DIFF 3
X#define NEW_CONTEXT_DIFF 4
XEXT int diff_type INIT(0);
X
XEXT bool do_defines INIT(FALSE);	/* patch using ifdef, ifndef, etc. */
XEXT char if_defined[128];		/* #ifdef xyzzy */
XEXT char not_defined[128];		/* #ifndef xyzzy */
XEXT char else_defined[] INIT("#else\n");/* #else */
XEXT char end_defined[128];		/* #endif xyzzy */
X
XEXT char *revision INIT(Nullch);	/* prerequisite revision, if any */
X
Xchar *malloc();
Xchar *realloc();
Xchar *strcpy();
Xchar *strcat();
Xlong atol();
Xlong lseek();
Xchar *mktemp();
X#ifdef CHARSPRINTF
Xchar *sprintf();
X#else
Xint *sprintf();
X#endif
SHAR_EOF
if test 3873 -ne "`wc -c < 'common.h'`"
then
	echo shar: "error transmitting 'common.h'" '(should have been 3873 characters)'
fi
fi
echo shar: "extracting 'config.H'" '(657 characters)'
if test -f 'config.H'
then
	echo shar: "will not over-write existing file 'config.H'"
else
sed 's/^X//' << \SHAR_EOF > 'config.H'
X/* config.h
X * This file was produced by running the Configure script.
X * Feel free to modify any of this as the need arises.
X */
X
X
X#/*undef	EUNICE		/* no file linking? */
X#/*undef	VMS
X
X#/*undef	index strchr	/* cultural */
X#/*undef	rindex strrchr	/*  differences? */
X
X#/*undef	void int	/* is void to be avoided? */
X
X/* How many register declarations are paid attention to? */
X
X#define Reg1 register
X#define Reg2 register
X#define Reg3 register
X#define Reg4 register
X#define Reg5 register
X#define Reg6 register
X#define Reg7 
X#define Reg8 
X#define Reg9 
X#define Reg10 
X#define Reg11 
X#define Reg12 
X#define Reg13 
X#define Reg14 
X#define Reg15 
X#define Reg16 
X
SHAR_EOF
if test 657 -ne "`wc -c < 'config.H'`"
then
	echo shar: "error transmitting 'config.H'" '(should have been 657 characters)'
fi
fi
exit 0
#	End of shell archive

egray@fthood.UUCP (06/07/88)

This is part 2 of 3 to Larry Wall's patch 2.0-11

Emmet P. Gray				US Army, HQ III Corps & Fort Hood
...!ihnp4!uiucuxc!fthood!egray		Attn: AFZF-DE-ENV
					Directorate of Engineering & Housing
					Environmental Management Office
					Fort Hood, TX 76544-5057

-------------------------------------------------------------------------------
#! /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:
#	config.h.SH
#	inp.c
#	inp.h
#	malloc.c
#	patch.c
#	util.h
#	version.c
#	version.h
# This archive created: Tue Jun  7 11:32:44 1988
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'config.h.SH'" '(4619 characters)'
if test -f 'config.h.SH'
then
	echo shar: "will not over-write existing file 'config.h.SH'"
else
sed 's/^X//' << \SHAR_EOF > 'config.h.SH'
Xcase $CONFIG in
X'')
X    if test ! -f config.sh; then
X	ln ../config.sh . || \
X	ln ../../config.sh . || \
X	ln ../../../config.sh . || \
X	(echo "Can't find config.sh."; exit 1)
X	echo "Using config.sh from above..."
X    fi
X    . ./config.sh
X    ;;
Xesac
Xecho "Extracting config.h (with variable substitutions)"
Xcat <<!GROK!THIS! >config.h
X/* config.h
X * This file was produced by running the config.h.SH script, which
X * gets its values from config.sh, which is generally produced by
X * running Configure.
X *
X * Feel free to modify any of this as the need arises.  Note, however,
X * that running config.h.SH again will wipe out any changes you've made.
X * For a more permanent change edit config.sh and rerun config.h.SH.
X */
X
X
X/* EUNICE:
X *	This symbol, if defined, indicates that the program is being compiled
X *	under the EUNICE package under VMS.  The program will need to handle
X *	things like files that don't go away the first time you unlink them,
X *	due to version numbering.  It will also need to compensate for lack
X *	of a respectable link() command.
X */
X/* VMS:
X *	This symbol, if defined, indicates that the program is running under
X *	VMS.  It is currently only set in conjunction with the EUNICE symbol.
X */
X#$d_eunice	EUNICE		/**/
X#$d_eunice	VMS		/**/
X
X/* CPPSTDIN:
X *	This symbol contains the first part of the string which will invoke
X *	the C preprocessor on the standard input and produce to standard
X *	output.	 Typical value of "cc -E" or "/lib/cpp".
X */
X/* CPPMINUS:
X *	This symbol contains the second part of the string which will invoke
X *	the C preprocessor on the standard input and produce to standard
X *	output.  This symbol will have the value "-" if CPPSTDIN needs a minus
X *	to specify standard input, otherwise the value is "".
X */
X#define CPPSTDIN "$cppstdin"
X#define CPPMINUS "$cppminus"
X
X/* CHARSPRINTF:
X *	This symbol is defined if this system declares "char *sprintf()" in
X *	stdio.h.  The trend seems to be to declare it as "int sprintf()".  It
X *	is up to the package author to declare sprintf correctly based on the
X *	symbol.
X */
X#$d_charsprf	CHARSPRINTF 	/**/
X
X/* FLEXFILENAMES:
X *	This symbol, if defined, indicates that the system supports filenames
X *	longer than 14 characters.
X */
X#$d_flexfnam	FLEXFILENAMES		/**/
X
X/* index:
X *	This preprocessor symbol is defined, along with rindex, if the system
X *	uses the strchr and strrchr routines instead.
X */
X/* rindex:
X *	This preprocessor symbol is defined, along with index, if the system
X *	uses the strchr and strrchr routines instead.
X */
X#$d_index	index strchr	/* cultural */
X#$d_index	rindex strrchr	/*  differences? */
X
X/* VOIDSIG:
X *	This symbol is defined if this system declares "void (*signal())()" in
X *	signal.h.  The old way was to declare it as "int (*signal())()".  It
X *	is up to the package author to declare things correctly based on the
X *	symbol.
X */
X#$d_voidsig	VOIDSIG 	/**/
X
X/* Reg1:
X *	This symbol, along with Reg2, Reg3, etc. is either the word "register"
X *	or null, depending on whether the C compiler pays attention to this
X *	many register declarations.  The intent is that you don't have to
X *	order your register declarations in the order of importance, so you
X *	can freely declare register variables in sub-blocks of code and as
X *	function parameters.  Do not use Reg<n> more than once per routine.
X */
X
X#define Reg1 $reg1		/**/
X#define Reg2 $reg2		/**/
X#define Reg3 $reg3		/**/
X#define Reg4 $reg4		/**/
X#define Reg5 $reg5		/**/
X#define Reg6 $reg6		/**/
X#define Reg7 $reg7		/**/
X#define Reg8 $reg8		/**/
X#define Reg9 $reg9		/**/
X#define Reg10 $reg10		/**/
X#define Reg11 $reg11		/**/
X#define Reg12 $reg12		/**/
X#define Reg13 $reg13		/**/
X#define Reg14 $reg14		/**/
X#define Reg15 $reg15		/**/
X#define Reg16 $reg16		/**/
X
X/* VOIDFLAGS:
X *	This symbol indicates how much support of the void type is given by this
X *	compiler.  What various bits mean:
X *
X *	    1 = supports declaration of void
X *	    2 = supports arrays of pointers to functions returning void
X *	    4 = supports comparisons between pointers to void functions and
X *		    addresses of void functions
X *
X *	The package designer should define VOIDUSED to indicate the requirements
X *	of the package.  This can be done either by #defining VOIDUSED before
X *	including config.h, or by defining defvoidused in Myinit.U.  If the
X *	level of void support necessary is not present, defines void to int.
X */
X#ifndef VOIDUSED
X#define VOIDUSED $defvoidused
X#endif
X#define VOIDFLAGS $voidflags
X#if (VOIDFLAGS & VOIDUSED) != VOIDUSED
X#$define void int		/* is void to be avoided? */
X#$define M_VOID		/* Xenix strikes again */
X#endif
X
X!GROK!THIS!
SHAR_EOF
if test 4619 -ne "`wc -c < 'config.h.SH'`"
then
	echo shar: "error transmitting 'config.h.SH'" '(should have been 4619 characters)'
fi
fi
echo shar: "extracting 'inp.c'" '(8134 characters)'
if test -f 'inp.c'
then
	echo shar: "will not over-write existing file 'inp.c'"
else
sed 's/^X//' << \SHAR_EOF > 'inp.c'
X/* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
X *
X * $Log:	inp.c,v $
X * Revision 2.0.1.1  88/06/03  15:06:13  lwall
X * patch10: made a little smarter about sccs files
X * 
X * Revision 2.0  86/09/17  15:37:02  lwall
X * Baseline for netwide release.
X * 
X */
X
X#include "EXTERN.h"
X#include "common.h"
X#include "util.h"
X#include "pch.h"
X#include "INTERN.h"
X#include "inp.h"
X
X/* Input-file-with-indexable-lines abstract type */
X
Xstatic long i_size;			/* size of the input file */
Xstatic char *i_womp;			/* plan a buffer for entire file */
Xstatic char **i_ptr;			/* pointers to lines in i_womp */
X
Xstatic int tifd = -1;			/* plan b virtual string array */
Xstatic char *tibuf[2];			/* plan b buffers */
Xstatic LINENUM tiline[2] = {-1, -1};	/* 1st line in each buffer */
Xstatic LINENUM lines_per_buf;		/* how many lines per buffer */
Xstatic int tireclen;			/* length of records in tmp file */
X
X/* New patch--prepare to edit another file. */
X
Xvoid
Xre_input()
X{
X    if (using_plan_a) {
X	i_size = 0;
X#ifndef lint
X	if (i_ptr != Null(char**))
X	    free((char *)i_ptr);
X#endif
X	if (i_womp != Nullch)
X	    free(i_womp);
X	i_womp = Nullch;
X	i_ptr = Null(char **);
X    }
X    else {
X	using_plan_a = TRUE;		/* maybe the next one is smaller */
X	Close(tifd);
X	tifd = -1;
X	free(tibuf[0]);
X	free(tibuf[1]);
X	tibuf[0] = tibuf[1] = Nullch;
X	tiline[0] = tiline[1] = -1;
X	tireclen = 0;
X    }
X}
X
X/* Constuct the line index, somehow or other. */
X
Xvoid
Xscan_input(filename)
Xchar *filename;
X{
X    if (!plan_a(filename))
X	plan_b(filename);
X    if (verbose) {
X	say3("Patching file %s using Plan %s...\n", filename,
X	  (using_plan_a ? "A" : "B") );
X    }
X}
X
X/* Try keeping everything in memory. */
X
Xbool
Xplan_a(filename)
Xchar *filename;
X{
X    int ifd;
X    Reg1 char *s;
X    Reg2 LINENUM iline;
X
X    if (ok_to_create_file && stat(filename, &filestat) < 0) {
X	if (verbose)
X	    say2("(Creating file %s...)\n",filename);
X	makedirs(filename, TRUE);
X	close(creat(filename, 0666));
X    }
X    if (stat(filename, &filestat) < 0) {
X	Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
X	if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
X	    Sprintf(buf, CHECKOUT, filename);
X	    if (verbose)
X		say2("Can't find %s--attempting to check it out from RCS.\n",
X		    filename);
X	    if (system(buf) || stat(filename, &filestat))
X		fatal2("Can't check out %s.\n", filename);
X	}
X	else {
X	    Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
X	    if (stat(s=buf+20, &filestat) >= 0 ||
X	      stat(s=buf+25, &filestat) >= 0) {
X		Sprintf(buf, GET, s);
X		if (verbose)
X		    say2("Can't find %s--attempting to get it from SCCS.\n",
X			filename);
X		if (system(buf) || stat(filename, &filestat))
X		    fatal2("Can't get %s.\n", filename);
X	    }
X	    else
X		fatal2("Can't find %s.\n", filename);
X	}
X    }
X    filemode = filestat.st_mode;
X    if ((filemode & S_IFMT) & ~S_IFREG)
X	fatal2("%s is not a normal file--can't patch.\n", filename);
X    i_size = filestat.st_size;
X    if (out_of_mem) {
X	set_hunkmax();		/* make sure dynamic arrays are allocated */
X	out_of_mem = FALSE;
X	return FALSE;			/* force plan b because plan a bombed */
X    }
X#ifdef lint
X    i_womp = Nullch;
X#else
X    i_womp = malloc((MEM)(i_size+2));	/* lint says this may alloc less than */
X					/* i_size, but that's okay, I think. */
X#endif
X    if (i_womp == Nullch)
X	return FALSE;
X    if ((ifd = open(filename, 0)) < 0)
X	fatal2("Can't open file %s\n", filename);
X#ifndef lint
X    if (read(ifd, i_womp, (int)i_size) != i_size) {
X	Close(ifd);	/* probably means i_size > 15 or 16 bits worth */
X	free(i_womp);	/* at this point it doesn't matter if i_womp was */
X	return FALSE;	/*   undersized. */
X    }
X#endif
X    Close(ifd);
X    if (i_size && i_womp[i_size-1] != '\n')
X	i_womp[i_size++] = '\n';
X    i_womp[i_size] = '\0';
X
X    /* count the lines in the buffer so we know how many pointers we need */
X
X    iline = 0;
X    for (s=i_womp; *s; s++) {
X	if (*s == '\n')
X	    iline++;
X    }
X#ifdef lint
X    i_ptr = Null(char**);
X#else
X    i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
X#endif
X    if (i_ptr == Null(char **)) {	/* shucks, it was a near thing */
X	free((char *)i_womp);
X	return FALSE;
X    }
X    
X    /* now scan the buffer and build pointer array */
X
X    iline = 1;
X    i_ptr[iline] = i_womp;
X    for (s=i_womp; *s; s++) {
X	if (*s == '\n')
X	    i_ptr[++iline] = s+1;	/* these are NOT null terminated */
X    }
X    input_lines = iline - 1;
X
X    /* now check for revision, if any */
X
X    if (revision != Nullch) { 
X	if (!rev_in_string(i_womp)) {
X	    if (force) {
X		if (verbose)
X		    say2(
X"Warning: this file doesn't appear to be the %s version--patching anyway.\n",
X			revision);
X	    }
X	    else {
X		ask2(
X"This file doesn't appear to be the %s version--patch anyway? [n] ",
X		    revision);
X	    if (*buf != 'y')
X		fatal1("Aborted.\n");
X	    }
X	}
X	else if (verbose)
X	    say2("Good.  This file appears to be the %s version.\n",
X		revision);
X    }
X    return TRUE;			/* plan a will work */
X}
X
X/* Keep (virtually) nothing in memory. */
X
Xvoid
Xplan_b(filename)
Xchar *filename;
X{
X    Reg3 FILE *ifp;
X    Reg1 int i = 0;
X    Reg2 int maxlen = 1;
X    Reg4 bool found_revision = (revision == Nullch);
X
X    using_plan_a = FALSE;
X    if ((ifp = fopen(filename, "r")) == Nullfp)
X	fatal2("Can't open file %s\n", filename);
X    if ((tifd = creat(TMPINNAME, 0666)) < 0)
X	fatal2("Can't open file %s\n", TMPINNAME);
X    while (fgets(buf, sizeof buf, ifp) != Nullch) {
X	if (revision != Nullch && !found_revision && rev_in_string(buf))
X	    found_revision = TRUE;
X	if ((i = strlen(buf)) > maxlen)
X	    maxlen = i;			/* find longest line */
X    }
X    if (revision != Nullch) {
X	if (!found_revision) {
X	    if (force) {
X		if (verbose)
X		    say2(
X"Warning: this file doesn't appear to be the %s version--patching anyway.\n",
X			revision);
X	    }
X	    else {
X		ask2(
X"This file doesn't appear to be the %s version--patch anyway? [n] ",
X		    revision);
X		if (*buf != 'y')
X		    fatal1("Aborted.\n");
X	    }
X	}
X	else if (verbose)
X	    say2("Good.  This file appears to be the %s version.\n",
X		revision);
X    }
X    Fseek(ifp, 0L, 0);		/* rewind file */
X    lines_per_buf = BUFFERSIZE / maxlen;
X    tireclen = maxlen;
X    tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
X    tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
X    if (tibuf[1] == Nullch)
X	fatal1("Can't seem to get enough memory.\n");
X    for (i=1; ; i++) {
X	if (! (i % lines_per_buf))	/* new block */
X	    if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
X		fatal1("patch: can't write temp file.\n");
X	if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
X	  == Nullch) {
X	    input_lines = i - 1;
X	    if (i % lines_per_buf)
X		if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
X		    fatal1("patch: can't write temp file.\n");
X	    break;
X	}
X    }
X    Fclose(ifp);
X    Close(tifd);
X    if ((tifd = open(TMPINNAME, 0)) < 0) {
X	fatal2("Can't reopen file %s\n", TMPINNAME);
X    }
X}
X
X/* Fetch a line from the input file, \n terminated, not necessarily \0. */
X
Xchar *
Xifetch(line,whichbuf)
XReg1 LINENUM line;
Xint whichbuf;				/* ignored when file in memory */
X{
X    if (line < 1 || line > input_lines)
X	return "";
X    if (using_plan_a)
X	return i_ptr[line];
X    else {
X	LINENUM offline = line % lines_per_buf;
X	LINENUM baseline = line - offline;
X
X	if (tiline[0] == baseline)
X	    whichbuf = 0;
X	else if (tiline[1] == baseline)
X	    whichbuf = 1;
X	else {
X	    tiline[whichbuf] = baseline;
X#ifndef lint		/* complains of long accuracy */
X	    Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
X#endif
X	    if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
X		fatal2("Error reading tmp file %s.\n", TMPINNAME);
X	}
X	return tibuf[whichbuf] + (tireclen*offline);
X    }
X}
X
X/* True if the string argument contains the revision number we want. */
X
Xbool
Xrev_in_string(string)
Xchar *string;
X{
X    Reg1 char *s;
X    Reg2 int patlen;
X
X    if (revision == Nullch)
X	return TRUE;
X    patlen = strlen(revision);
X    if (strnEQ(string,revision,patlen) && isspace(s[patlen]))
X	return TRUE;
X    for (s = string; *s; s++) {
X	if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
X		isspace(s[patlen+1] )) {
X	    return TRUE;
X	}
X    }
X    return FALSE;
X}
X
SHAR_EOF
if test 8134 -ne "`wc -c < 'inp.c'`"
then
	echo shar: "error transmitting 'inp.c'" '(should have been 8134 characters)'
fi
fi
echo shar: "extracting 'inp.h'" '(473 characters)'
if test -f 'inp.h'
then
	echo shar: "will not over-write existing file 'inp.h'"
else
sed 's/^X//' << \SHAR_EOF > 'inp.h'
X/* $Header: inp.h,v 2.0 86/09/17 15:37:25 lwall Exp $
X *
X * $Log:	inp.h,v $
X * Revision 2.0  86/09/17  15:37:25  lwall
X * Baseline for netwide release.
X * 
X */
X
XEXT LINENUM input_lines INIT(0);	/* how long is input file in lines */
XEXT LINENUM last_frozen_line INIT(0);	/* how many input lines have been */
X					/* irretractibly output */
X
Xbool rev_in_string();
Xvoid scan_input();
Xbool plan_a();			/* returns false if insufficient memory */
Xvoid plan_b();
Xchar *ifetch();
X
SHAR_EOF
if test 473 -ne "`wc -c < 'inp.h'`"
then
	echo shar: "error transmitting 'inp.h'" '(should have been 473 characters)'
fi
fi
echo shar: "extracting 'malloc.c'" '(12262 characters)'
if test -f 'malloc.c'
then
	echo shar: "will not over-write existing file 'malloc.c'"
else
sed 's/^X//' << \SHAR_EOF > 'malloc.c'
X/*
X * @(#)nmalloc.c 1 (Caltech) 2/21/82
X *
X *	U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
X *
X *	Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
X *
X * This is a very fast storage allocator.  It allocates blocks of a small 
X * number of different sizes, and keeps free lists of each size.  Blocks
X * that don't exactly fit are passed up to the next larger size.  In this 
X * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
X * This is designed for use in a program that uses vast quantities of
X * memory, but bombs when it runs out.  To make it a little better, it
X * warns the user when he starts to get near the end.
X *
X * June 84, ACT: modified rcheck code to check the range given to malloc,
X * rather than the range determined by the 2-power used.
X *
X * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
X * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
X * You should call malloc_init to reinitialize after loading dumped Emacs.
X * Call malloc_stats to get info on memory stats if MSTATS turned on.
X * realloc knows how to return same block given, just changing its size,
X * if the power of 2 is correct.
X */
X
X/*
X * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
X * smallest allocatable block is 8 bytes.  The overhead information will
X * go in the first int of the block, and the returned pointer will point
X * to the second.
X *
X#ifdef MSTATS
X * nmalloc[i] is the difference between the number of mallocs and frees
X * for a given block size.
X#endif /* MSTATS */
X */
X
X#define ISALLOC ((char) 0xf7)	/* magic byte that implies allocation */
X#define ISFREE ((char) 0x54)	/* magic byte that implies free block */
X				/* this is for error checking only */
X
Xextern char etext;
X
X/* end of the program; can be changed by calling init_malloc */
Xstatic char *endofpure = &etext;
X
X#ifdef MSTATS
Xstatic int nmalloc[30];
Xstatic int nmal, nfre;
X#endif /* MSTATS */
X
X/* If range checking is not turned on, all we have is a flag indicating
X   whether memory is allocated, an index in nextf[], and a size field; to
X   realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
X   on whether the former can hold the exact size (given the value of
X   'index').  If range checking is on, we always need to know how much space
X   is allocated, so the 'size' field is never used. */
X
Xstruct mhead {
X	char     mh_alloc;	/* ISALLOC or ISFREE */
X	char     mh_index;	/* index in nextf[] */
X/* Remainder are valid only when block is allocated */
X	unsigned short mh_size;	/* size, if < 0x10000 */
X#ifdef rcheck
X	unsigned mh_nbytes;	/* number of bytes allocated */
X	int      mh_magic4;	/* should be == MAGIC4 */
X#endif /* rcheck */
X	};
X
X/* Access free-list pointer of a block.
X  It is stored at block + 4.
X  This is not a field in the mhead structure
X  because we want sizeof (struct mhead)
X  to describe the overhead for when the block is in use,
X  and we do not want the free-list pointer to count in that.  */
X
X#define CHAIN(a) \
X  (*(struct mhead **) (sizeof (char *) + (char *) (a)))
X
X#ifdef rcheck
X
X/* To implement range checking, we write magic values in at the beginning and
X   end of each allocated block, and make sure they are undisturbed whenever a
X   free or a realloc occurs. */
X/* Written in each of the 4 bytes following the block's real space */
X#define MAGIC1 0x55
X/* Written in the 4 bytes before the block's real space */
X#define MAGIC4 0x55555555
X#define ASSERT(p) if (!(p)) botch("p"); else
Xstatic
Xbotch(s)
X	char *s;
X{
X
X	printf("assertion botched: %s\n", s);
X	abort();
X}
X#define EXTRA  4		/* 4 bytes extra for MAGIC1s */
X#else
X#define ASSERT(p)
X#define EXTRA  0
X#endif /* rcheck */
X
X/* nextf[i] is free list of blocks of size 2**(i + 3)  */
X
Xstatic struct mhead *nextf[30];
X
X#ifdef	M_WARN
X/* Number of bytes of writable memory we can expect to be able to get */
Xstatic int  lim_data;
X/* Level number of warnings already issued.
X  0 -- no warnings issued.
X  1 -- 75% warning already issued.
X  2 -- 85% warning already issued.
X*/
Xstatic int  warnlevel;
X#endif /* M_WARN */
X
X/* nonzero once initial bunch of free blocks made */
Xstatic int gotpool;
X
X/* Cause reinitialization based on job parameters;
X  also declare where the end of pure storage is. */
Xmalloc_init (end)
X    char *end; {
X	endofpure = end;
X#ifdef	M_WARN
X	lim_data = 0;
X	warnlevel = 0;
X#endif /* M_WARN */
X	}
X
Xstatic
Xmorecore (nu)			/* ask system for more memory */
X    register int nu; {		/* size index to get more of  */
X	char   *sbrk ();
X	register char  *cp;
X	register int    nblks;
X	register int    siz;
X
X#ifdef	M_WARN
X#ifndef BSD42
X#ifdef USG
X	extern long ulimit ();
X	if (lim_data == 0)		/* find out how much we can get */
X	    lim_data = ulimit (3, 0) - TEXT_START;
X#else	/*HMS: was endif */
X	if (lim_data == 0)		/* find out how much we can get */
X	    lim_data = vlimit (LIM_DATA, -1);
X#endif /* USG */	/HMS:* was not here */
X#else
X	if (lim_data == 0) {
X		struct rlimit   XXrlimit;
X
X		getrlimit (RLIMIT_DATA, &XXrlimit);
X		lim_data = XXrlimit.rlim_cur;}	/* soft limit */
X#endif /* BSD42 */
X#endif /* M_WARN */
X
X	/* On initial startup, get two blocks of each size up to 1k bytes */
X	if (!gotpool)
X	    getpool (), getpool (), gotpool = 1;
X
X	/* Find current end of memory and issue warning if getting near max */
X
X	cp = sbrk (0);
X	siz = cp - endofpure;
X#ifdef	M_WARN
X	switch (warnlevel) {
X	    case 0: 
X		if (siz > (lim_data / 4) * 3) {
X			warnlevel++;
X			malloc_warning ("Warning: past 75% of memory limit");}
X		break;
X	    case 1: 
X		if (siz > (lim_data / 20) * 17) {
X			warnlevel++;
X			malloc_warning ("Warning: past 85% of memory limit");}
X		break;
X	    case 2: 
X		if (siz > (lim_data / 20) * 19) {
X			warnlevel++;
X			malloc_warning ("Warning: past 95% of memory limit");}
X		break;}
X#endif /* M_WARN */
X
X	if ((int) cp & 0x3ff)	/* land on 1K boundaries */
X	    sbrk (1024 - ((int) cp & 0x3ff));
X
X	/* Take at least 2k, and figure out how many blocks of the desired size we're about to get */
X	nblks = 1;
X	if ((siz = nu) < 8)
X	    nblks = 1 << ((siz = 8) - nu);
X
X	if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
X	    return;			/* no more room! */
X	if ((int) cp & 7) {		/* shouldn't happen, but just in case */
X		cp = (char *) (((int) cp + 8) & ~7);
X		nblks--;}
X
X	/* save new header and link the nblks blocks together */
X	nextf[nu] = (struct mhead *) cp;
X	siz = 1 << (nu + 3);
X	while (1) {
X		((struct mhead *) cp) -> mh_alloc = ISFREE;
X		((struct mhead *) cp) -> mh_index = nu;
X		if (--nblks <= 0) break;
X		CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
X		cp += siz;}
X/*	CHAIN ((struct mhead *) cp) = 0;	/* since sbrk() returns cleared core, this is already set */
X	}
X
Xstatic
Xgetpool () {
X	register int nu;
X	register char *cp = sbrk (0);
X
X	if ((int) cp & 0x3ff)	/* land on 1K boundaries */
X	    sbrk (1024 - ((int) cp & 0x3ff));
X
X	/* Get 2k of storage */
X
X	cp = sbrk (04000);
X	if (cp == (char *) -1)
X	    return;
X
X	/* Divide it into an initial 8-word block
X	plus one block of size 2**nu for nu = 3 ... 10.  */
X
X	CHAIN (cp) = nextf[0];
X	nextf[0] = (struct mhead *) cp;
X	((struct mhead *) cp) -> mh_alloc = ISFREE;
X	((struct mhead *) cp) -> mh_index = 0;
X	cp += 8;
X
X	for (nu = 0; nu < 7; nu++) {
X		CHAIN (cp) = nextf[nu];
X		nextf[nu] = (struct mhead *) cp;
X		((struct mhead *) cp) -> mh_alloc = ISFREE;
X		((struct mhead *) cp) -> mh_index = nu;
X		cp += 8 << nu;}}
X
Xchar *
Xmalloc (n)		/* get a block */
X    unsigned n; {
X	register struct  mhead *p;
X	register unsigned int  nbytes;
X	register int    nunits = 0;
X
X	/* Figure out how many bytes are required, rounding up to the nearest
X	multiple of 4, then figure out which nextf[] area to use */
X	nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
X		{
X		register unsigned int   shiftr = (nbytes - 1) >> 2;
X
X		while (shiftr >>= 1)
X		    nunits++;
X		}
X
X	/* If there are no blocks of the appropriate size, go get some */
X	/* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
X	if (nextf[nunits] == 0)
X	    morecore (nunits);
X
X	/* Get one block off the list, and set the new list head */
X	if ((p = nextf[nunits]) == 0)
X	    return 0;
X	nextf[nunits] = CHAIN (p);
X
X	/* Check for free block clobbered */
X	/* If not for this check, we would gobble a clobbered free chain ptr */
X	/* and bomb out on the NEXT allocate of this size block */
X	if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
X#ifdef rcheck
X	    botch ("block on free list clobbered");
X#else
X	    abort ();
X#endif /* rcheck */
X
X	/* Fill in the info, and if range checking, set up the magic numbers */
X	p -> mh_alloc = ISALLOC;
X#ifdef rcheck
X	p -> mh_nbytes = n;
X	p -> mh_magic4 = MAGIC4;
X		{
X		register char  *m = (char *) (p + 1) + n;
X
X		*m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
X		}
X#else
X	p -> mh_size = n;
X#endif /* rcheck */
X#ifdef MSTATS
X	nmalloc[nunits]++;
X	nmal++;
X#endif /* MSTATS */
X	return (char *) (p + 1);}
X
Xfree (mem)
X    char *mem; {
X	register struct mhead *p;
X		{
X		register char *ap = mem;
X
X		ASSERT (ap != 0);
X		p = (struct mhead *) ap - 1;
X		ASSERT (p -> mh_alloc == ISALLOC);
X#ifdef rcheck
X		ASSERT (p -> mh_magic4 == MAGIC4);
X		ap += p -> mh_nbytes;
X		ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
X		ASSERT (*ap++ == MAGIC1); ASSERT (*ap   == MAGIC1);
X#endif /* rcheck */
X		}
X		{
X		register int nunits = p -> mh_index;
X
X		ASSERT (nunits <= 29);
X		p -> mh_alloc = ISFREE;
X		CHAIN (p) = nextf[nunits];
X		nextf[nunits] = p;
X#ifdef MSTATS
X		nmalloc[nunits]--;
X		nfre++;
X#endif /* MSTATS */
X		}
X	}
X
Xchar *
Xrealloc (mem, n)
X    char *mem;
X    register unsigned n; {
X	register struct mhead *p;
X	register unsigned int tocopy;
X	register int nbytes;
X	register int nunits;
X
X	if ((p = (struct mhead *) mem) == 0)
X	    return malloc (n);
X	p--;
X	nunits = p -> mh_index;
X	ASSERT (p -> mh_alloc == ISALLOC);
X#ifdef rcheck
X	ASSERT (p -> mh_magic4 == MAGIC4);
X		{
X		register char *m = mem + (tocopy = p -> mh_nbytes);
X		ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
X		ASSERT (*m++ == MAGIC1); ASSERT (*m   == MAGIC1);
X		}
X#else
X	if (p -> mh_index >= 13)
X	    tocopy = (1 << (p -> mh_index + 3)) - sizeof *p;
X	else
X	    tocopy = p -> mh_size;
X#endif /* rcheck */
X
X	/* See if desired size rounds to same power of 2 as actual size. */
X	nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
X
X	/* If ok, use the same block, just marking its size as changed.  */
X	if (nbytes > (4 << nunits) && nbytes <= (8 << nunits)) {
X#ifdef rcheck
X		register char *m = mem + tocopy;
X		*m++ = 0;  *m++ = 0;  *m++ = 0;  *m++ = 0;
X		p-> mh_nbytes = n;
X		m = mem + n;
X		*m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;  *m++ = MAGIC1;
X#else
X		p -> mh_size = n;
X#endif /* rcheck */
X		return mem;}
X
X	if (n < tocopy)
X	    tocopy = n;
X		{
X		register char *new;
X		void bcopy();	/*HMS: here? */
X
X		if ((new = malloc (n)) == 0)
X		    return 0;
X		bcopy (mem, new, tocopy);
X		free (mem);
X		return new;
X		}
X	}
X
X#ifdef MSTATS
X/* Return statistics describing allocation of blocks of size 2**n. */
X
Xstruct mstats_value {
X	int blocksize;
X	int nfree;
X	int nused;
X	};
X
Xstruct mstats_value
Xmalloc_stats (size)
X    int size; {
X	struct mstats_value v;
X	register int i;
X	register struct mhead *p;
X
X	v.nfree = 0;
X
X	if (size < 0 || size >= 30) {
X		v.blocksize = 0;
X		v.nused = 0;
X		return v;}
X
X	v.blocksize = 1 << (size + 3);
X	v.nused = nmalloc[size];
X
X	for (p = nextf[size]; p; p = CHAIN (p))
X	    v.nfree++;
X
X	return v;}
X#endif
X
X/* how much space is available? */
X
Xunsigned freespace() {
X  	register int i, j;
X  	register struct mhead *p;
X  	register unsigned space = 0;
X	int local;	/* address only is used */
X
X	space = (char *)&local - sbrk(0);	/* stack space */
X
X  	for (i = 0; i < 30; i++) {
X  		for (j = 0, p = nextf[i]; p; p = CHAIN (p), j++) ;
X  		space += j * (1 << (i + 3));}
X
X	return(space);}
X
X/* How big is this cell? */
X
Xunsigned mc_size(cp)
X    char *cp;{
X	register struct mhead *p;
X
X	if ((p = (struct mhead *) cp) == 0) {
X		/*HMS? */
X		}
X	p--;
X#ifdef rcheck
X	return p -> mh_nbytes;
X#else
X	return (1 << (p -> mh_index + 3)) - sizeof *p;
X/**/
X/*	if (p -> mh_index >= 13)
X/*	    return (1 << (p -> mh_index + 3)) - sizeof *p;
X/*	else
X/*	    return p -> mh_size;
X/**/
X#endif /* rcheck */
X	}
X
X/*HMS: Really should use memcpy, if available... */
X
Xvoid bcopy(source, dest, len)
X    register char *source, *dest;
X    register len; {
X	register i;
X	
X	for (i = 0; i < len; i++)
X	    *dest++ = *source++;}
SHAR_EOF
if test 12262 -ne "`wc -c < 'malloc.c'`"
then
	echo shar: "error transmitting 'malloc.c'" '(should have been 12262 characters)'
fi
fi
echo shar: "extracting 'patch.c'" '(18908 characters)'
if test -f 'patch.c'
then
	echo shar: "will not over-write existing file 'patch.c'"
else
sed 's/^X//' << \SHAR_EOF > 'patch.c'
Xchar rcsid[] =
X	"$Header: patch.c,v 2.0.1.5 88/06/03 15:09:37 lwall Locked $";
X
X/* patch - a program to apply diffs to original files
X *
X * Copyright 1986, Larry Wall
X *
X * This program may be copied as long as you don't try to make any
X * money off of it, or pretend that you wrote it.
X *
X * $Log:	patch.c,v $
X * Revision 2.0.1.5  88/06/03  15:09:37  lwall
X * patch10: exit code improved.
X * patch10: better support for non-flexfilenames.
X * 
X * Revision 2.0.1.4  87/02/16  14:00:04  lwall
X * Short replacement caused spurious "Out of sync" message.
X * 
X * Revision 2.0.1.3  87/01/30  22:45:50  lwall
X * Improved diagnostic on sync error.
X * Moved do_ed_script() to pch.c.
X * 
X * Revision 2.0.1.2  86/11/21  09:39:15  lwall
X * Fuzz factor caused offset of installed lines.
X * 
X * Revision 2.0.1.1  86/10/29  13:10:22  lwall
X * Backwards search could terminate prematurely.
X * 
X * Revision 2.0  86/09/17  15:37:32  lwall
X * Baseline for netwide release.
X * 
X * Revision 1.5  86/08/01  20:53:24  lwall
X * Changed some %d's to %ld's.
X * Linted.
X * 
X * Revision 1.4  86/08/01  19:17:29  lwall
X * Fixes for machines that can't vararg.
X * Added fuzz factor.
X * Generalized -p.
X * General cleanup.
X * 
X * 85/08/15 van%ucbmonet@berkeley
X * Changes for 4.3bsd diff -c.
X *
X * Revision 1.3  85/03/26  15:07:43  lwall
X * Frozen.
X * 
X * Revision 1.2.1.9  85/03/12  17:03:35  lwall
X * Changed pfp->_file to fileno(pfp).
X * 
X * Revision 1.2.1.8  85/03/12  16:30:43  lwall
X * Check i_ptr and i_womp to make sure they aren't null before freeing.
X * Also allow ed output to be suppressed.
X * 
X * Revision 1.2.1.7  85/03/12  15:56:13  lwall
X * Added -p option from jromine@uci-750a.
X * 
X * Revision 1.2.1.6  85/03/12  12:12:51  lwall
X * Now checks for normalness of file to patch.
X * 
X * Revision 1.2.1.5  85/03/12  11:52:12  lwall
X * Added -D (#ifdef) option from joe@fluke.
X * 
X * Revision 1.2.1.4  84/12/06  11:14:15  lwall
X * Made smarter about SCCS subdirectories.
X * 
X * Revision 1.2.1.3  84/12/05  11:18:43  lwall
X * Added -l switch to do loose string comparison.
X * 
X * Revision 1.2.1.2  84/12/04  09:47:13  lwall
X * Failed hunk count not reset on multiple patch file.
X * 
X * Revision 1.2.1.1  84/12/04  09:42:37  lwall
X * Branch for sdcrdcf changes.
X * 
X * Revision 1.2  84/11/29  13:29:51  lwall
X * Linted.  Identifiers uniqified.  Fixed i_ptr malloc() bug.  Fixed
X * multiple calls to mktemp().  Will now work on machines that can only
X * read 32767 chars.  Added -R option for diffs with new and old swapped.
X * Various cosmetic changes.
X * 
X * Revision 1.1  84/11/09  17:03:58  lwall
X * Initial revision
X * 
X */
X
X#include "INTERN.h"
X#include "common.h"
X#include "EXTERN.h"
X#include "version.h"
X#include "util.h"
X#include "pch.h"
X#include "inp.h"
X
X/* procedures */
X
Xvoid reinitialize_almost_everything();
Xvoid get_some_switches();
XLINENUM locate_hunk();
Xvoid abort_hunk();
Xvoid apply_hunk();
Xvoid init_output();
Xvoid init_reject();
Xvoid copy_till();
Xvoid spew_output();
Xvoid dump_line();
Xbool patch_match();
Xbool similar();
Xvoid re_input();
Xvoid my_exit();
X
X/* Apply a set of diffs as appropriate. */
X
Xmain(argc,argv)
Xint argc;
Xchar **argv;
X{
X    LINENUM where;
X    LINENUM newwhere;
X    LINENUM fuzz;
X    LINENUM mymaxfuzz;
X    int hunk = 0;
X    int failed = 0;
X    int failtotal = 0;
X    int i;
X
X    setbuf(stderr, serrbuf);
X    for (i = 0; i<MAXFILEC; i++)
X	filearg[i] = Nullch;
X    Mktemp(TMPOUTNAME);
X    Mktemp(TMPINNAME);
X    Mktemp(TMPREJNAME);
X    Mktemp(TMPPATNAME);
X
X    /* parse switches */
X    Argc = argc;
X    Argv = argv;
X    get_some_switches();
X    
X    /* make sure we clean up /tmp in case of disaster */
X    set_signals(0);
X
X    for (
X	open_patch_file(filearg[1]);
X	there_is_another_patch();
X	reinitialize_almost_everything()
X    ) {					/* for each patch in patch file */
X
X	if (outname == Nullch)
X	    outname = savestr(filearg[0]);
X    
X	/* initialize the patched file */
X	if (!skip_rest_of_patch)
X	    init_output(TMPOUTNAME);
X    
X	/* for ed script just up and do it and exit */
X	if (diff_type == ED_DIFF) {
X	    do_ed_script();
X	    continue;
X	}
X    
X	/* initialize reject file */
X	init_reject(TMPREJNAME);
X    
X	/* find out where all the lines are */
X	if (!skip_rest_of_patch)
X	    scan_input(filearg[0]);
X    
X	/* from here on, open no standard i/o files, because malloc */
X	/* might misfire and we can't catch it easily */
X    
X	/* apply each hunk of patch */
X	hunk = 0;
X	failed = 0;
X	out_of_mem = FALSE;
X	while (another_hunk()) {
X	    hunk++;
X	    fuzz = Nulline;
X	    mymaxfuzz = pch_context();
X	    if (maxfuzz < mymaxfuzz)
X		mymaxfuzz = maxfuzz;
X	    if (!skip_rest_of_patch) {
X		do {
X		    where = locate_hunk(fuzz);
X		    if (hunk == 1 && where == Nulline && !force) {
X						/* dwim for reversed patch? */
X			if (!pch_swap()) {
X			    if (fuzz == Nulline)
X				say1(
X"Not enough memory to try swapped hunk!  Assuming unswapped.\n");
X			    continue;
X			}
X			reverse = !reverse;
X			where = locate_hunk(fuzz);  /* try again */
X			if (where == Nulline) {	    /* didn't find it swapped */
X			    if (!pch_swap())         /* put it back to normal */
X				fatal1("Lost hunk on alloc error!\n");
X			    reverse = !reverse;
X			}
X			else if (noreverse) {
X			    if (!pch_swap())         /* put it back to normal */
X				fatal1("Lost hunk on alloc error!\n");
X			    reverse = !reverse;
X			    say1(
X"Ignoring previously applied (or reversed) patch.\n");
X			    skip_rest_of_patch = TRUE;
X			}
X			else {
X			    ask3(
X"%seversed (or previously applied) patch detected!  %s -R? [y] ",
X				reverse ? "R" : "Unr",
X				reverse ? "Assume" : "Ignore");
X			    if (*buf == 'n') {
X				ask1("Apply anyway? [n] ");
X				if (*buf != 'y')
X				    skip_rest_of_patch = TRUE;
X				where = Nulline;
X				reverse = !reverse;
X				if (!pch_swap())  /* put it back to normal */
X				    fatal1("Lost hunk on alloc error!\n");
X			    }
X			}
X		    }
X		} while (!skip_rest_of_patch && where == Nulline &&
X		    ++fuzz <= mymaxfuzz);
X
X		if (skip_rest_of_patch) {		/* just got decided */
X		    Fclose(ofp);
X		    ofp = Nullfp;
X		}
X	    }
X
X	    newwhere = pch_newfirst() + last_offset;
X	    if (skip_rest_of_patch) {
X		abort_hunk();
X		failed++;
X		if (verbose)
X		    say3("Hunk #%d ignored at %ld.\n", hunk, newwhere);
X	    }
X	    else if (where == Nulline) {
X		abort_hunk();
X		failed++;
X		if (verbose)
X		    say3("Hunk #%d failed at %ld.\n", hunk, newwhere);
X	    }
X	    else {
X		apply_hunk(where);
X		if (verbose) {
X		    say3("Hunk #%d succeeded at %ld", hunk, newwhere);
X		    if (fuzz)
X			say2(" with fuzz %ld", fuzz);
X		    if (last_offset)
X			say3(" (offset %ld line%s)",
X			    last_offset, last_offset==1L?"":"s");
X		    say1(".\n");
X		}
X	    }
X	}
X
X	if (out_of_mem && using_plan_a) {
X	    Argc = Argc_last;
X	    Argv = Argv_last;
X	    say1("\n\nRan out of memory using Plan A--trying again...\n\n");
X	    continue;
X	}
X    
X	assert(hunk);
X    
X	/* finish spewing out the new file */
X	if (!skip_rest_of_patch)
X	    spew_output();
X	
X	/* and put the output where desired */
X	ignore_signals();
X	if (!skip_rest_of_patch) {
X	    if (move_file(TMPOUTNAME, outname) < 0) {
X		toutkeep = TRUE;
X		chmod(TMPOUTNAME, filemode);
X	    }
X	    else
X		chmod(outname, filemode);
X	}
X	Fclose(rejfp);
X	rejfp = Nullfp;
X	if (failed) {
X	    failtotal += failed;
X	    if (!*rejname) {
X		Strcpy(rejname, outname);
X#ifndef FLEXFILENAMES
X		{
X		    char *s = rindex(rejname,'/');
X
X		    if (!s)
X			s = rejname;
X		    if (strlen(s) > 13)
X			if (s[12] == '.')	/* try to preserve difference */
X			    s[12] = s[13];	/* between .h, .c, .y, etc. */
X			s[13] = '\0';
X		}
X#endif
X		Strcat(rejname, REJEXT);
X	    }
X	    if (skip_rest_of_patch) {
X		say4("%d out of %d hunks ignored--saving rejects to %s\n",
X		    failed, hunk, rejname);
X	    }
X	    else {
X		say4("%d out of %d hunks failed--saving rejects to %s\n",
X		    failed, hunk, rejname);
X	    }
X	    if (move_file(TMPREJNAME, rejname) < 0)
X		trejkeep = TRUE;
X	}
X	set_signals(1);
X    }
X    my_exit(failtotal);
X}
X
X/* Prepare to find the next patch to do in the patch file. */
X
Xvoid
Xreinitialize_almost_everything()
X{
X    re_patch();
X    re_input();
X
X    input_lines = 0;
X    last_frozen_line = 0;
X
X    filec = 0;
X    if (filearg[0] != Nullch && !out_of_mem) {
X	free(filearg[0]);
X	filearg[0] = Nullch;
X    }
X
X    if (outname != Nullch) {
X	free(outname);
X	outname = Nullch;
X    }
X
X    last_offset = 0;
X
X    diff_type = 0;
X
X    if (revision != Nullch) {
X	free(revision);
X	revision = Nullch;
X    }
X
X    reverse = FALSE;
X    skip_rest_of_patch = FALSE;
X
X    get_some_switches();
X
X    if (filec >= 2)
X	fatal1("You may not change to a different patch file.\n");
X}
X
X/* Process switches and filenames up to next '+' or end of list. */
X
Xvoid
Xget_some_switches()
X{
X    Reg1 char *s;
X
X    rejname[0] = '\0';
X    Argc_last = Argc;
X    Argv_last = Argv;
X    if (!Argc)
X	return;
X    for (Argc--,Argv++; Argc; Argc--,Argv++) {
X	s = Argv[0];
X	if (strEQ(s, "+")) {
X	    return;			/* + will be skipped by for loop */
X	}
X	if (*s != '-' || !s[1]) {
X	    if (filec == MAXFILEC)
X		fatal1("Too many file arguments.\n");
X	    filearg[filec++] = savestr(s);
X	}
X	else {
X	    switch (*++s) {
X	    case 'b':
X		origext = savestr(Argv[1]);
X		Argc--,Argv++;
X		break;
X	    case 'B':
X		origprae = savestr(Argv[1]);
X		Argc--,Argv++;
X		break;
X	    case 'c':
X		diff_type = CONTEXT_DIFF;
X		break;
X	    case 'd':
X		if (!*++s) {
X		    Argc--,Argv++;
X		    s = Argv[0];
X		}
X		if (chdir(s) < 0)
X		    fatal2("Can't cd to %s.\n", s);
X		break;
X	    case 'D':
X	    	do_defines = TRUE;
X		if (!*++s) {
X		    Argc--,Argv++;
X		    s = Argv[0];
X		}
X		if (!isalpha(*s))
X		    fatal1("Argument to -D not an identifier.\n");
X		Sprintf(if_defined, "#ifdef %s\n", s);
X		Sprintf(not_defined, "#ifndef %s\n", s);
X		Sprintf(end_defined, "#endif /* %s */\n", s);
X		break;
X	    case 'e':
X		diff_type = ED_DIFF;
X		break;
X	    case 'f':
X		force = TRUE;
X		break;
X	    case 'F':
X		if (*++s == '=')
X		    s++;
X		maxfuzz = atoi(s);
X		break;
X	    case 'l':
X		canonicalize = TRUE;
X		break;
X	    case 'n':
X		diff_type = NORMAL_DIFF;
X		break;
X	    case 'N':
X		noreverse = TRUE;
X		break;
X	    case 'o':
X		outname = savestr(Argv[1]);
X		Argc--,Argv++;
X		break;
X	    case 'p':
X		if (*++s == '=')
X		    s++;
X		strippath = atoi(s);
X		break;
X	    case 'r':
X		Strcpy(rejname, Argv[1]);
X		Argc--,Argv++;
X		break;
X	    case 'R':
X		reverse = TRUE;
X		break;
X	    case 's':
X		verbose = FALSE;
X		break;
X	    case 'S':
X		skip_rest_of_patch = TRUE;
X		break;
X	    case 'v':
X		version();
X		break;
X#ifdef DEBUGGING
X	    case 'x':
X		debug = atoi(s+1);
X		break;
X#endif
X	    default:
X		fatal2("Unrecognized switch: %s\n", Argv[0]);
X	    }
X	}
X    }
X}
X
X/* Attempt to find the right place to apply this hunk of patch. */
X
XLINENUM
Xlocate_hunk(fuzz)
XLINENUM fuzz;
X{
X    Reg1 LINENUM first_guess = pch_first() + last_offset;
X    Reg2 LINENUM offset;
X    LINENUM pat_lines = pch_ptrn_lines();
X    Reg3 LINENUM max_pos_offset = input_lines - first_guess
X				- pat_lines + 1; 
X    Reg4 LINENUM max_neg_offset = first_guess - last_frozen_line - 1
X				+ pch_context();
X
X    if (!pat_lines)			/* null range matches always */
X	return first_guess;
X    if (max_neg_offset >= first_guess)	/* do not try lines < 0 */
X	max_neg_offset = first_guess - 1;
X    if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz))
X	return first_guess;
X    for (offset = 1; ; offset++) {
X	Reg5 bool check_after = (offset <= max_pos_offset);
X	Reg6 bool check_before = (offset <= max_neg_offset);
X
X	if (check_after && patch_match(first_guess, offset, fuzz)) {
X#ifdef DEBUGGING
X	    if (debug & 1)
X		say3("Offset changing from %ld to %ld\n", last_offset, offset);
X#endif
X	    last_offset = offset;
X	    return first_guess+offset;
X	}
X	else if (check_before && patch_match(first_guess, -offset, fuzz)) {
X#ifdef DEBUGGING
X	    if (debug & 1)
X		say3("Offset changing from %ld to %ld\n", last_offset, -offset);
X#endif
X	    last_offset = -offset;
X	    return first_guess-offset;
X	}
X	else if (!check_before && !check_after)
X	    return Nulline;
X    }
X}
X
X/* We did not find the pattern, dump out the hunk so they can handle it. */
X
Xvoid
Xabort_hunk()
X{
X    Reg1 LINENUM i;
X    Reg2 LINENUM pat_end = pch_end();
X    /* add in last_offset to guess the same as the previous successful hunk */
X    LINENUM oldfirst = pch_first() + last_offset;
X    LINENUM newfirst = pch_newfirst() + last_offset;
X    LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
X    LINENUM newlast = newfirst + pch_repl_lines() - 1;
X    char *stars = (diff_type == NEW_CONTEXT_DIFF ? " ****" : "");
X    char *minuses = (diff_type == NEW_CONTEXT_DIFF ? " ----" : " -----");
X
X    fprintf(rejfp, "***************\n");
X    for (i=0; i<=pat_end; i++) {
X	switch (pch_char(i)) {
X	case '*':
X	    if (oldlast < oldfirst)
X		fprintf(rejfp, "*** 0%s\n", stars);
X	    else if (oldlast == oldfirst)
X		fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
X	    else
X		fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst, oldlast, stars);
X	    break;
X	case '=':
X	    if (newlast < newfirst)
X		fprintf(rejfp, "--- 0%s\n", minuses);
X	    else if (newlast == newfirst)
X		fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
X	    else
X		fprintf(rejfp, "--- %ld,%ld%s\n", newfirst, newlast, minuses);
X	    break;
X	case '\n':
X	    fprintf(rejfp, "%s", pfetch(i));
X	    break;
X	case ' ': case '-': case '+': case '!':
X	    fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
X	    break;
X	default:
X	    say1("Fatal internal error in abort_hunk().\n"); 
X	    abort();
X	}
X    }
X}
X
X/* We found where to apply it (we hope), so do it. */
X
Xvoid
Xapply_hunk(where)
XLINENUM where;
X{
X    Reg1 LINENUM old = 1;
X    Reg2 LINENUM lastline = pch_ptrn_lines();
X    Reg3 LINENUM new = lastline+1;
X#define OUTSIDE 0
X#define IN_IFNDEF 1
X#define IN_IFDEF 2
X#define IN_ELSE 3
X    Reg4 int def_state = OUTSIDE;
X    Reg5 bool R_do_defines = do_defines;
X    Reg6 LINENUM pat_end = pch_end();
X
X    where--;
X    while (pch_char(new) == '=' || pch_char(new) == '\n')
X	new++;
X    
X    while (old <= lastline) {
X	if (pch_char(old) == '-') {
X	    copy_till(where + old - 1);
X	    if (R_do_defines) {
X		if (def_state == OUTSIDE) {
X		    fputs(not_defined, ofp);
X		    def_state = IN_IFNDEF;
X		}
X		else if (def_state == IN_IFDEF) {
X		    fputs(else_defined, ofp);
X		    def_state = IN_ELSE;
X		}
X		fputs(pfetch(old), ofp);
X	    }
X	    last_frozen_line++;
X	    old++;
X	}
X	else if (new > pat_end)
X	    break;
X	else if (pch_char(new) == '+') {
X	    copy_till(where + old - 1);
X	    if (R_do_defines) {
X		if (def_state == IN_IFNDEF) {
X		    fputs(else_defined, ofp);
X		    def_state = IN_ELSE;
X		}
X		else if (def_state == OUTSIDE) {
X		    fputs(if_defined, ofp);
X		    def_state = IN_IFDEF;
X		}
X	    }
X	    fputs(pfetch(new), ofp);
X	    new++;
X	}
X	else {
X	    if (pch_char(new) != pch_char(old)) {
X		say3("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
X		    pch_hunk_beg() + old,
X		    pch_hunk_beg() + new);
X#ifdef DEBUGGING
X		say3("oldchar = '%c', newchar = '%c'\n",
X		    pch_char(old), pch_char(new));
X#endif
X		my_exit(1);
X	    }
X	    if (pch_char(new) == '!') {
X		copy_till(where + old - 1);
X		if (R_do_defines) {
X		   fputs(not_defined, ofp);
X		   def_state = IN_IFNDEF;
X		}
X		while (pch_char(old) == '!') {
X		    if (R_do_defines) {
X			fputs(pfetch(old), ofp);
X		    }
X		    last_frozen_line++;
X		    old++;
X		}
X		if (R_do_defines) {
X		    fputs(else_defined, ofp);
X		    def_state = IN_ELSE;
X		}
X		while (pch_char(new) == '!') {
X		    fputs(pfetch(new), ofp);
X		    new++;
X		}
X		if (R_do_defines) {
X		    fputs(end_defined, ofp);
X		    def_state = OUTSIDE;
X		}
X	    }
X	    else {
X		assert(pch_char(new) == ' ');
X		old++;
X		new++;
X	    }
X	}
X    }
X    if (new <= pat_end && pch_char(new) == '+') {
X	copy_till(where + old - 1);
X	if (R_do_defines) {
X	    if (def_state == OUTSIDE) {
X	    	fputs(if_defined, ofp);
X		def_state = IN_IFDEF;
X	    }
X	    else if (def_state == IN_IFNDEF) {
X		fputs(else_defined, ofp);
X		def_state = IN_ELSE;
X	    }
X	}
X	while (new <= pat_end && pch_char(new) == '+') {
X	    fputs(pfetch(new), ofp);
X	    new++;
X	}
X    }
X    if (R_do_defines && def_state != OUTSIDE) {
X	fputs(end_defined, ofp);
X    }
X}
X
X/* Open the new file. */
X
Xvoid
Xinit_output(name)
Xchar *name;
X{
X    ofp = fopen(name, "w");
X    if (ofp == Nullfp)
X	fatal2("patch: can't create %s.\n", name);
X}
X
X/* Open a file to put hunks we can't locate. */
X
Xvoid
Xinit_reject(name)
Xchar *name;
X{
X    rejfp = fopen(name, "w");
X    if (rejfp == Nullfp)
X	fatal2("patch: can't create %s.\n", name);
X}
X
X/* Copy input file to output, up to wherever hunk is to be applied. */
X
Xvoid
Xcopy_till(lastline)
XReg1 LINENUM lastline;
X{
X    Reg2 LINENUM R_last_frozen_line = last_frozen_line;
X
X    if (R_last_frozen_line > lastline)
X	say1("patch: misordered hunks! output will be garbled.\n");
X    while (R_last_frozen_line < lastline) {
X	dump_line(++R_last_frozen_line);
X    }
X    last_frozen_line = R_last_frozen_line;
X}
X
X/* Finish copying the input file to the output file. */
X
Xvoid
Xspew_output()
X{
X#ifdef DEBUGGING
X    if (debug & 256)
X	say3("il=%ld lfl=%ld\n",input_lines,last_frozen_line);
X#endif
X    if (input_lines)
X	copy_till(input_lines);		/* dump remainder of file */
X    Fclose(ofp);
X    ofp = Nullfp;
X}
X
X/* Copy one line from input to output. */
X
Xvoid
Xdump_line(line)
XLINENUM line;
X{
X    Reg1 char *s;
X    Reg2 char R_newline = '\n';
X
X    /* Note: string is not null terminated. */
X    for (s=ifetch(line, 0); putc(*s, ofp) != R_newline; s++) ;
X}
X
X/* Does the patch pattern match at line base+offset? */
X
Xbool
Xpatch_match(base, offset, fuzz)
XLINENUM base;
XLINENUM offset;
XLINENUM fuzz;
X{
X    Reg1 LINENUM pline = 1 + fuzz;
X    Reg2 LINENUM iline;
X    Reg3 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
X
X    for (iline=base+offset+fuzz; pline <= pat_lines; pline++,iline++) {
X	if (canonicalize) {
X	    if (!similar(ifetch(iline, (offset >= 0)),
X			 pfetch(pline),
X			 pch_line_len(pline) ))
X		return FALSE;
X	}
X	else if (strnNE(ifetch(iline, (offset >= 0)),
X		   pfetch(pline),
X		   pch_line_len(pline) ))
X	    return FALSE;
X    }
X    return TRUE;
X}
X
X/* Do two lines match with canonicalized white space? */
X
Xbool
Xsimilar(a,b,len)
XReg1 char *a;
XReg2 char *b;
XReg3 int len;
X{
X    while (len) {
X	if (isspace(*b)) {		/* whitespace (or \n) to match? */
X	    if (!isspace(*a))		/* no corresponding whitespace? */
X		return FALSE;
X	    while (len && isspace(*b) && *b != '\n')
X		b++,len--;		/* skip pattern whitespace */
X	    while (isspace(*a) && *a != '\n')
X		a++;			/* skip target whitespace */
X	    if (*a == '\n' || *b == '\n')
X		return (*a == *b);	/* should end in sync */
X	}
X	else if (*a++ != *b++)		/* match non-whitespace chars */
X	    return FALSE;
X	else
X	    len--;			/* probably not necessary */
X    }
X    return TRUE;			/* actually, this is not reached */
X					/* since there is always a \n */
X}
X
X/* Exit with cleanup. */
X
Xvoid
Xmy_exit(status)
Xint status;
X{
X    Unlink(TMPINNAME);
X    if (!toutkeep) {
X	Unlink(TMPOUTNAME);
X    }
X    if (!trejkeep) {
X	Unlink(TMPREJNAME);
X    }
X    Unlink(TMPPATNAME);
X    exit(status);
X}
SHAR_EOF
if test 18908 -ne "`wc -c < 'patch.c'`"
then
	echo shar: "error transmitting 'patch.c'" '(should have been 18908 characters)'
fi
fi
echo shar: "extracting 'util.h'" '(1972 characters)'
if test -f 'util.h'
then
	echo shar: "will not over-write existing file 'util.h'"
else
sed 's/^X//' << \SHAR_EOF > 'util.h'
X/* $Header: util.h,v 2.0 86/09/17 15:40:06 lwall Exp $
X *
X * $Log:	util.h,v $
X * Revision 2.0  86/09/17  15:40:06  lwall
X * Baseline for netwide release.
X * 
X */
X
X/* and for those machine that can't handle a variable argument list */
X
X#ifdef CANVARARG
X
X#define say1 say
X#define say2 say
X#define say3 say
X#define say4 say
X#define ask1 ask
X#define ask2 ask
X#define ask3 ask
X#define ask4 ask
X#define fatal1 fatal
X#define fatal2 fatal
X#define fatal3 fatal
X#define fatal4 fatal
X
X#else /* hope they allow multi-line macro actual arguments */
X
X#ifdef lint
X
X#define say1(a) say(a, 0, 0, 0)
X#define say2(a,b) say(a, (b)==(b), 0, 0)
X#define say3(a,b,c) say(a, (b)==(b), (c)==(c), 0)
X#define say4(a,b,c,d) say(a, (b)==(b), (c)==(c), (d)==(d))
X#define ask1(a) ask(a, 0, 0, 0)
X#define ask2(a,b) ask(a, (b)==(b), 0, 0)
X#define ask3(a,b,c) ask(a, (b)==(b), (c)==(c), 0)
X#define ask4(a,b,c,d) ask(a, (b)==(b), (c)==(c), (d)==(d))
X#define fatal1(a) fatal(a, 0, 0, 0)
X#define fatal2(a,b) fatal(a, (b)==(b), 0, 0)
X#define fatal3(a,b,c) fatal(a, (b)==(b), (c)==(c), 0)
X#define fatal4(a,b,c,d) fatal(a, (b)==(b), (c)==(c), (d)==(d))
X
X#else /* lint */
X    /* if this doesn't work, try defining CANVARARG above */
X#define say1(a) say(a, Nullch, Nullch, Nullch)
X#define say2(a,b) say(a, b, Nullch, Nullch)
X#define say3(a,b,c) say(a, b, c, Nullch)
X#define say4 say
X#define ask1(a) ask(a, Nullch, Nullch, Nullch)
X#define ask2(a,b) ask(a, b, Nullch, Nullch)
X#define ask3(a,b,c) ask(a, b, c, Nullch)
X#define ask4 ask
X#define fatal1(a) fatal(a, Nullch, Nullch, Nullch)
X#define fatal2(a,b) fatal(a, b, Nullch, Nullch)
X#define fatal3(a,b,c) fatal(a, b, c, Nullch)
X#define fatal4 fatal
X
X#endif /* lint */
X
X/* if neither of the above work, join all multi-line macro calls. */
X#endif
X
XEXT char serrbuf[BUFSIZ];		/* buffer for stderr */
X
Xchar *fetchname();
Xint move_file();
Xvoid copy_file();
Xvoid say();
Xvoid fatal();
Xvoid ask();
Xchar *savestr();
Xvoid set_signals();
Xvoid ignore_signals();
Xvoid makedirs();
SHAR_EOF
if test 1972 -ne "`wc -c < 'util.h'`"
then
	echo shar: "error transmitting 'util.h'" '(should have been 1972 characters)'
fi
fi
echo shar: "extracting 'version.c'" '(489 characters)'
if test -f 'version.c'
then
	echo shar: "will not over-write existing file 'version.c'"
else
sed 's/^X//' << \SHAR_EOF > 'version.c'
X/* $Header: version.c,v 2.0 86/09/17 15:40:11 lwall Exp $
X *
X * $Log:	version.c,v $
X * Revision 2.0  86/09/17  15:40:11  lwall
X * Baseline for netwide release.
X * 
X */
X
X#include "EXTERN.h"
X#include "common.h"
X#include "util.h"
X#include "INTERN.h"
X#include "patchlevel.h"
X#include "version.h"
X
X/* Print out the version number and die. */
X
Xvoid
Xversion()
X{
X    extern char rcsid[];
X
X#ifdef lint
X    rcsid[0] = rcsid[0];
X#else
X    fatal3("%s\nPatch level: %d\n", rcsid, PATCHLEVEL);
X#endif
X}
SHAR_EOF
if test 489 -ne "`wc -c < 'version.c'`"
then
	echo shar: "error transmitting 'version.c'" '(should have been 489 characters)'
fi
fi
echo shar: "extracting 'version.h'" '(185 characters)'
if test -f 'version.h'
then
	echo shar: "will not over-write existing file 'version.h'"
else
sed 's/^X//' << \SHAR_EOF > 'version.h'
X/* $Header: version.h,v 2.0 86/09/17 15:40:14 lwall Exp $
X *
X * $Log:	version.h,v $
X * Revision 2.0  86/09/17  15:40:14  lwall
X * Baseline for netwide release.
X * 
X */
X
Xvoid version();
SHAR_EOF
if test 185 -ne "`wc -c < 'version.h'`"
then
	echo shar: "error transmitting 'version.h'" '(should have been 185 characters)'
fi
fi
exit 0
#	End of shell archive

egray@fthood.UUCP (06/07/88)

This is part 3 of 3 to Larry Wall's patch v2.0-11

Emmet P. Gray				US Army, HQ III Corps & Fort Hood
...!ihnp4!uiucuxc!fthood!egray		Attn: AFZF-DE-ENV
					Directorate of Engineering & Housing
					Environmental Management Office
					Fort Hood, TX 76544-5057

------------------------------------------------------------------------------
#! /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:
#	patch.man
#	patchlevel.h
#	pch.c
#	pch.h
#	util.c
# This archive created: Tue Jun  7 11:32:47 1988
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'patch.man'" '(14363 characters)'
if test -f 'patch.man'
then
	echo shar: "will not over-write existing file 'patch.man'"
else
sed 's/^X//' << \SHAR_EOF > 'patch.man'
X.rn '' }`
X''' $Header: patch.man,v 2.0.1.1 88/06/03 15:12:51 lwall Locked $
X''' 
X''' $Log:	patch.man,v $
X''' Revision 2.0.1.1  88/06/03  15:12:51  lwall
X''' patch10: -B switch was contributed.
X''' 
X''' Revision 2.0  86/09/17  15:39:09  lwall
X''' Baseline for netwide release.
X''' 
X''' Revision 1.4  86/08/01  19:23:22  lwall
X''' Documented -v, -p, -F.
X''' Added notes to patch senders.
X''' 
X''' Revision 1.3  85/03/26  15:11:06  lwall
X''' Frozen.
X''' 
X''' Revision 1.2.1.4  85/03/12  16:14:27  lwall
X''' Documented -p.
X''' 
X''' Revision 1.2.1.3  85/03/12  16:09:41  lwall
X''' Documented -D.
X''' 
X''' Revision 1.2.1.2  84/12/05  11:06:55  lwall
X''' Added -l switch, and noted bistability bug.
X''' 
X''' Revision 1.2.1.1  84/12/04  17:23:39  lwall
X''' Branch for sdcrdcf changes.
X''' 
X''' Revision 1.2  84/12/04  17:22:02  lwall
X''' Baseline version.
X''' 
X.de Sh
X.br
X.ne 5
X.PP
X\fB\\$1\fR
X.PP
X..
X.de Sp
X.if t .sp .5v
X.if n .sp
X..
X'''
X'''     Set up \*(-- to give an unbreakable dash;
X'''     string Tr holds user defined translation string.
X'''     Bell System Logo is used as a dummy character.
X'''
X.ie n \{\
X.tr \(bs-\*(Tr
X.ds -- \(bs-
X.if (\n(.H=4u)&(1m=24u) .ds -- \(bs\h'-12u'\(bs\h'-12u'-\" diablo 10 pitch
X.if (\n(.H=4u)&(1m=20u) .ds -- \(bs\h'-12u'\(bs\h'-8u'-\" diablo 12 pitch
X.ds L" ""
X.ds R" ""
X.ds L' '
X.ds R' '
X'br\}
X.el\{\
X.ds -- \(em\|
X.tr \*(Tr
X.ds L" ``
X.ds R" ''
X.ds L' `
X.ds R' '
X'br\}
X.TH PATCH 1 LOCAL
X.SH NAME
Xpatch - a program for applying a diff file to an original
X.SH SYNOPSIS
X.B patch
X[options] orig patchfile [+ [options] orig]
X.sp
Xbut usually just
X.sp
X.B patch
X<patchfile
X.SH DESCRIPTION
X.I Patch
Xwill take a patch file containing any of the three forms of difference
Xlisting produced by the
X.I diff
Xprogram and apply those differences to an original file, producing a patched
Xversion.
XBy default, the patched version is put in place of the original, with
Xthe original file backed up to the same name with the
Xextension \*(L".orig\*(R" or \*(L"~\*(R" , or as specified by the
X.B -b
Xswitch.
XYou may also specify where you want the output to go with a
X.B -o
Xswitch.
XIf
X.I patchfile
Xis omitted, or is a hyphen, the patch will be read from standard input.
X.PP
XUpon startup, patch will attempt to determine the type of the diff listing,
Xunless over-ruled by a
X.BR -c ,
X.BR -e ,
Xor
X.B -n
Xswitch.
XContext diffs and normal diffs are applied by the
X.I patch
Xprogram itself, while ed diffs are simply fed to the
X.I ed
Xeditor via a pipe.
X.PP
X.I Patch
Xwill try to skip any leading garbage, apply the diff,
Xand then skip any trailing garbage.
XThus you could feed an article or message containing a
Xdiff listing to
X.IR patch ,
Xand it should work.
XIf the entire diff is indented by a consistent amount,
Xthis will be taken into account.
X.PP
XWith context diffs, and to a lesser extent with normal diffs,
X.I patch
Xcan detect when the line numbers mentioned in the patch are incorrect,
Xand will attempt to find the correct place to apply each hunk of the patch.
XAs a first guess, it takes the line number mentioned for the hunk, plus or
Xminus any offset used in applying the previous hunk.
XIf that is not the correct place,
X.I patch
Xwill scan both forwards and backwards for a set of lines matching the context
Xgiven in the hunk.
XFirst
X.I patch
Xlooks for a place where all lines of the context match.
XIf no such place is found, and it's a context diff, and the maximum fuzz factor
Xis set to 1 or more, then another scan takes place ignoring the first and last
Xline of context.
XIf that fails, and the maximum fuzz factor is set to 2 or more,
Xthe first two and last two lines of context are ignored,
Xand another scan is made.
X(The default maximum fuzz factor is 2.)
XIf
X.I patch
Xcannot find a place to install that hunk of the patch, it will put the
Xhunk out to a reject file, which normally is the name of the output file
Xplus \*(L".rej\*(R" or \*(L"#\*(R" .
X(Note that the rejected hunk will come out in context diff form whether the
Xinput patch was a context diff or a normal diff.
XIf the input was a normal diff, many of the contexts will simply be null.)
XThe line numbers on the hunks in the reject file may be different than
Xin the patch file: they reflect the approximate location patch thinks the
Xfailed hunks belong in the new file rather than the old one.
X.PP
XAs each hunk is completed, you will be told whether the hunk succeeded or
Xfailed, and which line (in the new file)
X.I patch
Xthought the hunk should go on.
XIf this is different from the line number specified in the diff you will
Xbe told the offset.
XA single large offset MAY be an indication that a hunk was installed in the
Xwrong place.
XYou will also be told if a fuzz factor was used to make the match, in which
Xcase you should also be slightly suspicious.
X.PP
XIf no original file is specified on the command line,
X.I patch
Xwill try to figure out from the leading garbage what the name of the file
Xto edit is.
XIn the header of a context diff, the filename is found from lines beginning
Xwith \*(L"***\*(R" or \*(L"---\*(R", with the shortest name of an existing
Xfile winning.
XOnly context diffs have lines like that, but if there is an \*(L"Index:\*(R"
Xline in the leading garbage,
X.I patch
Xwill try to use the filename from that line.
XThe context diff header takes precedence over an Index line.
XIf no filename can be intuited from the leading garbage, you will be asked
Xfor the name of the file to patch.
X.PP
X(If the original file cannot be found, but a suitable SCCS or RCS file is
Xhandy,
X.I patch
Xwill attempt to get or check out the file.)
X.PP
XAdditionally, if the leading garbage contains a \*(L"Prereq: \*(R" line,
X.I patch
Xwill take the first word from the prerequisites line (normally a version
Xnumber) and check the input file to see if that word can be found.
XIf not,
X.I patch
Xwill ask for confirmation before proceeding.
X.PP
XThe upshot of all this is that you should be able to say, while in a news
Xinterface, the following:
X.Sp
X	| patch -d /usr/src/local/blurfl
X.Sp
Xand patch a file in the blurfl directory directly from the article containing
Xthe patch.
X.PP
XIf the patch file contains more than one patch,
X.I patch
Xwill try to apply each of them as if they came from separate patch files.
XThis means, among other things, that it is assumed that the name of the file
Xto patch must be determined for each diff listing,
Xand that the garbage before each diff listing will
Xbe examined for interesting things such as filenames and revision level, as
Xmentioned previously.
XYou can give switches (and another original file name) for the second and
Xsubsequent patches by separating the corresponding argument lists
Xby a \*(L'+\*(R'.
X(The argument list for a second or subsequent patch may not specify a new
Xpatch file, however.)
X.PP
X.I Patch
Xrecognizes the following switches:
X.TP 5
X.B \-b
Xcauses the next argument to be interpreted as the backup extension, to be
Xused in place of \*(L".orig\*(R" or \*(L"~\*(R".
X.TP 5
X.B \-B
Xcauses the next argument to be interpreted as a prefix to the backup file
Xname. If this argument is specified any argument from -b will be ignored.
XThis argument is an extension to Larry Wall's patch v2.0.1.4, patchlevel 8,
Xmade by M. Greim (greim@sbsvax.uucp).
X.TP 5
X.B \-c
Xforces
X.I patch
Xto interpret the patch file as a context diff.
X.TP 5
X.B \-d
Xcauses
X.I patch
Xto interpret the next argument as a directory, and cd to it before doing
Xanything else.
X.TP 5
X.B \-D
Xcauses
X.I patch
Xto use the "#ifdef...#endif" construct to mark changes.
XThe argument following will be used as the differentiating symbol.
XNote that, unlike the C compiler, there must be a space between the
X.B \-D
Xand the argument.
X.TP 5
X.B \-e
Xforces
X.I patch
Xto interpret the patch file as an ed script.
X.TP 5
X.B \-f
Xforces
X.I patch
Xto assume that the user knows exactly what he or she is doing, and to not
Xask any questions.
XIt does not suppress commentary, however.
XUse
X.B \-s
Xfor that.
X.TP 5
X.B \-F<number>
Xsets the maximum fuzz factor.
XThis switch only applies to context diffs, and causes
X.I patch
Xto ignore up to that many lines in looking for places to install a hunk.
XNote that a larger fuzz factor increases the odds of a faulty patch.
XThe default fuzz factor is 2, and it may not be set to more than
Xthe number of lines of context in the context diff, ordinarily 3.
X.TP 5
X.B \-l
Xcauses the pattern matching to be done loosely, in case the tabs and
Xspaces have been munged in your input file.
XAny sequence of whitespace in the pattern line will match any sequence
Xin the input file.
XNormal characters must still match exactly.
XEach line of the context must still match a line in the input file.
X.TP 5
X.B \-n
Xforces
X.I patch
Xto interpret the patch file as a normal diff.
X.TP 5
X.B \-N
Xcauses
X.I patch
Xto ignore patches that it thinks are reversed or already applied.
XSee also
X.B \-R .
X.TP 5
X.B \-o
Xcauses the next argument to be interpreted as the output file name.
X.TP 5
X.B \-p<number>
Xsets the pathname strip count,
Xwhich controls how pathnames found in the patch file are treated, in case
Xthe you keep your files in a different directory than the person who sent
Xout the patch.
XThe strip count specifies how many slashes are to be stripped from
Xthe front of the pathname.
X(Any intervening directory names also go away.)
XFor example, supposing the filename in the patch file was
X.sp
X	/u/howard/src/blurfl/blurfl.c
X.sp
Xsetting
X.B \-p
Xor
X.B \-p0
Xgives the entire pathname unmodified,
X.B \-p1
Xgives
X.sp
X	u/howard/src/blurfl/blurfl.c
X.sp
Xwithout the leading slash,
X.B \-p4
Xgives
X.sp
X	blurfl/blurfl.c
X.sp
Xand not specifying
X.B \-p
Xat all just gives you "blurfl.c".
XWhatever you end up with is looked for either in the current directory,
Xor the directory specified by the
X.B \-d
Xswitch.
X.TP 5
X.B \-r
Xcauses the next argument to be interpreted as the reject file name.
X.TP 5
X.B \-R
Xtells
X.I patch
Xthat this patch was created with the old and new files swapped.
X(Yes, I'm afraid that does happen occasionally, human nature being what it
Xis.)
X.I Patch
Xwill attempt to swap each hunk around before applying it.
XRejects will come out in the swapped format.
XThe
X.B \-R
Xswitch will not work with ed diff scripts because there is too little
Xinformation to reconstruct the reverse operation.
X.Sp
XIf the first hunk of a patch fails,
X.I patch
Xwill reverse the hunk to see if it can be applied that way.
XIf it can, you will be asked if you want to have the
X.B \-R
Xswitch set.
XIf it can't, the patch will continue to be applied normally.
X(Note: this method cannot detect a reversed patch if it is a normal diff
Xand if the first command is an append (i.e. it should have been a delete)
Xsince appends always succeed, due to the fact that a null context will match
Xanywhere.
XLuckily, most patches add or change lines rather than delete them, so most
Xreversed normal diffs will begin with a delete, which will fail, triggering
Xthe heuristic.)
X.TP 5
X.B \-s
Xmakes
X.I patch
Xdo its work silently, unless an error occurs.
X.TP 5
X.B \-S
Xcauses
X.I patch
Xto ignore this patch from the patch file, but continue on looking
Xfor the next patch in the file.
XThus
X.sp
X	patch -S + -S + <patchfile
X.sp
Xwill ignore the first and second of three patches.
X.TP 5
X.B \-v
Xcauses
X.I patch
Xto print out it's revision header and patch level.
X.TP 5
X.B \-x<number>
Xsets internal debugging flags, and is of interest only to
X.I patch
Xpatchers.
X.SH ENVIRONMENT
XNo environment variables are used by
X.IR patch .
X.SH FILES
X/tmp/patch*
X.SH SEE ALSO
Xdiff(1)
X.SH NOTES FOR PATCH SENDERS
XThere are several things you should bear in mind if you are going to
Xbe sending out patches.
XFirst, you can save people a lot of grief by keeping a patchlevel.h file
Xwhich is patched to increment the patch level as the first diff in the
Xpatch file you send out.
XIf you put a Prereq: line in with the patch, it won't let them apply
Xpatches out of order without some warning.
XSecond, make sure you've specified the filenames right, either in a
Xcontext diff header, or with an Index: line.
XIf you are patching something in a subdirectory, be sure to tell the patch
Xuser to specify a 
X.B \-p
Xswitch as needed.
XThird, you can create a file by sending out a diff that compares a
Xnull file to the file you want to create.
XThis will only work if the file you want to create doesn't exist already in
Xthe target directory.
XFourth, take care not to send out reversed patches, since it makes people wonder
Xwhether they already applied the patch.
XFifth, while you may be able to get away with putting 582 diff listings into
Xone file, it is probably wiser to group related patches into separate files in
Xcase something goes haywire.
X.SH DIAGNOSTICS
XToo many to list here, but generally indicative that
X.I patch
Xcouldn't parse your patch file.
X.PP
XThe message \*(L"Hmm...\*(R" indicates that there is unprocessed text in
Xthe patch file and that
X.I patch
Xis attempting to intuit whether there is a patch in that text and, if so,
Xwhat kind of patch it is.
X.PP
X.I Patch
Xwill exit with a non-zero status if any reject files were created.
XWhen applying a set of patches in a loop it behooves you to check this
Xexit status so you don't apply a later patch to a partially patched file.
X.SH CAVEATS
X.I Patch
Xcannot tell if the line numbers are off in an ed script, and can only detect
Xbad line numbers in a normal diff when it finds a \*(L"change\*(R" or
Xa \*(L"delete\*(R" command.
XA context diff using fuzz factor 3 may have the same problem.
XUntil a suitable interactive interface is added, you should probably do
Xa context diff in these cases to see if the changes made sense.
XOf course, compiling without errors is a pretty good indication that the patch
Xworked, but not always.
X.PP
X.I Patch
Xusually produces the correct results, even when it has to do a lot of
Xguessing.
XHowever, the results are guaranteed to be correct only when the patch is
Xapplied to exactly the same version of the file that the patch was
Xgenerated from.
X.SH BUGS
XCould be smarter about partial matches, excessively \&deviant offsets and
Xswapped code, but that would take an extra pass.
X.PP
XIf code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
X#endif),
X.I patch
Xis incapable of patching both versions, and, if it works at all, will likely
Xpatch the wrong one, and tell you that it succeeded to boot.
X.PP
XIf you apply a patch you've already applied,
X.I patch
Xwill think it is a reversed patch, and offer to un-apply the patch.
XThis could be construed as a feature.
X.rn }` ''
SHAR_EOF
if test 14363 -ne "`wc -c < 'patch.man'`"
then
	echo shar: "error transmitting 'patch.man'" '(should have been 14363 characters)'
fi
fi
echo shar: "extracting 'patchlevel.h'" '(22 characters)'
if test -f 'patchlevel.h'
then
	echo shar: "will not over-write existing file 'patchlevel.h'"
else
sed 's/^X//' << \SHAR_EOF > 'patchlevel.h'
X#define PATCHLEVEL 11
SHAR_EOF
if test 22 -ne "`wc -c < 'patchlevel.h'`"
then
	echo shar: "error transmitting 'patchlevel.h'" '(should have been 22 characters)'
fi
fi
echo shar: "extracting 'pch.c'" '(28639 characters)'
if test -f 'pch.c'
then
	echo shar: "will not over-write existing file 'pch.c'"
else
sed 's/^X//' << \SHAR_EOF > 'pch.c'
X/* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $
X *
X * $Log:	pch.c,v $
X * Revision 2.0.1.7  88/06/03  15:13:28  lwall
X * patch10: Can now find patches in shar scripts.
X * patch10: Hunks that swapped and then swapped back could core dump.
X * 
X * Revision 2.0.1.6  87/06/04  16:18:13  lwall
X * pch_swap didn't swap p_bfake and p_efake.
X * 
X * Revision 2.0.1.5  87/01/30  22:47:42  lwall
X * Improved responses to mangled patches.
X * 
X * Revision 2.0.1.4  87/01/05  16:59:53  lwall
X * New-style context diffs caused double call to free().
X * 
X * Revision 2.0.1.3  86/11/14  10:08:33  lwall
X * Fixed problem where a long pattern wouldn't grow the hunk.
X * Also restored p_input_line when backtracking so error messages are right.
X * 
X * Revision 2.0.1.2  86/11/03  17:49:52  lwall
X * New-style delete triggers spurious assertion error.
X * 
X * Revision 2.0.1.1  86/10/29  15:52:08  lwall
X * Could falsely report new-style context diff.
X * 
X * Revision 2.0  86/09/17  15:39:37  lwall
X * Baseline for netwide release.
X * 
X */
X
X#include "EXTERN.h"
X#include "common.h"
X#include "util.h"
X#include "INTERN.h"
X#include "pch.h"
X
X/* Patch (diff listing) abstract type. */
X
Xstatic long p_filesize;			/* size of the patch file */
Xstatic LINENUM p_first;			/* 1st line number */
Xstatic LINENUM p_newfirst;		/* 1st line number of replacement */
Xstatic LINENUM p_ptrn_lines;		/* # lines in pattern */
Xstatic LINENUM p_repl_lines;		/* # lines in replacement text */
Xstatic LINENUM p_end = -1;		/* last line in hunk */
Xstatic LINENUM p_max;			/* max allowed value of p_end */
Xstatic LINENUM p_context = 3;		/* # of context lines */
Xstatic LINENUM p_input_line = 0;	/* current line # from patch file */
Xstatic char **p_line = Null(char**);	/* the text of the hunk */
Xstatic short *p_len = Null(short*);	/* length of each line */
Xstatic char *p_char = Nullch;		/* +, -, and ! */
Xstatic int hunkmax = INITHUNKMAX;	/* size of above arrays to begin with */
Xstatic int p_indent;			/* indent to patch */
Xstatic LINENUM p_base;			/* where to intuit this time */
Xstatic LINENUM p_bline;			/* line # of p_base */
Xstatic LINENUM p_start;			/* where intuit found a patch */
Xstatic LINENUM p_sline;			/* and the line number for it */
Xstatic LINENUM p_hunk_beg;		/* line number of current hunk */
Xstatic LINENUM p_efake = -1;		/* end of faked up lines--don't free */
Xstatic LINENUM p_bfake = -1;		/* beg of faked up lines */
X
X/* Prepare to look for the next patch in the patch file. */
X
Xvoid
Xre_patch()
X{
X    p_first = Nulline;
X    p_newfirst = Nulline;
X    p_ptrn_lines = Nulline;
X    p_repl_lines = Nulline;
X    p_end = (LINENUM)-1;
X    p_max = Nulline;
X    p_indent = 0;
X}
X
X/* Open the patch file at the beginning of time. */
X
Xvoid
Xopen_patch_file(filename)
Xchar *filename;
X{
X    if (filename == Nullch || !*filename || strEQ(filename, "-")) {
X	pfp = fopen(TMPPATNAME, "w");
X	if (pfp == Nullfp)
X	    fatal2("patch: can't create %s.\n", TMPPATNAME);
X	while (fgets(buf, sizeof buf, stdin) != Nullch)
X	    fputs(buf, pfp);
X	Fclose(pfp);
X	filename = TMPPATNAME;
X    }
X    pfp = fopen(filename, "r");
X    if (pfp == Nullfp)
X	fatal2("patch file %s not found\n", filename);
X    Fstat(fileno(pfp), &filestat);
X    p_filesize = filestat.st_size;
X    next_intuit_at(0L,1L);			/* start at the beginning */
X    set_hunkmax();
X}
X
X/* Make sure our dynamically realloced tables are malloced to begin with. */
X
Xvoid
Xset_hunkmax()
X{
X#ifndef lint
X    if (p_line == Null(char**))
X	p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
X    if (p_len == Null(short*))
X	p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
X#endif
X    if (p_char == Nullch)
X	p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
X}
X
X/* Enlarge the arrays containing the current hunk of patch. */
X
Xvoid
Xgrow_hunkmax()
X{
X    hunkmax *= 2;
X    /* 
X     * Note that on most systems, only the p_line array ever gets fresh memory
X     * since p_len can move into p_line's old space, and p_char can move into
X     * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
X     */
X    assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
X#ifndef lint
X    p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
X    p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
X    p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
X#endif
X    if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
X	return;
X    if (!using_plan_a)
X	fatal1("patch: out of memory (grow_hunkmax)\n");
X    out_of_mem = TRUE;		/* whatever is null will be allocated again */
X				/* from within plan_a(), of all places */
X}
X
X/* True if the remainder of the patch file contains a diff of some sort. */
X
Xbool
Xthere_is_another_patch()
X{
X    if (p_base != 0L && p_base >= p_filesize) {
X	if (verbose)
X	    say1("done\n");
X	return FALSE;
X    }
X    if (verbose)
X	say1("Hmm...");
X    diff_type = intuit_diff_type();
X    if (!diff_type) {
X	if (p_base != 0L) {
X	    if (verbose)
X		say1("  Ignoring the trailing garbage.\ndone\n");
X	}
X	else
X	    say1("  I can't seem to find a patch in there anywhere.\n");
X	return FALSE;
X    }
X    if (verbose)
X	say3("  %sooks like %s to me...\n",
X	    (p_base == 0L ? "L" : "The next patch l"),
X	    diff_type == CONTEXT_DIFF ? "a context diff" :
X	    diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
X	    diff_type == NORMAL_DIFF ? "a normal diff" :
X	    "an ed script" );
X    if (p_indent && verbose)
X	say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
X    skip_to(p_start,p_sline);
X    while (filearg[0] == Nullch) {
X	if (force) {
X	    say1("No file to patch.  Skipping...\n");
X	    filearg[0] = savestr(bestguess);
X	    return TRUE;
X	}
X	ask1("File to patch: ");
X	if (*buf != '\n') {
X	    if (bestguess)
X		free(bestguess);
X	    bestguess = savestr(buf);
X	    filearg[0] = fetchname(buf, 0, FALSE);
X	}
X	if (filearg[0] == Nullch) {
X	    ask1("No file found--skip this patch? [n] ");
X	    if (*buf != 'y') {
X		continue;
X	    }
X	    if (verbose)
X		say1("Skipping patch...\n");
X	    filearg[0] = fetchname(bestguess, 0, TRUE);
X	    skip_rest_of_patch = TRUE;
X	    return TRUE;
X	}
X    }
X    return TRUE;
X}
X
X/* Determine what kind of diff is in the remaining part of the patch file. */
X
Xint
Xintuit_diff_type()
X{
X    Reg4 long this_line = 0;
X    Reg5 long previous_line;
X    Reg6 long first_command_line = -1;
X    long fcl_line;
X    Reg7 bool last_line_was_command = FALSE;
X    Reg8 bool this_is_a_command = FALSE;
X    Reg9 bool stars_last_line = FALSE;
X    Reg10 bool stars_this_line = FALSE;
X    Reg3 int indent;
X    Reg1 char *s;
X    Reg2 char *t;
X    char *indtmp = Nullch;
X    char *oldtmp = Nullch;
X    char *newtmp = Nullch;
X    char *indname = Nullch;
X    char *oldname = Nullch;
X    char *newname = Nullch;
X    Reg11 int retval;
X    bool no_filearg = (filearg[0] == Nullch);
X
X    ok_to_create_file = FALSE;
X    Fseek(pfp, p_base, 0);
X    p_input_line = p_bline - 1;
X    for (;;) {
X	previous_line = this_line;
X	last_line_was_command = this_is_a_command;
X	stars_last_line = stars_this_line;
X	this_line = ftell(pfp);
X	indent = 0;
X	p_input_line++;
X	if (fgets(buf, sizeof buf, pfp) == Nullch) {
X	    if (first_command_line >= 0L) {
X					/* nothing but deletes!? */
X		p_start = first_command_line;
X		p_sline = fcl_line;
X		retval = ED_DIFF;
X		goto scan_exit;
X	    }
X	    else {
X		p_start = this_line;
X		p_sline = p_input_line;
X		retval = 0;
X		goto scan_exit;
X	    }
X	}
X	for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
X	    if (*s == '\t')
X		indent += 8 - (indent % 8);
X	    else
X		indent++;
X	}
X	for (t=s; isdigit(*t) || *t == ','; t++) ; 
X	this_is_a_command = (isdigit(*s) &&
X	  (*t == 'd' || *t == 'c' || *t == 'a') );
X	if (first_command_line < 0L && this_is_a_command) { 
X	    first_command_line = this_line;
X	    fcl_line = p_input_line;
X	    p_indent = indent;		/* assume this for now */
X	}
X	if (!stars_last_line && strnEQ(s, "*** ", 4))
X	    oldtmp = savestr(s+4);
X	else if (strnEQ(s, "--- ", 4))
X	    newtmp = savestr(s+4);
X	else if (strnEQ(s, "Index:", 6))
X	    indtmp = savestr(s+6);
X	else if (strnEQ(s, "Prereq:", 7)) {
X	    for (t=s+7; isspace(*t); t++) ;
X	    revision = savestr(t);
X	    for (t=revision; *t && !isspace(*t); t++) ;
X	    *t = '\0';
X	    if (!*revision) {
X		free(revision);
X		revision = Nullch;
X	    }
X	}
X	if ((!diff_type || diff_type == ED_DIFF) &&
X	  first_command_line >= 0L &&
X	  strEQ(s, ".\n") ) {
X	    p_indent = indent;
X	    p_start = first_command_line;
X	    p_sline = fcl_line;
X	    retval = ED_DIFF;
X	    goto scan_exit;
X	}
X	stars_this_line = strnEQ(s, "********", 8);
X	if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
X		 strnEQ(s, "*** ", 4)) {
X	    if (!atol(s+4))
X		ok_to_create_file = TRUE;
X	    /* if this is a new context diff the character just before */
X	    /* the newline is a '*'. */
X	    while (*s != '\n')
X		s++;
X	    p_indent = indent;
X	    p_start = previous_line;
X	    p_sline = p_input_line - 1;
X	    retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
X	    goto scan_exit;
X	}
X	if ((!diff_type || diff_type == NORMAL_DIFF) && 
X	  last_line_was_command &&
X	  (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
X	    p_start = previous_line;
X	    p_sline = p_input_line - 1;
X	    p_indent = indent;
X	    retval = NORMAL_DIFF;
X	    goto scan_exit;
X	}
X    }
X  scan_exit:
X    if (no_filearg) {
X	if (indtmp != Nullch)
X	    indname = fetchname(indtmp, strippath, ok_to_create_file);
X	if (oldtmp != Nullch)
X	    oldname = fetchname(oldtmp, strippath, ok_to_create_file);
X	if (newtmp != Nullch)
X	    newname = fetchname(newtmp, strippath, ok_to_create_file);
X	if (oldname && newname) {
X	    if (strlen(oldname) < strlen(newname))
X		filearg[0] = savestr(oldname);
X	    else
X		filearg[0] = savestr(newname);
X	}
X	else if (oldname)
X	    filearg[0] = savestr(oldname);
X	else if (newname)
X	    filearg[0] = savestr(newname);
X	else if (indname)
X	    filearg[0] = savestr(indname);
X    }
X    if (bestguess) {
X	free(bestguess);
X	bestguess = Nullch;
X    }
X    if (filearg[0] != Nullch)
X	bestguess = savestr(filearg[0]);
X    else if (indtmp != Nullch)
X	bestguess = fetchname(indtmp, strippath, TRUE);
X    else {
X	if (oldtmp != Nullch)
X	    oldname = fetchname(oldtmp, strippath, TRUE);
X	if (newtmp != Nullch)
X	    newname = fetchname(newtmp, strippath, TRUE);
X	if (oldname && newname) {
X	    if (strlen(oldname) < strlen(newname))
X		bestguess = savestr(oldname);
X	    else
X		bestguess = savestr(newname);
X	}
X	else if (oldname)
X	    bestguess = savestr(oldname);
X	else if (newname)
X	    bestguess = savestr(newname);
X    }
X    if (indtmp != Nullch)
X	free(indtmp);
X    if (oldtmp != Nullch)
X	free(oldtmp);
X    if (newtmp != Nullch)
X	free(newtmp);
X    if (indname != Nullch)
X	free(indname);
X    if (oldname != Nullch)
X	free(oldname);
X    if (newname != Nullch)
X	free(newname);
X    return retval;
X}
X
X/* Remember where this patch ends so we know where to start up again. */
X
Xvoid
Xnext_intuit_at(file_pos,file_line)
Xlong file_pos;
Xlong file_line;
X{
X    p_base = file_pos;
X    p_bline = file_line;
X}
X
X/* Basically a verbose fseek() to the actual diff listing. */
X
Xvoid
Xskip_to(file_pos,file_line)
Xlong file_pos;
Xlong file_line;
X{
X    char *ret;
X
X    assert(p_base <= file_pos);
X    if (verbose && p_base < file_pos) {
X	Fseek(pfp, p_base, 0);
X	say1("The text leading up to this was:\n--------------------------\n");
X	while (ftell(pfp) < file_pos) {
X	    ret = fgets(buf, sizeof buf, pfp);
X	    assert(ret != Nullch);
X	    say2("|%s", buf);
X	}
X	say1("--------------------------\n");
X    }
X    else
X	Fseek(pfp, file_pos, 0);
X    p_input_line = file_line - 1;
X}
X
X/* True if there is more of the current diff listing to process. */
X
Xbool
Xanother_hunk()
X{
X    Reg1 char *s;
X    Reg8 char *ret;
X    Reg2 int context = 0;
X
X    while (p_end >= 0) {
X	if (p_end == p_efake)
X	    p_end = p_bfake;		/* don't free twice */
X	else
X	    free(p_line[p_end]);
X	p_end--;
X    }
X    assert(p_end == -1);
X    p_efake = -1;
X
X    p_max = hunkmax;			/* gets reduced when --- found */
X    if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
X	long line_beginning = ftell(pfp);
X					/* file pos of the current line */
X	LINENUM repl_beginning = 0;	/* index of --- line */
X	Reg4 LINENUM fillcnt = 0;	/* #lines of missing ptrn or repl */
X	Reg5 LINENUM fillsrc;		/* index of first line to copy */
X	Reg6 LINENUM filldst;		/* index of first missing line */
X	bool ptrn_spaces_eaten = FALSE;	/* ptrn was slightly misformed */
X	Reg9 bool repl_could_be_missing = TRUE;
X					/* no + or ! lines in this hunk */
X	bool repl_missing = FALSE;	/* we are now backtracking */
X	long repl_backtrack_position = 0;
X					/* file pos of first repl line */
X	LINENUM repl_patch_line;	/* input line number for same */
X	Reg7 LINENUM ptrn_copiable = 0;
X					/* # of copiable lines in ptrn */
X
X	ret = pgets(buf, sizeof buf, pfp);
X	p_input_line++;
X	if (ret == Nullch || strnNE(buf, "********", 8)) {
X	    next_intuit_at(line_beginning,p_input_line);
X	    return FALSE;
X	}
X	p_context = 100;
X	p_hunk_beg = p_input_line + 1;
X	while (p_end < p_max) {
X	    line_beginning = ftell(pfp);
X	    ret = pgets(buf, sizeof buf, pfp);
X	    p_input_line++;
X	    if (ret == Nullch) {
X		if (p_max - p_end < 4)
X		    Strcpy(buf, "  \n");  /* assume blank lines got chopped */
X		else {
X		    if (repl_beginning && repl_could_be_missing) {
X			repl_missing = TRUE;
X			goto hunk_done;
X		    }
X		    fatal1("Unexpected end of file in patch.\n");
X		}
X	    }
X	    p_end++;
X	    assert(p_end < hunkmax);
X	    p_char[p_end] = *buf;
X#ifdef zilog
X	    p_line[(short)p_end] = Nullch;
X#else
X	    p_line[p_end] = Nullch;
X#endif
X	    switch (*buf) {
X	    case '*':
X		if (strnEQ(buf, "********", 8)) {
X		    if (repl_beginning && repl_could_be_missing) {
X			repl_missing = TRUE;
X			goto hunk_done;
X		    }
X		    else
X			fatal2("Unexpected end of hunk at line %ld.\n",
X			    p_input_line);
X		}
X		if (p_end != 0) {
X		    if (repl_beginning && repl_could_be_missing) {
X			repl_missing = TRUE;
X			goto hunk_done;
X		    }
X		    fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
X		}
X		context = 0;
X		p_line[p_end] = savestr(buf);
X		if (out_of_mem) {
X		    p_end--;
X		    return FALSE;
X		}
X		for (s=buf; *s && !isdigit(*s); s++) ;
X		if (!*s)
X		    goto malformed;
X		if (strnEQ(s,"0,0",3))
X		    strcpy(s,s+2);
X		p_first = (LINENUM) atol(s);
X		while (isdigit(*s)) s++;
X		if (*s == ',') {
X		    for (; *s && !isdigit(*s); s++) ;
X		    if (!*s)
X			goto malformed;
X		    p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
X		}
X		else if (p_first)
X		    p_ptrn_lines = 1;
X		else {
X		    p_ptrn_lines = 0;
X		    p_first = 1;
X		}
X		p_max = p_ptrn_lines + 6;	/* we need this much at least */
X		while (p_max >= hunkmax)
X		    grow_hunkmax();
X		p_max = hunkmax;
X		break;
X	    case '-':
X		if (buf[1] == '-') {
X		    if (repl_beginning ||
X			(p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
X		    {
X			if (p_end == 1) {
X			    /* `old' lines were omitted - set up to fill */
X			    /* them in from 'new' context lines. */
X			    p_end = p_ptrn_lines + 1;
X			    fillsrc = p_end + 1;
X			    filldst = 1;
X			    fillcnt = p_ptrn_lines;
X			}
X			else {
X			    if (repl_beginning) {
X				if (repl_could_be_missing){
X				    repl_missing = TRUE;
X				    goto hunk_done;
X				}
X				fatal3(
X"Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
X				    p_input_line, p_hunk_beg + repl_beginning);
X			    }
X			    else {
X				fatal4(
X"%s \"---\" at line %ld--check line numbers at line %ld.\n",
X				    (p_end <= p_ptrn_lines
X					? "Premature"
X					: "Overdue" ),
X				    p_input_line, p_hunk_beg);
X			    }
X			}
X		    }
X		    repl_beginning = p_end;
X		    repl_backtrack_position = ftell(pfp);
X		    repl_patch_line = p_input_line;
X		    p_line[p_end] = savestr(buf);
X		    if (out_of_mem) {
X			p_end--;
X			return FALSE;
X		    }
X		    p_char[p_end] = '=';
X		    for (s=buf; *s && !isdigit(*s); s++) ;
X		    if (!*s)
X			goto malformed;
X		    p_newfirst = (LINENUM) atol(s);
X		    while (isdigit(*s)) s++;
X		    if (*s == ',') {
X			for (; *s && !isdigit(*s); s++) ;
X			if (!*s)
X			    goto malformed;
X			p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
X		    }
X		    else if (p_newfirst)
X			p_repl_lines = 1;
X		    else {
X			p_repl_lines = 0;
X			p_newfirst = 1;
X		    }
X		    p_max = p_repl_lines + p_end;
X		    if (p_max > MAXHUNKSIZE)
X			fatal4("Hunk too large (%ld lines) at line %ld: %s",
X			      p_max, p_input_line, buf);
X		    while (p_max >= hunkmax)
X			grow_hunkmax();
X		    if (p_repl_lines != ptrn_copiable)
X			repl_could_be_missing = FALSE;
X		    break;
X		}
X		goto change_line;
X	    case '+':  case '!':
X		repl_could_be_missing = FALSE;
X	      change_line:
X		if (buf[1] == '\n' && canonicalize)
X		    strcpy(buf+1," \n");
X		if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
X		  repl_beginning && repl_could_be_missing) {
X		    repl_missing = TRUE;
X		    goto hunk_done;
X		}
X		if (context > 0) {
X		    if (context < p_context)
X			p_context = context;
X		    context = -1000;
X		}
X		p_line[p_end] = savestr(buf+2);
X		if (out_of_mem) {
X		    p_end--;
X		    return FALSE;
X		}
X		break;
X	    case '\t': case '\n':	/* assume the 2 spaces got eaten */
X		if (repl_beginning && repl_could_be_missing &&
X		  (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
X		    repl_missing = TRUE;
X		    goto hunk_done;
X		}
X		p_line[p_end] = savestr(buf);
X		if (out_of_mem) {
X		    p_end--;
X		    return FALSE;
X		}
X		if (p_end != p_ptrn_lines + 1) {
X		    ptrn_spaces_eaten |= (repl_beginning != 0);
X		    context++;
X		    if (!repl_beginning)
X			ptrn_copiable++;
X		    p_char[p_end] = ' ';
X		}
X		break;
X	    case ' ':
X		if (!isspace(buf[1]) &&
X		  repl_beginning && repl_could_be_missing) {
X		    repl_missing = TRUE;
X		    goto hunk_done;
X		}
X		context++;
X		if (!repl_beginning)
X		    ptrn_copiable++;
X		p_line[p_end] = savestr(buf+2);
X		if (out_of_mem) {
X		    p_end--;
X		    return FALSE;
X		}
X		break;
X	    default:
X		if (repl_beginning && repl_could_be_missing) {
X		    repl_missing = TRUE;
X		    goto hunk_done;
X		}
X		goto malformed;
X	    }
X	    /* set up p_len for strncmp() so we don't have to */
X	    /* assume null termination */
X	    if (p_line[p_end])
X		p_len[p_end] = strlen(p_line[p_end]);
X	    else
X		p_len[p_end] = 0;
X	}
X	
X    hunk_done:
X	if (p_end >=0 && !repl_beginning)
X	    fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
X
X	if (repl_missing) {
X	    
X	    /* reset state back to just after --- */
X	    p_input_line = repl_patch_line;
X	    for (p_end--; p_end > repl_beginning; p_end--)
X		free(p_line[p_end]);
X	    Fseek(pfp, repl_backtrack_position, 0);
X	    
X	    /* redundant 'new' context lines were omitted - set */
X	    /* up to fill them in from the old file context */
X	    fillsrc = 1;
X	    filldst = repl_beginning+1;
X	    fillcnt = p_repl_lines;
X	    p_end = p_max;
X	}
X
X	if (diff_type == CONTEXT_DIFF &&
X	  (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
X	    if (verbose)
X		say4("%s\n%s\n%s\n",
X"(Fascinating--this is really a new-style context diff but without",
X"the telltale extra asterisks on the *** line that usually indicate",
X"the new style...)");
X	    diff_type = NEW_CONTEXT_DIFF;
X	}
X	
X	/* if there were omitted context lines, fill them in now */
X	if (fillcnt) {
X	    p_bfake = filldst;		/* remember where not to free() */
X	    p_efake = filldst + fillcnt - 1;
X	    while (fillcnt-- > 0) {
X		while (fillsrc <= p_end && p_char[fillsrc] != ' ')
X		    fillsrc++;
X		if (fillsrc > p_end)
X		    fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
X			p_hunk_beg);
X		p_line[filldst] = p_line[fillsrc];
X		p_char[filldst] = p_char[fillsrc];
X		p_len[filldst] = p_len[fillsrc];
X		fillsrc++; filldst++;
X	    }
X	    while (fillsrc <= p_end && fillsrc != repl_beginning &&
X	      p_char[fillsrc] != ' ')
X		fillsrc++;
X#ifdef DEBUGGING
X	    if (debug & 64)
X		printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
X		    fillsrc,filldst,repl_beginning,p_end+1);
X#endif
X	    assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
X	    assert(filldst==p_end+1 || filldst==repl_beginning);
X	}
X    }
X    else {				/* normal diff--fake it up */
X	char hunk_type;
X	Reg3 int i;
X	LINENUM min, max;
X	long line_beginning = ftell(pfp);
X
X	p_context = 0;
X	ret = pgets(buf, sizeof buf, pfp);
X	p_input_line++;
X	if (ret == Nullch || !isdigit(*buf)) {
X	    next_intuit_at(line_beginning,p_input_line);
X	    return FALSE;
X	}
X	p_first = (LINENUM)atol(buf);
X	for (s=buf; isdigit(*s); s++) ;
X	if (*s == ',') {
X	    p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
X	    while (isdigit(*s)) s++;
X	}
X	else
X	    p_ptrn_lines = (*s != 'a');
X	hunk_type = *s;
X	if (hunk_type == 'a')
X	    p_first++;			/* do append rather than insert */
X	min = (LINENUM)atol(++s);
X	for (; isdigit(*s); s++) ;
X	if (*s == ',')
X	    max = (LINENUM)atol(++s);
X	else
X	    max = min;
X	if (hunk_type == 'd')
X	    min++;
X	p_end = p_ptrn_lines + 1 + max - min + 1;
X	if (p_end > MAXHUNKSIZE)
X	    fatal4("Hunk too large (%ld lines) at line %ld: %s",
X		  p_end, p_input_line, buf);
X	while (p_end >= hunkmax)
X	    grow_hunkmax();
X	p_newfirst = min;
X	p_repl_lines = max - min + 1;
X	Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
X	p_line[0] = savestr(buf);
X	if (out_of_mem) {
X	    p_end = -1;
X	    return FALSE;
X	}
X	p_char[0] = '*';
X	for (i=1; i<=p_ptrn_lines; i++) {
X	    ret = pgets(buf, sizeof buf, pfp);
X	    p_input_line++;
X	    if (ret == Nullch)
X		fatal2("Unexpected end of file in patch at line %ld.\n",
X		  p_input_line);
X	    if (*buf != '<')
X		fatal2("< expected at line %ld of patch.\n", p_input_line);
X	    p_line[i] = savestr(buf+2);
X	    if (out_of_mem) {
X		p_end = i-1;
X		return FALSE;
X	    }
X	    p_len[i] = strlen(p_line[i]);
X	    p_char[i] = '-';
X	}
X	if (hunk_type == 'c') {
X	    ret = pgets(buf, sizeof buf, pfp);
X	    p_input_line++;
X	    if (ret == Nullch)
X		fatal2("Unexpected end of file in patch at line %ld.\n",
X		    p_input_line);
X	    if (*buf != '-')
X		fatal2("--- expected at line %ld of patch.\n", p_input_line);
X	}
X	Sprintf(buf, "--- %ld,%ld\n", min, max);
X	p_line[i] = savestr(buf);
X	if (out_of_mem) {
X	    p_end = i-1;
X	    return FALSE;
X	}
X	p_char[i] = '=';
X	for (i++; i<=p_end; i++) {
X	    ret = pgets(buf, sizeof buf, pfp);
X	    p_input_line++;
X	    if (ret == Nullch)
X		fatal2("Unexpected end of file in patch at line %ld.\n",
X		    p_input_line);
X	    if (*buf != '>')
X		fatal2("> expected at line %ld of patch.\n", p_input_line);
X	    p_line[i] = savestr(buf+2);
X	    if (out_of_mem) {
X		p_end = i-1;
X		return FALSE;
X	    }
X	    p_len[i] = strlen(p_line[i]);
X	    p_char[i] = '+';
X	}
X    }
X    if (reverse)			/* backwards patch? */
X	if (!pch_swap())
X	    say1("Not enough memory to swap next hunk!\n");
X#ifdef DEBUGGING
X    if (debug & 2) {
X	int i;
X	char special;
X
X	for (i=0; i <= p_end; i++) {
X	    if (i == p_ptrn_lines)
X		special = '^';
X	    else
X		special = ' ';
X	    fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
X	    Fflush(stderr);
X	}
X    }
X#endif
X    if (p_end+1 < hunkmax)	/* paranoia reigns supreme... */
X	p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
X    return TRUE;
X
Xmalformed:
X    fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
X		/* about as informative as "Syntax error" in C */
X    return FALSE;	/* for lint */
X}
X
X/* Input a line from the patch file, worrying about indentation. */
X
Xchar *
Xpgets(bf,sz,fp)
Xchar *bf;
Xint sz;
XFILE *fp;
X{
X    char *ret = fgets(bf, sz, fp);
X    Reg1 char *s;
X    Reg2 int indent = 0;
X
X    if (p_indent && ret != Nullch) {
X	for (s=buf;
X	  indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
X	    if (*s == '\t')
X		indent += 8 - (indent % 7);
X	    else
X		indent++;
X	}
X	if (buf != s)
X	    Strcpy(buf, s);
X    }
X    return ret;
X}
X
X/* Reverse the old and new portions of the current hunk. */
X
Xbool
Xpch_swap()
X{
X    char **tp_line;		/* the text of the hunk */
X    short *tp_len;		/* length of each line */
X    char *tp_char;		/* +, -, and ! */
X    Reg1 LINENUM i;
X    Reg2 LINENUM n;
X    bool blankline = FALSE;
X    Reg3 char *s;
X
X    i = p_first;
X    p_first = p_newfirst;
X    p_newfirst = i;
X    
X    /* make a scratch copy */
X
X    tp_line = p_line;
X    tp_len = p_len;
X    tp_char = p_char;
X    p_line = Null(char**);	/* force set_hunkmax to allocate again */
X    p_len = Null(short*);
X    p_char = Nullch;
X    set_hunkmax();
X    if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
X#ifndef lint
X	if (p_line == Null(char**))
X	    free((char*)p_line);
X	p_line = tp_line;
X	if (p_len == Null(short*))
X	    free((char*)p_len);
X	p_len = tp_len;
X#endif
X	if (p_char == Nullch)
X	    free((char*)p_char);
X	p_char = tp_char;
X	return FALSE;		/* not enough memory to swap hunk! */
X    }
X
X    /* now turn the new into the old */
X
X    i = p_ptrn_lines + 1;
X    if (tp_char[i] == '\n') {		/* account for possible blank line */
X	blankline = TRUE;
X	i++;
X    }
X    if (p_efake >= 0) {			/* fix non-freeable ptr range */
X	if (p_efake <= i)
X	    n = p_end - i + 1;
X	else
X	    n = -i;
X	p_efake += n;
X	p_bfake += n;
X    }
X    for (n=0; i <= p_end; i++,n++) {
X	p_line[n] = tp_line[i];
X	p_char[n] = tp_char[i];
X	if (p_char[n] == '+')
X	    p_char[n] = '-';
X	p_len[n] = tp_len[i];
X    }
X    if (blankline) {
X	i = p_ptrn_lines + 1;
X	p_line[n] = tp_line[i];
X	p_char[n] = tp_char[i];
X	p_len[n] = tp_len[i];
X	n++;
X    }
X    assert(p_char[0] == '=');
X    p_char[0] = '*';
X    for (s=p_line[0]; *s; s++)
X	if (*s == '-')
X	    *s = '*';
X
X    /* now turn the old into the new */
X
X    assert(tp_char[0] == '*');
X    tp_char[0] = '=';
X    for (s=tp_line[0]; *s; s++)
X	if (*s == '*')
X	    *s = '-';
X    for (i=0; n <= p_end; i++,n++) {
X	p_line[n] = tp_line[i];
X	p_char[n] = tp_char[i];
X	if (p_char[n] == '-')
X	    p_char[n] = '+';
X	p_len[n] = tp_len[i];
X    }
X    assert(i == p_ptrn_lines + 1);
X    i = p_ptrn_lines;
X    p_ptrn_lines = p_repl_lines;
X    p_repl_lines = i;
X#ifndef lint
X    if (tp_line == Null(char**))
X	free((char*)tp_line);
X    if (tp_len == Null(short*))
X	free((char*)tp_len);
X#endif
X    if (tp_char == Nullch)
X	free((char*)tp_char);
X    return TRUE;
X}
X
X/* Return the specified line position in the old file of the old context. */
X
XLINENUM
Xpch_first()
X{
X    return p_first;
X}
X
X/* Return the number of lines of old context. */
X
XLINENUM
Xpch_ptrn_lines()
X{
X    return p_ptrn_lines;
X}
X
X/* Return the probable line position in the new file of the first line. */
X
XLINENUM
Xpch_newfirst()
X{
X    return p_newfirst;
X}
X
X/* Return the number of lines in the replacement text including context. */
X
XLINENUM
Xpch_repl_lines()
X{
X    return p_repl_lines;
X}
X
X/* Return the number of lines in the whole hunk. */
X
XLINENUM
Xpch_end()
X{
X    return p_end;
X}
X
X/* Return the number of context lines before the first changed line. */
X
XLINENUM
Xpch_context()
X{
X    return p_context;
X}
X
X/* Return the length of a particular patch line. */
X
Xshort
Xpch_line_len(line)
XLINENUM line;
X{
X    return p_len[line];
X}
X
X/* Return the control character (+, -, *, !, etc) for a patch line. */
X
Xchar
Xpch_char(line)
XLINENUM line;
X{
X    return p_char[line];
X}
X
X/* Return a pointer to a particular patch line. */
X
Xchar *
Xpfetch(line)
XLINENUM line;
X{
X    return p_line[line];
X}
X
X/* Return where in the patch file this hunk began, for error messages. */
X
XLINENUM
Xpch_hunk_beg()
X{
X    return p_hunk_beg;
X}
X
X/* Apply an ed script by feeding ed itself. */
X
Xvoid
Xdo_ed_script()
X{
X    Reg1 char *t;
X    Reg2 long beginning_of_this_line;
X    Reg3 bool this_line_is_command = FALSE;
X    Reg4 FILE *pipefp;
X    FILE *popen();
X
X    if (!skip_rest_of_patch) {
X	Unlink(TMPOUTNAME);
X	copy_file(filearg[0], TMPOUTNAME);
X	if (verbose)
X	    Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
X	else
X	    Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
X	pipefp = popen(buf, "w");
X    }
X    for (;;) {
X	beginning_of_this_line = ftell(pfp);
X	if (pgets(buf, sizeof buf, pfp) == Nullch) {
X	    next_intuit_at(beginning_of_this_line,p_input_line);
X	    break;
X	}
X	p_input_line++;
X	for (t=buf; isdigit(*t) || *t == ','; t++) ;
X	this_line_is_command = (isdigit(*buf) &&
X	  (*t == 'd' || *t == 'c' || *t == 'a') );
X	if (this_line_is_command) {
X	    if (!skip_rest_of_patch)
X		fputs(buf, pipefp);
X	    if (*t != 'd') {
X		while (pgets(buf, sizeof buf, pfp) != Nullch) {
X		    p_input_line++;
X		    if (!skip_rest_of_patch)
X			fputs(buf, pipefp);
X		    if (strEQ(buf, ".\n"))
X			break;
X		}
X	    }
X	}
X	else {
X	    next_intuit_at(beginning_of_this_line,p_input_line);
X	    break;
X	}
X    }
X    if (skip_rest_of_patch)
X	return;
X    fprintf(pipefp, "w\n");
X    fprintf(pipefp, "q\n");
X    Fflush(pipefp);
X    Pclose(pipefp);
X    ignore_signals();
X    if (move_file(TMPOUTNAME, outname) < 0) {
X	toutkeep = TRUE;
X	chmod(TMPOUTNAME, filemode);
X    }
X    else
X	chmod(outname, filemode);
X    set_signals(1);
X}
SHAR_EOF
if test 28639 -ne "`wc -c < 'pch.c'`"
then
	echo shar: "error transmitting 'pch.c'" '(should have been 28639 characters)'
fi
fi
echo shar: "extracting 'pch.h'" '(780 characters)'
if test -f 'pch.h'
then
	echo shar: "will not over-write existing file 'pch.h'"
else
sed 's/^X//' << \SHAR_EOF > 'pch.h'
X/* $Header: pch.h,v 2.0.1.1 87/01/30 22:47:16 lwall Exp $
X *
X * $Log:	pch.h,v $
X * Revision 2.0.1.1  87/01/30  22:47:16  lwall
X * Added do_ed_script().
X * 
X * Revision 2.0  86/09/17  15:39:57  lwall
X * Baseline for netwide release.
X * 
X */
X
XEXT FILE *pfp INIT(Nullfp);		/* patch file pointer */
X
Xvoid re_patch();
Xvoid open_patch_file();
Xvoid set_hunkmax();
Xvoid grow_hunkmax();
Xbool there_is_another_patch();
Xint intuit_diff_type();
Xvoid next_intuit_at();
Xvoid skip_to();
Xbool another_hunk();
Xbool pch_swap();
Xchar *pfetch();
Xshort pch_line_len();
XLINENUM pch_first();
XLINENUM pch_ptrn_lines();
XLINENUM pch_newfirst();
XLINENUM pch_repl_lines();
XLINENUM pch_end();
XLINENUM pch_context();
XLINENUM pch_hunk_beg();
Xchar pch_char();
Xchar *pfetch();
Xchar *pgets();
Xvoid do_ed_script();
SHAR_EOF
if test 780 -ne "`wc -c < 'pch.h'`"
then
	echo shar: "error transmitting 'pch.h'" '(should have been 780 characters)'
fi
fi
echo shar: "extracting 'util.c'" '(7226 characters)'
if test -f 'util.c'
then
	echo shar: "will not over-write existing file 'util.c'"
else
sed 's/^X//' << \SHAR_EOF > 'util.c'
X#include "EXTERN.h"
X#include "common.h"
X#include "INTERN.h"
X#include "util.h"
X
X/* Rename a file, copying it if necessary. */
X
Xint
Xmove_file(from,to)
Xchar *from, *to;
X{
X    char bakname[512];
X    Reg1 char *s;
X    Reg2 int i;
X    Reg3 int fromfd;
X
X    /* to stdout? */
X
X    if (strEQ(to, "-")) {
X#ifdef DEBUGGING
X	if (debug & 4)
X	    say2("Moving %s to stdout.\n", from);
X#endif
X	fromfd = open(from, 0);
X	if (fromfd < 0)
X	    fatal2("patch: internal error, can't reopen %s\n", from);
X	while ((i=read(fromfd, buf, sizeof buf)) > 0)
X	    if (write(1, buf, i) != 1)
X		fatal1("patch: write failed\n");
X	Close(fromfd);
X	return 0;
X    }
X
X	if (origprae) {
X		Strcpy (bakname, origprae);
X    	Strcat(bakname, to);
X	} else {
X   		Strcpy(bakname, to);
X    	Strcat(bakname, origext?origext:ORIGEXT);
X	}
X    if (stat(to, &filestat) >= 0) {	/* output file exists */
X	dev_t to_device = filestat.st_dev;
X	ino_t to_inode  = filestat.st_ino;
X	char *simplename = bakname;
X	
X	for (s=bakname; *s; s++) {
X	    if (*s == '/')
X		simplename = s+1;
X	}
X	/* find a backup name that is not the same file */
X	while (stat(bakname, &filestat) >= 0 &&
X		to_device == filestat.st_dev && to_inode == filestat.st_ino) {
X	    for (s=simplename; *s && !islower(*s); s++) ;
X	    if (*s)
X		*s = toupper(*s);
X	    else
X		Strcpy(simplename, simplename+1);
X	}
X	while (unlink(bakname) >= 0) ;	/* while() is for benefit of Eunice */
X#ifdef DEBUGGING
X	if (debug & 4)
X	    say3("Moving %s to %s.\n", to, bakname);
X#endif
X	if (link(to, bakname) < 0) {
X	    say3("patch: can't backup %s, output is in %s\n",
X		to, from);
X	    return -1;
X	}
X	while (unlink(to) >= 0) ;
X    }
X#ifdef DEBUGGING
X    if (debug & 4)
X	say3("Moving %s to %s.\n", from, to);
X#endif
X    if (link(from, to) < 0) {		/* different file system? */
X	Reg4 int tofd;
X	
X	tofd = creat(to, 0666);
X	if (tofd < 0) {
X	    say3("patch: can't create %s, output is in %s.\n",
X	      to, from);
X	    return -1;
X	}
X	fromfd = open(from, 0);
X	if (fromfd < 0)
X	    fatal2("patch: internal error, can't reopen %s\n", from);
X	while ((i=read(fromfd, buf, sizeof buf)) > 0)
X	    if (write(tofd, buf, i) != i)
X		fatal1("patch: write failed\n");
X	Close(fromfd);
X	Close(tofd);
X    }
X    Unlink(from);
X    return 0;
X}
X
X/* Copy a file. */
X
Xvoid
Xcopy_file(from,to)
Xchar *from, *to;
X{
X    Reg3 int tofd;
X    Reg2 int fromfd;
X    Reg1 int i;
X    
X    tofd = creat(to, 0666);
X    if (tofd < 0)
X	fatal2("patch: can't create %s.\n", to);
X    fromfd = open(from, 0);
X    if (fromfd < 0)
X	fatal2("patch: internal error, can't reopen %s\n", from);
X    while ((i=read(fromfd, buf, sizeof buf)) > 0)
X	if (write(tofd, buf, i) != i)
X	    fatal2("patch: write (%s) failed\n", to);
X    Close(fromfd);
X    Close(tofd);
X}
X
X/* Allocate a unique area for a string. */
X
Xchar *
Xsavestr(s)
XReg1 char *s;
X{
X    Reg3 char *rv;
X    Reg2 char *t;
X
X    if (!s)
X	s = "Oops";
X    t = s;
X    while (*t++);
X    rv = malloc((MEM) (t - s));
X    if (rv == Nullch) {
X	if (using_plan_a)
X	    out_of_mem = TRUE;
X	else
X	    fatal1("patch: out of memory (savestr)\n");
X    }
X    else {
X	t = rv;
X	while (*t++ = *s++);
X    }
X    return rv;
X}
X
X#if defined(lint) && defined(CANVARARG)
X
X/*VARARGS ARGSUSED*/
Xsay(pat) char *pat; { ; }
X/*VARARGS ARGSUSED*/
Xfatal(pat) char *pat; { ; }
X/*VARARGS ARGSUSED*/
Xask(pat) char *pat; { ; }
X
X#else
X
X/* Vanilla terminal output (buffered). */
X
Xvoid
Xsay(pat,arg1,arg2,arg3)
Xchar *pat;
Xlong arg1,arg2,arg3;
X{
X    fprintf(stderr, pat, arg1, arg2, arg3);
X    Fflush(stderr);
X}
X
X/* Terminal output, pun intended. */
X
Xvoid				/* very void */
Xfatal(pat,arg1,arg2,arg3)
Xchar *pat;
Xlong arg1,arg2,arg3;
X{
X    void my_exit();
X
X    say(pat, arg1, arg2, arg3);
X    my_exit(1);
X}
X
X/* Get a response from the user, somehow or other. */
X
Xvoid
Xask(pat,arg1,arg2,arg3)
Xchar *pat;
Xlong arg1,arg2,arg3;
X{
X    int ttyfd;
X    int r;
X    bool tty2 = isatty(2);
X
X    Sprintf(buf, pat, arg1, arg2, arg3);
X    Fflush(stderr);
X    write(2, buf, strlen(buf));
X    if (tty2) {				/* might be redirected to a file */
X	r = read(2, buf, sizeof buf);
X    }
X    else if (isatty(1)) {		/* this may be new file output */
X	Fflush(stdout);
X	write(1, buf, strlen(buf));
X	r = read(1, buf, sizeof buf);
X    }
X    else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) {
X					/* might be deleted or unwriteable */
X	write(ttyfd, buf, strlen(buf));
X	r = read(ttyfd, buf, sizeof buf);
X	Close(ttyfd);
X    }
X    else if (isatty(0)) {		/* this is probably patch input */
X	Fflush(stdin);
X	write(0, buf, strlen(buf));
X	r = read(0, buf, sizeof buf);
X    }
X    else {				/* no terminal at all--default it */
X	buf[0] = '\n';
X	r = 1;
X    }
X    if (r <= 0)
X	buf[0] = 0;
X    else
X	buf[r] = '\0';
X    if (!tty2)
X	say1(buf);
X}
X#endif /* lint */
X
X/* How to handle certain events when not in a critical region. */
X
Xvoid
Xset_signals(reset)
Xint reset;
X{
X    void my_exit();
X#ifndef lint
X#ifdef VOIDSIG
X    static void (*hupval)(),(*intval)();
X#else
X    static int (*hupval)(),(*intval)();
X#endif
X
X    if (!reset) {
X	hupval = signal(SIGHUP, SIG_IGN);
X	if (hupval != SIG_IGN)
X	    hupval = my_exit;
X	intval = signal(SIGINT, SIG_IGN);
X	if (intval != SIG_IGN)
X	    intval = my_exit;
X    }
X    Signal(SIGHUP, hupval);
X    Signal(SIGINT, intval);
X#endif
X}
X
X/* How to handle certain events when in a critical region. */
X
Xvoid
Xignore_signals()
X{
X#ifndef lint
X    Signal(SIGHUP, SIG_IGN);
X    Signal(SIGINT, SIG_IGN);
X#endif
X}
X
X/* Make sure we'll have the directories to create a file. */
X
Xvoid
Xmakedirs(filename,striplast)
XReg1 char *filename;
Xbool striplast;
X{
X    char tmpbuf[256];
X    Reg2 char *s = tmpbuf;
X    char *dirv[20];
X    Reg3 int i;
X    Reg4 int dirvp = 0;
X
X    while (*filename) {
X	if (*filename == '/') {
X	    filename++;
X	    dirv[dirvp++] = s;
X	    *s++ = '\0';
X	}
X	else {
X	    *s++ = *filename++;
X	}
X    }
X    *s = '\0';
X    dirv[dirvp] = s;
X    if (striplast)
X	dirvp--;
X    if (dirvp < 0)
X	return;
X    strcpy(buf, "mkdir");
X    s = buf;
X    for (i=0; i<=dirvp; i++) {
X	while (*s) s++;
X	*s++ = ' ';
X	strcpy(s, tmpbuf);
X	*dirv[i] = '/';
X    }
X    system(buf);
X}
X
X/* Make filenames more reasonable. */
X
Xchar *
Xfetchname(at,strip_leading,assume_exists)
Xchar *at;
Xint strip_leading;
Xint assume_exists;
X{
X    char *s;
X    char *name;
X    Reg1 char *t;
X    char tmpbuf[200];
X
X    if (!at)
X	return Nullch;
X    s = savestr(at);
X    for (t=s; isspace(*t); t++) ;
X    name = t;
X#ifdef DEBUGGING
X    if (debug & 128)
X	say4("fetchname %s %d %d\n",name,strip_leading,assume_exists);
X#endif
X    if (strnEQ(name, "/dev/null", 9))	/* so files can be created by diffing */
X	return Nullch;			/*   against /dev/null. */
X    for (; *t && !isspace(*t); t++)
X	if (*t == '/')
X	    if (--strip_leading >= 0)
X		name = t+1;
X    *t = '\0';
X    if (name != s && *s != '/') {
X	name[-1] = '\0';
X	if (stat(s, &filestat) && filestat.st_mode & S_IFDIR) {
X	    name[-1] = '/';
X	    name=s;
X	}
X    }
X    name = savestr(name);
X    Sprintf(tmpbuf, "RCS/%s", name);
X    free(s);
X    if (stat(name, &filestat) < 0 && !assume_exists) {
X	Strcat(tmpbuf, RCSSUFFIX);
X	if (stat(tmpbuf, &filestat) < 0 && stat(tmpbuf+4, &filestat) < 0) {
X	    Sprintf(tmpbuf, "SCCS/%s%s", SCCSPREFIX, name);
X	    if (stat(tmpbuf, &filestat) < 0 && stat(tmpbuf+5, &filestat) < 0) {
X		free(name);
X		name = Nullch;
X	    }
X	}
X    }
X    return name;
X}
SHAR_EOF
if test 7226 -ne "`wc -c < 'util.c'`"
then
	echo shar: "error transmitting 'util.c'" '(should have been 7226 characters)'
fi
fi
exit 0
#	End of shell archive

greg@csanta.UUCP (Greg Comeau) (06/14/88)

In article <409@kosman.UUCP> kevin@kosman.UUCP (Kevin O'Gorman) writes:
>   char *s = rindex(rejname,'/');
>
>The compiler is complaining about the initializer on the declaration of
>variable s, and I don't blame it.

It is most likely complaining because rindex() has either not been
declared or because rindex is not declared a 'char *' and hence there
is a mismatched pointer.

rindex() should be declares in a .h file somewhere that probably didn't
get include.

wilber@alice.UUCP (06/18/88)

I got patch v2.0-11 running on my system but I did encounter a few glitches.
First, rindex must be defined as strrchr.  (I believe that the artificially
intelligent configuration script automatically defines index as strchr for USG
systems, but it misses rindex.)  Second, define rindex (alias strrchr) as
being of type char *.  Finally, the configuration script decided that my
compiler was smart about handling pointers to functions but I found it
necessary to tell it that it's dumb (can only handle arrays of pointers to
functions).  Otherwise compiler errors appeared.

I also had to uncomment the stuff in the 7300 makefile that creates assert.o.
As I recall there were comments in the makefile about how this is not
necessary because _assert has been added to the shared library.  This is news
to me.  Where does that fix come from?

Bob Wilber   Work: UUCP: {allegra, mtune, ihnp4}!gauss!wilber
                   ARPA: wilber@research.att.com
             Home: UUCP: {allegra, mtune, ihnp4}!gauss!heaven!wilber
                   ARPA: heaven!wilber%gauss@att.arpa