OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [fixincludes/] [fixincl.c] - Diff between revs 154 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 154 Rev 816
/* Install modified versions of certain ANSI-incompatible system header
/* Install modified versions of certain ANSI-incompatible system header
   files which are fixed to work correctly with ANSI C and placed in a
   files which are fixed to work correctly with ANSI C and placed in a
   directory that GCC will search.
   directory that GCC will search.
 
 
   Copyright (C) 1997, 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
   Copyright (C) 1997, 1998, 1999, 2000, 2004 Free Software Foundation, Inc.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
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 2, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful,
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Boston, MA 02110-1301, USA.  */
 
 
#include "fixlib.h"
#include "fixlib.h"
 
 
#include <sys/stat.h>
#include <sys/stat.h>
#ifndef SEPARATE_FIX_PROC
#ifndef SEPARATE_FIX_PROC
#include <sys/wait.h>
#include <sys/wait.h>
#endif
#endif
 
 
#if defined( HAVE_MMAP_FILE )
#if defined( HAVE_MMAP_FILE )
#include <sys/mman.h>
#include <sys/mman.h>
#define  BAD_ADDR ((void*)-1)
#define  BAD_ADDR ((void*)-1)
#endif
#endif
 
 
#ifndef SEPARATE_FIX_PROC
#ifndef SEPARATE_FIX_PROC
#include "server.h"
#include "server.h"
#endif
#endif
 
 
/*  The contents of this string are not very important.  It is mostly
/*  The contents of this string are not very important.  It is mostly
    just used as part of the "I am alive and working" test.  */
    just used as part of the "I am alive and working" test.  */
 
 
static const char program_id[] = "fixincl version 1.1";
static const char program_id[] = "fixincl version 1.1";
 
 
/*  This format will be used at the start of every generated file */
/*  This format will be used at the start of every generated file */
 
 
static const char z_std_preamble[] =
static const char z_std_preamble[] =
"/*  DO NOT EDIT THIS FILE.\n\n\
"/*  DO NOT EDIT THIS FILE.\n\n\
    It has been auto-edited by fixincludes from:\n\n\
    It has been auto-edited by fixincludes from:\n\n\
\t\"%s/%s\"\n\n\
\t\"%s/%s\"\n\n\
    This had to be done to correct non-standard usages in the\n\
    This had to be done to correct non-standard usages in the\n\
    original, manufacturer supplied header file.  */\n\n";
    original, manufacturer supplied header file.  */\n\n";
 
 
int find_base_len = 0;
int find_base_len = 0;
 
 
typedef enum {
typedef enum {
  VERB_SILENT = 0,
  VERB_SILENT = 0,
  VERB_FIXES,
  VERB_FIXES,
  VERB_APPLIES,
  VERB_APPLIES,
  VERB_PROGRESS,
  VERB_PROGRESS,
  VERB_TESTS,
  VERB_TESTS,
  VERB_EVERYTHING
  VERB_EVERYTHING
} te_verbose;
} te_verbose;
 
 
te_verbose  verbose_level = VERB_PROGRESS;
te_verbose  verbose_level = VERB_PROGRESS;
int have_tty = 0;
int have_tty = 0;
 
 
#define VLEVEL(l)  ((unsigned int) verbose_level >= (unsigned int) l)
#define VLEVEL(l)  ((unsigned int) verbose_level >= (unsigned int) l)
#define NOT_SILENT VLEVEL(VERB_FIXES)
#define NOT_SILENT VLEVEL(VERB_FIXES)
 
 
pid_t process_chain_head = (pid_t) -1;
pid_t process_chain_head = (pid_t) -1;
 
 
char*  pz_curr_file;  /*  name of the current file under test/fix  */
char*  pz_curr_file;  /*  name of the current file under test/fix  */
char*  pz_curr_data;  /*  original contents of that file  */
char*  pz_curr_data;  /*  original contents of that file  */
char*  pz_temp_file;  /*  for DOS, a place to stash the temporary
char*  pz_temp_file;  /*  for DOS, a place to stash the temporary
                          fixed data between system(3) calls  */
                          fixed data between system(3) calls  */
t_bool curr_data_mapped;
t_bool curr_data_mapped;
int    data_map_fd;
int    data_map_fd;
size_t data_map_size;
size_t data_map_size;
size_t ttl_data_size = 0;
size_t ttl_data_size = 0;
 
 
#ifdef DO_STATS
#ifdef DO_STATS
int process_ct = 0;
int process_ct = 0;
int apply_ct = 0;
int apply_ct = 0;
int fixed_ct = 0;
int fixed_ct = 0;
int altered_ct = 0;
int altered_ct = 0;
#endif /* DO_STATS */
#endif /* DO_STATS */
 
 
const char incl_quote_pat[] = "^[ \t]*#[ \t]*include[ \t]*\"[^/]";
const char incl_quote_pat[] = "^[ \t]*#[ \t]*include[ \t]*\"[^/]";
tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
tSCC z_fork_err[] = "Error %d (%s) starting filter process for %s\n";
regex_t incl_quote_re;
regex_t incl_quote_re;
 
 
static void do_version (void) ATTRIBUTE_NORETURN;
static void do_version (void) ATTRIBUTE_NORETURN;
char *load_file (const char *);
char *load_file (const char *);
void run_compiles (void);
void run_compiles (void);
void initialize (int argc, char** argv);
void initialize (int argc, char** argv);
void process (void);
void process (void);
 
 
/*  External Source Code */
/*  External Source Code */
 
 
#include "fixincl.x"
#include "fixincl.x"
 
 
/* * * * * * * * * * * * * * * * * * *
/* * * * * * * * * * * * * * * * * * *
 *
 *
 *  MAIN ROUTINE
 *  MAIN ROUTINE
 */
 */
extern int main (int, char **);
extern int main (int, char **);
int
int
main (int argc, char** argv)
main (int argc, char** argv)
{
{
  char *file_name_buf;
  char *file_name_buf;
 
 
  initialize ( argc, argv );
  initialize ( argc, argv );
 
 
  have_tty = isatty (fileno (stderr));
  have_tty = isatty (fileno (stderr));
 
 
  /* Before anything else, ensure we can allocate our file name buffer. */
  /* Before anything else, ensure we can allocate our file name buffer. */
  file_name_buf = load_file_data (stdin);
  file_name_buf = load_file_data (stdin);
 
 
  /*  Because of the way server shells work, you have to keep stdin, out
  /*  Because of the way server shells work, you have to keep stdin, out
      and err open so that the proper input file does not get closed
      and err open so that the proper input file does not get closed
      by accident  */
      by accident  */
 
 
  freopen ("/dev/null", "r", stdin);
  freopen ("/dev/null", "r", stdin);
 
 
  if (file_name_buf == (char *) NULL)
  if (file_name_buf == (char *) NULL)
    {
    {
      fputs ("No file names listed for fixing\n", stderr);
      fputs ("No file names listed for fixing\n", stderr);
      exit (EXIT_FAILURE);
      exit (EXIT_FAILURE);
    }
    }
 
 
  for (;;)
  for (;;)
    {
    {
      char* pz_end;
      char* pz_end;
 
 
      /*  skip to start of name, past any "./" prefixes */
      /*  skip to start of name, past any "./" prefixes */
 
 
      while (ISSPACE (*file_name_buf))  file_name_buf++;
      while (ISSPACE (*file_name_buf))  file_name_buf++;
      while ((file_name_buf[0] == '.') && (file_name_buf[1] == '/'))
      while ((file_name_buf[0] == '.') && (file_name_buf[1] == '/'))
        file_name_buf += 2;
        file_name_buf += 2;
 
 
      /*  Check for end of list  */
      /*  Check for end of list  */
 
 
      if (*file_name_buf == NUL)
      if (*file_name_buf == NUL)
        break;
        break;
 
 
      /*  Set global file name pointer and find end of name */
      /*  Set global file name pointer and find end of name */
 
 
      pz_curr_file = file_name_buf;
      pz_curr_file = file_name_buf;
      pz_end = strchr( pz_curr_file, '\n' );
      pz_end = strchr( pz_curr_file, '\n' );
      if (pz_end == (char*)NULL)
      if (pz_end == (char*)NULL)
        pz_end = file_name_buf = pz_curr_file + strlen (pz_curr_file);
        pz_end = file_name_buf = pz_curr_file + strlen (pz_curr_file);
      else
      else
        file_name_buf = pz_end + 1;
        file_name_buf = pz_end + 1;
 
 
      while ((pz_end > pz_curr_file) && ISSPACE( pz_end[-1]))  pz_end--;
      while ((pz_end > pz_curr_file) && ISSPACE( pz_end[-1]))  pz_end--;
 
 
      /*  IF no name is found (blank line) or comment marker, skip line  */
      /*  IF no name is found (blank line) or comment marker, skip line  */
 
 
      if ((pz_curr_file == pz_end) || (*pz_curr_file == '#'))
      if ((pz_curr_file == pz_end) || (*pz_curr_file == '#'))
        continue;
        continue;
      *pz_end = NUL;
      *pz_end = NUL;
 
 
      process ();
      process ();
    } /*  for (;;) */
    } /*  for (;;) */
 
 
#ifdef DO_STATS
#ifdef DO_STATS
  if (VLEVEL( VERB_PROGRESS )) {
  if (VLEVEL( VERB_PROGRESS )) {
    tSCC zFmt[] =
    tSCC zFmt[] =
      "\
      "\
Processed %5d files containing %d bytes    \n\
Processed %5d files containing %d bytes    \n\
Applying  %5d fixes to %d files\n\
Applying  %5d fixes to %d files\n\
Altering  %5d of them\n";
Altering  %5d of them\n";
 
 
    fprintf (stderr, zFmt, process_ct, ttl_data_size, apply_ct,
    fprintf (stderr, zFmt, process_ct, ttl_data_size, apply_ct,
             fixed_ct, altered_ct);
             fixed_ct, altered_ct);
  }
  }
#endif /* DO_STATS */
#endif /* DO_STATS */
 
 
# ifdef SEPARATE_FIX_PROC
# ifdef SEPARATE_FIX_PROC
  unlink( pz_temp_file );
  unlink( pz_temp_file );
# endif
# endif
  exit (EXIT_SUCCESS);
  exit (EXIT_SUCCESS);
}
}
 
 
 
 
static void
static void
do_version (void)
do_version (void)
{
{
  static const char zFmt[] = "echo '%s'";
  static const char zFmt[] = "echo '%s'";
  char zBuf[ 1024 ];
  char zBuf[ 1024 ];
 
 
  /* The 'version' option is really used to test that:
  /* The 'version' option is really used to test that:
     1.  The program loads correctly (no missing libraries)
     1.  The program loads correctly (no missing libraries)
     2.  that we can compile all the regular expressions.
     2.  that we can compile all the regular expressions.
     3.  we can correctly run our server shell process
     3.  we can correctly run our server shell process
  */
  */
  run_compiles ();
  run_compiles ();
  sprintf (zBuf, zFmt, program_id);
  sprintf (zBuf, zFmt, program_id);
#ifndef SEPARATE_FIX_PROC
#ifndef SEPARATE_FIX_PROC
  puts (zBuf + 5);
  puts (zBuf + 5);
  exit (strcmp (run_shell (zBuf), program_id));
  exit (strcmp (run_shell (zBuf), program_id));
#else
#else
  exit (system (zBuf));
  exit (system (zBuf));
#endif
#endif
}
}
 
 
/* * * * * * * * * * * * */
/* * * * * * * * * * * * */
 
 
void
void
initialize ( int argc, char** argv )
initialize ( int argc, char** argv )
{
{
  xmalloc_set_program_name (argv[0]);
  xmalloc_set_program_name (argv[0]);
 
 
  switch (argc)
  switch (argc)
    {
    {
    case 1:
    case 1:
      break;
      break;
 
 
    case 2:
    case 2:
      if (strcmp (argv[1], "-v") == 0)
      if (strcmp (argv[1], "-v") == 0)
        do_version ();
        do_version ();
      if (freopen (argv[1], "r", stdin) == (FILE*)NULL)
      if (freopen (argv[1], "r", stdin) == (FILE*)NULL)
        {
        {
          fprintf (stderr, "Error %d (%s) reopening %s as stdin\n",
          fprintf (stderr, "Error %d (%s) reopening %s as stdin\n",
                   errno, xstrerror (errno), argv[1] );
                   errno, xstrerror (errno), argv[1] );
          exit (EXIT_FAILURE);
          exit (EXIT_FAILURE);
        }
        }
      break;
      break;
 
 
    default:
    default:
      fputs ("fixincl ERROR:  too many command line arguments\n", stderr);
      fputs ("fixincl ERROR:  too many command line arguments\n", stderr);
      exit (EXIT_FAILURE);
      exit (EXIT_FAILURE);
    }
    }
 
 
#ifdef SIGCHLD
#ifdef SIGCHLD
  /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
  /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
     receive the signal.  A different setting is inheritable */
     receive the signal.  A different setting is inheritable */
  signal (SIGCHLD, SIG_DFL);
  signal (SIGCHLD, SIG_DFL);
#endif
#endif
 
 
  initialize_opts ();
  initialize_opts ();
 
 
  if (ISDIGIT ( *pz_verbose ))
  if (ISDIGIT ( *pz_verbose ))
    verbose_level = (te_verbose)atoi( pz_verbose );
    verbose_level = (te_verbose)atoi( pz_verbose );
  else
  else
    switch (*pz_verbose) {
    switch (*pz_verbose) {
    case 's':
    case 's':
    case 'S':
    case 'S':
      verbose_level = VERB_SILENT;     break;
      verbose_level = VERB_SILENT;     break;
 
 
    case 'f':
    case 'f':
    case 'F':
    case 'F':
      verbose_level = VERB_FIXES;      break;
      verbose_level = VERB_FIXES;      break;
 
 
    case 'a':
    case 'a':
    case 'A':
    case 'A':
      verbose_level = VERB_APPLIES;    break;
      verbose_level = VERB_APPLIES;    break;
 
 
    default:
    default:
    case 'p':
    case 'p':
    case 'P':
    case 'P':
      verbose_level = VERB_PROGRESS;   break;
      verbose_level = VERB_PROGRESS;   break;
 
 
    case 't':
    case 't':
    case 'T':
    case 'T':
      verbose_level = VERB_TESTS;      break;
      verbose_level = VERB_TESTS;      break;
 
 
    case 'e':
    case 'e':
    case 'E':
    case 'E':
      verbose_level = VERB_EVERYTHING; break;
      verbose_level = VERB_EVERYTHING; break;
    }
    }
  if (verbose_level >= VERB_EVERYTHING) {
  if (verbose_level >= VERB_EVERYTHING) {
    verbose_level = VERB_EVERYTHING;
    verbose_level = VERB_EVERYTHING;
    fputs ("fixinc verbosity:  EVERYTHING\n", stderr);
    fputs ("fixinc verbosity:  EVERYTHING\n", stderr);
  }
  }
  while ((pz_find_base[0] == '.') && (pz_find_base[1] == '/'))
  while ((pz_find_base[0] == '.') && (pz_find_base[1] == '/'))
    pz_find_base += 2;
    pz_find_base += 2;
  if ((pz_find_base[0] != '.') || (pz_find_base[1] != NUL))
  if ((pz_find_base[0] != '.') || (pz_find_base[1] != NUL))
    find_base_len = strlen( pz_find_base );
    find_base_len = strlen( pz_find_base );
 
 
  /*  Compile all the regular expressions now.
  /*  Compile all the regular expressions now.
      That way, it is done only once for the whole run.
      That way, it is done only once for the whole run.
      */
      */
  run_compiles ();
  run_compiles ();
 
 
# ifdef SEPARATE_FIX_PROC
# ifdef SEPARATE_FIX_PROC
  /* NULL as the first argument to `tempnam' causes it to DTRT
  /* NULL as the first argument to `tempnam' causes it to DTRT
     wrt the temporary directory where the file will be created.  */
     wrt the temporary directory where the file will be created.  */
  pz_temp_file = tempnam( NULL, "fxinc" );
  pz_temp_file = tempnam( NULL, "fxinc" );
# endif
# endif
 
 
  signal (SIGQUIT, SIG_IGN);
  signal (SIGQUIT, SIG_IGN);
  signal (SIGIOT,  SIG_IGN);
  signal (SIGIOT,  SIG_IGN);
  signal (SIGPIPE, SIG_IGN);
  signal (SIGPIPE, SIG_IGN);
  signal (SIGALRM, SIG_IGN);
  signal (SIGALRM, SIG_IGN);
  signal (SIGTERM, SIG_IGN);
  signal (SIGTERM, SIG_IGN);
}
}
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   load_file loads all the contents of a file into malloc-ed memory.
   load_file loads all the contents of a file into malloc-ed memory.
   Its argument is the name of the file to read in; the returned
   Its argument is the name of the file to read in; the returned
   result is the NUL terminated contents of the file.  The file
   result is the NUL terminated contents of the file.  The file
   is presumed to be an ASCII text file containing no NULs.  */
   is presumed to be an ASCII text file containing no NULs.  */
char *
char *
load_file ( const char* fname )
load_file ( const char* fname )
{
{
  struct stat stbf;
  struct stat stbf;
  char* res;
  char* res;
 
 
  if (stat (fname, &stbf) != 0)
  if (stat (fname, &stbf) != 0)
    {
    {
      if (NOT_SILENT)
      if (NOT_SILENT)
        fprintf (stderr, "error %d (%s) stat-ing %s\n",
        fprintf (stderr, "error %d (%s) stat-ing %s\n",
                 errno, xstrerror (errno), fname );
                 errno, xstrerror (errno), fname );
      return (char *) NULL;
      return (char *) NULL;
    }
    }
  if (stbf.st_size == 0)
  if (stbf.st_size == 0)
    return (char*)NULL;
    return (char*)NULL;
 
 
  /*  Make the data map size one larger than the file size for documentation
  /*  Make the data map size one larger than the file size for documentation
      purposes.  Truth is that there will be a following NUL character if
      purposes.  Truth is that there will be a following NUL character if
      the file size is not a multiple of the page size.  If it is a multiple,
      the file size is not a multiple of the page size.  If it is a multiple,
      then this adjustment sometimes fails anyway.  */
      then this adjustment sometimes fails anyway.  */
  data_map_size = stbf.st_size+1;
  data_map_size = stbf.st_size+1;
  data_map_fd   = open (fname, O_RDONLY);
  data_map_fd   = open (fname, O_RDONLY);
  ttl_data_size += data_map_size-1;
  ttl_data_size += data_map_size-1;
 
 
  if (data_map_fd < 0)
  if (data_map_fd < 0)
    {
    {
      if (NOT_SILENT)
      if (NOT_SILENT)
        fprintf (stderr, "error %d (%s) opening %s for read\n",
        fprintf (stderr, "error %d (%s) opening %s for read\n",
                 errno, xstrerror (errno), fname);
                 errno, xstrerror (errno), fname);
      return (char*)NULL;
      return (char*)NULL;
    }
    }
 
 
#ifdef HAVE_MMAP_FILE
#ifdef HAVE_MMAP_FILE
  curr_data_mapped = BOOL_TRUE;
  curr_data_mapped = BOOL_TRUE;
 
 
  /*  IF the file size is a multiple of the page size,
  /*  IF the file size is a multiple of the page size,
      THEN sometimes you will seg fault trying to access a trailing byte */
      THEN sometimes you will seg fault trying to access a trailing byte */
  if ((stbf.st_size & (getpagesize()-1)) == 0)
  if ((stbf.st_size & (getpagesize()-1)) == 0)
    res = (char*)BAD_ADDR;
    res = (char*)BAD_ADDR;
  else
  else
    res = (char*)mmap ((void*)NULL, data_map_size, PROT_READ,
    res = (char*)mmap ((void*)NULL, data_map_size, PROT_READ,
                       MAP_PRIVATE, data_map_fd, 0);
                       MAP_PRIVATE, data_map_fd, 0);
  if (res == (char*)BAD_ADDR)
  if (res == (char*)BAD_ADDR)
#endif
#endif
    {
    {
      FILE* fp = fdopen (data_map_fd, "r");
      FILE* fp = fdopen (data_map_fd, "r");
      curr_data_mapped = BOOL_FALSE;
      curr_data_mapped = BOOL_FALSE;
      res = load_file_data (fp);
      res = load_file_data (fp);
      fclose (fp);
      fclose (fp);
    }
    }
 
 
  return res;
  return res;
}
}
 
 
static int
static int
machine_matches( tFixDesc* p_fixd )
machine_matches( tFixDesc* p_fixd )
        {
        {
# ifndef SEPARATE_FIX_PROC
# ifndef SEPARATE_FIX_PROC
          tSCC case_fmt[] = "case %s in\n";     /*  9 bytes, plus string */
          tSCC case_fmt[] = "case %s in\n";     /*  9 bytes, plus string */
          tSCC esac_fmt[] =
          tSCC esac_fmt[] =
               " )\n    echo %s ;;\n* ) echo %s ;;\nesac";/*  4 bytes */
               " )\n    echo %s ;;\n* ) echo %s ;;\nesac";/*  4 bytes */
          tSCC skip[] = "skip";                 /*  4 bytes */
          tSCC skip[] = "skip";                 /*  4 bytes */
          tSCC run[] = "run";                   /*  3 bytes */
          tSCC run[] = "run";                   /*  3 bytes */
          /* total bytes to add to machine sum:    49 - see fixincl.tpl */
          /* total bytes to add to machine sum:    49 - see fixincl.tpl */
 
 
          const char **papz_machs = p_fixd->papz_machs;
          const char **papz_machs = p_fixd->papz_machs;
          char *pz;
          char *pz;
          const char *pz_sep = "";
          const char *pz_sep = "";
          tCC *pz_if_true;
          tCC *pz_if_true;
          tCC *pz_if_false;
          tCC *pz_if_false;
          char cmd_buf[ MACH_LIST_SIZE_LIMIT ]; /* size lim from fixincl.tpl */
          char cmd_buf[ MACH_LIST_SIZE_LIMIT ]; /* size lim from fixincl.tpl */
 
 
          /* Start the case statement */
          /* Start the case statement */
 
 
          sprintf (cmd_buf, case_fmt, pz_machine);
          sprintf (cmd_buf, case_fmt, pz_machine);
          pz = cmd_buf + strlen (cmd_buf);
          pz = cmd_buf + strlen (cmd_buf);
 
 
          /*  Determine if a match means to apply the fix or not apply it */
          /*  Determine if a match means to apply the fix or not apply it */
 
 
          if (p_fixd->fd_flags & FD_MACH_IFNOT)
          if (p_fixd->fd_flags & FD_MACH_IFNOT)
            {
            {
              pz_if_true  = skip;
              pz_if_true  = skip;
              pz_if_false = run;
              pz_if_false = run;
            }
            }
          else
          else
            {
            {
              pz_if_true  = run;
              pz_if_true  = run;
              pz_if_false = skip;
              pz_if_false = skip;
            }
            }
 
 
          /*  Emit all the machine names.  If there are more than one,
          /*  Emit all the machine names.  If there are more than one,
              then we will insert " | \\\n" between the names  */
              then we will insert " | \\\n" between the names  */
 
 
          for (;;)
          for (;;)
            {
            {
              const char* pz_mach = *(papz_machs++);
              const char* pz_mach = *(papz_machs++);
 
 
              if (pz_mach == (const char*) NULL)
              if (pz_mach == (const char*) NULL)
                break;
                break;
              sprintf (pz, "%s%s", pz_sep, pz_mach);
              sprintf (pz, "%s%s", pz_sep, pz_mach);
              pz += strlen (pz);
              pz += strlen (pz);
              pz_sep = " | \\\n";
              pz_sep = " | \\\n";
            }
            }
 
 
          /* Now emit the match and not-match actions and the esac */
          /* Now emit the match and not-match actions and the esac */
 
 
          sprintf (pz, esac_fmt, pz_if_true, pz_if_false);
          sprintf (pz, esac_fmt, pz_if_true, pz_if_false);
 
 
          /*  Run the script.
          /*  Run the script.
              The result will start either with 's' or 'r'.  */
              The result will start either with 's' or 'r'.  */
 
 
          {
          {
            int skip;
            int skip;
            pz = run_shell (cmd_buf);
            pz = run_shell (cmd_buf);
            skip = (*pz == 's');
            skip = (*pz == 's');
            free ( (void*)pz );
            free ( (void*)pz );
            if (skip)
            if (skip)
              {
              {
                p_fixd->fd_flags |= FD_SKIP_TEST;
                p_fixd->fd_flags |= FD_SKIP_TEST;
                return BOOL_FALSE;
                return BOOL_FALSE;
              }
              }
          }
          }
 
 
  return BOOL_TRUE;
  return BOOL_TRUE;
# else /* is SEPARATE_FIX_PROC */
# else /* is SEPARATE_FIX_PROC */
  const char **papz_machs = p_fixd->papz_machs;
  const char **papz_machs = p_fixd->papz_machs;
  int invert = (p_fixd->fd_flags & FD_MACH_IFNOT) != 0;
  int invert = (p_fixd->fd_flags & FD_MACH_IFNOT) != 0;
  for (;;)
  for (;;)
    {
    {
      const char* pz_mach = *(papz_machs++);
      const char* pz_mach = *(papz_machs++);
 
 
      if (pz_mach == (const char*) NULL)
      if (pz_mach == (const char*) NULL)
        break;
        break;
      if (strstr (pz_mach, "dos") != NULL && !invert)
      if (strstr (pz_mach, "dos") != NULL && !invert)
        return BOOL_TRUE;
        return BOOL_TRUE;
    }
    }
 
 
  p_fixd->fd_flags |= FD_SKIP_TEST;
  p_fixd->fd_flags |= FD_SKIP_TEST;
  return BOOL_FALSE;
  return BOOL_FALSE;
# endif
# endif
}
}
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   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
void
run_compiles (void)
run_compiles (void)
{
{
  tFixDesc *p_fixd = fixDescList;
  tFixDesc *p_fixd = fixDescList;
  int fix_ct = FIX_COUNT;
  int fix_ct = FIX_COUNT;
  regex_t *p_re = XCNEWVEC (regex_t, REGEX_COUNT);
  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 */
 
 
  memset (&incl_quote_re, '\0', sizeof (regex_t));
  memset (&incl_quote_re, '\0', sizeof (regex_t));
 
 
  compile_re (incl_quote_pat, &incl_quote_re, 1,
  compile_re (incl_quote_pat, &incl_quote_re, 1,
              "quoted include", "run_compiles");
              "quoted include", "run_compiles");
 
 
  /*  Allow machine name tests to be ignored (testing, mainly) */
  /*  Allow machine name tests to be ignored (testing, mainly) */
 
 
  if (pz_machine && ((*pz_machine == '\0') || (*pz_machine == '*')))
  if (pz_machine && ((*pz_machine == '\0') || (*pz_machine == '*')))
    pz_machine = (char*)NULL;
    pz_machine = (char*)NULL;
 
 
  /* FOR every fixup, ...  */
  /* FOR every fixup, ...  */
  do
  do
    {
    {
      tTestDesc *p_test = p_fixd->p_test_desc;
      tTestDesc *p_test = p_fixd->p_test_desc;
      int test_ct = p_fixd->test_ct;
      int test_ct = p_fixd->test_ct;
 
 
      /*  IF the machine type pointer is not NULL (we are not in test mode)
      /*  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
             AND this test is for or not done on particular machines
          THEN ...   */
          THEN ...   */
 
 
      if (  (pz_machine != NULL)
      if (  (pz_machine != NULL)
         && (p_fixd->papz_machs != (const char**) NULL)
         && (p_fixd->papz_machs != (const char**) NULL)
         && ! machine_matches (p_fixd) )
         && ! machine_matches (p_fixd) )
        continue;
        continue;
 
 
      /* FOR every test for the fixup, ...  */
      /* FOR every test for the fixup, ...  */
 
 
      while (--test_ct >= 0)
      while (--test_ct >= 0)
        {
        {
          switch (p_test->type)
          switch (p_test->type)
            {
            {
            case TT_EGREP:
            case TT_EGREP:
            case TT_NEGREP:
            case TT_NEGREP:
              p_test->p_test_regex = p_re++;
              p_test->p_test_regex = p_re++;
              compile_re (p_test->pz_test_text, p_test->p_test_regex, 0,
              compile_re (p_test->pz_test_text, p_test->p_test_regex, 0,
                          "select test", p_fixd->fix_name);
                          "select test", p_fixd->fix_name);
            default: break;
            default: break;
            }
            }
          p_test++;
          p_test++;
        }
        }
    }
    }
  while (p_fixd++, --fix_ct > 0);
  while (p_fixd++, --fix_ct > 0);
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   create_file  Create the output modified file.
   create_file  Create the output modified file.
   Input:    the name of the file to create
   Input:    the name of the file to create
   Returns:  a file pointer to the new, open file  */
   Returns:  a file pointer to the new, open file  */
 
 
#if defined(S_IRUSR) && defined(S_IWUSR) && \
#if defined(S_IRUSR) && defined(S_IWUSR) && \
    defined(S_IRGRP) && defined(S_IROTH)
    defined(S_IRGRP) && defined(S_IROTH)
 
 
#   define S_IRALL (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#   define S_IRALL (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#else
#else
#   define S_IRALL 0644
#   define S_IRALL 0644
#endif
#endif
 
 
#if defined(S_IRWXU) && defined(S_IRGRP) && defined(S_IXGRP) && \
#if defined(S_IRWXU) && defined(S_IRGRP) && defined(S_IXGRP) && \
    defined(S_IROTH) && defined(S_IXOTH)
    defined(S_IROTH) && defined(S_IXOTH)
 
 
#   define S_DIRALL (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#   define S_DIRALL (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
#else
#else
#   define S_DIRALL 0755
#   define S_DIRALL 0755
#endif
#endif
 
 
 
 
static FILE *
static FILE *
create_file (void)
create_file (void)
{
{
  int fd;
  int fd;
  FILE *pf;
  FILE *pf;
  char fname[MAXPATHLEN];
  char fname[MAXPATHLEN];
 
 
  sprintf (fname, "%s/%s", pz_dest_dir, pz_curr_file + find_base_len);
  sprintf (fname, "%s/%s", pz_dest_dir, pz_curr_file + find_base_len);
 
 
  fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
  fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
 
 
  /*  We may need to create the directories needed... */
  /*  We may need to create the directories needed... */
  if ((fd < 0) && (errno == ENOENT))
  if ((fd < 0) && (errno == ENOENT))
    {
    {
      char *pz_dir = strchr (fname + 1, '/');
      char *pz_dir = strchr (fname + 1, '/');
      struct stat stbf;
      struct stat stbf;
 
 
      while (pz_dir != (char *) NULL)
      while (pz_dir != (char *) NULL)
        {
        {
          *pz_dir = NUL;
          *pz_dir = NUL;
          if (stat (fname, &stbf) < 0)
          if (stat (fname, &stbf) < 0)
            {
            {
#ifdef _WIN32
#ifdef _WIN32
              mkdir (fname);
              mkdir (fname);
#else
#else
              mkdir (fname, S_IFDIR | S_DIRALL);
              mkdir (fname, S_IFDIR | S_DIRALL);
#endif
#endif
            }
            }
 
 
          *pz_dir = '/';
          *pz_dir = '/';
          pz_dir = strchr (pz_dir + 1, '/');
          pz_dir = strchr (pz_dir + 1, '/');
        }
        }
 
 
      /*  Now, lets try the open again... */
      /*  Now, lets try the open again... */
      fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
      fd = open (fname, O_WRONLY | O_CREAT | O_TRUNC, S_IRALL);
    }
    }
  if (fd < 0)
  if (fd < 0)
    {
    {
      fprintf (stderr, "Error %d (%s) creating %s\n",
      fprintf (stderr, "Error %d (%s) creating %s\n",
               errno, xstrerror (errno), fname);
               errno, xstrerror (errno), fname);
      exit (EXIT_FAILURE);
      exit (EXIT_FAILURE);
    }
    }
  if (NOT_SILENT)
  if (NOT_SILENT)
    fprintf (stderr, "Fixed:  %s\n", pz_curr_file);
    fprintf (stderr, "Fixed:  %s\n", pz_curr_file);
  pf = fdopen (fd, "w");
  pf = fdopen (fd, "w");
 
 
  /*
  /*
   *  IF pz_machine is NULL, then we are in some sort of test mode.
   *  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.
   *  Do not insert the current directory name.  Use a constant string.
   */
   */
  fprintf (pf, z_std_preamble,
  fprintf (pf, z_std_preamble,
           (pz_machine == NULL)
           (pz_machine == NULL)
           ? "fixinc/tests/inc"
           ? "fixinc/tests/inc"
           : pz_input_dir,
           : pz_input_dir,
           pz_curr_file);
           pz_curr_file);
 
 
  return pf;
  return pf;
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
  test_test   make sure a shell-style test expression passes.
  test_test   make sure a shell-style test expression passes.
  Input:  a pointer to the descriptor of the test to run and
  Input:  a pointer to the descriptor of the test to run and
          the name of the file that we might want to fix
          the name of the file that we might want to fix
  Result: APPLY_FIX or SKIP_FIX, depending on the result of the
  Result: APPLY_FIX or SKIP_FIX, depending on the result of the
          shell script we run.  */
          shell script we run.  */
#ifndef SEPARATE_FIX_PROC
#ifndef SEPARATE_FIX_PROC
static int
static int
test_test (tTestDesc* p_test, char* pz_test_file)
test_test (tTestDesc* p_test, char* pz_test_file)
{
{
  tSCC cmd_fmt[] =
  tSCC cmd_fmt[] =
"file=%s\n\
"file=%s\n\
if ( test %s ) > /dev/null 2>&1\n\
if ( test %s ) > /dev/null 2>&1\n\
then echo TRUE\n\
then echo TRUE\n\
else echo FALSE\n\
else echo FALSE\n\
fi";
fi";
 
 
  char *pz_res;
  char *pz_res;
  int res;
  int res;
 
 
  static char cmd_buf[4096];
  static char cmd_buf[4096];
 
 
  sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
  sprintf (cmd_buf, cmd_fmt, pz_test_file, p_test->pz_test_text);
  pz_res = run_shell (cmd_buf);
  pz_res = run_shell (cmd_buf);
 
 
  switch (*pz_res) {
  switch (*pz_res) {
  case 'T':
  case 'T':
    res = APPLY_FIX;
    res = APPLY_FIX;
    break;
    break;
 
 
  case 'F':
  case 'F':
    res = SKIP_FIX;
    res = SKIP_FIX;
    break;
    break;
 
 
  default:
  default:
    fprintf (stderr, "Script yielded bogus result of `%s':\n%s\n\n",
    fprintf (stderr, "Script yielded bogus result of `%s':\n%s\n\n",
             pz_res, cmd_buf );
             pz_res, cmd_buf );
    res = SKIP_FIX;
    res = SKIP_FIX;
  }
  }
 
 
  free ((void *) pz_res);
  free ((void *) pz_res);
  return res;
  return res;
}
}
#else
#else
/*
/*
 *  IF we are in MS-DOS land, then whatever shell-type test is required
 *  IF we are in MS-DOS land, then whatever shell-type test is required
 *  will, by definition, fail
 *  will, by definition, fail
 */
 */
#define test_test(t,tf)  SKIP_FIX
#define test_test(t,tf)  SKIP_FIX
#endif
#endif
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
  egrep_test   make sure an egrep expression is found in the file text.
  egrep_test   make sure an egrep expression is found in the file text.
  Input:  a pointer to the descriptor of the test to run and
  Input:  a pointer to the descriptor of the test to run and
          the pointer to the contents of the file under suspicion
          the pointer to the contents of the file under suspicion
  Result: APPLY_FIX if the pattern is found, SKIP_FIX otherwise
  Result: APPLY_FIX if the pattern is found, SKIP_FIX otherwise
 
 
  The caller may choose to reverse meaning if the sense of the test
  The caller may choose to reverse meaning if the sense of the test
  is inverted.  */
  is inverted.  */
 
 
static int
static int
egrep_test (char* pz_data, tTestDesc* p_test)
egrep_test (char* pz_data, tTestDesc* p_test)
{
{
#ifdef DEBUG
#ifdef DEBUG
  if (p_test->p_test_regex == 0)
  if (p_test->p_test_regex == 0)
    fprintf (stderr, "fixincl ERROR RE not compiled:  `%s'\n",
    fprintf (stderr, "fixincl ERROR RE not compiled:  `%s'\n",
             p_test->pz_test_text);
             p_test->pz_test_text);
#endif
#endif
  if (xregexec (p_test->p_test_regex, pz_data, 0, 0, 0) == 0)
  if (xregexec (p_test->p_test_regex, pz_data, 0, 0, 0) == 0)
    return APPLY_FIX;
    return APPLY_FIX;
  return SKIP_FIX;
  return SKIP_FIX;
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
  quoted_file_exists  Make sure that a file exists before we emit
  quoted_file_exists  Make sure that a file exists before we emit
  the file name.  If we emit the name, our invoking shell will try
  the file name.  If we emit the name, our invoking shell will try
  to copy a non-existing file into the destination directory.  */
  to copy a non-existing file into the destination directory.  */
 
 
static int
static int
quoted_file_exists (const char* pz_src_path,
quoted_file_exists (const char* pz_src_path,
                    const char* pz_file_path,
                    const char* pz_file_path,
                    const char* pz_file)
                    const char* pz_file)
{
{
  char z[ MAXPATHLEN ];
  char z[ MAXPATHLEN ];
  char* pz;
  char* pz;
  sprintf (z, "%s/%s/", pz_src_path, pz_file_path);
  sprintf (z, "%s/%s/", pz_src_path, pz_file_path);
  pz = z + strlen ( z );
  pz = z + strlen ( z );
 
 
  for (;;) {
  for (;;) {
    char ch = *pz_file++;
    char ch = *pz_file++;
    if (! ISGRAPH( ch ))
    if (! ISGRAPH( ch ))
      return 0;
      return 0;
    if (ch == '"')
    if (ch == '"')
      break;
      break;
    *pz++ = ch;
    *pz++ = ch;
  }
  }
  *pz = '\0';
  *pz = '\0';
  {
  {
    struct stat s;
    struct stat s;
    if (stat (z, &s) != 0)
    if (stat (z, &s) != 0)
      return 0;
      return 0;
    return S_ISREG( s.st_mode );
    return S_ISREG( s.st_mode );
  }
  }
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 *
 *
   extract_quoted_files
   extract_quoted_files
 
 
   The syntax, `#include "file.h"' specifies that the compiler is to
   The syntax, `#include "file.h"' specifies that the compiler is to
   search the local directory of the current file before the include
   search the local directory of the current file before the include
   list.  Consequently, if we have modified a header and stored it in
   list.  Consequently, if we have modified a header and stored it in
   another directory, any files that are included by that modified
   another directory, any files that are included by that modified
   file in that fashion must also be copied into this new directory.
   file in that fashion must also be copied into this new directory.
   This routine finds those flavors of #include and for each one found
   This routine finds those flavors of #include and for each one found
   emits a triple of:
   emits a triple of:
 
 
    1.  source directory of the original file
    1.  source directory of the original file
    2.  the relative path file name of the #includ-ed file
    2.  the relative path file name of the #includ-ed file
    3.  the full destination path for this file
    3.  the full destination path for this file
 
 
   Input:  the text of the file, the file name and a pointer to the
   Input:  the text of the file, the file name and a pointer to the
           match list where the match information was stored.
           match list where the match information was stored.
   Result: internally nothing.  The results are written to stdout
   Result: internally nothing.  The results are written to stdout
           for interpretation by the invoking shell  */
           for interpretation by the invoking shell  */
 
 
 
 
static void
static void
extract_quoted_files (char* pz_data,
extract_quoted_files (char* pz_data,
                      const char* pz_fixed_file,
                      const char* pz_fixed_file,
                      regmatch_t* p_re_match)
                      regmatch_t* p_re_match)
{
{
  char *pz_dir_end = strrchr (pz_fixed_file, '/');
  char *pz_dir_end = strrchr (pz_fixed_file, '/');
  char *pz_incl_quot = pz_data;
  char *pz_incl_quot = pz_data;
 
 
  if (VLEVEL( VERB_APPLIES ))
  if (VLEVEL( VERB_APPLIES ))
    fprintf (stderr, "Quoted includes in %s\n", pz_fixed_file);
    fprintf (stderr, "Quoted includes in %s\n", pz_fixed_file);
 
 
  /*  Set "pz_fixed_file" to point to the containing subdirectory of the source
  /*  Set "pz_fixed_file" to point to the containing subdirectory of the source
      If there is none, then it is in our current directory, ".".   */
      If there is none, then it is in our current directory, ".".   */
 
 
  if (pz_dir_end == (char *) NULL)
  if (pz_dir_end == (char *) NULL)
    pz_fixed_file = ".";
    pz_fixed_file = ".";
  else
  else
    *pz_dir_end = '\0';
    *pz_dir_end = '\0';
 
 
  for (;;)
  for (;;)
    {
    {
      pz_incl_quot += p_re_match->rm_so;
      pz_incl_quot += p_re_match->rm_so;
 
 
      /*  Skip forward to the included file name */
      /*  Skip forward to the included file name */
      while (*pz_incl_quot != '"')
      while (*pz_incl_quot != '"')
        pz_incl_quot++;
        pz_incl_quot++;
 
 
      if (quoted_file_exists (pz_src_dir, pz_fixed_file, pz_incl_quot))
      if (quoted_file_exists (pz_src_dir, pz_fixed_file, pz_incl_quot))
        {
        {
          /* Print the source directory and the subdirectory
          /* Print the source directory and the subdirectory
             of the file in question.  */
             of the file in question.  */
          printf ("%s  %s/", pz_src_dir, pz_fixed_file);
          printf ("%s  %s/", pz_src_dir, pz_fixed_file);
          pz_dir_end = pz_incl_quot;
          pz_dir_end = pz_incl_quot;
 
 
          /* Append to the directory the relative path of the desired file */
          /* Append to the directory the relative path of the desired file */
          while (*pz_incl_quot != '"')
          while (*pz_incl_quot != '"')
            putc (*pz_incl_quot++, stdout);
            putc (*pz_incl_quot++, stdout);
 
 
          /* Now print the destination directory appended with the
          /* Now print the destination directory appended with the
             relative path of the desired file */
             relative path of the desired file */
          printf ("  %s/%s/", pz_dest_dir, pz_fixed_file);
          printf ("  %s/%s/", pz_dest_dir, pz_fixed_file);
          while (*pz_dir_end != '"')
          while (*pz_dir_end != '"')
            putc (*pz_dir_end++, stdout);
            putc (*pz_dir_end++, stdout);
 
 
          /* End of entry */
          /* End of entry */
          putc ('\n', stdout);
          putc ('\n', stdout);
        }
        }
 
 
      /* Find the next entry */
      /* Find the next entry */
      if (xregexec (&incl_quote_re, pz_incl_quot, 1, p_re_match, 0) != 0)
      if (xregexec (&incl_quote_re, pz_incl_quot, 1, p_re_match, 0) != 0)
        break;
        break;
    }
    }
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
    Somebody wrote a *_fix subroutine that we must call.
    Somebody wrote a *_fix subroutine that we must call.
    */
    */
#ifndef SEPARATE_FIX_PROC
#ifndef SEPARATE_FIX_PROC
static int
static int
internal_fix (int read_fd, tFixDesc* p_fixd)
internal_fix (int read_fd, tFixDesc* p_fixd)
{
{
  int fd[2];
  int fd[2];
 
 
  if (pipe( fd ) != 0)
  if (pipe( fd ) != 0)
    {
    {
      fprintf (stderr, "Error %d on pipe(2) call\n", errno );
      fprintf (stderr, "Error %d on pipe(2) call\n", errno );
      exit (EXIT_FAILURE);
      exit (EXIT_FAILURE);
    }
    }
 
 
  for (;;)
  for (;;)
    {
    {
      pid_t childid = fork();
      pid_t childid = fork();
 
 
      switch (childid)
      switch (childid)
        {
        {
        case -1:
        case -1:
          break;
          break;
 
 
        case 0:
        case 0:
          close (fd[0]);
          close (fd[0]);
          goto do_child_task;
          goto do_child_task;
 
 
        default:
        default:
          /*
          /*
           *  Parent process
           *  Parent process
           */
           */
          close (read_fd);
          close (read_fd);
          close (fd[1]);
          close (fd[1]);
          return fd[0];
          return fd[0];
        }
        }
 
 
      /*
      /*
       *  Parent in error
       *  Parent in error
       */
       */
      fprintf (stderr, z_fork_err, errno, xstrerror (errno),
      fprintf (stderr, z_fork_err, errno, xstrerror (errno),
               p_fixd->fix_name);
               p_fixd->fix_name);
      {
      {
        static int failCt = 0;
        static int failCt = 0;
        if ((errno != EAGAIN) || (++failCt > 10))
        if ((errno != EAGAIN) || (++failCt > 10))
          exit (EXIT_FAILURE);
          exit (EXIT_FAILURE);
        sleep (1);
        sleep (1);
      }
      }
    } do_child_task:;
    } do_child_task:;
 
 
  /*
  /*
   *  Close our current stdin and stdout
   *  Close our current stdin and stdout
   */
   */
  close (STDIN_FILENO);
  close (STDIN_FILENO);
  close (STDOUT_FILENO);
  close (STDOUT_FILENO);
  UNLOAD_DATA();
  UNLOAD_DATA();
 
 
  /*
  /*
   *  Make the fd passed in the stdin, and the write end of
   *  Make the fd passed in the stdin, and the write end of
   *  the new pipe become the stdout.
   *  the new pipe become the stdout.
   */
   */
  dup2 (fd[1], STDOUT_FILENO);
  dup2 (fd[1], STDOUT_FILENO);
  dup2 (read_fd, STDIN_FILENO);
  dup2 (read_fd, STDIN_FILENO);
 
 
  apply_fix (p_fixd, pz_curr_file);
  apply_fix (p_fixd, pz_curr_file);
  exit (0);
  exit (0);
}
}
#endif /* !SEPARATE_FIX_PROC */
#endif /* !SEPARATE_FIX_PROC */
 
 
 
 
#ifdef SEPARATE_FIX_PROC
#ifdef SEPARATE_FIX_PROC
static void
static void
fix_with_system (tFixDesc* p_fixd,
fix_with_system (tFixDesc* p_fixd,
                 tCC* pz_fix_file,
                 tCC* pz_fix_file,
                 tCC* pz_file_source,
                 tCC* pz_file_source,
                 tCC* pz_temp_file)
                 tCC* pz_temp_file)
{
{
  char*  pz_cmd;
  char*  pz_cmd;
  char*  pz_scan;
  char*  pz_scan;
  size_t argsize;
  size_t argsize;
 
 
  if (p_fixd->fd_flags & FD_SUBROUTINE)
  if (p_fixd->fd_flags & FD_SUBROUTINE)
    {
    {
      static const char z_applyfix_prog[] =
      static const char z_applyfix_prog[] =
        "/../fixincludes/applyfix" EXE_EXT;
        "/../fixincludes/applyfix" EXE_EXT;
 
 
      struct stat buf;
      struct stat buf;
      argsize = 32
      argsize = 32
              + strlen (pz_orig_dir)
              + strlen (pz_orig_dir)
              + sizeof (z_applyfix_prog)
              + sizeof (z_applyfix_prog)
              + strlen (pz_fix_file)
              + strlen (pz_fix_file)
              + strlen (pz_file_source)
              + strlen (pz_file_source)
              + strlen (pz_temp_file);
              + strlen (pz_temp_file);
 
 
      /* Allocate something sure to be big enough for our purposes */
      /* Allocate something sure to be big enough for our purposes */
      pz_cmd = XNEWVEC (char, argsize);
      pz_cmd = XNEWVEC (char, argsize);
      strcpy (pz_cmd, pz_orig_dir);
      strcpy (pz_cmd, pz_orig_dir);
      pz_scan = pz_cmd + strlen (pz_orig_dir);
      pz_scan = pz_cmd + strlen (pz_orig_dir);
 
 
      strcpy (pz_scan, z_applyfix_prog);
      strcpy (pz_scan, z_applyfix_prog);
 
 
      /* IF we can't find the "applyfix" executable file at the first guess,
      /* 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)
      if (stat (pz_cmd, &buf) == -1)
        {
        {
          strcpy (pz_scan, "/..");
          strcpy (pz_scan, "/..");
          strcpy (pz_scan+3, z_applyfix_prog);
          strcpy (pz_scan+3, z_applyfix_prog);
        }
        }
 
 
      pz_scan += strlen (pz_scan);
      pz_scan += strlen (pz_scan);
 
 
      /*
      /*
       *  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' '%s'", p_fixd - fixDescList,
               pz_fix_file, pz_file_source, pz_temp_file);
               pz_fix_file, pz_file_source, pz_temp_file);
    }
    }
  else /* NOT an "internal" fix: */
  else /* NOT an "internal" fix: */
    {
    {
      size_t parg_size;
      size_t parg_size;
#ifdef __MSDOS__
#ifdef __MSDOS__
      /* Don't use the "src > dstX; rm -f dst; mv -f dstX dst" trick:
      /* 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
         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
         file by that name; and DOS's system(3) doesn't mind to
         clobber existing file in redirection.  Besides, with DOS 8+3
         clobber existing file in redirection.  Besides, with DOS 8+3
         limited file namespace, we can easily lose if dst already has
         limited file namespace, we can easily lose if dst already has
         an extension that is 3 or more characters long.
         an extension that is 3 or more characters long.
 
 
         I do not think the 8+3 issue is relevant because all the files
         I do not think the 8+3 issue is relevant because all the files
         we operate on are named "*.h", making 8+2 adequate.  Anyway,
         we operate on are named "*.h", making 8+2 adequate.  Anyway,
         the following bizarre use of 'cat' only works on DOS boxes.
         the following bizarre use of 'cat' only works on DOS boxes.
         It causes the file to be dropped into a temporary file for
         It causes the file to be dropped into a temporary file for
         'cat' to read (pipes do not work on DOS).  */
         'cat' to read (pipes do not work on DOS).  */
      tSCC   z_cmd_fmt[] = " '%s' | cat > '%s'";
      tSCC   z_cmd_fmt[] = " '%s' | cat > '%s'";
#else
#else
      /* Don't use positional formatting arguments because some lame-o
      /* Don't use positional formatting arguments because some lame-o
         implementations cannot cope  :-(.  */
         implementations cannot cope  :-(.  */
      tSCC   z_cmd_fmt[] = " %s > %sX ; rm -f %s; mv -f %sX %s";
      tSCC   z_cmd_fmt[] = " %s > %sX ; rm -f %s; mv -f %sX %s";
#endif
#endif
      tCC**  ppArgs = p_fixd->patch_args;
      tCC**  ppArgs = p_fixd->patch_args;
 
 
      argsize = sizeof( z_cmd_fmt ) + strlen( pz_temp_file )
      argsize = sizeof( z_cmd_fmt ) + strlen( pz_temp_file )
              + strlen( pz_file_source );
              + strlen( pz_file_source );
      parg_size = argsize;
      parg_size = argsize;
 
 
 
 
      /*
      /*
       *  Compute the size of the command line.  Add lotsa extra space
       *  Compute the size of the command line.  Add lotsa extra space
       *  because some of the args to sed use lotsa single quotes.
       *  because some of the args to sed use lotsa single quotes.
       *  (This requires three extra bytes per quote.  Here we allow
       *  (This requires three extra bytes per quote.  Here we allow
       *  for up to 8 single quotes for each argument, including the
       *  for up to 8 single quotes for each argument, including the
       *  command name "sed" itself.  Nobody will *ever* need more. :)
       *  command name "sed" itself.  Nobody will *ever* need more. :)
       */
       */
      for (;;)
      for (;;)
        {
        {
          tCC* p_arg = *(ppArgs++);
          tCC* p_arg = *(ppArgs++);
          if (p_arg == NULL)
          if (p_arg == NULL)
            break;
            break;
          argsize += 24 + strlen( p_arg );
          argsize += 24 + strlen( p_arg );
        }
        }
 
 
      /* Estimated buffer size we will need.  */
      /* Estimated buffer size we will need.  */
      pz_scan = pz_cmd = XNEWVEC (char, argsize);
      pz_scan = pz_cmd = XNEWVEC (char, argsize);
      /* How much of it do we allot to the program name and its
      /* How much of it do we allot to the program name and its
         arguments.  */
         arguments.  */
      parg_size = argsize - parg_size;
      parg_size = argsize - parg_size;
 
 
      ppArgs = p_fixd->patch_args;
      ppArgs = p_fixd->patch_args;
 
 
      /*
      /*
       *  Copy the program name, unquoted
       *  Copy the program name, unquoted
       */
       */
      {
      {
        tCC*   pArg = *(ppArgs++);
        tCC*   pArg = *(ppArgs++);
        for (;;)
        for (;;)
          {
          {
            char ch = *(pArg++);
            char ch = *(pArg++);
            if (ch == NUL)
            if (ch == NUL)
              break;
              break;
            *(pz_scan++) = ch;
            *(pz_scan++) = ch;
          }
          }
      }
      }
 
 
      /*
      /*
       *  Copy the program arguments, quoted
       *  Copy the program arguments, quoted
       */
       */
      for (;;)
      for (;;)
        {
        {
          tCC*   pArg = *(ppArgs++);
          tCC*   pArg = *(ppArgs++);
          char*  pz_scan_save;
          char*  pz_scan_save;
          if (pArg == NULL)
          if (pArg == NULL)
            break;
            break;
          *(pz_scan++) = ' ';
          *(pz_scan++) = ' ';
          pz_scan = make_raw_shell_str( pz_scan_save = pz_scan, pArg,
          pz_scan = make_raw_shell_str( pz_scan_save = 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
           *  Make sure we don't overflow the buffer due to sloppy
           *  size estimation.
           *  size estimation.
           */
           */
          while (pz_scan == (char*)NULL)
          while (pz_scan == (char*)NULL)
            {
            {
              size_t already_filled = pz_scan_save - pz_cmd;
              size_t already_filled = pz_scan_save - pz_cmd;
              pz_cmd = xrealloc (pz_cmd, argsize += 100);
              pz_cmd = xrealloc (pz_cmd, argsize += 100);
              pz_scan_save = pz_scan = pz_cmd + already_filled;
              pz_scan_save = pz_scan = pz_cmd + already_filled;
              parg_size += 100;
              parg_size += 100;
              pz_scan = make_raw_shell_str( pz_scan, pArg,
              pz_scan = make_raw_shell_str( pz_scan, pArg,
                                            parg_size - (pz_scan - pz_cmd) );
                                            parg_size - (pz_scan - pz_cmd) );
            }
            }
        }
        }
 
 
      /*
      /*
       *  add the file machinations.
       *  add the file machinations.
       */
       */
#ifdef __MSDOS__
#ifdef __MSDOS__
      sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file );
      sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file );
#else
#else
      sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file,
      sprintf (pz_scan, z_cmd_fmt, pz_file_source, pz_temp_file,
               pz_temp_file, pz_temp_file, pz_temp_file);
               pz_temp_file, pz_temp_file, pz_temp_file);
#endif
#endif
    }
    }
  system( pz_cmd );
  system( pz_cmd );
  free( (void*)pz_cmd );
  free( (void*)pz_cmd );
}
}
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
    This loop should only cycle for 1/2 of one loop.
    This loop should only cycle for 1/2 of one loop.
    "chain_open" starts a process that uses "read_fd" as
    "chain_open" starts a process that uses "read_fd" as
    its stdin and returns the new fd this process will use
    its stdin and returns the new fd this process will use
    for stdout.  */
    for stdout.  */
 
 
#else /* is *NOT* SEPARATE_FIX_PROC */
#else /* is *NOT* SEPARATE_FIX_PROC */
static int
static int
start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
start_fixer (int read_fd, tFixDesc* p_fixd, char* pz_fix_file)
{
{
  tCC* pz_cmd_save;
  tCC* pz_cmd_save;
  char* pz_cmd;
  char* pz_cmd;
 
 
  if ((p_fixd->fd_flags & FD_SUBROUTINE) != 0)
  if ((p_fixd->fd_flags & FD_SUBROUTINE) != 0)
    return internal_fix (read_fd, p_fixd);
    return internal_fix (read_fd, p_fixd);
 
 
  if ((p_fixd->fd_flags & FD_SHELL_SCRIPT) == 0)
  if ((p_fixd->fd_flags & FD_SHELL_SCRIPT) == 0)
    {
    {
      pz_cmd = NULL;
      pz_cmd = NULL;
      pz_cmd_save = NULL;
      pz_cmd_save = NULL;
    }
    }
  else
  else
    {
    {
      tSCC z_cmd_fmt[] = "file='%s'\n%s";
      tSCC z_cmd_fmt[] = "file='%s'\n%s";
      pz_cmd = XNEWVEC (char, strlen (p_fixd->patch_args[2])
      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]);
      sprintf (pz_cmd, z_cmd_fmt, pz_fix_file, p_fixd->patch_args[2]);
      pz_cmd_save = p_fixd->patch_args[2];
      pz_cmd_save = p_fixd->patch_args[2];
      p_fixd->patch_args[2] = pz_cmd;
      p_fixd->patch_args[2] = pz_cmd;
    }
    }
 
 
  /*  Start a fix process, handing off the  previous read fd for its
  /*  Start a fix process, handing off the  previous read fd for its
      stdin and getting a new fd that reads from the fix process' stdout.
      stdin and getting a new fd that reads from the fix process' stdout.
      We normally will not loop, but we will up to 10 times if we keep
      We normally will not loop, but we will up to 10 times if we keep
      getting "EAGAIN" errors.
      getting "EAGAIN" errors.
 
 
      */
      */
  for (;;)
  for (;;)
    {
    {
      static int failCt = 0;
      static int failCt = 0;
      int fd;
      int fd;
 
 
      fd = chain_open (read_fd,
      fd = chain_open (read_fd,
                       (tCC **) p_fixd->patch_args,
                       (tCC **) p_fixd->patch_args,
                       (process_chain_head == -1)
                       (process_chain_head == -1)
                       ? &process_chain_head : (pid_t *) NULL);
                       ? &process_chain_head : (pid_t *) NULL);
 
 
      if (fd != -1)
      if (fd != -1)
        {
        {
          read_fd = fd;
          read_fd = fd;
          break;
          break;
        }
        }
 
 
      fprintf (stderr, z_fork_err, errno, xstrerror (errno),
      fprintf (stderr, z_fork_err, errno, xstrerror (errno),
               p_fixd->fix_name);
               p_fixd->fix_name);
 
 
      if ((errno != EAGAIN) || (++failCt > 10))
      if ((errno != EAGAIN) || (++failCt > 10))
        exit (EXIT_FAILURE);
        exit (EXIT_FAILURE);
      sleep (1);
      sleep (1);
    }
    }
 
 
  /*  IF we allocated a shell script command,
  /*  IF we allocated a shell script command,
      THEN free it and restore the command format to the fix description */
      THEN free it and restore the command format to the fix description */
  if (pz_cmd != (char*)NULL)
  if (pz_cmd != (char*)NULL)
    {
    {
      free ((void*)pz_cmd);
      free ((void*)pz_cmd);
      p_fixd->patch_args[2] = pz_cmd_save;
      p_fixd->patch_args[2] = pz_cmd_save;
    }
    }
 
 
  return read_fd;
  return read_fd;
}
}
#endif
#endif
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   Process the potential fixes for a particular include file.
   Process the potential fixes for a particular include file.
   Input:  the original text of the file and the file's name
   Input:  the original text of the file and the file's name
   Result: none.  A new file may or may not be created.  */
   Result: none.  A new file may or may not be created.  */
 
 
static t_bool
static t_bool
fix_applies (tFixDesc* p_fixd)
fix_applies (tFixDesc* p_fixd)
{
{
  const char *pz_fname = pz_curr_file;
  const char *pz_fname = pz_curr_file;
  const char *pz_scan = p_fixd->file_list;
  const char *pz_scan = p_fixd->file_list;
  int test_ct;
  int test_ct;
  tTestDesc *p_test;
  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.
   *  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
   *  I hope to nuke it anyway, it does not apply to DOS and it would
   *  be painful to implement.  Therefore, no "shell" fixes for DOS.
   *  be painful to implement.  Therefore, no "shell" fixes for DOS.
   */
   */
  if (p_fixd->fd_flags & (FD_SHELL_SCRIPT | FD_SKIP_TEST))
  if (p_fixd->fd_flags & (FD_SHELL_SCRIPT | FD_SKIP_TEST))
    return BOOL_FALSE;
    return BOOL_FALSE;
# else
# else
  if (p_fixd->fd_flags & FD_SKIP_TEST)
  if (p_fixd->fd_flags & FD_SKIP_TEST)
    return BOOL_FALSE;
    return BOOL_FALSE;
# endif
# endif
 
 
  /*  IF there is a file name restriction,
  /*  IF there is a file name restriction,
      THEN ensure the current file name matches one in the pattern  */
      THEN ensure the current file name matches one in the pattern  */
 
 
  if (pz_scan != (char *) NULL)
  if (pz_scan != (char *) NULL)
    {
    {
      size_t name_len;
      size_t name_len;
 
 
      while ((pz_fname[0] == '.') && (pz_fname[1] == '/'))
      while ((pz_fname[0] == '.') && (pz_fname[1] == '/'))
        pz_fname += 2;
        pz_fname += 2;
      name_len = strlen (pz_fname);
      name_len = strlen (pz_fname);
 
 
      for (;;)
      for (;;)
        {
        {
          pz_scan = strstr (pz_scan + 1, pz_fname);
          pz_scan = strstr (pz_scan + 1, pz_fname);
          /*  IF we can't match the string at all,
          /*  IF we can't match the string at all,
              THEN bail  */
              THEN bail  */
          if (pz_scan == (char *) NULL)
          if (pz_scan == (char *) NULL)
            return BOOL_FALSE;
            return BOOL_FALSE;
 
 
          /*  IF the match is surrounded by the '|' markers,
          /*  IF the match is surrounded by the '|' markers,
              THEN we found a full match -- time to run the tests  */
              THEN we found a full match -- time to run the tests  */
 
 
          if ((pz_scan[-1] == '|') && (pz_scan[name_len] == '|'))
          if ((pz_scan[-1] == '|') && (pz_scan[name_len] == '|'))
            break;
            break;
        }
        }
    }
    }
 
 
  /*  FOR each test, see if it fails.
  /*  FOR each test, see if it fails.
      IF it does fail, then we go on to the next test */
      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;
  for (p_test = p_fixd->p_test_desc, test_ct = p_fixd->test_ct;
       test_ct-- > 0;
       test_ct-- > 0;
       p_test++)
       p_test++)
    {
    {
      switch (p_test->type)
      switch (p_test->type)
        {
        {
        case TT_TEST:
        case TT_TEST:
          if (test_test (p_test, pz_curr_file) != APPLY_FIX) {
          if (test_test (p_test, pz_curr_file) != APPLY_FIX) {
#ifdef DEBUG
#ifdef DEBUG
            if (VLEVEL( VERB_EVERYTHING ))
            if (VLEVEL( VERB_EVERYTHING ))
              fprintf (stderr, z_failed, "TEST", p_fixd->fix_name,
              fprintf (stderr, z_failed, "TEST", p_fixd->fix_name,
                       pz_fname, p_fixd->test_ct - test_ct);
                       pz_fname, p_fixd->test_ct - test_ct);
#endif
#endif
            return BOOL_FALSE;
            return BOOL_FALSE;
          }
          }
          break;
          break;
 
 
        case TT_EGREP:
        case TT_EGREP:
          if (egrep_test (pz_curr_data, p_test) != APPLY_FIX) {
          if (egrep_test (pz_curr_data, p_test) != APPLY_FIX) {
#ifdef DEBUG
#ifdef DEBUG
            if (VLEVEL( VERB_EVERYTHING ))
            if (VLEVEL( VERB_EVERYTHING ))
              fprintf (stderr, z_failed, "EGREP", p_fixd->fix_name,
              fprintf (stderr, z_failed, "EGREP", p_fixd->fix_name,
                       pz_fname, p_fixd->test_ct - test_ct);
                       pz_fname, p_fixd->test_ct - test_ct);
#endif
#endif
            return BOOL_FALSE;
            return BOOL_FALSE;
          }
          }
          break;
          break;
 
 
        case TT_NEGREP:
        case TT_NEGREP:
          if (egrep_test (pz_curr_data, p_test) == APPLY_FIX) {
          if (egrep_test (pz_curr_data, p_test) == APPLY_FIX) {
#ifdef DEBUG
#ifdef DEBUG
            if (VLEVEL( VERB_EVERYTHING ))
            if (VLEVEL( VERB_EVERYTHING ))
              fprintf (stderr, z_failed, "NEGREP", p_fixd->fix_name,
              fprintf (stderr, z_failed, "NEGREP", p_fixd->fix_name,
                       pz_fname, p_fixd->test_ct - test_ct);
                       pz_fname, p_fixd->test_ct - test_ct);
#endif
#endif
            /*  Negated sense  */
            /*  Negated sense  */
            return BOOL_FALSE;
            return BOOL_FALSE;
          }
          }
          break;
          break;
 
 
        case TT_FUNCTION:
        case TT_FUNCTION:
          if (run_test (p_test->pz_test_text, pz_curr_file, pz_curr_data)
          if (run_test (p_test->pz_test_text, pz_curr_file, pz_curr_data)
              != APPLY_FIX) {
              != APPLY_FIX) {
#ifdef DEBUG
#ifdef DEBUG
            if (VLEVEL( VERB_EVERYTHING ))
            if (VLEVEL( VERB_EVERYTHING ))
              fprintf (stderr, z_failed, "FTEST", p_fixd->fix_name,
              fprintf (stderr, z_failed, "FTEST", p_fixd->fix_name,
                       pz_fname, p_fixd->test_ct - test_ct);
                       pz_fname, p_fixd->test_ct - test_ct);
#endif
#endif
            return BOOL_FALSE;
            return BOOL_FALSE;
          }
          }
          break;
          break;
        }
        }
    }
    }
 
 
  return BOOL_TRUE;
  return BOOL_TRUE;
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   Write out a replacement file  */
   Write out a replacement file  */
 
 
static void
static void
write_replacement (tFixDesc* p_fixd)
write_replacement (tFixDesc* p_fixd)
{
{
   const char* pz_text = p_fixd->patch_args[0];
   const char* pz_text = p_fixd->patch_args[0];
 
 
   if ((pz_text == (char*)NULL) || (*pz_text == NUL))
   if ((pz_text == (char*)NULL) || (*pz_text == NUL))
     return;
     return;
 
 
   {
   {
     FILE* out_fp = create_file ();
     FILE* out_fp = create_file ();
     size_t sz = strlen (pz_text);
     size_t sz = strlen (pz_text);
     fwrite (pz_text, sz, 1, out_fp);
     fwrite (pz_text, sz, 1, out_fp);
     if (pz_text[ sz-1 ] != '\n')
     if (pz_text[ sz-1 ] != '\n')
       fputc ('\n', out_fp);
       fputc ('\n', out_fp);
     fclose (out_fp);
     fclose (out_fp);
   }
   }
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
    We have work to do.  Read back in the output
    We have work to do.  Read back in the output
    of the filtering chain.  Compare each byte as we read it with
    of the filtering chain.  Compare each byte as we read it with
    the contents of the original file.  As soon as we find any
    the contents of the original file.  As soon as we find any
    difference, we will create the output file, write out all
    difference, we will create the output file, write out all
    the matched text and then copy any remaining data from the
    the matched text and then copy any remaining data from the
    output of the filter chain.
    output of the filter chain.
    */
    */
static void
static void
test_for_changes (int read_fd)
test_for_changes (int read_fd)
{
{
  FILE *in_fp = fdopen (read_fd, "r");
  FILE *in_fp = fdopen (read_fd, "r");
  FILE *out_fp = (FILE *) NULL;
  FILE *out_fp = (FILE *) NULL;
  unsigned char *pz_cmp = (unsigned char*)pz_curr_data;
  unsigned char *pz_cmp = (unsigned char*)pz_curr_data;
 
 
#ifdef DO_STATS
#ifdef DO_STATS
  fixed_ct++;
  fixed_ct++;
#endif
#endif
  for (;;)
  for (;;)
    {
    {
      int ch;
      int ch;
 
 
      ch = getc (in_fp);
      ch = getc (in_fp);
      if (ch == EOF)
      if (ch == EOF)
        break;
        break;
      ch &= 0xFF; /* all bytes are 8 bits */
      ch &= 0xFF; /* all bytes are 8 bits */
 
 
      /*  IF we are emitting the output
      /*  IF we are emitting the output
          THEN emit this character, too.
          THEN emit this character, too.
      */
      */
      if (out_fp != (FILE *) NULL)
      if (out_fp != (FILE *) NULL)
        putc (ch, out_fp);
        putc (ch, out_fp);
 
 
      /*  ELSE if this character does not match the original,
      /*  ELSE if this character does not match the original,
          THEN now is the time to start the output.
          THEN now is the time to start the output.
      */
      */
      else if (ch != *pz_cmp)
      else if (ch != *pz_cmp)
        {
        {
          out_fp = create_file ();
          out_fp = create_file ();
 
 
#ifdef DO_STATS
#ifdef DO_STATS
          altered_ct++;
          altered_ct++;
#endif
#endif
          /*  IF there are matched data, write the matched part now. */
          /*  IF there are matched data, write the matched part now. */
          if ((char*)pz_cmp != pz_curr_data)
          if ((char*)pz_cmp != pz_curr_data)
            fwrite (pz_curr_data, (size_t)((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 */
          /*  Emit the current unmatching character */
          putc (ch, out_fp);
          putc (ch, out_fp);
        }
        }
      else
      else
        /*  ELSE the character matches.  Advance the compare ptr */
        /*  ELSE the character matches.  Advance the compare ptr */
        pz_cmp++;
        pz_cmp++;
    }
    }
 
 
  /*  IF we created the output file, ... */
  /*  IF we created the output file, ... */
  if (out_fp != (FILE *) NULL)
  if (out_fp != (FILE *) NULL)
    {
    {
      regmatch_t match;
      regmatch_t match;
 
 
      /* Close the file and see if we have to worry about
      /* Close the file and see if we have to worry about
         `#include "file.h"' constructs.  */
         `#include "file.h"' constructs.  */
      fclose (out_fp);
      fclose (out_fp);
      if (xregexec (&incl_quote_re, pz_curr_data, 1, &match, 0) == 0)
      if (xregexec (&incl_quote_re, pz_curr_data, 1, &match, 0) == 0)
        extract_quoted_files (pz_curr_data, pz_curr_file, &match);
        extract_quoted_files (pz_curr_data, pz_curr_file, &match);
    }
    }
 
 
  fclose (in_fp);
  fclose (in_fp);
  close (read_fd);  /* probably redundant, but I'm paranoid */
  close (read_fd);  /* probably redundant, but I'm paranoid */
}
}
 
 
 
 
/* * * * * * * * * * * * *
/* * * * * * * * * * * * *
 
 
   Process the potential fixes for a particular include file.
   Process the potential fixes for a particular include file.
   Input:  the original text of the file and the file's name
   Input:  the original text of the file and the file's name
   Result: none.  A new file may or may not be created.  */
   Result: none.  A new file may or may not be created.  */
 
 
void
void
process (void)
process (void)
{
{
  tFixDesc *p_fixd = fixDescList;
  tFixDesc *p_fixd = fixDescList;
  int todo_ct = FIX_COUNT;
  int todo_ct = FIX_COUNT;
  int read_fd = -1;
  int read_fd = -1;
# ifndef SEPARATE_FIX_PROC
# ifndef SEPARATE_FIX_PROC
  int num_children = 0;
  int num_children = 0;
# else /* is SEPARATE_FIX_PROC */
# else /* is SEPARATE_FIX_PROC */
  char* pz_file_source = pz_curr_file;
  char* pz_file_source = pz_curr_file;
# endif
# endif
 
 
  if (access (pz_curr_file, R_OK) != 0)
  if (access (pz_curr_file, R_OK) != 0)
    {
    {
      int erno = errno;
      int erno = errno;
      fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n",
      fprintf (stderr, "Cannot access %s from %s\n\terror %d (%s)\n",
               pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN),
               pz_curr_file, getcwd ((char *) NULL, MAXPATHLEN),
               erno, xstrerror (erno));
               erno, xstrerror (erno));
      return;
      return;
    }
    }
 
 
  pz_curr_data = load_file (pz_curr_file);
  pz_curr_data = load_file (pz_curr_file);
  if (pz_curr_data == (char *) NULL)
  if (pz_curr_data == (char *) NULL)
    return;
    return;
 
 
#ifdef DO_STATS
#ifdef DO_STATS
  process_ct++;
  process_ct++;
#endif
#endif
  if (VLEVEL( VERB_PROGRESS ) && have_tty)
  if (VLEVEL( VERB_PROGRESS ) && have_tty)
    fprintf (stderr, "%6lu %-50s   \r",
    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
# ifndef SEPARATE_FIX_PROC
  process_chain_head = NOPROCESS;
  process_chain_head = NOPROCESS;
 
 
  /* For every fix in our fix list, ...  */
  /* For every fix in our fix list, ...  */
  for (; todo_ct > 0; p_fixd++, todo_ct--)
  for (; todo_ct > 0; p_fixd++, todo_ct--)
    {
    {
      if (! fix_applies (p_fixd))
      if (! fix_applies (p_fixd))
        continue;
        continue;
 
 
      if (VLEVEL( VERB_APPLIES ))
      if (VLEVEL( VERB_APPLIES ))
        fprintf (stderr, "Applying %-24s to %s\n",
        fprintf (stderr, "Applying %-24s to %s\n",
                 p_fixd->fix_name, pz_curr_file);
                 p_fixd->fix_name, pz_curr_file);
 
 
      if (p_fixd->fd_flags & FD_REPLACEMENT)
      if (p_fixd->fd_flags & FD_REPLACEMENT)
        {
        {
          write_replacement (p_fixd);
          write_replacement (p_fixd);
          UNLOAD_DATA();
          UNLOAD_DATA();
          return;
          return;
        }
        }
 
 
      /*  IF we do not have a read pointer,
      /*  IF we do not have a read pointer,
          THEN this is the first fix for the current file.
          THEN this is the first fix for the current file.
          Open the source file.  That will be used as stdin for
          Open the source file.  That will be used as stdin for
          the first fix.  Any subsequent fixes will use the
          the first fix.  Any subsequent fixes will use the
          stdout descriptor of the previous fix for its stdin.  */
          stdout descriptor of the previous fix for its stdin.  */
 
 
      if (read_fd == -1)
      if (read_fd == -1)
        {
        {
          read_fd = open (pz_curr_file, O_RDONLY);
          read_fd = open (pz_curr_file, O_RDONLY);
          if (read_fd < 0)
          if (read_fd < 0)
            {
            {
              fprintf (stderr, "Error %d (%s) opening %s\n", errno,
              fprintf (stderr, "Error %d (%s) opening %s\n", errno,
                       xstrerror (errno), pz_curr_file);
                       xstrerror (errno), pz_curr_file);
              exit (EXIT_FAILURE);
              exit (EXIT_FAILURE);
            }
            }
 
 
          /*  Ensure we do not get duplicate output */
          /*  Ensure we do not get duplicate output */
 
 
          fflush (stdout);
          fflush (stdout);
        }
        }
 
 
      read_fd = start_fixer (read_fd, p_fixd, pz_curr_file);
      read_fd = start_fixer (read_fd, p_fixd, pz_curr_file);
      num_children++;
      num_children++;
    }
    }
 
 
  /*  IF we have a read-back file descriptor,
  /*  IF we have a read-back file descriptor,
      THEN check for changes and write output if changed.   */
      THEN check for changes and write output if changed.   */
 
 
  if (read_fd >= 0)
  if (read_fd >= 0)
    {
    {
      test_for_changes (read_fd);
      test_for_changes (read_fd);
#ifdef DO_STATS
#ifdef DO_STATS
      apply_ct += num_children;
      apply_ct += num_children;
#endif
#endif
      /* Wait for child processes created by chain_open()
      /* Wait for child processes created by chain_open()
         to avoid leaving zombies.  */
         to avoid leaving zombies.  */
      do  {
      do  {
        wait ((int *) NULL);
        wait ((int *) NULL);
      } while (--num_children > 0);
      } while (--num_children > 0);
    }
    }
 
 
# else /* is SEPARATE_FIX_PROC */
# else /* is SEPARATE_FIX_PROC */
 
 
  for (; todo_ct > 0; p_fixd++, todo_ct--)
  for (; todo_ct > 0; p_fixd++, todo_ct--)
    {
    {
      if (! fix_applies (p_fixd))
      if (! fix_applies (p_fixd))
        continue;
        continue;
 
 
      if (VLEVEL( VERB_APPLIES ))
      if (VLEVEL( VERB_APPLIES ))
        fprintf (stderr, "Applying %-24s to %s\n",
        fprintf (stderr, "Applying %-24s to %s\n",
                 p_fixd->fix_name, pz_curr_file);
                 p_fixd->fix_name, pz_curr_file);
 
 
      if (p_fixd->fd_flags & FD_REPLACEMENT)
      if (p_fixd->fd_flags & FD_REPLACEMENT)
        {
        {
          write_replacement (p_fixd);
          write_replacement (p_fixd);
          UNLOAD_DATA();
          UNLOAD_DATA();
          return;
          return;
        }
        }
      fix_with_system (p_fixd, pz_curr_file, pz_file_source, pz_temp_file);
      fix_with_system (p_fixd, pz_curr_file, pz_file_source, pz_temp_file);
      pz_file_source = pz_temp_file;
      pz_file_source = pz_temp_file;
    }
    }
 
 
  read_fd = open (pz_temp_file, O_RDONLY);
  read_fd = open (pz_temp_file, O_RDONLY);
  if (read_fd < 0)
  if (read_fd < 0)
    {
    {
      if (errno != ENOENT)
      if (errno != ENOENT)
        fprintf (stderr, "error %d (%s) opening output (%s) for read\n",
        fprintf (stderr, "error %d (%s) opening output (%s) for read\n",
                 errno, xstrerror (errno), pz_temp_file);
                 errno, xstrerror (errno), pz_temp_file);
    }
    }
  else
  else
    {
    {
      test_for_changes (read_fd);
      test_for_changes (read_fd);
      /* Unlinking a file while it is still open is a Bad Idea on
      /* Unlinking a file while it is still open is a Bad Idea on
         DOS/Windows.  */
         DOS/Windows.  */
      close (read_fd);
      close (read_fd);
      unlink (pz_temp_file);
      unlink (pz_temp_file);
    }
    }
 
 
# endif
# endif
  UNLOAD_DATA();
  UNLOAD_DATA();
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.