From 285f210af3fbe69247d0bdddad794c0a68ffef66 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Fri, 24 Apr 2015 23:24:30 +1000
Subject: Fix for gcc2


diff --git a/sanei/sanei_thread.c b/sanei/sanei_thread.c
index fd58af2..9ac397f 100644
--- a/sanei/sanei_thread.c
+++ b/sanei/sanei_thread.c
@@ -486,6 +486,7 @@ sanei_thread_sendsig( SANE_Pid pid, int sig )
 SANE_Pid
 sanei_thread_waitpid( SANE_Pid pid, int *status )
 {
+	int rc;
 #ifdef USE_PTHREAD
 	int *ls;
 #else
@@ -499,7 +500,6 @@ sanei_thread_waitpid( SANE_Pid pid, int *status )
 	DBG(2, "sanei_thread_waitpid() - %ld\n",
 	    sanei_thread_pid_to_long(pid));
 #ifdef USE_PTHREAD
-	int rc;
 	rc = pthread_join( (pthread_t)pid, (void*)&ls );
 
 	if( 0 == rc ) {
-- 
2.2.2


From f8cce1fbe95aa3cc42b42dcf41fabf9c5590effc Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Sat, 25 Apr 2015 14:00:41 +1000
Subject: Fix for gcc2


diff --git a/backend/epson2_net.h b/backend/epson2_net.h
index 6aef2b7..b00b48c 100644
--- a/backend/epson2_net.h
+++ b/backend/epson2_net.h
@@ -2,6 +2,11 @@
 #define _EPSON2_NET_H_
 
 #include <sys/types.h>
+
+#ifdef __HAIKU__
+#include <sys/select.h>
+#endif
+
 #include "../include/sane/sane.h"
 
 extern int sanei_epson_net_read(struct Epson_Scanner *s, unsigned char *buf, ssize_t buf_size,
diff --git a/backend/epsonds-cmd.c b/backend/epsonds-cmd.c
index 5fb1f9d..a766c9f 100644
--- a/backend/epsonds-cmd.c
+++ b/backend/epsonds-cmd.c
@@ -225,9 +225,11 @@ static SANE_Status esci2_cmd_simple(epsonds_scanner* s, char *cmd, SANE_Status (
 
 SANE_Status esci2_fin(epsonds_scanner *s)
 {
+	SANE_Status status;
+	
 	DBG(5, "%s\n", __func__);
 
-	SANE_Status status = esci2_cmd_simple(s, "FIN x0000000", NULL);
+	status = esci2_cmd_simple(s, "FIN x0000000", NULL);
 	s->locked = 0;
 	return status;
 }
@@ -260,6 +262,7 @@ static int decode_value(char *buf, int len)
 /* h000 */
 static char *decode_binary(char *buf)
 {
+	int hl;
 	char tmp[6];
 
 	memcpy(tmp, buf, 4);
@@ -268,7 +271,7 @@ static char *decode_binary(char *buf)
 	if (buf[0] != 'h')
 		return NULL;
 
-	int hl = strtol(tmp + 1, NULL, 16);
+	hl = strtol(tmp + 1, NULL, 16);
 	if (hl) {
 
 		char *v = malloc(hl + 1);
@@ -283,12 +286,13 @@ static char *decode_binary(char *buf)
 
 static char *decode_string(char *buf)
 {
+	char *p;
 	char *s = decode_binary(buf);
 	if (s == NULL)
 		return NULL;
 
 	/* trim white space at the end */
-	char *p = s + strlen(s);
+	p = s + strlen(s);
 	while (*--p == ' ')
 		*p = '\0';
 
@@ -310,13 +314,13 @@ static SANE_Status info_cb(void *userdata, char *token, int len)
 {
 	epsonds_scanner *s = (epsonds_scanner *)userdata;
 
+	/* pointer to the token's value */
+	char *value = token + 3;
+
 	if (DBG_LEVEL >= 11) {
 		debug_token(DBG_LEVEL, __func__, token, len);
 	}
 
-	/* pointer to the token's value */
-	char *value = token + 3;
-
 	/* nrd / nrdBUSY */
 
 	if (strncmp("nrd", token, 3) == 0) {
@@ -537,11 +541,11 @@ static SANE_Status info_cb(void *userdata, char *token, int len)
 
 SANE_Status esci2_info(epsonds_scanner *s)
 {
-	DBG(1, "= gathering device information\n");
-
 	SANE_Status status;
 	int i = 4;
 
+	DBG(1, "= gathering device information\n");
+
 	do {
 		status = esci2_cmd_simple(s, "INFOx0000000", &info_cb);
 		if (status == SANE_STATUS_DEVICE_BUSY) {
@@ -798,11 +802,11 @@ static SANE_Status img_cb(void *userdata, char *token, int len)
 
 	if (strncmp("err", token, 3) == 0) {
 
-		s->scanning = 0;
-
 		char *option = token + 3;	/* ADF, TPU, FB */
 		char *cause = token + 3 + 4;	/* OPN, PJ, PE, ERR, LTF, LOCK, DFED, DTCL, AUT, PERM */
 
+		s->scanning = 0;
+
 		DBG(1, "%s: error on option %3.3s, cause %4.4s\n",
 			__func__, option, cause);
 
@@ -837,6 +841,11 @@ SANE_Status
 esci2_img(struct epsonds_scanner *s, SANE_Int *length)
 {
 	SANE_Status status = SANE_STATUS_GOOD;
+	SANE_Status parse_status;
+
+	ssize_t read;
+
+	unsigned int more = 0;
 
 	*length = 0;
 
@@ -857,13 +866,13 @@ esci2_img(struct epsonds_scanner *s, SANE_Int *length)
 	}
 
 	/* check if we need to read any image data */
-	unsigned int more = 0;
+	more = 0;
 	if (!esci2_check_header("IMG ", (char *)s->buf, &more)) {
 		return SANE_STATUS_IO_ERROR;
 	}
 
 	/* this handles eof and errors */
-	SANE_Status parse_status = esci2_parse_block((char *)s->buf + 12, 64 - 12, s, &img_cb);
+	parse_status = esci2_parse_block((char *)s->buf + 12, 64 - 12, s, &img_cb);
 
 	/* no more data? return using the status of the esci2_parse_block
 	 * call, which might hold other error conditions.
@@ -873,7 +882,7 @@ esci2_img(struct epsonds_scanner *s, SANE_Int *length)
 	}
 
 	/* ALWAYS read image data */
-	ssize_t read = eds_recv(s, s->buf, more, &status);
+	read = eds_recv(s, s->buf, more, &status);
 	if (status != SANE_STATUS_GOOD) {
 		return status;
 	}
diff --git a/backend/epsonds-jpeg.c b/backend/epsonds-jpeg.c
index d825d99..03dc6b3 100644
--- a/backend/epsonds-jpeg.c
+++ b/backend/epsonds-jpeg.c
@@ -13,6 +13,10 @@
 
 #define DEBUG_DECLARE_ONLY
 
+#ifdef __HAIKU__
+#define _STDLIB_H_
+#endif
+
 #include <math.h>
 
 #include "epsonds.h"
@@ -47,6 +51,7 @@ jpeg_term_source(j_decompress_ptr UNUSEDARG cinfo)
 METHODDEF(boolean)
 jpeg_fill_input_buffer(j_decompress_ptr cinfo)
 {
+	int size;
 	epsonds_src_mgr *src = (epsonds_src_mgr *)cinfo->src;
 
 	/* read from the scanner or the ring buffer */
@@ -57,7 +62,7 @@ jpeg_fill_input_buffer(j_decompress_ptr cinfo)
 	}
 
 	/* read from scanner if no data? */
-	int size = min(1024, avail);
+	size = min(1024, avail);
 
 	eds_ring_read(src->s->current, src->buffer, size);
 
@@ -129,12 +134,14 @@ eds_jpeg_read_header(epsonds_scanner *s)
 
 		if (jpeg_start_decompress(&s->jpeg_cinfo)) {
 
+			int size;
+
 			DBG(3, "%s: w: %d, h: %d, components: %d\n",
 				__func__,
 				s->jpeg_cinfo.output_width, s->jpeg_cinfo.output_height,
 				s->jpeg_cinfo.output_components);
 
-			int size = s->jpeg_cinfo.output_width * s->jpeg_cinfo.output_components * 1;
+			size = s->jpeg_cinfo.output_width * s->jpeg_cinfo.output_components * 1;
 
 			src->linebuffer = (*s->jpeg_cinfo.mem->alloc_large)((j_common_ptr)&s->jpeg_cinfo,
 				JPOOL_PERMANENT, size);
@@ -166,13 +173,14 @@ void
 eds_jpeg_read(SANE_Handle handle, SANE_Byte *data,
 	   SANE_Int max_length, SANE_Int *length)
 {
+	int l;
 	epsonds_scanner *s = handle;
 
-	*length = 0;
-
 	struct jpeg_decompress_struct cinfo = s->jpeg_cinfo;
 	epsonds_src_mgr *src = (epsonds_src_mgr *)s->jpeg_cinfo.src;
 
+	*length = 0;
+
 	/* copy from line buffer if available */
 	if (src->linebuffer_size && src->linebuffer_index < src->linebuffer_size) {
 
@@ -196,7 +204,7 @@ eds_jpeg_read(SANE_Handle handle, SANE_Byte *data,
 	 * only one line at time is supported
 	 */
 
-	int l = jpeg_read_scanlines(&cinfo, s->jdst->buffer, 1);
+	l = jpeg_read_scanlines(&cinfo, s->jdst->buffer, 1);
 	if (l == 0) {
 		return;
 	}
diff --git a/backend/epsonds-ops.c b/backend/epsonds-ops.c
index 94f1071..e4a6291 100644
--- a/backend/epsonds-ops.c
+++ b/backend/epsonds-ops.c
@@ -48,9 +48,10 @@ eds_dev_init(epsonds_device *dev)
 SANE_Status
 eds_dev_post_init(struct epsonds_device *dev)
 {
+	SANE_String_Const *source_list_add = source_list;
+
 	DBG(10, "%s\n", __func__);
 
-	SANE_String_Const *source_list_add = source_list;
 	if (dev->has_fb)
 		*source_list_add++ = FBF_STR;
 
@@ -336,13 +337,14 @@ eds_copy_image_from_ring(epsonds_scanner *s, SANE_Byte *data, SANE_Int max_lengt
 	if (s->params.depth == 1) {
 
 		while (lines--) {
+			int i;
+			SANE_Byte *p;
 
 			eds_ring_read(s->current, s->line_buffer, s->params.bytes_per_line);
 			eds_ring_skip(s->current, s->dummy);
 
-			int i;
 
-			SANE_Byte *p = s->line_buffer;
+			p = s->line_buffer;
 
 			for (i = 0; i < s->params.bytes_per_line; i++) {
 				*data++ = ~*p++;
@@ -379,12 +381,13 @@ SANE_Status eds_ring_init(ring_buffer *ring, SANE_Int size)
 
 SANE_Status eds_ring_write(ring_buffer *ring, SANE_Byte *buf, SANE_Int size)
 {
+	SANE_Int tail = ring->end - ring->wp;
+
 	if (size > (ring->size - ring->fill)) {
 		DBG(1, "ring buffer full, requested: %d, available: %d\n", size, ring->size - ring->fill);
 		return SANE_STATUS_NO_MEM;
 	}
 
-	SANE_Int tail = ring->end - ring->wp;
 	if (size < tail) {
 
 		memcpy(ring->wp, buf, size);
@@ -409,6 +412,8 @@ SANE_Status eds_ring_write(ring_buffer *ring, SANE_Byte *buf, SANE_Int size)
 
 SANE_Int eds_ring_read(ring_buffer *ring, SANE_Byte *buf, SANE_Int size)
 {
+	SANE_Int tail = ring->end - ring->rp;
+
 	DBG(18, "reading from ring, %d bytes available\n", (int)ring->fill);
 
 	/* limit read to available */
@@ -417,7 +422,6 @@ SANE_Int eds_ring_read(ring_buffer *ring, SANE_Byte *buf, SANE_Int size)
 		size = ring->fill;
 	}
 
-	SANE_Int tail = ring->end - ring->rp;
 	if (size < tail) {
 
 		memcpy(buf, ring->rp, size);
@@ -444,11 +448,12 @@ SANE_Int eds_ring_read(ring_buffer *ring, SANE_Byte *buf, SANE_Int size)
 
 SANE_Int eds_ring_skip(ring_buffer *ring, SANE_Int size)
 {
+	SANE_Int tail = ring->end - ring->rp;
+
 	/* limit skip to available */
 	if (size > ring->fill)
 		size = ring->fill;
 
-	SANE_Int tail = ring->end - ring->rp;
 	if (size < tail) {
 		ring->rp += size;
 	} else {
diff --git a/backend/epsonds.c b/backend/epsonds.c
index d16744f..5a5f5b1 100644
--- a/backend/epsonds.c
+++ b/backend/epsonds.c
@@ -192,14 +192,14 @@ open_scanner(epsonds_scanner *s)
 static SANE_Status
 validate_usb(struct epsonds_scanner *s)
 {
-	DBG(1, "%s\n", __func__);
-
 	SANE_Status status;
 	int vendor, product;
 	int i = 0, numIds;
 
 	SANE_Bool is_valid = SANE_FALSE;
 
+	DBG(1, "%s\n", __func__);
+
 	/* if the sanei_usb_get_vendor_product call is not supported,
 	 * then we just ignore this and rely on the user to config
 	 * the correct device.
@@ -386,10 +386,11 @@ static SANE_Status
 attach(const char *name, int type)
 {
 	SANE_Status status;
+	epsonds_scanner *s;
 
 	DBG(7, "%s: devname = %s, type = %d\n", __func__, name, type);
 
-	epsonds_scanner *s = device_detect(name, type, &status);
+	s = device_detect(name, type, &status);
 	if (s == NULL)
 		return status;
 
@@ -1116,6 +1117,9 @@ sane_get_parameters(SANE_Handle handle, SANE_Parameters *params)
 SANE_Status
 sane_start(SANE_Handle handle)
 {
+	/* set scanning parameters */
+	char cmd[100]; /* take care not to overflow */
+
 	epsonds_scanner *s = (epsonds_scanner *)handle;
 	char buf[64];
 	SANE_Status status = 0;
@@ -1178,10 +1182,6 @@ sane_start(SANE_Handle handle)
 
 	print_params(s->params);
 
-	/* set scanning parameters */
-
-	char cmd[100]; /* take care not to overflow */
-
 	/* document source */
 	if (strcmp(source_list[s->val[OPT_SOURCE].w], ADF_STR) == 0) {
 
@@ -1264,6 +1264,7 @@ SANE_Status
 sane_read(SANE_Handle handle, SANE_Byte *data, SANE_Int max_length,
 	  SANE_Int *length)
 {
+	SANE_Int available;
 	SANE_Int read = 0, tries = 3;
 	SANE_Status status = 0;
 	epsonds_scanner *s = (epsonds_scanner *)handle;
@@ -1279,7 +1280,7 @@ sane_read(SANE_Handle handle, SANE_Byte *data, SANE_Int max_length,
 	}
 
 	/* anything in the buffer? pass it to the frontend */
-	SANE_Int available = eds_ring_avail(s->current);
+	available = eds_ring_avail(s->current);
 	if (available) {
 
 		DBG(18, "reading from ring buffer, %d left\n", available);
-- 
2.2.2


From 4b7f9a60223c5e8c99b54ff57b52303c14819106 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Sat, 25 Apr 2015 14:14:31 +1000
Subject: Disable HAVE_IFADDRS_H for Haiku


diff --git a/backend/pixma_bjnp.c b/backend/pixma_bjnp.c
index dece926..6668713 100644
--- a/backend/pixma_bjnp.c
+++ b/backend/pixma_bjnp.c
@@ -106,6 +106,11 @@
 # define SSIZE_MAX      LONG_MAX
 #endif 
 
+
+#ifdef __HAIKU__
+#undef HAVE_IFADDRS_H
+#endif
+
 /* static data */
 static bjnp_device_t device[BJNP_NO_DEVICES];
 static int bjnp_no_devices = 0;
-- 
2.2.2


From 8ef1284bbc93e1d7657433b24e053c13ede7f284 Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Sat, 25 Apr 2015 16:00:27 +1000
Subject: Hack: cut off sanei_usb_clear_halt for Haiku


diff --git a/sanei/sanei_usb.c b/sanei/sanei_usb.c
index f210d4f..fc569ac 100644
--- a/sanei/sanei_usb.c
+++ b/sanei/sanei_usb.c
@@ -2184,7 +2184,7 @@ sanei_usb_clear_halt (SANE_Int dn)
       DBG (1, "sanei_usb_clear_halt: dn >= device number || dn < 0\n");
       return SANE_STATUS_INVAL;
     }
-
+#ifndef __HAIKU__
 #ifdef HAVE_LIBUSB
 
   /* This call seems to be required by Linux xhci driver
@@ -2227,7 +2227,7 @@ sanei_usb_clear_halt (SANE_Int dn)
 #else /* not HAVE_LIBUSB && not HAVE_LIBUSB_1_0 */
   DBG (1, "sanei_usb_clear_halt: libusb support missing\n");
 #endif /* HAVE_LIBUSB || HAVE_LIBUSB_1_0 */
-
+#endif
   return SANE_STATUS_GOOD;
 }
 
-- 
2.2.2


From 5ea3cd7f4eb408791ff1c6a22b0711d43d3f208c Mon Sep 17 00:00:00 2001
From: Gerasim Troeglazov <3dEyes@gmail.com>
Date: Sat, 25 Apr 2015 16:39:38 +1000
Subject: Disable IPV6 for pixma backend


diff --git a/backend/pixma_bjnp.c b/backend/pixma_bjnp.c
index 6668713..c253ba4 100644
--- a/backend/pixma_bjnp.c
+++ b/backend/pixma_bjnp.c
@@ -109,6 +109,7 @@
 
 #ifdef __HAIKU__
 #undef HAVE_IFADDRS_H
+#undef ENABLE_IPV6
 #endif
 
 /* static data */
-- 
2.2.2

