version 1.24, 2003/07/25 02:12:45 |
version 1.25, 2003/07/28 18:35:36 |
|
|
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
|
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "EXTERN.h" |
|
#include "common.h" |
#include "common.h" |
#include "util.h" |
#include "util.h" |
#include "INTERN.h" |
|
#include "pch.h" |
#include "pch.h" |
|
|
/* Patch (diff listing) abstract type. */ |
/* Patch (diff listing) abstract type. */ |
|
|
static LINENUM p_max; /* max allowed value of p_end */ |
static LINENUM p_max; /* max allowed value of p_end */ |
static LINENUM p_context = 3; /* # of context lines */ |
static LINENUM p_context = 3; /* # of context lines */ |
static LINENUM p_input_line = 0; /* current line # from patch file */ |
static LINENUM p_input_line = 0; /* current line # from patch file */ |
static char **p_line = NULL; /* the text of the hunk */ |
static char **p_line = NULL;/* the text of the hunk */ |
static short *p_len = NULL; /* length of each line */ |
static short *p_len = NULL; /* length of each line */ |
static char *p_char = NULL;/* +, -, and ! */ |
static char *p_char = NULL; /* +, -, and ! */ |
static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */ |
static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */ |
static int p_indent; /* indent to patch */ |
static int p_indent; /* indent to patch */ |
static LINENUM p_base; /* where to intuit this time */ |
static LINENUM p_base; /* where to intuit this time */ |
|
|
static LINENUM p_hunk_beg; /* line number of current hunk */ |
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_efake = -1; /* end of faked up lines--don't free */ |
static LINENUM p_bfake = -1; /* beg of faked up lines */ |
static LINENUM p_bfake = -1; /* beg of faked up lines */ |
|
static FILE *pfp = NULL; /* patch file pointer */ |
|
static char *bestguess = NULL; /* guess at correct filename */ |
|
|
static void grow_hunkmax(void); |
static void grow_hunkmax(void); |
static int intuit_diff_type(void); |
static int intuit_diff_type(void); |
static void next_intuit_at(long, long); |
static void next_intuit_at(LINENUM, LINENUM); |
static void skip_to(long, long); |
static void skip_to(LINENUM, LINENUM); |
static char *pgets(char *, int, FILE *); |
static char *pgets(char *, int, FILE *); |
|
|
|
|
/* |
/* |
* Prepare to look for the next patch in the patch file. |
* Prepare to look for the next patch in the patch file. |
*/ |
*/ |
void |
void |
re_patch(void) |
re_patch(void) |
{ |
{ |
p_first = NULL; |
p_first = 0; |
p_newfirst = NULL; |
p_newfirst = 0; |
p_ptrn_lines = NULL; |
p_ptrn_lines = 0; |
p_repl_lines = NULL; |
p_repl_lines = 0; |
p_end = (LINENUM) - 1; |
p_end = (LINENUM) - 1; |
p_max = NULL; |
p_max = 0; |
p_indent = 0; |
p_indent = 0; |
} |
} |
|
|
|
|
* Open the patch file at the beginning of time. |
* Open the patch file at the beginning of time. |
*/ |
*/ |
void |
void |
open_patch_file(char *filename) |
open_patch_file(const char *filename) |
{ |
{ |
if (filename == NULL || !*filename || strEQ(filename, "-")) { |
struct stat filestat; |
|
|
|
if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) { |
pfp = fopen(TMPPATNAME, "w"); |
pfp = fopen(TMPPATNAME, "w"); |
if (pfp == NULL) |
if (pfp == NULL) |
pfatal("can't create %s", TMPPATNAME); |
pfatal("can't create %s", TMPPATNAME); |
|
|
* since p_len can move into p_line's old space, and p_char can move into |
* 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. |
* p_len's old space. Not on PDP-11's however. But it doesn't matter. |
*/ |
*/ |
assert(p_line != NULL &&p_len != NULL &&p_char != NULL); |
assert(p_line != NULL && p_len != NULL && p_char != NULL); |
|
|
p_line = realloc(p_line, hunkmax * sizeof(char *)); |
p_line = realloc(p_line, hunkmax * sizeof(char *)); |
p_len = realloc(p_len, hunkmax * sizeof(short)); |
p_len = realloc(p_len, hunkmax * sizeof(short)); |
p_char = realloc(p_char, hunkmax * sizeof(char)); |
p_char = realloc(p_char, hunkmax * sizeof(char)); |
|
|
if (p_line != NULL &&p_len != NULL &&p_char != NULL) |
if (p_line != NULL && p_len != NULL && p_char != NULL) |
return; |
return; |
if (!using_plan_a) |
if (!using_plan_a) |
fatal("out of memory\n"); |
fatal("out of memory\n"); |
|
|
} |
} |
ask("File to patch: "); |
ask("File to patch: "); |
if (*buf != '\n') { |
if (*buf != '\n') { |
if (bestguess) |
free(bestguess); |
free(bestguess); |
|
bestguess = savestr(buf); |
bestguess = savestr(buf); |
filearg[0] = fetchname(buf, 0, FALSE); |
filearg[0] = fetchname(buf, 0, FALSE); |
} |
} |
|
|
for (t = revision; *t && !isspace(*t); t++) |
for (t = revision; *t && !isspace(*t); t++) |
; |
; |
*t = '\0'; |
*t = '\0'; |
if (!*revision) { |
if (*revision == '\0') { |
free(revision); |
free(revision); |
revision = NULL; |
revision = NULL; |
} |
} |
|
|
else if (newname) |
else if (newname) |
filearg[0] = savestr(newname); |
filearg[0] = savestr(newname); |
} |
} |
if (bestguess) { |
|
free(bestguess); |
free(bestguess); |
bestguess = NULL; |
bestguess = NULL; |
} |
|
if (filearg[0] != NULL) |
if (filearg[0] != NULL) |
bestguess = savestr(filearg[0]); |
bestguess = savestr(filearg[0]); |
else if (indtmp != NULL) |
else if (indtmp != NULL) |
|
|
* Remember where this patch ends so we know where to start up again. |
* Remember where this patch ends so we know where to start up again. |
*/ |
*/ |
static void |
static void |
next_intuit_at(long file_pos, long file_line) |
next_intuit_at(LINENUM file_pos, LINENUM file_line) |
{ |
{ |
p_base = file_pos; |
p_base = file_pos; |
p_bline = file_line; |
p_bline = file_line; |
|
|
* Basically a verbose fseek() to the actual diff listing. |
* Basically a verbose fseek() to the actual diff listing. |
*/ |
*/ |
static void |
static void |
skip_to(long file_pos, long file_line) |
skip_to(LINENUM file_pos, LINENUM file_line) |
{ |
{ |
char *ret; |
char *ret; |
|
|