xref: /csrg-svn/usr.bin/error/touch.c (revision 35266)
121639Sdist /*
221639Sdist  * Copyright (c) 1980 Regents of the University of California.
334664Sbostic  * All rights reserved.
434664Sbostic  *
534664Sbostic  * Redistribution and use in source and binary forms are permitted
634874Sbostic  * provided that the above copyright notice and this paragraph are
734874Sbostic  * duplicated in all such forms and that any documentation,
834874Sbostic  * advertising materials, and other materials related to such
934874Sbostic  * distribution and use acknowledge that the software was developed
1034874Sbostic  * by the University of California, Berkeley.  The name of the
1134874Sbostic  * University may not be used to endorse or promote products derived
1234874Sbostic  * from this software without specific prior written permission.
1334874Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434874Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534874Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621639Sdist  */
1721639Sdist 
1821639Sdist #ifndef lint
19*35266Sbostic static char sccsid[] = "@(#)touch.c	5.4 (Berkeley) 07/22/88";
2034664Sbostic #endif /* not lint */
2121639Sdist 
221460Sroot #include <stdio.h>
231460Sroot #include <ctype.h>
241460Sroot #include <sys/types.h>
251460Sroot #include <sys/stat.h>
261460Sroot #include <signal.h>
271460Sroot #include "error.h"
281460Sroot 
295595Srrh /*
305595Srrh  *	Iterate through errors
315595Srrh  */
325595Srrh #define EITERATE(p, fv, i)	for (p = fv[i]; p < fv[i+1]; p++)
335595Srrh #define	ECITERATE(ei, p, lb)	for (ei = lb; p = errors[ei],ei < nerrors; ei++)
345595Srrh 
355595Srrh #define	FILEITERATE(fi, lb)	for (fi = lb; fi <= nfiles; fi++)
365595Srrh int	touchstatus = Q_YES;
375595Srrh 
381460Sroot findfiles(nerrors, errors, r_nfiles, r_files)
395595Srrh 		int	nerrors;
405595Srrh 	Eptr	*errors;
415595Srrh 		int	*r_nfiles;
425595Srrh 	Eptr	***r_files;
431460Sroot {
445595Srrh 		int	nfiles;
455595Srrh 	Eptr	**files;
461460Sroot 
475595Srrh 		char	*name;
485595Srrh 	reg	int	ei;
495595Srrh 		int	fi;
505595Srrh 	reg	Eptr	errorp;
515595Srrh 
525595Srrh 	nfiles = countfiles(errors);
535595Srrh 
545595Srrh 	files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
551460Sroot 	touchedfiles = (boolean	*)Calloc(nfiles+3, sizeof(boolean));
561460Sroot 	/*
575595Srrh 	 *	Now, partition off the error messages
581460Sroot 	 *	into those that are synchronization, discarded or
591460Sroot 	 *	not specific to any file, and those that were
601460Sroot 	 *	nulled or true errors.
611460Sroot 	 */
621460Sroot 	files[0] = &errors[0];
635595Srrh 	ECITERATE(ei, errorp, 0){
645595Srrh 		if ( ! (NOTSORTABLE(errorp->error_e_class)))
655595Srrh 			break;
661460Sroot 	}
671460Sroot 	/*
685595Srrh 	 *	Now, and partition off all error messages
691460Sroot 	 *	for a given file.
701460Sroot 	 */
715595Srrh 	files[1] = &errors[ei];
721460Sroot 	touchedfiles[0] = touchedfiles[1] = FALSE;
735595Srrh 	name = "\1";
745595Srrh 	fi = 1;
755595Srrh 	ECITERATE(ei, errorp, ei){
765595Srrh 		if (   (errorp->error_e_class == C_NULLED)
775595Srrh 		    || (errorp->error_e_class == C_TRUE) ){
785595Srrh 			if (strcmp(errorp->error_text[0], name) != 0){
795595Srrh 				name = errorp->error_text[0];
805595Srrh 				touchedfiles[fi] = FALSE;
815595Srrh 				files[fi] = &errors[ei];
825595Srrh 				fi++;
831460Sroot 			}
841460Sroot 		}
851460Sroot 	}
865595Srrh 	files[fi] = &errors[nerrors];
871460Sroot 	*r_nfiles = nfiles;
881460Sroot 	*r_files = files;
891460Sroot }
901460Sroot 
915595Srrh int countfiles(errors)
925595Srrh 	Eptr	*errors;
935595Srrh {
945595Srrh 	char	*name;
955595Srrh 	int	ei;
965595Srrh 	reg	Eptr	errorp;
975595Srrh 
985595Srrh 	int	nfiles;
995595Srrh 	nfiles = 0;
1005595Srrh 	name = "\1";
1015595Srrh 	ECITERATE(ei, errorp, 0){
1025595Srrh 		if (SORTABLE(errorp->error_e_class)){
1035595Srrh 			if (strcmp(errorp->error_text[0],name) != 0){
1045595Srrh 				nfiles++;
1055595Srrh 				name = errorp->error_text[0];
1065595Srrh 			}
1075595Srrh 		}
1085595Srrh 	}
1095595Srrh 	return(nfiles);
1105595Srrh }
1111460Sroot char	*class_table[] = {
1121460Sroot 	/*C_UNKNOWN	0	*/	"Unknown",
1131460Sroot 	/*C_IGNORE	1	*/	"ignore",
1141460Sroot 	/*C_SYNC	2	*/	"synchronization",
1151460Sroot 	/*C_DISCARD	3	*/	"discarded",
1161460Sroot 	/*C_NONSPEC	4	*/	"non specific",
1171460Sroot 	/*C_THISFILE	5	*/	"specific to this file",
1181460Sroot 	/*C_NULLED	6	*/	"nulled",
1191460Sroot 	/*C_TRUE	7	*/	"true",
1201460Sroot 	/*C_DUPL	8	*/	"duplicated"
1211460Sroot };
1221460Sroot 
1231460Sroot int	class_count[C_LAST - C_FIRST] = {0};
1241460Sroot 
1251460Sroot filenames(nfiles, files)
1261460Sroot 	int	nfiles;
1275595Srrh 	Eptr	**files;
1281460Sroot {
1295595Srrh 	reg	int	fi;
1305595Srrh 		char	*sep = " ";
1315595Srrh 	extern	char	*class_table[];
1325595Srrh 		int	someerrors;
1331460Sroot 
1341460Sroot 	/*
1355595Srrh 	 *	first, simply dump out errors that
1361460Sroot 	 *	don't pertain to any file
1371460Sroot 	 */
1385595Srrh 	someerrors = nopertain(files);
1395595Srrh 
1401460Sroot 	if (nfiles){
1411460Sroot 		someerrors++;
1425595Srrh 		fprintf(stdout, terse
1435595Srrh 			? "%d file%s"
1445595Srrh 			: "%d file%s contain%s errors",
1455595Srrh 			nfiles, plural(nfiles), verbform(nfiles));
1465595Srrh 		if (!terse){
1475595Srrh 			FILEITERATE(fi, 1){
1485595Srrh 				fprintf(stdout, "%s\"%s\" (%d)",
1495595Srrh 					sep, (*files[fi])->error_text[0],
1505595Srrh 					files[fi+1] - files[fi]);
1515595Srrh 				sep = ", ";
1525595Srrh 			}
1531460Sroot 		}
1541460Sroot 		fprintf(stdout, "\n");
1551460Sroot 	}
1561460Sroot 	if (!someerrors)
1571460Sroot 		fprintf(stdout, "No errors.\n");
1581460Sroot }
1591460Sroot 
1605595Srrh /*
1615595Srrh  *	Dump out errors that don't pertain to any file
1625595Srrh  */
1635595Srrh int nopertain(files)
1645595Srrh 	Eptr	**files;
1655595Srrh {
1665595Srrh 	int	type;
1675595Srrh 	int	someerrors = 0;
1685595Srrh 	reg	Eptr	*erpp;
1695595Srrh 	reg	Eptr	errorp;
1705595Srrh 
1715595Srrh 	if (files[1] - files[0] <= 0)
1725595Srrh 		return(0);
1735595Srrh 	for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
1745595Srrh 		if (class_count[type] <= 0)
1755595Srrh 			continue;
1765595Srrh 		if (type > C_SYNC)
1775595Srrh 			someerrors++;
1785595Srrh 		if (terse){
1795595Srrh 			fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
1805595Srrh 				class_count[type], class_table[type]);
1815595Srrh 		} else {
1825595Srrh 			fprintf(stdout, "\n\t%d %s errors follow\n",
1835595Srrh 				class_count[type], class_table[type]);
1845595Srrh 			EITERATE(erpp, files, 0){
1855595Srrh 				errorp = *erpp;
1865595Srrh 				if (errorp->error_e_class == type){
1875595Srrh 					errorprint(stdout, errorp, TRUE);
1885595Srrh 				}
1895595Srrh 			}
1905595Srrh 		}
1915595Srrh 	}
1925595Srrh 	return(someerrors);
1935595Srrh }
1945595Srrh 
1951460Sroot extern	boolean	notouch;
1961460Sroot 
1971460Sroot boolean touchfiles(nfiles, files, r_edargc, r_edargv)
1981460Sroot 	int	nfiles;
1995595Srrh 	Eptr	**files;
2001460Sroot 	int	*r_edargc;
2011460Sroot 	char	***r_edargv;
2021460Sroot {
2035595Srrh 		char	*name;
2045595Srrh 	reg	Eptr	errorp;
2055595Srrh 	reg	int	fi;
2065595Srrh 	reg	Eptr	*erpp;
2075595Srrh 		int		ntrueerrors;
2085595Srrh 		boolean		scribbled;
2095595Srrh 		int		n_pissed_on;	/* # of file touched*/
2105595Srrh 		int	spread;
2111462Sroot 
2125595Srrh 	FILEITERATE(fi, 1){
2135595Srrh 		name = (*files[fi])->error_text[0];
2145595Srrh 		spread = files[fi+1] - files[fi];
2155595Srrh 		fprintf(stdout, terse
2165595Srrh 			? "\"%s\" has %d error%s, "
2175595Srrh 			: "\nFile \"%s\" has %d error%s.\n"
2185595Srrh 			, name ,spread ,plural(spread));
2191460Sroot 		/*
2201460Sroot 		 *	First, iterate through all error messages in this file
2211460Sroot 		 *	to see how many of the error messages really will
2221460Sroot 		 *	get inserted into the file.
2231460Sroot 		 */
2245595Srrh 		ntrueerrors = 0;
2255595Srrh 		EITERATE(erpp, files, fi){
2261460Sroot 			errorp = *erpp;
2271460Sroot 			if (errorp->error_e_class == C_TRUE)
2281460Sroot 				ntrueerrors++;
2291460Sroot 		}
2305595Srrh 		fprintf(stdout, terse
2315595Srrh 		  ? "insert %d\n"
2325595Srrh 		  : "\t%d of these errors can be inserted into the file.\n",
2331460Sroot 			ntrueerrors);
2341460Sroot 
2355595Srrh 		hackfile(name, files, fi, ntrueerrors);
2365595Srrh 	}
2371460Sroot 	scribbled = FALSE;
2385595Srrh 	n_pissed_on = 0;
2395595Srrh 	FILEITERATE(fi, 1){
2405595Srrh 		scribbled |= touchedfiles[fi];
2411460Sroot 		n_pissed_on++;
2421460Sroot 	}
2431460Sroot 	if (scribbled){
2441460Sroot 		/*
2451460Sroot 		 *	Construct an execv argument
2461460Sroot 		 */
2475595Srrh 		execvarg(n_pissed_on, r_edargc, r_edargv);
2481460Sroot 		return(TRUE);
2491460Sroot 	} else {
2505595Srrh 		if (!terse)
2515595Srrh 			fprintf(stdout, "You didn't touch any files.\n");
2521460Sroot 		return(FALSE);
2531460Sroot 	}
2545595Srrh }
2551460Sroot 
2565595Srrh hackfile(name, files, ix, nerrors)
2575595Srrh 	char	*name;
2585595Srrh 	Eptr	**files;
2595595Srrh 	int	ix;
2605595Srrh {
2615595Srrh 	boolean	previewed;
2625595Srrh 	int	errordest;	/* where errors go*/
2635595Srrh 
26417212Sralph 	if (!oktotouch(name)) {
26517212Sralph 		previewed = FALSE;
26617212Sralph 		errordest = TOSTDOUT;
26717212Sralph 	} else {
26817212Sralph 		previewed = preview(name, nerrors, files, ix);
26917212Sralph 		errordest = settotouch(name);
27017212Sralph 	}
2715595Srrh 
2725595Srrh 	if (errordest != TOSTDOUT)
2735595Srrh 		touchedfiles[ix] = TRUE;
2745595Srrh 
2755595Srrh 	if (previewed && (errordest == TOSTDOUT))
2765595Srrh 		return;
2775595Srrh 
2785595Srrh 	diverterrors(name, errordest, files, ix, previewed, nerrors);
2795595Srrh 
2806612Srrh 	if (errordest == TOTHEFILE){
2816612Srrh 		/*
2826612Srrh 		 *	overwrite the original file
2836612Srrh 		 */
2846612Srrh 		writetouched(1);
2856612Srrh 	}
2865595Srrh }
2875595Srrh 
2885595Srrh boolean preview(name, nerrors, files, ix)
2895595Srrh 	char	*name;
2905595Srrh 	int	nerrors;
2915595Srrh 	Eptr	**files;
2925595Srrh 	int	ix;
2935595Srrh {
2945595Srrh 	int	back;
2955595Srrh 	reg	Eptr	*erpp;
2965595Srrh 
2975595Srrh 	if (nerrors <= 0)
29817212Sralph 		return(FALSE);
29917212Sralph 	back = FALSE;
3005595Srrh 	if(query){
3015595Srrh 		switch(inquire(terse
3025595Srrh 		    ? "Preview? "
3035595Srrh 		    : "Do you want to preview the errors first? ")){
3045595Srrh 		case Q_YES:
3055595Srrh 		case Q_yes:
30617212Sralph 			back = TRUE;
3075595Srrh 			EITERATE(erpp, files, ix){
3085595Srrh 				errorprint(stdout, *erpp, TRUE);
3095595Srrh 			}
3105595Srrh 			if (!terse)
3115595Srrh 				fprintf(stdout, "\n");
3125595Srrh 		default:
3135595Srrh 			break;
3145595Srrh 		}
3155595Srrh 	}
3165595Srrh 	return(back);
3175595Srrh }
3185595Srrh 
3195595Srrh int settotouch(name)
3205595Srrh 	char	*name;
3215595Srrh {
3225595Srrh 	int	dest = TOSTDOUT;
3235595Srrh 
3245595Srrh 	if (query){
3255595Srrh 		switch(touchstatus = inquire(terse
3265595Srrh 			? "Touch? "
3275595Srrh 			: "Do you want to touch file \"%s\"? ",
3285595Srrh 			name)){
3295595Srrh 		case Q_NO:
3305595Srrh 		case Q_no:
3315595Srrh 			return(dest);
3325595Srrh 		default:
3335595Srrh 			break;
3345595Srrh 		}
3355595Srrh 	}
3365595Srrh 
3375595Srrh 	switch(probethisfile(name)){
3385595Srrh 	case F_NOTREAD:
3395595Srrh 		dest = TOSTDOUT;
3405595Srrh 		fprintf(stdout, terse
3415595Srrh 			? "\"%s\" unreadable\n"
3425595Srrh 			: "File \"%s\" is unreadable\n",
3435595Srrh 			name);
3445595Srrh 		break;
3455595Srrh 	case F_NOTWRITE:
3465595Srrh 		dest = TOSTDOUT;
3475595Srrh 		fprintf(stdout, terse
3485595Srrh 			? "\"%s\" unwritable\n"
3495595Srrh 			: "File \"%s\" is unwritable\n",
3505595Srrh 			name);
3515595Srrh 		break;
3525595Srrh 	case F_NOTEXIST:
3535595Srrh 		dest = TOSTDOUT;
3545595Srrh 		fprintf(stdout, terse
3555595Srrh 			? "\"%s\" not found\n"
3565595Srrh 			: "Can't find file \"%s\" to insert error messages into.\n",
3575595Srrh 			name);
3585595Srrh 		break;
3595595Srrh 	default:
3605595Srrh 		dest = edit(name) ? TOSTDOUT : TOTHEFILE;
3615595Srrh 		break;
3625595Srrh 	}
3635595Srrh 	return(dest);
3645595Srrh }
3655595Srrh 
3665595Srrh diverterrors(name, dest, files, ix, previewed, nterrors)
3675595Srrh 	char	*name;
3685595Srrh 	int	dest;
3695595Srrh 	Eptr	**files;
3705595Srrh 	int	ix;
3715595Srrh 	boolean	previewed;
3725595Srrh 	int	nterrors;
3735595Srrh {
3745595Srrh 	int	nerrors;
3755595Srrh 	reg	Eptr	*erpp;
3765595Srrh 	reg	Eptr	errorp;
3775595Srrh 
3785595Srrh 	nerrors = files[ix+1] - files[ix];
3795595Srrh 
3805595Srrh 	if (   (nerrors != nterrors)
3815595Srrh 	    && (!previewed) ){
3825595Srrh 		fprintf(stdout, terse
3835595Srrh 			? "Uninserted errors\n"
3845595Srrh 			: ">>Uninserted errors for file \"%s\" follow.\n",
3855595Srrh 			name);
3865595Srrh 	}
3875595Srrh 
3885595Srrh 	EITERATE(erpp, files, ix){
3895595Srrh 		errorp = *erpp;
3905595Srrh 		if (errorp->error_e_class != C_TRUE){
3915595Srrh 			if (previewed || touchstatus == Q_NO)
3925595Srrh 				continue;
3935595Srrh 			errorprint(stdout, errorp, TRUE);
3945595Srrh 			continue;
3955595Srrh 		}
3965595Srrh 		switch (dest){
3975595Srrh 		case TOSTDOUT:
3985595Srrh 			if (previewed || touchstatus == Q_NO)
3995595Srrh 				continue;
4005595Srrh 			errorprint(stdout,errorp, TRUE);
4015595Srrh 			break;
4025595Srrh 		case TOTHEFILE:
4035595Srrh 			insert(errorp->error_line);
4045595Srrh 			text(errorp, FALSE);
4055595Srrh 			break;
4065595Srrh 		}
4075595Srrh 	}
4085595Srrh }
4095595Srrh 
4105595Srrh int oktotouch(filename)
4111460Sroot 	char	*filename;
4121460Sroot {
4131460Sroot 	extern		char	*suffixlist;
4145595Srrh 	reg	char	*src;
4155595Srrh 	reg	char	*pat;
4161460Sroot 			char	*osrc;
4171460Sroot 
4181460Sroot 	pat = suffixlist;
4191460Sroot 	if (pat == 0)
4201460Sroot 		return(0);
4211460Sroot 	if (*pat == '*')
4221460Sroot 		return(1);
4231460Sroot 	while (*pat++ != '.')
4241460Sroot 		continue;
4251460Sroot 	--pat;		/* point to the period */
4261460Sroot 
4271460Sroot 	for (src = &filename[strlen(filename)], --src;
4281460Sroot 	     (src > filename) && (*src != '.'); --src)
4291460Sroot 		continue;
4301460Sroot 	if (*src != '.')
4311460Sroot 		return(0);
4321460Sroot 
4331460Sroot 	for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
4341460Sroot 		for (;   *src			/* not at end of the source */
4351460Sroot 		      && *pat			/* not off end of pattern */
4361460Sroot 		      && *pat != '.'		/* not off end of sub pattern */
4371460Sroot 		      && *pat != '*'		/* not wild card */
4381460Sroot 		      && *src == *pat;		/* and equal... */
4391460Sroot 		      src++, pat++)
4401460Sroot 			continue;
4411460Sroot 		if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
4421460Sroot 			return(1);
4431460Sroot 		if (*src != 0 && *pat == '*')
4441460Sroot 			return(1);
4451460Sroot 		while (*pat && *pat != '.')
4461460Sroot 			pat++;
4471460Sroot 		if (! *pat)
4481460Sroot 			return(0);
4491460Sroot 	}
4501460Sroot 	return(0);
4511460Sroot }
4525595Srrh /*
4535595Srrh  *	Construct an execv argument
4545595Srrh  *	We need 1 argument for the editor's name
4555595Srrh  *	We need 1 argument for the initial search string
4565595Srrh  *	We need n_pissed_on arguments for the file names
4575595Srrh  *	We need 1 argument that is a null for execv.
4585595Srrh  *	The caller fills in the editor's name.
4595595Srrh  *	We fill in the initial search string.
4605595Srrh  *	We fill in the arguments, and the null.
4615595Srrh  */
4625595Srrh execvarg(n_pissed_on, r_argc, r_argv)
4635595Srrh 	int	n_pissed_on;
4645595Srrh 	int	*r_argc;
4655595Srrh 	char	***r_argv;
4665595Srrh {
4675595Srrh 	Eptr	p;
4685595Srrh 	char	*sep;
4695595Srrh 	int	fi;
4701460Sroot 
4715595Srrh 	(*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
4725595Srrh 	(*r_argc) =  n_pissed_on + 2;
4735595Srrh 	(*r_argv)[1] = "+1;/###/";
4745595Srrh 	n_pissed_on = 2;
4755595Srrh 	if (!terse){
4765595Srrh 		fprintf(stdout, "You touched file(s):");
4775595Srrh 		sep = " ";
4785595Srrh 	}
4795595Srrh 	FILEITERATE(fi, 1){
4805595Srrh 		if (!touchedfiles[fi])
4815595Srrh 			continue;
4825595Srrh 		p = *(files[fi]);
4835595Srrh 		if (!terse){
4845595Srrh 			fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
4855595Srrh 			sep = ", ";
4865595Srrh 		}
4875595Srrh 		(*r_argv)[n_pissed_on++] = p->error_text[0];
4885595Srrh 	}
4895595Srrh 	if (!terse)
4905595Srrh 		fprintf(stdout, "\n");
4915595Srrh 	(*r_argv)[n_pissed_on] = 0;
4925595Srrh }
4935595Srrh 
4941460Sroot FILE	*o_touchedfile;	/* the old file */
4951460Sroot FILE	*n_touchedfile;	/* the new file */
4961460Sroot char	*o_name;
4976612Srrh char	n_name[64];
4986612Srrh char	*canon_name = "/tmp/ErrorXXXXXX";
4991460Sroot int	o_lineno;
5001460Sroot int	n_lineno;
5011460Sroot boolean	tempfileopen = FALSE;
5021460Sroot /*
5031460Sroot  *	open the file; guaranteed to be both readable and writable
5041460Sroot  *	Well, if it isn't, then return TRUE if something failed
5051460Sroot  */
5061460Sroot boolean edit(name)
5071460Sroot 	char	*name;
5081460Sroot {
5091460Sroot 	o_name = name;
5101460Sroot 	if ( (o_touchedfile = fopen(name, "r")) == NULL){
5111460Sroot 		fprintf(stderr, "%s: Can't open file \"%s\" to touch (read).\n",
5121460Sroot 			processname, name);
5131460Sroot 		return(TRUE);
5141460Sroot 	}
5155595Srrh 	(void)strcpy(n_name, canon_name);
5165595Srrh 	(void)mktemp(n_name);
5171460Sroot 	if ( (n_touchedfile = fopen(n_name, "w")) == NULL){
5181460Sroot 		fprintf(stderr,"%s: Can't open file \"%s\" to touch (write).\n",
5191460Sroot 			processname, name);
5201460Sroot 		return(TRUE);
5211460Sroot 	}
5221460Sroot 	tempfileopen = TRUE;
5231460Sroot 	n_lineno = 0;
5241460Sroot 	o_lineno = 0;
5251460Sroot 	return(FALSE);
5261460Sroot }
5271460Sroot /*
5281460Sroot  *	Position to the line (before, after) the line given by place
5291460Sroot  */
5305595Srrh char	edbuf[BUFSIZ];
5311460Sroot insert(place)
5321460Sroot 	int	place;
5331460Sroot {
5341460Sroot 	--place;	/* always insert messages before the offending line*/
5351460Sroot 	for(; o_lineno < place; o_lineno++, n_lineno++){
5365595Srrh 		if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
5371460Sroot 			return;
5385595Srrh 		fputs(edbuf, n_touchedfile);
5391460Sroot 	}
5401460Sroot }
5411460Sroot 
5425595Srrh text(p, use_all)
5435595Srrh 	reg	Eptr	p;
5445595Srrh 		boolean	use_all;
5451460Sroot {
5461460Sroot 	int	offset = use_all ? 0 : 2;
5475595Srrh 
5485595Srrh 	fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
5491460Sroot 	fprintf(n_touchedfile, "%d [%s] ",
5505595Srrh 		p->error_line,
5515595Srrh 		lang_table[p->error_language].lang_name);
5525595Srrh 	wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
5535595Srrh 	fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
5541460Sroot 	n_lineno++;
5551460Sroot }
5561460Sroot 
5576612Srrh /*
5586612Srrh  *	write the touched file to its temporary copy,
5596612Srrh  *	then bring the temporary in over the local file
5606612Srrh  */
5616612Srrh writetouched(overwrite)
5626612Srrh 	int	overwrite;
5631460Sroot {
5646612Srrh 	reg	int	nread;
5656612Srrh 	reg	FILE	*localfile;
5666612Srrh 	reg	FILE	*tmpfile;
5676612Srrh 		int	botch;
5689309Srrh 		int	oktorm;
5695595Srrh 
5709309Srrh 	botch = 0;
5719309Srrh 	oktorm = 1;
5725595Srrh 	while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
5739309Srrh 		if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
5749309Srrh 			/*
5759309Srrh 			 *	Catastrophe in temporary area: file system full?
5769309Srrh 			 */
5779309Srrh 			botch = 1;
5789309Srrh 			fprintf(stderr,
5799309Srrh 			  "%s: write failure: No errors inserted in \"%s\"\n",
5809309Srrh 			  processname, o_name);
5819309Srrh 		}
5821460Sroot 	}
5831460Sroot 	fclose(n_touchedfile);
5841460Sroot 	fclose(o_touchedfile);
5856612Srrh 	/*
5866612Srrh 	 *	Now, copy the temp file back over the original
5876612Srrh 	 *	file, thus preserving links, etc
5886612Srrh 	 */
5899309Srrh 	if (botch == 0 && overwrite){
5906612Srrh 		botch = 0;
5916612Srrh 		localfile = NULL;
5926612Srrh 		tmpfile = NULL;
5936612Srrh 		if ((localfile = fopen(o_name, "w")) == NULL){
5946612Srrh 			fprintf(stderr,
5956612Srrh 				"%s: Can't open file \"%s\" to overwrite.\n",
5966612Srrh 				processname, o_name);
5976612Srrh 			botch++;
5986612Srrh 		}
5996612Srrh 		if ((tmpfile = fopen(n_name, "r")) == NULL){
6006612Srrh 			fprintf(stderr, "%s: Can't open file \"%s\" to read.\n",
6016612Srrh 				processname, n_name);
6026612Srrh 			botch++;
6036612Srrh 		}
6049309Srrh 		if (!botch)
6059309Srrh 			oktorm = mustoverwrite(localfile, tmpfile);
6066612Srrh 		if (localfile != NULL)
6076612Srrh 			fclose(localfile);
6086612Srrh 		if (tmpfile != NULL)
6096612Srrh 			fclose(tmpfile);
6106612Srrh 	}
6119309Srrh 	if (oktorm == 0){
612*35266Sbostic 		fprintf(stderr, "%s: Catastrophe: A copy of \"%s\": was saved in \"%s\"\n",
6139309Srrh 			processname, o_name, n_name);
6149309Srrh 		exit(1);
6159309Srrh 	}
6166612Srrh 	/*
6176612Srrh 	 *	Kiss the temp file good bye
6186612Srrh 	 */
6191460Sroot 	unlink(n_name);
6201460Sroot 	tempfileopen = FALSE;
6216612Srrh 	return(TRUE);
6221460Sroot }
6239309Srrh /*
6249309Srrh  *	return 1 if the tmpfile can be removed after writing it out
6259309Srrh  */
6269309Srrh int mustoverwrite(preciousfile, tmpfile)
6279309Srrh 	FILE	*preciousfile;
6289309Srrh 	FILE	*tmpfile;
6299309Srrh {
6309309Srrh 	int	nread;
6315595Srrh 
6329309Srrh 	while((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != NULL){
6339309Srrh 		if (mustwrite(edbuf, nread, preciousfile) == 0)
6349309Srrh 			return(0);
6359309Srrh 	}
6369309Srrh 	return(1);
6379309Srrh }
6389309Srrh /*
6399309Srrh  *	return 0 on catastrophe
6409309Srrh  */
6419309Srrh mustwrite(base, n, preciousfile)
6429309Srrh 	char	*base;
6439309Srrh 	int	n;
6449309Srrh 	FILE	*preciousfile;
6459309Srrh {
6469309Srrh 	int	nwrote;
6479309Srrh 
6489309Srrh 	if (n <= 0)
6499309Srrh 		return(1);
6509309Srrh 	nwrote = fwrite(base, 1, n, preciousfile);
6519309Srrh 	if (nwrote == n)
6529309Srrh 		return(1);
6539309Srrh 	perror(processname);
6549309Srrh 	switch(inquire(terse
6559309Srrh 	    ? "Botch overwriting: retry? "
6569309Srrh 	    : "Botch overwriting the source file: retry? ")){
6579309Srrh 	case Q_YES:
6589309Srrh 	case Q_yes:
6599309Srrh 		mustwrite(base + nwrote, n - nwrote, preciousfile);
6609309Srrh 		return(1);
6619309Srrh 	case Q_NO:
6629309Srrh 	case Q_no:
6639309Srrh 		switch(inquire("Are you sure? ")){
6649309Srrh 		case Q_YES:
6659309Srrh 		case Q_yes:
6669309Srrh 			return(0);
6679309Srrh 		case Q_NO:
6689309Srrh 		case Q_no:
6699309Srrh 			mustwrite(base + nwrote, n - nwrote, preciousfile);
6709309Srrh 			return(1);
6719309Srrh 		}
67216564Sralph 	default:
67316564Sralph 		return(0);
6749309Srrh 	}
6759309Srrh }
6769309Srrh 
6771460Sroot onintr()
6781460Sroot {
6795595Srrh 	switch(inquire(terse
6805595Srrh 	    ? "\nContinue? "
6815595Srrh 	    : "\nInterrupt: Do you want to continue? ")){
6825595Srrh 	case Q_YES:
6835595Srrh 	case Q_yes:
6841460Sroot 		signal(SIGINT, onintr);
6851460Sroot 		return;
6865595Srrh 	default:
6876612Srrh 		if (tempfileopen){
6886612Srrh 			/*
6896612Srrh 			 *	Don't overwrite the original file!
6906612Srrh 			 */
6916612Srrh 			writetouched(0);
6926612Srrh 		}
6935595Srrh 		exit(1);
6941460Sroot 	}
6955595Srrh 	/*NOTREACHED*/
6961460Sroot }
6975595Srrh 
6981460Sroot errorprint(place, errorp, print_all)
6991460Sroot 	FILE	*place;
7005595Srrh 	Eptr	errorp;
7011460Sroot 	boolean	print_all;
7021460Sroot {
7031460Sroot 	int	offset = print_all ? 0 : 2;
7041460Sroot 
7051460Sroot 	if (errorp->error_e_class == C_IGNORE)
7061460Sroot 		return;
7071460Sroot 	fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
7081460Sroot 	wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
7091460Sroot 	putc('\n', place);
7101460Sroot }
7111460Sroot 
7125595Srrh int inquire(fmt, a1, a2)
7131460Sroot 	char	*fmt;
7141460Sroot 	/*VARARGS1*/
7151460Sroot {
7161460Sroot 	char	buffer[128];
71716564Sralph 
71816564Sralph 	if (queryfile == NULL)
71916564Sralph 		return(0);
7201460Sroot 	for(;;){
7211460Sroot 		do{
7221460Sroot 			fflush(stdout);
7231460Sroot 			fprintf(stderr, fmt, a1, a2);
7241460Sroot 			fflush(stderr);
7251460Sroot 		} while (fgets(buffer, 127, queryfile) == NULL);
7265595Srrh 		switch(buffer[0]){
7275595Srrh 		case 'Y':	return(Q_YES);
7285595Srrh 		case 'y':	return(Q_yes);
7295595Srrh 		case 'N':	return(Q_NO);
7305595Srrh 		case 'n':	return(Q_no);
7315595Srrh 		default:	fprintf(stderr, "Yes or No only!\n");
7325595Srrh 		}
7331460Sroot 	}
7341460Sroot }
7351460Sroot 
7365595Srrh int probethisfile(name)
7375595Srrh 	char	*name;
7381460Sroot {
7391460Sroot 	struct stat statbuf;
7405595Srrh 	if (stat(name, &statbuf) < 0)
7415595Srrh 		return(F_NOTEXIST);
7425595Srrh 	if((statbuf.st_mode & S_IREAD) == 0)
7435595Srrh 		return(F_NOTREAD);
7445595Srrh 	if((statbuf.st_mode & S_IWRITE) == 0)
7455595Srrh 		return(F_NOTWRITE);
7465595Srrh 	return(F_TOUCHIT);
7471460Sroot }
748