📄 pch.c
字号:
/* $Header: pch.c,v 2.0.1.7 88/06/03 15:13:28 lwall Locked $ * * $Log: pch.c,v $ * Revision 2.0.1.7 88/06/03 15:13:28 lwall * patch10: Can now find patches in shar scripts. * patch10: Hunks that swapped and then swapped back could core dump. * * Revision 2.0.1.6 87/06/04 16:18:13 lwall * pch_swap didn't swap p_bfake and p_efake. * * Revision 2.0.1.5 87/01/30 22:47:42 lwall * Improved responses to mangled patches. * * Revision 2.0.1.4 87/01/05 16:59:53 lwall * New-style context diffs caused double call to free(). * * Revision 2.0.1.3 86/11/14 10:08:33 lwall * Fixed problem where a long pattern wouldn't grow the hunk. * Also restored p_input_line when backtracking so error messages are right. * * Revision 2.0.1.2 86/11/03 17:49:52 lwall * New-style delete triggers spurious assertion error. * * Revision 2.0.1.1 86/10/29 15:52:08 lwall * Could falsely report new-style context diff. * * Revision 2.0 86/09/17 15:39:37 lwall * Baseline for netwide release. * */#include "EXTERN.h"#include "common.h"#include "util.h"#include "INTERN.h"#include "pch.h"/* Patch (diff listing) abstract type. */static long p_filesize; /* size of the patch file */static LINENUM p_first; /* 1st line number */static LINENUM p_newfirst; /* 1st line number of replacement */static LINENUM p_ptrn_lines; /* # lines in pattern */static LINENUM p_repl_lines; /* # lines in replacement text */static LINENUM p_end = -1; /* last line in hunk */static LINENUM p_max; /* max allowed value of p_end */static LINENUM p_context = 3; /* # of context lines */static LINENUM p_input_line = 0; /* current line # from patch file */#ifdef SMALLstatic long *p_line = Null(long *); /* the text of the hunk */#elsestatic char **p_line = Null(char**); /* the text of the hunk */#endifstatic short *p_len = Null(short*); /* length of each line */static char *p_char = Nullch; /* +, -, and ! */static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */static int p_indent; /* indent to patch */static LINENUM p_base; /* where to intuit this time */static LINENUM p_bline; /* line # of p_base */static LINENUM p_start; /* where intuit found a patch */static LINENUM p_sline; /* and the line number for it */static LINENUM p_hunk_beg; /* line number of current hunk */static LINENUM p_efake = -1; /* end of faked up lines--don't free */static LINENUM p_bfake = -1; /* beg of faked up lines *//* Prepare to look for the next patch in the patch file. */voidre_patch(){ p_first = Nulline; p_newfirst = Nulline; p_ptrn_lines = Nulline; p_repl_lines = Nulline; p_end = (LINENUM)-1; p_max = Nulline; p_indent = 0;}/* Open the patch file at the beginning of time. */voidopen_patch_file(filename)char *filename;{ if (filename == Nullch || !*filename || strEQ(filename, "-")) { pfp = fopen(TMPPATNAME, "w"); if (pfp == Nullfp) fatal2("patch: can't create %s.\n", TMPPATNAME); while (fgets(buf, sizeof buf, stdin) != Nullch) fputs(buf, pfp); Fclose(pfp); filename = TMPPATNAME; } pfp = fopen(filename, "r"); if (pfp == Nullfp) fatal2("patch file %s not found\n", filename); Fstat(fileno(pfp), &filestat); p_filesize = filestat.st_size; next_intuit_at(0L,1L); /* start at the beginning */ set_hunkmax();}/* Make sure our dynamically realloced tables are malloced to begin with. */voidset_hunkmax(){#ifndef lint#ifdef SMALL if (p_line == Null(long*))#else if (p_line == Null(char**))#endif#ifdef SMALL p_line = (long *) malloc((MEM)hunkmax * sizeof(long));#else p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));#endif if (p_len == Null(short*)) p_len = (short*) malloc((MEM)hunkmax * sizeof(short));#endif if (p_char == Nullch) p_char = (char*) malloc((MEM)hunkmax * sizeof(char));}/* Enlarge the arrays containing the current hunk of patch. */voidgrow_hunkmax(){ hunkmax *= 2; /* * Note that on most systems, only the p_line array ever gets fresh memory * since p_len can move into p_line's old space, and p_char can move into * p_len's old space. Not on PDP-11's however. But it doesn't matter. */#ifdef SMALL assert(p_line != Null(long*) && p_len != Null(short*) && p_char != Nullch);#else assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);#endif#ifndef lint#ifdef SMALL p_line = (long*) realloc((char*)p_line, (MEM)hunkmax * sizeof(long));#else p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));#endif p_len = (short*) realloc((char*)p_len, (MEM)hunkmax * sizeof(short)); p_char = (char*) realloc((char*)p_char, (MEM)hunkmax * sizeof(char));#endif#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 return; if (!using_plan_a) fatal1("patch: out of memory (grow_hunkmax)\n"); out_of_mem = TRUE; /* whatever is null will be allocated again */ /* from within plan_a(), of all places */}/* True if the remainder of the patch file contains a diff of some sort. */boolthere_is_another_patch(){ if (p_base != 0L && p_base >= p_filesize) { if (verbose) say1("done\n"); return FALSE; } if (verbose) say1("Hmm..."); diff_type = intuit_diff_type(); if (!diff_type) { if (p_base != 0L) { if (verbose) say1(" Ignoring the trailing garbage.\ndone\n"); } else say1(" I can't seem to find a patch in there anywhere.\n"); return FALSE; } if (verbose) say3(" %sooks like %s to me...\n", (p_base == 0L ? "L" : "The next patch l"), diff_type == CONTEXT_DIFF ? "a context diff" : diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" : diff_type == NORMAL_DIFF ? "a normal diff" : "an ed script" ); if (p_indent && verbose) say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s"); skip_to(p_start,p_sline); while (filearg[0] == Nullch) { if (force) { say1("No file to patch. Skipping...\n"); filearg[0] = savestr(bestguess); return TRUE; } ask1("File to patch: "); if (*buf != '\n') { if (bestguess) free(bestguess); bestguess = savestr(buf); filearg[0] = fetchname(buf, 0, FALSE); } if (filearg[0] == Nullch) { ask1("No file found--skip this patch? [n] "); if (*buf != 'y') { continue; } if (verbose) say1("Skipping patch...\n"); filearg[0] = fetchname(bestguess, 0, TRUE); skip_rest_of_patch = TRUE; return TRUE; } } return TRUE;}/* Determine what kind of diff is in the remaining part of the patch file. */intintuit_diff_type(){ Reg4 long this_line = 0; Reg5 long previous_line; Reg6 long first_command_line = -1; long fcl_line; Reg7 bool last_line_was_command = FALSE; Reg8 bool this_is_a_command = FALSE; Reg9 bool stars_last_line = FALSE; Reg10 bool stars_this_line = FALSE; Reg3 int indent; Reg1 char *s; Reg2 char *t; char *indtmp = Nullch; char *oldtmp = Nullch; char *newtmp = Nullch; char *indname = Nullch; char *oldname = Nullch; char *newname = Nullch; Reg11 int retval; bool no_filearg = (filearg[0] == Nullch); ok_to_create_file = FALSE; Fseek(pfp, p_base, 0); p_input_line = p_bline - 1; for (;;) { previous_line = this_line; last_line_was_command = this_is_a_command; stars_last_line = stars_this_line; this_line = ftell(pfp); indent = 0; p_input_line++; if (fgets(buf, sizeof buf, pfp) == Nullch) { if (first_command_line >= 0L) { /* nothing but deletes!? */ p_start = first_command_line; p_sline = fcl_line; retval = ED_DIFF; goto scan_exit; } else { p_start = this_line; p_sline = p_input_line; retval = 0; goto scan_exit; } } for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) { if (*s == '\t') indent += 8 - (indent % 8); else indent++; } for (t=s; isdigit(*t) || *t == ','; t++) ; this_is_a_command = (isdigit(*s) && (*t == 'd' || *t == 'c' || *t == 'a') ); if (first_command_line < 0L && this_is_a_command) { first_command_line = this_line; fcl_line = p_input_line; p_indent = indent; /* assume this for now */ } if (!stars_last_line && strnEQ(s, "*** ", 4)) oldtmp = savestr(s+4); else if (strnEQ(s, "--- ", 4)) newtmp = savestr(s+4); else if (strnEQ(s, "Index:", 6)) indtmp = savestr(s+6); else if (strnEQ(s, "Prereq:", 7)) { for (t=s+7; isspace(*t); t++) ; revision = savestr(t); for (t=revision; *t && !isspace(*t); t++) ; *t = '\0'; if (!*revision) { free(revision); revision = Nullch; } } if ((!diff_type || diff_type == ED_DIFF) && first_command_line >= 0L && strEQ(s, ".\n") ) { p_indent = indent; p_start = first_command_line; p_sline = fcl_line; retval = ED_DIFF; goto scan_exit; } stars_this_line = strnEQ(s, "********", 8); if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && strnEQ(s, "*** ", 4)) { if (!atol(s+4)) ok_to_create_file = TRUE; /* if this is a new context diff the character just before */ /* the newline is a '*'. */ while (*s != '\n') s++; p_indent = indent; p_start = previous_line; p_sline = p_input_line - 1; retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF); goto scan_exit; } if ((!diff_type || diff_type == NORMAL_DIFF) && last_line_was_command && (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) { p_start = previous_line; p_sline = p_input_line - 1; p_indent = indent; retval = NORMAL_DIFF; goto scan_exit; } } scan_exit: if (no_filearg) { if (indtmp != Nullch) indname = fetchname(indtmp, strippath, ok_to_create_file); if (oldtmp != Nullch) oldname = fetchname(oldtmp, strippath, ok_to_create_file); if (newtmp != Nullch) newname = fetchname(newtmp, strippath, ok_to_create_file); if (oldname && newname) { if (strlen(oldname) < strlen(newname)) filearg[0] = savestr(oldname); else filearg[0] = savestr(newname); } else if (oldname) filearg[0] = savestr(oldname); else if (newname) filearg[0] = savestr(newname); else if (indname) filearg[0] = savestr(indname); } if (bestguess) { free(bestguess); bestguess = Nullch; } if (filearg[0] != Nullch) bestguess = savestr(filearg[0]); else if (indtmp != Nullch) bestguess = fetchname(indtmp, strippath, TRUE); else { if (oldtmp != Nullch) oldname = fetchname(oldtmp, strippath, TRUE); if (newtmp != Nullch) newname = fetchname(newtmp, strippath, TRUE); if (oldname && newname) { if (strlen(oldname) < strlen(newname)) bestguess = savestr(oldname); else bestguess = savestr(newname); } else if (oldname) bestguess = savestr(oldname); else if (newname) bestguess = savestr(newname); } if (indtmp != Nullch) free(indtmp); if (oldtmp != Nullch) free(oldtmp); if (newtmp != Nullch) free(newtmp); if (indname != Nullch) free(indname); if (oldname != Nullch) free(oldname); if (newname != Nullch) free(newname); return retval;}/* Remember where this patch ends so we know where to start up again. */voidnext_intuit_at(file_pos,file_line)long file_pos;long file_line;{ p_base = file_pos; p_bline = file_line;}/* Basically a verbose fseek() to the actual diff listing. */voidskip_to(file_pos,file_line)long file_pos;long file_line;{ char *ret; assert(p_base <= file_pos); if (verbose && p_base < file_pos) { Fseek(pfp, p_base, 0); say1("The text leading up to this was:\n--------------------------\n"); while (ftell(pfp) < file_pos) { ret = fgets(buf, sizeof buf, pfp); assert(ret != Nullch); say2("|%s", buf); } say1("--------------------------\n"); } else Fseek(pfp, file_pos, 0); p_input_line = file_line - 1;}/* True if there is more of the current diff listing to process. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -