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