/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
if (*name == '/') {
name++;
if (level-- == 0)
return name;
}
while (level-- != 0 && (p = strchr(name, '/')) != NULL)
name = p + 1;
return name;
}
void
findfiles(int my_nerrors, Eptr *my_errors, int *r_nfiles, Eptr ***r_files)
{
int my_nfiles;
Eptr **my_files;
const char *name;
int ei;
int fi;
Eptr errorp;
my_nfiles = countfiles(my_errors);
my_files = Calloc(my_nfiles + 3, sizeof (Eptr*));
touchedfiles = Calloc(my_nfiles+3, sizeof(touchedfiles[0]));
/*
* Now, partition off the error messages
* into those that are synchronization, discarded or
* not specific to any file, and those that were
* nulled or true errors.
*/
my_files[0] = &my_errors[0];
ECITERATE(ei, errorp, 0, my_errors, my_nerrors) {
if ( ! (NOTSORTABLE(errorp->error_e_class)))
break;
}
/*
* Now, and partition off all error messages
* for a given file.
*/
my_files[1] = &my_errors[ei];
touchedfiles[0] = false;
touchedfiles[1] = false;
name = "\1";
fi = 1;
ECITERATE(ei, errorp, ei, my_errors, my_nerrors) {
const char *fname = makename(errorp->error_text[0], filelevel);
if (errorp->error_e_class == C_NULLED
|| errorp->error_e_class == C_TRUE) {
if (strcmp(fname, name) != 0) {
name = fname;
touchedfiles[fi] = false;
my_files[fi] = &my_errors[ei];
fi++;
}
}
}
my_files[fi] = &my_errors[my_nerrors];
*r_nfiles = my_nfiles;
*r_files = my_files;
}
static int
countfiles(Eptr *errors)
{
const char *name;
int ei;
Eptr errorp;
int my_nfiles;
my_nfiles = 0;
name = "\1";
ECITERATE(ei, errorp, 0, errors, nerrors) {
if (SORTABLE(errorp->error_e_class)) {
const char *fname = makename(errorp->error_text[0],
filelevel);
if (strcmp(fname, name) != 0) {
my_nfiles++;
name = fname;
}
}
}
return my_nfiles;
}
fprintf(stdout, terse
? "\"%s\" has %d error%s, "
: "\nFile \"%s\" has %d error%s.\n"
, name ,spread ,plural(spread));
/*
* First, iterate through all error messages in this file
* to see how many of the error messages really will
* get inserted into the file.
*/
ntrueerrors = 0;
EITERATE(erpp, my_files, fi) {
errorp = *erpp;
if (errorp->error_e_class == C_TRUE)
ntrueerrors++;
}
fprintf(stdout, terse
? "insert %d\n"
: "\t%d of these errors can be inserted into the file.\n",
ntrueerrors);
pat = suffixlist;
if (pat == 0)
return false;
if (*pat == '*')
return true;
while (*pat++ != '.')
continue;
--pat; /* point to the period */
for (src = &filename[strlen(filename)], --src;
src > filename && *src != '.'; --src)
continue;
if (*src != '.')
return false;
for (src++, pat++, osrc = src;
*src != '\0' && *pat != '\0'; src = osrc, pat++) {
for (; *src != '\0' /* not at end of the source */
&& *pat != '\0' /* not off end of pattern */
&& *pat != '.' /* not off end of sub pattern */
&& *pat != '*' /* not wild card */
&& *src == *pat; /* and equal... */
src++, pat++)
continue;
if (*src == '\0'
&& (*pat == '\0' || *pat == '.' || *pat == '*'))
return true;
if (*src != '\0' && *pat == '*')
return true;
while (*pat != '\0' && *pat != '.')
pat++;
if (*pat == '\0')
return false;
}
return false;
}
/*
* Construct an execv argument
* We need 1 argument for the editor's name
* We need 1 argument for the initial search string
* We need n_pissed_on arguments for the file names
* We need 1 argument that is a null for execv.
* The caller fills in the editor's name.
* We fill in the initial search string.
* We fill in the arguments, and the null.
*/
static void
execvarg(int n_pissed_on, int *r_argc, char ***r_argv)
{
Eptr p;
const char *sep, *name;
int fi;
static FILE *o_touchedfile; /* the old file */
static FILE *n_touchedfile; /* the new file */
static const char *o_name;
static char n_name[MAXPATHLEN];
static int o_lineno;
static int n_lineno;
static bool tempfileopen = false;
/*
* open the file; guaranteed to be both readable and writable
* Well, if it isn't, then return TRUE if something failed
*/
static bool
edit(const char *name)
{
int fd;
const char *tmpdir;
/*
* write the touched file to its temporary copy,
* then bring the temporary in over the local file
*/
static bool
writetouched(bool overwrite)
{
size_t nread;
FILE *localfile;
FILE *temp;
bool botch;
bool oktorm;
botch = false;
oktorm = true;
while ((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != 0) {
if (nread != fwrite(edbuf, 1, nread, n_touchedfile)) {
/*
* Catastrophe in temporary area: file system full?
*/
botch = true;
warn("write failure: No errors inserted in `%s'",
o_name);
}
}
fclose(n_touchedfile);
fclose(o_touchedfile);
/*
* Now, copy the temp file back over the original
* file, thus preserving links, etc
*/
if (!botch && overwrite) {
localfile = NULL;
temp = NULL;
if ((localfile = fopen(o_name, "w")) == NULL) {
warn("Can't open file `%s' to overwrite", o_name);
botch = true;
}
if ((temp = fopen(n_name, "r")) == NULL) {
warn("Can't open file `%s' to read", n_name);
botch = true;
}
if (!botch)
oktorm = mustoverwrite(localfile, temp);
if (localfile != NULL)
fclose(localfile);
if (temp != NULL)
fclose(temp);
}
if (!oktorm)
errx(1, "Catastrophe: A copy of `%s': was saved in `%s'",
o_name, n_name);
/*
* Kiss the temp file good bye
*/
unlink(n_name);
tempfileopen = false;
return true;
}
/*
* return whether the tmpfile can be removed after writing it out
*/
static bool
mustoverwrite(FILE *preciousfile, FILE *temp)
{
size_t nread;
while ((nread = fread(edbuf, 1, sizeof(edbuf), temp)) != 0) {
if (!mustwrite(edbuf, nread, preciousfile))
return false;
}
return true;
}
/*
* return false on catastrophe
*/
static bool
mustwrite(const char *base, size_t n, FILE *preciousfile)
{
size_t nwrote;
if (n == 0)
return true;
nwrote = fwrite(base, 1, n, preciousfile);
if (nwrote == n)
return true;
warn("write failed");
switch (inquire(terse
? "Botch overwriting: retry? "
: "Botch overwriting the source file: retry? ")) {
case Q_YES:
case Q_yes:
mustwrite(base + nwrote, n - nwrote, preciousfile);
return true;
case Q_NO:
case Q_no:
switch (inquire("Are you sure? ")) {
case Q_error:
case Q_YES:
case Q_yes:
return false;
case Q_NO:
case Q_no:
mustwrite(base + nwrote, n - nwrote, preciousfile);
return true;
default:
abort();
}
/* FALLTHROUGH */
case Q_error:
default:
return false;
}
}
void
onintr(int sig)
{
switch (inquire(terse
? "\nContinue? "
: "\nInterrupt: Do you want to continue? ")) {
case Q_YES:
case Q_yes:
signal(sig, onintr);
return;
case Q_error:
default:
if (tempfileopen) {
/*
* Don't overwrite the original file!
*/
writetouched(false);
}
(void)raise_default_signal(sig);
_exit(127);
}
/*NOTREACHED*/
}