示例#1
0
void print_worddiff(const char * const text1, const char * const text2, std::string &ret)
{
	std::vector<Word> text1_words, text2_words;

	split_tokens(text1, text1_words);
	split_tokens(text2, text2_words);
	std::vector<diff_op<Word> > worddiff = do_diff(text1_words, text2_words);
	
	// print twice; first for left side, then for right side
	ret += "<tr>"
		"<td>−</td>"
		"<td class=\"diff-deletedline\">";
	print_worddiff_side(worddiff, false, ret);
	ret += "</td>"
		"<td>+</td>"
		"<td class=\"diff-addedline\">";
	print_worddiff_side(worddiff, true, ret);
	ret += "</td>"
		"</tr>";
}
示例#2
0
文件: diff.c 项目: lysine/wasp
int main(int argc, const char *argv[])
{
  apr_pool_t *pool;
  svn_stream_t *ostream;
  svn_error_t *svn_err;
  svn_boolean_t has_changes;
  svn_diff_file_options_t *diff_options;
  apr_array_header_t *options_array;
  int i;
  const char *from = NULL;
  const char *to = NULL;
  svn_boolean_t show_c_function = FALSE;
  svn_boolean_t no_more_options = FALSE;

  apr_initialize();
  atexit(apr_terminate);

  pool = svn_pool_create(NULL);

  svn_err = svn_stream_for_stdout(&ostream, pool);
  if (svn_err)
    {
      svn_handle_error2(svn_err, stdout, FALSE, "diff: ");
      return 2;
    }

  options_array = apr_array_make(pool, 0, sizeof(const char *));

  diff_options = svn_diff_file_options_create(pool);

  for (i = 1 ; i < argc ; i++)
    {
      if (!no_more_options && (argv[i][0] == '-'))
        {
          /* Special case: '--' means "no more options follow" */
          if (argv[i][1] == '-' && !argv[i][2])
            {
              no_more_options = TRUE;
              continue;
            }
          /* Special case: we need to detect '-p' and handle it specially */
          if (argv[i][1] == 'p' && !argv[i][2])
            {
              show_c_function = TRUE;
              continue;
            }
          if (argv[i][1] == 'w' && !argv[i][2])
            {
              diff_options->ignore_space = svn_diff_file_ignore_space_all;
              continue;
            }

          APR_ARRAY_PUSH(options_array, const char *) = argv[i];

          /* Special case: '-U' takes an argument, so capture the
           * next argument in the array. */
          if (argv[i][1] == 'U' && !argv[i][2])
            {
              i++;
              APR_ARRAY_PUSH(options_array, const char *) = argv[i];
            }
        }
      else
        {
          if (from == NULL)
            from = argv[i];
          else if (to == NULL)
            to = argv[i];
          else
            {
              print_usage(ostream, argv[0], pool);
              return 2;
            }
        }
    }

  if (!from || !to)
    {
      print_usage(ostream, argv[0], pool);
      return 2;
    }

  svn_err = svn_diff_file_options_parse(diff_options, options_array, pool);
  if (svn_err)
    {
      svn_handle_error2(svn_err, stdout, FALSE, "diff: ");
      return 2;
    }

  svn_err = do_diff(ostream, from, to, &has_changes,
                    diff_options, show_c_function, pool);
  if (svn_err)
    {
      svn_handle_error2(svn_err, stdout, FALSE, "diff: ");
      return 2;
    }

  return has_changes ? 1 : 0;
}
示例#3
0
int main(int argc, char *argv[])
{
	int opt;
	int option_index;
	int mode = 0;
	int obj = 0;
	int replace = 0;
	int which = 0;
	int ispatch = 0;
	int reverse = 0;
	int verbose = 0, quiet = 0;
	int strip = -1;
	int exit_status = 0;
	int ignore = 1;
	int show_wiggles = 0;
	char *helpmsg;
	char *trace;
	int selftest = 0;

	trace = getenv("WIGGLE_TRACE");
	if (trace && *trace)
		do_trace = 1;

	while ((opt = getopt_long(argc, argv,
				  short_options, long_options,
				  &option_index)) != -1)
		switch (opt) {
		case 'h':
			helpmsg = Help;
			switch (mode) {
			case 'x':
				helpmsg = HelpExtract;
				break;
			case 'd':
				helpmsg = HelpDiff;
				break;
			case 'm':
				helpmsg = HelpMerge;
				break;
			case 'B':
				helpmsg = HelpBrowse;
				break;
			}
			fputs(helpmsg, stderr);
			exit(0);

		case 'V':
			fputs(Version, stderr);
			exit(0);
		case ':':
		case '?':
		default:
			fputs(Usage, stderr);
			exit(2);

		case 'B':
		case 'x':
		case 'd':
		case 'm':
			if (mode == 0) {
				mode = opt;
				continue;
			}
			fprintf(stderr,
				"%s: mode is '%c' - cannot set to '%c'\n",
				Cmd, mode, opt);
			exit(2);

		case 'w':
		case 'l':
			if (obj == 0 || obj == opt) {
				obj = opt;
				continue;
			}
			fprintf(stderr,
				"%s: cannot select both words and lines.\n", Cmd);
			exit(2);

		case 'r':
			replace = 1;
			continue;
		case 'R':
			reverse = 1;
			continue;

		case 'i':
			ignore = 0;
			continue;
		case 'W':
			show_wiggles = 1;
			ignore = 0;
			continue;

		case '1':
		case '2':
		case '3':
			if (which == 0 || which == opt) {
				which = opt;
				continue;
			}
			fprintf(stderr,
				"%s: can only select one of -1, -2, -3\n", Cmd);
			exit(2);

		case 'p': /* 'patch' or 'strip' */
			if (optarg)
				strip = atol(optarg);
			ispatch = 1;
			continue;

		case 'v':
			verbose++;
			continue;
		case 'q':
			quiet = 1;
			continue;

		case SELF_TEST:
			selftest = 1;
			continue;
		}
	if (!mode)
		mode = 'm';

	if (mode == 'B') {
		vpatch(argc-optind, argv+optind, ispatch,
		       strip, reverse, replace, selftest);
		/* should not return */
		exit(1);
	}

	if (obj && mode == 'x') {
		fprintf(stderr,
			"%s: cannot specify --line or --word with --extract\n",
			Cmd);
		exit(2);
	}
	if (mode != 'm' && !obj)
		obj = 'w';
	if (replace && mode != 'm') {
		fprintf(stderr,
			"%s: --replace only allowed with --merge\n", Cmd);
		exit(2);
	}
	if (mode == 'x' && !which) {
		fprintf(stderr,
			"%s: must specify -1, -2 or -3 with --extract\n", Cmd);
		exit(2);
	}
	if (mode != 'x' && mode != 'd' && which) {
		fprintf(stderr,
			"%s: -1, -2 or -3 only allowed with --extract or --diff\n",
			Cmd);
		exit(2);
	}

	if (ispatch && which == '3') {
		fprintf(stderr,
			"%s: cannot extract -3 from a patch.\n", Cmd);
		exit(2);
	}

	switch (mode) {
	case 'x':
		exit_status = extract(argc-optind, argv+optind, ispatch, which);
		break;
	case 'd':
		exit_status = do_diff(argc-optind, argv+optind, obj, ispatch, which, reverse);
		break;
	case 'm':
		if (ispatch)
			exit_status = multi_merge(argc-optind,
						  argv+optind, obj,
						  reverse, ignore,
						  show_wiggles,
						  replace, strip,
						  quiet);
		else
			exit_status = do_merge(argc-optind, argv+optind, obj, reverse, replace,
					       ignore, show_wiggles, quiet);
		break;
	}
	exit(exit_status);
}
示例#4
0
static int filterdiff (FILE *f, const char *patchname)
{
	static unsigned long linenum = 1;
	char *names[2];
	char *header[MAX_HEADERS] = { NULL, NULL };
        unsigned int num_headers = 0;
	char *line = NULL;
	size_t linelen = 0;
	char *p;
	const char *p_stripped;
	int match;
	int i;

	if (getline (&line, &linelen, f) == -1)
		return 0;

	for (;;) {
		char status = '!';
		unsigned long start_linenum;
		int orig_file_exists, new_file_exists;
		int is_context = -1;
		int result;
		int (*do_diff) (FILE *, char **, unsigned int,
                                int, char **, size_t *,
				unsigned long *, unsigned long,
				char, const char *, const char *,
				int *, int *);

		orig_file_exists = 0; // shut gcc up

		// Search for start of patch ("diff ", or "--- " for
		// unified diff, "*** " for context).
		for (;;) {
                        if (!strncmp (line, "diff ", 5))
                                break;

			if (!strncmp (line, "--- ", 4)) {
				is_context = 0;
				break;
			}

			if (!strncmp (line, "*** ", 4)) {
				is_context = 1;
				break;
			}

			/* Show non-diff lines if excluding, or if
			 * in verbose mode, and if --clean isn't specified. */
			if (mode == mode_filter && (pat_exclude || verbose)
				&& !clean_comments)
				fputs (line, stdout);

			if (getline (&line, &linelen, f) == -1)
				goto eof;
			linenum++;
		}

		start_linenum = linenum;
		header[0] = xstrdup (line);
                num_headers = 1;

                if (is_context == -1) {
                        int valid_extended = 1;
                        for (;;) {
                                if (getline (&line, &linelen, f) == -1)
                                        goto eof;
                                linenum++;

                                if (!strncmp (line, "diff ", 5)) {
                                        header[num_headers++] = xstrdup (line);
                                        break;
                                }

                                if (!strncmp (line, "--- ", 4))
                                        is_context = 0;
                                else if (!strncmp (line, "*** ", 4))
                                        is_context = 1;
                                else if (strncmp (line, "old mode ", 9) &&
                                    strncmp (line, "new mode ", 9) &&
                                    strncmp (line, "deleted file mode ", 18) &&
                                    strncmp (line, "new file mode ", 15) &&
                                    strncmp (line, "copy from ", 10) &&
                                    strncmp (line, "copy to ", 8) &&
                                    strncmp (line, "rename from ", 12) &&
                                    strncmp (line, "rename to ", 10) &&
                                    strncmp (line, "similarity index ", 17) &&
                                    strncmp (line, "dissimilarity index ", 20) &&
                                    strncmp (line, "index ", 6))
                                        valid_extended = 0;

                                if (!valid_extended)
                                        break;

                                /* Drop excess header lines */
                                if (num_headers >= MAX_HEADERS - 2)
                                        free (header[--num_headers]);

                                header[num_headers++] = xstrdup (line);

                                if (is_context != -1)
                                        break;
                        }

                        if (!valid_extended)
                                goto flush_continue;
                }

                if (is_context == -1) {
                        /* We don't yet do anything with diffs with
                         * zero hunks. */
                        unsigned int i = 0;
                flush_continue:
                        if (mode == mode_filter && (pat_exclude || verbose)
                            && !clean_comments) {
                                for (i = 0; i < num_headers; i++)
                                        fputs (header[i], stdout);
                        }
                        for (i = 0; i < num_headers; i++) {
                                free (header[i]);
                                header[i] = NULL;
                        }
                        num_headers = 0;
                        continue;
                }

		names[0] = filename_from_header (line + 4);
		if (mode != mode_filter && show_status)
			orig_file_exists = file_exists (names[0], line + 4 +
							strlen (names[0]));

		if (getline (&line, &linelen, f) == -1) {
			/* Show non-diff lines if excluding, or if
			 * in verbose mode, and if --clean isn't specified. */
			if (mode == mode_filter && (pat_exclude || verbose)
				&& !clean_comments)
				fputs (header[0], stdout);
			free (names[0]);
			goto eof;
		}
		linenum++;

		if (strncmp (line, is_context ? "--- " : "+++ ", 4)) {
			/* Show non-diff lines if excluding, or if
			 * in verbose mode, and if --clean isn't specified. */
			free (names[0]);
                        goto flush_continue;
		}

		filecount++;
		header[num_headers++] = xstrdup (line);
		names[1] = filename_from_header (line + 4);

		if (mode != mode_filter && show_status)
			new_file_exists = file_exists (names[1], line + 4 +
						       strlen (names[1]));

		// Decide whether this matches this pattern.
		p = best_name (2, names);
		p_stripped = stripped (p, ignore_components);

		match = !patlist_match(pat_exclude, p_stripped);
		if (match && pat_include != NULL)
			match = patlist_match(pat_include, p_stripped);

		// print if it matches.
		if (match && !show_status && mode == mode_list)
			display_filename (start_linenum, status,
					  p, patchname);

		if (is_context)
			do_diff = do_context;
		else
			do_diff = do_unified;

		result = do_diff (f, header, num_headers,
                                  match, &line,
				  &linelen, &linenum,
				  start_linenum, status, p, patchname,
				  &orig_file_exists, &new_file_exists);

		// print if it matches.
		if (match && show_status && mode == mode_list) {
			if (!orig_file_exists)
				status = '+';
			else if (!new_file_exists)
				status = '-';

			display_filename (start_linenum, status,
					  p, patchname);
		}

		switch (result) {
		case EOF:
			free (names[0]);
			free (names[1]);
			goto eof;
		case 1:
			goto next_diff;
		}

	next_diff:
		for (i = 0; i < 2; i++)
			free (names[i]);
                for (i = 0; i < num_headers; i++) {
			free (header[i]);
			header[i] = NULL;
		}
                num_headers = 0;
	}

 eof:
	for (i = 0; i < num_headers; i++)
		if (header[i])
			free (header[i]);

	if (line)
		free (line);

	return 0;
}
示例#5
0
static int cmpdir(const char* a, const char* b)
{
	struct dirent **namelist1, **namelist2;
	int n1, n2;
	int i1 = 0, i2 = 0;
	n1 = scandir(a, &namelist1, filter, alphasort);
	if (n1 < 0)
		perror("scandir");
	//printf("%d entries in %s\n", n1, a);
	n2 = scandir(b, &namelist2, filter, alphasort);
	if (n2 < 0)
		perror("scandir");
	//printf("%d entries in %s\n", n2, b);
	while (i1 < n1 || i2 < n2) {
		int ret; 
		if (i1 >= n1)
			ret = 1;
		else if (i2 >= n2)
			ret = -1;
		else
			ret = alphasort((const struct dirent **)namelist1+i1, (const struct dirent **)namelist2+i2);

		if (ret < 0) {
			do_delete(namelist1[i1]);
			free(namelist1[i1]);
			i1++;
		} else if (ret > 0) {
			do_add(namelist2[i2]);
			free(namelist2[i2]);
			i2++;
		} else {
			char buf1[1024], buf2[1024];
			//printf("%s match (recurse)\n", namelist1[i1]->d_name);
			sprintf(buf1, "%s/%s", a, namelist1[i1]->d_name);
			sprintf(buf2, "%s/%s", b, namelist2[i2]->d_name);
			if(namelist1[i1]->d_type != namelist2[i2]->d_type)
			{
				fprintf(stderr, "%s types differ, delete then add?\n", namelist1[i1]->d_name);
			} else if(namelist1[i1]->d_type == DT_LNK) {
				int len1, len2;
				len1 = readlink(buf1, buf1, 1024);
				len2 = readlink(buf2, buf2, 1024);
				//printf("SYMLINK %d %d\n", len1, len2);
				if(len1 != len2 || strncmp(buf1, buf2, len1)) {
					fprintf(stderr, "%s symlink target mismatch\n", namelist1[i1]->d_name);
				}
			} else if(namelist1[i1]->d_type == DT_DIR) {
				int len;
				len = strlen(prefix);
				strcat(prefix, "/");
				strcat(prefix, namelist1[i1]->d_name);
				cmpdir(buf1,buf2);
				prefix[len] = 0;
			} else {
				// stat files and compare
				struct stat sb1, sb2;
				ret = stat(buf1, &sb1);
				if(ret != 0)
					fprintf(stderr, "couldn't stat %s\n", buf1);
				ret = stat(buf2, &sb2);
				if(ret != 0)
					fprintf(stderr, "couldn't stat %s\n", buf2);
				if(sb1.st_size != sb2.st_size ||
						cmpfiles(buf1, buf2, sb1.st_size)) {
					 do_diff(namelist1[i1], namelist2[i2]);
				}
			}
			free(namelist1[i1]);
			free(namelist2[i2]);
			i1++, i2++;
		}
	}
	free(namelist1);
	free(namelist2);

	return 0;
}
示例#6
0
void print_diff(std::vector<std::string> &text1, std::vector<std::string> &text2, unsigned num_lines_context, std::string &ret)
{
	// first do line-level diff
	std::vector<diff_op<std::string> > linediff = do_diff(text1, text2);
	int ctx = 0;

	for (unsigned i = 0; i < linediff.size(); ++i) {
		switch (linediff[i].op) {
		case ins:
			// inserted line
			ret += "<tr>"
				"<td colspan=\"2\">&nbsp;</td>"
			        "<td>+</td>"
			        "<td class=\"diff-addedline\">" + *linediff[i].to + "</td></tr>";
			break;
		case del:
			// deleted text
			ret += "<tr>"
				"<td>−</td>"
				"<td class=\"diff-deletedline\">" + *linediff[i].from + "</td>"
				"<td colspan=\"2\">&nbsp;</td>"
				"</tr>";
			break;
		case copy:
			// copy/context
			if (ctx == 0) {
				// peek to see if next NUM_LINES_CONTEXT lines are also context -- if so, don't print this
				bool context_future = true;
				for (unsigned j = 0; j < num_lines_context; ++j) {
					if (i + j + 1 < linediff.size() && linediff[i + j + 1].op != copy) {
						context_future = false;
						break;
					}
				}
				if (context_future)
					break;

				char buf[256]; // should be plenty
				sprintf(buf, "<tr><td colspan=\"2\" align=\"left\"><strong>Line %u:</strong></td>"
					"<td colspan=\"2\" align=\"left\"><strong>Line %u:</strong></td></tr>",
					linediff[i].from_ind + 1, linediff[i].to_ind + 1);
				ret += buf;

				// reset ctx so we won't print this heading over and over again
				ctx = num_lines_context;
			}
			
			ret += "<tr><td> </td><td class=\"diff-context\">" + *linediff[i].from + "</td>"
				"<td> </td><td class=\"diff-context\">" + *linediff[i].to + "</td></tr>";
			break;
		case rep:
			// replace, ie. we do a word diff between the two lines
			print_worddiff(linediff[i].from->c_str(), linediff[i].to->c_str(), ret);
			break;
		}

		if (linediff[i].op == copy) {
			if (ctx > 0)
				--ctx;
		} else {
			ctx = num_lines_context;
		}
	}
}
示例#7
0
int main(int argc, char *argv[]) {
	char c;
	int ret = 1;
	const struct sysdef *s;
	const struct targetdef *t;
	uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
	uint32_t addr = 0;
	const char *streamfn = NULL, *difffn = NULL;
	struct msr msrval = MSR2(-1, -1);
	while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
		switch (c) {
		case 'h':
			syntax(argv);
			return 0;
		case 'q':
			quiet = 1;
			break;
		case 'v':
			++verbose;
			break;
		case 'r':
			reserved = 1;
			break;
		case 'k':
			listknown = 1;
			break;
		case 'l':
			listmsrs = 1;
			break;
		case 'c':
			cpu = atoi(optarg);
			break;
		case 'm':
			for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
				if (!strcmp(s->name, optarg)) {
					sys = s;
					break;
				}
			break;
		case 't':
			for (t = alltargets; !TARGET_ISEOT(*t); t++)
				if (!strcmp(t->name, optarg)) {
					add_target(t);
					break;
				}
			break;
		case 'i':
			input = 1;
			addr = msraddrbyname(optarg);
			optarg = strchr(optarg, '=');
			if (NULL == optarg) {
				fprintf(stderr, "missing value in -i argument!\n");
				break;
			}
			if (!str2msr(++optarg, &msrval, NULL))
				fprintf(stderr, "invalid value in -i argument!\n");
			break;
		case 's':
			streamfn = optarg;
			break;
		case 'd':
			difffn = optarg;
			break;
		default:
			break;
		}

	printf_quiet("msrtool %s\n", VERSION);

	pacc = pci_alloc();
	if (NULL == pacc) {
		fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
		return 1;
	}
	pci_init(pacc);
	pci_scan_bus(pacc);

	if (!sys && !input && !listknown)
		for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
			printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
			if (!sys->probe(sys))
				continue;
			printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
			break;
		}

	if (targets)
		for (tn = 0; tn < targets_found; tn++)
			printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
	else
		for (t = alltargets; !TARGET_ISEOT(*t); t++) {
			printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
			if (!t->probe(t))
				continue;
			printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
			add_target(t);
		}

	printf_quiet("\n");
	fflush(stdout);

	if (listknown) {
		printf("Known systems:\n");
		for (s = allsystems; s->name; s++)
			printf("%s: %s\n", s->name, s->prettyname);
		printf("\nKnown targets:\n");
		for (t = alltargets; t->name; t++) {
			if (listmsrs && alltargets != t)
				printf("\n");
			printf("%s: %s\n", t->name, t->prettyname);
			if (listmsrs)
				dumpmsrdefs(t);
		}
		printf("\n");
		return 0;
	}

	if (!targets_found || !targets) {
		fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
		fprintf(stderr, "Please send a report or patch to [email protected]. Thanks for your help!\n");
		fprintf(stderr, "\n");
		return 1;
	}

	if (input) {
		decodemsr(cpu, addr, msrval);
		return 0;
	}

	if (listmsrs) {
		if (streamfn)
			return do_stream(streamfn, 1);
		for (tn = 0; tn < targets_found; tn++) {
			if (tn)
				printf("\n");
			dumpmsrdefs(targets[tn]);
		}
		printf("\n");
		return 0;
	}

	if (streamfn)
		return do_stream(streamfn, 0);

	if (difffn) {
		ret = do_diff(difffn);
		goto done;
	}

	if (optind == argc) {
		syntax(argv);
		printf("\nNo mode or address(es) specified!\n");
		goto done;
	}

	if (!found_system())
		return 1;
	if (!sys->open(cpu, SYS_RDONLY))
		return 1;

	for (; optind < argc; optind++) {
		addr = msraddrbyname(argv[optind]);
		if (!sys->rdmsr(cpu, addr, &msrval))
			break;
		decodemsr(cpu, addr, msrval);
	}
	ret = 0;
done:
	sys->close(cpu);
	return ret;
}