OSDN Git Service

2000-07-21 Alexandre Petit-Bianco <apbianco@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / fixinc / fixincl.c
index 2dc8d21..8deafa8 100644 (file)
@@ -38,10 +38,21 @@ Boston, MA 02111-1307, USA.  */
 
 static const char program_id[] = "fixincl version 1.1";
 
+/*  This format will be used at the start of every generated file */
+
+static const char z_std_preamble[] =
+"/*  DO NOT EDIT THIS FILE.\n\n\
+    It has been auto-edited by fixincludes from:\n\n\
+\t\"%s/%s\"\n\n\
+    This had to be done to correct non-standard usages in the\n\
+    original, manufacturer supplied header file.  */\n\n";
+
 /*  Working environment strings.  Essentially, invocation 'options'.  */
-char *pz_dest_dir = NULL;
-char *pz_src_dir = NULL;
-char *pz_machine = NULL;
+
+#define _ENV_(v,m,n,t)   tCC* v = NULL;
+ENV_TABLE
+#undef _ENV_
+
 int find_base_len = 0;
 
 typedef enum {
@@ -67,6 +78,7 @@ t_bool curr_data_mapped;
 int    data_map_fd;
 size_t data_map_size;
 size_t ttl_data_size = 0;
+
 #ifdef DO_STATS
 int process_ct = 0;
 int apply_ct = 0;
@@ -74,14 +86,6 @@ int fixed_ct = 0;
 int altered_ct = 0;
 #endif /* DO_STATS */
 
-#ifdef HAVE_MMAP_FILE
-#define UNLOAD_DATA() do { if (curr_data_mapped) { \
-  munmap ((void*)pz_curr_data, data_map_size); close (data_map_fd); } \
-  else free ((void*)pz_curr_data); } while(0)
-#else
-#define UNLOAD_DATA() free ((void*)pz_curr_data)
-#endif
-
 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;
@@ -203,8 +207,16 @@ initialize ( argc, argv )
   char** argv;
 {
   static const char var_not_found[] =
-    "fixincl ERROR:  %s environment variable not defined\n\
-\tTARGET_MACHINE, DESTDIR, SRCDIR and FIND_BASE are required\n";
+    "fixincl ERROR:  %s environment variable not defined\n"
+#ifdef __STDC__
+    "each of these must be defined:\n"
+#define _ENV_(v,m,n,t) "\t" n "  - " t "\n"
+ENV_TABLE
+#undef _ENV_
+#endif
+    ;
+
+  xmalloc_set_program_name (argv[0]);
 
   switch (argc)
     {
@@ -227,85 +239,48 @@ initialize ( argc, argv )
       exit (EXIT_FAILURE);
     }
 
-  {
-    static const char var[] = "TARGET_MACHINE";
-    pz_machine = getenv (var);
-    if (pz_machine == (char *) NULL)
-      {
-        fprintf (stderr, var_not_found, var);
-        exit (EXIT_FAILURE);
-      }
-  }
+#define _ENV_(v,m,n,t)   { tSCC var[] = n;  \
+  v = getenv (var); if (m && (v == NULL)) { \
+  fprintf (stderr, var_not_found, var);     \
+  exit (EXIT_FAILURE); } }
 
-  {
-    static const char var[] = "DESTDIR";
-    pz_dest_dir = getenv (var);
-    if (pz_dest_dir == (char *) NULL)
-      {
-        fprintf (stderr, var_not_found, var);
-        exit (EXIT_FAILURE);
-      }
-  }
+ENV_TABLE
 
-  {
-    static const char var[] = "SRCDIR";
-    pz_src_dir = getenv (var);
-    if (pz_src_dir == (char *) NULL)
-      {
-        fprintf (stderr, var_not_found, var);
-        exit (EXIT_FAILURE);
-      }
-  }
+#undef _ENV_
 
-  {
-    static const char var[] = "VERBOSE";
-    char* pz = getenv (var);
-    if (pz != (char *) NULL)
-      {
-        if (isdigit( *pz ))
-          verbose_level = (te_verbose)atoi( pz );
-        else
-          switch (*pz) {
-          case 's':
-          case 'S':
-            verbose_level = VERB_SILENT;     break;
-
-          case 'f':
-          case 'F':
-            verbose_level = VERB_FIXES;      break;
-
-          case 'a':
-          case 'A':
-            verbose_level = VERB_APPLIES;    break;
-
-          case 'p':
-          case 'P':
-            verbose_level = VERB_PROGRESS;   break;
-
-          case 't':
-          case 'T':
-            verbose_level = VERB_TESTS;      break;
-
-          case 'e':
-          case 'E':
-            verbose_level = VERB_EVERYTHING; break;
-          }
-      }
-  }
+  if (isdigit( *pz_verbose ))
+    verbose_level = (te_verbose)atoi( pz_verbose );
+  else
+    switch (*pz_verbose) {
+    case 's':
+    case 'S':
+      verbose_level = VERB_SILENT;     break;
+
+    case 'f':
+    case 'F':
+      verbose_level = VERB_FIXES;      break;
+
+    case 'a':
+    case 'A':
+      verbose_level = VERB_APPLIES;    break;
+
+    case 'p':
+    case 'P':
+      verbose_level = VERB_PROGRESS;   break;
+
+    case 't':
+    case 'T':
+      verbose_level = VERB_TESTS;      break;
+
+    case 'e':
+    case 'E':
+      verbose_level = VERB_EVERYTHING; break;
+    }
 
-  {
-    static const char var[] = "FIND_BASE";
-    char *pz = getenv (var);
-    if (pz == (char *) NULL)
-      {
-        fprintf (stderr, var_not_found, var);
-        exit (EXIT_FAILURE);
-      }
-    while ((pz[0] == '.') && (pz[1] == '/'))
-      pz += 2;
-    if ((pz[0] != '.') || (pz[1] != NUL))
-      find_base_len = strlen( pz );
-  }
+ while ((pz_find_base[0] == '.') && (pz_find_base[1] == '/'))
+   pz_find_base += 2;
+ if ((pz_find_base[0] != '.') || (pz_find_base[1] != NUL))
+   find_base_len = strlen( pz_find_base );
 
   /*  Compile all the regular expressions now.
       That way, it is done only once for the whole run.
@@ -582,17 +557,16 @@ create_file ()
     fprintf (stderr, "Fixed:  %s\n", pz_curr_file);
   pf = fdopen (fd, "w");
 
-#ifdef LATER
-  {
-    static const char hdr[] =
-    "/*  DO NOT EDIT THIS FILE.\n\n"
-    "    It has been auto-edited by fixincludes from /usr/include/%s\n"
-    "    This had to be done to correct non-standard usages in the\n"
-    "    original, manufacturer supplied header file.  */\n\n";
+  /*
+   *  IF pz_machine is NULL, then we are in some sort of test mode.
+   *  Do not insert the current directory name.  Use a constant string.
+   */
+  fprintf (pf, z_std_preamble,
+           (pz_machine == NULL)
+           ? "fixinc/tests/inc"
+           : pz_input_dir,
+           pz_curr_file);
 
-    fprintf (pf, hdr, pz_curr_file);
-  }
-#endif
   return pf;
 }
 
@@ -618,14 +592,27 @@ else echo FALSE\n\
 fi";
 
   char *pz_res;
-  int res = SKIP_FIX;
+  int res;
 
   static char cmd_buf[4096];
 
   sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
   pz_res = run_shell (cmd_buf);
-  if (*pz_res == 'T')
+
+  switch (*pz_res) {
+  case 'T':
     res = APPLY_FIX;
+    break;
+
+  case 'F':
+    res = SKIP_FIX;
+    break;
+
+  default:
+    fprintf (stderr, "Script yielded bogus result of `%s':\n%s\n\n",
+             pz_res, cmd_buf );
+  }
+
   free ((void *) pz_res);
   return res;
 }
@@ -938,7 +925,8 @@ fix_applies (p_fixd)
   tFixDesc *p_fixd;
 {
 #ifdef DEBUG
-  static const char z_failed[] = "not applying %s to %s - test %d failed\n";
+  static const char z_failed[] = "not applying %s %s to %s - \
+test %d failed\n";
 #endif
   const char *pz_fname = pz_curr_file;
   const char *pz_scan = p_fixd->file_list;
@@ -994,8 +982,8 @@ fix_applies (p_fixd)
           if (test_test (p_test, pz_curr_file) != APPLY_FIX) {
 #ifdef DEBUG
             if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, p_fixd->fix_name, pz_fname,
-                       p_fixd->test_ct - test_ct);
+              fprintf (stderr, z_failed, "TEST", p_fixd->fix_name,
+                       pz_fname, p_fixd->test_ct - test_ct);
 #endif
             return BOOL_FALSE;
           }
@@ -1005,8 +993,8 @@ fix_applies (p_fixd)
           if (egrep_test (pz_curr_data, p_test) != APPLY_FIX) {
 #ifdef DEBUG
             if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, p_fixd->fix_name, pz_fname,
-                       p_fixd->test_ct - test_ct);
+              fprintf (stderr, z_failed, "EGREP", p_fixd->fix_name,
+                       pz_fname, p_fixd->test_ct - test_ct);
 #endif
             return BOOL_FALSE;
           }
@@ -1016,8 +1004,8 @@ fix_applies (p_fixd)
           if (egrep_test (pz_curr_data, p_test) == APPLY_FIX) {
 #ifdef DEBUG
             if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, p_fixd->fix_name, pz_fname,
-                       p_fixd->test_ct - test_ct);
+              fprintf (stderr, z_failed, "NEGREP", p_fixd->fix_name,
+                       pz_fname, p_fixd->test_ct - test_ct);
 #endif
             /*  Negated sense  */
             return BOOL_FALSE;
@@ -1029,8 +1017,8 @@ fix_applies (p_fixd)
               != APPLY_FIX) {
 #ifdef DEBUG
             if (VLEVEL( VERB_EVERYTHING ))
-              fprintf (stderr, z_failed, p_fixd->fix_name, pz_fname,
-                       p_fixd->test_ct - test_ct);
+              fprintf (stderr, z_failed, "FTEST", p_fixd->fix_name,
+                       pz_fname, p_fixd->test_ct - test_ct);
 #endif
             return BOOL_FALSE;
           }