source: trunk/minix/commands/simple/fix.c@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.1 KB
Line 
1/* fix file difflist - update file from difflist Author: Erik Baalbergen */
2
3
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
23*/
24
25#include <ctype.h>
26#include <stdarg.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdio.h>
30
31#define IGNORE_WHITE_SPACE /* This makes it white space insensitive */
32
33#ifdef IGNORE_WHITE_SPACE
34#define strcmp strwcmp
35#endif
36
37#define LINELEN 1024
38
39char *prog = 0, *processing = 0;
40
41_PROTOTYPE(int main, (int argc, char **argv));
42_PROTOTYPE(char *getline, (FILE *fp, char *b));
43_PROTOTYPE(char *range, (char *s, int *p1, int *p2));
44_PROTOTYPE(int getcommand, (FILE *fp, int *o1, int *o2, char *pcmd, int *n1, int *n2));
45_PROTOTYPE(void fatal, (char *s, ...));
46_PROTOTYPE(int strwcmp, (char *s1, char *s2));
47_PROTOTYPE(int whitespace, (int ch));
48
49char *
50 getline(fp, b)
51FILE *fp;
52char *b;
53{
54 if (fgets(b, LINELEN, fp) == NULL) fatal("unexpected eof");
55
56 return b;
57}
58
59#define copy(str) printf("%s", str)
60
61int main(argc, argv)
62int argc;
63char **argv;
64{
65 char cmd, *fl, *fd, obuf[LINELEN], nbuf[LINELEN];
66 int o1, o2, n1, n2, here;
67 FILE *fpf, *fpd;
68
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));
79 }
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++;
93 }
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++;
103 }
104 while (n1 <= n2) {
105 if (strncmp(getline(fpd, nbuf), ">", (size_t)1))
106 fatal("illegal append line");
107 copy(nbuf + 2);
108 n1++;
109 }
110 break;
111 }
112 }
113 while (fgets(obuf, LINELEN, fpf) != NULL) copy(obuf);
114 return(0);
115}
116
117char *
118 range(s, p1, p2)
119char *s;
120int *p1, *p2;
121{
122 register int v1 = 0, v2;
123
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';
130 }
131 if (v1 > v2) fatal("illegal range");
132 *p1 = v1;
133 *p2 = v2;
134 return s;
135}
136
137int getcommand(fp, o1, o2, pcmd, n1, n2)
138FILE *fp;
139int *o1, *o2, *n1, *n2;
140char *pcmd;
141{
142 char buf[LINELEN];
143 register char *s;
144 char cmd;
145
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;
155}
156
157#ifdef __STDC__
158void fatal(char *s, ...)
159{
160 va_list args;
161
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);
168}
169#else
170/* the K&R lib does not have vfprintf */
171void fatal(s, a)
172char *s, *a;
173{
174 fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
175 fprintf(stderr, s, a);
176 fprintf(stderr, "\n");
177 exit(1);
178}
179#endif
180
181#ifdef IGNORE_WHITE_SPACE
182
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 */
187
188int strwcmp(s1, s2)
189char *s1, *s2;
190{
191 char *x1 = s1, *x2 = s2;
192
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++;
200 }
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);
209}
210
211int whitespace(ch)
212char ch;
213{
214 switch (ch) {
215 case ' ':
216 case '\n':
217 case 0x0D:
218 case '\t':
219 return(1);
220 default: return(0);
221}
222}
223
224#endif
Note: See TracBrowser for help on using the repository browser.