Alexandre Julliard : makedep: Get rid of restrictions on line lengths. Merge lines that end in a backslash.

Alexandre Julliard julliard at wine.codeweavers.com
Tue Aug 1 12:43:00 CDT 2006


Module: wine
Branch: refs/heads/master
Commit: b7ef1b2e203d67e3849badc3e44f6f162b56f718
URL:    http://source.winehq.org/git/?p=wine.git;a=commit;h=b7ef1b2e203d67e3849badc3e44f6f162b56f718

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Tue Aug  1 12:16:29 2006 +0200

makedep: Get rid of restrictions on line lengths. Merge lines that end in a backslash.

---

 tools/makedep.c |   88 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/tools/makedep.c b/tools/makedep.c
index 268fdb3..fa3555b 100644
--- a/tools/makedep.c
+++ b/tools/makedep.c
@@ -22,6 +22,7 @@ #include "config.h"
 #define NO_LIBWINE_PORT
 #include "wine/port.h"
 
+#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
@@ -63,6 +64,7 @@ static const char *SrcDir = NULL;
 static const char *OutputFileName = "Makefile";
 static const char *Separator = "### Dependencies";
 static const char *ProgramName;
+static int input_line;
 
 static const char Usage[] =
     "Usage: %s [options] [files]\n"
@@ -89,7 +91,7 @@ static void fatal_error( const char *msg
 /*******************************************************************
  *         xmalloc
  */
-static void *xmalloc( int size )
+static void *xmalloc( size_t size )
 {
     void *res;
     if (!(res = malloc (size ? size : 1)))
@@ -99,6 +101,18 @@ static void *xmalloc( int size )
 
 
 /*******************************************************************
+ *         xrealloc
+ */
+void *xrealloc (void *ptr, size_t size)
+{
+    void *res;
+    assert( size );
+    if (!(res = realloc( ptr, size )))
+        fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
+    return res;
+}
+
+/*******************************************************************
  *         xstrdup
  */
 static char *xstrdup( const char *str )
@@ -121,6 +135,50 @@ static char *get_extension( char *filena
 
 
 /*******************************************************************
+ *         get_line
+ */
+static char *get_line( FILE *file )
+{
+    static char *buffer;
+    static unsigned int size;
+
+    if (!size)
+    {
+        size = 1024;
+        buffer = xmalloc( size );
+    }
+    if (!fgets( buffer, size, file )) return NULL;
+    input_line++;
+
+    for (;;)
+    {
+        char *p = buffer + strlen(buffer);
+        /* if line is larger than buffer, resize buffer */
+        while (p == buffer + size - 1 && p[-1] != '\n')
+        {
+            buffer = xrealloc( buffer, size * 2 );
+            fgets( buffer + size - 1, size + 1, file );
+            p = buffer + strlen(buffer);
+            size *= 2;
+        }
+        if (p > buffer && p[-1] == '\n')
+        {
+            *(--p) = 0;
+            if (p > buffer && p[-1] == '\r') *(--p) = 0;
+            if (p > buffer && p[-1] == '\\')
+            {
+                *(--p) = 0;
+                /* line ends in backslash, read continuation line */
+                fgets( p, size - (p - buffer), file );
+                input_line++;
+                continue;
+            }
+        }
+        return buffer;
+    }
+}
+
+/*******************************************************************
  *         is_generated
  *
  * Test if a given file type is generated during the make process
@@ -319,15 +377,13 @@ static FILE *open_include_file( INCL_FIL
  */
 static void parse_idl_file( INCL_FILE *pFile, FILE *file )
 {
-    char buffer[1024];
-    char *include;
-    int line = 0;
+    char *buffer, *include;
 
-    while (fgets( buffer, sizeof(buffer)-1, file ))
+    input_line = 0;
+    while ((buffer = get_line( file )))
     {
         char quote;
         char *p = buffer;
-        line++;
         while (*p && isspace(*p)) p++;
 
         if (!strncmp( p, "import", 6 ))
@@ -351,9 +407,9 @@ static void parse_idl_file( INCL_FILE *p
         include = p;
         while (*p && (*p != quote)) p++;
         if (!*p) fatal_error( "%s:%d: Malformed #include or import directive\n",
-                              pFile->filename, line );
+                              pFile->filename, input_line );
         *p = 0;
-        add_include( pFile, include, line, (quote == '>') );
+        add_include( pFile, include, input_line, (quote == '>') );
     }
 }
 
@@ -362,15 +418,13 @@ static void parse_idl_file( INCL_FILE *p
  */
 static void parse_c_file( INCL_FILE *pFile, FILE *file )
 {
-    char buffer[1024];
-    char *include;
-    int line = 0;
+    char *buffer, *include;
 
-    while (fgets( buffer, sizeof(buffer)-1, file ))
+    input_line = 0;
+    while ((buffer = get_line( file )))
     {
         char quote;
         char *p = buffer;
-        line++;
         while (*p && isspace(*p)) p++;
         if (*p++ != '#') continue;
         while (*p && isspace(*p)) p++;
@@ -383,9 +437,9 @@ static void parse_c_file( INCL_FILE *pFi
         include = p;
         while (*p && (*p != quote)) p++;
         if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
-                              pFile->filename, line );
+                              pFile->filename, input_line );
         *p = 0;
-        add_include( pFile, include, line, (quote == '>') );
+        add_include( pFile, include, input_line, (quote == '>') );
     }
 }
 
@@ -485,11 +539,11 @@ static void output_dependencies(void)
     INCL_FILE *pFile;
     int i, column;
     FILE *file = NULL;
-    char buffer[1024];
+    char *buffer;
 
     if (Separator && ((file = fopen( OutputFileName, "r+" ))))
     {
-        while (fgets( buffer, sizeof(buffer), file ))
+        while ((buffer = get_line( file )))
             if (!strncmp( buffer, Separator, strlen(Separator) )) break;
         ftruncate( fileno(file), ftell(file) );
         fseek( file, 0L, SEEK_END );




More information about the wine-cvs mailing list