From 3b0061c7cbebd23f6f8ac0d52722cd4b8095ee6c Mon Sep 17 00:00:00 2001
From: Reini Urban <rurban@cpan.org>
Date: Sun, 11 Mar 2018 19:05:31 +0100
Subject: [PATCH] [maint] Fix compilation errors and warnings

for ./configure --enable-trace --enable-write.

* configure.ac: probe for malloc.h.
* src/decode.c: global loglevel, static variable 'loglevel' is used in
    an inline function with external linkage [-Wstatic-in-inline].
  dwg_decode_handleref_with_code make global: [-Wstatic-in-inline].
* src/encode.c: declare dwg_encode_handleref,
  dwg_encode_handleref_with_code, dwg_encode_common_entity_handle_data,
  dwg_encode_common_entity_handle_data: declare it as static.
* src/dwg.c: fix various integer fmt types.
  get_first_owned_object: error with Unsupported version.
  get_next_owned_object: error with Unsupported version.
  declare dwg_encode_chains.
* src/dwg_api.c: check HAVE_MALLOC_H.
* .gitignore: add TAGS.
---
 .gitignore    |  1 +
 configure.ac  |  2 +-
 src/decode.c  |  2 +-
 src/dwg.c     | 10 +++++++++-
 src/dwg_api.c |  2 ++
 src/encode.c  | 12 ++++++++----
 6 files changed, 22 insertions(+), 7 deletions(-)

diff --git .gitignore .gitignore
index b734fe6..f5ec8f5 100644
--- .gitignore
+++ .gitignore
@@ -11,6 +11,7 @@ Makefile
 .deps
 .libs
 site.exp
+TAGS
 
 # globs, in specific dirs
 /examples/*.log
diff --git configure.ac configure.ac
index e8c21c6..889e4fc 100644
--- configure.ac
+++ configure.ac
@@ -48,7 +48,7 @@ AC_SUBST([XML2_LIBS])
 
 dnl Checks for header files.
 AC_HEADER_STDC
-AC_CHECK_HEADERS([stdlib.h string.h unistd.h])
+AC_CHECK_HEADERS([stdlib.h string.h unistd.h malloc.h])
 
 AC_CHECK_HEADER(dejagnu.h, , [AC_MSG_ERROR(
 [Couldn't find dejagnu.h. Install Dejagnu first] )])
diff --git src/decode.c src/decode.c
index 1425a26..ac61e3d 100644
--- src/decode.c
+++ src/decode.c
@@ -35,7 +35,7 @@ extern unsigned int
 bit_ckr8(unsigned int dx, unsigned char *adr, long n);
 
 /* The logging level for the read (decode) path.  */
-static unsigned int loglevel;
+unsigned int loglevel;
 
 #ifdef USE_TRACING
 /* This flag means we have checked the environment variable
diff --git src/dwg.c src/dwg.c
index 9c4b9a1..a51b917 100644
--- src/dwg.c
+++ src/dwg.c
@@ -23,11 +23,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <assert.h>
 
 #include "bits.h"
 #include "common.h"
 #include "decode.h"
 #include "dwg.h"
+#include "encode.h"
 
 #include "logging.h"
 
@@ -194,7 +196,7 @@ dwg_bmp(Dwg_Data *stk, long int *size)
       else if (code == 3)
         {
           bit_read_RL(dat);
-          LOG_TRACE("\t\tWMF size: 0x%x\n", bit_read_RL (dat))
+          LOG_TRACE("\t\tWMF size: 0x%lx\n", bit_read_RL (dat))
         }
       else
         {
@@ -364,6 +366,9 @@ Dwg_Object* get_first_owned_object(Dwg_Object* hdr_obj, Dwg_Object_BLOCK_HEADER*
       hdr->__iterator = 0;
       return hdr->entities[0]->obj;
     }
+
+  LOG_ERROR("Unsupported version: %d\n", version);
+  return NULL;
 }
 
 Dwg_Object* get_next_owned_object(Dwg_Object* hdr_obj, Dwg_Object* current, Dwg_Object_BLOCK_HEADER* hdr){
@@ -381,6 +386,9 @@ Dwg_Object* get_next_owned_object(Dwg_Object* hdr_obj, Dwg_Object* current, Dwg_
       if (hdr->__iterator == hdr->owned_object_count) return 0;
       return hdr->entities[hdr->__iterator]->obj;
     }
+
+  LOG_ERROR("Unsupported version: %d\n", version);
+  return NULL;
 }
 
 void
diff --git src/dwg_api.c src/dwg_api.c
index 9531fae..ddee130 100644
--- src/dwg_api.c
+++ src/dwg_api.c
@@ -1,6 +1,8 @@
 #include "dwg_api.h"
 #include "dwg.h"
+#ifdef HAVE_MALLOC_H
 #include <malloc.h>
+#endif
 
 /*******************************************************************
 *        Functions created from macro to extract entities           *
diff --git src/encode.c src/encode.c
index 0c6a0d5..ec477ea 100644
--- src/encode.c
+++ src/encode.c
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 
 #include "common.h"
 #include "bits.h"
@@ -241,6 +242,8 @@ dwg_encode_entity(Dwg_Object * obj, Bit_Chain * dat);
 static void
 dwg_encode_object(Dwg_Object * obj, Bit_Chain * dat);
 static void
+dwg_encode_common_entity_handle_data(Bit_Chain * dat, Dwg_Object * obj);
+static void
 dwg_encode_header_variables(Bit_Chain* dat, Dwg_Data * dwg);
 void
 dwg_encode_handleref(Bit_Chain * dat, Dwg_Object * obj, Dwg_Data* dwg, Dwg_Object_Ref* ref);
@@ -518,7 +521,7 @@ dwg_encode_chains(Dwg_Data * dwg, Bit_Chain * dat)
       bit_write_CRC(dat, omap[i].address, 0xC0C1);
     }
     for (i = 0; i < dwg->num_objects; i++) 
-      LOG_INFO ("Object(%i): %6lu / Address: %08X / Idc: %u\n", 
+      LOG_INFO ("Object(%lu): %6lu / Address: %08lX / Idc: %u\n", 
 		 i, omap[i].handle, omap[i].address, omap[i].idc);
 
   /* Unknown bitdouble between objects and object map
@@ -715,7 +718,7 @@ dwg_encode_chains(Dwg_Data * dwg, Bit_Chain * dat)
   return 0;
 }
 
-#include<dwg.spec>
+#include <dwg.spec>
 
 void
 dwg_encode_add_object(Dwg_Object * obj, Bit_Chain * dat,
@@ -735,7 +738,7 @@ dwg_encode_add_object(Dwg_Object * obj, Bit_Chain * dat,
   dat->byte = address;
   dat->bit = 0;
 
-      LOG_INFO("\n\n======================\nObject number: %lu",
+      LOG_INFO("\n\n======================\nObject number: %u",
           obj->index)
 
   bit_write_MS(dat, obj->size);
@@ -1085,7 +1088,7 @@ dwg_encode_entity(Dwg_Object * obj, Bit_Chain * dat)
 
 }
 
-void
+static void
 dwg_encode_common_entity_handle_data(Bit_Chain * dat, Dwg_Object * obj)
 {
   //XXX: not sure about this
@@ -1109,6 +1112,7 @@ dwg_encode_handleref(Bit_Chain * dat, Dwg_Object * obj, Dwg_Data* dwg, Dwg_Objec
   //this function should receive a Object_Ref without an abs_ref, calculate it and return a Dwg_Handle
   //this should be a higher level function 
   //not sure if the prototype is correct
+  assert(obj);
 }
 
 void 
-- 
2.8.4 (Apple Git-73)

