touch.c revision 1.8.2.1 1 1.8.2.1 he /* $NetBSD: touch.c,v 1.8.2.1 2000/10/19 16:31:16 he 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.8.2.1 he __RCSID("$NetBSD: touch.c,v 1.8.2.1 2000/10/19 16:31:16 he Exp $");
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/types.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.8.2.1 he if (terse)
177 1.8.2.1 he fprintf(stdout, "%d file%s", nfiles, plural(nfiles));
178 1.8.2.1 he else
179 1.8.2.1 he fprintf(stdout, "%d file%s contain%s errors",
180 1.8.2.1 he nfiles, plural(nfiles), verbform(nfiles));
181 1.1 cgd if (!terse){
182 1.1 cgd FILEITERATE(fi, 1){
183 1.1 cgd fprintf(stdout, "%s\"%s\" (%d)",
184 1.1 cgd sep, (*files[fi])->error_text[0],
185 1.7 mrg (int)(files[fi+1] - files[fi]));
186 1.1 cgd sep = ", ";
187 1.1 cgd }
188 1.1 cgd }
189 1.1 cgd fprintf(stdout, "\n");
190 1.1 cgd }
191 1.1 cgd if (!someerrors)
192 1.1 cgd fprintf(stdout, "No errors.\n");
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd /*
196 1.1 cgd * Dump out errors that don't pertain to any file
197 1.1 cgd */
198 1.6 lukem int
199 1.6 lukem nopertain(files)
200 1.1 cgd Eptr **files;
201 1.1 cgd {
202 1.1 cgd int type;
203 1.1 cgd int someerrors = 0;
204 1.6 lukem Eptr *erpp;
205 1.6 lukem Eptr errorp;
206 1.1 cgd
207 1.1 cgd if (files[1] - files[0] <= 0)
208 1.1 cgd return(0);
209 1.1 cgd for(type = C_UNKNOWN; NOTSORTABLE(type); type++){
210 1.1 cgd if (class_count[type] <= 0)
211 1.1 cgd continue;
212 1.1 cgd if (type > C_SYNC)
213 1.1 cgd someerrors++;
214 1.1 cgd if (terse){
215 1.1 cgd fprintf(stdout, "\t%d %s errors NOT PRINTED\n",
216 1.1 cgd class_count[type], class_table[type]);
217 1.1 cgd } else {
218 1.1 cgd fprintf(stdout, "\n\t%d %s errors follow\n",
219 1.1 cgd class_count[type], class_table[type]);
220 1.1 cgd EITERATE(erpp, files, 0){
221 1.1 cgd errorp = *erpp;
222 1.1 cgd if (errorp->error_e_class == type){
223 1.1 cgd errorprint(stdout, errorp, TRUE);
224 1.1 cgd }
225 1.1 cgd }
226 1.1 cgd }
227 1.1 cgd }
228 1.1 cgd return(someerrors);
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd extern boolean notouch;
232 1.1 cgd
233 1.6 lukem boolean
234 1.6 lukem touchfiles(nfiles, files, r_edargc, r_edargv)
235 1.1 cgd int nfiles;
236 1.1 cgd Eptr **files;
237 1.1 cgd int *r_edargc;
238 1.1 cgd char ***r_edargv;
239 1.1 cgd {
240 1.6 lukem char *name;
241 1.6 lukem Eptr errorp;
242 1.6 lukem int fi;
243 1.6 lukem Eptr *erpp;
244 1.6 lukem int ntrueerrors;
245 1.6 lukem boolean scribbled;
246 1.6 lukem int n_pissed_on; /* # of file touched*/
247 1.6 lukem int spread;
248 1.1 cgd
249 1.1 cgd FILEITERATE(fi, 1){
250 1.1 cgd name = (*files[fi])->error_text[0];
251 1.1 cgd spread = files[fi+1] - files[fi];
252 1.1 cgd fprintf(stdout, terse
253 1.1 cgd ? "\"%s\" has %d error%s, "
254 1.1 cgd : "\nFile \"%s\" has %d error%s.\n"
255 1.1 cgd , name ,spread ,plural(spread));
256 1.1 cgd /*
257 1.1 cgd * First, iterate through all error messages in this file
258 1.1 cgd * to see how many of the error messages really will
259 1.1 cgd * get inserted into the file.
260 1.1 cgd */
261 1.1 cgd ntrueerrors = 0;
262 1.1 cgd EITERATE(erpp, files, fi){
263 1.1 cgd errorp = *erpp;
264 1.1 cgd if (errorp->error_e_class == C_TRUE)
265 1.1 cgd ntrueerrors++;
266 1.1 cgd }
267 1.1 cgd fprintf(stdout, terse
268 1.1 cgd ? "insert %d\n"
269 1.1 cgd : "\t%d of these errors can be inserted into the file.\n",
270 1.1 cgd ntrueerrors);
271 1.1 cgd
272 1.1 cgd hackfile(name, files, fi, ntrueerrors);
273 1.1 cgd }
274 1.1 cgd scribbled = FALSE;
275 1.1 cgd n_pissed_on = 0;
276 1.1 cgd FILEITERATE(fi, 1){
277 1.1 cgd scribbled |= touchedfiles[fi];
278 1.1 cgd n_pissed_on++;
279 1.1 cgd }
280 1.1 cgd if (scribbled){
281 1.1 cgd /*
282 1.1 cgd * Construct an execv argument
283 1.1 cgd */
284 1.1 cgd execvarg(n_pissed_on, r_edargc, r_edargv);
285 1.1 cgd return(TRUE);
286 1.1 cgd } else {
287 1.1 cgd if (!terse)
288 1.1 cgd fprintf(stdout, "You didn't touch any files.\n");
289 1.1 cgd return(FALSE);
290 1.1 cgd }
291 1.1 cgd }
292 1.1 cgd
293 1.6 lukem void
294 1.1 cgd hackfile(name, files, ix, nerrors)
295 1.1 cgd char *name;
296 1.1 cgd Eptr **files;
297 1.1 cgd int ix;
298 1.6 lukem int nerrors;
299 1.1 cgd {
300 1.1 cgd boolean previewed;
301 1.1 cgd int errordest; /* where errors go*/
302 1.1 cgd
303 1.1 cgd if (!oktotouch(name)) {
304 1.1 cgd previewed = FALSE;
305 1.1 cgd errordest = TOSTDOUT;
306 1.1 cgd } else {
307 1.1 cgd previewed = preview(name, nerrors, files, ix);
308 1.1 cgd errordest = settotouch(name);
309 1.1 cgd }
310 1.1 cgd
311 1.1 cgd if (errordest != TOSTDOUT)
312 1.1 cgd touchedfiles[ix] = TRUE;
313 1.1 cgd
314 1.1 cgd if (previewed && (errordest == TOSTDOUT))
315 1.1 cgd return;
316 1.1 cgd
317 1.1 cgd diverterrors(name, errordest, files, ix, previewed, nerrors);
318 1.1 cgd
319 1.1 cgd if (errordest == TOTHEFILE){
320 1.1 cgd /*
321 1.1 cgd * overwrite the original file
322 1.1 cgd */
323 1.1 cgd writetouched(1);
324 1.1 cgd }
325 1.1 cgd }
326 1.1 cgd
327 1.6 lukem boolean
328 1.6 lukem preview(name, nerrors, files, ix)
329 1.1 cgd char *name;
330 1.1 cgd int nerrors;
331 1.1 cgd Eptr **files;
332 1.1 cgd int ix;
333 1.1 cgd {
334 1.1 cgd int back;
335 1.6 lukem Eptr *erpp;
336 1.1 cgd
337 1.1 cgd if (nerrors <= 0)
338 1.1 cgd return(FALSE);
339 1.1 cgd back = FALSE;
340 1.1 cgd if(query){
341 1.1 cgd switch(inquire(terse
342 1.1 cgd ? "Preview? "
343 1.1 cgd : "Do you want to preview the errors first? ")){
344 1.1 cgd case Q_YES:
345 1.1 cgd case Q_yes:
346 1.1 cgd back = TRUE;
347 1.1 cgd EITERATE(erpp, files, ix){
348 1.1 cgd errorprint(stdout, *erpp, TRUE);
349 1.1 cgd }
350 1.1 cgd if (!terse)
351 1.1 cgd fprintf(stdout, "\n");
352 1.1 cgd default:
353 1.1 cgd break;
354 1.1 cgd }
355 1.1 cgd }
356 1.1 cgd return(back);
357 1.1 cgd }
358 1.1 cgd
359 1.6 lukem int
360 1.6 lukem settotouch(name)
361 1.1 cgd char *name;
362 1.1 cgd {
363 1.1 cgd int dest = TOSTDOUT;
364 1.1 cgd
365 1.1 cgd if (query){
366 1.1 cgd switch(touchstatus = inquire(terse
367 1.1 cgd ? "Touch? "
368 1.1 cgd : "Do you want to touch file \"%s\"? ",
369 1.1 cgd name)){
370 1.1 cgd case Q_NO:
371 1.1 cgd case Q_no:
372 1.1 cgd return(dest);
373 1.1 cgd default:
374 1.1 cgd break;
375 1.1 cgd }
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd switch(probethisfile(name)){
379 1.1 cgd case F_NOTREAD:
380 1.1 cgd dest = TOSTDOUT;
381 1.1 cgd fprintf(stdout, terse
382 1.1 cgd ? "\"%s\" unreadable\n"
383 1.1 cgd : "File \"%s\" is unreadable\n",
384 1.1 cgd name);
385 1.1 cgd break;
386 1.1 cgd case F_NOTWRITE:
387 1.1 cgd dest = TOSTDOUT;
388 1.1 cgd fprintf(stdout, terse
389 1.1 cgd ? "\"%s\" unwritable\n"
390 1.1 cgd : "File \"%s\" is unwritable\n",
391 1.1 cgd name);
392 1.1 cgd break;
393 1.1 cgd case F_NOTEXIST:
394 1.1 cgd dest = TOSTDOUT;
395 1.1 cgd fprintf(stdout, terse
396 1.1 cgd ? "\"%s\" not found\n"
397 1.1 cgd : "Can't find file \"%s\" to insert error messages into.\n",
398 1.1 cgd name);
399 1.1 cgd break;
400 1.1 cgd default:
401 1.1 cgd dest = edit(name) ? TOSTDOUT : TOTHEFILE;
402 1.1 cgd break;
403 1.1 cgd }
404 1.1 cgd return(dest);
405 1.1 cgd }
406 1.1 cgd
407 1.6 lukem void
408 1.1 cgd diverterrors(name, dest, files, ix, previewed, nterrors)
409 1.1 cgd char *name;
410 1.1 cgd int dest;
411 1.1 cgd Eptr **files;
412 1.1 cgd int ix;
413 1.1 cgd boolean previewed;
414 1.1 cgd int nterrors;
415 1.1 cgd {
416 1.1 cgd int nerrors;
417 1.6 lukem Eptr *erpp;
418 1.6 lukem Eptr errorp;
419 1.1 cgd
420 1.1 cgd nerrors = files[ix+1] - files[ix];
421 1.1 cgd
422 1.1 cgd if ( (nerrors != nterrors)
423 1.1 cgd && (!previewed) ){
424 1.1 cgd fprintf(stdout, terse
425 1.1 cgd ? "Uninserted errors\n"
426 1.1 cgd : ">>Uninserted errors for file \"%s\" follow.\n",
427 1.1 cgd name);
428 1.1 cgd }
429 1.1 cgd
430 1.1 cgd EITERATE(erpp, files, ix){
431 1.1 cgd errorp = *erpp;
432 1.1 cgd if (errorp->error_e_class != C_TRUE){
433 1.1 cgd if (previewed || touchstatus == Q_NO)
434 1.1 cgd continue;
435 1.1 cgd errorprint(stdout, errorp, TRUE);
436 1.1 cgd continue;
437 1.1 cgd }
438 1.1 cgd switch (dest){
439 1.1 cgd case TOSTDOUT:
440 1.1 cgd if (previewed || touchstatus == Q_NO)
441 1.1 cgd continue;
442 1.1 cgd errorprint(stdout,errorp, TRUE);
443 1.1 cgd break;
444 1.1 cgd case TOTHEFILE:
445 1.1 cgd insert(errorp->error_line);
446 1.1 cgd text(errorp, FALSE);
447 1.1 cgd break;
448 1.1 cgd }
449 1.1 cgd }
450 1.1 cgd }
451 1.1 cgd
452 1.6 lukem int
453 1.6 lukem oktotouch(filename)
454 1.1 cgd char *filename;
455 1.1 cgd {
456 1.6 lukem extern char *suffixlist;
457 1.6 lukem char *src;
458 1.6 lukem char *pat;
459 1.6 lukem char *osrc;
460 1.1 cgd
461 1.1 cgd pat = suffixlist;
462 1.1 cgd if (pat == 0)
463 1.1 cgd return(0);
464 1.1 cgd if (*pat == '*')
465 1.1 cgd return(1);
466 1.1 cgd while (*pat++ != '.')
467 1.1 cgd continue;
468 1.1 cgd --pat; /* point to the period */
469 1.1 cgd
470 1.1 cgd for (src = &filename[strlen(filename)], --src;
471 1.1 cgd (src > filename) && (*src != '.'); --src)
472 1.1 cgd continue;
473 1.1 cgd if (*src != '.')
474 1.1 cgd return(0);
475 1.1 cgd
476 1.1 cgd for (src++, pat++, osrc = src; *src && *pat; src = osrc, pat++){
477 1.1 cgd for (; *src /* not at end of the source */
478 1.1 cgd && *pat /* not off end of pattern */
479 1.1 cgd && *pat != '.' /* not off end of sub pattern */
480 1.1 cgd && *pat != '*' /* not wild card */
481 1.1 cgd && *src == *pat; /* and equal... */
482 1.1 cgd src++, pat++)
483 1.1 cgd continue;
484 1.1 cgd if (*src == 0 && (*pat == 0 || *pat == '.' || *pat == '*'))
485 1.1 cgd return(1);
486 1.1 cgd if (*src != 0 && *pat == '*')
487 1.1 cgd return(1);
488 1.1 cgd while (*pat && *pat != '.')
489 1.1 cgd pat++;
490 1.1 cgd if (! *pat)
491 1.1 cgd return(0);
492 1.1 cgd }
493 1.1 cgd return(0);
494 1.1 cgd }
495 1.6 lukem
496 1.1 cgd /*
497 1.1 cgd * Construct an execv argument
498 1.1 cgd * We need 1 argument for the editor's name
499 1.1 cgd * We need 1 argument for the initial search string
500 1.1 cgd * We need n_pissed_on arguments for the file names
501 1.1 cgd * We need 1 argument that is a null for execv.
502 1.1 cgd * The caller fills in the editor's name.
503 1.1 cgd * We fill in the initial search string.
504 1.1 cgd * We fill in the arguments, and the null.
505 1.1 cgd */
506 1.6 lukem void
507 1.1 cgd execvarg(n_pissed_on, r_argc, r_argv)
508 1.1 cgd int n_pissed_on;
509 1.1 cgd int *r_argc;
510 1.1 cgd char ***r_argv;
511 1.1 cgd {
512 1.1 cgd Eptr p;
513 1.1 cgd char *sep;
514 1.1 cgd int fi;
515 1.1 cgd
516 1.6 lukem sep = NULL;
517 1.1 cgd (*r_argv) = (char **)Calloc(n_pissed_on + 3, sizeof(char *));
518 1.1 cgd (*r_argc) = n_pissed_on + 2;
519 1.1 cgd (*r_argv)[1] = "+1;/###/";
520 1.1 cgd n_pissed_on = 2;
521 1.1 cgd if (!terse){
522 1.1 cgd fprintf(stdout, "You touched file(s):");
523 1.1 cgd sep = " ";
524 1.1 cgd }
525 1.1 cgd FILEITERATE(fi, 1){
526 1.1 cgd if (!touchedfiles[fi])
527 1.1 cgd continue;
528 1.1 cgd p = *(files[fi]);
529 1.1 cgd if (!terse){
530 1.1 cgd fprintf(stdout,"%s\"%s\"", sep, p->error_text[0]);
531 1.1 cgd sep = ", ";
532 1.1 cgd }
533 1.1 cgd (*r_argv)[n_pissed_on++] = p->error_text[0];
534 1.1 cgd }
535 1.1 cgd if (!terse)
536 1.1 cgd fprintf(stdout, "\n");
537 1.1 cgd (*r_argv)[n_pissed_on] = 0;
538 1.1 cgd }
539 1.1 cgd
540 1.1 cgd FILE *o_touchedfile; /* the old file */
541 1.1 cgd FILE *n_touchedfile; /* the new file */
542 1.1 cgd char *o_name;
543 1.1 cgd char n_name[64];
544 1.1 cgd char *canon_name = _PATH_TMP;
545 1.1 cgd int o_lineno;
546 1.1 cgd int n_lineno;
547 1.1 cgd boolean tempfileopen = FALSE;
548 1.1 cgd /*
549 1.1 cgd * open the file; guaranteed to be both readable and writable
550 1.1 cgd * Well, if it isn't, then return TRUE if something failed
551 1.1 cgd */
552 1.6 lukem boolean
553 1.6 lukem edit(name)
554 1.1 cgd char *name;
555 1.1 cgd {
556 1.4 lukem int fd;
557 1.4 lukem
558 1.1 cgd o_name = name;
559 1.1 cgd if ( (o_touchedfile = fopen(name, "r")) == NULL){
560 1.1 cgd fprintf(stderr, "%s: Can't open file \"%s\" to touch (read).\n",
561 1.1 cgd processname, name);
562 1.1 cgd return(TRUE);
563 1.1 cgd }
564 1.1 cgd (void)strcpy(n_name, canon_name);
565 1.4 lukem fd = -1;
566 1.4 lukem if ((fd = mkstemp(n_name)) == -1 ||
567 1.4 lukem (n_touchedfile = fdopen(fd, "w")) == NULL) {
568 1.4 lukem if (fd != -1)
569 1.4 lukem close(fd);
570 1.1 cgd fprintf(stderr,"%s: Can't open file \"%s\" to touch (write).\n",
571 1.1 cgd processname, name);
572 1.1 cgd return(TRUE);
573 1.1 cgd }
574 1.1 cgd tempfileopen = TRUE;
575 1.1 cgd n_lineno = 0;
576 1.1 cgd o_lineno = 0;
577 1.1 cgd return(FALSE);
578 1.1 cgd }
579 1.6 lukem
580 1.1 cgd /*
581 1.1 cgd * Position to the line (before, after) the line given by place
582 1.1 cgd */
583 1.1 cgd char edbuf[BUFSIZ];
584 1.6 lukem
585 1.6 lukem void
586 1.1 cgd insert(place)
587 1.1 cgd int place;
588 1.1 cgd {
589 1.1 cgd --place; /* always insert messages before the offending line*/
590 1.1 cgd for(; o_lineno < place; o_lineno++, n_lineno++){
591 1.1 cgd if(fgets(edbuf, BUFSIZ, o_touchedfile) == NULL)
592 1.1 cgd return;
593 1.1 cgd fputs(edbuf, n_touchedfile);
594 1.1 cgd }
595 1.1 cgd }
596 1.1 cgd
597 1.6 lukem void
598 1.1 cgd text(p, use_all)
599 1.6 lukem Eptr p;
600 1.6 lukem boolean use_all;
601 1.1 cgd {
602 1.1 cgd int offset = use_all ? 0 : 2;
603 1.1 cgd
604 1.1 cgd fputs(lang_table[p->error_language].lang_incomment, n_touchedfile);
605 1.1 cgd fprintf(n_touchedfile, "%d [%s] ",
606 1.1 cgd p->error_line,
607 1.1 cgd lang_table[p->error_language].lang_name);
608 1.1 cgd wordvprint(n_touchedfile, p->error_lgtext-offset, p->error_text+offset);
609 1.1 cgd fputs(lang_table[p->error_language].lang_outcomment,n_touchedfile);
610 1.1 cgd n_lineno++;
611 1.1 cgd }
612 1.1 cgd
613 1.1 cgd /*
614 1.1 cgd * write the touched file to its temporary copy,
615 1.1 cgd * then bring the temporary in over the local file
616 1.1 cgd */
617 1.6 lukem boolean
618 1.1 cgd writetouched(overwrite)
619 1.1 cgd int overwrite;
620 1.1 cgd {
621 1.6 lukem int nread;
622 1.6 lukem FILE *localfile;
623 1.6 lukem FILE *tmpfile;
624 1.6 lukem int botch;
625 1.6 lukem int oktorm;
626 1.1 cgd
627 1.1 cgd botch = 0;
628 1.1 cgd oktorm = 1;
629 1.5 pk while ((nread = fread(edbuf, 1, sizeof(edbuf), o_touchedfile)) != 0) {
630 1.1 cgd if (nread != fwrite(edbuf, 1, nread, n_touchedfile)){
631 1.1 cgd /*
632 1.1 cgd * Catastrophe in temporary area: file system full?
633 1.1 cgd */
634 1.1 cgd botch = 1;
635 1.1 cgd fprintf(stderr,
636 1.1 cgd "%s: write failure: No errors inserted in \"%s\"\n",
637 1.1 cgd processname, o_name);
638 1.1 cgd }
639 1.1 cgd }
640 1.1 cgd fclose(n_touchedfile);
641 1.1 cgd fclose(o_touchedfile);
642 1.1 cgd /*
643 1.1 cgd * Now, copy the temp file back over the original
644 1.1 cgd * file, thus preserving links, etc
645 1.1 cgd */
646 1.1 cgd if (botch == 0 && overwrite){
647 1.1 cgd botch = 0;
648 1.1 cgd localfile = NULL;
649 1.1 cgd tmpfile = NULL;
650 1.1 cgd if ((localfile = fopen(o_name, "w")) == NULL){
651 1.1 cgd fprintf(stderr,
652 1.1 cgd "%s: Can't open file \"%s\" to overwrite.\n",
653 1.1 cgd processname, o_name);
654 1.1 cgd botch++;
655 1.1 cgd }
656 1.1 cgd if ((tmpfile = fopen(n_name, "r")) == NULL){
657 1.1 cgd fprintf(stderr, "%s: Can't open file \"%s\" to read.\n",
658 1.1 cgd processname, n_name);
659 1.1 cgd botch++;
660 1.1 cgd }
661 1.1 cgd if (!botch)
662 1.1 cgd oktorm = mustoverwrite(localfile, tmpfile);
663 1.1 cgd if (localfile != NULL)
664 1.1 cgd fclose(localfile);
665 1.1 cgd if (tmpfile != NULL)
666 1.1 cgd fclose(tmpfile);
667 1.1 cgd }
668 1.1 cgd if (oktorm == 0){
669 1.1 cgd fprintf(stderr, "%s: Catastrophe: A copy of \"%s\": was saved in \"%s\"\n",
670 1.1 cgd processname, o_name, n_name);
671 1.1 cgd exit(1);
672 1.1 cgd }
673 1.1 cgd /*
674 1.1 cgd * Kiss the temp file good bye
675 1.1 cgd */
676 1.1 cgd unlink(n_name);
677 1.1 cgd tempfileopen = FALSE;
678 1.1 cgd return(TRUE);
679 1.1 cgd }
680 1.6 lukem
681 1.1 cgd /*
682 1.1 cgd * return 1 if the tmpfile can be removed after writing it out
683 1.1 cgd */
684 1.6 lukem int
685 1.6 lukem mustoverwrite(preciousfile, tmpfile)
686 1.1 cgd FILE *preciousfile;
687 1.1 cgd FILE *tmpfile;
688 1.1 cgd {
689 1.1 cgd int nread;
690 1.1 cgd
691 1.5 pk while ((nread = fread(edbuf, 1, sizeof(edbuf), tmpfile)) != 0) {
692 1.1 cgd if (mustwrite(edbuf, nread, preciousfile) == 0)
693 1.1 cgd return(0);
694 1.1 cgd }
695 1.1 cgd return(1);
696 1.1 cgd }
697 1.1 cgd /*
698 1.1 cgd * return 0 on catastrophe
699 1.1 cgd */
700 1.6 lukem int
701 1.1 cgd mustwrite(base, n, preciousfile)
702 1.1 cgd char *base;
703 1.1 cgd int n;
704 1.1 cgd FILE *preciousfile;
705 1.1 cgd {
706 1.1 cgd int nwrote;
707 1.1 cgd
708 1.1 cgd if (n <= 0)
709 1.1 cgd return(1);
710 1.1 cgd nwrote = fwrite(base, 1, n, preciousfile);
711 1.1 cgd if (nwrote == n)
712 1.1 cgd return(1);
713 1.1 cgd perror(processname);
714 1.1 cgd switch(inquire(terse
715 1.1 cgd ? "Botch overwriting: retry? "
716 1.1 cgd : "Botch overwriting the source file: retry? ")){
717 1.1 cgd case Q_YES:
718 1.1 cgd case Q_yes:
719 1.1 cgd mustwrite(base + nwrote, n - nwrote, preciousfile);
720 1.1 cgd return(1);
721 1.1 cgd case Q_NO:
722 1.1 cgd case Q_no:
723 1.1 cgd switch(inquire("Are you sure? ")){
724 1.1 cgd case Q_YES:
725 1.1 cgd case Q_yes:
726 1.1 cgd return(0);
727 1.1 cgd case Q_NO:
728 1.1 cgd case Q_no:
729 1.1 cgd mustwrite(base + nwrote, n - nwrote, preciousfile);
730 1.1 cgd return(1);
731 1.1 cgd }
732 1.1 cgd default:
733 1.1 cgd return(0);
734 1.1 cgd }
735 1.1 cgd }
736 1.1 cgd
737 1.1 cgd void
738 1.6 lukem onintr(dummy)
739 1.6 lukem int dummy;
740 1.1 cgd {
741 1.1 cgd switch(inquire(terse
742 1.1 cgd ? "\nContinue? "
743 1.1 cgd : "\nInterrupt: Do you want to continue? ")){
744 1.1 cgd case Q_YES:
745 1.1 cgd case Q_yes:
746 1.1 cgd signal(SIGINT, onintr);
747 1.1 cgd return;
748 1.1 cgd default:
749 1.1 cgd if (tempfileopen){
750 1.1 cgd /*
751 1.1 cgd * Don't overwrite the original file!
752 1.1 cgd */
753 1.1 cgd writetouched(0);
754 1.1 cgd }
755 1.1 cgd exit(1);
756 1.1 cgd }
757 1.1 cgd /*NOTREACHED*/
758 1.1 cgd }
759 1.1 cgd
760 1.6 lukem void
761 1.1 cgd errorprint(place, errorp, print_all)
762 1.1 cgd FILE *place;
763 1.1 cgd Eptr errorp;
764 1.1 cgd boolean print_all;
765 1.1 cgd {
766 1.1 cgd int offset = print_all ? 0 : 2;
767 1.1 cgd
768 1.1 cgd if (errorp->error_e_class == C_IGNORE)
769 1.1 cgd return;
770 1.1 cgd fprintf(place, "[%s] ", lang_table[errorp->error_language].lang_name);
771 1.1 cgd wordvprint(place,errorp->error_lgtext-offset,errorp->error_text+offset);
772 1.1 cgd putc('\n', place);
773 1.1 cgd }
774 1.1 cgd
775 1.6 lukem int
776 1.6 lukem #if __STDC__
777 1.6 lukem inquire(char *fmt, ...)
778 1.6 lukem #else
779 1.6 lukem inquire(fmt, va_alist)
780 1.1 cgd char *fmt;
781 1.6 lukem va_dcl
782 1.6 lukem #endif
783 1.1 cgd {
784 1.6 lukem va_list ap;
785 1.1 cgd char buffer[128];
786 1.1 cgd
787 1.6 lukem #if __STDC__
788 1.6 lukem va_start(ap, fmt);
789 1.6 lukem #else
790 1.6 lukem va_start(ap);
791 1.6 lukem #endif
792 1.6 lukem
793 1.1 cgd if (queryfile == NULL)
794 1.1 cgd return(0);
795 1.1 cgd for(;;){
796 1.1 cgd do{
797 1.1 cgd fflush(stdout);
798 1.6 lukem vfprintf(stderr, fmt, ap);
799 1.1 cgd fflush(stderr);
800 1.1 cgd } while (fgets(buffer, 127, queryfile) == NULL);
801 1.1 cgd switch(buffer[0]){
802 1.1 cgd case 'Y': return(Q_YES);
803 1.1 cgd case 'y': return(Q_yes);
804 1.1 cgd case 'N': return(Q_NO);
805 1.1 cgd case 'n': return(Q_no);
806 1.1 cgd default: fprintf(stderr, "Yes or No only!\n");
807 1.1 cgd }
808 1.1 cgd }
809 1.1 cgd }
810 1.1 cgd
811 1.6 lukem int
812 1.6 lukem probethisfile(name)
813 1.1 cgd char *name;
814 1.1 cgd {
815 1.1 cgd struct stat statbuf;
816 1.1 cgd if (stat(name, &statbuf) < 0)
817 1.1 cgd return(F_NOTEXIST);
818 1.1 cgd if((statbuf.st_mode & S_IREAD) == 0)
819 1.1 cgd return(F_NOTREAD);
820 1.1 cgd if((statbuf.st_mode & S_IWRITE) == 0)
821 1.1 cgd return(F_NOTWRITE);
822 1.1 cgd return(F_TOUCHIT);
823 1.1 cgd }
824