toybox

changeset 376:35c8beb54800

For CFG_TOYBOX_DEBUG add -x option to patch to dump lots of debug output about the search and match.
author Rob Landley <rob@landley.net>
date Sun Feb 28 14:11:41 2010 -0600 (2010-02-28)
parents 9f4e5e15597c
children 15ca7e49b6ff
files toys/patch.c
line diff
     1.1 --- a/toys/patch.c	Sun Feb 28 12:16:43 2010 -0600
     1.2 +++ b/toys/patch.c	Sun Feb 28 14:11:41 2010 -0600
     1.3 @@ -21,7 +21,7 @@
     1.4   * -F fuzz (number, default 2)
     1.5   * [file] which file to patch
     1.6  
     1.7 -USE_PATCH(NEWTOY(patch, "up#i:R", TOYFLAG_USR|TOYFLAG_BIN))
     1.8 +USE_PATCH(NEWTOY(patch, USE_TOYBOX_DEBUG("x")"up#i:R", TOYFLAG_USR|TOYFLAG_BIN))
     1.9  
    1.10  config PATCH
    1.11  	bool "patch"
    1.12 @@ -68,6 +68,8 @@
    1.13  // state = 3: write whole line to fileout
    1.14  // state > 3: write line+1 to fileout when *line != state
    1.15  
    1.16 +#define PATCH_DEBUG (CFG_TOYBOX_DEBUG && (toys.optflags & 16))
    1.17 +
    1.18  static void do_line(void *data)
    1.19  {
    1.20  	struct double_list *dlist = (struct double_list *)data;
    1.21 @@ -76,6 +78,8 @@
    1.22  		fdprintf(TT.state == 2 ? 2 : TT.fileout,
    1.23  			"%s\n", dlist->data+(TT.state>3 ? 1 : 0));
    1.24  
    1.25 +	if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
    1.26 +
    1.27  	free(dlist->data);
    1.28  	free(data);
    1.29  }
    1.30 @@ -104,7 +108,14 @@
    1.31  	TT.state = 0;
    1.32  }
    1.33  
    1.34 -static int apply_hunk(void)
    1.35 +// Given a hunk of a unified diff, make the appropriate change to the file.
    1.36 +// This does not use the location information, but instead treats a hunk
    1.37 +// as a sort of regex.  Copies data from input to output until it finds
    1.38 +// the change to be made, then outputs the changed data and returns.
    1.39 +// (Finding EOF first is an error.)  This is a single pass operation, so
    1.40 +// multiple hunks must occur in order in the file.
    1.41 +
    1.42 +static int apply_one_hunk(void)
    1.43  {
    1.44  	struct double_list *plist, *buf = NULL, *check;
    1.45  	int matcheof = 0, reverse = toys.optflags & FLAG_REVERSE, backwarn = 0;
    1.46 @@ -116,9 +127,12 @@
    1.47  	for (plist = TT.current_hunk; plist; plist = plist->next) {
    1.48  		if (plist->data[0]==' ') matcheof++;
    1.49  		else matcheof = 0;
    1.50 +		if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
    1.51  	}
    1.52  	matcheof = matcheof < TT.context;
    1.53  
    1.54 +	if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
    1.55 +
    1.56  	// Loop through input data searching for this hunk.  Match all context
    1.57  	// lines and all lines to be removed until we've found the end of a
    1.58  	// complete hunk.
    1.59 @@ -144,13 +158,15 @@
    1.60  
    1.61  		// Is this EOF?
    1.62  		if (!data) {
    1.63 +			if (PATCH_DEBUG) fdprintf(2, "INEOF\n");
    1.64 +
    1.65  			// Does this hunk need to match EOF?
    1.66  			if (!plist && matcheof) break;
    1.67  
    1.68  			// File ended before we found a place for this hunk.
    1.69  			fail_hunk();
    1.70  			goto done;
    1.71 -		}
    1.72 +		} else if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
    1.73  		check = dlist_add(&buf, data);
    1.74  
    1.75  		// Compare this line with next expected line of hunk.
    1.76 @@ -168,6 +184,9 @@
    1.77  				// Match failed.  Write out first line of buffered data and
    1.78  				// recheck remaining buffered data for a new match.
    1.79  	
    1.80 +				if (PATCH_DEBUG)
    1.81 +					fdprintf(2, "NOT: %s\n", plist->data);
    1.82 +
    1.83  				TT.state = 3;
    1.84  				check = llist_pop(&buf);
    1.85  				check->prev->next = buf;
    1.86 @@ -183,6 +202,8 @@
    1.87  				}
    1.88  				check = buf;
    1.89  			} else {
    1.90 +				if (PATCH_DEBUG)
    1.91 +					fdprintf(2, "MAYBE: %s\n", plist->data);
    1.92  				// This line matches.  Advance plist, detect successful match.
    1.93  				plist = plist->next;
    1.94  				if (!plist && !matcheof) goto out;
    1.95 @@ -206,6 +227,9 @@
    1.96  	return TT.state;
    1.97  }
    1.98  
    1.99 +// Read a patch file and find hunks, opening/creating/deleting files.
   1.100 +// Call apply_one_hunk() on each hunk.
   1.101 +
   1.102  // state 0: Not in a hunk, look for +++.
   1.103  // state 1: Found +++ file indicator, look for @@
   1.104  // state 2: In hunk: counting initial context lines
   1.105 @@ -247,7 +271,7 @@
   1.106  
   1.107  				// If we've consumed all expected hunk lines, apply the hunk.
   1.108  
   1.109 -				if (!TT.oldlen && !TT.newlen) state = apply_hunk();
   1.110 +				if (!TT.oldlen && !TT.newlen) state = apply_one_hunk();
   1.111  				continue;
   1.112  			}
   1.113  			fail_hunk();