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;
42 /* getline() already declared in stdio.h */
43 #define getline fix_getline
45 char *getline(FILE *fp
, char *b
);
46 char *range(char *s
, int *p1
, int *p2
);
47 int getcommand(FILE *fp
, int *o1
, int *o2
, char *pcmd
, int *n1
, int
49 void fatal(const char *s
, ...);
50 int strwcmp(char *s1
, char *s2
);
51 int whitespace(int ch
);
54 getline(FILE *fp
, char *b
)
56 if (fgets(b
, LINELEN
, fp
) == NULL
) fatal("unexpected eof");
61 #define copy(str) printf("%s", str)
63 int main(int argc
, char **argv
)
65 char cmd
, *fl
, *fd
, obuf
[LINELEN
], nbuf
[LINELEN
];
66 int o1
, o2
, n1
, n2
, here
;
71 if (argc
!= 3) fatal("use: %s original-file diff-list-file", prog
);
72 if ((fpf
= fopen(argv
[1], "r")) == NULL
) fatal("can't read %s", argv
[1]);
73 if ((fpd
= fopen(argv
[2], "r")) == NULL
) fatal("can't read %s", argv
[2]);
75 while (getcommand(fpd
, &o1
, &o2
, &cmd
, &n1
, &n2
)) {
76 while (here
< o1
- 1) {
78 copy(getline(fpf
, obuf
));
83 if (cmd
== 'd' && n1
!= n2
) fatal("delete count conflict");
85 fl
= getline(fpf
, obuf
);
87 fd
= getline(fpd
, nbuf
);
88 if (strncmp(fd
, "<", (size_t)1))
89 fatal("illegal delete line");
90 if (strcmp(fl
, fd
+ 2))
91 fatal("delete line conflict");
94 if (cmd
== 'd') break;
95 if (strcmp(getline(fpd
, nbuf
), "---\n"))
96 fatal("illegal separator in chunk");
100 if (o1
!= o2
) fatal("append count conflict");
101 copy(getline(fpf
, obuf
));
105 if (strncmp(getline(fpd
, nbuf
), ">", (size_t)1))
106 fatal("illegal append line");
113 while (fgets(obuf
, LINELEN
, fpf
) != NULL
) copy(obuf
);
122 register int v1
= 0, v2
;
124 while (isdigit(*s
)) v1
= 10 * v1
+ *s
++ - '0';
129 while (isdigit(*s
)) v2
= 10 * v2
+ *s
++ - '0';
131 if (v1
> v2
) fatal("illegal range");
137 int getcommand(fp
, o1
, o2
, pcmd
, n1
, n2
)
139 int *o1
, *o2
, *n1
, *n2
;
146 if ((s
= fgets(buf
, LINELEN
, fp
)) == NULL
) return 0;
147 s
= range(s
, o1
, o2
);
148 if ((cmd
= *s
++) != 'a' && cmd
!= 'c' && cmd
!= 'd')
149 fatal("illegal command");
150 s
= range(s
, n1
, n2
);
151 if (*s
!= '\n' && s
[1] != '\0')
152 fatal("extra characters at end of command: %s", s
);
158 void fatal(const char *s
, ...)
163 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
164 vfprintf(stderr
, s
, args
);
165 fprintf(stderr
, "\n");
170 /* the K&R lib does not have vfprintf */
174 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
175 fprintf(stderr
, s
, a
);
176 fprintf(stderr
, "\n");
181 #ifdef IGNORE_WHITE_SPACE
183 /* This routine is a white space insensitive version of strcmp.
184 It is needed for testing things which might have undergone
185 tab conversion or trailing space removal
186 Bret Mckee June, 1988 */
191 char *x1
= s1
, *x2
= s2
;
193 /* Remove leading white space */
194 while (whitespace(*s1
)) s1
++;
195 while (whitespace(*s2
)) s2
++;
197 while ((*s1
== *s2
) && *s1
&& *s2
) {
201 ; /* consume identical characters */
202 while (whitespace(*s1
)) s1
++;
203 while (whitespace(*s2
)) s2
++;
204 } while (*s1
&& *s2
&& (*s1
== *s2
));
206 fprintf(stderr
, "Failing for (%x)[%s]\n (%x)[%s]\n",
207 (int) *s1
, x1
, (int) *s2
, x2
);