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

📄 pch.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
		    p_end = fillsrc-1;		    malformed ();		}		context++;		p_char[fillsrc] = ch;		p_line[fillsrc] = s;		p_len[fillsrc++] = strlen(s);		s = savestr(s);		if (out_of_mem) {		    while (--filldst > p_ptrn_lines)			free(p_line[filldst]);		    p_end = fillsrc-1;		    return FALSE;		}		/* FALL THROUGH */	    case '+':		if (filldst > p_end) {		    free(s);		    while (--filldst > p_ptrn_lines)			free(p_line[filldst]);		    p_end = fillsrc-1;		    malformed ();		}		p_char[filldst] = ch;		p_line[filldst] = s;		p_len[filldst++] = strlen(s);		break;	    default:		p_end = filldst;		malformed ();	    }	    if (ch != ' ' && context > 0) {		if (context < p_context)		    p_context = context;		context = -1000;	    }	}/* while */    }    else {				/* normal diff--fake it up */	char hunk_type;	Reg3 int i;	LINENUM min, max;	long line_beginning = ftell(pfp);	p_context = 0;	ret = pgets(buf, sizeof buf, pfp);	p_input_line++;	if (ret == Nullch || !isdigit(*buf)) {	    next_intuit_at(line_beginning,p_input_line);	    return FALSE;	}	p_first = (LINENUM)atol(buf);	for (s=buf; isdigit(*s); s++) ;	if (*s == ',') {	    p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;	    while (isdigit(*s)) s++;	}	else	    p_ptrn_lines = (*s != 'a');	hunk_type = *s;	if (hunk_type == 'a')	    p_first++;			/* do append rather than insert */	min = (LINENUM)atol(++s);	for (; isdigit(*s); s++) ;	if (*s == ',')	    max = (LINENUM)atol(++s);	else	    max = min;	if (hunk_type == 'd')	    min++;	p_end = p_ptrn_lines + 1 + max - min + 1;	if (p_end > MAXHUNKSIZE)	    fatal4("hunk too large (%ld lines) at line %ld: %s",		  p_end, p_input_line, buf);	while (p_end >= hunkmax)	    grow_hunkmax();	p_newfirst = min;	p_repl_lines = max - min + 1;	Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);	p_line[0] = savestr(buf);	if (out_of_mem) {	    p_end = -1;	    return FALSE;	}	p_char[0] = '*';	for (i=1; i<=p_ptrn_lines; i++) {	    ret = pgets(buf, sizeof buf, pfp);	    p_input_line++;	    if (ret == Nullch)		fatal2("unexpected end of file in patch at line %ld\n",		  p_input_line);	    if (*buf != '<')		fatal2("< expected at line %ld of patch\n", p_input_line);	    p_line[i] = savestr(buf+2);	    if (out_of_mem) {		p_end = i-1;		return FALSE;	    }	    p_len[i] = strlen(p_line[i]);	    p_char[i] = '-';	}	if (hunk_type == 'c') {	    ret = pgets(buf, sizeof buf, pfp);	    p_input_line++;	    if (ret == Nullch)		fatal2("unexpected end of file in patch at line %ld\n",		    p_input_line);	    if (*buf != '-')		fatal2("--- expected at line %ld of patch\n", p_input_line);	}	Sprintf(buf, "--- %ld,%ld\n", min, max);	p_line[i] = savestr(buf);	if (out_of_mem) {	    p_end = i-1;	    return FALSE;	}	p_char[i] = '=';	for (i++; i<=p_end; i++) {	    ret = pgets(buf, sizeof buf, pfp);	    p_input_line++;	    if (ret == Nullch)		fatal2("unexpected end of file in patch at line %ld\n",		    p_input_line);	    if (*buf != '>')		fatal2("> expected at line %ld of patch\n", p_input_line);	    p_line[i] = savestr(buf+2);	    if (out_of_mem) {		p_end = i-1;		return FALSE;	    }	    p_len[i] = strlen(p_line[i]);	    p_char[i] = '+';	}    }    if (reverse)			/* backwards patch? */	if (!pch_swap())	    say1("Not enough memory to swap next hunk!\n");#ifdef DEBUGGING    if (debug & 2) {	int i;	char special;	for (i=0; i <= p_end; i++) {	    if (i == p_ptrn_lines)		special = '^';	    else		special = ' ';	    fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);	    Fflush(stderr);	}    }#endif    if (p_end+1 < hunkmax)	/* paranoia reigns supreme... */	p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */    return TRUE;}/* Input a line from the patch file, worrying about indentation. */char *pgets(bf,sz,fp)char *bf;int sz;FILE *fp;{    char *ret = fgets(bf, sz, fp);    Reg1 char *s;    Reg2 int indent = 0;    if (p_indent && ret != Nullch) {	for (s=buf;	  indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {	    if (*s == '\t')		indent += 8 - (indent % 7);	    else		indent++;	}	if (buf != s)	    Strcpy(buf, s);    }    return ret;}/* Reverse the old and new portions of the current hunk. */boolpch_swap(){    char **tp_line;		/* the text of the hunk */    short *tp_len;		/* length of each line */    char *tp_char;		/* +, -, and ! */    Reg1 LINENUM i;    Reg2 LINENUM n;    bool blankline = FALSE;    Reg3 char *s;    i = p_first;    p_first = p_newfirst;    p_newfirst = i;        /* make a scratch copy */    tp_line = p_line;    tp_len = p_len;    tp_char = p_char;    p_line = Null(char**);	/* force set_hunkmax to allocate again */    p_len = Null(short*);    p_char = Nullch;    set_hunkmax();    if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {#ifndef lint	if (p_line == Null(char**))	    free((char*)p_line);	p_line = tp_line;	if (p_len == Null(short*))	    free((char*)p_len);	p_len = tp_len;#endif	if (p_char == Nullch)	    free((char*)p_char);	p_char = tp_char;	return FALSE;		/* not enough memory to swap hunk! */    }    /* now turn the new into the old */    i = p_ptrn_lines + 1;    if (tp_char[i] == '\n') {		/* account for possible blank line */	blankline = TRUE;	i++;    }    if (p_efake >= 0) {			/* fix non-freeable ptr range */	if (p_efake <= i)	    n = p_end - i + 1;	else	    n = -i;	p_efake += n;	p_bfake += n;    }    for (n=0; i <= p_end; i++,n++) {	p_line[n] = tp_line[i];	p_char[n] = tp_char[i];	if (p_char[n] == '+')	    p_char[n] = '-';	p_len[n] = tp_len[i];    }    if (blankline) {	i = p_ptrn_lines + 1;	p_line[n] = tp_line[i];	p_char[n] = tp_char[i];	p_len[n] = tp_len[i];	n++;    }    assert(p_char[0] == '=');    p_char[0] = '*';    for (s=p_line[0]; *s; s++)	if (*s == '-')	    *s = '*';    /* now turn the old into the new */    assert(tp_char[0] == '*');    tp_char[0] = '=';    for (s=tp_line[0]; *s; s++)	if (*s == '*')	    *s = '-';    for (i=0; n <= p_end; i++,n++) {	p_line[n] = tp_line[i];	p_char[n] = tp_char[i];	if (p_char[n] == '-')	    p_char[n] = '+';	p_len[n] = tp_len[i];    }    assert(i == p_ptrn_lines + 1);    i = p_ptrn_lines;    p_ptrn_lines = p_repl_lines;    p_repl_lines = i;#ifndef lint    if (tp_line == Null(char**))	free((char*)tp_line);    if (tp_len == Null(short*))	free((char*)tp_len);#endif    if (tp_char == Nullch)	free((char*)tp_char);    return TRUE;}/* Return the specified line position in the old file of the old context. */LINENUMpch_first(){    return p_first;}/* Return the number of lines of old context. */LINENUMpch_ptrn_lines(){    return p_ptrn_lines;}/* Return the probable line position in the new file of the first line. */LINENUMpch_newfirst(){    return p_newfirst;}/* Return the number of lines in the replacement text including context. */LINENUMpch_repl_lines(){    return p_repl_lines;}/* Return the number of lines in the whole hunk. */LINENUMpch_end(){    return p_end;}/* Return the number of context lines before the first changed line. */LINENUMpch_context(){    return p_context;}/* Return the length of a particular patch line. */shortpch_line_len(line)LINENUM line;{    return p_len[line];}/* Return the control character (+, -, *, !, etc) for a patch line. */charpch_char(line)LINENUM line;{    return p_char[line];}/* Return a pointer to a particular patch line. */char *pfetch(line)LINENUM line;{    return p_line[line];}/* Return where in the patch file this hunk began, for error messages. */LINENUMpch_hunk_beg(){    return p_hunk_beg;}/* Apply an ed script by feeding ed itself. */voiddo_ed_script(){    Reg1 char *t;    Reg2 long beginning_of_this_line;    Reg3 bool this_line_is_command = FALSE;    Reg4 FILE *pipefp;    if (!skip_rest_of_patch) {	Unlink(TMPOUTNAME);	copy_file(filearg[0], TMPOUTNAME);	if (verbose)	    Sprintf(buf, "/bin/ed %s", TMPOUTNAME);	else	    Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);	pipefp = popen(buf, "w");    }    for (;;) {	beginning_of_this_line = ftell(pfp);	if (pgets(buf, sizeof buf, pfp) == Nullch) {	    next_intuit_at(beginning_of_this_line,p_input_line);	    break;	}	p_input_line++;	for (t=buf; isdigit(*t) || *t == ','; t++) ;	this_line_is_command = (isdigit(*buf) &&	  (*t == 'd' || *t == 'c' || *t == 'a') );	if (this_line_is_command) {	    if (!skip_rest_of_patch)		fputs(buf, pipefp);	    if (*t != 'd') {		while (pgets(buf, sizeof buf, pfp) != Nullch) {		    p_input_line++;		    if (!skip_rest_of_patch)			fputs(buf, pipefp);		    if (strEQ(buf, ".\n"))			break;		}	    }	}	else {	    next_intuit_at(beginning_of_this_line,p_input_line);	    break;	}    }    if (skip_rest_of_patch)	return;    fprintf(pipefp, "w\n");    fprintf(pipefp, "q\n");    Fflush(pipefp);    Pclose(pipefp);    ignore_signals();    if (move_file(TMPOUTNAME, outname) < 0) {	toutkeep = TRUE;	chmod(TMPOUTNAME, filemode);    }    else	chmod(outname, filemode);    set_signals(1);}

⌨️ 快捷键说明

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