iso9660fs: initialize buffer cache
[minix.git] / commands / fix / fix.c
blob3952fc2f985f403680aa053c11dad94c924ebe2f
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:
9 difflist ::= chunk*
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)
15 '---\n'
16 ['> ' line '\n'](n4 - n3 + 1)
17 where
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
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
31 #define IGNORE_WHITE_SPACE /* This makes it white space insensitive */
33 #ifdef IGNORE_WHITE_SPACE
34 #define strcmp strwcmp
35 #endif
37 #define LINELEN 1024
39 char *prog = 0, *processing = 0;
41 #ifdef __NBSD_LIBC
42 /* getline() already declared in stdio.h */
43 #define getline fix_getline
44 #endif
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
48 *n2);
49 void fatal(const char *s, ...);
50 int strwcmp(char *s1, char *s2);
51 int whitespace(int ch);
53 char *
54 getline(FILE *fp, char *b)
56 if (fgets(b, LINELEN, fp) == NULL) fatal("unexpected eof");
58 return b;
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;
67 FILE *fpf, *fpd;
69 prog = argv[0];
70 processing = argv[1];
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]);
74 here = 0;
75 while (getcommand(fpd, &o1, &o2, &cmd, &n1, &n2)) {
76 while (here < o1 - 1) {
77 here++;
78 copy(getline(fpf, obuf));
80 switch (cmd) {
81 case 'c':
82 case 'd':
83 if (cmd == 'd' && n1 != n2) fatal("delete count conflict");
84 while (o1 <= o2) {
85 fl = getline(fpf, obuf);
86 here++;
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");
92 o1++;
94 if (cmd == 'd') break;
95 if (strcmp(getline(fpd, nbuf), "---\n"))
96 fatal("illegal separator in chunk");
97 /* FALLTHROUGH */
98 case 'a':
99 if (cmd == 'a') {
100 if (o1 != o2) fatal("append count conflict");
101 copy(getline(fpf, obuf));
102 here++;
104 while (n1 <= n2) {
105 if (strncmp(getline(fpd, nbuf), ">", (size_t)1))
106 fatal("illegal append line");
107 copy(nbuf + 2);
108 n1++;
110 break;
113 while (fgets(obuf, LINELEN, fpf) != NULL) copy(obuf);
114 return(0);
117 char *
118 range(s, p1, p2)
119 char *s;
120 int *p1, *p2;
122 register int v1 = 0, v2;
124 while (isdigit(*s)) v1 = 10 * v1 + *s++ - '0';
125 v2 = v1;
126 if (*s == ',') {
127 s++;
128 v2 = 0;
129 while (isdigit(*s)) v2 = 10 * v2 + *s++ - '0';
131 if (v1 > v2) fatal("illegal range");
132 *p1 = v1;
133 *p2 = v2;
134 return s;
137 int getcommand(fp, o1, o2, pcmd, n1, n2)
138 FILE *fp;
139 int *o1, *o2, *n1, *n2;
140 char *pcmd;
142 char buf[LINELEN];
143 register char *s;
144 char cmd;
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);
153 *pcmd = cmd;
154 return 1;
157 #ifdef __STDC__
158 void fatal(const char *s, ...)
160 va_list args;
162 va_start (args, s);
163 fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
164 vfprintf(stderr, s, args);
165 fprintf(stderr, "\n");
166 va_end(args);
167 exit(1);
169 #else
170 /* the K&R lib does not have vfprintf */
171 void fatal(s, a)
172 const char *s, *a;
174 fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
175 fprintf(stderr, s, a);
176 fprintf(stderr, "\n");
177 exit(1);
179 #endif
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 */
188 int strwcmp(s1, s2)
189 char *s1, *s2;
191 char *x1 = s1, *x2 = s2;
193 /* Remove leading white space */
194 while (whitespace(*s1)) s1++;
195 while (whitespace(*s2)) s2++;
196 do {
197 while ((*s1 == *s2) && *s1 && *s2) {
198 s1++;
199 s2++;
201 ; /* consume identical characters */
202 while (whitespace(*s1)) s1++;
203 while (whitespace(*s2)) s2++;
204 } while (*s1 && *s2 && (*s1 == *s2));
205 if (*s1 - *s2)
206 fprintf(stderr, "Failing for (%x)[%s]\n (%x)[%s]\n",
207 (int) *s1, x1, (int) *s2, x2);
208 return(*s1 - *s2);
211 int whitespace(ch)
212 char ch;
214 switch (ch) {
215 case ' ':
216 case '\n':
217 case 0x0D:
218 case '\t':
219 return(1);
220 default: return(0);
224 #endif