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