Home | History | Annotate | Line # | Download | only in patch
inp.c revision 1.15
      1  1.15    itojun /*	$NetBSD: inp.c,v 1.15 2003/07/12 13:47:43 itojun Exp $	*/
      2  1.15    itojun 
      3  1.15    itojun /*
      4  1.15    itojun  * Copyright (c) 1988, Larry Wall
      5  1.15    itojun  *
      6  1.15    itojun  * Redistribution and use in source and binary forms, with or without
      7  1.15    itojun  * modification, are permitted provided that the following condition
      8  1.15    itojun  * is met:
      9  1.15    itojun  *  1. Redistributions of source code must retain the above copyright
     10  1.15    itojun  *     notice, this condition and the following disclaimer.
     11  1.15    itojun  *
     12  1.15    itojun  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     13  1.15    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14  1.15    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15  1.15    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     16  1.15    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17  1.15    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18  1.15    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     19  1.15    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20  1.15    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21  1.15    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22  1.15    itojun  * SUCH DAMAGE.
     23  1.15    itojun  */
     24  1.15    itojun 
     25   1.4  christos #include <sys/cdefs.h>
     26   1.2   mycroft #ifndef lint
     27  1.15    itojun __RCSID("$NetBSD: inp.c,v 1.15 2003/07/12 13:47:43 itojun Exp $");
     28   1.2   mycroft #endif /* not lint */
     29   1.1       cgd 
     30   1.1       cgd #include "EXTERN.h"
     31   1.7  kristerw #include "backupfile.h"
     32   1.1       cgd #include "common.h"
     33   1.1       cgd #include "util.h"
     34   1.1       cgd #include "pch.h"
     35   1.1       cgd #include "INTERN.h"
     36   1.1       cgd #include "inp.h"
     37   1.1       cgd 
     38   1.4  christos #include <stdlib.h>
     39   1.4  christos #include <unistd.h>
     40   1.4  christos #include <fcntl.h>
     41   1.4  christos 
     42  1.13  kristerw static void plan_a(char *);
     43  1.10  kristerw static bool rev_in_string(char *);
     44  1.10  kristerw 
     45   1.9  kristerw /* Input-file-with-indexable-lines abstract type. */
     46   1.1       cgd 
     47  1.11  kristerw static size_t i_size;			/* Size of the input file */
     48   1.9  kristerw static char *i_womp;			/* Plan a buffer for entire file */
     49   1.9  kristerw static char **i_ptr;			/* Pointers to lines in i_womp */
     50   1.1       cgd 
     51   1.9  kristerw /*
     52  1.13  kristerw  * New patch -- prepare to edit another file.
     53   1.9  kristerw  */
     54   1.1       cgd void
     55   1.7  kristerw re_input(void)
     56   1.1       cgd {
     57  1.13  kristerw 	i_size = 0;
     58   1.7  kristerw 
     59  1.13  kristerw 	if (i_ptr != NULL)
     60  1.13  kristerw 		free(i_ptr);
     61  1.13  kristerw 	if (i_womp != NULL)
     62  1.13  kristerw 		free(i_womp);
     63  1.13  kristerw 	i_womp = NULL;
     64  1.13  kristerw 	i_ptr = NULL;
     65   1.1       cgd }
     66   1.1       cgd 
     67   1.9  kristerw /*
     68  1.13  kristerw  * Construct the line index, somehow or other.
     69   1.9  kristerw  */
     70   1.1       cgd void
     71   1.7  kristerw scan_input(char *filename)
     72   1.1       cgd {
     73  1.13  kristerw 	plan_a(filename);
     74  1.13  kristerw 	if (verbose)
     75  1.13  kristerw 		say("Patching file %s using Plan A...\n", filename);
     76   1.1       cgd }
     77   1.1       cgd 
     78   1.9  kristerw /*
     79   1.9  kristerw  * Try keeping everything in memory.
     80   1.9  kristerw  */
     81  1.13  kristerw static void
     82   1.7  kristerw plan_a(char *filename)
     83   1.1       cgd {
     84   1.9  kristerw 	int ifd, statfailed;
     85   1.9  kristerw 	char *s;
     86   1.9  kristerw 	LINENUM iline;
     87   1.9  kristerw 	char lbuf[MAXLINELEN];
     88   1.9  kristerw 
     89   1.1       cgd 	statfailed = stat(filename, &filestat);
     90   1.9  kristerw 	if (statfailed && ok_to_create_file) {
     91   1.9  kristerw 		if (verbose)
     92   1.9  kristerw 			say("(Creating file %s...)\n",filename);
     93   1.9  kristerw 		makedirs(filename, TRUE);
     94   1.9  kristerw 		close(creat(filename, 0666));
     95   1.9  kristerw 		statfailed = stat(filename, &filestat);
     96   1.9  kristerw 	}
     97   1.9  kristerw 	/*
     98   1.9  kristerw 	 * For nonexistent or read-only files, look for RCS or SCCS
     99   1.9  kristerw 	 * versions.
    100   1.9  kristerw 	 */
    101   1.9  kristerw 	if (statfailed ||
    102   1.9  kristerw 	    /* No one can write to it. */
    103   1.9  kristerw 	    (filestat.st_mode & 0222) == 0 ||
    104   1.9  kristerw 	    /* I can't write to it. */
    105   1.9  kristerw 	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != myuid)) {
    106   1.9  kristerw 		struct stat cstat;
    107  1.14  kristerw 		const char *cs = NULL;
    108   1.9  kristerw 		char *filebase;
    109  1.11  kristerw 		size_t pathlen;
    110   1.9  kristerw 
    111   1.9  kristerw 		filebase = basename(filename);
    112   1.9  kristerw 		pathlen = filebase - filename;
    113   1.9  kristerw 
    114   1.9  kristerw 		/*
    115   1.9  kristerw 		 * Put any leading path into `s'.
    116   1.9  kristerw 		 * Leave room in lbuf for the diff command.
    117   1.9  kristerw 		 */
    118   1.9  kristerw 		s = lbuf + 20;
    119   1.9  kristerw 		strncpy(s, filename, pathlen);
    120   1.1       cgd 
    121   1.1       cgd #define try(f, a1, a2) (Sprintf(s + pathlen, f, a1, a2), stat(s, &cstat) == 0)
    122   1.4  christos #define try1(f, a1) (Sprintf(s + pathlen, f, a1), stat(s, &cstat) == 0)
    123   1.9  kristerw 		if (try("RCS/%s%s", filebase, RCSSUFFIX) ||
    124   1.9  kristerw 		    try1("RCS/%s"  , filebase) ||
    125   1.9  kristerw 		    try("%s%s", filebase, RCSSUFFIX)) {
    126   1.9  kristerw 			Sprintf(buf, CHECKOUT, filename);
    127   1.9  kristerw 			Sprintf(lbuf, RCSDIFF, filename);
    128   1.9  kristerw 			cs = "RCS";
    129   1.9  kristerw 		} else if (try("SCCS/%s%s", SCCSPREFIX, filebase) ||
    130   1.9  kristerw 			   try("%s%s", SCCSPREFIX, filebase)) {
    131   1.9  kristerw 			Sprintf(buf, GET, s);
    132   1.9  kristerw 			Sprintf(lbuf, SCCSDIFF, s, filename);
    133   1.9  kristerw 			cs = "SCCS";
    134   1.9  kristerw 		} else if (statfailed)
    135   1.9  kristerw 			fatal("can't find %s\n", filename);
    136   1.9  kristerw 		/*
    137   1.9  kristerw 		 * else we can't write to it but it's not under a version
    138   1.9  kristerw 		 * control system, so just proceed.
    139   1.9  kristerw 		 */
    140   1.9  kristerw 		if (cs) {
    141   1.9  kristerw 			if (!statfailed) {
    142   1.9  kristerw 				if ((filestat.st_mode & 0222) != 0)
    143   1.9  kristerw 					/* The owner can write to it.  */
    144   1.9  kristerw 					fatal(
    145   1.9  kristerw "file %s seems to be locked by somebody else under %s\n",
    146   1.9  kristerw 					      filename, cs);
    147   1.9  kristerw 				/*
    148   1.9  kristerw 				 * It might be checked out unlocked.  See if
    149   1.9  kristerw 				 * it's safe to check out the default version
    150   1.9  kristerw 				 * locked.
    151   1.9  kristerw 				 */
    152   1.9  kristerw 				if (verbose)
    153   1.9  kristerw 					say(
    154   1.9  kristerw "Comparing file %s to default %s version...\n",
    155   1.9  kristerw 					    filename, cs);
    156   1.9  kristerw 				if (system(lbuf))
    157   1.9  kristerw 					fatal(
    158   1.9  kristerw "can't check out file %s: differs from default %s version\n",
    159   1.9  kristerw 					      filename, cs);
    160   1.9  kristerw 			}
    161   1.9  kristerw 			if (verbose)
    162   1.9  kristerw 				say("Checking out file %s from %s...\n",
    163   1.9  kristerw 				    filename, cs);
    164   1.9  kristerw 			if (system(buf) || stat(filename, &filestat))
    165   1.9  kristerw 				fatal("can't check out file %s from %s\n",
    166   1.9  kristerw 				      filename, cs);
    167   1.9  kristerw 		}
    168   1.9  kristerw 	}
    169   1.9  kristerw 	if (old_file_is_dev_null &&
    170   1.9  kristerw 	    ok_to_create_file &&
    171   1.9  kristerw 	    (filestat.st_size != 0)) {
    172   1.9  kristerw 		fatal(
    173   1.9  kristerw "patch creates new file but existing file %s not empty\n",
    174   1.9  kristerw 		      filename);
    175   1.1       cgd 	}
    176   1.6  sommerfe 
    177   1.9  kristerw 	filemode = filestat.st_mode;
    178   1.9  kristerw 	if (!S_ISREG(filemode))
    179   1.9  kristerw 		fatal("%s is not a normal file--can't patch\n", filename);
    180   1.9  kristerw 	i_size = filestat.st_size;
    181   1.9  kristerw 
    182  1.12  kristerw 	i_womp = xmalloc(i_size + 2);
    183   1.9  kristerw 	if ((ifd = open(filename, 0)) < 0)
    184   1.9  kristerw 		pfatal("can't open file %s", filename);
    185  1.13  kristerw 	if (read(ifd, i_womp, i_size) != i_size)
    186  1.13  kristerw 		pfatal("read error");
    187   1.9  kristerw 	Close(ifd);
    188   1.9  kristerw 	if (i_size && i_womp[i_size - 1] != '\n')
    189   1.9  kristerw 		i_womp[i_size++] = '\n';
    190   1.9  kristerw 	i_womp[i_size] = '\0';
    191   1.9  kristerw 
    192   1.9  kristerw 	/*
    193   1.9  kristerw 	 * Count the lines in the buffer so we know how many pointers we
    194   1.9  kristerw 	 * need.
    195   1.9  kristerw 	 */
    196   1.9  kristerw 	iline = 0;
    197   1.9  kristerw 	for (s = i_womp; *s; s++) {
    198   1.9  kristerw 		if (*s == '\n')
    199   1.9  kristerw 			iline++;
    200   1.9  kristerw 	}
    201  1.12  kristerw 	i_ptr = xmalloc((iline + 2) * sizeof(char *));
    202   1.1       cgd 
    203   1.9  kristerw 	/* Now scan the buffer and build pointer array. */
    204   1.9  kristerw 	iline = 1;
    205   1.9  kristerw 	i_ptr[iline] = i_womp;
    206   1.9  kristerw 	for (s = i_womp; *s; s++) {
    207   1.9  kristerw 		if (*s == '\n') {
    208   1.9  kristerw 			/* These are NOT null terminated. */
    209   1.9  kristerw 			i_ptr[++iline] = s + 1;
    210   1.9  kristerw 		}
    211   1.9  kristerw 	}
    212   1.9  kristerw 	input_lines = iline - 1;
    213   1.1       cgd 
    214   1.9  kristerw 	/* Now check for revision, if any. */
    215   1.9  kristerw 	if (revision != NULL) {
    216   1.9  kristerw 		if (!rev_in_string(i_womp)) {
    217   1.9  kristerw 			if (force) {
    218   1.9  kristerw 				if (verbose)
    219   1.9  kristerw 					say(
    220   1.1       cgd "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
    221   1.1       cgd 			revision);
    222   1.9  kristerw 			} else if (batch) {
    223   1.9  kristerw 				fatal(
    224   1.1       cgd "this file doesn't appear to be the %s version--aborting.\n", revision);
    225   1.9  kristerw 			} else {
    226   1.9  kristerw 				ask(
    227   1.1       cgd "This file doesn't appear to be the %s version--patch anyway? [n] ",
    228   1.1       cgd 		    revision);
    229   1.9  kristerw 				if (*buf != 'y')
    230   1.9  kristerw 					fatal("aborted\n");
    231   1.9  kristerw 			}
    232   1.9  kristerw 		} else if (verbose)
    233   1.9  kristerw 			say("Good.  This file appears to be the %s version.\n",
    234   1.9  kristerw 			    revision);
    235   1.9  kristerw 	}
    236   1.1       cgd }
    237   1.1       cgd 
    238   1.9  kristerw /*
    239   1.9  kristerw  * Fetch a line from the input file, \n terminated, not necessarily \0.
    240   1.9  kristerw  */
    241  1.14  kristerw const char *
    242  1.13  kristerw ifetch(LINENUM line)
    243   1.1       cgd {
    244   1.9  kristerw 	if (line < 1 || line > input_lines)
    245   1.9  kristerw 		return "";
    246  1.13  kristerw 
    247  1.13  kristerw 	return i_ptr[line];
    248   1.1       cgd }
    249   1.1       cgd 
    250   1.9  kristerw /*
    251   1.9  kristerw  * True if the string argument contains the revision number we want.
    252   1.9  kristerw  */
    253  1.10  kristerw static bool
    254   1.7  kristerw rev_in_string(char *string)
    255   1.1       cgd {
    256   1.9  kristerw 	char *s;
    257  1.11  kristerw 	size_t patlen;
    258   1.1       cgd 
    259   1.9  kristerw 	if (revision == NULL)
    260   1.9  kristerw 		return TRUE;
    261   1.9  kristerw 	patlen = strlen(revision);
    262   1.9  kristerw 	if (strnEQ(string,revision,patlen) &&
    263   1.9  kristerw 	    isspace((unsigned char)string[patlen]))
    264   1.9  kristerw 		return TRUE;
    265   1.9  kristerw 	for (s = string; *s; s++) {
    266   1.9  kristerw 		if (isspace((unsigned char)*s) &&
    267   1.9  kristerw 		    strnEQ(s + 1, revision, patlen) &&
    268   1.9  kristerw 		    isspace((unsigned char)s[patlen + 1] )) {
    269   1.9  kristerw 			return TRUE;
    270   1.9  kristerw 		}
    271   1.1       cgd 	}
    272  1.13  kristerw 	return FALSE;
    273   1.1       cgd }
    274