1 /* fix file difflist - update file from difflist Author: Erik Baalbergen */
2 /* Change all K&R-style functions declarations to ANSI-style
3 declarations - Author: Alexandre Beletti (rhiguita@gmail.com) */
5 /* Notes: files old and old.patch are equal after the following commands
6 diff old new > difflist
7 patch old difflist > old.patch
8 * the diff output is assumed to be produced by my diff program.
9 * the difflist has the following form:
11 chunk ::= append | delete | change ;
12 append ::= n1 'a' n2 [',' n3]? '\n' ['> ' line '\n'](n3 - n2 + 1)
13 delete ::= n1 [',' n2]? 'd' n3 '\n' ['< ' line '\n'](n2 - n1 + 1)
14 change ::= n1 [',' n2]? 'c' n3 [',' n4]? '\n'
15 ['< ' line '\n'](n2 - n1 + 1)
17 ['> ' line '\n'](n4 - n3 + 1)
19 - n[1234] is an unsigned integer
20 - "[pat](expr)" means "(expr) occurences of pat"
21 - "[pat]?" means "either pat or nothing"
22 * the information in the diff listing is checked against the file to which
23 it is applied; an error is printed if there is a conflict
32 #define IGNORE_WHITE_SPACE /* This makes it white space insensitive */
34 #ifdef IGNORE_WHITE_SPACE
35 #define strcmp strwcmp
40 char *prog
= 0, *processing
= 0;
42 /* getline() already declared in stdio.h */
43 #define getline fix_getline
44 char *getline(FILE *fp
, char *b
);
45 char *range(char *s
, int *p1
, int *p2
);
46 int getcommand(FILE *fp
, int *o1
, int *o2
, char *pcmd
, int *n1
, int
48 void fatal(const char *s
, ...);
49 int strwcmp(char *s1
, char *s2
);
50 int whitespace(char ch
);
53 getline(FILE *fp
, char *b
)
55 if (fgets(b
, LINELEN
, fp
) == NULL
) fatal("unexpected eof");
60 #define copy(str) printf("%s", str)
62 int main(int argc
, char **argv
)
64 char cmd
, *fl
, *fd
, obuf
[LINELEN
], nbuf
[LINELEN
];
65 int o1
, o2
, n1
, n2
, here
;
70 if (argc
!= 3) fatal("use: %s original-file diff-list-file", prog
);
71 if ((fpf
= fopen(argv
[1], "r")) == NULL
) fatal("can't read %s", argv
[1]);
72 if ((fpd
= fopen(argv
[2], "r")) == NULL
) fatal("can't read %s", argv
[2]);
74 while (getcommand(fpd
, &o1
, &o2
, &cmd
, &n1
, &n2
)) {
75 while (here
< o1
- 1) {
77 copy(getline(fpf
, obuf
));
82 if (cmd
== 'd' && n1
!= n2
) fatal("delete count conflict");
84 fl
= getline(fpf
, obuf
);
86 fd
= getline(fpd
, nbuf
);
87 if (strncmp(fd
, "<", (size_t)1))
88 fatal("illegal delete line");
89 if (strcmp(fl
, fd
+ 2))
90 fatal("delete line conflict");
93 if (cmd
== 'd') break;
94 if (strcmp(getline(fpd
, nbuf
), "---\n"))
95 fatal("illegal separator in chunk");
99 if (o1
!= o2
) fatal("append count conflict");
100 copy(getline(fpf
, obuf
));
104 if (strncmp(getline(fpd
, nbuf
), ">", (size_t)1))
105 fatal("illegal append line");
112 while (fgets(obuf
, LINELEN
, fpf
) != NULL
) copy(obuf
);
117 range(char *s
, int *p1
, int *p2
)
119 register int v1
= 0, v2
;
121 while (isdigit(*s
)) v1
= 10 * v1
+ *s
++ - '0';
126 while (isdigit(*s
)) v2
= 10 * v2
+ *s
++ - '0';
128 if (v1
> v2
) fatal("illegal range");
134 int getcommand(FILE *fp
, int *o1
, int *o2
, char *pcmd
, int *n1
, int *n2
)
140 if ((s
= fgets(buf
, LINELEN
, fp
)) == NULL
) return 0;
141 s
= range(s
, o1
, o2
);
142 if ((cmd
= *s
++) != 'a' && cmd
!= 'c' && cmd
!= 'd')
143 fatal("illegal command");
144 s
= range(s
, n1
, n2
);
145 if (*s
!= '\n' && s
[1] != '\0')
146 fatal("extra characters at end of command: %s", s
);
152 void fatal(const char *s
, ...)
157 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
158 vfprintf(stderr
, s
, args
);
159 fprintf(stderr
, "\n");
164 /* the K&R lib does not have vfprintf */
165 void fatal(const char *s
, const char *a
)
167 fprintf(stderr
, "%s: processing: %s fatal: ", prog
, processing
);
168 fprintf(stderr
, s
, a
);
169 fprintf(stderr
, "\n");
174 #ifdef IGNORE_WHITE_SPACE
176 /* This routine is a white space insensitive version of strcmp.
177 It is needed for testing things which might have undergone
178 tab conversion or trailing space removal
179 Bret Mckee June, 1988 */
181 int strwcmp(char *s1
, char *s2
)
183 char *x1
= s1
, *x2
= s2
;
185 /* Remove leading white space */
186 while (whitespace(*s1
)) s1
++;
187 while (whitespace(*s2
)) s2
++;
189 while ((*s1
== *s2
) && *s1
&& *s2
) {
193 ; /* consume identical characters */
194 while (whitespace(*s1
)) s1
++;
195 while (whitespace(*s2
)) s2
++;
196 } while (*s1
&& *s2
&& (*s1
== *s2
));
198 fprintf(stderr
, "Failing for (%x)[%s]\n (%x)[%s]\n",
199 (int) *s1
, x1
, (int) *s2
, x2
);
203 int whitespace(char ch
)