]> git.xonotic.org Git - xonotic/xonotic.git/commitdiff
Merge branch 'master' of git://nl.git.xonotic.org/xonotic/xonotic
authormerlijn <mhofstra@gmail.com>
Sun, 20 May 2012 16:44:13 +0000 (18:44 +0200)
committermerlijn <mhofstra@gmail.com>
Sun, 20 May 2012 16:44:13 +0000 (18:44 +0200)
Makefile
misc/tools/all/release.subr
misc/tools/all/xonotic.subr
misc/tools/conflict-watch.sh [new file with mode: 0755]
misc/tools/midi2cfg/tuba-settings.cfg
misc/tools/progs-analyzer.pl [new file with mode: 0644]

index 8be69c8e9ec54aa3834efdd33485431d1ea7d1bb..18861e38c146f17c679e5961830d5f19f4cbf80e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,4 @@
+DESTDIR ?= 
 PREFIX ?= /usr/local
 BINDIR ?= $(PREFIX)/bin
 LIBDIR ?= $(PREFIX)/lib/xonotic
@@ -6,8 +7,11 @@ ZIP ?= zip -9
 INSTALL ?= install
 ARCH ?= $(shell if [ x"`uname -m`" = x"x86_64" ]; then echo linux64; else echo linux32; fi)
 LN ?= ln
-SUFFIX ?= $(shell if [ -d .git ]; then echo git; else echo zip; fi)
 CP ?= cp
+BINARY ?= yes
+SUFFIX ?= $(shell if [ -d .git ]; then echo git; elif [ x"$(BINARY)" = x"yes" ]; then echo zip-binary; else echo zip-source; fi)
+RIJNDAELDETECT_CONFIGURE ?= $(shell if ! [ -f source/d0_blind_id/d0_rijndael.c ]; then echo --disable-rijndael; fi)
+RIJNDAELDETECT_MAKE_DP ?= $(shell if [ -f source/d0_blind_id/d0_rijndael.c ]; then echo DP_CRYPTO_RIJNDAEL_STATIC_LIBDIR=$(CURDIR)/source/d0_blind_id/.libs; fi)
 
 
 .PHONY: all
@@ -17,10 +21,19 @@ all: all-$(SUFFIX)
 all-git:
        ./all compile -r
 
-.PHONY: all-zip
-all-zip:
-       @echo Sorry, this is not implemented yet
-       @false
+.PHONY: all-zip-binary
+all-zip-binary:
+       @echo Nothing to do
+
+.PHONY: all-zip-source
+all-zip-source:
+       ( cd source/d0_blind_id && ./configure --enable-static --disable-shared $(RIJNDAELDETECT_CONFIGURE) )
+       $(MAKE) -C source/d0_blind_id
+       $(MAKE) -C source/fteqcc
+       $(MAKE) -C source/qcsrc FTEQCC=$(CURDIR)/source/fteqcc/fteqcc.bin
+       $(MAKE) -C source/darkplaces sv-release DP_CRYPTO_STATIC_LIBDIR=$(CURDIR)/source/d0_blind_id/.libs
+       $(MAKE) -C source/darkplaces cl-release DP_CRYPTO_STATIC_LIBDIR=$(CURDIR)/source/d0_blind_id/.libs
+       $(MAKE) -C source/darkplaces sdl-release DP_CRYPTO_STATIC_LIBDIR=$(CURDIR)/source/d0_blind_id/.libs
 
 
 .PHONY: clean
@@ -31,65 +44,88 @@ clean-git:
        ./all clean
 
 .PHONY: clean-zip
-clean-zip:
-       @echo Sorry, this is not implemented yet
-       @false
+clean-zip-binary:
+       @echo Nothing to do
+
+.PHONY: clean-zip
+clean-zip-source:
+       -$(MAKE) -C source/d0_blind_id distclean
+       $(MAKE) -C source/fteqcc clean
+       $(MAKE) -C source/qcsrc clean
+       $(MAKE) -C source/darkplaces clean
 
 
 .PHONY: install-data
 install-data: install-data-$(SUFFIX)
 
 .PHONY: install-data-git
-install-data-git:
-       $(RM) -rf $(LIBDIR)/data
-       $(INSTALL) -d $(LIBDIR)/data
-       for p in data/*.pk3; do $(INSTALL) $$p $(LIBDIR)/$$p || exit 1; done
-       for p in data/*.pk3dir; do ( cd $$p; $(ZIP) -r $(LIBDIR)/$${p%dir} * ) || exit 1; done
-
-.PHONY: install-data-zip
-install-data-zip:
-       $(RM) -rf $(LIBDIR)/data
-       $(INSTALL) -d $(LIBDIR)/data
-       for p in data/*.pk3; do $(INSTALL) $$p $(LIBDIR)/$$p || exit 1; done
+install-data-git: all-git
+       $(RM) -rf $(DESTDIR)$(LIBDIR)/data
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)/data
+       for p in data/*.pk3; do $(INSTALL) $$p $(DESTDIR)$(LIBDIR)/$$p || exit 1; done
+       for p in data/*.pk3dir; do ( cd $$p && $(ZIP) -r $(DESTDIR)$(LIBDIR)/$${p%dir} * ) || exit 1; done
+
+.PHONY: install-data-zip-binary
+install-data-zip-binary: all-zip-binary
+       $(RM) -rf $(DESTDIR)$(LIBDIR)/data
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)/data
+       for p in data/*.pk3; do $(INSTALL) $$p $(DESTDIR)$(LIBDIR)/$$p || exit 1; done
+
+.PHONY: install-data-zip-source
+install-data-zip-source: all-zip-source
+       $(RM) -rf $(DESTDIR)$(LIBDIR)/data
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)/data
+       for p in data/*.pk3; do $(INSTALL) $$p $(DESTDIR)$(LIBDIR)/$$p || exit 1; done
+       for p in data/xonotic-*-data*.pk3; do cd source && $(ZIP) $(DESTDIR)$(LIBDIR)/$$p progs.dat menu.dat csprogs.dat; done
 
 
 .PHONY: install-engine
 install-engine: install-engine-$(SUFFIX)
 
 .PHONY: install-engine-git
-install-engine-git:
-       $(INSTALL) -d $(LIBDIR)
-       $(INSTALL) xonotic-linux-glx.sh $(LIBDIR)/xonotic-linux-glx.sh
-       $(INSTALL) xonotic-linux-sdl.sh $(LIBDIR)/xonotic-linux-sdl.sh
-       $(INSTALL) xonotic-linux-dedicated.sh $(LIBDIR)/xonotic-linux-dedicated.sh
-       $(INSTALL) darkplaces/darkplaces-sdl $(LIBDIR)/xonotic-$(ARCH)-sdl
-       $(INSTALL) darkplaces/darkplaces-glx $(LIBDIR)/xonotic-$(ARCH)-glx
-       $(INSTALL) darkplaces/darkplaces-dedicated $(LIBDIR)/xonotic-$(ARCH)-dedicated
-
-.PHONY: install-engine-zip
-install-engine-zip:
-       $(INSTALL) -d $(LIBDIR)
-       $(INSTALL) xonotic-linux-glx.sh $(LIBDIR)/xonotic-linux-glx.sh
-       $(INSTALL) xonotic-linux-sdl.sh $(LIBDIR)/xonotic-linux-sdl.sh
-       $(INSTALL) xonotic-linux-dedicated.sh $(LIBDIR)/xonotic-linux-dedicated.sh
-       $(INSTALL) xonotic-$(ARCH)-sdl $(LIBDIR)/xonotic-$(ARCH)-sdl
-       $(INSTALL) xonotic-$(ARCH)-glx $(LIBDIR)/xonotic-$(ARCH)-glx
-       $(INSTALL) xonotic-$(ARCH)-dedicated $(LIBDIR)/xonotic-$(ARCH)-dedicated
+install-engine-git: all-git
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)
+       $(INSTALL) xonotic-linux-glx.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-glx.sh
+       $(INSTALL) xonotic-linux-sdl.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-sdl.sh
+       $(INSTALL) xonotic-linux-dedicated.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-dedicated.sh
+       $(INSTALL) darkplaces/darkplaces-sdl $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-sdl
+       $(INSTALL) darkplaces/darkplaces-glx $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-glx
+       $(INSTALL) darkplaces/darkplaces-dedicated $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-dedicated
+
+.PHONY: install-engine-zip-binary
+install-engine-zip-binary: all-zip-binary
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)
+       $(INSTALL) xonotic-linux-glx.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-glx.sh
+       $(INSTALL) xonotic-linux-sdl.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-sdl.sh
+       $(INSTALL) xonotic-linux-dedicated.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-dedicated.sh
+       $(INSTALL) xonotic-$(ARCH)-sdl $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-sdl
+       $(INSTALL) xonotic-$(ARCH)-glx $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-glx
+       $(INSTALL) xonotic-$(ARCH)-dedicated $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-dedicated
+
+.PHONY: install-engine-zip-source
+install-engine-zip-source: all-zip-source
+       $(INSTALL) -d $(DESTDIR)$(LIBDIR)
+       $(INSTALL) xonotic-linux-glx.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-glx.sh
+       $(INSTALL) xonotic-linux-sdl.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-sdl.sh
+       $(INSTALL) xonotic-linux-dedicated.sh $(DESTDIR)$(LIBDIR)/xonotic-linux-dedicated.sh
+       $(INSTALL) source/darkplaces/darkplaces-sdl $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-sdl
+       $(INSTALL) source/darkplaces/darkplaces-glx $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-glx
+       $(INSTALL) source/darkplaces/darkplaces-dedicated $(DESTDIR)$(LIBDIR)/xonotic-$(ARCH)-dedicated
 
 
 .PHONY: install-links
 install-links:
        $(INSTALL) -d $(BINDIR)
-       $(LN) -snf $(LIBDIR)/xonotic-$(ARCH)-sdl $(BINDIR)/xonotic-sdl
-       $(LN) -snf $(LIBDIR)/xonotic-$(ARCH)-glx $(BINDIR)/xonotic-glx
-       $(LN) -snf $(LIBDIR)/xonotic-$(ARCH)-dedicated $(BINDIR)/xonotic-dedicated
+       $(LN) -snf $(LIBDIR)/xonotic-linux-sdl.sh $(DESTDIR)$(BINDIR)/xonotic-sdl
+       $(LN) -snf $(LIBDIR)/xonotic-linux-glx.sh $(DESTDIR)$(BINDIR)/xonotic-glx
+       $(LN) -snf $(LIBDIR)/xonotic-linux-dedicated.sh $(DESTDIR)$(BINDIR)/xonotic-dedicated
 
 
 .PHONY: install-doc
 install-doc:
-       $(INSTALL) -d $(DOCDIR)/server
-       $(CP) -R Docs/* $(DOCDIR)/
-       $(CP) -R server/* $(DOCDIR)/server
+       $(INSTALL) -d $(DESTDIR)$(DOCDIR)/server
+       $(CP) -R Docs/* $(DESTDIR)$(DOCDIR)/
+       $(CP) -R server/* $(DESTDIR)$(DOCDIR)/server
 
 
 .PHONY: install
index 69f54b241c3581ac57dbc78efd2b9ccf2b11917d..e97bddb2595e270435d9dad2b9ae864b4f8f5dd8 100644 (file)
@@ -170,7 +170,7 @@ case "$cmd" in
                        verbose date +%Y%m%d > Xonotic/stamp.txt
                        verbose date +%Y%m%d > Xonotic/pk3stamp.txt
                fi
-               release_git_extract_dir "." "Xonotic" Docs misc server xonotic-linux-glx.sh xonotic-linux-sdl.sh misc/buildfiles key_0.d0pk COPYING GPL-2 GPL-3
+               release_git_extract_dir "." "Xonotic" Docs misc server xonotic-linux-glx.sh xonotic-linux-sdl.sh xonotic-linux-dedicated.sh Makefile misc/buildfiles key_0.d0pk COPYING GPL-2 GPL-3
                (
                        verbose cd Xonotic
                        verbose mkdir data fteqcc source source/darkplaces source/fteqcc source/d0_blind_id mapping
@@ -293,14 +293,14 @@ case "$cmd" in
        release-engine-linux32)
                release_common
                verbose "$SELF" release-compile linux32 \
-                       'STRIP=: CC="gcc -m32 -march=i686 -g1 -I.deps/include -L.deps/lib -DSUPPORTIPV6" DP_MODPLUG_STATIC_LIBDIR=.deps/lib LIB_JPEG=.deps/lib/libjpeg.a DP_CRYPTO_STATIC_LIBDIR=.deps/lib' \
+                       'STRIP=: CC="gcc -m32 -march=i686 -g1 -I.deps/include -L.deps/lib -DSUPPORTIPV6" DP_MODPLUG_STATIC_LIBDIR=.deps/lib LIB_JPEG=.deps/lib/libjpeg.a DP_CRYPTO_STATIC_LIBDIR=.deps/lib DP_GMP_STATIC_LIBDIR=.deps/lib' \
                        all 'fteqcc.bin:Xonotic/fteqcc/fteqcc.linux32' \
                        release 'darkplaces-glx:Xonotic/xonotic-linux32-glx darkplaces-sdl:Xonotic/xonotic-linux32-sdl darkplaces-dedicated:Xonotic/xonotic-linux32-dedicated'
                ;;
        release-engine-linux64)
                release_common
                verbose "$SELF" release-compile linux64 \
-                       'STRIP=: CC="gcc -m64 -g1 -I.deps/include -L.deps/lib -DSUPPORTIPV6" DP_MODPLUG_STATIC_LIBDIR=.deps/lib LIB_JPEG=.deps/lib/libjpeg.a DP_CRYPTO_STATIC_LIBDIR=.deps/lib' \
+                       'STRIP=: CC="gcc -m64 -g1 -I.deps/include -L.deps/lib -DSUPPORTIPV6" DP_MODPLUG_STATIC_LIBDIR=.deps/lib LIB_JPEG=.deps/lib/libjpeg.a DP_CRYPTO_STATIC_LIBDIR=.deps/lib DP_GMP_STATIC_LIBDIR=.deps/lib' \
                        all 'fteqcc.bin:Xonotic/fteqcc/fteqcc.linux64' \
                        release 'darkplaces-glx:Xonotic/xonotic-linux64-glx darkplaces-sdl:Xonotic/xonotic-linux64-sdl darkplaces-dedicated:Xonotic/xonotic-linux64-dedicated'
                ;;
@@ -557,6 +557,7 @@ case "$cmd" in
                fi
                # build the archives
                verbose mkzip Xonotic-$stamp-enginesource.zip \
+                       Xonotic/Makefile \
                        Xonotic/source/darkplaces/ \
                        Xonotic/COPYING Xonotic/GPL-2 Xonotic/GPL-3
                verbose cp Xonotic-$stamp-enginesource.zip Xonotic-$stamp-engine.zip
index dca4a33de4ae70b9eb081692961c5a9693e24854..e18d7fe2f9d0c84efac34c9a7b95c6cd30ddc73b 100644 (file)
@@ -166,13 +166,13 @@ case "$cmd" in
                                                                #verbose cp "$d0/misc/builddeps/dp.linux64/lib/libd0_blind_id".* .libs/
                                                                #verbose cp "$d0/misc/builddeps/dp.linux64/lib/libd0_rijndael".* .libs/
                                                                #verbose cp "$d0/misc/builddeps/dp.linux64/lib/libgmp".* .libs/
-                                                               MAKEFLAGS="$MAKEFLAGS DP_CRYPTO_STATIC_LIBDIR=../misc/builddeps/dp.linux64/lib/ DP_CRYPTO_RIJNDAEL_STATIC_LIBDIR=../misc/builddeps/dp.linux64/lib/"
+                                                               MAKEFLAGS="$MAKEFLAGS DP_CRYPTO_STATIC_LIBDIR=../misc/builddeps/dp.linux64/lib/ DP_CRYPTO_RIJNDAEL_STATIC_LIBDIR=../misc/builddeps/dp.linux64/lib/ DP_GMP_STATIC_LIBDIR=../misc/builddeps/dp.linux64/lib/"
                                                                ;;
                                                        *86)
                                                                #verbose cp "$d0/misc/builddeps/dp.linux32/lib/libd0_blind_id".* .libs/
                                                                #verbose cp "$d0/misc/builddeps/dp.linux32/lib/libd0_rijndael".* .libs/
                                                                #verbose cp "$d0/misc/builddeps/dp.linux32/lib/libgmp".* .libs/
-                                                               MAKEFLAGS="$MAKEFLAGS DP_CRYPTO_STATIC_LIBDIR=../misc/builddeps/dp.linux32/lib/ DP_CRYPTO_RIJNDAEL_STATIC_LIBDIR=../misc/builddeps/dp.linux32/lib/"
+                                                               MAKEFLAGS="$MAKEFLAGS DP_CRYPTO_STATIC_LIBDIR=../misc/builddeps/dp.linux32/lib/ DP_CRYPTO_RIJNDAEL_STATIC_LIBDIR=../misc/builddeps/dp.linux32/lib/ DP_GMP_STATIC_LIBDIR=../misc/builddeps/dp.linux32/lib/"
                                                                ;;
                                                        *)
                                                                compiled0=true
diff --git a/misc/tools/conflict-watch.sh b/misc/tools/conflict-watch.sh
new file mode 100755 (executable)
index 0000000..d1c4208
--- /dev/null
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+set -e
+
+action=$1
+outdir=$2
+repodir=$3
+
+branches()
+{
+       git for-each-ref 'refs/remotes' | grep -vE '    refs/remotes/([^/]*/HEAD|.*/archived/.*)$'
+}
+
+escape_html()
+{
+       sed -e 's/&/\&amp;/g; s/</&lt;/g; s/>/&gt;/g'
+}
+
+to_rss()
+{
+       outdir=$1
+       name=$2
+       masterhash=$3
+       masterbranch=$4
+       hash=$5
+       branch=$6
+       repo=$7
+
+       filename=`echo -n "$name" | tr -c 'A-Za-z0-9' '_'`.rss
+       outfilename="$outdir/$filename"
+       masterbranch=`echo -n "$masterbranch" | escape_html`
+       branch=`echo -n "$branch" | escape_html`
+       repo=`echo -n "$repo" | escape_html`
+
+       if [ -n "$repo" ]; then
+               repotxt=" in $repo"
+       else
+               repotxt=
+       fi
+       against="$masterbranch at $masterhash"
+
+       if ! [ -f "$outfilename" ]; then
+               datetime=`date --rfc-2822`
+               cat >"$outfilename" <<EOF
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
+<channel>
+       <title>XonCW: $name</title>
+       <link>http://git.xonotic.org/</link>
+       <description>Xonotic Conflict Watch for branches by $name</description>
+       <ttl>10800</ttl>
+       <atom:link href="http://nl.git.xonotic.org/xoncw/$filename" rel="self" type="application/rss+xml" />
+       <lastBuildDate>$datetime</lastBuildDate>
+EOF
+       fi
+       cat >>"$outfilename" <<EOF
+       <item>
+               <title>$branch$repotxt</title>
+               <link>http://git.xonotic.org/?p=$repo;a=shortlog;h=refs/heads/$branch</link>
+               <guid isPermaLink="false">http://nl.git.xonotic.org/xoncw/$filename#$hash</guid>
+               <description><![CDATA[
+               Conflicts of $branch at $hash against $against:
+EOF
+       echo -n "<pre>" >>"$outfilename"
+       escape_html >>"$outfilename"
+       echo "</pre>" >>"$outfilename"
+
+       cat >>"$outfilename" <<EOF
+               ]]></description>
+       </item>
+EOF
+}
+
+clear_rss()
+{
+       datetime=`date --rfc-2822`
+       sed -i -e '/<lastBuildDate>/,$d' "$1"
+       cat <<EOF >>"$1"
+       <lastBuildDate>$datetime</lastBuildDate>
+EOF
+}
+
+finish_rss()
+{
+       cat <<EOF >>"$1"
+</channel>
+</rss>
+EOF
+}
+
+if [ -z "$outdir" ]; then
+       set --
+fi
+
+repo=$(
+       (
+               if [ -n "$repodir" ]; then
+                       cd "$repodir"
+               fi
+               git config remote.origin.url | cut -d / -f 4-
+       )
+)
+
+case "$action" in
+       --init)
+               mkdir -p "$outdir"
+               for f in "$outdir"/*; do
+                       [ -f "$f" ] || continue
+                       clear_rss "$f"
+               done
+               ;;
+       --finish)
+               for f in "$outdir"/*; do
+                       [ -f "$f" ] || continue
+                       finish_rss "$f"
+               done
+               ;;
+       --add)
+               masterhash=$(
+                       (
+                               if [ -n "$repodir" ]; then
+                                       cd "$repodir"
+                               fi
+                               git rev-parse HEAD
+                       )
+               )
+               masterbranch=$(
+                       (
+                               if [ -n "$repodir" ]; then
+                                       cd "$repodir"
+                               fi
+                               git symbolic-ref HEAD
+                       )
+               )
+               masterbranch=${masterbranch#refs/heads/}
+
+               masterhash2=$(
+                       (
+                               if [ -n "$repodir" ]; then
+                                       cd "$repodir"
+                               fi
+                               git rev-parse master
+                       )
+               )
+               masterbranch2=master
+
+               (
+                       if [ -n "$repodir" ]; then
+                               cd "$repodir"
+                       fi
+                       branches
+               ) | while read -r HASH TYPE REFNAME; do
+                       echo >&2 -n "$repo $REFNAME..."
+                       case "$repo:$REFNAME" in
+                               xonotic/netradiant.git:refs/remotes/origin/divVerent/zeroradiant) continue ;;
+                               xonotic/netradiant.git:refs/remotes/origin/divVerent/zeroradiant-original) continue ;;
+                               xonotic/netradiant.git:refs/remotes/origin/divVerent/zeroradiant-split-up-the-q3map2-commit) continue ;;
+                               xonotic/netradiant.git:refs/remotes/origin/divVerent/zeroradiant-split-up-the-q3map2-commit-goal) continue ;;
+                               xonotic/fteqcc.git:refs/remotes/origin/wip) continue ;;
+                               xonotic/darkplaces.git:refs/remotes/origin/dp-mqc-render) continue ;;
+                       esac
+
+                       if [ x"$masterhash" = x"$masterhash2" ]; then
+                               thismasterhash=$masterhash
+                               thismasterbranch=$masterbranch
+                       else
+                               l=$(
+                                       if [ -n "$repodir" ]; then
+                                               cd "$repodir"
+                                       fi
+                                       git rev-list "$masterhash".."$REFNAME" | wc -l
+                               )
+                               l2=$(
+                                       if [ -n "$repodir" ]; then
+                                               cd "$repodir"
+                                       fi
+                                       git rev-list "$masterhash2".."$REFNAME" | wc -l
+                               )
+                               if [ $l -gt $l2 ]; then
+                                       thismasterhash=$masterhash2
+                                       thismasterbranch=$masterbranch2
+                               else
+                                       thismasterhash=$masterhash
+                                       thismasterbranch=$masterbranch
+                               fi
+                       fi
+                               
+                       out=$(
+                               (
+                                       if [ -n "$repodir" ]; then
+                                               cd "$repodir"
+                                       fi
+                                       git reset --hard "$thismasterhash" >/dev/null 2>&1
+                                       if out=`git merge --no-commit -- "$REFNAME" 2>&1`; then
+                                               good=true
+                                       else
+                                               good=false
+                                               echo "$out"
+                                       fi
+                                       git reset --hard "$masterhash" >/dev/null 2>&1
+                               )
+                       )
+                       if [ -n "$out" ]; then
+                               b=${REFNAME#refs/remotes/[!/]*/}
+                               case "$b" in
+                                       */*)
+                                               n=${b%%/*}
+                                               ;;
+                                       *)
+                                               n=divVerent
+                                               ;;
+                               esac
+                               echo "$out" | to_rss "$outdir" "$n" "$thismasterhash" "$thismasterbranch" "$HASH" "$b" "$repo"
+                               echo >&2 " CONFLICT"
+                       else
+                               echo >&2 " ok"
+                       fi
+               done
+               ;;
+       *)
+               echo "Usage: $0 --init OUTDIR"
+               echo "       $0 --add OUTDIR [REPODIR]"
+               echo "       $0 --finish OUTDIR"
+               exit 1
+               ;;
+esac
index 772ab3b87bb6f2de99669f481633f2615d121434..daeba7271a295840b48be21318408f94a86d7c6d 100644 (file)
@@ -23,3 +23,8 @@ r_usedepthtextures 1 // workaround for broken shadowmaps
 r_shadows 2
 r_shadow_shadowmapping 1
 snd_soundradius 3200
+
+// HUD
+hud_panel_pressedkeys_aspect 1.07
+hud_panel_pressedkeys_attack 1
+hud_panel_pressedkeys_bg_padding -16 // HACK to enlarge the pressedkeys view
diff --git a/misc/tools/progs-analyzer.pl b/misc/tools/progs-analyzer.pl
new file mode 100644 (file)
index 0000000..6ba7de8
--- /dev/null
@@ -0,0 +1,1379 @@
+use strict;
+use warnings;
+use Digest::SHA;
+use Carp;
+
+sub id()
+{
+       return sub { $_[0]; };
+}
+
+sub signed($)
+{
+       my ($bits) = @_;
+       return sub { $_[0] >= (2**($bits-1)) ? $_[0]-(2**$bits) : $_[0]; };
+}
+
+use constant OPCODE_E => [qw[
+       DONE
+       MUL_F MUL_V MUL_FV MUL_VF
+       DIV_F
+       ADD_F ADD_V
+       SUB_F SUB_V
+       EQ_F EQ_V EQ_S EQ_E EQ_FNC
+       NE_F NE_V NE_S NE_E NE_FNC
+       LE GE LT GT
+       LOAD_F LOAD_V LOAD_S LOAD_ENT LOAD_FLD LOAD_FNC
+       ADDRESS
+       STORE_F STORE_V STORE_S STORE_ENT STORE_FLD STORE_FNC
+       STOREP_F STOREP_V STOREP_S STOREP_ENT STOREP_FLD STOREP_FNC
+       RETURN
+       NOT_F NOT_V NOT_S NOT_ENT NOT_FNC
+       IF IFNOT
+       CALL0 CALL1 CALL2 CALL3 CALL4 CALL5 CALL6 CALL7 CALL8
+       STATE
+       GOTO
+       AND OR
+       BITAND BITOR
+]];
+use constant ETYPE_E => [qw[
+       void
+       string
+       float
+       vector
+       entity
+       field
+       function
+       pointer
+]];
+use constant DEF_SAVEGLOBAL => 32768;
+sub typesize($)
+{
+       my ($type) = @_;
+       return 3 if $type eq 'vector';
+       return 1;
+}
+
+sub checkop($)
+{
+       my ($op) = @_;
+       if($op =~ /^IF.*_V$/)
+       {
+               return { a => 'inglobalvec', b => 'ipoffset', isjump => 'b', isconditional => 1 };
+       }
+       if($op =~ /^IF/)
+       {
+               return { a => 'inglobal', b => 'ipoffset', isjump => 'b', isconditional => 1 };
+       }
+       if($op eq 'GOTO')
+       {
+               return { a => 'ipoffset', isjump => 'a', isconditional => 0 };
+       }
+       if($op =~ /^ADD_V$|^SUB_V$/)
+       {
+               return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobalvec' };
+       }
+       if($op =~ /^MUL_V$|^EQ_V$|^NE_V$/)
+       {
+               return { a => 'inglobalvec', b => 'inglobalvec', c => 'outglobal' };
+       }
+       if($op eq 'MUL_FV')
+       {
+               return { a => 'inglobal', b => 'inglobalvec', c => 'outglobalvec' };
+       }
+       if($op eq 'MUL_VF')
+       {
+               return { a => 'inglobalvec', b => 'inglobal', c => 'outglobalvec' };
+       }
+       if($op eq 'LOAD_V')
+       {
+               return { a => 'inglobal', b => 'inglobal', c => 'outglobalvec' };
+       }
+       if($op =~ /^NOT_V/)
+       {
+               return { a => 'inglobalvec', c => 'outglobal' };
+       }
+       if($op =~ /^NOT_/)
+       {
+               return { a => 'inglobal', c => 'outglobal' };
+       }
+       if($op eq 'STOREP_V')
+       {
+               return { a => 'inglobalvec', b => 'inglobal' };
+       }
+       if($op eq 'STORE_V')
+       {
+               return { a => 'inglobalvec', b => 'outglobalvec' };
+       }
+       if($op =~ /^STOREP_/)
+       {
+               return { a => 'inglobal', b => 'inglobal' };
+       }
+       if($op =~ /^STORE_/)
+       {
+               return { a => 'inglobal', b => 'outglobal' };
+       }
+       if($op =~ /^CALL/)
+       {
+               return { a => 'inglobalfunc', iscall => 1 };
+       }
+       if($op =~ /^DONE$|^RETURN$/)
+       {
+               return { a => 'inglobalvec', isreturn => 1 };
+       }
+       if($op eq 'STATE')
+       {
+               return { a => 'inglobal', b => 'inglobalfunc' };
+       }
+       return { a => 'inglobal', b => 'inglobal', c => 'outglobal' };
+}
+
+use constant TYPES => {
+       int => ['V', 4, signed 32],
+       ushort => ['v', 2, id],
+       short => ['v', 2, signed 16],
+       opcode => ['v', 2, sub { OPCODE_E->[$_[0]] or do { warn "Invalid opcode: $_[0]"; "INVALID#$_[0]"; }; }],
+       float => ['f', 4, id],
+       uchar8 => ['a8', 8, sub { [unpack 'C8', $_[0]] }],
+       global => ['i', 4, sub { { int => $_[0], float => unpack "f", pack "L", $_[0] }; }],
+       deftype => ['v', 2, sub { { type => ETYPE_E->[$_[0] & ~DEF_SAVEGLOBAL], save => !!($_[0] & DEF_SAVEGLOBAL) }; }],
+};
+
+use constant DPROGRAMS_T => [
+       [int => 'version'],
+       [int => 'crc'],
+       [int => 'ofs_statements'],
+       [int => 'numstatements'],
+       [int => 'ofs_globaldefs'],
+       [int => 'numglobaldefs'],
+       [int => 'ofs_fielddefs'],
+       [int => 'numfielddefs'],
+       [int => 'ofs_functions'],
+       [int => 'numfunctions'],
+       [int => 'ofs_strings'],
+       [int => 'numstrings'],
+       [int => 'ofs_globals'],
+       [int => 'numglobals'],
+       [int => 'entityfields']
+];
+
+use constant DSTATEMENT_T => [
+       [opcode => 'op'],
+       [short => 'a'],
+       [short => 'b'],
+       [short => 'c']
+];
+
+use constant DDEF_T => [
+       [deftype => 'type'],
+       [ushort => 'ofs'],
+       [int => 's_name']
+];
+
+use constant DGLOBAL_T => [
+       [global => 'v'],
+];
+
+use constant DFUNCTION_T => [
+       [int => 'first_statement'],
+       [int => 'parm_start'],
+       [int => 'locals'],
+       [int => 'profile'],
+       [int => 's_name'],
+       [int => 's_file'],
+       [int => 'numparms'],
+       [uchar8 => 'parm_size'],
+];
+
+sub get_section($$$)
+{
+       my ($fh, $start, $len) = @_;
+       seek $fh, $start, 0
+               or die "seek: $!";
+       $len == read $fh, my $buf, $len
+               or die "short read from $start length $len (malformed progs header)";
+       return $buf;
+}
+
+sub parse_section($$$$$)
+{
+       my ($fh, $struct, $start, $len, $cnt) = @_;
+
+       my $itemlen = 0;
+       $itemlen += TYPES->{$_->[0]}->[1]
+               for @$struct;
+       my $packspec = join '', map { TYPES->{$_->[0]}->[0]; } @$struct;
+       my @packnames = map { $_->[1]; } @$struct;
+
+       $len = $cnt * $itemlen
+               if not defined $len and defined $cnt;
+       $cnt = int($len / $itemlen)
+               if not defined $cnt and defined $len;
+       die "Invalid length specification"
+               unless defined $len and defined $cnt and $len == $cnt * $itemlen;
+       die "Invalid length specification in scalar context"
+               unless wantarray or $cnt == 1;
+
+       seek $fh, $start, 0
+               or die "seek: $!";
+       my @out = map
+       {
+               $itemlen == read $fh, my $buf, $itemlen
+                       or die "short read from $start length $cnt * $itemlen $(malformed progs header)";
+               my %h = ();
+               @h{@packnames} = unpack $packspec, $buf;
+               $h{$_->[1]} = TYPES->{$_->[0]}->[2]->($h{$_->[1]})
+                       for @$struct;
+               \%h;
+       }
+       0..($cnt-1);
+       return @out
+               if wantarray;
+       return $out[0];
+}
+
+sub nfa_default_state_checker()
+{
+       my %seen;
+       return sub
+       {
+               my ($ip, $state) = @_;
+               return $seen{"$ip $state"}++;
+       };
+}
+
+sub run_nfa($$$$$$)
+{
+       my ($progs, $ip, $state, $copy_handler, $state_checker, $instruction_handler) = @_;
+
+       my $statements = $progs->{statements};
+
+       my $nfa;
+       $nfa = sub
+       {
+               no warnings 'recursion';
+
+               my ($ip, $state) = @_;
+               my $ret = 0;
+
+               for(;;)
+               {
+                       return $ret
+                               if $state_checker->($ip, $state);
+
+                       my $s = $statements->[$ip];
+                       my $c = checkop $s->{op};
+
+                       if(($ret = $instruction_handler->($ip, $state, $s, $c)))
+                       {
+                               # abort execution
+                               last;
+                       }
+
+                       if($c->{isreturn})
+                       {
+                               last;
+                       }
+                       elsif($c->{iscall})
+                       {
+                               my $func = $s->{a};
+                               my $funcid = $progs->{globals}[$func]{v}{int};
+                               last
+                                       if $progs->{error_func}{$funcid};
+                               $ip += 1;
+                       }
+                       elsif($c->{isjump})
+                       {
+                               if($c->{isconditional})
+                               {
+                                       if(rand 2)
+                                       {
+                                               if(($ret = $nfa->($ip+$s->{$c->{isjump}}, $copy_handler->($state))) < 0)
+                                               {
+                                                       last;
+                                               }
+                                               $ip += 1;
+                                       }
+                                       else
+                                       {
+                                               $nfa->($ip+1, $copy_handler->($state));
+                                               $ip += $s->{$c->{isjump}};
+                                       }
+                               }
+                               else
+                               {
+                                       $ip += $s->{$c->{isjump}};
+                               }
+                       }
+                       else
+                       {
+                               $ip += 1;
+                       }
+               }
+
+               return $ret;
+       };
+
+       $nfa->($ip, $copy_handler->($state));
+}
+
+sub get_constant($$)
+{
+       my ($progs, $g) = @_;
+       if($g->{int} == 0)
+       {
+               return 0;
+       }
+       elsif($g->{int} > 0 && $g->{int} < 8388608)
+       {
+               if($g->{int} < length $progs->{strings} && $g->{int} > 0)
+               {
+                       return str($progs->{getstring}->($g->{int}));
+               }
+               else
+               {
+                       return $g->{int} . "i";
+               }
+       }
+       else
+       {
+               return $g->{float};
+       }
+}
+
+use constant PRE_MARK_STATEMENT => "";
+use constant POST_MARK_STATEMENT => "";
+use constant PRE_MARK_OPERAND => "*** ";
+use constant POST_MARK_OPERAND => " ***";
+
+use constant INSTRUCTION_FORMAT => "%8s %3s | %-12s ";
+use constant OPERAND_FORMAT => "%s";
+use constant OPERAND_SEPARATOR => ", ";
+use constant INSTRUCTION_SEPARATOR => "\n";
+
+sub str($)
+{
+       my ($str) = @_;
+       $str =~ s/[\000-\037\\\"\177-\377]/sprintf "\\%03o", ord $&/ge;
+       return "\"$str\"";
+}
+
+sub disassemble_function($$;$)
+{
+       my ($progs, $func, $highlight) = @_;
+
+       print "$func->{debugname}:\n";
+
+       if($func->{first_statement} < 0) # builtin
+       {
+               printf INSTRUCTION_FORMAT, '', '', '.BUILTIN';
+               printf OPERAND_FORMAT, -$func->{first_statement};
+               print INSTRUCTION_SEPARATOR;
+               return;
+       }
+
+       my $initializer = sub
+       {
+               my ($ofs) = @_;
+               my $g = get_constant($progs, $progs->{globals}[$ofs]{v});
+               print " = $g"
+                       if defined $g;
+       };
+
+       printf INSTRUCTION_FORMAT, '', '', '.PARM_START';
+       printf OPERAND_FORMAT, "$func->{parm_start}";
+       print INSTRUCTION_SEPARATOR;
+
+       printf INSTRUCTION_FORMAT, '', '', '.LOCALS';
+       printf OPERAND_FORMAT, "$func->{locals}";
+       print INSTRUCTION_SEPARATOR;
+
+       my %override_locals = ();
+       my $p = $func->{parm_start};
+       for(0..($func->{numparms}-1))
+       {
+               $override_locals{$p} //= "argv_$_";
+               for my $comp(0..($func->{parm_size}[$_]-1))
+               {
+                       $override_locals{$p} //= "argv_$_\[$comp]";
+                       ++$p;
+               }
+               printf INSTRUCTION_FORMAT, '', '', '.ARG';
+               printf OPERAND_FORMAT, "argv_$_";
+               print OPERAND_SEPARATOR;
+               printf OPERAND_FORMAT, $func->{parm_size}[$_];
+               print INSTRUCTION_SEPARATOR;
+       }
+       for($func->{parm_start}..($func->{parm_start} + $func->{locals} - 1))
+       {
+               next
+                       if exists $override_locals{$_};
+               $override_locals{$_} = "local_$_";
+
+               printf INSTRUCTION_FORMAT, '', '', '.LOCAL';
+               printf OPERAND_FORMAT, "local_$_";
+               $initializer->($_);
+               print INSTRUCTION_SEPARATOR;
+       }
+
+       my $getname = sub
+       {
+               my ($ofs) = @_;
+               return $override_locals{$ofs}
+                       if exists $override_locals{$ofs};
+               my $def = $progs->{globaldef_byoffset}->($ofs);
+               return $def->{debugname};
+       };
+
+       my $operand = sub
+       {
+               my ($ip, $type, $operand) = @_;
+               if($type eq 'inglobal')
+               {
+                       my $name = $getname->($operand);
+                       printf OPERAND_FORMAT, "$name";
+               }
+               elsif($type eq 'outglobal')
+               {
+                       my $name = $getname->($operand);
+                       printf OPERAND_FORMAT, "&$name";
+               }
+               elsif($type eq 'inglobalvec')
+               {
+                       my $name = $getname->($operand);
+                       printf OPERAND_FORMAT, "$name\[\]";
+               }
+               elsif($type eq 'outglobalvec')
+               {
+                       my $name = $getname->($operand);
+                       printf OPERAND_FORMAT, "&$name\[\]";
+               }
+               elsif($type eq 'inglobalfunc')
+               {
+                       my $name = $getname->($operand);
+                       printf OPERAND_FORMAT, "$name()";
+               }
+               elsif($type eq 'ipoffset')
+               {
+                       printf OPERAND_FORMAT, "@{[$ip + $operand]}" . sprintf ' ($%+d)', $operand;
+               }
+               else
+               {
+                       die "unknown type: $type";
+               }
+       };
+
+       my $statements = $func->{statements};
+       my $come_from = $func->{come_from};
+
+       my $ipprev = undef;
+       for my $ip(sort { $a <=> $b } keys %$statements)
+       {
+               if($ip == $func->{first_statement})
+               {
+                       printf INSTRUCTION_FORMAT, $ip, '', '.ENTRY';
+                       print INSTRUCTION_SEPARATOR;
+               }
+               if(defined $ipprev && $ip != $ipprev + 1)
+               {
+                       printf INSTRUCTION_FORMAT, $ip, '', '.SKIP';
+                       printf OPERAND_FORMAT, $ip - $ipprev - 1;
+                       print INSTRUCTION_SEPARATOR;
+               }
+               if(my $cf = $come_from->{$ip})
+               {
+                       printf INSTRUCTION_FORMAT, $ip, '', '.XREF';
+                       my $cnt = 0;
+                       for(sort { $a <=> $b } keys %$cf)
+                       {
+                               print OPERAND_SEPARATOR
+                                       if $cnt++;
+                               printf OPERAND_FORMAT, ($cf->{$_} ? 'c' : 'j') . $_ . sprintf ' ($%+d)', $_ - $ip;
+                       }
+                       print INSTRUCTION_SEPARATOR;
+               }
+
+               my $op = $progs->{statements}[$ip]{op};
+               my $ipt = $progs->{statements}[$ip];
+               my $opprop = checkop $op;
+
+               if($highlight and $highlight->{$ip})
+               {
+                       for(values %{$highlight->{$ip}})
+                       {
+                               for(@$_)
+                               {
+                                       print PRE_MARK_STATEMENT;
+                                       printf INSTRUCTION_FORMAT, '', '<!>', '.WARN';
+                                       printf OPERAND_FORMAT, "$_ (in $func->{debugname})";
+                                       print INSTRUCTION_SEPARATOR;
+                               }
+                       }
+               }
+
+               print PRE_MARK_STATEMENT
+                       if $highlight and $highlight->{$ip};
+
+               my $showip = $opprop->{isjump};
+               printf INSTRUCTION_FORMAT, $showip ? $ip : '', $highlight->{$ip} ? '<!>' : '', $op;
+
+               my $cnt = 0;
+               for my $o(qw(a b c))
+               {
+                       next
+                               if not defined $opprop->{$o};
+                       print OPERAND_SEPARATOR
+                               if $cnt++;
+                       print PRE_MARK_OPERAND
+                               if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
+                       $operand->($ip, $opprop->{$o}, $ipt->{$o});
+                       print POST_MARK_OPERAND
+                               if $highlight and $highlight->{$ip} and $highlight->{$ip}{$o};
+               }
+
+               print POST_MARK_STATEMENT
+                       if $highlight and $highlight->{$ip};
+
+               print INSTRUCTION_SEPARATOR;
+       }
+}
+
+sub find_uninitialized_locals($$)
+{
+       my ($progs, $func) = @_;
+
+       return
+               if $func->{first_statement} < 0; # builtin
+
+       print STDERR "Checking $func->{debugname}...\n";
+
+       my $p = $func->{parm_start};
+       for(0..($func->{numparms}-1))
+       {
+               $p += $func->{parm_size}[$_];
+       }
+
+       use constant WATCHME_R => 1;
+       use constant WATCHME_W => 2;
+       use constant WATCHME_X => 4;
+       use constant WATCHME_T => 8;
+       my %watchme = map { $_ => WATCHME_X } ($func->{parm_start} .. ($func->{parm_start} + $func->{locals} - 1));
+
+       for(keys %{$progs->{temps}})
+       {
+               next
+                       if exists $watchme{$_};
+               if($progs->{temps}{$_})
+               {
+                       # shared temp
+                       $watchme{$_} = WATCHME_T | WATCHME_X
+               }
+               else
+               {
+                       # unique temp
+                       $watchme{$_} = WATCHME_X
+               }
+       }
+
+       $watchme{$_} |= WATCHME_R
+               for keys %{$func->{globals_read}};
+       $watchme{$_} |= WATCHME_W
+               for keys %{$func->{globals_written}};
+
+       my %write_places = ();
+       for my $ofs(keys %{$func->{globals_written}})
+       {
+               next
+                       unless exists $watchme{$ofs} and $watchme{$ofs} & WATCHME_X;
+               for my $ip(keys %{$func->{globals_written}{$ofs}})
+               {
+                       for my $op(keys %{$func->{globals_written}{$ofs}{$ip}})
+                       {
+                               push @{$write_places{$ip}{$op}}, $ofs;
+                       }
+               }
+       }
+
+       for(keys %watchme)
+       {
+               delete $watchme{$_}
+                       if ($watchme{$_} & (WATCHME_R | WATCHME_W | WATCHME_X)) != (WATCHME_R | WATCHME_W | WATCHME_X);
+       }
+
+       return
+               if not keys %watchme;
+
+       for(keys %watchme)
+       {
+               $watchme{$_} = {
+                       flags => $watchme{$_},
+                       valid => [0, undef, undef]
+               };
+       }
+
+       # mark parameters as initialized
+       for($func->{parm_start} .. ($p-1))
+       {
+               $watchme{$_}{valid} = [1, undef, undef]
+                       if defined $watchme{$_};
+       }
+
+       my %warned = ();
+       my %ip_seen = ();
+       run_nfa $progs, $func->{first_statement}, \%watchme,
+               sub {
+                       my ($h) = @_;
+                       return { map { $_ => { %{$h->{$_}} } } keys %$h };
+               },
+               sub {
+                       my ($ip, $state) = @_;
+
+                       my $s = $ip_seen{$ip};
+                       if($s)
+                       {
+                               # if $state is stronger or equal to $s, return 1
+
+                               for(keys %$state)
+                               {
+                                       if($state->{$_}{valid}[0] < $s->{$_})
+                                       {
+                                               # The current state is LESS valid than the previously run one. We NEED to run this.
+                                               # The saved state can safely become the intersection [citation needed].
+                                               for(keys %$state)
+                                               {
+                                                       $s->{$_} = $state->{$_}{valid}[0]
+                                                               if $state->{$_}{valid}[0] < $s->{$_};
+                                               }
+                                               return 0;
+                                       }
+                               }
+                               # if we get here, $state is stronger or equal. No need to try it.
+                               return 1;
+                       }
+                       else
+                       {
+                               # Never seen this IP yet.
+                               $ip_seen{$ip} = { map { ($_ => $state->{$_}{valid}[0]); } keys %$state };
+                               return 0;
+                       }
+               },
+               sub {
+                       my ($ip, $state, $s, $c) = @_;
+                       my $op = $s->{op};
+
+                       # QCVM BUG: RETURN always takes vector, there is no float equivalent
+                       my $return_hack = $c->{isreturn} // 0;
+
+                       if($op eq 'STORE_V')
+                       {
+                               # COMPILER BUG of QCC: params are always copied using STORE_V
+                               if($s->{b} >= 4 && $s->{b} < 28) # parameter range
+                               {
+                                       $return_hack = 1;
+                               }
+                       }
+
+                       for(qw(a b c))
+                       {
+                               my $type = $c->{$_};
+                               next
+                                       unless defined $type;
+
+                               my $ofs = $s->{$_};
+
+                               my $read = sub
+                               {
+                                       my ($ofs) = @_;
+                                       ++$return_hack
+                                               if $return_hack;
+                                       return
+                                               if not exists $state->{$ofs};
+                                       my $valid = $state->{$ofs}{valid};
+                                       if($valid->[0] == 0)
+                                       {
+                                               # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
+                                               if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
+                                               {
+                                                       push @{$warned{$ip}{$_}}, "Use of uninitialized value";
+                                               }
+                                       }
+                                       elsif($valid->[0] < 0)
+                                       {
+                                               # COMPILER BUG of FTEQCC: AND and OR may take uninitialized as second argument (logicops)
+                                               if($return_hack <= 2 and ($op ne 'OR' && $op ne 'AND' || $_ ne 'b'))
+                                               {
+                                                       push @{$warned{$ip}{$_}}, "Use of temporary across CALL";
+                                               }
+                                       }
+                                       else
+                                       {
+                                               # it's VALID
+                                               if(defined $valid->[1])
+                                               {
+                                                       delete $write_places{$valid->[1]}{$valid->[2]};
+                                               }
+                                       }
+                               };
+                               my $write = sub
+                               {
+                                       my ($ofs) = @_;
+                                       $state->{$ofs}{valid} = [1, $ip, $_]
+                                               if exists $state->{$ofs};
+                               };
+
+                               if($type eq 'inglobal' || $type eq 'inglobalfunc')
+                               {
+                                       $read->($ofs);
+                               }
+                               elsif($type eq 'inglobalvec')
+                               {
+                                       $read->($ofs);
+                                       $read->($ofs+1);
+                                       $read->($ofs+2);
+                               }
+                               elsif($type eq 'outglobal')
+                               {
+                                       $write->($ofs);
+                               }
+                               elsif($type eq 'outglobalvec')
+                               {
+                                       $write->($ofs);
+                                       $write->($ofs+1);
+                                       $write->($ofs+2);
+                               }
+                       }
+                       if($c->{iscall})
+                       {
+                               # builtin calls may clobber stuff
+                               my $func = $s->{a};
+                               my $funcid = $progs->{globals}[$func]{v}{int};
+                               my $funcobj = $progs->{functions}[$funcid];
+                               if(!$funcobj || $funcobj->{first_statement} >= 0)
+                               {
+                                       # invalidate temps
+                                       for(values %$state)
+                                       {
+                                               if($_->{flags} & WATCHME_T)
+                                               {
+                                                       $_->{valid} = [-1, undef, undef];
+                                               }
+                                       }
+                               }
+                       }
+
+                       return 0;
+               };
+
+       for my $ip(keys %write_places)
+       {
+               for my $operand(keys %{$write_places{$ip}})
+               {
+                       # TODO verify it
+                       my %left = map { $_ => 1 } @{$write_places{$ip}{$operand}};
+                       my $isread = 0;
+
+                       my %writeplace_seen = ();
+                       run_nfa $progs, $ip+1, \%left,
+                               sub
+                               {
+                                       return { %{$_[0]} };
+                               },
+                               sub
+                               {
+                                       my ($ip, $state) = @_;
+                                       return $writeplace_seen{"$ip " . join " ", sort keys %$state}++;
+                               },
+                               sub
+                               {
+                                       my ($ip, $state, $s, $c) = @_;
+                                       for(qw(a b c))
+                                       {
+                                               my $type = $c->{$_};
+                                               next
+                                                       unless defined $type;
+
+                                               my $ofs = $s->{$_};
+                                               if($type eq 'inglobal' || $type eq 'inglobalfunc')
+                                               {
+                                                       if($state->{$ofs})
+                                                       {
+                                                               $isread = 1;
+                                                               return -1; # exit TOTALLY
+                                                       }
+                                               }
+                                               elsif($type eq 'inglobalvec')
+                                               {
+                                                       if($state->{$ofs} || $state->{$ofs+1} || $state->{$ofs+2})
+                                                       {
+                                                               $isread = 1;
+                                                               return -1; # exit TOTALLY
+                                                       }
+                                               }
+                                               elsif($type eq 'outglobal')
+                                               {
+                                                       delete $state->{$ofs};
+                                                       return 1
+                                                               if !%$state;
+                                               }
+                                               elsif($type eq 'outglobalvec')
+                                               {
+                                                       delete $state->{$ofs};
+                                                       delete $state->{$ofs+1};
+                                                       delete $state->{$ofs+2};
+                                                       return 1
+                                                               if !%$state;
+                                               }
+                                       }
+                                       return 0;
+                               };
+
+                       if(!$isread)
+                       {
+                               push @{$warned{$ip}{$operand}}, "Value is never used";
+                       }
+               }
+       }
+       
+       disassemble_function($progs, $func, \%warned)
+               if keys %warned;
+}
+
+use constant DEFAULTGLOBALS => [
+       "OFS_NULL",
+       "OFS_RETURN",
+       "OFS_RETURN[1]",
+       "OFS_RETURN[2]",
+       "OFS_PARM0",
+       "OFS_PARM0[1]",
+       "OFS_PARM0[2]",
+       "OFS_PARM1",
+       "OFS_PARM1[1]",
+       "OFS_PARM1[2]",
+       "OFS_PARM2",
+       "OFS_PARM2[1]",
+       "OFS_PARM2[2]",
+       "OFS_PARM3",
+       "OFS_PARM3[1]",
+       "OFS_PARM3[2]",
+       "OFS_PARM4",
+       "OFS_PARM4[1]",
+       "OFS_PARM4[2]",
+       "OFS_PARM5",
+       "OFS_PARM5[1]",
+       "OFS_PARM5[2]",
+       "OFS_PARM6",
+       "OFS_PARM6[1]",
+       "OFS_PARM6[2]",
+       "OFS_PARM7",
+       "OFS_PARM7[1]",
+       "OFS_PARM7[2]"
+];
+
+sub defaultglobal($)
+{
+       my ($ofs) = @_;
+       if($ofs < @{(DEFAULTGLOBALS)})
+       {
+               return { ofs => $ofs, s_name => undef, debugname => DEFAULTGLOBALS->[$ofs], type => undef };
+       }
+       return { ofs => $ofs, s_name => undef, debugname => "<undefined>\@$ofs", type => undef };
+}
+
+sub detect_constants($)
+{
+       my ($progs) = @_;
+       use constant GLOBALFLAG_R => 1; # read
+       use constant GLOBALFLAG_W => 2; # written
+       use constant GLOBALFLAG_S => 4; # saved
+       use constant GLOBALFLAG_I => 8; # initialized
+       use constant GLOBALFLAG_N => 16; # named
+       use constant GLOBALFLAG_Q => 32; # unique to function
+       use constant GLOBALFLAG_U => 64; # unused
+       use constant GLOBALFLAG_P => 128; # possibly parameter passing
+       my @globalflags = (GLOBALFLAG_Q | GLOBALFLAG_U) x @{$progs->{globals}};
+
+       for(@{$progs->{functions}})
+       {
+               for(keys %{$_->{globals_used}})
+               {
+                       if($globalflags[$_] & GLOBALFLAG_U)
+                       {
+                               $globalflags[$_] &= ~GLOBALFLAG_U;
+                       }
+                       elsif($globalflags[$_] & GLOBALFLAG_Q)
+                       {
+                               $globalflags[$_] &= ~GLOBALFLAG_Q;
+                       }
+               }
+               $globalflags[$_] |= GLOBALFLAG_R
+                       for keys %{$_->{globals_read}};
+               $globalflags[$_] |= GLOBALFLAG_W
+                       for keys %{$_->{globals_written}};
+               next
+                       if $_->{first_statement} < 0;
+               for my $ip($_->{first_statement} .. (@{$progs->{statements}}-1))
+               {
+                       my $s = $progs->{statements}[$ip];
+                       if($s->{op} eq 'STORE_V')
+                       {
+                               $globalflags[$s->{a}] |= GLOBALFLAG_P
+                                       if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
+                               $globalflags[$s->{a}+1] |= GLOBALFLAG_P
+                                       if $s->{b}+1 >= $_->{parm_start} and $s->{b}+1 < $_->{parm_start} + $_->{locals};
+                               $globalflags[$s->{a}+2] |= GLOBALFLAG_P
+                                       if $s->{b}+2 >= $_->{parm_start} and $s->{b}+2 < $_->{parm_start} + $_->{locals};
+                       }
+                       elsif($s->{op} =~ /^STORE_/)
+                       {
+                               $globalflags[$s->{a}] |= GLOBALFLAG_P
+                                       if $s->{b} >= $_->{parm_start} and $s->{b} < $_->{parm_start} + $_->{locals};
+                       }
+                       else
+                       {
+                               last;
+                       }
+               }
+       }
+
+       # parameter passing globals are only ever used in STORE_ instructions
+       for my $s(@{$progs->{statements}})
+       {
+               next
+                       if $s->{op} =~ /^STORE_/;
+
+               my $c = checkop $s->{op};
+
+               for(qw(a b c))
+               {
+                       my $type = $c->{$_};
+                       next
+                               unless defined $type;
+
+                       my $ofs = $s->{$_};
+                       if($type eq 'inglobal' || $type eq 'inglobalfunc' || $type eq 'outglobal')
+                       {
+                               $globalflags[$ofs] &= ~GLOBALFLAG_P;
+                       }
+                       if($type eq 'inglobalvec' || $type eq 'outglobalvec')
+                       {
+                               $globalflags[$ofs] &= ~GLOBALFLAG_P;
+                               $globalflags[$ofs+1] &= ~GLOBALFLAG_P;
+                               $globalflags[$ofs+2] &= ~GLOBALFLAG_P;
+                       }
+               }
+       }
+
+       my %offsets_saved = ();
+       for(@{$progs->{globaldefs}})
+       {
+               my $type = $_->{type};
+               my $name = $progs->{getstring}->($_->{s_name});
+               $name = ''
+                       if $name eq 'IMMEDIATE' or $name =~ /^\./;
+               $_->{debugname} = $name
+                       if $name ne '';
+               if($type->{save})
+               {
+                       for my $i(0..(typesize($_->{type}{type})-1))
+                       {
+                               $globalflags[$_->{ofs}] |= GLOBALFLAG_S;
+                       }
+               }
+               if($name ne '')
+               {
+                       for my $i(0..(typesize($_->{type}{type})-1))
+                       {
+                               $globalflags[$_->{ofs}] |= GLOBALFLAG_N;
+                       }
+               }
+       }
+       my %offsets_initialized = ();
+       for(0..(@{$progs->{globals}}-1))
+       {
+               if($progs->{globals}[$_]{v}{int})
+               {
+                       $globalflags[$_] |= GLOBALFLAG_I;
+               }
+       }
+
+       my @globaltypes = (undef) x @{$progs->{globals}};
+
+       my %istemp = ();
+       for(0..(@{$progs->{globals}}-1))
+       {
+               next
+                       if $_ < @{(DEFAULTGLOBALS)};
+               if(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == 0)
+               {
+                       $globaltypes[$_] = "unused";
+               }
+               elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_R)
+               {
+                       # so it is ro
+                       if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
+                       {
+                               $globaltypes[$_] = "read_only";
+                       }
+                       elsif(($globalflags[$_] & GLOBALFLAG_S) == 0)
+                       {
+                               $globaltypes[$_] = "const";
+                       }
+                       else
+                       {
+                               $globaltypes[$_] = "read_only";
+                       }
+               }
+               elsif(($globalflags[$_] & (GLOBALFLAG_R | GLOBALFLAG_W)) == GLOBALFLAG_W)
+               {
+                       $globaltypes[$_] = "write_only";
+               }
+               else
+               {
+                       # now we know it is rw
+                       if(($globalflags[$_] & GLOBALFLAG_N) == GLOBALFLAG_N)
+                       {
+                               $globaltypes[$_] = "global";
+                       }
+                       elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == 0)
+                       {
+                               if($globalflags[$_] & GLOBALFLAG_P)
+                               {
+                                       $globaltypes[$_] = "OFS_PARM";
+                               }
+                               elsif($globalflags[$_] & GLOBALFLAG_Q)
+                               {
+                                       $globaltypes[$_] = "uniquetemp";
+                                       $istemp{$_} = 0;
+                               }
+                               else
+                               {
+                                       $globaltypes[$_] = "temp";
+                                       $istemp{$_} = 1;
+                               }
+                       }
+                       elsif(($globalflags[$_] & (GLOBALFLAG_S | GLOBALFLAG_I)) == GLOBALFLAG_I)
+                       {
+                               $globaltypes[$_] = "not_saved";
+                       }
+                       else
+                       {
+                               $globaltypes[$_] = "global";
+                       }
+               }
+       }
+       $progs->{temps} = \%istemp;
+
+       # globaldefs
+       my @globaldefs = (undef) x @{$progs->{globals}};
+       for(@{$progs->{globaldefs}})
+       {
+               $globaldefs[$_->{ofs}] //= $_
+                       if defined $_->{debugname};
+       }
+       for(@{$progs->{globaldefs}})
+       {
+               $globaldefs[$_->{ofs}] //= $_;
+       }
+       for(0..(@{$progs->{globals}}-1))
+       {
+               $globaldefs[$_] //= {
+                       ofs => $_,
+                       s_name => undef,
+                       debugname => undef
+               };
+       }
+       for(0..(@{(DEFAULTGLOBALS)}-1))
+       {
+               $globaldefs[$_] = { ofs => $_, s_name => undef, debugname => DEFAULTGLOBALS->[$_], type => undef };
+               $globaltypes[$_] = 'defglobal';
+       }
+       my %globaldefs_namecount = ();
+       for(@globaldefs)
+       {
+               $_->{globaltype} = $globaltypes[$_->{ofs}];
+               if(defined $_->{debugname})
+               {
+                       # already has debugname
+               }
+               elsif($_->{globaltype} eq 'const')
+               {
+                       $_->{debugname} = get_constant($progs, $progs->{globals}[$_->{ofs}]{v});
+               }
+               else
+               {
+                       $_->{debugname} = "$_->{globaltype}_$_->{ofs}";
+               }
+               ++$globaldefs_namecount{$_->{debugname}};
+       }
+       for(@globaldefs)
+       {
+               next
+                       if $globaldefs_namecount{$_->{debugname}} <= 1 && !$ENV{FORCE_OFFSETS};
+               #print "Not unique: $_->{debugname} at $_->{ofs}\n";
+               $_->{debugname} .= "\@$_->{ofs}";
+       }
+       $progs->{globaldef_byoffset} = sub
+       {
+               my ($ofs) = @_;
+               my $def = $globaldefs[$ofs];
+               return $def;
+       };
+}
+
+sub parse_progs($)
+{
+       my ($fh) = @_;
+
+       my %p = ();
+
+       print STDERR "Parsing header...\n";
+       $p{header} = parse_section $fh, DPROGRAMS_T, 0, undef, 1;
+       
+       print STDERR "Parsing strings...\n";
+       $p{strings} = get_section $fh, $p{header}{ofs_strings}, $p{header}{numstrings};
+       $p{getstring} = sub
+       {
+               my ($startpos) = @_;
+               my $endpos = index $p{strings}, "\0", $startpos;
+               return substr $p{strings}, $startpos, $endpos - $startpos;
+       };
+
+       print STDERR "Parsing globals...\n";
+       $p{globals} = [parse_section $fh, DGLOBAL_T, $p{header}{ofs_globals}, undef, $p{header}{numglobals}];
+
+       print STDERR "Parsing globaldefs...\n";
+       $p{globaldefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_globaldefs}, undef, $p{header}{numglobaldefs}];
+
+       print STDERR "Range checking globaldefs...\n";
+       for(0 .. (@{$p{globaldefs}}-1))
+       {
+               my $g = $p{globaldefs}[$_];
+               die "Out of range name in globaldef $_"
+                       if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
+               my $name = $p{getstring}->($g->{s_name});
+               die "Out of range ofs in globaldef $_ (name: \"$name\")"
+                       if $g->{ofs} >= $p{globals};
+       }
+
+       print STDERR "Parsing fielddefs...\n";
+       $p{fielddefs} = [parse_section $fh, DDEF_T, $p{header}{ofs_fielddefs}, undef, $p{header}{numfielddefs}];
+
+       print STDERR "Range checking fielddefs...\n";
+       for(0 .. (@{$p{fielddefs}}-1))
+       {
+               my $g = $p{fielddefs}[$_];
+               die "Out of range name in fielddef $_"
+                       if $g->{s_name} < 0 || $g->{s_name} >= length $p{strings};
+               my $name = $p{getstring}->($g->{s_name});
+               die "Out of range ofs in globaldef $_ (name: \"$name\")"
+                       if $g->{ofs} >= $p{header}{entityfields};
+       }
+
+       print STDERR "Parsing statements...\n";
+       $p{statements} = [parse_section $fh, DSTATEMENT_T, $p{header}{ofs_statements}, undef, $p{header}{numstatements}];
+
+       print STDERR "Parsing functions...\n";
+       $p{functions} = [parse_section $fh, DFUNCTION_T, $p{header}{ofs_functions}, undef, $p{header}{numfunctions}];
+
+       print STDERR "Range checking functions...\n";
+       for(0 .. (@{$p{functions}} - 1))
+       {
+               my $f = $p{functions}[$_];
+               die "Out of range name in function $_"
+                       if $f->{s_name} < 0 || $f->{s_name} >= length $p{strings};
+               my $name = $p{getstring}->($f->{s_name});
+               die "Out of range file in function $_"
+                       if $f->{s_file} < 0 || $f->{s_file} >= length $p{strings};
+               my $file = $p{getstring}->($f->{s_file});
+               die "Out of range first_statement in function $_ (name: \"$name\", file: \"$file\")"
+                       if $f->{first_statement} >= @{$p{statements}};
+               die "Out of range parm_start in function $_ (name: \"$name\", file: \"$file\")"
+                       if $f->{parm_start} < 0 || $f->{parm_start} >= @{$p{globals}};
+               die "Out of range locals in function $_ (name: \"$name\", file: \"$file\")"
+                       if $f->{locals} < 0 || $f->{parm_start} + $f->{locals} >= @{$p{globals}};
+               die "Out of range numparms in function $_ (name: \"$name\", file: \"$file\")"
+                       if $f->{numparms} < 0 || $f->{numparms} > 8;
+               my $totalparms = 0;
+               for(0..($f->{numparms}-1))
+               {
+                       die "Out of range parm_size[$_] in function $_ (name: \"$name\", file: \"$file\")"
+                               unless { 0 => 1, 1 => 1, 3 => 1 }->{$f->{parm_size}[$_]};
+                       $totalparms += $f->{parm_size}[$_];
+               }
+               die "Out of range parms in function $_ (name: \"$name\", file: \"$file\")"
+                       if $f->{locals} < 0 || $f->{parm_start} + $totalparms >= @{$p{globals}};
+       }
+
+       print STDERR "Range checking statements...\n";
+       for my $ip(0 .. (@{$p{statements}}-1))
+       {
+               my $s = $p{statements}[$ip];
+               my $c = checkop $s->{op};
+
+               for(qw(a b c))
+               {
+                       my $type = $c->{$_};
+                       next
+                               unless defined $type;
+
+                       if($type eq 'inglobal' || $type eq 'inglobalfunc')
+                       {
+                               $s->{$_} &= 0xFFFF;
+                               die "Out of range global offset in statement $ip - cannot continue"
+                                       if $s->{$_} >= @{$p{globals}};
+                       }
+                       elsif($type eq 'inglobalvec')
+                       {
+                               $s->{$_} &= 0xFFFF;
+                               die "Out of range global offset in statement $ip - cannot continue"
+                                       if $s->{$_} >= @{$p{globals}}-2;
+                       }
+                       elsif($type eq 'outglobal')
+                       {
+                               $s->{$_} &= 0xFFFF;
+                               die "Out of range global offset in statement $ip - cannot continue"
+                                       if $s->{$_} >= @{$p{globals}};
+                       }
+                       elsif($type eq 'outglobalvec')
+                       {
+                               $s->{$_} &= 0xFFFF;
+                               die "Out of range global offset in statement $ip - cannot continue"
+                                       if $s->{$_} >= @{$p{globals}}-2;
+                       }
+                       elsif($type eq 'ipoffset')
+                       {
+                               die "Out of range GOTO/IF/IFNOT in statement $ip - cannot continue"
+                                       if $ip + $s->{$_} < 0 || $ip + $s->{$_} >= @{$p{statements}};
+                       }
+               }
+       }
+
+       print STDERR "Looking for error()...\n";
+       $p{error_func} = {};
+       for(@{$p{globaldefs}})
+       {
+               next
+                       if $p{getstring}($_->{s_name}) ne 'error';
+               my $v = $p{globals}[$_->{ofs}]{v}{int};
+               next
+                       if $v <= 0 || $v >= @{$p{functions}};
+               my $first = $p{functions}[$v]{first_statement};
+               next
+                       if $first >= 0;
+               print STDERR "Detected error() at offset $_->{ofs} (builtin #@{[-$first]})\n";
+               $p{error_func}{$_->{ofs}} = 1;
+       }
+
+       print STDERR "Scanning functions...\n";
+       for(@{$p{functions}})
+       {
+               my $file = $p{getstring}->($_->{s_file});
+               my $name = $p{getstring}->($_->{s_name});
+               $name = "$file:$name"
+                       if length $file;
+               $_->{debugname} = $name;
+
+               next
+                       if $_->{first_statement} < 0;
+
+               my %statements = ();
+               my %come_from = ();
+               my %go_to = ();
+               my %globals_read = ();
+               my %globals_written = ();
+               my %globals_used = ();
+
+               if($_->{first_statement} >= 0)
+               {
+                       run_nfa \%p, $_->{first_statement}, "", id, nfa_default_state_checker,
+                               sub
+                               {
+                                       my ($ip, $state, $s, $c) = @_;
+                                       ++$statements{$ip};
+
+                                       if(my $j = $c->{isjump})
+                                       {
+                                               my $t = $ip + $s->{$j};
+                                               $come_from{$t}{$ip} = $c->{isconditional};
+                                               $go_to{$ip}{$t} = $c->{isconditional};
+                                       }
+
+                                       for my $o(qw(a b c))
+                                       {
+                                               my $type = $c->{$o}
+                                                       or next;
+                                               my $ofs = $s->{$o};
+
+                                               my $read = sub
+                                               {
+                                                       my ($ofs) = @_;
+                                                       $globals_read{$ofs}{$ip}{$o} = 1;
+                                                       $globals_used{$ofs} = 1;
+                                               };
+                                               my $write = sub
+                                               {
+                                                       my ($ofs) = @_;
+                                                       $globals_written{$ofs}{$ip}{$o} = 1;
+                                                       $globals_used{$ofs} = 1;
+                                               };
+
+                                               if($type eq 'inglobal' || $type eq 'inglobalfunc')
+                                               {
+                                                       $read->($ofs);
+                                               }
+                                               elsif($type eq 'inglobalvec')
+                                               {
+                                                       $read->($ofs);
+                                                       $read->($ofs+1);
+                                                       $read->($ofs+2);
+                                               }
+                                               elsif($type eq 'outglobal')
+                                               {
+                                                       $write->($ofs);
+                                               }
+                                               elsif($type eq 'outglobalvec')
+                                               {
+                                                       $write->($ofs);
+                                                       $write->($ofs+1);
+                                                       $write->($ofs+2);
+                                               }
+                                       }
+
+                                       return 0;
+                               };
+               }
+
+               $_->{statements} = \%statements;
+               $_->{come_from} = \%come_from;
+               $_->{go_to} = \%go_to;
+               $_->{globals_read} = \%globals_read;
+               $_->{globals_written} = \%globals_written;
+               $_->{globals_used} = \%globals_used;
+
+               # using this info, we could now identify basic blocks
+       }
+
+       print STDERR "Detecting constants and temps, and naming...\n";
+       detect_constants \%p;
+
+       if($ENV{DUMP})
+       {
+               use Data::Dumper;
+               print Dumper \%p;
+               return;
+       }
+
+       # what do we want to do?
+       my $checkfunc = \&find_uninitialized_locals;
+       if($ENV{DISASSEMBLE})
+       {
+               $checkfunc = \&disassemble_function;
+       }
+       for(sort { $a->{debugname} cmp $b->{debugname} } @{$p{functions}})
+       {
+               $checkfunc->(\%p, $_);
+       }
+}
+
+open my $fh, '<', $ARGV[0];
+parse_progs $fh;