summaryrefslogtreecommitdiff
path: root/minix/commands/fix/fix.c
blob: 40d730ff3a66f87d3ce46ec7e47191a2d2991d98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* fix file difflist - update file from difflist     Author: Erik Baalbergen */
/* Change all K&R-style functions declarations to ANSI-style 
declarations - Author: Alexandre Beletti (rhiguita@gmail.com) */

/* Notes: files old and old.patch are equal after the following commands
     diff old new > difflist
     patch old difflist > old.patch
   * the diff output is assumed to be produced by my diff program.
   * the difflist has the following form:
     difflist ::= chunk*
     chunk ::= append | delete | change ;
     append ::= n1 'a' n2 [',' n3]? '\n' ['> ' line '\n'](n3 - n2 + 1)
     delete ::= n1 [',' n2]? 'd' n3 '\n' ['< ' line '\n'](n2 - n1 + 1)
     change ::= n1 [',' n2]? 'c' n3 [',' n4]? '\n'
	      ['< ' line '\n'](n2 - n1 + 1)
	      '---\n'
	      ['> ' line '\n'](n4 - n3 + 1)
     where
     - n[1234] is an unsigned integer
     - "[pat](expr)" means "(expr) occurences of pat"
     - "[pat]?" means "either pat or nothing"
   * the information in the diff listing is checked against the file to which
     it is applied; an error is printed if there is a conflict
*/

#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define IGNORE_WHITE_SPACE	/* This makes it white space insensitive */

#ifdef IGNORE_WHITE_SPACE
#define strcmp strwcmp
#endif

#define LINELEN	1024

char *prog = 0, *processing = 0;

/* getline() already declared in stdio.h */
#define getline fix_getline
char *getline(FILE *fp, char *b);
char *range(char *s, int *p1, int *p2);
int getcommand(FILE *fp, int *o1, int *o2, char *pcmd, int *n1, int
	*n2);
void fatal(const char *s, ...);
int strwcmp(char *s1, char *s2);
int whitespace(char ch);

char *
getline(FILE *fp, char *b)
{
  if (fgets(b, LINELEN, fp) == NULL) fatal("unexpected eof");

  return b;
}

#define copy(str) printf("%s", str)

int main(int argc, char **argv)
{
  char cmd, *fl, *fd, obuf[LINELEN], nbuf[LINELEN];
  int o1, o2, n1, n2, here;
  FILE *fpf, *fpd;

  prog = argv[0];
  processing = argv[1];
  if (argc != 3) fatal("use: %s original-file diff-list-file", prog);
  if ((fpf = fopen(argv[1], "r")) == NULL) fatal("can't read %s", argv[1]);
  if ((fpd = fopen(argv[2], "r")) == NULL) fatal("can't read %s", argv[2]);
  here = 0;
  while (getcommand(fpd, &o1, &o2, &cmd, &n1, &n2)) {
	while (here < o1 - 1) {
		here++;
		copy(getline(fpf, obuf));
	}
	switch (cmd) {
	    case 'c':
	    case 'd':
		if (cmd == 'd' && n1 != n2) fatal("delete count conflict");
		while (o1 <= o2) {
			fl = getline(fpf, obuf);
			here++;
			fd = getline(fpd, nbuf);
			if (strncmp(fd, "<", (size_t)1))
				fatal("illegal delete line");
			if (strcmp(fl, fd + 2))
				fatal("delete line conflict");
			o1++;
		}
		if (cmd == 'd') break;
		if (strcmp(getline(fpd, nbuf), "---\n"))
			fatal("illegal separator in chunk");
		/* FALLTHROUGH */
	    case 'a':
		if (cmd == 'a') {
			if (o1 != o2) fatal("append count conflict");
			copy(getline(fpf, obuf));
			here++;
		}
		while (n1 <= n2) {
			if (strncmp(getline(fpd, nbuf), ">", (size_t)1))
				fatal("illegal append line");
			copy(nbuf + 2);
			n1++;
		}
		break;
	}
  }
  while (fgets(obuf, LINELEN, fpf) != NULL) copy(obuf);
  return(0);
}

char *
 range(char *s, int *p1, int *p2)
{
  register int v1 = 0, v2;

  while (isdigit(*s)) v1 = 10 * v1 + *s++ - '0';
  v2 = v1;
  if (*s == ',') {
	s++;
	v2 = 0;
	while (isdigit(*s)) v2 = 10 * v2 + *s++ - '0';
  }
  if (v1 > v2) fatal("illegal range");
  *p1 = v1;
  *p2 = v2;
  return s;
}

int getcommand(FILE *fp, int *o1, int *o2, char *pcmd, int *n1, int *n2)
{
  char buf[LINELEN];
  register char *s;
  char cmd;

  if ((s = fgets(buf, LINELEN, fp)) == NULL) return 0;
  s = range(s, o1, o2);
  if ((cmd = *s++) != 'a' && cmd != 'c' && cmd != 'd')
	fatal("illegal command");
  s = range(s, n1, n2);
  if (*s != '\n' && s[1] != '\0')
	fatal("extra characters at end of command: %s", s);
  *pcmd = cmd;
  return 1;
}

#ifdef __STDC__
void fatal(const char *s, ...)
{
  va_list args;

  va_start (args, s);
  fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
  vfprintf(stderr, s, args);
  fprintf(stderr, "\n");
  va_end(args);
  exit(1);
}
#else
/* the K&R lib does not have vfprintf */
void fatal(const char *s, const char *a)
{
  fprintf(stderr, "%s: processing: %s fatal: ", prog, processing);
  fprintf(stderr, s, a);
  fprintf(stderr, "\n");
  exit(1);
}
#endif

#ifdef IGNORE_WHITE_SPACE

/* This routine is a white space insensitive version of strcmp.
   It is needed for testing things which might have undergone
   tab conversion or trailing space removal
   Bret Mckee June, 1988 */

int strwcmp(char *s1, char *s2)
{
  char *x1 = s1, *x2 = s2;

  /* Remove leading white space */
  while (whitespace(*s1)) s1++;
  while (whitespace(*s2)) s2++;
  do {
	while ((*s1 == *s2) && *s1 && *s2) {
		s1++;
		s2++;
	}
	;			/* consume identical characters */
	while (whitespace(*s1)) s1++;
	while (whitespace(*s2)) s2++;
  } while (*s1 && *s2 && (*s1 == *s2));
  if (*s1 - *s2)
	fprintf(stderr, "Failing for (%x)[%s]\n            (%x)[%s]\n",
		(int) *s1, x1, (int) *s2, x2);
  return(*s1 - *s2);
}

int whitespace(char ch)
{
  switch (ch) {
      case ' ':
      case '\n':
      case 0x0D:
      case '\t':
	return(1);
      default:	return(0);
}
}

#endif