Home | History | Annotate | Line # | Download | only in error
touch.c revision 1.9
      1  1.9  kleink /*	$NetBSD: touch.c,v 1.9 1999/11/06 15:11:46 kleink Exp $	*/
      2  1.3     jtc 
      3  1.1     cgd /*
      4  1.3     jtc  * Copyright (c) 1980, 1993
      5  1.3     jtc  *	The Regents of the University of California.  All rights reserved.
      6  1.1     cgd  *
      7  1.1     cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1     cgd  * modification, are permitted provided that the following conditions
      9  1.1     cgd  * are met:
     10  1.1     cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1     cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1     cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1     cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1     cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1     cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1     cgd  *    must display the following acknowledgement:
     17  1.1     cgd  *	This product includes software developed by the University of
     18  1.1     cgd  *	California, Berkeley and its contributors.
     19  1.1     cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1     cgd  *    may be used to endorse or promote products derived from this software
     21  1.1     cgd  *    without specific prior written permission.
     22  1.1     cgd  *
     23  1.1     cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1     cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     cgd  * SUCH DAMAGE.
     34  1.1     cgd  */
     35  1.1     cgd 
     36  1.6   lukem #include <sys/cdefs.h>
     37  1.1     cgd #ifndef lint
     38  1.3     jtc #if 0
     39  1.3     jtc static char sccsid[] = "@(#)touch.c	8.1 (Berkeley) 6/6/93";
     40  1.3     jtc #endif
     41  1.9  kleink __RCSID("$NetBSD: touch.c,v 1.9 1999/11/06 15:11:46 kleink Exp $");
     42  1.1     cgd #endif /* not lint */
     43  1.1     cgd 
     44  1.9  kleink #include <sys/param.h>
     45  1.1     cgd #include <sys/stat.h>
     46  1.6   lukem #include <ctype.h>
     47  1.1     cgd #include <signal.h>
     48  1.1     cgd #include <stdio.h>
     49  1.1     cgd #include <stdlib.h>
     50  1.1     cgd #include <string.h>
     51  1.6   lukem #include <unistd.h>
     52  1.6   lukem #if __STDC__
     53  1.6   lukem #include <stdarg.h>
     54  1.6   lukem #else
     55  1.6   lukem #include <varargs.h>
     56  1.6   lukem #endif
     57  1.1     cgd #include "error.h"
     58  1.1     cgd #include "pathnames.h"
     59  1.1     cgd 
     60  1.1     cgd /*
     61  1.1     cgd  *	Iterate through errors
     62  1.1     cgd  */
     63  1.1     cgd #define EITERATE(p, fv, i)	for (p = fv[i]; p < fv[i+1]; p++)
     64  1.1     cgd #define	ECITERATE(ei, p, lb)	for (ei = lb; p = errors[ei],ei < nerrors; ei++)
     65  1.1     cgd 
     66  1.1     cgd #define	FILEITERATE(fi, lb)	for (fi = lb; fi <= nfiles; fi++)
     67  1.1     cgd int	touchstatus = Q_YES;
     68  1.1     cgd 
     69  1.6   lukem void
     70  1.1     cgd findfiles(nerrors, errors, r_nfiles, r_files)
     71  1.6   lukem 	int	nerrors;
     72  1.1     cgd 	Eptr	*errors;
     73  1.6   lukem 	int	*r_nfiles;
     74  1.1     cgd 	Eptr	***r_files;
     75  1.1     cgd {
     76  1.6   lukem 	int	nfiles;
     77  1.1     cgd 	Eptr	**files;
     78  1.1     cgd 
     79  1.6   lukem 	char	*name;
     80  1.6   lukem 	int	ei;
     81  1.6   lukem 	int	fi;
     82  1.6   lukem 	Eptr	errorp;
     83  1.1     cgd 
     84  1.1     cgd 	nfiles = countfiles(errors);
     85  1.1     cgd 
     86  1.1     cgd 	files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
     87  1.1     cgd 	touchedfiles = (boolean	*)Calloc(nfiles+3, sizeof(boolean));
     88  1.1     cgd 	/*
     89  1.1     cgd 	 *	Now, partition off the error messages
     90  1.1     cgd 	 *	into those that are synchronization, discarded or
     91  1.1     cgd 	 *	not specific to any file, and those that were
     92  1.1     cgd 	 *	nulled or true errors.
     93  1.1     cgd 	 */
     94  1.1     cgd 	files[0] = &errors[0];
     95  1.1     cgd 	ECITERATE(ei, errorp, 0){
     96  1.1     cgd 		if ( ! (NOTSORTABLE(errorp->error_e_class)))
     97  1.1     cgd 			break;
     98  1.1     cgd 	}
     99  1.1     cgd 	/*
    100  1.1     cgd 	 *	Now, and partition off all error messages
    101  1.1     cgd 	 *	for a given file.
    102  1.1     cgd 	 */
    103  1.1     cgd 	files[1] = &errors[ei];
    104  1.1     cgd 	touchedfiles[0] = touchedfiles[1] = FALSE;
    105  1.1     cgd 	name = "\1";
    106  1.1     cgd 	fi = 1;
    107  1.1     cgd 	ECITERATE(ei, errorp, ei){
    108  1.1     cgd 		if (   (errorp->error_e_class == C_NULLED)
    109  1.1     cgd 		    || (errorp->error_e_class == C_TRUE) ){
    110  1.1     cgd 			if (strcmp(errorp->error_text[0], name) != 0){
    111  1.1     cgd 				name = errorp->error_text[0];
    112  1.1     cgd 				touchedfiles[fi] = FALSE;
    113  1.1     cgd 				files[fi] = &errors[ei];
    114  1.1     cgd 				fi++;
    115  1.1     cgd 			}
    116  1.1     cgd 		}
    117  1.1     cgd 	}
    118  1.1     cgd 	files[fi] = &errors[nerrors];
    119  1.1     cgd 	*r_nfiles = nfiles;
    120  1.1     cgd 	*r_files = files;
    121  1.1     cgd }
    122  1.1     cgd 
    123  1.6   lukem int
    124  1.6   lukem countfiles(errors)
    125  1.1     cgd 	Eptr	*errors;
    126  1.1     cgd {
    127  1.1     cgd 	char	*name;
    128  1.1     cgd 	int	ei;
    129  1.6   lukem 	Eptr	errorp;
    130  1.1     cgd 
    131  1.1     cgd 	int	nfiles;
    132  1.1     cgd 	nfiles = 0;
    133  1.1     cgd 	name = "\1";
    134  1.1     cgd 	ECITERATE(ei, errorp, 0){
    135  1.1     cgd 		if (SORTABLE(errorp->error_e_class)){
    136  1.1     cgd 			if (strcmp(errorp->error_text[0],name) != 0){
    137  1.1     cgd 				nfiles++;
    138  1.1     cgd 				name = errorp->error_text[0];
    139  1.1     cgd 			}
    140  1.1     cgd 		}
    141  1.1     cgd 	}
    142  1.1     cgd 	return(nfiles);
    143  1.1     cgd }
    144  1.6   lukem 
    145  1.1     cgd char	*class_table[] = {
    146  1.1     cgd 	/*C_UNKNOWN	0	*/	"Unknown",
    147  1.1     cgd 	/*C_IGNORE	1	*/	"ignore",
    148  1.1     cgd 	/*C_SYNC	2	*/	"synchronization",
    149  1.1     cgd 	/*C_DISCARD	3	*/	"discarded",
    150  1.1     cgd 	/*C_NONSPEC	4	*/	"non specific",
    151  1.1     cgd 	/*C_THISFILE	5	*/	"specific to this file",
    152  1.1     cgd 	/*C_NULLED	6	*/	"nulled",
    153  1.1     cgd 	/*C_TRUE	7	*/	"true",
    154  1.1     cgd 	/*C_DUPL	8	*/	"duplicated"
    155  1.1     cgd };
    156  1.1     cgd 
    157  1.1     cgd int	class_count[C_LAST - C_FIRST] = {0};
    158  1.1     cgd 
    159  1.6   lukem void
    160  1.1     cgd filenames(nfiles, files)
    161  1.1     cgd 	int	nfiles;
    162  1.1     cgd 	Eptr	**files;
    163  1.1     cgd {
    164  1.6   lukem 	int	fi;
    165  1.6   lukem 	char	*sep = " ";
    166  1.6   lukem 	int	someerrors;
    167  1.1     cgd 
    168  1.1     cgd 	/*
    169  1.1     cgd 	 *	first, simply dump out errors that
    170  1.1     cgd 	 *	don't pertain to any file
    171  1.1     cgd 	 */
    172  1.1     cgd 	someerrors = nopertain(files);
    173  1.1     cgd 
    174  1.1     cgd 	if (nfiles){
    175  1.1     cgd 		someerrors++;
    176  1.1     cgd 		fprintf(stdout, terse
    177  1.1     cgd 			? "%d file%s"
    178  1.1     cgd 			: "%d file%s contain%s errors",
    179  1.1     cgd 			nfiles, plural(nfiles), verbform(nfiles));
    180  1.1     cgd 		if (!terse){
    181  1.1     cgd 			FILEITERATE(fi, 1){
    182  1.1     cgd 				fprintf(stdout, "%s\"%s\" (%d)",
    183  1.1     cgd 					sep, (*files[fi])->error_text[0],
    184  1.7     mrg 					(int)(files[fi+1] - files[fi]));
    185  1.1     cgd 				sep = ", ";
    186  1.1     cgd 			}
    187  1.1     cgd 		}
    188  1.1     cgd 		fprintf(stdout, "\n");
    189  1.1     cgd 	}
    190  1.1     cgd 	if (!someerrors)
    191  1.1     cgd 		fprintf(stdout, "No errors.\n");
    192  1.1     cgd }
    193  1.1     cgd 
    194  1.1     cgd /*
    195  1.1     cgd  *	Dump out errors that don't pertain to any file
    196  1.1     cgd  */
    197  1.6   lukem int
    198  1.6   lukem nopertain(files)
    199  1.1     cgd 	Eptr	**files;
    200  1.1     cgd {
    201  1.1     cgd 	int	type;
    202  1.1     cgd 	int	someerrors = 0;
    203  1.6   lukem 	Eptr	*erpp;
    204  1.6   lukem 	Eptr	errorp;
    205  1.1     cgd 
    206  1.1     cgd 	if (files[1] - files[0] <= 0)
    207  1.1     cgd 		return(0);
    208  1.1     cgd 	for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
    209  1.1     cgd 		if (class_count[type] <= 0)
    210  1.1     cgd 			continue;
    211  1.1     cgd 		if (type > C_SYNC)
    212  1.1     cgd 			someerrors++;
    213  1.1     cgd 		if (terse){
    214  1.1     cgd 			fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
    215  1.1     cgd 				class_count[type], class_table[type]);
    216  1.1     cgd 		} else {
    217  1.1     cgd 			fprintf(stdout, "\n\t%d %s errors follow\n",
    218  1.1     cgd 				class_count[type], class_table[type]);
    219  1.1     cgd 			EITERATE(erpp, files, 0){
    220  1.1     cgd 				errorp = *erpp;
    221  1.1     cgd 				if (errorp->error_e_class == type){
    222  1.1     cgd 					errorprint(stdout, errorp, TRUE);
    223  1.1     cgd 				}
    224  1.1     cgd 			}
    225  1.1     cgd 		}
    226  1.1     cgd 	}
    227  1.1     cgd 	return(someerrors);
    228  1.1     cgd }
    229  1.1     cgd 
    230  1.1     cgd extern	boolean	notouch;
    231  1.1     cgd 
    232  1.6   lukem boolean
    233  1.6   lukem touchfiles(nfiles, files, r_edargc, r_edargv)
    234  1.1     cgd 	int	nfiles;
    235  1.1     cgd 	Eptr	**files;
    236  1.1     cgd 	int	*r_edargc;
    237  1.1     cgd 	char	***r_edargv;
    238  1.1     cgd {
    239  1.6   lukem 	char	*name;
    240  1.6   lukem 	Eptr	errorp;
    241  1.6   lukem 	int	fi;
    242  1.6   lukem 	Eptr	*erpp;
    243  1.6   lukem 	int	ntrueerrors;
    244  1.6   lukem 	boolean	scribbled;
    245  1.6   lukem 	int	n_pissed_on;	/* # of file touched*/
    246  1.6   lukem 	int	spread;
    247  1.1     cgd 
    248  1.1     cgd 	FILEITERATE(fi, 1){
    249  1.1     cgd 		name = (*files[fi])->error_text[0];
    250  1.1     cgd 		spread = files[fi+1] - files[fi];
    251  1.1     cgd 		fprintf(stdout, terse
    252  1.1     cgd 			? "\"%s\" has %d error%s, "
    253  1.1     cgd 			: "\nFile \"%s\" has %d error%s.\n"
    254  1.1     cgd 			, name ,spread ,plural(spread));
    255  1.1     cgd 		/*
    256  1.1     cgd 		 *	First, iterate through all error messages in this file
    257  1.1     cgd 		 *	to see how many of the error messages really will
    258  1.1     cgd 		 *	get inserted into the file.
    259  1.1     cgd 		 */
    260  1.1     cgd 		ntrueerrors = 0;
    261  1.1     cgd 		EITERATE(erpp, files, fi){
    262  1.1     cgd 			errorp = *erpp;
    263  1.1     cgd 			if (errorp->error_e_class == C_TRUE)
    264  1.1     cgd 				ntrueerrors++;
    265  1.1     cgd 		}
    266  1.1     cgd 		fprintf(stdout, terse
    267  1.1     cgd 		  ? "insert %d\n"
    268  1.1     cgd 		  : "\t%d of these errors can be inserted into the file.\n",
    269  1.1     cgd 			ntrueerrors);
    270  1.1     cgd 
    271  1.1     cgd 		hackfile(name, files, fi, ntrueerrors);
    272  1.1     cgd 	}
    273  1.1     cgd 	scribbled = FALSE;
    274  1.1     cgd 	n_pissed_on = 0;
    275  1.1     cgd 	FILEITERATE(fi, 1){
    276  1.1     cgd 		scribbled |= touchedfiles[fi];
    277  1.1     cgd 		n_pissed_on++;
    278  1.1     cgd 	}
    279  1.1     cgd 	if (scribbled){
    280  1.1     cgd 		/*
    281  1.1     cgd 		 *	Construct an execv argument
    282  1.1     cgd 		 */
    283  1.1     cgd 		execvarg(n_pissed_on, r_edargc, r_edargv);
    284  1.1     cgd 		return(TRUE);
    285  1.1     cgd 	} else {
    286  1.1     cgd 		if (!terse)
    287  1.1     cgd 			fprintf(stdout, "You didn't touch any files.\n");
    288  1.1     cgd 		return(FALSE);
    289  1.1     cgd 	}
    290  1.1     cgd }
    291  1.1     cgd 
    292  1.6   lukem void
    293  1.1     cgd hackfile(name, files, ix, nerrors)
    294  1.1     cgd 	char	*name;
    295  1.1     cgd 	Eptr	**files;
    296  1.1     cgd 	int	ix;
    297  1.6   lukem 	int	nerrors;
    298  1.1     cgd {
    299  1.1     cgd 	boolean	previewed;
    300  1.1     cgd 	int	errordest;	/* where errors go*/
    301  1.1     cgd 
    302  1.1     cgd 	if (!oktotouch(name)) {
    303  1.1     cgd 		previewed = FALSE;
    304  1.1     cgd 		errordest = TOSTDOUT;
    305  1.1     cgd 	} else {
    306  1.1     cgd 		previewed = preview(name, nerrors, files, ix);
    307  1.1     cgd 		errordest = settotouch(name);
    308  1.1     cgd 	}
    309  1.1     cgd 
    310  1.1     cgd 	if (errordest != TOSTDOUT)
    311  1.1     cgd 		touchedfiles[ix] = TRUE;
    312  1.1     cgd 
    313  1.1     cgd 	if (previewed && (errordest == TOSTDOUT))
    314  1.1     cgd 		return;
    315  1.1     cgd 
    316  1.1     cgd 	diverterrors(name, errordest, files, ix, previewed, nerrors);
    317  1.1     cgd 
    318  1.1     cgd 	if (errordest == TOTHEFILE){
    319  1.1     cgd 		/*
    320  1.1     cgd 		 *	overwrite the original file
    321  1.1     cgd 		 */
    322  1.1     cgd 		writetouched(1);
    323  1.1     cgd 	}
    324  1.1     cgd }
    325  1.1     cgd 
    326  1.6   lukem boolean
    327  1.6   lukem preview(name, nerrors, files, ix)
    328  1.1     cgd 	char	*name;
    329  1.1     cgd 	int	nerrors;
    330  1.1     cgd 	Eptr	**files;
    331  1.1     cgd 	int	ix;
    332  1.1     cgd {
    333  1.1     cgd 	int	back;
    334  1.6   lukem 	Eptr	*erpp;
    335  1.1     cgd 
    336  1.1     cgd 	if (nerrors <= 0)
    337  1.1     cgd 		return(FALSE);
    338  1.1     cgd 	back = FALSE;
    339  1.1     cgd 	if(query){
    340  1.1     cgd 		switch(inquire(terse
    341  1.1     cgd 		    ? "Preview? "
    342  1.1     cgd 		    : "Do you want to preview the errors first? ")){
    343  1.1     cgd 		case Q_YES:
    344  1.1     cgd 		case Q_yes:
    345  1.1     cgd 			back = TRUE;
    346  1.1     cgd 			EITERATE(erpp, files, ix){
    347  1.1     cgd 				errorprint(stdout, *erpp, TRUE);
    348  1.1     cgd 			}
    349  1.1     cgd 			if (!terse)
    350  1.1     cgd 				fprintf(stdout, "\n");
    351  1.1     cgd 		default:
    352  1.1     cgd 			break;
    353  1.1     cgd 		}
    354  1.1     cgd 	}
    355  1.1     cgd 	return(back);
    356  1.1     cgd }
    357  1.1     cgd 
    358  1.6   lukem int
    359  1.6   lukem settotouch(name)
    360  1.1     cgd 	char	*name;
    361  1.1     cgd {
    362  1.1     cgd 	int	dest = TOSTDOUT;
    363  1.1     cgd 
    364  1.1     cgd 	if (query){
    365  1.1     cgd 		switch(touchstatus = inquire(terse
    366  1.1     cgd 			? "Touch? "
    367  1.1     cgd 			: "Do you want to touch file \"%s\"? ",
    368  1.1     cgd 			name)){
    369  1.1     cgd 		case Q_NO:
    370  1.1     cgd 		case Q_no:
    371  1.1     cgd 			return(dest);
    372  1.1     cgd 		default:
    373  1.1     cgd 			break;
    374  1.1     cgd 		}
    375  1.1     cgd 	}
    376  1.1     cgd 
    377  1.1     cgd 	switch(probethisfile(name)){
    378  1.1     cgd 	case F_NOTREAD:
    379  1.1     cgd 		dest = TOSTDOUT;
    380  1.1     cgd 		fprintf(stdout, terse
    381  1.1     cgd 			? "\"%s\" unreadable\n"
    382  1.1     cgd 			: "File \"%s\" is unreadable\n",
    383  1.1     cgd 			name);
    384  1.1     cgd 		break;
    385  1.1     cgd 	case F_NOTWRITE:
    386  1.1     cgd 		dest = TOSTDOUT;
    387  1.1     cgd 		fprintf(stdout, terse
    388  1.1     cgd 			? "\"%s\" unwritable\n"
    389  1.1     cgd 			: "File \"%s\" is unwritable\n",
    390  1.1     cgd 			name);
    391  1.1     cgd 		break;
    392  1.1     cgd 	case F_NOTEXIST:
    393  1.1     cgd 		dest = TOSTDOUT;
    394  1.1     cgd 		fprintf(stdout, terse
    395  1.1     cgd 			? "\"%s\" not found\n"
    396  1.1     cgd 			: "Can't find file \"%s\" to insert error messages into.\n",
    397  1.1     cgd 			name);
    398  1.1     cgd 		break;
    399  1.1     cgd 	default:
    400  1.1     cgd 		dest = edit(name) ? TOSTDOUT : TOTHEFILE;
    401  1.1     cgd 		break;
    402  1.1     cgd 	}
    403  1.1     cgd 	return(dest);
    404  1.1     cgd }
    405  1.1     cgd 
    406  1.6   lukem void
    407  1.1     cgd diverterrors(name, dest, files, ix, previewed, nterrors)
    408  1.1     cgd 	char	*name;
    409  1.1     cgd 	int	dest;
    410  1.1     cgd 	Eptr	**files;
    411  1.1     cgd 	int	ix;
    412  1.1     cgd 	boolean	previewed;
    413  1.1     cgd 	int	nterrors;
    414  1.1     cgd {
    415  1.1     cgd 	int	nerrors;
    416  1.6   lukem 	Eptr	*erpp;
    417  1.6   lukem 	Eptr	errorp;
    418  1.1     cgd 
    419  1.1     cgd 	nerrors = files[ix+1] - files[ix];
    420  1.1     cgd 
    421  1.1     cgd 	if (   (nerrors != nterrors)
    422  1.1     cgd 	    && (!previewed) ){
    423  1.1     cgd 		fprintf(stdout, terse
    424  1.1     cgd 			? "Uninserted errors\n"
    425  1.1     cgd 			: ">>Uninserted errors for file \"%s\" follow.\n",
    426  1.1     cgd 			name);
    427  1.1     cgd 	}
    428  1.1     cgd 
    429  1.1     cgd 	EITERATE(erpp, files, ix){
    430  1.1     cgd 		errorp = *erpp;
    431  1.1     cgd 		if (errorp->error_e_class != C_TRUE){
    432  1.1     cgd 			if (previewed || touchstatus == Q_NO)
    433  1.1     cgd 				continue;
    434  1.1     cgd 			errorprint(stdout, errorp, TRUE);
    435  1.1     cgd 			continue;
    436  1.1     cgd 		}
    437  1.1     cgd 		switch (dest){
    438  1.1     cgd 		case TOSTDOUT:
    439  1.1     cgd 			if (previewed || touchstatus == Q_NO)
    440  1.1     cgd 				continue;
    441  1.1     cgd 			errorprint(stdout,errorp, TRUE);
    442  1.1     cgd 			break;
    443  1.1     cgd 		case TOTHEFILE:
    444  1.1     cgd 			insert(errorp->error_line);
    445  1.1     cgd 			text(errorp, FALSE);
    446  1.1     cgd 			break;
    447  1.1     cgd 		}
    448  1.1     cgd 	}
    449  1.1     cgd }
    450  1.1     cgd 
    451  1.6   lukem int
    452  1.6   lukem oktotouch(filename)
    453  1.1     cgd 	char	*filename;
    454  1.1     cgd {
    455  1.6   lukem 	extern	char	*suffixlist;
    456  1.6   lukem 	char	*src;
    457  1.6   lukem 	char	*pat;
    458  1.6   lukem 	char	*osrc;
    459  1.1     cgd 
    460  1.1     cgd 	pat = suffixlist;
    461  1.1     cgd 	if (pat == 0)
    462  1.1     cgd 		return(0);
    463  1.1     cgd 	if (*pat == '*')
    464  1.1     cgd 		return(1);
    465  1.1     cgd 	while (*pat++ != '.')
    466  1.1     cgd 		continue;
    467  1.1     cgd 	--pat;		/* point to the period */
    468  1.1     cgd 
    469  1.1     cgd 	for (src = &filename[strlen(filename)], --src;
    470  1.1     cgd 	     (src > filename) && (*src != '.'); --src)
    471  1.1     cgd 		continue;
    472  1.1     cgd 	if (*src != '.')
    473  1.1     cgd 		return(0);
    474  1.1     cgd 
    475  1.1     cgd 	for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
    476  1.1     cgd 		for (;   *src			/* not at end of the source */
    477  1.1     cgd 		      && *pat			/* not off end of pattern */
    478  1.1     cgd 		      && *pat != '.'		/* not off end of sub pattern */
    479  1.1     cgd 		      && *pat != '*'		/* not wild card */
    480  1.1     cgd 		      && *src == *pat;		/* and equal... */
    481  1.1     cgd 		      src++, pat++)
    482  1.1     cgd 			continue;
    483  1.1     cgd 		if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
    484  1.1     cgd 			return(1);
    485  1.1     cgd 		if (*src != 0 && *pat == '*')
    486  1.1     cgd 			return(1);
    487  1.1     cgd 		while (*pat && *pat != '.')
    488  1.1     cgd 			pat++;
    489  1.1     cgd 		if (! *pat)
    490  1.1     cgd 			return(0);
    491  1.1     cgd 	}
    492  1.1     cgd 	return(0);
    493  1.1     cgd }
    494  1.6   lukem 
    495  1.1     cgd /*
    496  1.1     cgd  *	Construct an execv argument
    497  1.1     cgd  *	We need 1 argument for the editor's name
    498  1.1     cgd  *	We need 1 argument for the initial search string
    499  1.1     cgd  *	We need n_pissed_on arguments for the file names
    500  1.1     cgd  *	We need 1 argument that is a null for execv.
    501  1.1     cgd  *	The caller fills in the editor's name.
    502  1.1     cgd  *	We fill in the initial search string.
    503  1.1     cgd  *	We fill in the arguments, and the null.
    504  1.1     cgd  */
    505  1.6   lukem void
    506  1.1     cgd execvarg(n_pissed_on, r_argc, r_argv)
    507  1.1     cgd 	int	n_pissed_on;
    508  1.1     cgd 	int	*r_argc;
    509  1.1     cgd 	char	***r_argv;
    510  1.1     cgd {
    511  1.1     cgd 	Eptr	p;
    512  1.1     cgd 	char	*sep;
    513  1.1     cgd 	int	fi;
    514  1.1     cgd 
    515  1.6   lukem 	sep = NULL;
    516  1.1     cgd 	(*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
    517  1.1     cgd 	(*r_argc) =  n_pissed_on + 2;
    518  1.1     cgd 	(*r_argv)[1] = "+1;/###/";
    519  1.1     cgd 	n_pissed_on = 2;
    520  1.1     cgd 	if (!terse){
    521  1.1     cgd 		fprintf(stdout, "You touched file(s):");
    522  1.1     cgd 		sep = " ";
    523  1.1     cgd 	}
    524  1.1     cgd 	FILEITERATE(fi, 1){
    525  1.1     cgd 		if (!touchedfiles[fi])
    526  1.1     cgd 			continue;
    527  1.1     cgd 		p = *(files[fi]);
    528  1.1     cgd 		if (!terse){
    529  1.1     cgd 			fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
    530  1.1     cgd 			sep = ", ";
    531  1.1     cgd 		}
    532  1.1     cgd 		(*r_argv)[n_pissed_on++] = p->error_text[0];
    533  1.1     cgd 	}
    534  1.1     cgd 	if (!terse)
    535  1.1     cgd 		fprintf(stdout, "\n");
    536  1.1     cgd 	(*r_argv)[n_pissed_on] = 0;
    537  1.1     cgd }
    538  1.1     cgd 
    539  1.1     cgd FILE	*o_touchedfile;	/* the old file */
    540  1.1     cgd FILE	*n_touchedfile;	/* the new file */
    541  1.1     cgd char	*o_name;
    542  1.9  kleink char	n_name[MAXPATHLEN];
    543  1.1     cgd int	o_lineno;
    544  1.1     cgd int	n_lineno;
    545  1.1     cgd boolean	tempfileopen = FALSE;
    546  1.1     cgd /*
    547  1.1     cgd  *	open the file; guaranteed to be both readable and writable
    548  1.1     cgd  *	Well, if it isn't, then return TRUE if something failed
    549  1.1     cgd  */
    550  1.6   lukem boolean
    551  1.6   lukem edit(name)
    552  1.1     cgd 	char	*name;
    553  1.1     cgd {
    554  1.4   lukem 	int fd;
    555  1.9  kleink 	const char *tmpdir;
    556  1.4   lukem 
    557  1.1     cgd 	o_name = name;
    558  1.1     cgd 	if ( (o_touchedfile = fopen(name, "r")) == NULL){
    559  1.1     cgd 		fprintf(stderr, "%s: Can't open file \"%s\" to touch (read).\n",
    560  1.1     cgd 			processname, name);
    561  1.1     cgd 		return(TRUE);
    562  1.1     cgd 	}
    563  1.9  kleink 	if ((tmpdir = getenv("TMPDIR")) == NULL)
    564  1.9  kleink 		tmpdir = _PATH_TMP;
    565  1.9  kleink 	(void)snprintf(n_name, sizeof (n_name), "%s/%s", tmpdir, TMPFILE);
    566  1.4   lukem 	fd = -1;
    567  1.4   lukem 	if ((fd = mkstemp(n_name)) == -1 ||
    568  1.4   lukem 	    (n_touchedfile = fdopen(fd, "w")) == NULL) {
    569  1.4   lukem 		if (fd != -1)
    570  1.4   lukem 			close(fd);
    571  1.1     cgd 		fprintf(stderr,"%s: Can't open file \"%s\" to touch (write).\n",
    572  1.1     cgd 			processname, name);
    573  1.1     cgd 		return(TRUE);
    574  1.1     cgd 	}
    575  1.1     cgd 	tempfileopen = TRUE;
    576  1.1     cgd 	n_lineno = 0;
    577  1.1     cgd 	o_lineno = 0;
    578  1.1     cgd 	return(FALSE);
    579  1.1     cgd }
    580  1.6   lukem 
    581  1.1     cgd /*
    582  1.1     cgd  *	Position to the line (before, after) the line given by place
    583  1.1     cgd  */
    584  1.1     cgd char	edbuf[BUFSIZ];
    585  1.6   lukem 
    586  1.6   lukem void
    587  1.1     cgd insert(place)
    588  1.1     cgd 	int	place;
    589  1.1     cgd {
    590  1.1     cgd 	--place;	/* always insert messages before the offending line*/
    591  1.1     cgd 	for(; o_lineno < place; o_lineno++, n_lineno++){
    592  1.1     cgd 		if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
    593  1.1     cgd 			return;
    594  1.1     cgd 		fputs(edbuf, n_touchedfile);
    595  1.1     cgd 	}
    596  1.1     cgd }
    597  1.1     cgd 
    598  1.6   lukem void
    599  1.1     cgd text(p, use_all)
    600  1.6   lukem 	Eptr	p;
    601  1.6   lukem 	boolean	use_all;
    602  1.1     cgd {
    603  1.1     cgd 	int	offset = use_all ? 0 : 2;
    604  1.1     cgd 
    605  1.1     cgd 	fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
    606  1.1     cgd 	fprintf(n_touchedfile, "%d [%s] ",
    607  1.1     cgd 		p->error_line,
    608  1.1     cgd 		lang_table[p->error_language].lang_name);
    609  1.1     cgd 	wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
    610  1.1     cgd 	fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
    611  1.1     cgd 	n_lineno++;
    612  1.1     cgd }
    613  1.1     cgd 
    614  1.1     cgd /*
    615  1.1     cgd  *	write the touched file to its temporary copy,
    616  1.1     cgd  *	then bring the temporary in over the local file
    617  1.1     cgd  */
    618  1.6   lukem boolean
    619  1.1     cgd writetouched(overwrite)
    620  1.1     cgd 	int	overwrite;
    621  1.1     cgd {
    622  1.6   lukem 	int	nread;
    623  1.6   lukem 	FILE	*localfile;
    624  1.6   lukem 	FILE	*tmpfile;
    625  1.6   lukem 	int	botch;
    626  1.6   lukem 	int	oktorm;
    627  1.1     cgd 
    628  1.1     cgd 	botch = 0;
    629  1.1     cgd 	oktorm = 1;
    630  1.5      pk 	while ((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != 0) {
    631  1.1     cgd 		if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
    632  1.1     cgd 			/*
    633  1.1     cgd 			 *	Catastrophe in temporary area: file system full?
    634  1.1     cgd 			 */
    635  1.1     cgd 			botch = 1;
    636  1.1     cgd 			fprintf(stderr,
    637  1.1     cgd 			  "%s: write failure: No errors inserted in \"%s\"\n",
    638  1.1     cgd 			  processname, o_name);
    639  1.1     cgd 		}
    640  1.1     cgd 	}
    641  1.1     cgd 	fclose(n_touchedfile);
    642  1.1     cgd 	fclose(o_touchedfile);
    643  1.1     cgd 	/*
    644  1.1     cgd 	 *	Now, copy the temp file back over the original
    645  1.1     cgd 	 *	file, thus preserving links, etc
    646  1.1     cgd 	 */
    647  1.1     cgd 	if (botch == 0 && overwrite){
    648  1.1     cgd 		botch = 0;
    649  1.1     cgd 		localfile = NULL;
    650  1.1     cgd 		tmpfile = NULL;
    651  1.1     cgd 		if ((localfile = fopen(o_name, "w")) == NULL){
    652  1.1     cgd 			fprintf(stderr,
    653  1.1     cgd 				"%s: Can't open file \"%s\" to overwrite.\n",
    654  1.1     cgd 				processname, o_name);
    655  1.1     cgd 			botch++;
    656  1.1     cgd 		}
    657  1.1     cgd 		if ((tmpfile = fopen(n_name, "r")) == NULL){
    658  1.1     cgd 			fprintf(stderr, "%s: Can't open file \"%s\" to read.\n",
    659  1.1     cgd 				processname, n_name);
    660  1.1     cgd 			botch++;
    661  1.1     cgd 		}
    662  1.1     cgd 		if (!botch)
    663  1.1     cgd 			oktorm = mustoverwrite(localfile, tmpfile);
    664  1.1     cgd 		if (localfile != NULL)
    665  1.1     cgd 			fclose(localfile);
    666  1.1     cgd 		if (tmpfile != NULL)
    667  1.1     cgd 			fclose(tmpfile);
    668  1.1     cgd 	}
    669  1.1     cgd 	if (oktorm == 0){
    670  1.1     cgd 		fprintf(stderr, "%s: Catastrophe: A copy of \"%s\": was saved in \"%s\"\n",
    671  1.1     cgd 			processname, o_name, n_name);
    672  1.1     cgd 		exit(1);
    673  1.1     cgd 	}
    674  1.1     cgd 	/*
    675  1.1     cgd 	 *	Kiss the temp file good bye
    676  1.1     cgd 	 */
    677  1.1     cgd 	unlink(n_name);
    678  1.1     cgd 	tempfileopen = FALSE;
    679  1.1     cgd 	return(TRUE);
    680  1.1     cgd }
    681  1.6   lukem 
    682  1.1     cgd /*
    683  1.1     cgd  *	return 1 if the tmpfile can be removed after writing it out
    684  1.1     cgd  */
    685  1.6   lukem int
    686  1.6   lukem mustoverwrite(preciousfile, tmpfile)
    687  1.1     cgd 	FILE	*preciousfile;
    688  1.1     cgd 	FILE	*tmpfile;
    689  1.1     cgd {
    690  1.1     cgd 	int	nread;
    691  1.1     cgd 
    692  1.5      pk 	while ((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != 0) {
    693  1.1     cgd 		if (mustwrite(edbuf, nread, preciousfile) == 0)
    694  1.1     cgd 			return(0);
    695  1.1     cgd 	}
    696  1.1     cgd 	return(1);
    697  1.1     cgd }
    698  1.1     cgd /*
    699  1.1     cgd  *	return 0 on catastrophe
    700  1.1     cgd  */
    701  1.6   lukem int
    702  1.1     cgd mustwrite(base, n, preciousfile)
    703  1.1     cgd 	char	*base;
    704  1.1     cgd 	int	n;
    705  1.1     cgd 	FILE	*preciousfile;
    706  1.1     cgd {
    707  1.1     cgd 	int	nwrote;
    708  1.1     cgd 
    709  1.1     cgd 	if (n <= 0)
    710  1.1     cgd 		return(1);
    711  1.1     cgd 	nwrote = fwrite(base, 1, n, preciousfile);
    712  1.1     cgd 	if (nwrote == n)
    713  1.1     cgd 		return(1);
    714  1.1     cgd 	perror(processname);
    715  1.1     cgd 	switch(inquire(terse
    716  1.1     cgd 	    ? "Botch overwriting: retry? "
    717  1.1     cgd 	    : "Botch overwriting the source file: retry? ")){
    718  1.1     cgd 	case Q_YES:
    719  1.1     cgd 	case Q_yes:
    720  1.1     cgd 		mustwrite(base + nwrote, n - nwrote, preciousfile);
    721  1.1     cgd 		return(1);
    722  1.1     cgd 	case Q_NO:
    723  1.1     cgd 	case Q_no:
    724  1.1     cgd 		switch(inquire("Are you sure? ")){
    725  1.1     cgd 		case Q_YES:
    726  1.1     cgd 		case Q_yes:
    727  1.1     cgd 			return(0);
    728  1.1     cgd 		case Q_NO:
    729  1.1     cgd 		case Q_no:
    730  1.1     cgd 			mustwrite(base + nwrote, n - nwrote, preciousfile);
    731  1.1     cgd 			return(1);
    732  1.1     cgd 		}
    733  1.1     cgd 	default:
    734  1.1     cgd 		return(0);
    735  1.1     cgd 	}
    736  1.1     cgd }
    737  1.1     cgd 
    738  1.1     cgd void
    739  1.6   lukem onintr(dummy)
    740  1.6   lukem 	int dummy;
    741  1.1     cgd {
    742  1.1     cgd 	switch(inquire(terse
    743  1.1     cgd 	    ? "\nContinue? "
    744  1.1     cgd 	    : "\nInterrupt: Do you want to continue? ")){
    745  1.1     cgd 	case Q_YES:
    746  1.1     cgd 	case Q_yes:
    747  1.1     cgd 		signal(SIGINT, onintr);
    748  1.1     cgd 		return;
    749  1.1     cgd 	default:
    750  1.1     cgd 		if (tempfileopen){
    751  1.1     cgd 			/*
    752  1.1     cgd 			 *	Don't overwrite the original file!
    753  1.1     cgd 			 */
    754  1.1     cgd 			writetouched(0);
    755  1.1     cgd 		}
    756  1.1     cgd 		exit(1);
    757  1.1     cgd 	}
    758  1.1     cgd 	/*NOTREACHED*/
    759  1.1     cgd }
    760  1.1     cgd 
    761  1.6   lukem void
    762  1.1     cgd errorprint(place, errorp, print_all)
    763  1.1     cgd 	FILE	*place;
    764  1.1     cgd 	Eptr	errorp;
    765  1.1     cgd 	boolean	print_all;
    766  1.1     cgd {
    767  1.1     cgd 	int	offset = print_all ? 0 : 2;
    768  1.1     cgd 
    769  1.1     cgd 	if (errorp->error_e_class == C_IGNORE)
    770  1.1     cgd 		return;
    771  1.1     cgd 	fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
    772  1.1     cgd 	wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
    773  1.1     cgd 	putc('\n', place);
    774  1.1     cgd }
    775  1.1     cgd 
    776  1.6   lukem int
    777  1.6   lukem #if __STDC__
    778  1.6   lukem inquire(char *fmt, ...)
    779  1.6   lukem #else
    780  1.6   lukem inquire(fmt, va_alist)
    781  1.1     cgd 	char	*fmt;
    782  1.6   lukem 	va_dcl
    783  1.6   lukem #endif
    784  1.1     cgd {
    785  1.6   lukem 	va_list ap;
    786  1.1     cgd 	char	buffer[128];
    787  1.1     cgd 
    788  1.6   lukem #if __STDC__
    789  1.6   lukem 	va_start(ap, fmt);
    790  1.6   lukem #else
    791  1.6   lukem 	va_start(ap);
    792  1.6   lukem #endif
    793  1.6   lukem 
    794  1.1     cgd 	if (queryfile == NULL)
    795  1.1     cgd 		return(0);
    796  1.1     cgd 	for(;;){
    797  1.1     cgd 		do{
    798  1.1     cgd 			fflush(stdout);
    799  1.6   lukem 			vfprintf(stderr, fmt, ap);
    800  1.1     cgd 			fflush(stderr);
    801  1.1     cgd 		} while (fgets(buffer, 127, queryfile) == NULL);
    802  1.1     cgd 		switch(buffer[0]){
    803  1.1     cgd 		case 'Y':	return(Q_YES);
    804  1.1     cgd 		case 'y':	return(Q_yes);
    805  1.1     cgd 		case 'N':	return(Q_NO);
    806  1.1     cgd 		case 'n':	return(Q_no);
    807  1.1     cgd 		default:	fprintf(stderr, "Yes or No only!\n");
    808  1.1     cgd 		}
    809  1.1     cgd 	}
    810  1.1     cgd }
    811  1.1     cgd 
    812  1.6   lukem int
    813  1.6   lukem probethisfile(name)
    814  1.1     cgd 	char	*name;
    815  1.1     cgd {
    816  1.1     cgd 	struct stat statbuf;
    817  1.1     cgd 	if (stat(name, &statbuf) < 0)
    818  1.1     cgd 		return(F_NOTEXIST);
    819  1.1     cgd 	if((statbuf.st_mode & S_IREAD) == 0)
    820  1.1     cgd 		return(F_NOTREAD);
    821  1.1     cgd 	if((statbuf.st_mode & S_IWRITE) == 0)
    822  1.1     cgd 		return(F_NOTWRITE);
    823  1.1     cgd 	return(F_TOUCHIT);
    824  1.1     cgd }
    825