1 /* fix file difflist - update file from difflist Author: Erik Baalbergen */
4 /* Notes: files old and old.patch are equal after the following commands
5 diff old new > difflist
6 patch old difflist > old.patch
7 * the diff output is assumed to be produced by my diff program.
8 * the difflist has the following form:
10 chunk ::= append | delete | change ;
11 append ::= n1 'a' n2 [',' n3]? '\n' ['> ' line '\n'](n3 - n2 + 1)
12 delete ::= n1 [',' n2]? 'd' n3 '\n' ['< ' line '\n'](n2 - n1 + 1)
13 change ::= n1 [',' n2]? 'c' n3 [',' n4]? '\n'
14 ['< ' line '\n'](n2 - n1 + 1)
16 ['> ' line '\n'](n4 - n3 + 1)
18 - n[1234] is an unsigned integer
19 - "[pat](expr)" means "(expr) occurences of pat"
20 - "[pat]?" means "either pat or nothing"
21 * the information in the diff listing is checked against the file to which
22 it is applied; an error is printed if there is a conflict
31 #define IGNORE_WHITE_SPACE /* This makes it white space insensitive */
33 #ifdef IGNORE_WHITE_SPACE
34 #define strcmp strwcmp
39 char *prog
= 0, *processing
= 0;
41 _PROTOTYPE(char *getline
, (FILE *fp
, char *b
));
42 _PROTOTYPE(char *range
, (char *s
, int *p1
, int *p2
));
43 _PROTOTYPE(int getcommand
, (FILE *fp
, int *o1
, int *o2
, char *pcmd
, int *n1
, int *n2
));
44 _PROTOTYPE(void fatal
, (const char *s
, ...));
45 _PROTOTYPE(int strwcmp
, (char *s1
, char *s2
));
46 _PROTOTYPE(int whitespace
, (int ch
));
49 getline(FILE *fp
, char *b
)
51 if (fgets(b
, LINELEN
, fp
) == NULL
) fatal("unexpected eof");
56 #define copy(str) printf("%s", str)
58 int main(int argc
, char **argv
)
60 char cmd
, *fl
, *fd
, obuf
[LINELEN
], nbuf
[LINELEN
];
61 int o1
, o2
, n1
, n2
, here
;
66 if (argc
!= 3) fatal("use: %s original-file diff-list-file", prog
);
67 if ((fpf
= fopen(argv
[1], "r")) == NULL
) fatal("can't read %s", argv
[1]);
68 if ((fpd
= fopen(argv
[2], "r")) == NULL
) fatal("can't read %s", argv
[2]);
70 while (getcommand(fpd
, &o1
, &o2
, &cmd
, &n1
, &n2
)) {
71 while (here
< o1
- 1) {
73 copy(getline(fpf
, obuf
));
78 if (cmd
== 'd' && n1
!= n2
) fatal("delete count conflict");
80 fl
= getline(fpf
, obuf
);
82 fd
= getline(fpd
, nbuf
);
83 if (strncmp(fd
, "<", (size_t)1))
84 fatal("illegal delete line");
85 if (strcmp(fl
, fd
+ 2))
86 fatal("delete line conflict");
89 if (cmd
== 'd') break;
90 if (strcmp(getline(fpd
, nbuf
), "---\n"))
91 fatal("illegal separator in chunk");
95 if (o1
!= o2
) fatal("append count conflict");
96 copy(getline(fpf
, obuf
));
100 if (strncmp(getline(fpd
, nbuf
), ">", (size_t)1))
101 fatal("illegal append line");
108 while (fgets(obuf
, LINELEN
, fpf
) != NULL
) copy(obuf
);
117 register int v1
= 0, v2
;
119 while (isdigit(*s
)) v1
= 10 * v1
+ *s
++ - '0';
124 while (isdigit(*s
)) v2
= 10 * v2
+ *s
++ - '0';
126 if (v1
> v2
) fatal("illegal range");
132 int getcommand(fp
, o1
, o2
, pcmd
, n1
, n2
)
134 int *o1
, *o2
, *n1
, *n2
;
141 if ((s
= fgets(buf
, LINELEN
, fp
)) == NULL
) return 0;
142 s
= range(s
, o1
, o2
);
143 if ((cmd
= *s
++) != 'a' && cmd
!= 'c' && cmd
!= 'd')
144 fatal("illegal command");
145 s
= range(s
, n1
, n2
);
146 if (*s
!= '\n' && s
[1] != '\0')
147 fatal("extra characters at end of command: %s", s
);
153 void fatal(const char *s
, ...)
158 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
159 vfprintf(stderr
, s
, args
);
160 fprintf(stderr
, "\n");
165 /* the K&R lib does not have vfprintf */
169 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
170 fprintf(stderr
, s
, a
);
171 fprintf(stderr
, "\n");
176 #ifdef IGNORE_WHITE_SPACE
178 /* This routine is a white space insensitive version of strcmp.
179 It is needed for testing things which might have undergone
180 tab conversion or trailing space removal
181 Bret Mckee June, 1988 */
186 char *x1
= s1
, *x2
= s2
;
188 /* Remove leading white space */
189 while (whitespace(*s1
)) s1
++;
190 while (whitespace(*s2
)) s2
++;
192 while ((*s1
== *s2
) && *s1
&& *s2
) {
196 ; /* consume identical characters */
197 while (whitespace(*s1
)) s1
++;
198 while (whitespace(*s2
)) s2
++;
199 } while (*s1
&& *s2
&& (*s1
== *s2
));
201 fprintf(stderr
, "Failing for (%x)[%s]\n (%x)[%s]\n",
202 (int) *s1
, x1
, (int) *s2
, x2
);