From ea571c54dd2f219ab0c2b8d5339c6dfc63352fcd Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Wed, 21 Nov 2007 14:27:25 -0600
Subject: [PATCH 01/12] Add open_console() command line parameter and stubs on
 each platform Add Config.windows.nt

---
 vm/Config.windows.nt        | 8 ++++++++
 vm/Config.windows.nt.x86.32 | 9 ++-------
 vm/factor.c                 | 6 ++++++
 vm/image.h                  | 1 +
 vm/os-unix.c                | 2 ++
 vm/os-unix.h                | 1 +
 vm/os-windows-ce.c          | 2 ++
 vm/os-windows-ce.h          | 1 +
 vm/os-windows-nt.c          | 4 ++++
 vm/os-windows-nt.h          | 1 +
 10 files changed, 28 insertions(+), 7 deletions(-)
 create mode 100644 vm/Config.windows.nt

diff --git a/vm/Config.windows.nt b/vm/Config.windows.nt
new file mode 100644
index 0000000000..8c67b11c19
--- /dev/null
+++ b/vm/Config.windows.nt
@@ -0,0 +1,8 @@
+LIBS = -lm
+EXE_SUFFIX=-nt
+DLL_SUFFIX=-nt
+PLAF_DLL_OBJS += vm/os-windows-nt.o
+PLAF_EXE_OBJS += vm/resources.o
+PLAF_EXE_OBJS += vm/main-windows-nt.o
+CFLAGS += -mwindows
+include vm/Config.windows
diff --git a/vm/Config.windows.nt.x86.32 b/vm/Config.windows.nt.x86.32
index adc69b1e27..a8d4931b4d 100644
--- a/vm/Config.windows.nt.x86.32
+++ b/vm/Config.windows.nt.x86.32
@@ -1,7 +1,2 @@
-LIBS = -lm
-EXE_SUFFIX=-nt
-DLL_SUFFIX=-nt
-PLAF_DLL_OBJS += vm/os-windows-nt.o
-PLAF_EXE_OBJS += vm/resources.o
-PLAF_EXE_OBJS += vm/main-windows-nt.o
-include vm/Config.x86.32 vm/Config.windows
+include vm/Config.windows.nt
+include vm/Config.x86.32
diff --git a/vm/factor.c b/vm/factor.c
index 690f5d490c..8719416b72 100755
--- a/vm/factor.c
+++ b/vm/factor.c
@@ -26,6 +26,7 @@ void default_parameters(F_PARAMETERS *p)
 
 	p->secure_gc = false;
 	p->fep = false;
+	p->console = false;
 }
 
 /* Get things started */
@@ -110,6 +111,8 @@ void init_factor_from_args(F_CHAR *image, int argc, F_CHAR **argv, bool embedded
 			p.fep = true;
 		else if(STRNCMP(argv[i],STR_FORMAT("-i="),3) == 0)
 			p.image = argv[i] + 3;
+		else if(STRCMP(argv[i],STR_FORMAT("-console")) == 0)
+			p.console = true ;
 	}
 
 	init_factor(&p);
@@ -135,6 +138,9 @@ void init_factor_from_args(F_CHAR *image, int argc, F_CHAR **argv, bool embedded
 
 	nest_stacks();
 
+	if(p.console)
+		open_console();
+
 	if(p.fep)
 		factorbug();
 
diff --git a/vm/image.h b/vm/image.h
index 52b666254e..3774263031 100755
--- a/vm/image.h
+++ b/vm/image.h
@@ -32,6 +32,7 @@ typedef struct {
 	CELL code_size;
 	bool secure_gc;
 	bool fep;
+	bool console;
 } F_PARAMETERS;
 
 void load_image(F_PARAMETERS *p);
diff --git a/vm/os-unix.c b/vm/os-unix.c
index 303c01491a..437a528fb8 100644
--- a/vm/os-unix.c
+++ b/vm/os-unix.c
@@ -256,3 +256,5 @@ void reset_stdio(void)
 	fcntl(0,F_SETFL,0);
 	fcntl(1,F_SETFL,0);
 }
+
+void open_console(void) { }
diff --git a/vm/os-unix.h b/vm/os-unix.h
index cbce7de985..85f760b5aa 100755
--- a/vm/os-unix.h
+++ b/vm/os-unix.h
@@ -39,3 +39,4 @@ s64 current_millis(void);
 void sleep_millis(CELL msec);
 
 void reset_stdio(void);
+void open_console(void);
diff --git a/vm/os-windows-ce.c b/vm/os-windows-ce.c
index 1465e0c89f..e68a6385ae 100755
--- a/vm/os-windows-ce.c
+++ b/vm/os-windows-ce.c
@@ -46,3 +46,5 @@ void c_to_factor_toplevel(CELL quot)
 {
 	c_to_factor(quot);
 }
+
+void open_console(void) { }
diff --git a/vm/os-windows-ce.h b/vm/os-windows-ce.h
index 959de89634..f1d6df6f3d 100755
--- a/vm/os-windows-ce.h
+++ b/vm/os-windows-ce.h
@@ -24,3 +24,4 @@ char *getenv(char *name);
 
 s64 current_millis(void);
 void c_to_factor_toplevel(CELL quot);
+void open_console(void);
diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c
index baa0a06c4b..7ea8155072 100755
--- a/vm/os-windows-nt.c
+++ b/vm/os-windows-nt.c
@@ -88,3 +88,7 @@ void c_to_factor_toplevel(CELL quot)
 	c_to_factor(quot);
 	RemoveVectoredExceptionHandler((void*)exception_handler);
 }
+
+void open_console(void)
+{
+}
diff --git a/vm/os-windows-nt.h b/vm/os-windows-nt.h
index f7c56c129d..f74c7b1883 100755
--- a/vm/os-windows-nt.h
+++ b/vm/os-windows-nt.h
@@ -18,3 +18,4 @@ typedef char F_SYMBOL;
 
 void c_to_factor_toplevel(CELL quot);
 long exception_handler(PEXCEPTION_POINTERS pe);
+void open_console(void);

From 01682c1cd2dcb5f95db776e4a2d1c6441c0f52aa Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Wed, 21 Nov 2007 14:31:36 -0600
Subject: [PATCH 02/12] Open a console when calling factorbug()

---
 vm/debug.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/vm/debug.c b/vm/debug.c
index 55ffcadca6..2692bdf59c 100755
--- a/vm/debug.c
+++ b/vm/debug.c
@@ -213,6 +213,7 @@ void dump_objects(F_FIXNUM type)
 void factorbug(void)
 {
 	reset_stdio();
+	open_console();
 
 	printf("Starting low level debugger...\n");
 	printf("  Basic commands:\n");

From 0da81b301833a87dde90a620daa397908a2bbbdc Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Wed, 21 Nov 2007 14:47:15 -0600
Subject: [PATCH 03/12] Reuse existing console if present, or else open a new
 console, when -console is supplied

---
 vm/os-windows-nt.c | 10 ++++++++++
 vm/os-windows-nt.h |  1 +
 2 files changed, 11 insertions(+)

diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c
index 7ea8155072..afd1e0ed3b 100755
--- a/vm/os-windows-nt.c
+++ b/vm/os-windows-nt.c
@@ -91,4 +91,14 @@ void c_to_factor_toplevel(CELL quot)
 
 void open_console(void)
 {
+	if(!console_open)
+	{
+		if(!AttachConsole(ATTACH_PARENT_PROCESS))
+		{
+			if(AllocConsole())
+				console_open = true;
+		}
+		else
+			console_open = true;
+	}
 }
diff --git a/vm/os-windows-nt.h b/vm/os-windows-nt.h
index f74c7b1883..9e451f0301 100755
--- a/vm/os-windows-nt.h
+++ b/vm/os-windows-nt.h
@@ -18,4 +18,5 @@ typedef char F_SYMBOL;
 
 void c_to_factor_toplevel(CELL quot);
 long exception_handler(PEXCEPTION_POINTERS pe);
+bool console_open;
 void open_console(void);

From 9695beb00c67a1d69c189a265e534c752b782a87 Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 17:59:11 -0600
Subject: [PATCH 04/12] Rename the install.sh to factor.sh  - Add install and
 update parameters to it

---
 misc/factor.sh | 203 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 203 insertions(+)
 create mode 100755 misc/factor.sh

diff --git a/misc/factor.sh b/misc/factor.sh
new file mode 100755
index 0000000000..3f8152518c
--- /dev/null
+++ b/misc/factor.sh
@@ -0,0 +1,203 @@
+#!/bin/bash -e
+
+# Programs returning != 0 will not cause script to exit
+set +e
+
+# Case insensitive string comparison
+shopt -s nocaseglob
+#shopt -s nocasematch
+
+OS=
+ARCH=
+WORD=
+
+ensure_program_installed() {
+    echo -n "Checking for $1..."
+    result=`type -p $1`
+    if ! [[ -n $result ]] ; then
+        echo "not found!"
+        echo "Install $1 and try again."
+        exit 1
+    fi
+    echo "found!"
+}
+
+check_ret() {
+    RET=$?
+    if [[ $RET -ne 0 ]] ; then
+       echo $1 failed
+       exit 5
+    fi
+}
+
+check_gcc_version() {
+    GCC_VERSION=`gcc --version`
+    if [[ $GCC_VERSION == *3.3.* ]] ; then
+        echo "You have a known buggy version of gcc (3.3)"
+        echo "Install gcc 3.4 or higher and try again."
+        exit 1
+    fi
+}
+
+check_installed_programs() {
+    ensure_program_installed chmod
+    ensure_program_installed uname
+    ensure_program_installed git
+    ensure_program_installed wget
+    ensure_program_installed gcc
+    ensure_program_installed make
+    check_gcc_version
+}
+
+
+find_os() {
+    uname_s=`uname -s`
+    case $uname_s in
+        CYGWIN_NT-5.2-WOW64) OS=windows-nt;;
+        *CYGWIN_NT*) OS=windows-nt;;
+        *CYGWIN*) OS=windows-nt;;
+        *darwin*) OS=macosx;;
+        *Darwin*) OS=macosx;;
+        *linux*) OS=linux;;
+        *Linux*) OS=linux;;
+    esac
+}
+
+find_architecture() {
+    uname_m=`uname -m`
+    case $uname_m in
+           i386) ARCH=x86;;
+           i686) ARCH=x86;;
+           *86) ARCH=x86;;
+           "Power Macintosh") ARCH=ppc;;
+    esac
+}
+
+write_test_program() {
+    echo "#include <stdio.h>" > $C_WORD.c
+    echo "int main(){printf(\"%d\", 8*sizeof(void*)); return 0; }" >> $C_WORD.c
+}
+
+find_word_size() {
+    C_WORD=factor-word-size
+    write_test_program
+    gcc -o $C_WORD $C_WORD.c
+    WORD=$(./$C_WORD)
+    check_ret $C_WORD
+    rm -f $C_WORD*
+}
+
+set_factor_binary() {
+    case $OS in
+        windows-nt) FACTOR_BINARY=factor-nt;;
+        macosx) FACTOR_BINARY=./Factor.app/Contents/MacOS/factor;;
+        *) FACTOR_BINARY=factor;;
+    esac
+}
+
+echo_build_info() {
+    echo OS=$OS
+    echo ARCH=$ARCH
+    echo WORD=$WORD
+    echo FACTOR_BINARY=$FACTOR_BINARY
+    echo MAKE_TARGET=$MAKE_TARGET
+    echo BOOT_IMAGE=$BOOT_IMAGE
+}
+
+set_build_info() {
+    if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
+        echo "OS, ARCH, or WORD is empty.  Please report this"
+        exit 4
+    fi
+    MAKE_TARGET=$OS-$ARCH-$WORD
+    BOOT_IMAGE=boot.$ARCH.$WORD.image
+}
+
+find_build_info() {
+    find_os
+    find_architecture
+    find_word_size
+    set_factor_binary
+    set_build_info
+    echo_build_info
+}
+
+
+
+git_clone() {
+    echo "Downloading the git repository from factorcode.org..."
+    git clone git://factorcode.org/git/factor.git
+    check_ret git
+}
+
+git_pull_factorcode() {
+    git pull git://factorcode.org/git/factor.git
+    check_ret git
+}
+
+cd_factor() {
+    cd factor
+    check_ret cd
+}
+
+make_factor() {
+    make $MAKE_TARGET
+    check_ret make
+}
+
+delete_boot_images() {
+    echo "Deleting old images..."
+    rm $BOOT_IMAGE > /dev/null 2>&1
+    rm $BOOT_IMAGE.* > /dev/null 2>&1
+}
+
+get_boot_image() {
+    wget http://factorcode.org/images/latest/$BOOT_IMAGE
+    check_ret wget
+}
+
+maybe_download_dlls() {
+    if [[ $OS == windows-nt ]] ; then
+        wget http://factorcode.org/dlls/freetype6.dll
+        check_ret
+        wget http://factorcode.org/dlls/zlib1.dll
+        check_ret
+        chmod 777 *.dll
+        check_ret
+    fi
+}
+
+bootstrap() {
+    ./$FACTOR_BINARY -i=$BOOT_IMAGE
+}
+
+usage() {
+    echo "usage: $0 install|update"
+}
+
+case "$1" in
+    install)
+        check_installed_programs
+        find_build_info
+        git_clone
+        cd_factor
+        make_factor
+        get_boot_image
+        maybe_download_dlls
+        bootstrap
+    ;;
+
+    update)
+        check_installed_programs
+        find_build_info
+        git_pull_factorcode
+        make_factor
+        delete_boot_images
+        get_boot_image
+        bootstrap
+    ;;
+
+    *)
+        usage
+    ;;
+esac

From 5849496a93ae77f866ce7a5f99a42c06fd8a6081 Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 18:00:18 -0600
Subject: [PATCH 05/12] add Config file for win64

---
 vm/Config.windows.nt.x86.64 | 4 ++++
 1 file changed, 4 insertions(+)
 create mode 100644 vm/Config.windows.nt.x86.64

diff --git a/vm/Config.windows.nt.x86.64 b/vm/Config.windows.nt.x86.64
new file mode 100644
index 0000000000..1c30e64096
--- /dev/null
+++ b/vm/Config.windows.nt.x86.64
@@ -0,0 +1,4 @@
+CC=/k/target/bin/x86_64-pc-mingw32-gcc
+include vm/Config.windows.nt
+include vm/Config.x86.64
+WINDRES = /k/target/bin/windres

From 1347c04163c952cf6f68fe33e4a82cd766a38542 Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 18:00:48 -0600
Subject: [PATCH 06/12] Remove old install.sh

---
 misc/install.sh | 120 ------------------------------------------------
 1 file changed, 120 deletions(-)
 delete mode 100755 misc/install.sh

diff --git a/misc/install.sh b/misc/install.sh
deleted file mode 100755
index 006a7cf604..0000000000
--- a/misc/install.sh
+++ /dev/null
@@ -1,120 +0,0 @@
-#!/bin/bash -e
-
-# Programs returning != 0 will not cause script to exit
-set +e
-
-# Case insensitive string comparison
-shopt -s nocaseglob
-#shopt -s nocasematch
-
-ensure_program_installed() {
-        echo -n "Checking for $1..."
-        result=`type -p $1`
-        if ! [[ -n $result ]] ; then
-                echo "not found!"
-                echo "Install $1 and try again."
-                exit 1
-        fi
-        echo "found!"
-}
-
-check_ret() {
-        RET=$?
-        if [[ $RET -ne 0 ]] ; then
-                echo $1 failed
-                exit 5
-        fi
-}
-
-ensure_program_installed uname
-ensure_program_installed git
-ensure_program_installed wget
-ensure_program_installed gcc
-ensure_program_installed make
-
-GCC_VERSION=`gcc --version`
-if [[ $GCC_VERSION == *3.3.* ]] ; then
-        echo "You have a known buggy version of gcc (3.3)"
-        echo "Install gcc 3.4 or higher and try again."
-        exit 1
-fi
-
-# OS
-OS=
-uname_s=`uname -s`
-case $uname_s in
-        CYGWIN_NT-5.2-WOW64) OS=windows-nt;;
-        *CYGWIN_NT*) OS=windows-nt;;
-        *CYGWIN*) OS=windows-nt;;
-        *darwin*) OS=macosx;;
-        *Darwin*) OS=macosx;;
-        *linux*) OS=linux;;
-        *Linux*) OS=linux;;
-esac
-
-# Architecture
-ARCH=
-uname_m=`uname -m`
-case $uname_m in
-        i386) ARCH=x86;;
-        i686) ARCH=x86;;
-        *86) ARCH=x86;;
-        "Power Macintosh") ARCH=ppc;;
-esac
-
-WORD=
-C_WORD=factor-word-size
-# Word size
-echo "#include <stdio.h>" > $C_WORD.c
-echo "int main() { printf(\"%d\", 8*sizeof(long)); return 0; }" >> $C_WORD.c
-gcc -o $C_WORD $C_WORD.c
-WORD=$(./$C_WORD)
-check_ret $C_WORD
-rm -f $C_WORD*
-
-case $OS in
-        windows-nt) FACTOR_BINARY=factor-nt;;
-        macosx) FACTOR_BINARY=./Factor.app/Contents/MacOS/factor;;
-        *) FACTOR_BINARY=factor;;
-esac
-
-MAKE_TARGET=$OS-$ARCH-$WORD
-BOOT_IMAGE=boot.$ARCH.$WORD.image
-
-echo OS=$OS
-echo ARCH=$ARCH
-echo WORD=$WORD
-echo FACTOR_BINARY=$FACTOR_BINARY
-echo MAKE_TARGET=$MAKE_TARGET
-echo BOOT_IMAGE=$BOOT_IMAGE
-
-if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
-        echo "OS, ARCH, or WORD is empty.  Please report this"
-        exit 4
-fi
-
-echo "Downloading the git repository from factorcode.org..."
-git clone git://factorcode.org/git/factor.git
-check_ret git
-
-cd factor
-check_ret cd
-
-make $MAKE_TARGET
-check_ret make
-
-echo "Deleting old images..."
-rm $BOOT_IMAGE > /dev/null 2>&1
-rm $BOOT_IMAGE.* > /dev/null 2>&1
-wget http://factorcode.org/images/latest/$BOOT_IMAGE
-check_ret wget
-
-if [[ $OS == windows-nt ]] ; then
-	wget http://factorcode.org/dlls/freetype6.dll
-	check_ret
-	wget http://factorcode.org/dlls/zlib1.dll
-	check_ret
-fi
-
-
-./$FACTOR_BINARY -i=$BOOT_IMAGE

From be1e2961ef34f6b6c2f098af50748c49e82ba0fe Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 18:02:34 -0600
Subject: [PATCH 07/12] Update Makefile for win64

---
 Makefile | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 77a6fb6409..4c6eb2f4e6 100644
--- a/Makefile
+++ b/Makefile
@@ -62,6 +62,7 @@ default:
 	@echo "solaris-x86-64"
 	@echo "windows-ce-arm"
 	@echo "windows-nt-x86-32"
+	@echo "windows-nt-x86-64"
 	@echo ""
 	@echo "Additional modifiers:"
 	@echo ""
@@ -113,6 +114,9 @@ solaris-x86-64:
 windows-nt-x86-32:
 	$(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.32
 
+windows-nt-x86-64:
+	$(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.nt.x86.64
+
 windows-ce-arm:
 	$(MAKE) $(EXECUTABLE) CONFIG=vm/Config.windows.ce.arm
 
@@ -138,7 +142,7 @@ clean:
 	rm -f vm/*.o
 
 vm/resources.o:
-	windres vm/factor.rs vm/resources.o
+	$(WINDRES) vm/factor.rs vm/resources.o
 
 .c.o:
 	$(CC) -c $(CFLAGS) -o $@ $<

From dc76d551e0f8f4b5758789260cb2183fbf854df8 Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 18:03:01 -0600
Subject: [PATCH 08/12] Don't install factor if a factor/ already exists

---
 misc/factor.sh | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/misc/factor.sh b/misc/factor.sh
index 3f8152518c..9db0a29fcb 100755
--- a/misc/factor.sh
+++ b/misc/factor.sh
@@ -26,7 +26,7 @@ check_ret() {
     RET=$?
     if [[ $RET -ne 0 ]] ; then
        echo $1 failed
-       exit 5
+       exit 2
     fi
 }
 
@@ -35,7 +35,7 @@ check_gcc_version() {
     if [[ $GCC_VERSION == *3.3.* ]] ; then
         echo "You have a known buggy version of gcc (3.3)"
         echo "Install gcc 3.4 or higher and try again."
-        exit 1
+        exit 3
     fi
 }
 
@@ -49,6 +49,13 @@ check_installed_programs() {
     check_gcc_version
 }
 
+check_factor_exists() {
+	if [[ -d "factor" ]] ; then
+		echo "A directory called 'factor' already exists."
+		echo "Rename or delete it and try again."
+		exit 4
+	fi
+}
 
 find_os() {
     uname_s=`uname -s`
@@ -66,10 +73,10 @@ find_os() {
 find_architecture() {
     uname_m=`uname -m`
     case $uname_m in
-           i386) ARCH=x86;;
-           i686) ARCH=x86;;
-           *86) ARCH=x86;;
-           "Power Macintosh") ARCH=ppc;;
+       i386) ARCH=x86;;
+       i686) ARCH=x86;;
+       *86) ARCH=x86;;
+       "Power Macintosh") ARCH=ppc;;
     esac
 }
 
@@ -107,7 +114,7 @@ echo_build_info() {
 set_build_info() {
     if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
         echo "OS, ARCH, or WORD is empty.  Please report this"
-        exit 4
+        exit 5
     fi
     MAKE_TARGET=$OS-$ARCH-$WORD
     BOOT_IMAGE=boot.$ARCH.$WORD.image
@@ -122,8 +129,6 @@ find_build_info() {
     echo_build_info
 }
 
-
-
 git_clone() {
     echo "Downloading the git repository from factorcode.org..."
     git clone git://factorcode.org/git/factor.git
@@ -177,6 +182,7 @@ usage() {
 
 case "$1" in
     install)
+		check_factor_exists
         check_installed_programs
         find_build_info
         git_clone

From b8ab6a8cb188549aae66ee6743de66b30947dae7 Mon Sep 17 00:00:00 2001
From: "U-C4\\Administrator" <Administrator@c4.(none)>
Date: Sat, 24 Nov 2007 18:07:05 -0600
Subject: [PATCH 09/12] Comment out the AllocConsole() code for windows Add
 WINDRES variable to NT 32-bit backend

---
 vm/Config.windows.nt        |  2 +-
 vm/Config.windows.nt.x86.32 |  1 +
 vm/os-windows-nt.c          | 10 +++++-----
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/vm/Config.windows.nt b/vm/Config.windows.nt
index 8c67b11c19..c712c7d053 100644
--- a/vm/Config.windows.nt
+++ b/vm/Config.windows.nt
@@ -4,5 +4,5 @@ DLL_SUFFIX=-nt
 PLAF_DLL_OBJS += vm/os-windows-nt.o
 PLAF_EXE_OBJS += vm/resources.o
 PLAF_EXE_OBJS += vm/main-windows-nt.o
-CFLAGS += -mwindows
+#CFLAGS += -mwindows
 include vm/Config.windows
diff --git a/vm/Config.windows.nt.x86.32 b/vm/Config.windows.nt.x86.32
index a8d4931b4d..9a020a7bc1 100644
--- a/vm/Config.windows.nt.x86.32
+++ b/vm/Config.windows.nt.x86.32
@@ -1,2 +1,3 @@
+WINDRES=windres
 include vm/Config.windows.nt
 include vm/Config.x86.32
diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c
index afd1e0ed3b..fc7fc61e0d 100755
--- a/vm/os-windows-nt.c
+++ b/vm/os-windows-nt.c
@@ -91,14 +91,14 @@ void c_to_factor_toplevel(CELL quot)
 
 void open_console(void)
 {
+	/*
+	// Do this: http://www.cygwin.com/ml/cygwin/2007-11/msg00432.html
 	if(!console_open)
 	{
-		if(!AttachConsole(ATTACH_PARENT_PROCESS))
+		if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
 		{
-			if(AllocConsole())
-				console_open = true;
-		}
-		else
 			console_open = true;
+		}
 	}
+	*/
 }

From 096b9fe4b73466e857334de08475e6bcb1e12165 Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Sat, 24 Nov 2007 18:17:25 -0600
Subject: [PATCH 10/12] Clean up C code a bit

---
 vm/os-windows-nt.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/vm/os-windows-nt.c b/vm/os-windows-nt.c
index fc7fc61e0d..be9dde1fa8 100755
--- a/vm/os-windows-nt.c
+++ b/vm/os-windows-nt.c
@@ -93,12 +93,12 @@ void open_console(void)
 {
 	/*
 	// Do this: http://www.cygwin.com/ml/cygwin/2007-11/msg00432.html
-	if(!console_open)
+	if(console_open)
+		return;
+
+	if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
 	{
-		if(AttachConsole(ATTACH_PARENT_PROCESS) || AllocConsole())
-		{
-			console_open = true;
-		}
+		console_open = true;
 	}
 	*/
 }

From f26a6569374d7e9250e8718a08f500932ffc96d0 Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Sat, 24 Nov 2007 18:51:24 -0600
Subject: [PATCH 11/12] Do a make clean on update Changed spaces to tabs

---
 misc/factor.sh | 246 +++++++++++++++++++++++++------------------------
 1 file changed, 126 insertions(+), 120 deletions(-)

diff --git a/misc/factor.sh b/misc/factor.sh
index 9db0a29fcb..eb231cd565 100755
--- a/misc/factor.sh
+++ b/misc/factor.sh
@@ -12,41 +12,41 @@ ARCH=
 WORD=
 
 ensure_program_installed() {
-    echo -n "Checking for $1..."
-    result=`type -p $1`
-    if ! [[ -n $result ]] ; then
-        echo "not found!"
-        echo "Install $1 and try again."
-        exit 1
-    fi
-    echo "found!"
+	echo -n "Checking for $1..."
+	result=`type -p $1`
+	if ! [[ -n $result ]] ; then
+		echo "not found!"
+		echo "Install $1 and try again."
+		exit 1
+	fi
+	echo "found!"
 }
 
 check_ret() {
-    RET=$?
-    if [[ $RET -ne 0 ]] ; then
-       echo $1 failed
-       exit 2
-    fi
+	RET=$?
+	if [[ $RET -ne 0 ]] ; then
+	   echo $1 failed
+	   exit 2
+	fi
 }
 
 check_gcc_version() {
-    GCC_VERSION=`gcc --version`
-    if [[ $GCC_VERSION == *3.3.* ]] ; then
-        echo "You have a known buggy version of gcc (3.3)"
-        echo "Install gcc 3.4 or higher and try again."
-        exit 3
-    fi
+	GCC_VERSION=`gcc --version`
+	if [[ $GCC_VERSION == *3.3.* ]] ; then
+		echo "You have a known buggy version of gcc (3.3)"
+		echo "Install gcc 3.4 or higher and try again."
+		exit 3
+	fi
 }
 
 check_installed_programs() {
-    ensure_program_installed chmod
-    ensure_program_installed uname
-    ensure_program_installed git
-    ensure_program_installed wget
-    ensure_program_installed gcc
-    ensure_program_installed make
-    check_gcc_version
+	ensure_program_installed chmod
+	ensure_program_installed uname
+	ensure_program_installed git
+	ensure_program_installed wget
+	ensure_program_installed gcc
+	ensure_program_installed make
+	check_gcc_version
 }
 
 check_factor_exists() {
@@ -58,152 +58,158 @@ check_factor_exists() {
 }
 
 find_os() {
-    uname_s=`uname -s`
-    case $uname_s in
-        CYGWIN_NT-5.2-WOW64) OS=windows-nt;;
-        *CYGWIN_NT*) OS=windows-nt;;
-        *CYGWIN*) OS=windows-nt;;
-        *darwin*) OS=macosx;;
-        *Darwin*) OS=macosx;;
-        *linux*) OS=linux;;
-        *Linux*) OS=linux;;
-    esac
+	uname_s=`uname -s`
+	case $uname_s in
+		CYGWIN_NT-5.2-WOW64) OS=windows-nt;;
+		*CYGWIN_NT*) OS=windows-nt;;
+		*CYGWIN*) OS=windows-nt;;
+		*darwin*) OS=macosx;;
+		*Darwin*) OS=macosx;;
+		*linux*) OS=linux;;
+		*Linux*) OS=linux;;
+	esac
 }
 
 find_architecture() {
-    uname_m=`uname -m`
-    case $uname_m in
-       i386) ARCH=x86;;
-       i686) ARCH=x86;;
-       *86) ARCH=x86;;
-       "Power Macintosh") ARCH=ppc;;
-    esac
+	uname_m=`uname -m`
+	case $uname_m in
+	   i386) ARCH=x86;;
+	   i686) ARCH=x86;;
+	   *86) ARCH=x86;;
+	   "Power Macintosh") ARCH=ppc;;
+	esac
 }
 
 write_test_program() {
-    echo "#include <stdio.h>" > $C_WORD.c
-    echo "int main(){printf(\"%d\", 8*sizeof(void*)); return 0; }" >> $C_WORD.c
+	echo "#include <stdio.h>" > $C_WORD.c
+	echo "int main(){printf(\"%d\", 8*sizeof(void*)); return 0; }" >> $C_WORD.c
 }
 
 find_word_size() {
-    C_WORD=factor-word-size
-    write_test_program
-    gcc -o $C_WORD $C_WORD.c
-    WORD=$(./$C_WORD)
-    check_ret $C_WORD
-    rm -f $C_WORD*
+	C_WORD=factor-word-size
+	write_test_program
+	gcc -o $C_WORD $C_WORD.c
+	WORD=$(./$C_WORD)
+	check_ret $C_WORD
+	rm -f $C_WORD*
 }
 
 set_factor_binary() {
-    case $OS in
-        windows-nt) FACTOR_BINARY=factor-nt;;
-        macosx) FACTOR_BINARY=./Factor.app/Contents/MacOS/factor;;
-        *) FACTOR_BINARY=factor;;
-    esac
+	case $OS in
+		windows-nt) FACTOR_BINARY=factor-nt;;
+		macosx) FACTOR_BINARY=./Factor.app/Contents/MacOS/factor;;
+		*) FACTOR_BINARY=factor;;
+	esac
 }
 
 echo_build_info() {
-    echo OS=$OS
-    echo ARCH=$ARCH
-    echo WORD=$WORD
-    echo FACTOR_BINARY=$FACTOR_BINARY
-    echo MAKE_TARGET=$MAKE_TARGET
-    echo BOOT_IMAGE=$BOOT_IMAGE
+	echo OS=$OS
+	echo ARCH=$ARCH
+	echo WORD=$WORD
+	echo FACTOR_BINARY=$FACTOR_BINARY
+	echo MAKE_TARGET=$MAKE_TARGET
+	echo BOOT_IMAGE=$BOOT_IMAGE
 }
 
 set_build_info() {
-    if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
-        echo "OS, ARCH, or WORD is empty.  Please report this"
-        exit 5
-    fi
-    MAKE_TARGET=$OS-$ARCH-$WORD
-    BOOT_IMAGE=boot.$ARCH.$WORD.image
+	if ! [[ -n $OS && -n $ARCH && -n $WORD ]] ; then
+		echo "OS, ARCH, or WORD is empty.  Please report this"
+		exit 5
+	fi
+	MAKE_TARGET=$OS-$ARCH-$WORD
+	BOOT_IMAGE=boot.$ARCH.$WORD.image
 }
 
 find_build_info() {
-    find_os
-    find_architecture
-    find_word_size
-    set_factor_binary
-    set_build_info
-    echo_build_info
+	find_os
+	find_architecture
+	find_word_size
+	set_factor_binary
+	set_build_info
+	echo_build_info
 }
 
 git_clone() {
-    echo "Downloading the git repository from factorcode.org..."
-    git clone git://factorcode.org/git/factor.git
-    check_ret git
+	echo "Downloading the git repository from factorcode.org..."
+	git clone git://factorcode.org/git/factor.git
+	check_ret git
 }
 
 git_pull_factorcode() {
-    git pull git://factorcode.org/git/factor.git
-    check_ret git
+	git pull git://factorcode.org/git/factor.git
+	check_ret git
 }
 
 cd_factor() {
-    cd factor
-    check_ret cd
+	cd factor
+	check_ret cd
+}
+
+make_clean() {
+	make clean
+	check_ret make
 }
 
 make_factor() {
-    make $MAKE_TARGET
-    check_ret make
+	make $MAKE_TARGET -j5
+	check_ret make
 }
 
 delete_boot_images() {
-    echo "Deleting old images..."
-    rm $BOOT_IMAGE > /dev/null 2>&1
-    rm $BOOT_IMAGE.* > /dev/null 2>&1
+	echo "Deleting old images..."
+	rm $BOOT_IMAGE > /dev/null 2>&1
+	rm $BOOT_IMAGE.* > /dev/null 2>&1
 }
 
 get_boot_image() {
-    wget http://factorcode.org/images/latest/$BOOT_IMAGE
-    check_ret wget
+	wget http://factorcode.org/images/latest/$BOOT_IMAGE
+	check_ret wget
 }
 
 maybe_download_dlls() {
-    if [[ $OS == windows-nt ]] ; then
-        wget http://factorcode.org/dlls/freetype6.dll
-        check_ret
-        wget http://factorcode.org/dlls/zlib1.dll
-        check_ret
-        chmod 777 *.dll
-        check_ret
-    fi
+	if [[ $OS == windows-nt ]] ; then
+		wget http://factorcode.org/dlls/freetype6.dll
+		check_ret
+		wget http://factorcode.org/dlls/zlib1.dll
+		check_ret
+		chmod 777 *.dll
+		check_ret
+	fi
 }
 
 bootstrap() {
-    ./$FACTOR_BINARY -i=$BOOT_IMAGE
+	./$FACTOR_BINARY -i=$BOOT_IMAGE
 }
 
 usage() {
-    echo "usage: $0 install|update"
+	echo "usage: $0 install|update"
 }
 
 case "$1" in
-    install)
+	install)
 		check_factor_exists
-        check_installed_programs
-        find_build_info
-        git_clone
-        cd_factor
-        make_factor
-        get_boot_image
-        maybe_download_dlls
-        bootstrap
-    ;;
+		check_installed_programs
+		find_build_info
+		git_clone
+		cd_factor
+		make_factor
+		get_boot_image
+		maybe_download_dlls
+		bootstrap
+	;;
 
-    update)
-        check_installed_programs
-        find_build_info
-        git_pull_factorcode
-        make_factor
-        delete_boot_images
-        get_boot_image
-        bootstrap
-    ;;
+	update)
+		check_installed_programs
+		find_build_info
+		git_pull_factorcode
+		make_clean
+		make_factor
+		delete_boot_images
+		get_boot_image
+		bootstrap
+	;;
 
-    *)
-        usage
-    ;;
+	*)
+		usage
+	;;
 esac

From af2cb6d2e1aefda73f5d427bfa6782e1d195bdbf Mon Sep 17 00:00:00 2001
From: Doug Coleman <doug.coleman@gmail.com>
Date: Sat, 24 Nov 2007 19:15:45 -0600
Subject: [PATCH 12/12] Don't open all UI windows at 0,0 in Windows

---
 extra/ui/windows/windows.factor | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/extra/ui/windows/windows.factor b/extra/ui/windows/windows.factor
index 290e4ef311..5fa078de89 100755
--- a/extra/ui/windows/windows.factor
+++ b/extra/ui/windows/windows.factor
@@ -381,7 +381,7 @@ M: windows-ui-backend (close-window)
     >r class-name-ptr get-global f r>
     >r >r >r ex-style r> r>
         WS_CLIPSIBLINGS WS_CLIPCHILDREN bitor style bitor
-        0 0 r>
+        CW_USEDEFAULT dup r>
     get-RECT-dimensions
     f f f GetModuleHandle f CreateWindowEx dup win32-error=0/f ;