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