]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
ChangeLog, ansi2knr.c:
authorHarlan Stenn <stenn@ntp.org>
Wed, 18 Aug 1999 23:33:56 +0000 (23:33 -0000)
committerHarlan Stenn <stenn@ntp.org>
Wed, 18 Aug 1999 23:33:56 +0000 (23:33 -0000)
  * util/ansi2knr.c: New release (fix for bug reported by Marc Brett)
  From: "L. Peter Deutsch" <ghost@aladdin.com>

bk: 37bb42e4za4xCfbZI5ofVgF9CuY9Vg

ChangeLog
util/ansi2knr.c

index 53ed62a69a973f2f80f345e7e986b3f8052e60f4..99755df7a1e2f7627f6ef498adcd96447c129f10 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 1999-08-18  Harlan Stenn  <stenn@whimsy.udel.edu>
 
+       * util/ansi2knr.c: New release (fix for bug reported by Marc Brett)
+       From: "L. Peter Deutsch" <ghost@aladdin.com>
+
        * include/ntp_stdlib.h: NeXT portability patch
        From: Jack Bryans <jbryans@csulb.edu>
 
index 611d9b3c1a6ff378cb42458257126691fc9fd8f1..16895283e4973ee5e0e06aba8a8bcfc765063694 100644 (file)
@@ -37,18 +37,21 @@ program under the GPL.
  * There are no error messages.
  *
  * ansi2knr recognizes function definitions by seeing a non-keyword
- * identifier at the left margin, followed by a left parenthesis,
- * with a right parenthesis as the last character on the line,
- * and with a left brace as the first token on the following line
- * (ignoring possible intervening comments), except that a line
+ * identifier at the left margin, followed by a left parenthesis, with a
+ * right parenthesis as the last character on the line, and with a left
+ * brace as the first token on the following line (ignoring possible
+ * intervening comments and/or preprocessor directives), except that a line
  * consisting of only
  *     identifier1(identifier2)
  * will not be considered a function definition unless identifier2 is
- * the word "void".  ansi2knr will recognize a multi-line header provided
- * that no intervening line ends with a left or right brace or a semicolon.
- * These algorithms ignore whitespace and comments, except that
- * the function name must be the first thing on the line.
- * The following constructs will confuse it:
+ * the word "void", and a line consisting of
+ *     identifier1(identifier2, <<arbitrary>>)
+ * will not be considered a function definition.
+ * ansi2knr will recognize a multi-line header provided that no intervening
+ * line ends with a left or right brace or a semicolon.  These algorithms
+ * ignore whitespace, comments, and preprocessor directives, except that
+ * the function name must be the first thing on the line.  The following
+ * constructs will confuse it:
  *     - Any other construct that starts at the left margin and
  *         follows the above syntax (such as a macro or function call).
  *     - Some macros that tinker with the syntax of function headers.
@@ -58,6 +61,14 @@ program under the GPL.
  * The original and principal author of ansi2knr is L. Peter Deutsch
  * <ghost@aladdin.com>.  Other authors are noted in the change history
  * that follows (in reverse chronological order):
+       lpd 1999-08-17 added code to allow preprocessor directives
+               wherever comments are allowed
+       lpd 1999-04-12 added minor fixes from Pavel Roskin
+               <pavel_roskin@geocities.com> for clean compilation with
+               gcc -W -Wall
+       lpd 1999-03-22 added hack to recognize lines consisting of
+               identifier1(identifier2, xxx) as *not* being procedures
+       lpd 1999-02-03 made indentation of preprocessor commands consistent
        lpd 1999-01-28 fixed two bugs: a '/' in an argument list caused an
                endless loop; quoted strings within an argument list
                confused the parser
@@ -158,19 +169,24 @@ program under the GPL.
 
 #endif
 
+/* Define NULL (for *very* old compilers). */
+#ifndef NULL
+# define NULL (0)
+#endif
+
 /*
  * The ctype macros don't always handle 8-bit characters correctly.
  * Compensate for this here.
  */
 #ifdef isascii
-#  undef HAVE_ISASCII          /* just in case */
-#  define HAVE_ISASCII 1
+# undef HAVE_ISASCII           /* just in case */
+# define HAVE_ISASCII 1
 #else
 #endif
 #if STDC_HEADERS || !HAVE_ISASCII
-#  define is_ascii(c) 1
+# define is_ascii(c) 1
 #else
-#  define is_ascii(c) isascii(c)
+# define is_ascii(c) isascii(c)
 #endif
 
 #define is_space(c) (is_ascii(c) && isspace(c))
@@ -182,6 +198,8 @@ program under the GPL.
 #define isidfirstchar(ch) (is_alpha(ch) || (ch) == '_')
 
 /* Forward references */
+char *ppdirforward();
+char *ppdirbackward();
 char *skipspace();
 char *scanstring();
 int writeblanks();
@@ -263,6 +281,11 @@ main(argc, argv)
        if ( filename )
          fprintf(out, "#line 1 \"%s\"\n", filename);
        buf = malloc(bufsize);
+       if ( buf == NULL )
+          {
+               fprintf(stderr, "Unable to allocate read buffer!\n");
+               exit(1);
+          }
        line = buf;
        while ( fgets(line, (unsigned)(buf + bufsize - line), in) != NULL )
           {
@@ -279,7 +302,7 @@ f:                  if ( line >= buf + (bufsize - 1) ) /* overflow check */
                          goto wl;
                        if ( fgets(line, (unsigned)(buf + bufsize - line), in) == NULL )
                          goto wl;
-                       switch ( *skipspace(more, 1) )
+                       switch ( *skipspace(ppdirforward(more), 1) )
                          {
                          case '{':
                            /* Definitely a function header. */
@@ -330,32 +353,70 @@ wl:                       fputs(buf, out);
        return 0;
 }
 
-/* Skip over whitespace and comments, in either direction. */
+/*
+ * Skip forward or backward over one or more preprocessor directives.
+ */
+char *
+ppdirforward(p)
+    char *p;
+{
+    for (; *p == '#'; ++p) {
+       for (; *p != '\r' && *p != '\n'; ++p)
+           if (*p == 0)
+               return p;
+       if (*p == '\r' && p[1] == '\n')
+           ++p;
+    }
+    return p;
+}
+char *
+ppdirbackward(p, limit)
+    char *p;
+    char *limit;
+{
+    char *np = p;
+
+    for (;; p = --np) {
+       if (*np == '\n' && np[-1] == '\r')
+           --np;
+       for (; np > limit && np[-1] != '\r' && np[-1] != '\n'; --np)
+           if (np[-1] == 0)
+               return np;
+       if (*np != '#')
+           return p;
+    }
+}
+
+/*
+ * Skip over whitespace, comments, and preprocessor directives,
+ * in either direction.
+ */
 char *
 skipspace(p, dir)
-    register char *p;
-    register int dir;                  /* 1 for forward, -1 for backward */
-{      for ( ; ; )
-          {    while ( is_space(*p) )
-                 p += dir;
-               if ( !(*p == '/' && p[dir] == '*') )
-                 break;
-               p += dir;  p += dir;
-               while ( !(*p == '*' && p[dir] == '/') )
-                  {    if ( *p == 0 )
-                         return p;     /* multi-line comment?? */
-                       p += dir;
-                  }
-               p += dir;  p += dir;
-          }
-       return p;
+    char *p;
+    int dir;                   /* 1 for forward, -1 for backward */
+{
+    for ( ; ; ) {
+       while ( is_space(*p) )
+           p += dir;
+       if ( !(*p == '/' && p[dir] == '*') )
+           break;
+       p += dir;  p += dir;
+       while ( !(*p == '*' && p[dir] == '/') ) {
+           if ( *p == 0 )
+               return p;       /* multi-line comment?? */
+           p += dir;
+       }
+       p += dir;  p += dir;
+    }
+    return p;
 }
 
 /* Scan over a quoted string, in either direction. */
 char *
 scanstring(p, dir)
-    register char *p;
-    register int dir;
+    char *p;
+    int dir;
 {
     for (p += dir; ; p += dir)
        if (*p == '"' && p[-dir] != '\\')
@@ -393,14 +454,14 @@ writeblanks(start, end)
 int
 test1(buf)
     char *buf;
-{      register char *p = buf;
+{      char *p = buf;
        char *bend;
        char *endfn;
        int contin;
 
        if ( !isidfirstchar(*p) )
          return 0;             /* no name at left margin */
-       bend = skipspace(buf + strlen(buf) - 1, -1);
+       bend = skipspace(ppdirbackward(buf + strlen(buf) - 1, buf), -1);
        switch ( *bend )
           {
           case ';': contin = 0 /*2*/; break;
@@ -430,7 +491,7 @@ test1(buf)
                   };
                char **key = words;
                char *kp;
-               int len = endfn - buf;
+               unsigned len = endfn - buf;
 
                while ( (kp = *key) != 0 )
                   {    if ( strlen(kp) == len && !strncmp(kp, buf, len) )
@@ -443,14 +504,16 @@ test1(buf)
               int len;
               /*
                * Check for identifier1(identifier2) and not
-               * identifier1(void).
+               * identifier1(void), or identifier1(identifier2, xxxx).
                */
 
               while ( isidchar(*p) )
                   p++;
               len = p - id;
               p = skipspace(p, 1);
-              if ( *p == ')' && (len != 4 || strncmp(id, "void", 4)) )
+              if (*p == ',' ||
+                  (*p == ')' && (len != 4 || strncmp(id, "void", 4)))
+                  )
                   return 0;    /* not a function */
           }
        /*
@@ -477,7 +540,7 @@ convert1(buf, out, header, convert_varargs)
     int header;                        /* Boolean */
     int convert_varargs;       /* Boolean */
 {      char *endfn;
-       register char *p;
+       char *p;
        /*
         * The breaks table contains pointers to the beginning and end
         * of each argument.
@@ -495,7 +558,7 @@ convert1(buf, out, header, convert_varargs)
          ;
 top:   p = endfn;
        breaks = (char **)malloc(sizeof(char *) * num_breaks * 2);
-       if ( breaks == 0 )
+       if ( breaks == NULL )
           {    /* Couldn't allocate break table, give up */
                fprintf(stderr, "Unable to allocate break table!\n");
                fputs(buf, out);
@@ -507,7 +570,7 @@ top:        p = endfn;
        do
           {    int level = 0;
                char *lp = NULL;
-               char *rp;
+               char *rp = NULL;
                char *end = NULL;
 
                if ( bp >= btop )
@@ -545,7 +608,7 @@ top:        p = endfn;
                           }
                   }
                /* Erase any embedded prototype parameters. */
-               if ( lp )
+               if ( lp && rp )
                  writeblanks(lp + 1, rp);
                p--;                    /* back up over terminator */
                /* Find the name being declared. */