Пример #1
0
static const char *get_token(char *buf, int blen, const char *str)
{
  const char *p, *s, *e;
  int len;

  p = str;
  while (isspace_(*p))
    p++;
  if (*p == 0)
    return NULL;
  if (*p == ';') {
    strcpy(buf, ";");
    return p + 1;
  }

  s = p;
  while (*p != 0 && *p != ';' && !isspace_(*p))
    p++;
  e = p;
  while (isspace_(*e))
    e++;

  len = p - s;
  if (len > blen - 1)
    len = blen - 1;
  memcpy(buf, s, len);
  buf[len] = 0;
  return e;
}
Пример #2
0
static char *spaces_right(char *p) {
    while (*p && isspace_(*p))
        ++p;
    return p;
}
Пример #3
0
static char *spaces_left(char *p) {
    while (isspace_(*p))
        --p;
    return p;
}
Пример #4
0
static int
lw_diff2wc_diff(int mindiff, int doChar, char *lw_diff_fn, char *wc_diff_fn)
{
	FILE *lw_diff_fp, *wc_diff_fp, *fp[2], *pipe_fp;
	char line[BUFLEN], command[CMDLEN], pipe_ln[BUFLEN];
	char *ok, *fn[2];
	size_t i;
	int j;
	int space, alpha_, digit, l[2], k[2];
	char wc_old_fn[CMDLEN], wc_new_fn[CMDLEN];
	char *_d = mindiff ? "-d" : "";

	TRACE(fprintf(STDERR, "lw_diff2wc_diff(%i,%i,%s,%s)\n", mindiff, doChar, lw_diff_fn, wc_diff_fn));

	lw_diff_fp = Rfopen(lw_diff_fn);
	if (!(ok = fgets(line, BUFLEN, lw_diff_fp))) {
		fclose(lw_diff_fp);
		ERRHNDL(0, "empty file in lw_diff2wc_diff:", lw_diff_fn, 1);
	}

	sprintf(wc_old_fn, "%s%c.difflib-%ld-lw_diff2wc_diff-old", tmpdir(), DIR_SEP, (long) getpid());
	fn[0] = wc_old_fn;
	sprintf(wc_new_fn, "%s%c.difflib-%ld-lw_diff2wc_diff-new", tmpdir(), DIR_SEP, (long) getpid());
	fn[1] = wc_new_fn;

	wc_diff_fp = Wfopen(wc_diff_fn);
	while (ok && strncmp(line, "@@ -", 4)) {
		fprintf(wc_diff_fp, "%s", line);
		ok = fgets(line, BUFLEN, lw_diff_fp);
	}
	fflush(wc_diff_fp);
	fclose(wc_diff_fp);

	while (ok) {
		wc_diff_fp = Afopen(wc_diff_fn);
		do {
			fprintf(wc_diff_fp, "%s", line);
		} while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
		fflush(wc_diff_fp);
		fclose(wc_diff_fp);

		l[0] = l[1] = k[0] = k[1] = 0;
		for (j = 0; j < 2; j++)
			fp[j] = Wfopen(fn[j]);
		while (ok && (ok = fgets(line, BUFLEN, lw_diff_fp)) && strchr(" -+", line[0])) {
			if (line[0] == ' ') {
				char l1 = line[1];
				while (k[0] < k[1]) {
					markNL(fp[0], k[0]++);
					l[0]++;
				}
				while (k[1] < k[0]) {
					markNL(fp[1], k[1]++);
					l[1]++;
				}
				i = 1;
				do {
					for (j = 0; j < 2; j++) {
						fprintf(fp[j], "%s", line+i);
					}
					i = 0;
				} while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
				for (j = 0; j < 2; j++) {
					l[j]++;
				}
				if (l1 == '\1')
					for (j = 0; j < 2; j++) {
						markNL(fp[j], k[j]++);
						l[j]++;
					}
			} else {
				if (line[0] == '-')
					j = 0;
				else
					j = 1;
				if (line[1] == '\1') {
					fprintf(fp[j], "\1\n");
					markNL(fp[j], k[j]++);
					l[j] += 2;
				} else if (doChar) {
					i = 1;
					do {
						for (; line[i] != '\n' && line[i] != '\0'; i++) {
							fprintf(fp[j], "%c\n", line[i]);
							l[j]++;
						}
						i = 0;
					} while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
				} else {
					space = isspace_((int) (line[1]));
					alpha_ = isalpha_((int) (line[1]));
					digit = isdigit_((int) (line[1]));
					i = 1;
					do {
						for (; line[i] != '\n' && line[i] != '\0'; i++) {
							if ((space && !isspace_((int) line[i])) ||
							    (!space && isspace_((int) line[i])) ||
							    (alpha_ && !isalpha_((int) line[i])) ||
							    (!alpha_ && isalpha_((int) line[i])) ||
							    (digit && !isdigit_((int) line[i])) ||
							    (!digit && isdigit_((int) line[i])) ||
							    (!isspace_((int) line[i]) &&
							     !isalpha_((int) line[i]) &&
							     !isdigit_((int) line[i]))) {
								fprintf(fp[j], "\n");
								space = isspace_((int) line[i]);
								alpha_ = isalpha_((int) line[i]);
								digit = isdigit_((int) line[i]);
								l[j]++;
							}
							fprintf(fp[j], "%c", line[i]);
						}
						i = 0;
					} while (line[strlen(line) - 1] != '\n' && (ok = fgets(line, BUFLEN, lw_diff_fp)));
					fprintf(fp[j], "\n");
					l[j]++;
				}
			}
		}
		for (j = 0; j < 2; j++) {
			fflush(fp[j]);
			fclose(fp[j]);
		}

/*
      sprintf(command,
              "%s -a %s -u%i %s %s | egrep -v '^(@@ \\-|\\+\\+\\+ |\\-\\-\\- |[ \\+\\-]@\\+\\-)' >> %s",
              DIFF,_d,MAX(l[0],l[1]),fn[0],fn[1],wc_diff_fn);
      SYSTEM(command);
*/

		sprintf(command, "%s -a %s -U%d %s %s", DIFF, _d, MAX(l[0], l[1]), fn[0], fn[1]);

		pipe_fp = popen(command, "r");

		wc_diff_fp = Afopen(wc_diff_fn);
		while (fgets(pipe_ln, BUFLEN, pipe_fp)) {
			if (strncmp(pipe_ln, "@@ -", 4) &&
			    strncmp(pipe_ln, "+++ ", 4) &&
			    strncmp(pipe_ln, "--- ", 4) &&
			    strncmp(pipe_ln, " @+-", 4) &&
			    strncmp(pipe_ln, "+@+-", 4) &&
			    strncmp(pipe_ln, "-@+-", 4)) {
				fprintf(wc_diff_fp, "%s", pipe_ln);
			}
		}
		fflush(wc_diff_fp);
		fclose(wc_diff_fp);
		pclose(pipe_fp);
	}
	UNLINK(wc_old_fn);
	UNLINK(wc_new_fn);

	fclose(lw_diff_fp);
	return 1;
}