pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / simple / fix.c
blobaf04cd60e1f74e416302413280a45bc20487f646
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 _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));
48 char *
49 getline(FILE *fp, char *b)
51 if (fgets(b, LINELEN, fp) == NULL) fatal("unexpected eof");
53 return b;
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;
62 FILE *fpf, *fpd;
64 prog = argv[0];
65 processing = argv[1];
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]);
69 here = 0;
70 while (getcommand(fpd, &o1, &o2, &cmd, &n1, &n2)) {
71 while (here < o1 - 1) {
72 here++;
73 copy(getline(fpf, obuf));
75 switch (cmd) {
76 case 'c':
77 case 'd':
78 if (cmd == 'd' && n1 != n2) fatal("delete count conflict");
79 while (o1 <= o2) {
80 fl = getline(fpf, obuf);
81 here++;
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");
87 o1++;
89 if (cmd == 'd') break;
90 if (strcmp(getline(fpd, nbuf), "---\n"))
91 fatal("illegal separator in chunk");
92 /* FALLTHROUGH */
93 case 'a':
94 if (cmd == 'a') {
95 if (o1 != o2) fatal("append count conflict");
96 copy(getline(fpf, obuf));
97 here++;
99 while (n1 <= n2) {
100 if (strncmp(getline(fpd, nbuf), ">", (size_t)1))
101 fatal("illegal append line");
102 copy(nbuf + 2);
103 n1++;
105 break;
108 while (fgets(obuf, LINELEN, fpf) != NULL) copy(obuf);
109 return(0);
112 char *
113 range(s, p1, p2)
114 char *s;
115 int *p1, *p2;
117 register int v1 = 0, v2;
119 while (isdigit(*s)) v1 = 10 * v1 + *s++ - '0';
120 v2 = v1;
121 if (*s == ',') {
122 s++;
123 v2 = 0;
124 while (isdigit(*s)) v2 = 10 * v2 + *s++ - '0';
126 if (v1 > v2) fatal("illegal range");
127 *p1 = v1;
128 *p2 = v2;
129 return s;
132 int getcommand(fp, o1, o2, pcmd, n1, n2)
133 FILE *fp;
134 int *o1, *o2, *n1, *n2;
135 char *pcmd;
137 char buf[LINELEN];
138 register char *s;
139 char cmd;
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);
148 *pcmd = cmd;
149 return 1;
152 #ifdef __STDC__
153 void fatal(const char *s, ...)
155 va_list args;
157 va_start (args, s);
158 fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
159 vfprintf(stderr, s, args);
160 fprintf(stderr, "\n");
161 va_end(args);
162 exit(1);
164 #else
165 /* the K&R lib does not have vfprintf */
166 void fatal(s, a)
167 const char *s, *a;
169 fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
170 fprintf(stderr, s, a);
171 fprintf(stderr, "\n");
172 exit(1);
174 #endif
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 */
183 int strwcmp(s1, s2)
184 char *s1, *s2;
186 char *x1 = s1, *x2 = s2;
188 /* Remove leading white space */
189 while (whitespace(*s1)) s1++;
190 while (whitespace(*s2)) s2++;
191 do {
192 while ((*s1 == *s2) && *s1 && *s2) {
193 s1++;
194 s2++;
196 ; /* consume identical characters */
197 while (whitespace(*s1)) s1++;
198 while (whitespace(*s2)) s2++;
199 } while (*s1 && *s2 && (*s1 == *s2));
200 if (*s1 - *s2)
201 fprintf(stderr, "Failing for (%x)[%s]\n (%x)[%s]\n",
202 (int) *s1, x1, (int) *s2, x2);
203 return(*s1 - *s2);
206 int whitespace(ch)
207 char ch;
209 switch (ch) {
210 case ' ':
211 case '\n':
212 case 0x0D:
213 case '\t':
214 return(1);
215 default: return(0);
219 #endif