[BACK]Return to pch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / patch

Diff for /src/usr.bin/patch/pch.c between version 1.41.6.2 and 1.42

version 1.41.6.2, 2015/07/31 18:21:58 version 1.42, 2014/11/17 10:58:09
Line 44 
Line 44 
   
 /* Patch (diff listing) abstract type. */  /* Patch (diff listing) abstract type. */
   
 static long     p_filesize;     /* size of the patch file */  static off_t    p_filesize;     /* size of the patch file */
 static LINENUM  p_first;        /* 1st line number */  static LINENUM  p_first;        /* 1st line number */
 static LINENUM  p_newfirst;     /* 1st line number of replacement */  static LINENUM  p_newfirst;     /* 1st line number of replacement */
 static LINENUM  p_ptrn_lines;   /* # lines in pattern */  static LINENUM  p_ptrn_lines;   /* # lines in pattern */
Line 58 
Line 58 
 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 off_t    p_base;         /* where to intuit this time */
 static LINENUM  p_bline;        /* line # of p_base */  static LINENUM  p_bline;        /* line # of p_base */
 static LINENUM  p_start;        /* where intuit found a patch */  static off_t    p_start;        /* where intuit found a patch */
 static LINENUM  p_sline;        /* and the line number for it */  static LINENUM  p_sline;        /* and the line number for it */
 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 */
Line 70 
Line 70 
   
 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(LINENUM, LINENUM);  static void     next_intuit_at(off_t, LINENUM);
 static void     skip_to(LINENUM, LINENUM);  static void     skip_to(off_t, LINENUM);
 static char     *pgets(char *, int, FILE *);  static char     *pgets(char *, int, FILE *);
 static char     *best_name(const struct file_name *, bool);  static char     *best_name(const struct file_name *, bool);
 static char     *posix_name(const struct file_name *, bool);  static char     *posix_name(const struct file_name *, bool);
Line 112 
Line 112 
         pfp = fopen(filename, "r");          pfp = fopen(filename, "r");
         if (pfp == NULL)          if (pfp == NULL)
                 pfatal("patch file %s not found", filename);                  pfatal("patch file %s not found", filename);
         fstat(fileno(pfp), &filestat);          if (fstat(fileno(pfp), &filestat))
                   pfatal("can't stat %s", filename);
         p_filesize = filestat.st_size;          p_filesize = filestat.st_size;
         next_intuit_at(0L, 1L); /* start at the beginning */          next_intuit_at(0, 1L);  /* start at the beginning */
         set_hunkmax();          set_hunkmax();
 }  }
   
Line 182 
Line 183 
 {  {
         bool exists = false;          bool exists = false;
   
         if (p_base != 0L && p_base >= p_filesize) {          if (p_base != 0 && p_base >= p_filesize) {
                 if (verbose)                  if (verbose)
                         say("done\n");                          say("done\n");
                 return false;                  return false;
Line 191 
Line 192 
                 say("Hmm...");                  say("Hmm...");
         diff_type = intuit_diff_type();          diff_type = intuit_diff_type();
         if (!diff_type) {          if (!diff_type) {
                 if (p_base != 0L) {                  if (p_base != 0) {
                         if (verbose)                          if (verbose)
                                 say("  Ignoring the trailing garbage.\ndone\n");                                  say("  Ignoring the trailing garbage.\ndone\n");
                 } else                  } else
Line 200 
Line 201 
         }          }
         if (verbose)          if (verbose)
                 say("  %sooks like %s to me...\n",                  say("  %sooks like %s to me...\n",
                     (p_base == 0L ? "L" : "The next patch l"),                      (p_base == 0 ? "L" : "The next patch l"),
                     diff_type == UNI_DIFF ? "a unified diff" :                      diff_type == UNI_DIFF ? "a unified diff" :
                     diff_type == CONTEXT_DIFF ? "a context diff" :                      diff_type == CONTEXT_DIFF ? "a context diff" :
                 diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :                  diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
Line 243 
Line 244 
 static int  static int
 intuit_diff_type(void)  intuit_diff_type(void)
 {  {
         long    this_line = 0, previous_line;          off_t   this_line = 0, previous_line;
         long    first_command_line = -1;          off_t   first_command_line = -1;
         LINENUM fcl_line = -1;          LINENUM fcl_line = -1;
         bool    last_line_was_command = false, this_is_a_command = false;          bool    last_line_was_command = false, this_is_a_command = false;
         bool    stars_last_line = false, stars_this_line = false;          bool    stars_last_line = false, stars_this_line = false;
Line 254 
Line 255 
   
         memset(names, 0, sizeof(names));          memset(names, 0, sizeof(names));
         ok_to_create_file = false;          ok_to_create_file = false;
         fseek(pfp, p_base, SEEK_SET);          fseeko(pfp, p_base, SEEK_SET);
         p_input_line = p_bline - 1;          p_input_line = p_bline - 1;
         for (;;) {          for (;;) {
                 previous_line = this_line;                  previous_line = this_line;
                 last_line_was_command = this_is_a_command;                  last_line_was_command = this_is_a_command;
                 stars_last_line = stars_this_line;                  stars_last_line = stars_this_line;
                 this_line = ftell(pfp);                  this_line = ftello(pfp);
                 indent = 0;                  indent = 0;
                 p_input_line++;                  p_input_line++;
                 if (fgets(buf, sizeof buf, pfp) == NULL) {                  if (fgets(buf, sizeof buf, pfp) == NULL) {
                         if (first_command_line >= 0L) {                          if (first_command_line >= 0) {
                                 /* nothing but deletes!? */                                  /* nothing but deletes!? */
                                 p_start = first_command_line;                                  p_start = first_command_line;
                                 p_sline = fcl_line;                                  p_sline = fcl_line;
Line 287 
Line 288 
                         ;                          ;
                 this_is_a_command = (isdigit((unsigned char)*s) &&                  this_is_a_command = (isdigit((unsigned char)*s) &&
                     (*t == 'd' || *t == 'c' || *t == 'a'));                      (*t == 'd' || *t == 'c' || *t == 'a'));
                 if (first_command_line < 0L && this_is_a_command) {                  if (first_command_line < 0 && this_is_a_command) {
                         first_command_line = this_line;                          first_command_line = this_line;
                         fcl_line = p_input_line;                          fcl_line = p_input_line;
                         p_indent = indent;      /* assume this for now */                          p_indent = indent;      /* assume this for now */
Line 319 
Line 320 
                         }                          }
                 }                  }
                 if ((!diff_type || diff_type == ED_DIFF) &&                  if ((!diff_type || diff_type == ED_DIFF) &&
                     first_command_line >= 0L &&                      first_command_line >= 0 &&
                     strEQ(s, ".\n")) {                      strEQ(s, ".\n")) {
                         p_indent = indent;                          p_indent = indent;
                         p_start = first_command_line;                          p_start = first_command_line;
Line 409 
Line 410 
  * 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(LINENUM file_pos, LINENUM file_line)  next_intuit_at(off_t 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 fseeko() to the actual diff listing.
  */   */
 static void  static void
 skip_to(LINENUM file_pos, LINENUM file_line)  skip_to(off_t file_pos, LINENUM file_line)
 {  {
         char    *ret;          char    *ret;
   
         if (p_base > file_pos)          if (p_base > file_pos)
                 fatal("Internal error: seek %ld>%ld\n", p_base, file_pos);                  fatal("Internal error: seek %lld>%lld\n",
                       (long long)p_base, (long long)file_pos);
         if (verbose && p_base < file_pos) {          if (verbose && p_base < file_pos) {
                 fseek(pfp, p_base, SEEK_SET);                  fseeko(pfp, p_base, SEEK_SET);
                 say("The text leading up to this was:\n--------------------------\n");                  say("The text leading up to this was:\n--------------------------\n");
                 while (ftell(pfp) < file_pos) {                  while (ftello(pfp) < file_pos) {
                         ret = fgets(buf, sizeof buf, pfp);                          ret = fgets(buf, sizeof buf, pfp);
                         if (ret == NULL)                          if (ret == NULL)
                                 fatal("Unexpected end of file\n");                                  fatal("Unexpected end of file\n");
Line 436 
Line 438 
                 }                  }
                 say("--------------------------\n");                  say("--------------------------\n");
         } else          } else
                 fseek(pfp, file_pos, SEEK_SET);                  fseeko(pfp, file_pos, SEEK_SET);
         p_input_line = file_line - 1;          p_input_line = file_line - 1;
 }  }
   
Line 466 
Line 468 
                 return true;                  return true;
         }          }
         if (c != EOF)          if (c != EOF)
                 fseek(pfp, -1L, SEEK_CUR);                  fseeko(pfp, -1, SEEK_CUR);
   
         return false;          return false;
 }  }
Line 477 
Line 479 
 bool  bool
 another_hunk(void)  another_hunk(void)
 {  {
         long    line_beginning;                 /* file pos of the current line */          off_t   line_beginning;                 /* file pos of the current line */
         LINENUM repl_beginning;                 /* index of --- line */          LINENUM repl_beginning;                 /* index of --- line */
         LINENUM fillcnt;                        /* #lines of missing ptrn or repl */          LINENUM fillcnt;                        /* #lines of missing ptrn or repl */
         LINENUM fillsrc;                        /* index of first line to copy */          LINENUM fillsrc;                        /* index of first line to copy */
Line 485 
Line 487 
         bool    ptrn_spaces_eaten;              /* ptrn was slightly misformed */          bool    ptrn_spaces_eaten;              /* ptrn was slightly misformed */
         bool    repl_could_be_missing;          /* no + or ! lines in this hunk */          bool    repl_could_be_missing;          /* no + or ! lines in this hunk */
         bool    repl_missing;                   /* we are now backtracking */          bool    repl_missing;                   /* we are now backtracking */
         long    repl_backtrack_position;        /* file pos of first repl line */          off_t   repl_backtrack_position;        /* file pos of first repl line */
         LINENUM repl_patch_line;                /* input line number for same */          LINENUM repl_patch_line;                /* input line number for same */
         LINENUM ptrn_copiable;                  /* # of copiable lines in ptrn */          LINENUM ptrn_copiable;                  /* # of copiable lines in ptrn */
         char    *s, *ret;          char    *s, *ret;
Line 502 
Line 504 
   
         p_max = hunkmax;        /* gets reduced when --- found */          p_max = hunkmax;        /* gets reduced when --- found */
         if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {          if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
                 line_beginning = ftell(pfp);                  line_beginning = ftello(pfp);
                 repl_beginning = 0;                  repl_beginning = 0;
                 fillcnt = 0;                  fillcnt = 0;
                 fillsrc = 0;                  fillsrc = 0;
Line 522 
Line 524 
                 p_context = 100;                  p_context = 100;
                 p_hunk_beg = p_input_line + 1;                  p_hunk_beg = p_input_line + 1;
                 while (p_end < p_max) {                  while (p_end < p_max) {
                         line_beginning = ftell(pfp);                          line_beginning = ftello(pfp);
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgets(buf, sizeof buf, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL) {                          if (ret == NULL) {
Line 630 
Line 632 
                                                 }                                                  }
                                         }                                          }
                                         repl_beginning = p_end;                                          repl_beginning = p_end;
                                         repl_backtrack_position = ftell(pfp);                                          repl_backtrack_position = ftello(pfp);
                                         repl_patch_line = p_input_line;                                          repl_patch_line = p_input_line;
                                         p_line[p_end] = savestr(buf);                                          p_line[p_end] = savestr(buf);
                                         if (out_of_mem) {                                          if (out_of_mem) {
Line 763 
Line 765 
                         p_input_line = repl_patch_line;                          p_input_line = repl_patch_line;
                         for (p_end--; p_end > repl_beginning; p_end--)                          for (p_end--; p_end > repl_beginning; p_end--)
                                 free(p_line[p_end]);                                  free(p_line[p_end]);
                         fseek(pfp, repl_backtrack_position, SEEK_SET);                          fseeko(pfp, repl_backtrack_position, SEEK_SET);
   
                         /* redundant 'new' context lines were omitted - set */                          /* redundant 'new' context lines were omitted - set */
                         /* up to fill them in from the old file context */                          /* up to fill them in from the old file context */
Line 837 
Line 839 
                         }                          }
                 }                  }
         } else if (diff_type == UNI_DIFF) {          } else if (diff_type == UNI_DIFF) {
                 long    line_beginning = ftell(pfp); /* file pos of the current line */                  off_t   line_beginning = ftello(pfp); /* file pos of the current line */
                 LINENUM fillsrc;        /* index of old lines */                  LINENUM fillsrc;        /* index of old lines */
                 LINENUM filldst;        /* index of new lines */                  LINENUM filldst;        /* index of new lines */
                 char    ch;                  char    ch;
Line 905 
Line 907 
                 context = 0;                  context = 0;
                 p_hunk_beg = p_input_line + 1;                  p_hunk_beg = p_input_line + 1;
                 while (fillsrc <= p_ptrn_lines || filldst <= p_end) {                  while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
                         line_beginning = ftell(pfp);                          line_beginning = ftello(pfp);
                         ret = pgets(buf, sizeof buf, pfp);                          ret = pgets(buf, sizeof buf, pfp);
                         p_input_line++;                          p_input_line++;
                         if (ret == NULL) {                          if (ret == NULL) {
Line 1007 
Line 1009 
                 char    hunk_type;                  char    hunk_type;
                 int     i;                  int     i;
                 LINENUM min, max;                  LINENUM min, max;
                 long    line_beginning = ftell(pfp);                  off_t   line_beginning = ftello(pfp);
   
                 p_context = 0;                  p_context = 0;
                 ret = pgets(buf, sizeof buf, pfp);                  ret = pgets(buf, sizeof buf, pfp);
Line 1367 
Line 1369 
 do_ed_script(void)  do_ed_script(void)
 {  {
         char    *t;          char    *t;
         long    beginning_of_this_line;          off_t   beginning_of_this_line;
         FILE    *pipefp = NULL;          FILE    *pipefp = NULL;
   
         if (!skip_rest_of_patch) {          if (!skip_rest_of_patch) {
Line 1380 
Line 1382 
                 pipefp = popen(buf, "w");                  pipefp = popen(buf, "w");
         }          }
         for (;;) {          for (;;) {
                 beginning_of_this_line = ftell(pfp);                  beginning_of_this_line = ftello(pfp);
                 if (pgets(buf, sizeof buf, pfp) == NULL) {                  if (pgets(buf, sizeof buf, pfp) == NULL) {
                         next_intuit_at(beginning_of_this_line, p_input_line);                          next_intuit_at(beginning_of_this_line, p_input_line);
                         break;                          break;
Line 1393 
Line 1395 
                     (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) {                      (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) {
                         if (pipefp != NULL)                          if (pipefp != NULL)
                                 fputs(buf, pipefp);                                  fputs(buf, pipefp);
                         if (*t == 's') {                          if (*t != 'd') {
                                 for (;;) {  
                                         bool continued = false;  
                                         t = buf + strlen(buf) - 1;  
                                         while (--t >= buf && *t == '\\')  
                                                 continued = !continued;  
                                         if (!continued ||  
                                             pgets(buf, sizeof buf, pfp) == NULL)  
                                                 break;  
                                         if (pipefp != NULL)  
                                                 fputs(buf, pipefp);  
                                 }  
                         } else if (*t != 'd') {  
                                 while (pgets(buf, sizeof buf, pfp) != NULL) {                                  while (pgets(buf, sizeof buf, pfp) != NULL) {
                                         p_input_line++;                                          p_input_line++;
                                         if (pipefp != NULL)                                          if (pipefp != NULL)
Line 1460 
Line 1450 
         }          }
         if (path == NULL && !assume_exists) {          if (path == NULL && !assume_exists) {
                 /*                  /*
                  * No files found, check to see if the diff could be                   * No files found, look for something we can checkout from
                  * creating a new file.                   * RCS/SCCS dirs.  Same order as above.
                  */                   */
                   for (i = 0; i < MAX_FILE; i++) {
                           if (names[i].path != NULL &&
                               (path = checked_in(names[i].path)) != NULL)
                                   break;
                   }
                   /*
                    * Still no match?  Check to see if the diff could be creating
                    * a new file.
                    */
                 if (path == NULL && ok_to_create_file &&                  if (path == NULL && ok_to_create_file &&
                     names[NEW_FILE].path != NULL)                      names[NEW_FILE].path != NULL)
                         path = names[NEW_FILE].path;                          path = names[NEW_FILE].path;
Line 1472 
Line 1471 
 }  }
   
 static char *  static char *
 compare_names(const struct file_name *names, bool assume_exists)  compare_names(const struct file_name *names, bool assume_exists, int phase)
 {  {
         size_t min_components, min_baselen, min_len, tmp;          size_t min_components, min_baselen, min_len, tmp;
         char *best = NULL;          char *best = NULL;
Line 1489 
Line 1488 
         min_components = min_baselen = min_len = SIZE_MAX;          min_components = min_baselen = min_len = SIZE_MAX;
         for (i = INDEX_FILE; i >= OLD_FILE; i--) {          for (i = INDEX_FILE; i >= OLD_FILE; i--) {
                 path = names[i].path;                  path = names[i].path;
                 if (path == NULL || (!names[i].exists && !assume_exists))                  if (path == NULL ||
                       (phase == 1 && !names[i].exists && !assume_exists) ||
                       (phase == 2 && checked_in(path) == NULL))
                         continue;                          continue;
                 if ((tmp = num_components(path)) > min_components)                  if ((tmp = num_components(path)) > min_components)
                         continue;                          continue;
Line 1520 
Line 1521 
 {  {
         char *best;          char *best;
   
         best = compare_names(names, assume_exists);          best = compare_names(names, assume_exists, 1);
           if (best == NULL) {
         /* No match?  Check to see if the diff could be creating a new file. */                  best = compare_names(names, assume_exists, 2);
         if (best == NULL && ok_to_create_file)                  /*
                 best = names[NEW_FILE].path;                   * Still no match?  Check to see if the diff could be creating
                    * a new file.
                    */
                   if (best == NULL && ok_to_create_file &&
                       names[NEW_FILE].path != NULL)
                           best = names[NEW_FILE].path;
           }
         return best ? savestr(best) : NULL;          return best ? savestr(best) : NULL;
 }  }
   

Legend:
Removed from v.1.41.6.2  
changed lines
  Added in v.1.42