📄 pch.c
字号:
p_line[i] = saveStr(buf, p_len+i);#else p_line[i] = savestr(buf); if (out_of_mem) { p_end = i-1; return FALSE; }#endif 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);#ifdef SMALL p_line[i] = saveStr(buf+2, p_len+i);#else p_line[i] = savestr(buf+2); if (out_of_mem) { p_end = i-1; return FALSE; }#endif#ifndef SMALL p_len[i] = strlen(p_line[i]);#endif p_char[i] = '+'; } } if (reverse) /* backwards patch? */ if (!pch_swap()) say1("Not enough memory to swap next hunk!\n");#ifdef SMALL Fclose(sfp); sfp = fopen(TMPSTRNAME, "r");#endif#ifdef DEBUGGING if (debug & 2) { int i; char special; for (i=0; i <= p_end; i++) { if (i == p_ptrn_lines) special = '^'; else special = ' ';#ifdef SMALL fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, pfetch(i));#else fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);#endif Fflush(stderr); } }#endif if (p_end+1 < hunkmax) /* paranoia reigns supreme... */ p_char[p_end+1] = '^'; /* add a stopper for apply_hunk */ return TRUE;malformed:#ifdef SMALL Fclose(sfp); sfp = Nullfp;#endif fatal3("Malformed patch at line %ld: %s", p_input_line, buf); /* about as informative as "Syntax error" in C */ return FALSE; /* for lint */}/* 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(){#ifdef SMALL long *tp_line; /* the text of the hunk */#else char **tp_line; /* the text of the hunk */#endif 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;#ifdef SMALL p_line = Null(long*); /* force set_hunkmax to allocate again */#else p_line = Null(char**); /* force set_hunkmax to allocate again */#endif p_len = Null(short*); p_char = Nullch; set_hunkmax();#ifdef SMALL if (p_line == Null(long*) || p_len == Null(short*) || p_char == Nullch) {#else if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {#endif#ifndef lint#ifdef SMALL if (p_line == Null(long*))#else if (p_line == Null(char**))#endif 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] = '*';#ifdef SMALL strEdit(p_line[0], '*', '-');#else for (s=p_line[0]; *s; s++) if (*s == '-') *s = '*';#endif /* now turn the old into the new */ assert(tp_char[0] == '*'); tp_char[0] = '=';#ifdef SMALL strEdit(tp_line[0], '-', '*');#else for (s=tp_line[0]; *s; s++) if (*s == '*') *s = '-';#endif 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#ifdef SMALL if (tp_line == Null(long*))#else if (tp_line == Null(char**))#endif 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. */#ifdef SMALLlongsaveStr(str, plen)char *str;short *plen;{ long pos, ftell(); int len; pos = ftell(sfp); len = strlen(str); fwrite(str, sizeof(char), len+1, sfp); *plen = len; return pos;}char *pfetch(line)LINENUM line;{ static char *s, strbuf[BUFSIZ]; int i, c; if (p_line[line] == -1L) return Nullch; else { Fseek(sfp, p_line[line], 0); for (i = 0, s = strbuf; i < BUFSIZ && (c = fgetc(sfp)) != EOF && c; i++) *s++ = c; if (i == BUFSIZ) fatal2("too long line (%.40s ..\n", strbuf); } *s = '\0'; return strbuf;}voidstrEdit(pos, to, from)long pos;int to, from;{ static char *s, strbuf[BUFSIZ]; int i, c; if (pos != -1L) { for (i = 0, s = strbuf; i < BUFSIZ && (c = fgetc(sfp)) != EOF && c; i++) *s++ = c; for (s = strbuf; *s; s++) if (*s == from) *s = to; fwrite(strbuf, sizeof(char), i+1, sfp); }}#elsechar *pfetch(line)LINENUM line;{ return p_line[line];}#endif/* 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 + -