From b277f5ae6fe9b263410945796263aa52d95d4ab5 Mon Sep 17 00:00:00 2001
From: Junegunn Choi <junegunn.c@gmail.com>
Date: Wed, 7 Jan 2015 00:24:05 +0900
Subject: [PATCH] Fix i386 build

---
 install              | 13 +++++++------
 src/Dockerfile.arch  |  6 ++++++
 src/Makefile         |  2 +-
 src/README.md        |  7 +++----
 src/curses/curses.go | 23 ++++++++++++-----------
 5 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/install b/install
index b83920fc..ce5ec792 100755
--- a/install
+++ b/install
@@ -15,6 +15,7 @@ check_binary() {
   echo "- Checking fzf executable"
   echo -n "  - "
   if ! "$fzf_base"/bin/fzf --version; then
+    rm -v "$fzf_base"/bin/fzf
     binary_error="Error occurred"
   fi
 }
@@ -33,7 +34,7 @@ download() {
     return
   fi
 
-  local url=https://github.com/junegunn/fzf-bin/releases/download/snapshot/${1}.tgz
+  local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
   if which curl > /dev/null; then
     curl -fL $url | tar -xz
   elif which wget > /dev/null; then
@@ -56,11 +57,11 @@ archi=$(uname -sm)
 binary_available=1
 binary_error=""
 case "$archi" in
-  "Darwin x86_64")  download fzf-$version-darwin_amd64 ;;
-# "Darwin i[36]86") download fzf-$version-darwin_386   ;;
-  "Linux x86_64")   download fzf-$version-linux_amd64  ;;
-# "Linux i[36]86")  download fzf-$version-linux_386    ;;
-  *)                binary_available=0                 ;;
+  Darwin\ x86_64) download fzf-$version-darwin_amd64 ;;
+  Darwin\ i*86)   download fzf-$version-darwin_386   ;;
+  Linux\ x86_64)  download fzf-$version-linux_amd64  ;;
+  Linux\ i*86)    download fzf-$version-linux_386    ;;
+  *)              binary_available=0                 ;;
 esac
 
 cd "$fzf_base"
diff --git a/src/Dockerfile.arch b/src/Dockerfile.arch
index 9fa4ea3c..8f942db2 100644
--- a/src/Dockerfile.arch
+++ b/src/Dockerfile.arch
@@ -13,6 +13,12 @@ ENV GOPATH /go
 ENV GOROOT /go1.4
 ENV PATH /go1.4/bin:$PATH
 
+# For i386 build
+RUN echo '[multilib]' >> /etc/pacman.conf && \
+    echo 'Include = /etc/pacman.d/mirrorlist' >> /etc/pacman.conf && \
+    pacman-db-upgrade && yes | pacman -Sy gcc-multilib lib32-ncurses && \
+    cd $GOROOT/src && GOARCH=386 ./make.bash
+
 # Symlink fzf directory
 RUN mkdir -p /go/src/github.com/junegunn && \
     ln -s /fzf /go/src/github.com/junegunn/fzf
diff --git a/src/Makefile b/src/Makefile
index 3e57a178..12767d3a 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -45,7 +45,7 @@ clean:
 	cd fzf && rm -f $(BINARY32) $(BINARY64) $(RELEASE32).tgz $(RELEASE64).tgz
 
 # Linux distribution to build fzf on
-DISTRO := ubuntu
+DISTRO := arch
 
 docker:
 	docker build -t junegunn/$(DISTRO)-sandbox - < Dockerfile.$(DISTRO)
diff --git a/src/README.md b/src/README.md
index 70b2c1d1..06b915a2 100644
--- a/src/README.md
+++ b/src/README.md
@@ -29,10 +29,9 @@ make linux
 System requirements
 -------------------
 
-Currently prebuilt binaries are provided only for 64 bit OS X and Linux.
-The install script will fall back to the legacy Ruby version on the other
-systems, but if you have Go installed, you can try building it yourself.
-(`make install`)
+Currently prebuilt binaries are provided only for OS X and Linux. The install
+script will fall back to the legacy Ruby version on the other systems, but if
+you have Go installed, you can try building it yourself. (`make install`)
 
 However, as pointed out in [golang.org/doc/install][req], the Go version will
 not run on CentOS/RHEL 5.x and thus the install script will choose the Ruby
diff --git a/src/curses/curses.go b/src/curses/curses.go
index 945a3ce4..e4a65756 100644
--- a/src/curses/curses.go
+++ b/src/curses/curses.go
@@ -1,8 +1,15 @@
 package curses
 
-// #include <ncurses.h>
-// #include <locale.h>
-// #cgo LDFLAGS: -lncurses
+/*
+#include <ncurses.h>
+#include <locale.h>
+#cgo LDFLAGS: -lncurses
+void swapOutput() {
+  FILE* temp = stdout;
+  stdout = stderr;
+  stderr = temp;
+}
+*/
 import "C"
 
 import (
@@ -162,7 +169,7 @@ func Init(color bool, color256 bool, black bool, mouse bool) {
 		// syscall.Dup2(int(in.Fd()), int(os.Stdin.Fd()))
 	}
 
-	swapOutput()
+	C.swapOutput()
 
 	C.setlocale(C.LC_ALL, C.CString(""))
 	C.initscr()
@@ -218,13 +225,7 @@ func Init(color bool, color256 bool, black bool, mouse bool) {
 
 func Close() {
 	C.endwin()
-	swapOutput()
-}
-
-func swapOutput() {
-	syscall.Dup2(2, 3)
-	syscall.Dup2(1, 2)
-	syscall.Dup2(3, 1)
+	C.swapOutput()
 }
 
 func GetBytes() []byte {
-- 
GitLab