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