2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
38 #include <sys/types.h>
47 #include "pathnames.h"
50 * Iterate through errors
52 #define EITERATE(p, fv, i) for (p = fv[i]; p < fv[i+1]; p++)
53 #define ECITERATE(ei, p, lb) for (ei = lb; p = errors[ei],ei < nerrors; ei++)
55 #define FILEITERATE(fi, lb) for (fi = lb; fi <= nfiles; fi++)
56 int touchstatus = Q_YES;
58 findfiles(nerrors, errors, r_nfiles, r_files)
72 nfiles = countfiles(errors);
74 files = (Eptr**)Calloc(nfiles + 3, sizeof (Eptr*));
75 touchedfiles = (boolean *)Calloc(nfiles+3, sizeof(boolean));
77 * Now, partition off the error messages
78 * into those that are synchronization, discarded or
79 * not specific to any file, and those that were
80 * nulled or true errors.
82 files[0] = &errors[0];
83 ECITERATE(ei, errorp, 0){
84 if ( ! (NOTSORTABLE(errorp->error_e_class)))
88 * Now, and partition off all error messages
91 files[1] = &errors[ei];
92 touchedfiles[0] = touchedfiles[1] = FALSE;
95 ECITERATE(ei, errorp, ei){
96 if ( (errorp->error_e_class == C_NULLED)
97 || (errorp->error_e_class == C_TRUE) ){
98 if (strcmp(errorp->error_text[0], name) != 0){
99 name = errorp->error_text[0];
100 touchedfiles[fi] = FALSE;
101 files[fi] = &errors[ei];
106 files[fi] = &errors[nerrors];
111 int countfiles(errors)
121 ECITERATE(ei, errorp, 0){
122 if (SORTABLE(errorp->error_e_class)){
123 if (strcmp(errorp->error_text[0],name) != 0){
125 name = errorp->error_text[0];
131 char *class_table[] = {
132 /*C_UNKNOWN 0 */ "Unknown",
133 /*C_IGNORE 1 */ "ignore",
134 /*C_SYNC 2 */ "synchronization",
135 /*C_DISCARD 3 */ "discarded",
136 /*C_NONSPEC 4 */ "non specific",
137 /*C_THISFILE 5 */ "specific to this file",
138 /*C_NULLED 6 */ "nulled",
139 /*C_TRUE 7 */ "true",
140 /*C_DUPL 8 */ "duplicated"
143 int class_count[C_LAST - C_FIRST] = {0};
145 filenames(nfiles, files)
151 extern char *class_table[];
155 * first, simply dump out errors that
156 * don't pertain to any file
158 someerrors = nopertain(files);
162 fprintf(stdout, terse
164 : "%d file%s contain%s errors",
165 nfiles, plural(nfiles), verbform(nfiles));
168 fprintf(stdout, "%s\"%s\" (%d)",
169 sep, (*files[fi])->error_text[0],
170 files[fi+1] - files[fi]);
174 fprintf(stdout, "\n");
177 fprintf(stdout, "No errors.\n");
181 * Dump out errors that don't pertain to any file
191 if (files[1] - files[0] <= 0)
193 for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
194 if (class_count[type] <= 0)
199 fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
200 class_count[type], class_table[type]);
202 fprintf(stdout, "\n\t%d %s errors follow\n",
203 class_count[type], class_table[type]);
204 EITERATE(erpp, files, 0){
206 if (errorp->error_e_class == type){
207 errorprint(stdout, errorp, TRUE);
215 extern boolean notouch;
217 boolean touchfiles(nfiles, files, r_edargc, r_edargv)
229 int n_pissed_on; /* # of file touched*/
233 name = (*files[fi])->error_text[0];
234 spread = files[fi+1] - files[fi];
235 fprintf(stdout, terse
236 ? "\"%s\" has %d error%s, "
237 : "\nFile \"%s\" has %d error%s.\n"
238 , name ,spread ,plural(spread));
240 * First, iterate through all error messages in this file
241 * to see how many of the error messages really will
242 * get inserted into the file.
245 EITERATE(erpp, files, fi){
247 if (errorp->error_e_class == C_TRUE)
250 fprintf(stdout, terse
252 : "\t%d of these errors can be inserted into the file.\n",
255 hackfile(name, files, fi, ntrueerrors);
260 scribbled |= touchedfiles[fi];
265 * Construct an execv argument
267 execvarg(n_pissed_on, r_edargc, r_edargv);
271 fprintf(stdout, "You didn't touch any files.\n");
276 hackfile(name, files, ix, nerrors)
282 int errordest; /* where errors go*/
284 if (!oktotouch(name)) {
286 errordest = TOSTDOUT;
288 previewed = preview(name, nerrors, files, ix);
289 errordest = settotouch(name);
292 if (errordest != TOSTDOUT)
293 touchedfiles[ix] = TRUE;
295 if (previewed && (errordest == TOSTDOUT))
298 diverterrors(name, errordest, files, ix, previewed, nerrors);
300 if (errordest == TOTHEFILE){
302 * overwrite the original file
308 boolean preview(name, nerrors, files, ix)
323 : "Do you want to preview the errors first? ")){
327 EITERATE(erpp, files, ix){
328 errorprint(stdout, *erpp, TRUE);
331 fprintf(stdout, "\n");
345 switch(touchstatus = inquire(terse
347 : "Do you want to touch file \"%s\"? ",
357 switch(probethisfile(name)){
360 fprintf(stdout, terse
361 ? "\"%s\" unreadable\n"
362 : "File \"%s\" is unreadable\n",
367 fprintf(stdout, terse
368 ? "\"%s\" unwritable\n"
369 : "File \"%s\" is unwritable\n",
374 fprintf(stdout, terse
375 ? "\"%s\" not found\n"
376 : "Can't find file \"%s\" to insert error messages into.\n",
380 dest = edit(name) ? TOSTDOUT : TOTHEFILE;
386 diverterrors(name, dest, files, ix, previewed, nterrors)
398 nerrors = files[ix+1] - files[ix];
400 if ( (nerrors != nterrors)
402 fprintf(stdout, terse
403 ? "Uninserted errors\n"
404 : ">>Uninserted errors for file \"%s\" follow.\n",
408 EITERATE(erpp, files, ix){
410 if (errorp->error_e_class != C_TRUE){
411 if (previewed || touchstatus == Q_NO)
413 errorprint(stdout, errorp, TRUE);
418 if (previewed || touchstatus == Q_NO)
420 errorprint(stdout,errorp, TRUE);
423 insert(errorp->error_line);
430 int oktotouch(filename)
433 extern char *suffixlist;
443 while (*pat++ != '.')
445 --pat; /* point to the period */
447 for (src = &filename[strlen(filename)], --src;
448 (src > filename) && (*src != '.'); --src)
453 for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
454 for (; *src /* not at end of the source */
455 && *pat /* not off end of pattern */
456 && *pat != '.' /* not off end of sub pattern */
457 && *pat != '*' /* not wild card */
458 && *src == *pat; /* and equal... */
461 if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
463 if (*src != 0 && *pat == '*')
465 while (*pat && *pat != '.')
473 * Construct an execv argument
474 * We need 1 argument for the editor's name
475 * We need 1 argument for the initial search string
476 * We need n_pissed_on arguments for the file names
477 * We need 1 argument that is a null for execv.
478 * The caller fills in the editor's name.
479 * We fill in the initial search string.
480 * We fill in the arguments, and the null.
482 execvarg(n_pissed_on, r_argc, r_argv)
491 (*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
492 (*r_argc) = n_pissed_on + 2;
493 (*r_argv)[1] = "+1;/###/";
496 fprintf(stdout, "You touched file(s):");
500 if (!touchedfiles[fi])
504 fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
507 (*r_argv)[n_pissed_on++] = p->error_text[0];
510 fprintf(stdout, "\n");
511 (*r_argv)[n_pissed_on] = 0;
514 FILE *o_touchedfile; /* the old file */
515 FILE *n_touchedfile; /* the new file */
518 char *canon_name = _PATH_TMP;
521 boolean tempfileopen = FALSE;
523 * open the file; guaranteed to be both readable and writable
524 * Well, if it isn't, then return TRUE if something failed
530 if ( (o_touchedfile = fopen(name, "r")) == NULL){
531 fprintf(stderr, "%s: Can't open file \"%s\" to touch (read).\n",
535 (void)strcpy(n_name, canon_name);
536 (void)mktemp(n_name);
537 if ( (n_touchedfile = fopen(n_name, "w")) == NULL){
538 fprintf(stderr,"%s: Can't open file \"%s\" to touch (write).\n",
548 * Position to the line (before, after) the line given by place
554 --place; /* always insert messages before the offending line*/
555 for(; o_lineno < place; o_lineno++, n_lineno++){
556 if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
558 fputs(edbuf, n_touchedfile);
566 int offset = use_all ? 0 : 2;
568 fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
569 fprintf(n_touchedfile, "%d [%s] ",
571 lang_table[p->error_language].lang_name);
572 wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
573 fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
578 * write the touched file to its temporary copy,
579 * then bring the temporary in over the local file
581 writetouched(overwrite)
592 while((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != NULL){
593 if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
595 * Catastrophe in temporary area: file system full?
599 "%s: write failure: No errors inserted in \"%s\"\n",
600 processname, o_name);
603 fclose(n_touchedfile);
604 fclose(o_touchedfile);
606 * Now, copy the temp file back over the original
607 * file, thus preserving links, etc
609 if (botch == 0 && overwrite){
613 if ((localfile = fopen(o_name, "w")) == NULL){
615 "%s: Can't open file \"%s\" to overwrite.\n",
616 processname, o_name);
619 if ((tmpfile = fopen(n_name, "r")) == NULL){
620 fprintf(stderr, "%s: Can't open file \"%s\" to read.\n",
621 processname, n_name);
625 oktorm = mustoverwrite(localfile, tmpfile);
626 if (localfile != NULL)
632 fprintf(stderr, "%s: Catastrophe: A copy of \"%s\": was saved in \"%s\"\n",
633 processname, o_name, n_name);
637 * Kiss the temp file good bye
640 tempfileopen = FALSE;
644 * return 1 if the tmpfile can be removed after writing it out
646 int mustoverwrite(preciousfile, tmpfile)
652 while((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != NULL){
653 if (mustwrite(edbuf, nread, preciousfile) == 0)
659 * return 0 on catastrophe
661 mustwrite(base, n, preciousfile)
670 nwrote = fwrite(base, 1, n, preciousfile);
675 ? "Botch overwriting: retry? "
676 : "Botch overwriting the source file: retry? ")){
679 mustwrite(base + nwrote, n - nwrote, preciousfile);
683 switch(inquire("Are you sure? ")){
689 mustwrite(base + nwrote, n - nwrote, preciousfile);
702 : "\nInterrupt: Do you want to continue? ")){
705 signal(SIGINT, onintr);
710 * Don't overwrite the original file!
719 errorprint(place, errorp, print_all)
724 int offset = print_all ? 0 : 2;
726 if (errorp->error_e_class == C_IGNORE)
728 fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
729 wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
733 int inquire(fmt, a1, a2)
739 if (queryfile == NULL)
744 fprintf(stderr, fmt, a1, a2);
746 } while (fgets(buffer, 127, queryfile) == NULL);
748 case 'Y': return(Q_YES);
749 case 'y': return(Q_yes);
750 case 'N': return(Q_NO);
751 case 'n': return(Q_no);
752 default: fprintf(stderr, "Yes or No only!\n");
757 int probethisfile(name)
761 if (stat(name, &statbuf) < 0)
763 if((statbuf.st_mode & S_IREAD) == 0)
765 if((statbuf.st_mode & S_IWRITE) == 0)