source: trunk/minix/commands/patch/inp.c@ 10

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

Minix 3.1.2a

File size: 8.2 KB
Line 
1/* $Header: /cvsup/minix/src/commands/patch/inp.c,v 1.1.1.1 2005/04/21 14:55:10 beng Exp $
2 *
3 * $Log: inp.c,v $
4 * Revision 1.1.1.1 2005/04/21 14:55:10 beng
5 * Initial import of pre-3.0.1
6 *
7 * Revision 1.1.1.1 2005/04/20 13:33:18 beng
8 * Initial import of minix 2.0.4
9 *
10 * Revision 2.0.1.1 88/06/03 15:06:13 lwall
11 * patch10: made a little smarter about sccs files
12 *
13 * Revision 2.0 86/09/17 15:37:02 lwall
14 * Baseline for netwide release.
15 *
16 */
17
18#include "EXTERN.h"
19#include "common.h"
20#include "util.h"
21#include "pch.h"
22#include "INTERN.h"
23#include "inp.h"
24
25/* Input-file-with-indexable-lines abstract type */
26
27static long i_size; /* size of the input file */
28static char *i_womp; /* plan a buffer for entire file */
29static char **i_ptr; /* pointers to lines in i_womp */
30
31static int tifd = -1; /* plan b virtual string array */
32static char *tibuf[2]; /* plan b buffers */
33static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */
34static LINENUM lines_per_buf; /* how many lines per buffer */
35static int tireclen; /* length of records in tmp file */
36
37/* New patch--prepare to edit another file. */
38
39void
40re_input()
41{
42 if (using_plan_a) {
43 i_size = 0;
44#ifndef lint
45 if (i_ptr != Null(char**))
46 free((char *)i_ptr);
47#endif
48 if (i_womp != Nullch)
49 free(i_womp);
50 i_womp = Nullch;
51 i_ptr = Null(char **);
52 }
53 else {
54#ifndef SMALL
55 using_plan_a = TRUE; /* maybe the next one is smaller */
56#endif
57 Close(tifd);
58 tifd = -1;
59 free(tibuf[0]);
60 free(tibuf[1]);
61 tibuf[0] = tibuf[1] = Nullch;
62 tiline[0] = tiline[1] = -1;
63 tireclen = 0;
64 }
65}
66
67/* Constuct the line index, somehow or other. */
68
69void
70scan_input(filename)
71char *filename;
72{
73#ifndef SMALL
74 if (!plan_a(filename))
75#endif
76 plan_b(filename);
77 if (verbose) {
78 say3("Patching file %s using Plan %s...\n", filename,
79 (using_plan_a ? "A" : "B") );
80 }
81}
82
83#ifndef SMALL
84/* Try keeping everything in memory. */
85
86bool
87plan_a(filename)
88char *filename;
89{
90 int ifd;
91 Reg1 char *s;
92 Reg2 LINENUM iline;
93
94 if (ok_to_create_file && stat(filename, &filestat) < 0) {
95 if (verbose)
96 say2("(Creating file %s...)\n",filename);
97 makedirs(filename, TRUE);
98 close(creat(filename, 0666));
99 }
100 if (stat(filename, &filestat) < 0) {
101 Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
102 if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
103 Sprintf(buf, CHECKOUT, filename);
104 if (verbose)
105 say2("Can't find %s--attempting to check it out from RCS.\n",
106 filename);
107 if (system(buf) || stat(filename, &filestat))
108 fatal2("Can't check out %s.\n", filename);
109 }
110 else {
111 Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
112 if (stat(s=buf+20, &filestat) >= 0 ||
113 stat(s=buf+25, &filestat) >= 0) {
114 Sprintf(buf, GET, s);
115 if (verbose)
116 say2("Can't find %s--attempting to get it from SCCS.\n",
117 filename);
118 if (system(buf) || stat(filename, &filestat))
119 fatal2("Can't get %s.\n", filename);
120 }
121 else
122 fatal2("Can't find %s.\n", filename);
123 }
124 }
125 filemode = filestat.st_mode;
126 if ((filemode & S_IFMT) & ~S_IFREG)
127 fatal2("%s is not a normal file--can't patch.\n", filename);
128 i_size = filestat.st_size;
129 if (out_of_mem) {
130 set_hunkmax(); /* make sure dynamic arrays are allocated */
131 out_of_mem = FALSE;
132 return FALSE; /* force plan b because plan a bombed */
133 }
134#ifdef lint
135 i_womp = Nullch;
136#else
137 i_womp = malloc((MEM)(i_size+2)); /* lint says this may alloc less than */
138 /* i_size, but that's okay, I think. */
139#endif
140 if (i_womp == Nullch)
141 return FALSE;
142 if ((ifd = open(filename, 0)) < 0)
143 fatal2("Can't open file %s\n", filename);
144#ifndef lint
145 if (read(ifd, i_womp, (int)i_size) != i_size) {
146 Close(ifd); /* probably means i_size > 15 or 16 bits worth */
147 free(i_womp); /* at this point it doesn't matter if i_womp was */
148 return FALSE; /* undersized. */
149 }
150#endif
151 Close(ifd);
152 if (i_size && i_womp[i_size-1] != '\n')
153 i_womp[i_size++] = '\n';
154 i_womp[i_size] = '\0';
155
156 /* count the lines in the buffer so we know how many pointers we need */
157
158 iline = 0;
159 for (s=i_womp; *s; s++) {
160 if (*s == '\n')
161 iline++;
162 }
163#ifdef lint
164 i_ptr = Null(char**);
165#else
166 i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
167#endif
168 if (i_ptr == Null(char **)) { /* shucks, it was a near thing */
169 free((char *)i_womp);
170 return FALSE;
171 }
172
173 /* now scan the buffer and build pointer array */
174
175 iline = 1;
176 i_ptr[iline] = i_womp;
177 for (s=i_womp; *s; s++) {
178 if (*s == '\n')
179 i_ptr[++iline] = s+1; /* these are NOT null terminated */
180 }
181 input_lines = iline - 1;
182
183 /* now check for revision, if any */
184
185 if (revision != Nullch) {
186 if (!rev_in_string(i_womp)) {
187 if (force) {
188 if (verbose)
189 say2(
190"Warning: this file doesn't appear to be the %s version--patching anyway.\n",
191 revision);
192 }
193 else {
194 ask2(
195"This file doesn't appear to be the %s version--patch anyway? [n] ",
196 revision);
197 if (*buf != 'y')
198 fatal1("Aborted.\n");
199 }
200 }
201 else if (verbose)
202 say2("Good. This file appears to be the %s version.\n",
203 revision);
204 }
205 return TRUE; /* plan a will work */
206}
207#endif
208
209/* Keep (virtually) nothing in memory. */
210
211void
212plan_b(filename)
213char *filename;
214{
215 Reg3 FILE *ifp;
216 Reg1 int i = 0;
217 Reg2 int maxlen = 1;
218 Reg4 bool found_revision = (revision == Nullch);
219
220 using_plan_a = FALSE;
221 if ((ifp = fopen(filename, "r")) == Nullfp)
222 fatal2("Can't open file %s\n", filename);
223 if ((tifd = creat(TMPINNAME, 0666)) < 0)
224 fatal2("Can't open file %s\n", TMPINNAME);
225 while (fgets(buf, sizeof buf, ifp) != Nullch) {
226 if (revision != Nullch && !found_revision && rev_in_string(buf))
227 found_revision = TRUE;
228 if ((i = strlen(buf)) > maxlen)
229 maxlen = i; /* find longest line */
230 }
231 if (revision != Nullch) {
232 if (!found_revision) {
233 if (force) {
234 if (verbose)
235 say2(
236"Warning: this file doesn't appear to be the %s version--patching anyway.\n",
237 revision);
238 }
239 else {
240 ask2(
241"This file doesn't appear to be the %s version--patch anyway? [n] ",
242 revision);
243 if (*buf != 'y')
244 fatal1("Aborted.\n");
245 }
246 }
247 else if (verbose)
248 say2("Good. This file appears to be the %s version.\n",
249 revision);
250 }
251 Fseek(ifp, 0L, 0); /* rewind file */
252 lines_per_buf = BUFFERSIZE / maxlen;
253 tireclen = maxlen;
254 tibuf[0] = (char *)malloc((MEM)(BUFFERSIZE + 1));
255 tibuf[1] = (char *)malloc((MEM)(BUFFERSIZE + 1));
256 if (tibuf[1] == Nullch)
257 fatal1("Can't seem to get enough memory.\n");
258 for (i=1; ; i++) {
259 if (! (i % lines_per_buf)) /* new block */
260 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
261 fatal1("patch: can't write temp file.\n");
262 if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
263 == Nullch) {
264 input_lines = i - 1;
265 if (i % lines_per_buf)
266 if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
267 fatal1("patch: can't write temp file.\n");
268 break;
269 }
270 }
271 Fclose(ifp);
272 Close(tifd);
273 if ((tifd = open(TMPINNAME, 0)) < 0) {
274 fatal2("Can't reopen file %s\n", TMPINNAME);
275 }
276}
277
278/* Fetch a line from the input file, \n terminated, not necessarily \0. */
279
280char *
281ifetch(line,whichbuf)
282Reg1 LINENUM line;
283int whichbuf; /* ignored when file in memory */
284{
285 if (line < 1 || line > input_lines)
286 return "";
287 if (using_plan_a)
288 return i_ptr[line];
289 else {
290 LINENUM offline = line % lines_per_buf;
291 LINENUM baseline = line - offline;
292
293 if (tiline[0] == baseline)
294 whichbuf = 0;
295 else if (tiline[1] == baseline)
296 whichbuf = 1;
297 else {
298 tiline[whichbuf] = baseline;
299#ifndef lint /* complains of long accuracy */
300 Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
301#endif
302 if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
303 fatal2("Error reading tmp file %s.\n", TMPINNAME);
304 }
305 return tibuf[whichbuf] + (tireclen*offline);
306 }
307}
308
309/* True if the string argument contains the revision number we want. */
310
311bool
312rev_in_string(string)
313char *string;
314{
315 Reg1 char *s;
316 Reg2 int patlen;
317
318 if (revision == Nullch)
319 return TRUE;
320 patlen = strlen(revision);
321 if (strnEQ(string,revision,patlen) && isspace(s[patlen]))
322 return TRUE;
323 for (s = string; *s; s++) {
324 if (isspace(*s) && strnEQ(s+1, revision, patlen) &&
325 isspace(s[patlen+1] )) {
326 return TRUE;
327 }
328 }
329 return FALSE;
330}
331
Note: See TracBrowser for help on using the repository browser.