inp.c revision 1.7 1 /* $NetBSD: inp.c,v 1.7 2002/03/08 21:57:33 kristerw Exp $ */
2 #include <sys/cdefs.h>
3 #ifndef lint
4 __RCSID("$NetBSD: inp.c,v 1.7 2002/03/08 21:57:33 kristerw Exp $");
5 #endif /* not lint */
6
7 #include "EXTERN.h"
8 #include "backupfile.h"
9 #include "common.h"
10 #include "util.h"
11 #include "pch.h"
12 #include "INTERN.h"
13 #include "inp.h"
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18
19 /* Input-file-with-indexable-lines abstract type */
20
21 static long i_size; /* size of the input file */
22 static char *i_womp; /* plan a buffer for entire file */
23 static char **i_ptr; /* pointers to lines in i_womp */
24
25 static int tifd = -1; /* plan b virtual string array */
26 static char *tibuf[2]; /* plan b buffers */
27 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
28 static LINENUM lines_per_buf; /* how many lines per buffer */
29 static int tireclen; /* length of records in tmp file */
30
31 /* New patch--prepare to edit another file. */
32
33 void
34 re_input(void)
35 {
36 if (using_plan_a) {
37 i_size = 0;
38
39 if (i_ptr != NULL)
40 free(i_ptr);
41 if (i_womp != Nullch)
42 free(i_womp);
43 i_womp = Nullch;
44 i_ptr = Null(char **);
45 }
46 else {
47 using_plan_a = TRUE; /* maybe the next one is smaller */
48 Close(tifd);
49 tifd = -1;
50 free(tibuf[0]);
51 free(tibuf[1]);
52 tibuf[0] = tibuf[1] = Nullch;
53 tiline[0] = tiline[1] = -1;
54 tireclen = 0;
55 }
56 }
57
58 /* Constuct the line index, somehow or other. */
59
60 void
61 scan_input(char *filename)
62 {
63 if (!plan_a(filename))
64 plan_b(filename);
65 if (verbose) {
66 say3("Patching file %s using Plan %s...\n", filename,
67 (using_plan_a ? "A" : "B") );
68 }
69 }
70
71 /* Try keeping everything in memory. */
72
73 bool
74 plan_a(char *filename)
75 {
76 int ifd, statfailed;
77 char *s;
78 LINENUM iline;
79 char lbuf[MAXLINELEN];
80
81 statfailed = stat(filename, &filestat);
82 if (statfailed && ok_to_create_file) {
83 if (verbose)
84 say2("(Creating file %s...)\n",filename);
85 makedirs(filename, TRUE);
86 close(creat(filename, 0666));
87 statfailed = stat(filename, &filestat);
88 }
89 /* For nonexistent or read-only files, look for RCS or SCCS versions. */
90 if (statfailed
91 /* No one can write to it. */
92 || (filestat.st_mode & 0222) == 0
93 /* I can't write to it. */
94 || ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) {
95 struct stat cstat;
96 char *cs = Nullch;
97 char *filebase;
98 int pathlen;
99
100 filebase = basename(filename);
101 pathlen = filebase - filename;
102
103 /* Put any leading path into `s'.
104 Leave room in lbuf for the diff command. */
105 s = lbuf + 20;
106 strncpy(s, filename, pathlen);
107
108 #define try(f, a1, a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
109 #define try1(f, a1) (Sprintf(s + pathlen, f, a1), stat(s, &cstat) == 0)
110 if ( try("RCS/%s%s", filebase, RCSSUFFIX)
111 || try1("RCS/%s" , filebase)
112 || try( "%s%s", filebase, RCSSUFFIX)) {
113 Sprintf(buf, CHECKOUT, filename);
114 Sprintf(lbuf, RCSDIFF, filename);
115 cs = "RCS";
116 } else if ( try("SCCS/%s%s", SCCSPREFIX, filebase)
117 || try( "%s%s", SCCSPREFIX, filebase)) {
118 Sprintf(buf, GET, s);
119 Sprintf(lbuf, SCCSDIFF, s, filename);
120 cs = "SCCS";
121 } else if (statfailed)
122 fatal2("can't find %s\n", filename);
123 /* else we can't write to it but it's not under a version
124 control system, so just proceed. */
125 if (cs) {
126 if (!statfailed) {
127 if ((filestat.st_mode & 0222) != 0)
128 /* The owner can write to it. */
129 fatal3("file %s seems to be locked by somebody else under %s\n",
130 filename, cs);
131 /* It might be checked out unlocked. See if it's safe to
132 check out the default version locked. */
133 if (verbose)
134 say3("Comparing file %s to default %s version...\n",
135 filename, cs);
136 if (system(lbuf))
137 fatal3("can't check out file %s: differs from default %s version\n",
138 filename, cs);
139 }
140 if (verbose)
141 say3("Checking out file %s from %s...\n", filename, cs);
142 if (system(buf) || stat(filename, &filestat))
143 fatal3("can't check out file %s from %s\n", filename, cs);
144 }
145 }
146 if (old_file_is_dev_null && ok_to_create_file && (filestat.st_size != 0)) {
147 fatal2("patch creates new file but existing file %s not empty\n",
148 filename);
149 }
150
151 filemode = filestat.st_mode;
152 if (!S_ISREG(filemode))
153 fatal2("%s is not a normal file--can't patch\n", filename);
154 i_size = filestat.st_size;
155 if (out_of_mem) {
156 set_hunkmax(); /* make sure dynamic arrays are allocated */
157 out_of_mem = FALSE;
158 return FALSE; /* force plan b because plan a bombed */
159 }
160
161 i_womp = malloc((MEM)(i_size+2));
162 if (i_womp == Nullch)
163 return FALSE;
164 if ((ifd = open(filename, 0)) < 0)
165 pfatal2("can't open file %s", filename);
166 if (read(ifd, i_womp, i_size) != i_size) {
167 Close(ifd); /* probably means i_size > 15 or 16 bits worth */
168 free(i_womp); /* at this point it doesn't matter if i_womp was */
169 return FALSE; /* undersized. */
170 }
171 Close(ifd);
172 if (i_size && i_womp[i_size-1] != '\n')
173 i_womp[i_size++] = '\n';
174 i_womp[i_size] = '\0';
175
176 /* count the lines in the buffer so we know how many pointers we need */
177
178 iline = 0;
179 for (s=i_womp; *s; s++) {
180 if (*s == '\n')
181 iline++;
182 }
183 i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
184 if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
185 free((char *)i_womp);
186 return FALSE;
187 }
188
189 /* now scan the buffer and build pointer array */
190
191 iline = 1;
192 i_ptr[iline] = i_womp;
193 for (s=i_womp; *s; s++) {
194 if (*s == '\n')
195 i_ptr[++iline] = s+1; /* these are NOT null terminated */
196 }
197 input_lines = iline - 1;
198
199 /* now check for revision, if any */
200
201 if (revision != Nullch) {
202 if (!rev_in_string(i_womp)) {
203 if (force) {
204 if (verbose)
205 say2(
206 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
207 revision);
208 }
209 else if (batch) {
210 fatal2(
211 "this file doesn't appear to be the %s version--aborting.\n", revision);
212 }
213 else {
214 ask2(
215 "This file doesn't appear to be the %s version--patch anyway? [n] ",
216 revision);
217 if (*buf != 'y')
218 fatal1("aborted\n");
219 }
220 }
221 else if (verbose)
222 say2("Good. This file appears to be the %s version.\n",
223 revision);
224 }
225 return TRUE; /* plan a will work */
226 }
227
228 /* Keep (virtually) nothing in memory. */
229
230 void
231 plan_b(char *filename)
232 {
233 FILE *ifp;
234 int i = 0;
235 int maxlen = 1;
236 bool found_revision = (revision == Nullch);
237
238 using_plan_a = FALSE;
239 if ((ifp = fopen(filename, "r")) == Nullfp)
240 pfatal2("can't open file %s", filename);
241 if ((tifd = creat(TMPINNAME, 0666)) < 0)
242 pfatal2("can't open file %s", TMPINNAME);
243 while (fgets(buf, sizeof buf, ifp) != Nullch) {
244 if (revision != Nullch && !found_revision && rev_in_string(buf))
245 found_revision = TRUE;
246 if ((i = strlen(buf)) > maxlen)
247 maxlen = i; /* find longest line */
248 }
249 if (revision != Nullch) {
250 if (!found_revision) {
251 if (force) {
252 if (verbose)
253 say2(
254 "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
255 revision);
256 }
257 else if (batch) {
258 fatal2(
259 "this file doesn't appear to be the %s version--aborting.\n", revision);
260 }
261 else {
262 ask2(
263 "This file doesn't appear to be the %s version--patch anyway? [n] ",
264 revision);
265 if (*buf != 'y')
266 fatal1("aborted\n");
267 }
268 }
269 else if (verbose)
270 say2("Good. This file appears to be the %s version.\n",
271 revision);
272 }
273 Fseek(ifp, 0L, 0); /* rewind file */
274 lines_per_buf = BUFFERSIZE / maxlen;
275 tireclen = maxlen;
276 tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
277 tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
278 if (tibuf[1] == Nullch)
279 fatal1("out of memory\n");
280 for (i=1; ; i++) {
281 if (! (i % lines_per_buf)) /* new block */
282 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
283 pfatal1("can't write temp file");
284 if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
285 == Nullch) {
286 input_lines = i - 1;
287 if (i % lines_per_buf)
288 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
289 pfatal1("can't write temp file");
290 break;
291 }
292 }
293 Fclose(ifp);
294 Close(tifd);
295 if ((tifd = open(TMPINNAME, 0)) < 0) {
296 pfatal2("can't reopen file %s", TMPINNAME);
297 }
298 }
299
300 /* Fetch a line from the input file, \n terminated, not necessarily \0. */
301
302 char *
303 ifetch(LINENUM line, int whichbuf)
304 {
305 if (line < 1 || line > input_lines)
306 return "";
307 if (using_plan_a)
308 return i_ptr[line];
309 else {
310 LINENUM offline = line % lines_per_buf;
311 LINENUM baseline = line - offline;
312
313 if (tiline[0] == baseline)
314 whichbuf = 0;
315 else if (tiline[1] == baseline)
316 whichbuf = 1;
317 else {
318 tiline[whichbuf] = baseline;
319 Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
320 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
321 pfatal2("error reading tmp file %s", TMPINNAME);
322 }
323 return tibuf[whichbuf] + (tireclen*offline);
324 }
325 }
326
327 /* True if the string argument contains the revision number we want. */
328
329 bool
330 rev_in_string(char *string)
331 {
332 char *s;
333 int patlen;
334
335 if (revision == Nullch)
336 return TRUE;
337 patlen = strlen(revision);
338 if (strnEQ(string,revision,patlen) && isspace((unsigned char)string[patlen]))
339 return TRUE;
340 for (s = string; *s; s++) {
341 if (isspace((unsigned char)*s) && strnEQ(s+1, revision, patlen) &&
342 isspace((unsigned char)s[patlen+1] )) {
343 return TRUE;
344 }
345 }
346 return FALSE;
347 }
348