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