arc: Refurbish adc/sbc patterns
[gcc.git] / fixincludes / fixincl.c
index 39b389d6775c969cb22146255c2e24749485ef58..6dba2f6e830a9b9827a799984e68bb81e108fba6 100644 (file)
@@ -2,24 +2,24 @@
    files which are fixed to work correctly with ANSI C and placed in a
    directory that GCC will search.
 
-   Copyright (C) 1997, 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
+   Copyright (C) 1997, 1998, 1999, 2000, 2004, 2009, 2012
+   Free Software Foundation, Inc.
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 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
-GNU General Public License for more de\atails.
+GNU General Public License for more details.
 
 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, 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "fixlib.h"
 
@@ -53,22 +53,8 @@ static const char z_std_preamble[] =
     original, manufacturer supplied header file.  */\n\n";
 
 int find_base_len = 0;
-
-typedef enum {
-  VERB_SILENT = 0,
-  VERB_FIXES,
-  VERB_APPLIES,
-  VERB_PROGRESS,
-  VERB_TESTS,
-  VERB_EVERYTHING
-} te_verbose;
-
-te_verbose  verbose_level = VERB_PROGRESS;
 int have_tty = 0;
 
-#define VLEVEL(l)  ((unsigned int) verbose_level >= (unsigned int) l)
-#define NOT_SILENT VLEVEL(VERB_FIXES)
-
 pid_t process_chain_head = (pid_t) -1;
 
 char*  pz_curr_file;  /*  name of the current file under test/fix  */
@@ -88,9 +74,12 @@ int altered_ct = 0;
 #endif /* DO_STATS */
 
 const char incl_quote_pat[] = "^[ \t]*#[ \t]*include[ \t]*\"[^/]";
-tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
 regex_t incl_quote_re;
 
+#ifndef SEPARATE_FIX_PROC
+tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
+#endif
+
 static void do_version (void) ATTRIBUTE_NORETURN;
 char *load_file (const char *);
 void run_compiles (void);
@@ -202,7 +191,7 @@ do_version (void)
   puts (zBuf + 5);
   exit (strcmp (run_shell (zBuf), program_id));
 #else
-  exit (system (zBuf));
+  exit (system_with_shell (zBuf));
 #endif
 }
 
@@ -289,6 +278,11 @@ initialize ( int argc, char** argv )
   /* NULL as the first argument to `tempnam' causes it to DTRT
      wrt the temporary directory where the file will be created.  */
   pz_temp_file = tempnam( NULL, "fxinc" );
+
+#if defined(__MINGW32__)
+  fix_path_separators (pz_temp_file);
+#endif
+
 # endif
 
   signal (SIGQUIT, SIG_IGN);
@@ -412,8 +406,17 @@ run_compiles (void)
   /* FOR every fixup, ...  */
   do
     {
-      tTestDesc *p_test = p_fixd->p_test_desc;
-      int test_ct = p_fixd->test_ct;
+      tTestDesc *p_test;
+      int test_ct;
+
+      if (fixinc_mode && (p_fixd->fd_flags & FD_REPLACEMENT))
+        {
+          p_fixd->fd_flags |= FD_SKIP_TEST;
+          continue;
+        }
+
+      p_test = p_fixd->p_test_desc;
+      test_ct = p_fixd->test_ct;
 
       /*  IF the machine type pointer is not NULL (we are not in test mode)
              AND this test is for or not done on particular machines
@@ -571,6 +574,26 @@ fi";
   free ((void *) pz_res);
   return res;
 }
+#elif defined(__MINGW32__) || defined(__DJGPP__)
+static int
+test_test (tTestDesc* p_test, char* pz_test_file)
+{
+  tSCC cmd_fmt[] =
+#if defined(__DJGPP__)
+    "file=%s; test %s >/dev/null 2>/dev/null";
+#else
+    "file=%s; test %s > /dev/null 2>&1";
+#endif
+  int res;
+
+  char *cmd_buf = XNEWVEC (char, strlen(cmd_fmt) + strlen(pz_test_file) + strlen(p_test->pz_test_text));
+
+  sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
+  res = system_with_shell (cmd_buf);
+
+  free (cmd_buf);
+  return res ? SKIP_FIX : APPLY_FIX;
+}
 #else
 /*
  *  IF we are in MS-DOS land, then whatever shell-type test is required
@@ -602,6 +625,63 @@ egrep_test (char* pz_data, tTestDesc* p_test)
   return SKIP_FIX;
 }
 
+/* * * * * * * * * * * * *
+
+  cksum_test   check the sum of the candidate file
+  Input:  the original file contents and the file name
+  Result: APPLY_FIX if the check sum matches, SKIP_FIX otherwise
+
+  The caller may choose to reverse meaning if the sense of the test
+  is inverted.  */
+
+static int
+cksum_test (char * pz_data, tTestDesc * p_test, char * fname)
+{
+  unsigned int cksum;
+
+  /*
+   * Testing is off in normal operation mode.
+   * So, in testing mode, APPLY_FIX is always returned.
+   */
+  if (fixinc_mode != TESTING_OFF)
+    return APPLY_FIX;
+
+  {
+    char * fnm = strrchr(fname, '/');
+    if (fnm != NULL)
+      fname = fnm + 1;
+    
+    errno = 0;
+    cksum = (unsigned int)strtoul(p_test->pz_test_text, &fnm, 10);
+    if (errno != 0)
+      return SKIP_FIX;
+
+    if (! ISSPACE(*fnm++))
+      return SKIP_FIX;
+    while (ISSPACE(*fnm)) fnm++;
+
+    if (! ISDIGIT(*fnm++))
+      return SKIP_FIX;
+    while (ISDIGIT(*fnm)) fnm++;
+
+    if (! ISSPACE(*fnm++))
+      return SKIP_FIX;
+    while (ISSPACE(*fnm)) fnm++;
+
+    if (strcmp(fnm, fname) != 0)
+      return SKIP_FIX;
+  }
+
+  {
+    unsigned int sum = 0;
+    while (*pz_data != NUL) {
+      sum = (sum >> 1) + ((sum & 1) << 15) + (unsigned)(*pz_data++);
+      sum &= 0xFFFF;
+    }
+
+    return (sum == cksum) ? APPLY_FIX : SKIP_FIX;
+  }
+}
 
 /* * * * * * * * * * * * *
 
@@ -799,7 +879,7 @@ fix_with_system (tFixDesc* p_fixd,
   if (p_fixd->fd_flags & FD_SUBROUTINE)
     {
       static const char z_applyfix_prog[] =
-       "/../fixincludes/applyfix" EXE_EXT;
+        "/../fixincludes/applyfix" EXE_EXT;
 
       struct stat buf;
       argsize = 32
@@ -817,25 +897,25 @@ fix_with_system (tFixDesc* p_fixd,
       strcpy (pz_scan, z_applyfix_prog);
 
       /* IF we can't find the "applyfix" executable file at the first guess,
-        try one level higher up  */
+         try one level higher up  */
       if (stat (pz_cmd, &buf) == -1)
-       {
-         strcpy (pz_scan, "/..");
-         strcpy (pz_scan+3, z_applyfix_prog);
-       }
+        {
+          strcpy (pz_scan, "/..");
+          strcpy (pz_scan+3, z_applyfix_prog);
+        }
 
       pz_scan += strlen (pz_scan);
 
       /*
        *  Now add the fix number and file names that may be needed
        */
-      sprintf (pz_scan, " %ld '%s' '%s'  (long) (p_fixd - fixDescList),
-              pz_fix_file, pz_file_source, pz_temp_file);
+      sprintf (pz_scan, " %ld '%s' '%s' '%s'", (long) (p_fixd - fixDescList),
+               pz_fix_file, pz_file_source, pz_temp_file);
     }
   else /* NOT an "internal" fix: */
     {
       size_t parg_size;
-#ifdef __MSDOS__
+#if defined(__MSDOS__) && !defined(__DJGPP__)
       /* Don't use the "src > dstX; rm -f dst; mv -f dstX dst" trick:
          dst is a temporary file anyway, so we know there's no other
          file by that name; and DOS's system(3) doesn't mind to
@@ -854,12 +934,18 @@ fix_with_system (tFixDesc* p_fixd,
          implementations cannot cope  :-(.  */
       tSCC   z_cmd_fmt[] = " %s > %sX ; rm -f %s; mv -f %sX %s";
 #endif
+      tSCC   z_subshell_start[] = "( ";
+      tSCC   z_subshell_end[] = " ) < ";
       tCC**  ppArgs = p_fixd->patch_args;
 
       argsize = sizeof( z_cmd_fmt ) + strlen( pz_temp_file )
               + strlen( pz_file_source );
       parg_size = argsize;
       
+      if (p_fixd->fd_flags & FD_SHELL_SCRIPT)
+        {
+          argsize += strlen( z_subshell_start ) + strlen ( z_subshell_end );
+        }
 
       /*
        *  Compute the size of the command line.  Add lotsa extra space
@@ -884,6 +970,16 @@ fix_with_system (tFixDesc* p_fixd,
 
       ppArgs = p_fixd->patch_args;
 
+      /*
+       * If it's shell script, enclose it in parentheses and skip "sh -c".
+       */
+      if (p_fixd->fd_flags & FD_SHELL_SCRIPT)
+        {
+          strcpy (pz_scan, z_subshell_start);
+          pz_scan += strlen (z_subshell_start);
+          ppArgs += 2;
+        }
+
       /*
        *  Copy the program name, unquoted
        */
@@ -904,39 +1000,48 @@ fix_with_system (tFixDesc* p_fixd,
       for (;;)
         {
           tCC*   pArg = *(ppArgs++);
-         char*  pz_scan_save;
+          char*  pz_scan_save;
           if (pArg == NULL)
             break;
           *(pz_scan++) = ' ';
           pz_scan = make_raw_shell_str( pz_scan_save = pz_scan, pArg,
-                                       parg_size - (pz_scan - pz_cmd) );
-         /*
-          *  Make sure we don't overflow the buffer due to sloppy
-          *  size estimation.
-          */
-         while (pz_scan == (char*)NULL)
-           {
-             size_t already_filled = pz_scan_save - pz_cmd;
-             pz_cmd = xrealloc (pz_cmd, argsize += 100);
-             pz_scan_save = pz_scan = pz_cmd + already_filled;
-             parg_size += 100;
-             pz_scan = make_raw_shell_str( pz_scan, pArg,
-                                           parg_size - (pz_scan - pz_cmd) );
-           }
+                                        parg_size - (pz_scan - pz_cmd) );
+          /*
+           *  Make sure we don't overflow the buffer due to sloppy
+           *  size estimation.
+           */
+          while (pz_scan == (char*)NULL)
+            {
+              size_t already_filled = pz_scan_save - pz_cmd;
+              pz_cmd = xrealloc (pz_cmd, argsize += 100);
+              pz_scan_save = pz_scan = pz_cmd + already_filled;
+              parg_size += 100;
+              pz_scan = make_raw_shell_str( pz_scan, pArg,
+                                            parg_size - (pz_scan - pz_cmd) );
+            }
+        }
+
+      /*
+       * Close parenthesis if it's shell script.
+       */
+      if (p_fixd->fd_flags & FD_SHELL_SCRIPT)
+        {
+          strcpy (pz_scan, z_subshell_end);
+          pz_scan += strlen (z_subshell_end);
         }
 
       /*
        *  add the file machinations.
        */
-#ifdef __MSDOS__
+#if defined(__MSDOS__) && !defined(__DJGPP__)
       sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file );
 #else
       sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file,
                pz_temp_file, pz_temp_file, pz_temp_file);
 #endif
     }
-  system( pz_cmd );
-  free( (void*)pz_cmd );
+  system_with_shell (pz_cmd);
+  free (pz_cmd);
 }
 
 /* * * * * * * * * * * * *
@@ -965,7 +1070,7 @@ start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
     {
       tSCC z_cmd_fmt[] = "file='%s'\n%s";
       pz_cmd = XNEWVEC (char, strlen (p_fixd->patch_args[2])
-                       + sizeof (z_cmd_fmt) + strlen (pz_fix_file));
+                        + 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];
       p_fixd->patch_args[2] = pz_cmd;
@@ -1012,7 +1117,15 @@ start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
   return read_fd;
 }
 #endif
-
+#ifdef DEBUG
+# define NOTE_SKIP(_ttyp)  do {                                         \
+            if (VLEVEL( VERB_EVERYTHING ))                              \
+              fprintf (stderr, z_failed, _ttyp, p_fixd->fix_name,       \
+                       pz_fname, p_fixd->test_ct - test_ct);            \
+          } while (0)
+#else
+# define NOTE_SKIP(_ttyp)
+#endif
 
 /* * * * * * * * * * * * *
  *
@@ -1027,8 +1140,10 @@ fix_applies (tFixDesc* p_fixd)
   const char *pz_scan = p_fixd->file_list;
   int test_ct;
   tTestDesc *p_test;
+  t_bool saw_sum_test   = BOOL_FALSE;
+  t_bool one_sum_passed = BOOL_FALSE;
 
-#ifdef SEPARATE_FIX_PROC
+#if defined(__MSDOS__) && !defined(__DJGPP__)
   /*
    *  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
@@ -1060,6 +1175,7 @@ fix_applies (tFixDesc* p_fixd)
     }
 
   /*  FOR each test, see if it fails.
+      "sum" fails only if all "sum" tests fail.
       IF it does fail, then we go on to the next test */
 
   for (p_test = p_fixd->p_test_desc, test_ct = p_fixd->test_ct;
@@ -1070,52 +1186,51 @@ fix_applies (tFixDesc* p_fixd)
         {
         case TT_TEST:
           if (test_test (p_test, pz_curr_file) != APPLY_FIX) {
-#ifdef DEBUG
-            if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, "TEST", p_fixd->fix_name,
-                       pz_fname, p_fixd->test_ct - test_ct);
-#endif
+            NOTE_SKIP("TEST");
             return BOOL_FALSE;
           }
           break;
 
         case TT_EGREP:
           if (egrep_test (pz_curr_data, p_test) != APPLY_FIX) {
-#ifdef DEBUG
-            if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, "EGREP", p_fixd->fix_name,
-                       pz_fname, p_fixd->test_ct - test_ct);
-#endif
+            NOTE_SKIP("EGREP");
             return BOOL_FALSE;
           }
           break;
 
         case TT_NEGREP:
           if (egrep_test (pz_curr_data, p_test) == APPLY_FIX) {
-#ifdef DEBUG
-            if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, "NEGREP", p_fixd->fix_name,
-                       pz_fname, p_fixd->test_ct - test_ct);
-#endif
+            NOTE_SKIP("NEGREP");
             /*  Negated sense  */
             return BOOL_FALSE;
           }
           break;
 
+        case TT_CKSUM:
+         if (one_sum_passed)
+           break; /*  No need to check any more  */
+
+          saw_sum_test = BOOL_TRUE;
+          if (cksum_test (pz_curr_data, p_test, pz_curr_file) != APPLY_FIX) {
+            NOTE_SKIP("CKSUM");
+          } else {
+            one_sum_passed = BOOL_TRUE;
+          }
+          break;
+
         case TT_FUNCTION:
           if (run_test (p_test->pz_test_text, pz_curr_file, pz_curr_data)
               != APPLY_FIX) {
-#ifdef DEBUG
-            if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, "FTEST", p_fixd->fix_name,
-                       pz_fname, p_fixd->test_ct - test_ct);
-#endif
+            NOTE_SKIP("FTEST");
             return BOOL_FALSE;
           }
           break;
         }
     }
 
+  if (saw_sum_test)
+    return one_sum_passed;
+
   return BOOL_TRUE;
 }
 
@@ -1190,7 +1305,7 @@ test_for_changes (int read_fd)
           /*  IF there are matched data, write the matched part now. */
           if ((char*)pz_cmp != pz_curr_data)
             fwrite (pz_curr_data, (size_t)((char*)pz_cmp - pz_curr_data),
-                                       1, out_fp);
+                    1, out_fp);
 
           /*  Emit the current unmatching character */
           putc (ch, out_fp);
@@ -1253,7 +1368,7 @@ process (void)
 #endif
   if (VLEVEL( VERB_PROGRESS ) && have_tty)
     fprintf (stderr, "%6lu %-50s   \r",
-            (unsigned long) data_map_size, pz_curr_file);
+             (unsigned long) data_map_size, pz_curr_file);
 
 # ifndef SEPARATE_FIX_PROC
   process_chain_head = NOPROCESS;