From 08cc4b7ec6c97ce35c9b005c92a4583ed53c5e52 Mon Sep 17 00:00:00 2001
From: chrysn <chrysn@fsfe.org>
Date: Wed, 11 Mar 2015 19:05:50 +0100
Subject: [PATCH 4/4] unix port: rename archtecture defines (linux ->
 LWIP_UNIX_LINUX)

the fine differences between unix implementations used to be covered by
preprocessor macros "linux", "openbsd" and "cygwin". given that gcc
under linux pre-defines a legacy "linux" macro, this can lead to
confusion -- the default setting of -Dopenbsd is often ignored due to
the sequence ifdefs often are used in (if defined linux, else if defined
openbsd), but that is a time bomb.

putting gcc in standard mode using --std=c99 or similar would remove the
macro, but still make the flags hard to spot; besides, the rest of the
code does not work with standard C out of the box.

as a solution, all macros are prefixed with LWIP_UNIX_ followed by an
uppercased variant of the operating system name. in the course of this
change, the defaults are set to LINUX in all make files that use it --
the default was sometimes openbsd, sometimes "generic" before, but
covered by the linux-define of gcc on linux systems.
---
 ports/unix/netif/delif.c           | 18 +++++++++---------
 ports/unix/netif/sio.c             |  8 ++++----
 ports/unix/netif/tapif.c           |  8 ++++----
 ports/unix/netif/unixif.c          | 16 ++++++++--------
 ports/unix/proj/lib/Makefile       |  5 ++++-
 ports/unix/proj/minimal/Makefile   |  9 ++++-----
 ports/unix/proj/minimal/mintapif.c |  6 +++---
 ports/unix/proj/unixsim/Makefile   |  8 ++++----
 8 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/ports/unix/netif/delif.c b/ports/unix/netif/delif.c
index 6371e84..7871304 100644
--- a/ports/unix/netif/delif.c
+++ b/ports/unix/netif/delif.c
@@ -37,11 +37,11 @@
 #include "lwip/def.h"
 #include "netif/delif.h"
 
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
 #include "netif/tapif.h"
-#else /* linux */
+#else /* LWIP_UNIX_LINUX */
 #include "netif/tunif.h"
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
 
 #include "lwip/sys.h"
 
@@ -253,12 +253,12 @@ delif_init(struct netif *netif)
       free(del);
       return ERR_MEM;
   }
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
   /*  tapif_init(del->netif);*/
   tunif_init(del->netif);
-#else /* linux */
+#else /* LWIP_UNIX_LINUX */
   tunif_init(del->netif);
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
   del->input = netif->input;
   del->netif->input = delif_input;
   sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
@@ -275,11 +275,11 @@ delif_thread(void *arg)
   sys_sem_t sem;
   
   del = netif->state;
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
   tapif_init(del->netif);
-#else /* linux */
+#else /* LWIP_UNIX_LINUX */
   tunif_init(del->netif);
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
 
   sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
   sys_timeout(DELIF_TIMEOUT, delif_output_timeout, netif);
diff --git a/ports/unix/netif/sio.c b/ports/unix/netif/sio.c
index 288696a..86790dd 100644
--- a/ports/unix/netif/sio.c
+++ b/ports/unix/netif/sio.c
@@ -20,7 +20,7 @@
 #undef NTOHS
 
 #include <stdlib.h>
-#if defined(openbsd)
+#if defined(LWIP_UNIX_OPENBSD)
 #include <util.h>
 #endif
 #include <termios.h>
@@ -31,7 +31,7 @@
 #include <sys/signal.h>
 #include <sys/types.h>
 
-#if PPP_SUPPORT && defined(linux)
+#if PPP_SUPPORT && defined(LWIP_UNIX_LINUX)
 #include <pty.h>
 #endif
 
@@ -127,9 +127,9 @@ static int sio_unix_init( char * device, int devnum, sio_status_t * siostat )
 	}
 
 	saio.sa_flags = 0;
-#if linux
+#if defined(LWIP_UNIX_LINUX)
 	saio.sa_restorer = NULL;
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
 	sigaction( SIGIO,&saio,NULL );
 
 	/* allow the process to receive SIGIO */
diff --git a/ports/unix/netif/tapif.c b/ports/unix/netif/tapif.c
index 0ef3c8f..496a23d 100644
--- a/ports/unix/netif/tapif.c
+++ b/ports/unix/netif/tapif.c
@@ -60,7 +60,7 @@
 
 #define IFCONFIG_BIN "/sbin/ifconfig "
 
-#if defined(linux)
+#if defined(LWIP_UNIX_LINUX)
 #include <sys/ioctl.h>
 #include <linux/if.h>
 #include <linux/if_tun.h>
@@ -74,7 +74,7 @@
 #define DEVTAP "/dev/net/tun"
 #define NETMASK_ARGS "netmask %d.%d.%d.%d"
 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
-#elif defined(openbsd)
+#elif defined(LWIP_UNIX_OPENBSD)
 #define DEVTAP "/dev/tun0"
 #define NETMASK_ARGS "netmask %d.%d.%d.%d"
 #define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d " NETMASK_ARGS " link0"
@@ -128,7 +128,7 @@ low_level_init(struct netif *netif)
   tapif->fd = open(DEVTAP, O_RDWR);
   LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd));
   if(tapif->fd == -1) {
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
     perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP);
 #else
     perror("tapif_init: cannot open "DEVTAP);
@@ -136,7 +136,7 @@ low_level_init(struct netif *netif)
     exit(1);
   }
 
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
   {
     struct ifreq ifr;
     memset(&ifr, 0, sizeof(ifr));
diff --git a/ports/unix/netif/unixif.c b/ports/unix/netif/unixif.c
index a8f7f13..dcc17ee 100644
--- a/ports/unix/netif/unixif.c
+++ b/ports/unix/netif/unixif.c
@@ -84,7 +84,7 @@ static int
 unix_socket_client(const char *name)
 {
   int fd;
-#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
+#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
   int len;
 #endif
   struct sockaddr_un unix_addr;
@@ -99,11 +99,11 @@ unix_socket_client(const char *name)
   memset(&unix_addr, 0, sizeof(unix_addr));
   unix_addr.sun_family = AF_UNIX;
   snprintf(unix_addr.sun_path, sizeof(unix_addr.sun_path), "%s%05d", "/var/tmp/", getpid());
-#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
+#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
   len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
     strlen(unix_addr.sun_path) + 1;
   unix_addr.sun_len = len;
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
 
   unlink(unix_addr.sun_path);             /* in case it already exists */
   if (bind(fd, (struct sockaddr *) &unix_addr,
@@ -120,11 +120,11 @@ unix_socket_client(const char *name)
   memset(&unix_addr, 0, sizeof(unix_addr));
   unix_addr.sun_family = AF_UNIX;
   strcpy(unix_addr.sun_path, name);
-#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
+#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
   len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
     strlen(unix_addr.sun_path) + 1;  
   unix_addr.sun_len = len;
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
   if (connect(fd, (struct sockaddr *) &unix_addr,
       sizeof(struct sockaddr_un)) < 0) {
     perror("unixif: unix_socket_client: socket");
@@ -138,7 +138,7 @@ static int
 unix_socket_server(const char *name)
 {
   int fd;
-#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
+#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
   int len;
 #endif
   struct sockaddr_un unix_addr;
@@ -155,11 +155,11 @@ unix_socket_server(const char *name)
   memset(&unix_addr, 0, sizeof(unix_addr));
   unix_addr.sun_family = AF_UNIX;
   strcpy(unix_addr.sun_path, name);
-#if !defined(linux) && !defined(cygwin) && !defined(__CYGWIN__)
+#if !defined(LWIP_UNIX_LINUX) && !defined(LWIP_UNIX_CYGWIN) && !defined(__CYGWIN__)
   len = sizeof(unix_addr.sun_len) + sizeof(unix_addr.sun_family) +
     strlen(unix_addr.sun_path) + 1;
   unix_addr.sun_len = len;
-#endif /* linux */
+#endif /* LWIP_UNIX_LINUX */
 
   /* bind the name to the descriptor */
   if (bind(fd, (struct sockaddr *) &unix_addr,
diff --git a/ports/unix/proj/lib/Makefile b/ports/unix/proj/lib/Makefile
index 04a457d..1ac9366 100644
--- a/ports/unix/proj/lib/Makefile
+++ b/ports/unix/proj/lib/Makefile
@@ -39,7 +39,10 @@ LWIPDIR=$(CONTRIBDIR)/../lwip/src
 
 CCDEP=gcc
 CC=gcc
-CFLAGS=-g -Wall -DLWIP_DEBUG -fPIC -pedantic -Werror \
+#To compile for openbsd: make UNIXARCH=OPENBSD
+#To compile for cygwin: make UNIXARCH=CYGWIN
+UNIXARCH ?= LINUX
+CFLAGS=-g -Wall -DLWIP_UNIX_${UNIXARCH} -DLWIP_DEBUG -fPIC -pedantic -Werror \
 	-Wparentheses -Wsequence-point -Wswitch-default \
 	-Wextra -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast \
 	-Wc++-compat -Wwrite-strings -Wold-style-definition \
diff --git a/ports/unix/proj/minimal/Makefile b/ports/unix/proj/minimal/Makefile
index 8a8f8ed..a0ac3a7 100644
--- a/ports/unix/proj/minimal/Makefile
+++ b/ports/unix/proj/minimal/Makefile
@@ -31,11 +31,10 @@
  
 CCDEP=gcc
 CC=gcc
-#To compile for linux: make ARCH=linux
-#To compile for cygwin: make ARCH=cygwin
-#To compile for openbsd: make ARCH=openbsd
-ARCH=openbsd
-CFLAGS=-g -Wall -D$(ARCH) -Os -DLWIP_DEBUG -pedantic -Werror \
+#To compile for openbsd: make UNIXARCH=OPENBSD
+#To compile for cygwin: make UNIXARCH=CYGWIN
+UNIXARCH ?= LINUX
+CFLAGS=-g -Wall -DLWIP_UNIX_$(UNIXARCH) -Os -DLWIP_DEBUG -pedantic -Werror \
 	-Wparentheses -Wsequence-point -Wswitch-default \
 	-Wextra -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast \
 	-Wc++-compat -Wwrite-strings -Wold-style-definition \
diff --git a/ports/unix/proj/minimal/mintapif.c b/ports/unix/proj/minimal/mintapif.c
index 0fabe8d..6fbae6a 100644
--- a/ports/unix/proj/minimal/mintapif.c
+++ b/ports/unix/proj/minimal/mintapif.c
@@ -43,14 +43,14 @@
 #include <sys/uio.h>
 #include <sys/socket.h>
 
-#if defined(linux)
+#if defined(LWIP_UNIX_LINUX)
 #include <sys/ioctl.h>
 #include <linux/if.h>
 #include <linux/if_tun.h>
 #define DEVTAP "/dev/net/tun"
 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d"
 
-#elif defined(openbsd)
+#elif defined(LWIP_UNIX_OPENBSD)
 #define DEVTAP "/dev/tun0"
 #define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d link0"
 
@@ -112,7 +112,7 @@ low_level_init(struct netif *netif)
     exit(1);
   }
 
-#ifdef linux
+#ifdef LWIP_UNIX_LINUX
   {
     struct ifreq ifr;
     memset(&ifr, 0, sizeof(ifr));
diff --git a/ports/unix/proj/unixsim/Makefile b/ports/unix/proj/unixsim/Makefile
index a6f4fc4..86b678b 100644
--- a/ports/unix/proj/unixsim/Makefile
+++ b/ports/unix/proj/unixsim/Makefile
@@ -32,10 +32,10 @@
 CCDEP=gcc
 CC=gcc
 
-#To compile for linux: make ARCH=linux
-#To compile for cygwin: make ARCH=cygwin
-ARCH=unix
-CFLAGS=-g -Wall -D$(ARCH) -DLWIP_DEBUG -pedantic -Werror \
+#To compile for openbsd: make UNIXARCH=OPENBSD
+#To compile for cygwin: make UNIXARCH=CYGWIN
+UNIXARCH ?= LINUX
+CFLAGS=-g -Wall -DLWIP_UNIX_$(UNIXARCH) -DLWIP_DEBUG -pedantic -Werror \
 	-Wparentheses -Wsequence-point -Wswitch-default \
 	-Wextra -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast \
 	-Wc++-compat -Wwrite-strings -Wold-style-definition \
-- 
2.1.4

