⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 WinMerge可以显示两个文件的不同之处
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Support routines for GNU DIFF.
   Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.

This file is part of GNU DIFF.

GNU DIFF 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)
any later version.

GNU DIFF is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU DIFF; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <windows.h>
#include "diff.h"

// reduce some noise produced with the MSVC compiler
#if defined (_AFXDLL)
#pragma warning(disable : 4131 4127)
#endif


/* Queue up one-line messages to be printed at the end,
   when -l is specified.  Each message is recorded with a `struct msg'.  */

struct msg
{
  struct msg *next;
  char const *format;
  char const *arg1;
  char const *arg2;
  char const *arg3;
  char const *arg4;
};

/* Head of the chain of queues messages.  */

static struct msg *msg_chain;

/* Tail of the chain of queues messages.  */

static struct msg **msg_chain_end = &msg_chain;

/* Use when a system call returns non-zero status.
   TEXT should normally be the file name.  */

void
perror_with_name (text)
     char const *text;
{
  int e = errno;
  fprintf (stderr, "%s: ", program);
  errno = e;
  perror (text);
}

/* Use when a system call returns non-zero status and that is fatal.  */

void
pfatal_with_name (text)
     char const *text;
{
  int e = errno;
  print_message_queue ();
  fprintf (stderr, "%s: ", program);
  errno = e;
  perror (text);
  //exit (2);
  RaiseException(STATUS_ACCESS_VIOLATION, 0, 0, NULL);
}

/* Print an error message from the format-string FORMAT
   with args ARG1 and ARG2.  */

void
error (format, arg, arg1)
     char const *format, *arg, *arg1;
{
  fprintf (stderr, "%s: ", program);
  fprintf (stderr, format, arg, arg1);
  fprintf (stderr, "\n");
}

/* Print an error message containing the string TEXT, then exit.  */

void
fatal (m)
     char const *m;
{
  print_message_queue ();
  error ("%s", m, 0);
  //exit (2);
  RaiseException(STATUS_ACCESS_VIOLATION, 0, 0, NULL);
}

/* Like printf, except if -l in effect then save the message and print later.
   This is used for things like "binary files differ" and "Only in ...".  */

void
message (format, arg1, arg2)
     char const *format, *arg1, *arg2;
{
  message5 (format, arg1, arg2, 0, 0);
}

void
message5 (format, arg1, arg2, arg3, arg4)
     char const *format, *arg1, *arg2, *arg3, *arg4;
{
  if (paginate_flag)
    {
      struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
      new->format = format;
      new->arg1 = concat (arg1, "", "");
      new->arg2 = concat (arg2, "", "");
      new->arg3 = arg3 ? concat (arg3, "", "") : 0;
      new->arg4 = arg4 ? concat (arg4, "", "") : 0;
      new->next = 0;
      *msg_chain_end = new;
      msg_chain_end = &new->next;
    }
  else
    {
      if (sdiff_help_sdiff)
	putchar (' ');
      printf (format, arg1, arg2, arg3, arg4);
    }
}

/* Output all the messages that were saved up by calls to `message'.  */

void
print_message_queue ()
{
  struct msg *m;

  for (m = msg_chain; m; m = m->next)
    printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);
}

/* Call before outputting the results of comparing files NAME0 and NAME1
   to set up OUTFILE, the stdio stream for the output to go to.

   Usually, OUTFILE is just stdout.  But when -l was specified
   we fork off a `pr' and make OUTFILE a pipe to it.
   `pr' then outputs to our stdout.  */

static char const *current_name0;
static char const *current_name1;
static int current_depth;

void
setup_output (name0, name1, depth)
     char const *name0, *name1;
     int depth;
{
  current_name0 = name0;
  current_name1 = name1;
  current_depth = depth;
  outfile = 0;
}

static pid_t pr_pid;

void
begin_output ()
{
  char *name;

  if (outfile != 0)
    return;

  /* Construct the header of this piece of diff.  */
  name = xmalloc (strlen (current_name0) + strlen (current_name1)
		  + strlen (switch_string) + 7);
  /* Posix.2 section 4.17.6.1.1 specifies this format.  But there are some
     bugs in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
     it says that we must print only the last component of the pathnames,
     and it requires two spaces after "diff" if there are no options.
     These requirements are silly and do not match historical practice.  */
  sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);

  if (paginate_flag)
    {
#if defined(__MSDOS__) || defined(__NT__) || defined(WIN32)
      char command[120];

      sprintf(command, "%s -f -h \"%s\"", PR_FILE_NAME, name);
      if ((outfile = popen(command, "w")) == NULL)
        pfatal_with_name ("popen");
#else
      int pipes[2];

      /* Fork a `pr' and make OUTFILE a pipe to it.  */
      if (pipe (pipes) < 0)
	pfatal_with_name ("pipe");

      fflush (stdout);

      pr_pid = vfork ();
      if (pr_pid < 0)
	pfatal_with_name ("vfork");

      if (pr_pid == 0)
	{
	  close (pipes[1]);
	  if (pipes[0] != STDIN_FILENO)
	    {
	      if (dup2 (pipes[0], STDIN_FILENO) < 0)
		pfatal_with_name ("dup2");
	      close (pipes[0]);
	    }

	  execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0);
	  pfatal_with_name (PR_FILE_NAME);
	}
      else
	{
	  close (pipes[0]);
	  outfile = fdopen (pipes[1], "w");
	}
#endif /*__MSDOS__||__NT__*/
    }
  else
    {

      /* If -l was not specified, output the diff straight to `stdout'.  */

      outfile = stdout;

      /* If handling multiple files (because scanning a directory),
	 print which files the following output is about.  */
      if (current_depth > 0)
	printf ("%s\n", name);
    }

  free (name);

  /* A special header is needed at the beginning of context output.  */
  switch (output_style)
    {
    case OUTPUT_CONTEXT:
      print_context_header (files, 0);
      break;

    case OUTPUT_UNIFIED:
      print_context_header (files, 1);
      break;

    default:
      break;
    }
}

/* Call after the end of output of diffs for one file.
   Close OUTFILE and get rid of the `pr' subfork.  */

void
finish_output ()
{
  if (outfile != 0 && outfile != stdout)
    {
#if defined(__MSDOS__) || defined(__NT__) || defined(WIN32)
      if (pclose (outfile))
	pfatal_with_name ("write error");
#else
      int wstatus;
      if (ferror (outfile))
	fatal ("write error");
      if (fclose (outfile) != 0)
	pfatal_with_name ("write error");
#if HAVE_WAITPID
      if (waitpid (pr_pid, &wstatus, 0) < 0)
	pfatal_with_name ("waitpid");
#else
      for (;;) {
	pid_t w = wait (&wstatus);
	if (w < 0)
	  pfatal_with_name ("wait");
	if (w == pr_pid)
	  break;
      }
#endif
      if (! WIFEXITED (wstatus) || WEXITSTATUS (wstatus) != 0)
	fatal ("subsidiary pr failed");
#endif /*__MSDOS__||__NT__*/
    }

  outfile = 0;
}


static int
ISWSPACE (char ch)
{
	return ch==' ' || ch=='\t';
}

/* Compare two lines (typically one from each input file)
   according to the command line options.
   Return 1 if the lines differ, like `memcmp'.  */

int
line_cmp (s1, len1, s2, len2)
     char const HUGE *s1, HUGE *s2;
     size_t len1, len2;
{
  register unsigned char const *t1, *t2;
  register unsigned char end_char = line_end_char;

  /* Check first for exact identity.
     If that is true, return 0 immediately.
     This detects the common case of exact identity
     faster than complete comparison would.  */

  if (len1 == len2 && memcmp (s1, s2, len1) == 0)
    return 0;

  /* Not exactly identical, but perhaps they match anyway
     when case or white space is ignored.  */
  /* c1 is the current character value for the line s1, it is set to 0
     when the line has been entirely scanned.
     c2 is the equivalent of c1 for the line s2 */

  if (ignore_case_flag | ignore_space_change_flag | ignore_all_space_flag | ignore_eol_diff)
    {
      t1 = (unsigned char const *) s1;
      t2 = (unsigned char const *) s2;

      while (1)
	{
	  register unsigned char c1;
	  register unsigned char c2;
	  if (t1-s1<(int)len1)
	    c1 = *t1++;
	  else
	    c1 = 0;
	  if (t2-s2<(int)len2)
	    c2 = *t2++;
	  else
	    c2 = 0;

      /* Test for exact char equality first, since it's a common case.  */
      if (c1 != c2)
	{
	  /* Ignore horizontal white space if -b or -w is specified.  */

	  if (ignore_all_space_flag)
	    {
	      /* For -w, just skip past any white space.  */
	      while (ISWSPACE (c1))
		{
		  if (t1-s1<(int)len1)
		    {
		      c1 = *t1++;
		    }
		  else
		    {
		      c1 = 0;
		      break;
		    }
		}
	      while (ISWSPACE (c2))
		{
		  if (t2-s2<(int)len2)
		    {
		      c2 = *t2++;
		    }
		  else
		    {
		      c2 = 0;
		      break;
		    }
		}
	    }
	  else if (ignore_space_change_flag)
	    {
	      /* For -b, advance past any sequence of white space in line 1
		 and consider it just one Space, or nothing at all
		 if it is at the end of the line.  */
	      if (ISWSPACE (c1))
		{
		  /* Any whitespace sequence counts as one space */
		  c1 = ' ';
		  /* Skip to end of whitespace sequence */
		  while (t1-s1<(int)len1 && ISWSPACE(*t1))
		    ++t1;
		  /* if c1 is whitespace and c2 is end of line
		  we must advance c1 to next char, because c1
		  whitespace matches the nothing in c2 */
		  if (c2=='\r' || c2=='\n')
		    {
		      if (t1-s1<(int)len1)
			c1 = *t1++;
		      else
			c1 = 0;
		    }
		}

	      /* Likewise for line 2.  */
	      if (ISWSPACE (c2))
		{
		  /* Any whitespace sequence counts as one space */
		  c2 = ' ';
		  /* Skip to end of whitespace sequence */
		  while (t2-s2<(int)len2 && ISWSPACE(*t2))
		    ++t2;
		  /* if c2 is whitespace and c1 is end of line
		  we must advance c1 to next char, because c2
		  whitespace matches the nothing in c1 */
		  if (c1=='\r' || c1=='\n')
		    {
		      if (t2-s2<(int)len2)
			c2 = *t2++;
		      else
			c2 = 0;
		    }
		}

	      /*
	      Whitespace at end of line matches end of file
	      make them both say ' ' so they match, and the one
	      not at end will get advanced
	      */
	      if (c1 != c2)
	        {
		  if (c1==' ' && !c2)
		    c2 = ' ';
		  else if (c2==' ' && !c1)
		    c1 = ' ';
	        }

	      if (c1 != c2)
		{
		  /* backtracking necessary when matching "cat and" against "cat  and"
		     because the spaces got matched with equality, so now "a" and " "
		     are mismatches */

		  /* If we went too far when doing the simple test
		     for equality, go back to the first non-white-space
		     character in both sides and try again.  */
		  if (c2 == ' ' && c1 && c1 != '\n' && c1 != '\r'
		      && (unsigned char const *) s1 + 1 < t1
		      && isspace(t1[-2]))
		    {
		      --t1;
		      continue;
		    }
		  if (c1 == ' ' && c2 && c2 != '\n' && c2 != '\r'
		      && (unsigned char const *) s2 + 1 < t2
		      && isspace(t2[-2]))
		    {
		      --t2;
		      continue;
		    }
		}
	    }

	  /* Upcase all letters if -i is specified.  */

	  if (ignore_case_flag)
	    {
	      if (islower (c1))
		c1 = (unsigned char)toupper (c1);
	      if (islower (c2))
		c2 = (unsigned char)toupper (c2);
	    }

	  if (ignore_eol_diff)
	    {
	      if (c1 == '\r')
		c1 = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -