OSDN Git Service

2008-04-25 Kai Tietz <kai.tietz@onevision.com>
[pf3gnuchains/gcc-fork.git] / fixincludes / fixincl.c
index b361146..489f145 100644 (file)
@@ -14,16 +14,20 @@ any later version.
 GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU General Public License for more de\atails.
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to
 
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 #include "fixlib.h"
 
 
 #include "fixlib.h"
 
+#include <fnmatch.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
+#ifndef SEPARATE_FIX_PROC
+#include <sys/wait.h>
+#endif
 
 #if defined( HAVE_MMAP_FILE )
 #include <sys/mman.h>
 
 #if defined( HAVE_MMAP_FILE )
 #include <sys/mman.h>
@@ -356,102 +360,42 @@ load_file ( const char* fname )
 
 static int
 machine_matches( tFixDesc* p_fixd )
 
 static int
 machine_matches( tFixDesc* p_fixd )
-        {
-# ifndef SEPARATE_FIX_PROC
-          tSCC case_fmt[] = "case %s in\n";     /*  9 bytes, plus string */
-          tSCC esac_fmt[] =
-               " )\n    echo %s ;;\n* ) echo %s ;;\nesac";/*  4 bytes */
-          tSCC skip[] = "skip";                 /*  4 bytes */
-          tSCC run[] = "run";                   /*  3 bytes */
-          /* total bytes to add to machine sum:    49 - see fixincl.tpl */
-
-          const char **papz_machs = p_fixd->papz_machs;
-          char *pz;
-          const char *pz_sep = "";
-          tCC *pz_if_true;
-          tCC *pz_if_false;
-          char cmd_buf[ MACH_LIST_SIZE_LIMIT ]; /* size lim from fixincl.tpl */
-
-          /* Start the case statement */
-
-          sprintf (cmd_buf, case_fmt, pz_machine);
-          pz = cmd_buf + strlen (cmd_buf);
-
-          /*  Determine if a match means to apply the fix or not apply it */
-
-          if (p_fixd->fd_flags & FD_MACH_IFNOT)
-            {
-              pz_if_true  = skip;
-              pz_if_false = run;
-            }
-          else
-            {
-              pz_if_true  = run;
-              pz_if_false = skip;
-            }
-
-          /*  Emit all the machine names.  If there are more than one,
-              then we will insert " | \\\n" between the names  */
-
-          for (;;)
-            {
-              const char* pz_mach = *(papz_machs++);
-
-              if (pz_mach == (const char*) NULL)
-                break;
-              sprintf (pz, "%s%s", pz_sep, pz_mach);
-              pz += strlen (pz);
-              pz_sep = " | \\\n";
-            }
-
-          /* Now emit the match and not-match actions and the esac */
-
-          sprintf (pz, esac_fmt, pz_if_true, pz_if_false);
-
-          /*  Run the script.
-              The result will start either with 's' or 'r'.  */
-
-          {
-            int skip;
-            pz = run_shell (cmd_buf);
-            skip = (*pz == 's');
-            free ( (void*)pz );
-            if (skip)
-              {
-                p_fixd->fd_flags |= FD_SKIP_TEST;
-               return BOOL_FALSE;
-             }
-         }
+{
+  char const ** papz_machs = p_fixd->papz_machs;
+  int have_match = BOOL_FALSE;
 
 
-  return BOOL_TRUE;
-# else /* is SEPARATE_FIX_PROC */
-  const char **papz_machs = p_fixd->papz_machs;
-  int invert = (p_fixd->fd_flags & FD_MACH_IFNOT) != 0;
   for (;;)
     {
   for (;;)
     {
-      const char* pz_mach = *(papz_machs++);
-
-      if (pz_mach == (const char*) NULL)
+      char const * pz_mpat = *(papz_machs++);
+      if (pz_mpat == NULL)
         break;
         break;
-      if (strstr (pz_mach, "dos") != NULL && !invert)
-       return BOOL_TRUE;
+      if (fnmatch(pz_mpat, pz_machine, 0) == 0)
+        {
+          have_match = BOOL_TRUE;
+          break;
+        }
     }
 
     }
 
-  p_fixd->fd_flags |= FD_SKIP_TEST;
-  return BOOL_FALSE;
-# endif
+  /* Check for sense inversion then set the "skip test" flag, if needed */
+  if (p_fixd->fd_flags & FD_MACH_IFNOT)
+    have_match = ! have_match;
+
+  if (! have_match)
+    p_fixd->fd_flags |= FD_SKIP_TEST;
+
+  return have_match;
 }
 
 /* * * * * * * * * * * * *
 }
 
 /* * * * * * * * * * * * *
-
-   run_compiles   run all the regexp compiles for all the fixes once.
  */
+ *
*  run_compiles   run all the regexp compiles for all the fixes once.
+ */
 void
 run_compiles (void)
 {
   tFixDesc *p_fixd = fixDescList;
   int fix_ct = FIX_COUNT;
 void
 run_compiles (void)
 {
   tFixDesc *p_fixd = fixDescList;
   int fix_ct = FIX_COUNT;
-  regex_t *p_re = xcalloc (REGEX_COUNT, sizeof (regex_t));
+  regex_t *p_re = XCNEWVEC (regex_t, REGEX_COUNT);
 
   /*  Make sure compile_re does not stumble across invalid data */
 
 
   /*  Make sure compile_re does not stumble across invalid data */
 
@@ -866,7 +810,7 @@ fix_with_system (tFixDesc* p_fixd,
               + strlen (pz_temp_file);
 
       /* Allocate something sure to be big enough for our purposes */
               + strlen (pz_temp_file);
 
       /* Allocate something sure to be big enough for our purposes */
-      pz_cmd = xmalloc (argsize);
+      pz_cmd = XNEWVEC (char, argsize);
       strcpy (pz_cmd, pz_orig_dir);
       pz_scan = pz_cmd + strlen (pz_orig_dir);
 
       strcpy (pz_cmd, pz_orig_dir);
       pz_scan = pz_cmd + strlen (pz_orig_dir);
 
@@ -885,7 +829,7 @@ fix_with_system (tFixDesc* p_fixd,
       /*
        *  Now add the fix number and file names that may be needed
        */
       /*
        *  Now add the fix number and file names that may be needed
        */
-      sprintf (pz_scan, " %ld \'%s\' \'%s\' \'%s\'", p_fixd - fixDescList,
+      sprintf (pz_scan, " %ld '%s' '%s'",  (long) (p_fixd - fixDescList),
               pz_fix_file, pz_file_source, pz_temp_file);
     }
   else /* NOT an "internal" fix: */
               pz_fix_file, pz_file_source, pz_temp_file);
     }
   else /* NOT an "internal" fix: */
@@ -904,7 +848,7 @@ fix_with_system (tFixDesc* p_fixd,
          the following bizarre use of 'cat' only works on DOS boxes.
          It causes the file to be dropped into a temporary file for
          'cat' to read (pipes do not work on DOS).  */
          the following bizarre use of 'cat' only works on DOS boxes.
          It causes the file to be dropped into a temporary file for
          'cat' to read (pipes do not work on DOS).  */
-      tSCC   z_cmd_fmt[] = " \'%s\' | cat > \'%s\'";
+      tSCC   z_cmd_fmt[] = " '%s' | cat > '%s'";
 #else
       /* Don't use positional formatting arguments because some lame-o
          implementations cannot cope  :-(.  */
 #else
       /* Don't use positional formatting arguments because some lame-o
          implementations cannot cope  :-(.  */
@@ -933,7 +877,7 @@ fix_with_system (tFixDesc* p_fixd,
         }
 
       /* Estimated buffer size we will need.  */
         }
 
       /* Estimated buffer size we will need.  */
-      pz_scan = pz_cmd = xmalloc (argsize);
+      pz_scan = pz_cmd = XNEWVEC (char, argsize);
       /* How much of it do we allot to the program name and its
          arguments.  */
       parg_size = argsize - parg_size;
       /* How much of it do we allot to the program name and its
          arguments.  */
       parg_size = argsize - parg_size;
@@ -1020,7 +964,7 @@ start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
   else
     {
       tSCC z_cmd_fmt[] = "file='%s'\n%s";
   else
     {
       tSCC z_cmd_fmt[] = "file='%s'\n%s";
-      pz_cmd = xmalloc (strlen (p_fixd->patch_args[2])
+      pz_cmd = XNEWVEC (char, strlen (p_fixd->patch_args[2])
                        + sizeof (z_cmd_fmt) + strlen (pz_fix_file));
       sprintf (pz_cmd, z_cmd_fmt, pz_fix_file, p_fixd->patch_args[2]);
       pz_cmd_save = p_fixd->patch_args[2];
                        + sizeof (z_cmd_fmt) + strlen (pz_fix_file));
       sprintf (pz_cmd, z_cmd_fmt, pz_fix_file, p_fixd->patch_args[2]);
       pz_cmd_save = p_fixd->patch_args[2];
@@ -1071,11 +1015,11 @@ start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
 
 
 /* * * * * * * * * * * * *
 
 
 /* * * * * * * * * * * * *
-
-   Process the potential fixes for a particular include file.
-   Input:  the original text of the file and the file's name
-   Result: none.  A new file may or may not be created.  */
-
+ *
*  Process the potential fixes for a particular include file.
*  Input:  the original text of the file and the file's name
+ *  Result: none.  A new file may or may not be created.
+ */
 static t_bool
 fix_applies (tFixDesc* p_fixd)
 {
 static t_bool
 fix_applies (tFixDesc* p_fixd)
 {
@@ -1084,7 +1028,7 @@ fix_applies (tFixDesc* p_fixd)
   int test_ct;
   tTestDesc *p_test;
 
   int test_ct;
   tTestDesc *p_test;
 
-# ifdef SEPARATE_FIX_PROC
+#ifdef SEPARATE_FIX_PROC
   /*
    *  There is only one fix that uses a shell script as of this writing.
    *  I hope to nuke it anyway, it does not apply to DOS and it would
   /*
    *  There is only one fix that uses a shell script as of this writing.
    *  I hope to nuke it anyway, it does not apply to DOS and it would
@@ -1092,35 +1036,26 @@ fix_applies (tFixDesc* p_fixd)
    */
   if (p_fixd->fd_flags & (FD_SHELL_SCRIPT | FD_SKIP_TEST))
     return BOOL_FALSE;
    */
   if (p_fixd->fd_flags & (FD_SHELL_SCRIPT | FD_SKIP_TEST))
     return BOOL_FALSE;
-# else
+#else
   if (p_fixd->fd_flags & FD_SKIP_TEST)
     return BOOL_FALSE;
   if (p_fixd->fd_flags & FD_SKIP_TEST)
     return BOOL_FALSE;
-# endif
+#endif
 
   /*  IF there is a file name restriction,
       THEN ensure the current file name matches one in the pattern  */
 
   if (pz_scan != (char *) NULL)
     {
 
   /*  IF there is a file name restriction,
       THEN ensure the current file name matches one in the pattern  */
 
   if (pz_scan != (char *) NULL)
     {
-      size_t name_len;
-
       while ((pz_fname[0] == '.') && (pz_fname[1] == '/'))
         pz_fname += 2;
       while ((pz_fname[0] == '.') && (pz_fname[1] == '/'))
         pz_fname += 2;
-      name_len = strlen (pz_fname);
 
       for (;;)
         {
 
       for (;;)
         {
-          pz_scan = strstr (pz_scan + 1, pz_fname);
-          /*  IF we can't match the string at all,
-              THEN bail  */
-          if (pz_scan == (char *) NULL)
-            return BOOL_FALSE;
-
-          /*  IF the match is surrounded by the '|' markers,
-              THEN we found a full match -- time to run the tests  */
-
-          if ((pz_scan[-1] == '|') && (pz_scan[name_len] == '|'))
+          if (fnmatch (pz_scan, pz_fname, 0) == 0)
             break;
             break;
+          pz_scan += strlen (pz_scan) + 1;
+          if (*pz_scan == NUL)
+            return BOOL_FALSE;
         }
     }
 
         }
     }
 
@@ -1199,7 +1134,10 @@ write_replacement (tFixDesc* p_fixd)
 
    {
      FILE* out_fp = create_file ();
 
    {
      FILE* out_fp = create_file ();
-     fputs (pz_text, out_fp);
+     size_t sz = strlen (pz_text);
+     fwrite (pz_text, sz, 1, out_fp);
+     if (pz_text[ sz-1 ] != '\n')
+       fputc ('\n', out_fp);
      fclose (out_fp);
    }
 }
      fclose (out_fp);
    }
 }