home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / patch2 / part02 < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  50.0 KB

  1. Subject:  v07i039:  Release 2.0 of patch, Part02/03
  2. Newsgroups: mod.sources
  3. Approved: mirror!rs
  4.  
  5. Submitted by: sdcrdcf!lwall (Larry Wall)
  6. Mod.sources: Volume 7, Issue 39
  7. Archive-name: patch2/Part02
  8.  
  9. Here is the official 2.0 release of patch.  It supersedes the 1.5 beta
  10. version posted to net.sources, and the version that comes with 4.3bsd.
  11.  
  12. Larry Wall
  13. sdcrdcf!lwall
  14.  
  15. --------------------------CUT HERE---------------------------
  16. #! /bin/sh
  17.  
  18. # Make a new directory for the patch sources, cd to it, and run kits 1 thru 3 
  19. # through sh.  When all 3 kits have been run, read README.
  20.  
  21. echo "This is patch kit 2 (of 3).  If kit 2 is complete, the line"
  22. echo '"'"End of kit 2 (of 3)"'" will echo at the end.'
  23. echo ""
  24. export PATH || (echo "You didn't use sh, you clunch." ; kill $$)
  25. echo Extracting patch.c
  26. sed >patch.c <<'!STUFFY!FUNK!' -e 's/X//'
  27. Xchar rcsid[] =
  28. X    "$Header: patch.c,v 2.0 86/09/17 15:37:32 lwall Exp $";
  29. X
  30. X/* patch - a program to apply diffs to original files
  31. X *
  32. X * Copyright 1986, Larry Wall
  33. X *
  34. X * This program may be copied as long as you don't try to make any
  35. X * money off of it, or pretend that you wrote it.
  36. X *
  37. X * $Log:    patch.c,v $
  38. X * Revision 2.0  86/09/17  15:37:32  lwall
  39. X * Baseline for netwide release.
  40. X * 
  41. X * Revision 1.5  86/08/01  20:53:24  lwall
  42. X * Changed some %d's to %ld's.
  43. X * Linted.
  44. X * 
  45. X * Revision 1.4  86/08/01  19:17:29  lwall
  46. X * Fixes for machines that can't vararg.
  47. X * Added fuzz factor.
  48. X * Generalized -p.
  49. X * General cleanup.
  50. X * 
  51. X * 85/08/15 van%ucbmonet@berkeley
  52. X * Changes for 4.3bsd diff -c.
  53. X *
  54. X * Revision 1.3  85/03/26  15:07:43  lwall
  55. X * Frozen.
  56. X * 
  57. X * Revision 1.2.1.9  85/03/12  17:03:35  lwall
  58. X * Changed pfp->_file to fileno(pfp).
  59. X * 
  60. X * Revision 1.2.1.8  85/03/12  16:30:43  lwall
  61. X * Check i_ptr and i_womp to make sure they aren't null before freeing.
  62. X * Also allow ed output to be suppressed.
  63. X * 
  64. X * Revision 1.2.1.7  85/03/12  15:56:13  lwall
  65. X * Added -p option from jromine@uci-750a.
  66. X * 
  67. X * Revision 1.2.1.6  85/03/12  12:12:51  lwall
  68. X * Now checks for normalness of file to patch.
  69. X * 
  70. X * Revision 1.2.1.5  85/03/12  11:52:12  lwall
  71. X * Added -D (#ifdef) option from joe@fluke.
  72. X * 
  73. X * Revision 1.2.1.4  84/12/06  11:14:15  lwall
  74. X * Made smarter about SCCS subdirectories.
  75. X * 
  76. X * Revision 1.2.1.3  84/12/05  11:18:43  lwall
  77. X * Added -l switch to do loose string comparison.
  78. X * 
  79. X * Revision 1.2.1.2  84/12/04  09:47:13  lwall
  80. X * Failed hunk count not reset on multiple patch file.
  81. X * 
  82. X * Revision 1.2.1.1  84/12/04  09:42:37  lwall
  83. X * Branch for sdcrdcf changes.
  84. X * 
  85. X * Revision 1.2  84/11/29  13:29:51  lwall
  86. X * Linted.  Identifiers uniqified.  Fixed i_ptr malloc() bug.  Fixed
  87. X * multiple calls to mktemp().  Will now work on machines that can only
  88. X * read 32767 chars.  Added -R option for diffs with new and old swapped.
  89. X * Various cosmetic changes.
  90. X * 
  91. X * Revision 1.1  84/11/09  17:03:58  lwall
  92. X * Initial revision
  93. X * 
  94. X */
  95. X
  96. X#include "INTERN.h"
  97. X#include "common.h"
  98. X#include "EXTERN.h"
  99. X#include "version.h"
  100. X#include "util.h"
  101. X#include "pch.h"
  102. X#include "inp.h"
  103. X
  104. X/* procedures */
  105. X
  106. Xvoid reinitialize_almost_everything();
  107. Xvoid get_some_switches();
  108. XLINENUM locate_hunk();
  109. Xvoid abort_hunk();
  110. Xvoid apply_hunk();
  111. Xvoid do_ed_script();
  112. Xvoid init_output();
  113. Xvoid init_reject();
  114. Xvoid copy_till();
  115. Xvoid spew_output();
  116. Xvoid dump_line();
  117. Xbool patch_match();
  118. Xbool similar();
  119. Xvoid re_input();
  120. Xvoid my_exit();
  121. X
  122. X/* Apply a set of diffs as appropriate. */
  123. X
  124. Xmain(argc,argv)
  125. Xint argc;
  126. Xchar **argv;
  127. X{
  128. X    LINENUM where;
  129. X    LINENUM newwhere;
  130. X    LINENUM fuzz;
  131. X    LINENUM mymaxfuzz;
  132. X    int hunk = 0;
  133. X    int failed = 0;
  134. X    int i;
  135. X
  136. X    setbuf(stderr, serrbuf);
  137. X    for (i = 0; i<MAXFILEC; i++)
  138. X    filearg[i] = Nullch;
  139. X    Mktemp(TMPOUTNAME);
  140. X    Mktemp(TMPINNAME);
  141. X    Mktemp(TMPREJNAME);
  142. X    Mktemp(TMPPATNAME);
  143. X
  144. X    /* parse switches */
  145. X    Argc = argc;
  146. X    Argv = argv;
  147. X    get_some_switches();
  148. X    
  149. X    /* make sure we clean up /tmp in case of disaster */
  150. X    set_signals();
  151. X
  152. X    for (
  153. X    open_patch_file(filearg[1]);
  154. X    there_is_another_patch();
  155. X    reinitialize_almost_everything()
  156. X    ) {                    /* for each patch in patch file */
  157. X
  158. X    if (outname == Nullch)
  159. X        outname = savestr(filearg[0]);
  160. X    
  161. X    /* initialize the patched file */
  162. X    if (!skip_rest_of_patch)
  163. X        init_output(TMPOUTNAME);
  164. X    
  165. X    /* for ed script just up and do it and exit */
  166. X    if (diff_type == ED_DIFF) {
  167. X        do_ed_script();
  168. X        continue;
  169. X    }
  170. X    
  171. X    /* initialize reject file */
  172. X    init_reject(TMPREJNAME);
  173. X    
  174. X    /* find out where all the lines are */
  175. X    if (!skip_rest_of_patch)
  176. X        scan_input(filearg[0]);
  177. X    
  178. X    /* from here on, open no standard i/o files, because malloc */
  179. X    /* might misfire and we can't catch it easily */
  180. X    
  181. X    /* apply each hunk of patch */
  182. X    hunk = 0;
  183. X    failed = 0;
  184. X    out_of_mem = FALSE;
  185. X    while (another_hunk()) {
  186. X        hunk++;
  187. X        fuzz = Nulline;
  188. X        mymaxfuzz = pch_context();
  189. X        if (maxfuzz < mymaxfuzz)
  190. X        mymaxfuzz = maxfuzz;
  191. X        if (!skip_rest_of_patch) {
  192. X        do {
  193. X            where = locate_hunk(fuzz);
  194. X            if (hunk == 1 && where == Nulline && !force) {
  195. X                        /* dwim for reversed patch? */
  196. X            if (!pch_swap()) {
  197. X                if (fuzz == Nulline)
  198. X                say1("\
  199. XNot enough memory to try swapped hunk!  Assuming unswapped.\n");
  200. X                continue;
  201. X            }
  202. X            reverse = !reverse;
  203. X            where = locate_hunk(fuzz);  /* try again */
  204. X            if (where == Nulline) {        /* didn't find it swapped */
  205. X                if (!pch_swap())         /* put it back to normal */
  206. X                fatal1("Lost hunk on alloc error!\n");
  207. X                reverse = !reverse;
  208. X            }
  209. X            else if (noreverse) {
  210. X                if (!pch_swap())         /* put it back to normal */
  211. X                fatal1("Lost hunk on alloc error!\n");
  212. X                reverse = !reverse;
  213. X                say1("\
  214. XIgnoring previously applied (or reversed) patch.\n");
  215. X                skip_rest_of_patch = TRUE;
  216. X            }
  217. X            else {
  218. X                ask3("\
  219. X%seversed (or previously applied) patch detected!  %s -R? [y] ",
  220. X                reverse ? "R" : "Unr",
  221. X                reverse ? "Assume" : "Ignore");
  222. X                if (*buf == 'n') {
  223. X                ask1("Apply anyway? [n] ");
  224. X                if (*buf != 'y')
  225. X                    skip_rest_of_patch = TRUE;
  226. X                where = Nulline;
  227. X                reverse = !reverse;
  228. X                if (!pch_swap())  /* put it back to normal */
  229. X                    fatal1("Lost hunk on alloc error!\n");
  230. X                }
  231. X            }
  232. X            }
  233. X        } while (!skip_rest_of_patch && where == Nulline &&
  234. X            ++fuzz <= mymaxfuzz);
  235. X
  236. X        if (skip_rest_of_patch) {        /* just got decided */
  237. X            Fclose(ofp);
  238. X            ofp = Nullfp;
  239. X        }
  240. X        }
  241. X
  242. X        newwhere = pch_newfirst() + last_offset;
  243. X        if (skip_rest_of_patch) {
  244. X        abort_hunk();
  245. X        failed++;
  246. X        if (verbose)
  247. X            say3("Hunk #%d ignored at %ld.\n", hunk, newwhere);
  248. X        }
  249. X        else if (where == Nulline) {
  250. X        abort_hunk();
  251. X        failed++;
  252. X        if (verbose)
  253. X            say3("Hunk #%d failed at %ld.\n", hunk, newwhere);
  254. X        }
  255. X        else {
  256. X        apply_hunk(where);
  257. X        if (verbose) {
  258. X            say3("Hunk #%d succeeded at %ld", hunk, newwhere);
  259. X            if (fuzz)
  260. X            say2(" with fuzz %ld", fuzz);
  261. X            if (last_offset)
  262. X            say3(" (offset %ld line%s)",
  263. X                last_offset, last_offset==1L?"":"s");
  264. X            say1(".\n");
  265. X        }
  266. X        }
  267. X    }
  268. X
  269. X    if (out_of_mem && using_plan_a) {
  270. X        Argc = Argc_last;
  271. X        Argv = Argv_last;
  272. X        say1("\n\nRan out of memory using Plan A--trying again...\n\n");
  273. X        continue;
  274. X    }
  275. X    
  276. X    assert(hunk);
  277. X    
  278. X    /* finish spewing out the new file */
  279. X    if (!skip_rest_of_patch)
  280. X        spew_output();
  281. X    
  282. X    /* and put the output where desired */
  283. X    ignore_signals();
  284. X    if (!skip_rest_of_patch) {
  285. X        if (move_file(TMPOUTNAME, outname) < 0) {
  286. X        toutkeep = TRUE;
  287. X        chmod(TMPOUTNAME, filemode);
  288. X        }
  289. X        else
  290. X        chmod(outname, filemode);
  291. X    }
  292. X    Fclose(rejfp);
  293. X    rejfp = Nullfp;
  294. X    if (failed) {
  295. X        if (!*rejname) {
  296. X        Strcpy(rejname, outname);
  297. X        Strcat(rejname, ".rej");
  298. X        }
  299. X        if (skip_rest_of_patch) {
  300. X        say4("%d out of %d hunks ignored--saving rejects to %s\n",
  301. X            failed, hunk, rejname);
  302. X        }
  303. X        else {
  304. X        say4("%d out of %d hunks failed--saving rejects to %s\n",
  305. X            failed, hunk, rejname);
  306. X        }
  307. X        if (move_file(TMPREJNAME, rejname) < 0)
  308. X        trejkeep = TRUE;
  309. X    }
  310. X    set_signals();
  311. X    }
  312. X    my_exit(0);
  313. X}
  314. X
  315. X/* Prepare to find the next patch to do in the patch file. */
  316. X
  317. Xvoid
  318. Xreinitialize_almost_everything()
  319. X{
  320. X    re_patch();
  321. X    re_input();
  322. X
  323. X    input_lines = 0;
  324. X    last_frozen_line = 0;
  325. X
  326. X    filec = 0;
  327. X    if (filearg[0] != Nullch && !out_of_mem) {
  328. X    free(filearg[0]);
  329. X    filearg[0] = Nullch;
  330. X    }
  331. X
  332. X    if (outname != Nullch) {
  333. X    free(outname);
  334. X    outname = Nullch;
  335. X    }
  336. X
  337. X    last_offset = 0;
  338. X
  339. X    diff_type = 0;
  340. X
  341. X    if (revision != Nullch) {
  342. X    free(revision);
  343. X    revision = Nullch;
  344. X    }
  345. X
  346. X    reverse = FALSE;
  347. X    skip_rest_of_patch = FALSE;
  348. X
  349. X    get_some_switches();
  350. X
  351. X    if (filec >= 2)
  352. X    fatal1("You may not change to a different patch file.\n");
  353. X}
  354. X
  355. X/* Process switches and filenames up to next '+' or end of list. */
  356. X
  357. Xvoid
  358. Xget_some_switches()
  359. X{
  360. X    Reg1 char *s;
  361. X
  362. X    rejname[0] = '\0';
  363. X    Argc_last = Argc;
  364. X    Argv_last = Argv;
  365. X    if (!Argc)
  366. X    return;
  367. X    for (Argc--,Argv++; Argc; Argc--,Argv++) {
  368. X    s = Argv[0];
  369. X    if (strEQ(s, "+")) {
  370. X        return;            /* + will be skipped by for loop */
  371. X    }
  372. X    if (*s != '-' || !s[1]) {
  373. X        if (filec == MAXFILEC)
  374. X        fatal1("Too many file arguments.\n");
  375. X        filearg[filec++] = savestr(s);
  376. X    }
  377. X    else {
  378. X        switch (*++s) {
  379. X        case 'b':
  380. X        origext = savestr(Argv[1]);
  381. X        Argc--,Argv++;
  382. X        break;
  383. X        case 'c':
  384. X        diff_type = CONTEXT_DIFF;
  385. X        break;
  386. X        case 'd':
  387. X        if (!*++s) {
  388. X            Argc--,Argv++;
  389. X            s = Argv[0];
  390. X        }
  391. X        if (chdir(s) < 0)
  392. X            fatal2("Can't cd to %s.\n", s);
  393. X        break;
  394. X        case 'D':
  395. X            do_defines = TRUE;
  396. X        if (!*++s) {
  397. X            Argc--,Argv++;
  398. X            s = Argv[0];
  399. X        }
  400. X        Sprintf(if_defined, "#ifdef %s\n", s);
  401. X        Sprintf(not_defined, "#ifndef %s\n", s);
  402. X        Sprintf(end_defined, "#endif /* %s */\n", s);
  403. X        break;
  404. X        case 'e':
  405. X        diff_type = ED_DIFF;
  406. X        break;
  407. X        case 'f':
  408. X        force = TRUE;
  409. X        break;
  410. X        case 'F':
  411. X        if (*++s == '=')
  412. X            s++;
  413. X        maxfuzz = atoi(s);
  414. X        break;
  415. X        case 'l':
  416. X        canonicalize = TRUE;
  417. X        break;
  418. X        case 'n':
  419. X        diff_type = NORMAL_DIFF;
  420. X        break;
  421. X        case 'N':
  422. X        noreverse = TRUE;
  423. X        break;
  424. X        case 'o':
  425. X        outname = savestr(Argv[1]);
  426. X        Argc--,Argv++;
  427. X        break;
  428. X        case 'p':
  429. X        if (*++s == '=')
  430. X            s++;
  431. X        strippath = atoi(s);
  432. X        break;
  433. X        case 'r':
  434. X        Strcpy(rejname, Argv[1]);
  435. X        Argc--,Argv++;
  436. X        break;
  437. X        case 'R':
  438. X        reverse = TRUE;
  439. X        break;
  440. X        case 's':
  441. X        verbose = FALSE;
  442. X        break;
  443. X        case 'S':
  444. X        skip_rest_of_patch = TRUE;
  445. X        break;
  446. X        case 'v':
  447. X        version();
  448. X        break;
  449. X#ifdef DEBUGGING
  450. X        case 'x':
  451. X        debug = atoi(s+1);
  452. X        break;
  453. X#endif
  454. X        default:
  455. X        fatal2("Unrecognized switch: %s\n", Argv[0]);
  456. X        }
  457. X    }
  458. X    }
  459. X}
  460. X
  461. X/* Attempt to find the right place to apply this hunk of patch. */
  462. X
  463. XLINENUM
  464. Xlocate_hunk(fuzz)
  465. XLINENUM fuzz;
  466. X{
  467. X    Reg1 LINENUM first_guess = pch_first() + last_offset;
  468. X    Reg2 LINENUM offset;
  469. X    LINENUM pat_lines = pch_ptrn_lines();
  470. X    Reg3 LINENUM max_pos_offset = input_lines - first_guess
  471. X                - pat_lines + 1; 
  472. X    Reg4 LINENUM max_neg_offset = first_guess - last_frozen_line - 1
  473. X                + pch_context();
  474. X
  475. X    if (!pat_lines)            /* null range matches always */
  476. X    return first_guess;
  477. X    if (max_neg_offset >= first_guess)    /* do not try lines < 0 */
  478. X    max_neg_offset = first_guess - 1;
  479. X    if (first_guess <= input_lines && patch_match(first_guess, Nulline, fuzz))
  480. X    return first_guess;
  481. X    for (offset = 1; ; offset++) {
  482. X    Reg5 bool check_after = (offset <= max_pos_offset);
  483. X    Reg6 bool check_before = (offset <= max_pos_offset);
  484. X
  485. X    if (check_after && patch_match(first_guess, offset, fuzz)) {
  486. X#ifdef DEBUGGING
  487. X        if (debug & 1)
  488. X        say3("Offset changing from %ld to %ld\n", last_offset, offset);
  489. X#endif
  490. X        last_offset = offset;
  491. X        return first_guess+offset;
  492. X    }
  493. X    else if (check_before && patch_match(first_guess, -offset, fuzz)) {
  494. X#ifdef DEBUGGING
  495. X        if (debug & 1)
  496. X        say3("Offset changing from %ld to %ld\n", last_offset, -offset);
  497. X#endif
  498. X        last_offset = -offset;
  499. X        return first_guess-offset;
  500. X    }
  501. X    else if (!check_before && !check_after)
  502. X        return Nulline;
  503. X    }
  504. X}
  505. X
  506. X/* We did not find the pattern, dump out the hunk so they can handle it. */
  507. X
  508. Xvoid
  509. Xabort_hunk()
  510. X{
  511. X    Reg1 LINENUM i;
  512. X    Reg2 LINENUM pat_end = pch_end();
  513. X    /* add in last_offset to guess the same as the previous successful hunk */
  514. X    LINENUM oldfirst = pch_first() + last_offset;
  515. X    LINENUM newfirst = pch_newfirst() + last_offset;
  516. X    LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1;
  517. X    LINENUM newlast = newfirst + pch_repl_lines() - 1;
  518. X    char *stars = (diff_type == NEW_CONTEXT_DIFF ? " ****" : "");
  519. X    char *minuses = (diff_type == NEW_CONTEXT_DIFF ? " ----" : " -----");
  520. X
  521. X    fprintf(rejfp, "***************\n");
  522. X    for (i=0; i<=pat_end; i++) {
  523. X    switch (pch_char(i)) {
  524. X    case '*':
  525. X        if (oldlast < oldfirst)
  526. X        fprintf(rejfp, "*** 0%s\n", stars);
  527. X        else if (oldlast == oldfirst)
  528. X        fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
  529. X        else
  530. X        fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst, oldlast, stars);
  531. X        break;
  532. X    case '=':
  533. X        if (newlast < newfirst)
  534. X        fprintf(rejfp, "--- 0%s\n", minuses);
  535. X        else if (newlast == newfirst)
  536. X        fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
  537. X        else
  538. X        fprintf(rejfp, "--- %ld,%ld%s\n", newfirst, newlast, minuses);
  539. X        break;
  540. X    case '\n':
  541. X        fprintf(rejfp, "%s", pfetch(i));
  542. X        break;
  543. X    case ' ': case '-': case '+': case '!':
  544. X        fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
  545. X        break;
  546. X    default:
  547. X        say1("Fatal internal error in abort_hunk().\n"); 
  548. X        abort();
  549. X    }
  550. X    }
  551. X}
  552. X
  553. X/* We found where to apply it (we hope), so do it. */
  554. X
  555. Xvoid
  556. Xapply_hunk(where)
  557. XLINENUM where;
  558. X{
  559. X    Reg1 LINENUM old = 1;
  560. X    Reg2 LINENUM lastline = pch_ptrn_lines();
  561. X    Reg3 LINENUM new = lastline+1;
  562. X#define OUTSIDE 0
  563. X#define IN_IFNDEF 1
  564. X#define IN_IFDEF 2
  565. X#define IN_ELSE 3
  566. X    Reg4 int def_state = OUTSIDE;
  567. X    Reg5 bool R_do_defines = do_defines;
  568. X
  569. X    where--;
  570. X    while (pch_char(new) == '=' || pch_char(new) == '\n')
  571. X    new++;
  572. X    
  573. X    while (old <= lastline) {
  574. X    if (pch_char(old) == '-') {
  575. X        copy_till(where + old - 1);
  576. X        if (R_do_defines) {
  577. X        if (def_state == OUTSIDE) {
  578. X            fputs(not_defined, ofp);
  579. X            def_state = IN_IFNDEF;
  580. X        }
  581. X        else if (def_state == IN_IFDEF) {
  582. X            fputs(else_defined, ofp);
  583. X            def_state = IN_ELSE;
  584. X        }
  585. X        fputs(pfetch(old), ofp);
  586. X        }
  587. X        last_frozen_line++;
  588. X        old++;
  589. X    }
  590. X    else if (pch_char(new) == '+') {
  591. X        copy_till(where + old - 1);
  592. X        if (R_do_defines) {
  593. X        if (def_state == IN_IFNDEF) {
  594. X            fputs(else_defined, ofp);
  595. X            def_state = IN_ELSE;
  596. X        }
  597. X        else if (def_state == OUTSIDE) {
  598. X            fputs(if_defined, ofp);
  599. X            def_state = IN_IFDEF;
  600. X        }
  601. X        }
  602. X        fputs(pfetch(new), ofp);
  603. X        new++;
  604. X    }
  605. X    else {
  606. X        if (pch_char(new) != pch_char(old)) {
  607. X        say3("Out-of-sync patch, lines %ld,%ld\n",
  608. X            pch_hunk_beg() + old - 1,
  609. X            pch_hunk_beg() + new - 1);
  610. X#ifdef DEBUGGING
  611. X        say3("oldchar = '%c', newchar = '%c'\n",
  612. X            pch_char(old), pch_char(new));
  613. X#endif
  614. X        my_exit(1);
  615. X        }
  616. X        if (pch_char(new) == '!') {
  617. X        copy_till(where + old - 1);
  618. X        if (R_do_defines) {
  619. X           fputs(not_defined, ofp);
  620. X           def_state = IN_IFNDEF;
  621. X        }
  622. X        while (pch_char(old) == '!') {
  623. X            if (R_do_defines) {
  624. X            fputs(pfetch(old), ofp);
  625. X            }
  626. X            last_frozen_line++;
  627. X            old++;
  628. X        }
  629. X        if (R_do_defines) {
  630. X            fputs(else_defined, ofp);
  631. X            def_state = IN_ELSE;
  632. X        }
  633. X        while (pch_char(new) == '!') {
  634. X            fputs(pfetch(new), ofp);
  635. X            new++;
  636. X        }
  637. X        if (R_do_defines) {
  638. X            fputs(end_defined, ofp);
  639. X            def_state = OUTSIDE;
  640. X        }
  641. X        }
  642. X        else {
  643. X        assert(pch_char(new) == ' ');
  644. X        old++;
  645. X        new++;
  646. X        }
  647. X    }
  648. X    }
  649. X    if (new <= pch_end() && pch_char(new) == '+') {
  650. X    copy_till(where + old - 1);
  651. X    if (R_do_defines) {
  652. X        if (def_state == OUTSIDE) {
  653. X            fputs(if_defined, ofp);
  654. X        def_state = IN_IFDEF;
  655. X        }
  656. X        else if (def_state == IN_IFNDEF) {
  657. X        fputs(else_defined, ofp);
  658. X        def_state = IN_ELSE;
  659. X        }
  660. X    }
  661. X    while (new <= pch_end() && pch_char(new) == '+') {
  662. X        fputs(pfetch(new), ofp);
  663. X        new++;
  664. X    }
  665. X    }
  666. X    if (R_do_defines && def_state != OUTSIDE) {
  667. X    fputs(end_defined, ofp);
  668. X    }
  669. X}
  670. X
  671. X/* Apply an ed script by feeding ed itself. */
  672. X
  673. Xvoid
  674. Xdo_ed_script()
  675. X{
  676. X    Reg1 char *t;
  677. X    Reg2 long beginning_of_this_line;
  678. X    Reg3 bool this_line_is_command = FALSE;
  679. X    Reg4 FILE *pipefp;
  680. X    FILE *popen();
  681. X
  682. X    if (!skip_rest_of_patch) {
  683. X    Unlink(TMPOUTNAME);
  684. X    copy_file(filearg[0], TMPOUTNAME);
  685. X    if (verbose)
  686. X        Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  687. X    else
  688. X        Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  689. X    pipefp = popen(buf, "w");
  690. X    }
  691. X    for (;;) {
  692. X    beginning_of_this_line = ftell(pfp);
  693. X    if (pgets(buf, sizeof buf, pfp) == Nullch) {
  694. X        next_intuit_at(beginning_of_this_line);
  695. X        break;
  696. X    }
  697. X    for (t=buf; isdigit(*t) || *t == ','; t++) ;
  698. X    this_line_is_command = (isdigit(*buf) &&
  699. X      (*t == 'd' || *t == 'c' || *t == 'a') );
  700. X    if (this_line_is_command) {
  701. X        if (!skip_rest_of_patch)
  702. X        fputs(buf, pipefp);
  703. X        if (*t != 'd') {
  704. X        while (pgets(buf, sizeof buf, pfp) != Nullch) {
  705. X            if (!skip_rest_of_patch)
  706. X            fputs(buf, pipefp);
  707. X            if (strEQ(buf, ".\n"))
  708. X            break;
  709. X        }
  710. X        }
  711. X    }
  712. X    else {
  713. X        next_intuit_at(beginning_of_this_line);
  714. X        break;
  715. X    }
  716. X    }
  717. X    if (skip_rest_of_patch)
  718. X    return;
  719. X    fprintf(pipefp, "w\n");
  720. X    fprintf(pipefp, "q\n");
  721. X    Fflush(pipefp);
  722. X    Pclose(pipefp);
  723. X    ignore_signals();
  724. X    if (move_file(TMPOUTNAME, outname) < 0) {
  725. X    toutkeep = TRUE;
  726. X    chmod(TMPOUTNAME, filemode);
  727. X    }
  728. X    else
  729. X    chmod(outname, filemode);
  730. X    set_signals();
  731. X}
  732. X
  733. X/* Open the new file. */
  734. X
  735. Xvoid
  736. Xinit_output(name)
  737. Xchar *name;
  738. X{
  739. X    ofp = fopen(name, "w");
  740. X    if (ofp == Nullfp)
  741. X    fatal2("patch: can't create %s.\n", name);
  742. X}
  743. X
  744. X/* Open a file to put hunks we can't locate. */
  745. X
  746. Xvoid
  747. Xinit_reject(name)
  748. Xchar *name;
  749. X{
  750. X    rejfp = fopen(name, "w");
  751. X    if (rejfp == Nullfp)
  752. X    fatal2("patch: can't create %s.\n", name);
  753. X}
  754. X
  755. X/* Copy input file to output, up to wherever hunk is to be applied. */
  756. X
  757. Xvoid
  758. Xcopy_till(lastline)
  759. XReg1 LINENUM lastline;
  760. X{
  761. X    Reg2 LINENUM R_last_frozen_line = last_frozen_line;
  762. X
  763. X    if (R_last_frozen_line > lastline)
  764. X    say1("patch: misordered hunks! output will be garbled.\n");
  765. X    while (R_last_frozen_line < lastline) {
  766. X    dump_line(++R_last_frozen_line);
  767. X    }
  768. X    last_frozen_line = R_last_frozen_line;
  769. X}
  770. X
  771. X/* Finish copying the input file to the output file. */
  772. X
  773. Xvoid
  774. Xspew_output()
  775. X{
  776. X#ifdef DEBUGGING
  777. X    if (debug & 256)
  778. X    say3("il=%ld lfl=%ld\n",input_lines,last_frozen_line);
  779. X#endif
  780. X    if (input_lines)
  781. X    copy_till(input_lines);        /* dump remainder of file */
  782. X    Fclose(ofp);
  783. X    ofp = Nullfp;
  784. X}
  785. X
  786. X/* Copy one line from input to output. */
  787. X
  788. Xvoid
  789. Xdump_line(line)
  790. XLINENUM line;
  791. X{
  792. X    Reg1 char *s;
  793. X    Reg2 char R_newline = '\n';
  794. X
  795. X    /* Note: string is not null terminated. */
  796. X    for (s=ifetch(line, 0); putc(*s, ofp) != R_newline; s++) ;
  797. X}
  798. X
  799. X/* Does the patch pattern match at line base+offset? */
  800. X
  801. Xbool
  802. Xpatch_match(base, offset, fuzz)
  803. XLINENUM base;
  804. XLINENUM offset;
  805. XLINENUM fuzz;
  806. X{
  807. X    Reg1 LINENUM pline = 1 + fuzz;
  808. X    Reg2 LINENUM iline;
  809. X    Reg3 LINENUM pat_lines = pch_ptrn_lines() - fuzz;
  810. X
  811. X    for (iline=base+offset; pline <= pat_lines; pline++,iline++) {
  812. X    if (canonicalize) {
  813. X        if (!similar(ifetch(iline, (offset >= 0)),
  814. X             pfetch(pline),
  815. X             pch_line_len(pline) ))
  816. X        return FALSE;
  817. X    }
  818. X    else if (strnNE(ifetch(iline, (offset >= 0)),
  819. X           pfetch(pline),
  820. X           pch_line_len(pline) ))
  821. X        return FALSE;
  822. X    }
  823. X    return TRUE;
  824. X}
  825. X
  826. X/* Do two lines match with canonicalized white space? */
  827. X
  828. Xbool
  829. Xsimilar(a,b,len)
  830. XReg1 char *a;
  831. XReg2 char *b;
  832. XReg3 int len;
  833. X{
  834. X    while (len) {
  835. X    if (isspace(*b)) {        /* whitespace (or \n) to match? */
  836. X        if (!isspace(*a))        /* no corresponding whitespace? */
  837. X        return FALSE;
  838. X        while (len && isspace(*b) && *b != '\n')
  839. X        b++,len--;        /* skip pattern whitespace */
  840. X        while (isspace(*a) && *a != '\n')
  841. X        a++;            /* skip target whitespace */
  842. X        if (*a == '\n' || *b == '\n')
  843. X        return (*a == *b);    /* should end in sync */
  844. X    }
  845. X    else if (*a++ != *b++)        /* match non-whitespace chars */
  846. X        return FALSE;
  847. X    else
  848. X        len--;            /* probably not necessary */
  849. X    }
  850. X    return TRUE;            /* actually, this is not reached */
  851. X                    /* since there is always a \n */
  852. X}
  853. X
  854. X/* Exit with cleanup. */
  855. X
  856. Xvoid
  857. Xmy_exit(status)
  858. Xint status;
  859. X{
  860. X    Unlink(TMPINNAME);
  861. X    if (!toutkeep) {
  862. X    Unlink(TMPOUTNAME);
  863. X    }
  864. X    if (!trejkeep) {
  865. X    Unlink(TMPREJNAME);
  866. X    }
  867. X    Unlink(TMPPATNAME);
  868. X    exit(status);
  869. X}
  870. !STUFFY!FUNK!
  871. echo Extracting patch.man
  872. sed >patch.man <<'!STUFFY!FUNK!' -e 's/X//'
  873. X''' $Header: patch.man,v 2.0 86/09/17 15:39:09 lwall Exp $
  874. X''' 
  875. X''' $Log:    patch.man,v $
  876. X''' Revision 2.0  86/09/17  15:39:09  lwall
  877. X''' Baseline for netwide release.
  878. X''' 
  879. X''' Revision 1.4  86/08/01  19:23:22  lwall
  880. X''' Documented -v, -p, -F.
  881. X''' Added notes to patch senders.
  882. X''' 
  883. X''' Revision 1.3  85/03/26  15:11:06  lwall
  884. X''' Frozen.
  885. X''' 
  886. X''' Revision 1.2.1.4  85/03/12  16:14:27  lwall
  887. X''' Documented -p.
  888. X''' 
  889. X''' Revision 1.2.1.3  85/03/12  16:09:41  lwall
  890. X''' Documented -D.
  891. X''' 
  892. X''' Revision 1.2.1.2  84/12/05  11:06:55  lwall
  893. X''' Added -l switch, and noted bistability bug.
  894. X''' 
  895. X''' Revision 1.2.1.1  84/12/04  17:23:39  lwall
  896. X''' Branch for sdcrdcf changes.
  897. X''' 
  898. X''' Revision 1.2  84/12/04  17:22:02  lwall
  899. X''' Baseline version.
  900. X''' 
  901. X.de Sh
  902. X.br
  903. X.ne 5
  904. X.PP
  905. X\fB\\$1\fR
  906. X.PP
  907. X..
  908. X.de Sp
  909. X.if t .sp .5v
  910. X.if n .sp
  911. X..
  912. X'''
  913. X'''     Set up \*(-- to give an unbreakable dash;
  914. X'''     string Tr holds user defined translation string.
  915. X'''     Bell System Logo is used as a dummy character.
  916. X'''
  917. X.ie n \{\
  918. X.tr \(bs-\*(Tr
  919. X.ds -- \(bs-
  920. X.if (\n(.H=4u)&(1m=24u) .ds -- \(bs\h'-12u'\(bs\h'-12u'-\" diablo 10 pitch
  921. X.if (\n(.H=4u)&(1m=20u) .ds -- \(bs\h'-12u'\(bs\h'-8u'-\" diablo 12 pitch
  922. X.ds L" ""
  923. X.ds R" ""
  924. X.ds L' '
  925. X.ds R' '
  926. X'br\}
  927. X.el\{\
  928. X.ds -- \(em\|
  929. X.tr \*(Tr
  930. X.ds L" ``
  931. X.ds R" ''
  932. X.ds L' `
  933. X.ds R' '
  934. X'br\}
  935. X.TH PATCH 1 LOCAL
  936. X.SH NAME
  937. Xpatch - a program for applying a diff file to an original
  938. X.SH SYNOPSIS
  939. X.B patch
  940. X[options] orig patchfile [+ [options] orig]
  941. X.sp
  942. Xbut usually just
  943. X.sp
  944. X.B patch
  945. X<patchfile
  946. X.SH DESCRIPTION
  947. X.I Patch
  948. Xwill take a patch file containing any of the three forms of difference
  949. Xlisting produced by the
  950. X.I diff
  951. Xprogram and apply those differences to an original file, producing a patched
  952. Xversion.
  953. XBy default, the patched version is put in place of the original, with
  954. Xthe original file backed up to the same name with the
  955. Xextension \*(L".orig\*(R", or as specified by the
  956. X.B -b
  957. Xswitch.
  958. XYou may also specify where you want the output to go with a
  959. X.B -o
  960. Xswitch.
  961. XIf
  962. X.I patchfile
  963. Xis omitted, or is a hyphen, the patch will be read from standard input.
  964. X.PP
  965. XUpon startup, patch will attempt to determine the type of the diff listing,
  966. Xunless over-ruled by a
  967. X.BR -c ,
  968. X.BR -e ,
  969. Xor
  970. X.B -n
  971. Xswitch.
  972. XContext diffs and normal diffs are applied by the
  973. X.I patch
  974. Xprogram itself, while ed diffs are simply fed to the
  975. X.I ed
  976. Xeditor via a pipe.
  977. X.PP
  978. X.I Patch
  979. Xwill try to skip any leading garbage, apply the diff,
  980. Xand then skip any trailing garbage.
  981. XThus you could feed an article or message containing a
  982. Xdiff listing to
  983. X.IR patch ,
  984. Xand it should work.
  985. XIf the entire diff is indented by a consistent amount,
  986. Xthis will be taken into account.
  987. X.PP
  988. XWith context diffs, and to a lesser extent with normal diffs,
  989. X.I patch
  990. Xcan detect when the line numbers mentioned in the patch are incorrect,
  991. Xand will attempt to find the correct place to apply each hunk of the patch.
  992. XAs a first guess, it takes the line number mentioned for the hunk, plus or
  993. Xminus any offset used in applying the previous hunk.
  994. XIf that is not the correct place,
  995. X.I patch
  996. Xwill scan both forwards and backwards for a set of lines matching the context
  997. Xgiven in the hunk.
  998. XFirst
  999. X.I patch
  1000. Xlooks for a place where all lines of the context match.
  1001. XIf no such place is found, and it's a context diff, and the maximum fuzz factor
  1002. Xis set to 1 or more, then another scan takes place ignoring the first and last
  1003. Xline of context.
  1004. XIf that fails, and the maximum fuzz factor is set to 2 or more,
  1005. Xthe first two and last two lines of context are ignored,
  1006. Xand another scan is made.
  1007. X(The default maximum fuzz factor is 2.)
  1008. XIf
  1009. X.I patch
  1010. Xcannot find a place to install that hunk of the patch, it will put the
  1011. Xhunk out to a reject file, which normally is the name of the output file
  1012. Xplus \*(L".rej\*(R".
  1013. X(Note that the rejected hunk will come out in context diff form whether the
  1014. Xinput patch was a context diff or a normal diff.
  1015. XIf the input was a normal diff, many of the contexts will simply be null.)
  1016. XThe line numbers on the hunks in the reject file may be different than
  1017. Xin the patch file: they reflect the approximate location patch thinks the
  1018. Xfailed hunks belong in the new file rather than the old one.
  1019. X.PP
  1020. XAs each hunk is completed, you will be told whether the hunk succeeded or
  1021. Xfailed, and which line (in the new file)
  1022. X.I patch
  1023. Xthought the hunk should go on.
  1024. XIf this is different from the line number specified in the diff you will
  1025. Xbe told the offset.
  1026. XA single large offset MAY be an indication that a hunk was installed in the
  1027. Xwrong place.
  1028. XYou will also be told if a fuzz factor was used to make the match, in which
  1029. Xcase you should also be slightly suspicious.
  1030. X.PP
  1031. XIf no original file is specified on the command line,
  1032. X.I patch
  1033. Xwill try to figure out from the leading garbage what the name of the file
  1034. Xto edit is.
  1035. XIn the header of a context diff, the filename is found from lines beginning
  1036. Xwith \*(L"***\*(R" or \*(L"---\*(R", with the shortest name of an existing
  1037. Xfile winning.
  1038. XOnly context diffs have lines like that, but if there is an \*(L"Index:\*(R"
  1039. Xline in the leading garbage,
  1040. X.I patch
  1041. Xwill try to use the filename from that line.
  1042. XThe context diff header takes precedence over an Index line.
  1043. XIf no filename can be intuited from the leading garbage, you will be asked
  1044. Xfor the name of the file to patch.
  1045. X.PP
  1046. X(If the original file cannot be found, but a suitable SCCS or RCS file is
  1047. Xhandy,
  1048. X.I patch
  1049. Xwill attempt to get or check out the file.)
  1050. X.PP
  1051. XAdditionally, if the leading garbage contains a \*(L"Prereq: \*(R" line,
  1052. X.I patch
  1053. Xwill take the first word from the prerequisites line (normally a version
  1054. Xnumber) and check the input file to see if that word can be found.
  1055. XIf not,
  1056. X.I patch
  1057. Xwill ask for confirmation before proceeding.
  1058. X.PP
  1059. XThe upshot of all this is that you should be able to say, while in a news
  1060. Xinterface, the following:
  1061. X.Sp
  1062. X    | patch -d /usr/src/local/blurfl
  1063. X.Sp
  1064. Xand patch a file in the blurfl directory directly from the article containing
  1065. Xthe patch.
  1066. X.PP
  1067. XIf the patch file contains more than one patch,
  1068. X.I patch
  1069. Xwill try to apply each of them as if they came from separate patch files.
  1070. XThis means, among other things, that it is assumed that the name of the file
  1071. Xto patch must be determined for each diff listing,
  1072. Xand that the garbage before each diff listing will
  1073. Xbe examined for interesting things such as filenames and revision level, as
  1074. Xmentioned previously.
  1075. XYou can give switches (and another original file name) for the second and
  1076. Xsubsequent patches by separating the corresponding argument lists
  1077. Xby a \*(L'+\*(R'.
  1078. X(The argument list for a second or subsequent patch may not specify a new
  1079. Xpatch file, however.)
  1080. X.PP
  1081. X.I Patch
  1082. Xrecognizes the following switches:
  1083. X.TP 5
  1084. X.B \-b
  1085. Xcauses the next argument to be interpreted as the backup extension, to be
  1086. Xused in place of \*(L".orig\*(R".
  1087. X.TP 5
  1088. X.B \-c
  1089. Xforces
  1090. X.I patch
  1091. Xto interpret the patch file as a context diff.
  1092. X.TP 5
  1093. X.B \-d
  1094. Xcauses
  1095. X.I patch
  1096. Xto interpret the next argument as a directory, and cd to it before doing
  1097. Xanything else.
  1098. X.TP 5
  1099. X.B \-D
  1100. Xcauses
  1101. X.I patch
  1102. Xto use the "#ifdef...#endif" construct to mark changes.
  1103. XThe argument following will be used as the differentiating symbol.
  1104. XNote that, unlike the C compiler, there must be a space between the
  1105. X.B \-D
  1106. Xand the argument.
  1107. X.TP 5
  1108. X.B \-e
  1109. Xforces
  1110. X.I patch
  1111. Xto interpret the patch file as an ed script.
  1112. X.TP 5
  1113. X.B \-f
  1114. Xforces
  1115. X.I patch
  1116. Xto assume that the user knows exactly what he or she is doing, and to not
  1117. Xask any questions.
  1118. XIt does not suppress commentary, however.
  1119. XUse
  1120. X.B \-s
  1121. Xfor that.
  1122. X.TP 5
  1123. X.B \-F<number>
  1124. Xsets the maximum fuzz factor.
  1125. XThis switch only applied to context diffs, and causes
  1126. X.I patch
  1127. Xto ignore up to that many lines in looking for places to install a hunk.
  1128. XNote that a larger fuzz factor increases the odds of a faulty patch.
  1129. XThe default fuzz factor is 2, and it may not be set to more than
  1130. Xthe number of lines of context in the context diff, ordinarily 3.
  1131. X.TP 5
  1132. X.B \-l
  1133. Xcauses the pattern matching to be done loosely, in case the tabs and
  1134. Xspaces have been munged in your input file.
  1135. XAny sequence of whitespace in the pattern line will match any sequence
  1136. Xin the input file.
  1137. XNormal characters must still match exactly.
  1138. XEach line of the context must still match a line in the input file.
  1139. X.TP 5
  1140. X.B \-n
  1141. Xforces
  1142. X.I patch
  1143. Xto interpret the patch file as a normal diff.
  1144. X.TP 5
  1145. X.B \-N
  1146. Xcauses
  1147. X.I patch
  1148. Xto ignore patches that it thinks are reversed or already applied.
  1149. XSee also
  1150. X.B \-R .
  1151. X.TP 5
  1152. X.B \-o
  1153. Xcauses the next argument to be interpreted as the output file name.
  1154. X.TP 5
  1155. X.B \-p<number>
  1156. Xsets the pathname strip count,
  1157. Xwhich controls how pathnames found in the patch file are treated, in case
  1158. Xthe you keep your files in a different directory than the person who sent
  1159. Xout the patch.
  1160. XThe strip count specifies how many backslashes are to be stripped from
  1161. Xthe front of the pathname.
  1162. X(Any intervening directory names also go away.)
  1163. XFor example, supposing the filename in the patch file was
  1164. X.sp
  1165. X    /u/howard/src/blurfl/blurfl.c
  1166. X.sp
  1167. Xsetting
  1168. X.B \-p
  1169. Xor
  1170. X.B \-p0
  1171. Xgives the entire pathname unmodified,
  1172. X.B \-p1
  1173. Xgives
  1174. X.sp
  1175. X    u/howard/src/blurfl/blurfl.c
  1176. X.sp
  1177. Xwithout the leading slash,
  1178. X.B \-p4
  1179. Xgives
  1180. X.sp
  1181. X    blurfl/blurfl.c
  1182. X.sp
  1183. Xand not specifying
  1184. X.B \-p
  1185. Xat all just gives you "blurfl.c".
  1186. XWhatever you end up with is looked for either in the current directory,
  1187. Xor the directory specified by the
  1188. X.B \-d
  1189. Xswitch.
  1190. X.TP 5
  1191. X.B \-r
  1192. Xcauses the next argument to be interpreted as the reject file name.
  1193. X.TP 5
  1194. X.B \-R
  1195. Xtells
  1196. X.I patch
  1197. Xthat this patch was created with the old and new files swapped.
  1198. X(Yes, I'm afraid that does happen occasionally, human nature being what it
  1199. Xis.)
  1200. X.I Patch
  1201. Xwill attempt to swap each hunk around before applying it.
  1202. XRejects will come out in the swapped format.
  1203. XThe
  1204. X.B \-R
  1205. Xswitch will not work with ed diff scripts because there is too little
  1206. Xinformation to reconstruct the reverse operation.
  1207. X.Sp
  1208. XIf the first hunk of a patch fails,
  1209. X.I patch
  1210. Xwill reverse the hunk to see if it can be applied that way.
  1211. XIf it can, you will be asked if you want to have the
  1212. X.B \-R
  1213. Xswitch set.
  1214. XIf it can't, the patch will continue to be applied normally.
  1215. X(Note: this method cannot detect a reversed patch if it is a normal diff
  1216. Xand if the first command is an append (i.e. it should have been a delete)
  1217. Xsince appends always succeed, due to the fact that a null context will match
  1218. Xanywhere.
  1219. XLuckily, most patches add or change lines rather than delete them, so most
  1220. Xreversed normal diffs will begin with a delete, which will fail, triggering
  1221. Xthe heuristic.)
  1222. X.TP 5
  1223. X.B \-s
  1224. Xmakes
  1225. X.I patch
  1226. Xdo its work silently, unless an error occurs.
  1227. X.TP 5
  1228. X.B \-S
  1229. Xcauses
  1230. X.I patch
  1231. Xto ignore this patch from the patch file, but continue on looking
  1232. Xfor the next patch in the file.
  1233. XThus
  1234. X.sp
  1235. X    patch -S + -S + <patchfile
  1236. X.sp
  1237. Xwill ignore the first and second of three patches.
  1238. X.TP 5
  1239. X.B \-v
  1240. Xcauses
  1241. X.I patch
  1242. Xto print out it's revision header and patch level.
  1243. X.TP 5
  1244. X.B \-x<number>
  1245. Xsets internal debugging flags, and is of interest only to
  1246. X.I patch
  1247. Xpatchers.
  1248. X.SH ENVIRONMENT
  1249. XNo environment variables are used by
  1250. X.IR patch .
  1251. X.SH FILES
  1252. X/tmp/patch*
  1253. X.SH SEE ALSO
  1254. Xdiff(1)
  1255. X.SH NOTES FOR PATCH SENDERS
  1256. XThere are several things you should bear in mind if you are going to
  1257. Xbe sending out patches.
  1258. XFirst, you can save people a lot of grief by keeping a patchlevel.h file
  1259. Xwhich is patched to increment the patch level as the first diff in the
  1260. Xpatch file you send out.
  1261. XIf you put a Prereq: line in with the patch, it won't let them apply
  1262. Xpatches out of order without some warning.
  1263. XSecond, make sure you've specified the filenames right, either in a
  1264. Xcontext diff header, or with an Index: line.
  1265. XIf you are patching something in a subdirectory, be sure to tell the patch
  1266. Xuser to specify a 
  1267. X.B \-p
  1268. Xswitch as needed.
  1269. XThird, you can create a file by sending out a diff that compares a
  1270. Xnull file to the file you want to create.
  1271. XThis will only work if the file you want to create doesn't exist already in
  1272. Xthe target directory.
  1273. XFourth, take care not to send out reversed patches, since it makes people wonder
  1274. Xwhether they already applied the patch.
  1275. XFifth, while you may be able to get away with putting 582 diff listings into
  1276. Xone file, it is probably wiser to group related patches into separate files in
  1277. Xcase something goes haywire.
  1278. X.SH DIAGNOSTICS
  1279. XToo many to list here, but generally indicative that
  1280. X.I patch
  1281. Xcouldn't parse your patch file.
  1282. X.PP
  1283. XThe message \*(L"Hmm...\*(R" indicates that there is unprocessed text in
  1284. Xthe patch file and that
  1285. X.I patch
  1286. Xis attempting to intuit whether there is a patch in that text and, if so,
  1287. Xwhat kind of patch it is.
  1288. X.SH CAVEATS
  1289. X.I Patch
  1290. Xcannot tell if the line numbers are off in an ed script, and can only detect
  1291. Xbad line numbers in a normal diff when it finds a \*(L"change\*(R" or
  1292. Xa \*(L"delete\*(R" command.
  1293. XA context diff using fuzz factor 3 may have the same problem.
  1294. XUntil a suitable interactive interface is added, you should probably do
  1295. Xa context diff in these cases to see if the changes made sense.
  1296. XOf course, compiling without errors is a pretty good indication that the patch
  1297. Xworked, but not always.
  1298. X.PP
  1299. X.I Patch
  1300. Xusually produces the correct results, even when it has to do a lot of
  1301. Xguessing.
  1302. XHowever, the results are guaranteed to be correct only when the patch is
  1303. Xapplied to exactly the same version of the file that the patch was
  1304. Xgenerated from.
  1305. X.SH BUGS
  1306. XCould be smarter about partial matches, excessively \&deviant offsets and
  1307. Xswapped code, but that would take an extra pass.
  1308. X.PP
  1309. XIf code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
  1310. X#endif),
  1311. X.I patch
  1312. Xis incapable of patching both versions, and, if it works at all, will likely
  1313. Xpatch the wrong one, and tell you that it succeeded to boot.
  1314. X.PP
  1315. XIf you apply a patch you've already applied,
  1316. X.I patch
  1317. Xwill think it is a reversed patch, and offer to un-apply the patch.
  1318. XThis could be construed as a feature.
  1319. !STUFFY!FUNK!
  1320. echo Extracting inp.c
  1321. sed >inp.c <<'!STUFFY!FUNK!' -e 's/X//'
  1322. X/* $Header: inp.c,v 2.0 86/09/17 15:37:02 lwall Exp $
  1323. X *
  1324. X * $Log:    inp.c,v $
  1325. X * Revision 2.0  86/09/17  15:37:02  lwall
  1326. X * Baseline for netwide release.
  1327. X * 
  1328. X */
  1329. X
  1330. X#include "EXTERN.h"
  1331. X#include "common.h"
  1332. X#include "util.h"
  1333. X#include "pch.h"
  1334. X#include "INTERN.h"
  1335. X#include "inp.h"
  1336. X
  1337. X/* Input-file-with-indexable-lines abstract type */
  1338. X
  1339. Xstatic long i_size;            /* size of the input file */
  1340. Xstatic char *i_womp;            /* plan a buffer for entire file */
  1341. Xstatic char **i_ptr;            /* pointers to lines in i_womp */
  1342. X
  1343. Xstatic int tifd = -1;            /* plan b virtual string array */
  1344. Xstatic char *tibuf[2];            /* plan b buffers */
  1345. Xstatic LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  1346. Xstatic LINENUM lines_per_buf;        /* how many lines per buffer */
  1347. Xstatic int tireclen;            /* length of records in tmp file */
  1348. X
  1349. X/* New patch--prepare to edit another file. */
  1350. X
  1351. Xvoid
  1352. Xre_input()
  1353. X{
  1354. X    if (using_plan_a) {
  1355. X    i_size = 0;
  1356. X#ifndef lint
  1357. X    if (i_ptr != Null(char**))
  1358. X        free((char *)i_ptr);
  1359. X#endif
  1360. X    if (i_womp != Nullch)
  1361. X        free(i_womp);
  1362. X    i_womp = Nullch;
  1363. X    i_ptr = Null(char **);
  1364. X    }
  1365. X    else {
  1366. X    using_plan_a = TRUE;        /* maybe the next one is smaller */
  1367. X    Close(tifd);
  1368. X    tifd = -1;
  1369. X    free(tibuf[0]);
  1370. X    free(tibuf[1]);
  1371. X    tibuf[0] = tibuf[1] = Nullch;
  1372. X    tiline[0] = tiline[1] = -1;
  1373. X    tireclen = 0;
  1374. X    }
  1375. X}
  1376. X
  1377. X/* Constuct the line index, somehow or other. */
  1378. X
  1379. Xvoid
  1380. Xscan_input(filename)
  1381. Xchar *filename;
  1382. X{
  1383. X    if (!plan_a(filename))
  1384. X    plan_b(filename);
  1385. X    if (verbose) {
  1386. X    say3("Patching file %s using Plan %s...\n", filename,
  1387. X      (using_plan_a ? "A" : "B") );
  1388. X    }
  1389. X}
  1390. X
  1391. X/* Try keeping everything in memory. */
  1392. X
  1393. Xbool
  1394. Xplan_a(filename)
  1395. Xchar *filename;
  1396. X{
  1397. X    int ifd;
  1398. X    Reg1 char *s;
  1399. X    Reg2 LINENUM iline;
  1400. X
  1401. X    if (ok_to_create_file && stat(filename, &filestat) < 0) {
  1402. X    if (verbose)
  1403. X        say2("(Creating file %s...)\n",filename);
  1404. X    makedirs(filename, TRUE);
  1405. X    close(creat(filename, 0666));
  1406. X    }
  1407. X    if (stat(filename, &filestat) < 0) {
  1408. X    Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  1409. X    if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  1410. X        Sprintf(buf, CHECKOUT, filename);
  1411. X        if (verbose)
  1412. X        say2("Can't find %s--attempting to check it out from RCS.\n",
  1413. X            filename);
  1414. X        if (system(buf) || stat(filename, &filestat))
  1415. X        fatal2("Can't check out %s.\n", filename);
  1416. X    }
  1417. X    else {
  1418. X        Sprintf(buf, "SCCS/%s%s", SCCSPREFIX, filename);
  1419. X        if (stat(buf, &filestat) >= 0 || stat(buf+5, &filestat) >= 0) {
  1420. X        Sprintf(buf, GET, filename);
  1421. X        if (verbose)
  1422. X            say2("Can't find %s--attempting to get it from SCCS.\n",
  1423. X            filename);
  1424. X        if (system(buf) || stat(filename, &filestat))
  1425. X            fatal2("Can't get %s.\n", filename);
  1426. X        }
  1427. X        else
  1428. X        fatal2("Can't find %s.\n", filename);
  1429. X    }
  1430. X    }
  1431. X    filemode = filestat.st_mode;
  1432. X    if ((filemode & S_IFMT) & ~S_IFREG)
  1433. X    fatal2("%s is not a normal file--can't patch.\n", filename);
  1434. X    i_size = filestat.st_size;
  1435. X    if (out_of_mem) {
  1436. X    set_hunkmax();        /* make sure dynamic arrays are allocated */
  1437. X    out_of_mem = FALSE;
  1438. X    return FALSE;            /* force plan b because plan a bombed */
  1439. X    }
  1440. X#ifdef lint
  1441. X    i_womp = Nullch;
  1442. X#else
  1443. X    i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  1444. X                    /* i_size, but that's okay, I think. */
  1445. X#endif
  1446. X    if (i_womp == Nullch)
  1447. X    return FALSE;
  1448. X    if ((ifd = open(filename, 0)) < 0)
  1449. X    fatal2("Can't open file %s\n", filename);
  1450. X#ifndef lint
  1451. X    if (read(ifd, i_womp, (int)i_size) != i_size) {
  1452. X    Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  1453. X    free(i_womp);    /* at this point it doesn't matter if i_womp was */
  1454. X    return FALSE;    /*   undersized. */
  1455. X    }
  1456. X#endif
  1457. X    Close(ifd);
  1458. X    if (i_size && i_womp[i_size-1] != '\n')
  1459. X    i_womp[i_size++] = '\n';
  1460. X    i_womp[i_size] = '\0';
  1461. X
  1462. X    /* count the lines in the buffer so we know how many pointers we need */
  1463. X
  1464. X    iline = 0;
  1465. X    for (s=i_womp; *s; s++) {
  1466. X    if (*s == '\n')
  1467. X        iline++;
  1468. X    }
  1469. X#ifdef lint
  1470. X    i_ptr = Null(char**);
  1471. X#else
  1472. X    i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  1473. X#endif
  1474. X    if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  1475. X    free((char *)i_womp);
  1476. X    return FALSE;
  1477. X    }
  1478. X    
  1479. X    /* now scan the buffer and build pointer array */
  1480. X
  1481. X    iline = 1;
  1482. X    i_ptr[iline] = i_womp;
  1483. X    for (s=i_womp; *s; s++) {
  1484. X    if (*s == '\n')
  1485. X        i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  1486. X    }
  1487. X    input_lines = iline - 1;
  1488. X
  1489. X    /* now check for revision, if any */
  1490. X
  1491. X    if (revision != Nullch) { 
  1492. X    if (!rev_in_string(i_womp)) {
  1493. X        if (force) {
  1494. X        if (verbose)
  1495. X            say2("\
  1496. XWarning: this file doesn't appear to be the %s version--patching anyway.\n",
  1497. X            revision);
  1498. X        }
  1499. X        else {
  1500. X        ask2("\
  1501. XThis file doesn't appear to be the %s version--patch anyway? [n] ",
  1502. X            revision);
  1503. X        if (*buf != 'y')
  1504. X        fatal1("Aborted.\n");
  1505. X        }
  1506. X    }
  1507. X    else if (verbose)
  1508. X        say2("Good.  This file appears to be the %s version.\n",
  1509. X        revision);
  1510. X    }
  1511. X    return TRUE;            /* plan a will work */
  1512. X}
  1513. X
  1514. X/* Keep (virtually) nothing in memory. */
  1515. X
  1516. Xvoid
  1517. Xplan_b(filename)
  1518. Xchar *filename;
  1519. X{
  1520. X    Reg3 FILE *ifp;
  1521. X    Reg1 int i = 0;
  1522. X    Reg2 int maxlen = 1;
  1523. X    Reg4 bool found_revision = (revision == Nullch);
  1524. X
  1525. X    using_plan_a = FALSE;
  1526. X    if ((ifp = fopen(filename, "r")) == Nullfp)
  1527. X    fatal2("Can't open file %s\n", filename);
  1528. X    if ((tifd = creat(TMPINNAME, 0666)) < 0)
  1529. X    fatal2("Can't open file %s\n", TMPINNAME);
  1530. X    while (fgets(buf, sizeof buf, ifp) != Nullch) {
  1531. X    if (revision != Nullch && !found_revision && rev_in_string(buf))
  1532. X        found_revision = TRUE;
  1533. X    if ((i = strlen(buf)) > maxlen)
  1534. X        maxlen = i;            /* find longest line */
  1535. X    }
  1536. X    if (revision != Nullch) {
  1537. X    if (!found_revision) {
  1538. X        if (force) {
  1539. X        if (verbose)
  1540. X            say2("\
  1541. XWarning: this file doesn't appear to be the %s version--patching anyway.\n",
  1542. X            revision);
  1543. X        }
  1544. X        else {
  1545. X        ask2("\
  1546. XThis file doesn't appear to be the %s version--patch anyway? [n] ",
  1547. X            revision);
  1548. X        if (*buf != 'y')
  1549. X            fatal1("Aborted.\n");
  1550. X        }
  1551. X    }
  1552. X    else if (verbose)
  1553. X        say2("Good.  This file appears to be the %s version.\n",
  1554. X        revision);
  1555. X    }
  1556. X    Fseek(ifp, 0L, 0);        /* rewind file */
  1557. X    lines_per_buf = BUFFERSIZE / maxlen;
  1558. X    tireclen = maxlen;
  1559. X    tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  1560. X    tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  1561. X    if (tibuf[1] == Nullch)
  1562. X    fatal1("Can't seem to get enough memory.\n");
  1563. X    for (i=1; ; i++) {
  1564. X    if (! (i % lines_per_buf))    /* new block */
  1565. X        if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  1566. X        fatal1("patch: can't write temp file.\n");
  1567. X    if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  1568. X      == Nullch) {
  1569. X        input_lines = i - 1;
  1570. X        if (i % lines_per_buf)
  1571. X        if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  1572. X            fatal1("patch: can't write temp file.\n");
  1573. X        break;
  1574. X    }
  1575. X    }
  1576. X    Fclose(ifp);
  1577. X    Close(tifd);
  1578. X    if ((tifd = open(TMPINNAME, 0)) < 0) {
  1579. X    fatal2("Can't reopen file %s\n", TMPINNAME);
  1580. X    }
  1581. X}
  1582. X
  1583. X/* Fetch a line from the input file, \n terminated, not necessarily \0. */
  1584. X
  1585. Xchar *
  1586. Xifetch(line,whichbuf)
  1587. XReg1 LINENUM line;
  1588. Xint whichbuf;                /* ignored when file in memory */
  1589. X{
  1590. X    if (line < 1 || line > input_lines)
  1591. X    return "";
  1592. X    if (using_plan_a)
  1593. X    return i_ptr[line];
  1594. X    else {
  1595. X    LINENUM offline = line % lines_per_buf;
  1596. X    LINENUM baseline = line - offline;
  1597. X
  1598. X    if (tiline[0] == baseline)
  1599. X        whichbuf = 0;
  1600. X    else if (tiline[1] == baseline)
  1601. X        whichbuf = 1;
  1602. X    else {
  1603. X        tiline[whichbuf] = baseline;
  1604. X#ifndef lint        /* complains of long accuracy */
  1605. X        Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  1606. X#endif
  1607. X        if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  1608. X        fatal2("Error reading tmp file %s.\n", TMPINNAME);
  1609. X    }
  1610. X    return tibuf[whichbuf] + (tireclen*offline);
  1611. X    }
  1612. X}
  1613. X
  1614. X/* True if the string argument contains the revision number we want. */
  1615. X
  1616. Xbool
  1617. Xrev_in_string(string)
  1618. Xchar *string;
  1619. X{
  1620. X    Reg1 char *s;
  1621. X    Reg2 int patlen;
  1622. X
  1623. X    if (revision == Nullch)
  1624. X    return TRUE;
  1625. X    patlen = strlen(revision);
  1626. X    for (s = string; *s; s++) {
  1627. X    if (isspace(*s) && strnEQ(s+1, revision, patlen) && 
  1628. X        isspace(s[patlen+1] )) {
  1629. X        return TRUE;
  1630. X    }
  1631. X    }
  1632. X    return FALSE;
  1633. X}
  1634. X
  1635. !STUFFY!FUNK!
  1636. echo Extracting util.c
  1637. sed >util.c <<'!STUFFY!FUNK!' -e 's/X//'
  1638. X#include "EXTERN.h"
  1639. X#include "common.h"
  1640. X#include "INTERN.h"
  1641. X#include "util.h"
  1642. X
  1643. X/* Rename a file, copying it if necessary. */
  1644. X
  1645. Xint
  1646. Xmove_file(from,to)
  1647. Xchar *from, *to;
  1648. X{
  1649. X    char bakname[512];
  1650. X    Reg1 char *s;
  1651. X    Reg2 int i;
  1652. X    Reg3 int fromfd;
  1653. X
  1654. X    /* to stdout? */
  1655. X
  1656. X    if (strEQ(to, "-")) {
  1657. X#ifdef DEBUGGING
  1658. X    if (debug & 4)
  1659. X        say2("Moving %s to stdout.\n", from);
  1660. X#endif
  1661. X    fromfd = open(from, 0);
  1662. X    if (fromfd < 0)
  1663. X        fatal2("patch: internal error, can't reopen %s\n", from);
  1664. X    while ((i=read(fromfd, buf, sizeof buf)) > 0)
  1665. X        if (write(1, buf, i) != 1)
  1666. X        fatal1("patch: write failed\n");
  1667. X    Close(fromfd);
  1668. X    return 0;
  1669. X    }
  1670. X
  1671. X    Strcpy(bakname, to);
  1672. X    Strcat(bakname, origext?origext:ORIGEXT);
  1673. X    if (stat(to, &filestat) >= 0) {    /* output file exists */
  1674. X    dev_t to_device = filestat.st_dev;
  1675. X    ino_t to_inode  = filestat.st_ino;
  1676. X    char *simplename = bakname;
  1677. X    
  1678. X    for (s=bakname; *s; s++) {
  1679. X        if (*s == '/')
  1680. X        simplename = s+1;
  1681. X    }
  1682. X    /* find a backup name that is not the same file */
  1683. X    while (stat(bakname, &filestat) >= 0 &&
  1684. X        to_device == filestat.st_dev && to_inode == filestat.st_ino) {
  1685. X        for (s=simplename; *s && !islower(*s); s++) ;
  1686. X        if (*s)
  1687. X        *s = toupper(*s);
  1688. X        else
  1689. X        Strcpy(simplename, simplename+1);
  1690. X    }
  1691. X    while (unlink(bakname) >= 0) ;    /* while() is for benefit of Eunice */
  1692. X#ifdef DEBUGGING
  1693. X    if (debug & 4)
  1694. X        say3("Moving %s to %s.\n", to, bakname);
  1695. X#endif
  1696. X    if (link(to, bakname) < 0) {
  1697. X        say3("patch: can't backup %s, output is in %s\n",
  1698. X        to, from);
  1699. X        return -1;
  1700. X    }
  1701. X    while (unlink(to) >= 0) ;
  1702. X    }
  1703. X#ifdef DEBUGGING
  1704. X    if (debug & 4)
  1705. X    say3("Moving %s to %s.\n", from, to);
  1706. X#endif
  1707. X    if (link(from, to) < 0) {        /* different file system? */
  1708. X    Reg4 int tofd;
  1709. X    
  1710. X    tofd = creat(to, 0666);
  1711. X    if (tofd < 0) {
  1712. X        say3("patch: can't create %s, output is in %s.\n",
  1713. X          to, from);
  1714. X        return -1;
  1715. X    }
  1716. X    fromfd = open(from, 0);
  1717. X    if (fromfd < 0)
  1718. X        fatal2("patch: internal error, can't reopen %s\n", from);
  1719. X    while ((i=read(fromfd, buf, sizeof buf)) > 0)
  1720. X        if (write(tofd, buf, i) != i)
  1721. X        fatal1("patch: write failed\n");
  1722. X    Close(fromfd);
  1723. X    Close(tofd);
  1724. X    }
  1725. X    Unlink(from);
  1726. X    return 0;
  1727. X}
  1728. X
  1729. X/* Copy a file. */
  1730. X
  1731. Xvoid
  1732. Xcopy_file(from,to)
  1733. Xchar *from, *to;
  1734. X{
  1735. X    Reg3 int tofd;
  1736. X    Reg2 int fromfd;
  1737. X    Reg1 int i;
  1738. X    
  1739. X    tofd = creat(to, 0666);
  1740. X    if (tofd < 0)
  1741. X    fatal2("patch: can't create %s.\n", to);
  1742. X    fromfd = open(from, 0);
  1743. X    if (fromfd < 0)
  1744. X    fatal2("patch: internal error, can't reopen %s\n", from);
  1745. X    while ((i=read(fromfd, buf, sizeof buf)) > 0)
  1746. X    if (write(tofd, buf, i) != i)
  1747. X        fatal2("patch: write (%s) failed\n", to);
  1748. X    Close(fromfd);
  1749. X    Close(tofd);
  1750. X}
  1751. X
  1752. X/* Allocate a unique area for a string. */
  1753. X
  1754. Xchar *
  1755. Xsavestr(s)
  1756. XReg1 char *s;
  1757. X{
  1758. X    Reg3 char *rv;
  1759. X    Reg2 char *t;
  1760. X
  1761. X    if (!s)
  1762. X    s = "Oops";
  1763. X    t = s;
  1764. X    while (*t++);
  1765. X    rv = malloc((MEM) (t - s));
  1766. X    if (rv == Nullch) {
  1767. X    if (using_plan_a)
  1768. X        out_of_mem = TRUE;
  1769. X    else
  1770. X        fatal1("patch: out of memory (savestr)\n");
  1771. X    }
  1772. X    else {
  1773. X    t = rv;
  1774. X    while (*t++ = *s++);
  1775. X    }
  1776. X    return rv;
  1777. X}
  1778. X
  1779. X#if defined(lint) && defined(CANVARARG)
  1780. X
  1781. X/*VARARGS ARGSUSED*/
  1782. Xsay(pat) char *pat; { ; }
  1783. X/*VARARGS ARGSUSED*/
  1784. Xfatal(pat) char *pat; { ; }
  1785. X/*VARARGS ARGSUSED*/
  1786. Xask(pat) char *pat; { ; }
  1787. X
  1788. X#else
  1789. X
  1790. X/* Vanilla terminal output (buffered). */
  1791. X
  1792. Xvoid
  1793. Xsay(pat,arg1,arg2,arg3)
  1794. Xchar *pat;
  1795. Xint arg1,arg2,arg3;
  1796. X{
  1797. X    fprintf(stderr, pat, arg1, arg2, arg3);
  1798. X    Fflush(stderr);
  1799. X}
  1800. X
  1801. X/* Terminal output, pun intended. */
  1802. X
  1803. Xvoid                /* very void */
  1804. Xfatal(pat,arg1,arg2,arg3)
  1805. Xchar *pat;
  1806. Xint arg1,arg2,arg3;
  1807. X{
  1808. X    void my_exit();
  1809. X
  1810. X    say(pat, arg1, arg2, arg3);
  1811. X    my_exit(1);
  1812. X}
  1813. X
  1814. X/* Get a response from the user, somehow or other. */
  1815. X
  1816. Xvoid
  1817. Xask(pat,arg1,arg2,arg3)
  1818. Xchar *pat;
  1819. Xint arg1,arg2,arg3;
  1820. X{
  1821. X    int ttyfd;
  1822. X    int r;
  1823. X    bool tty2 = isatty(2);
  1824. X
  1825. X    Sprintf(buf, pat, arg1, arg2, arg3);
  1826. X    Fflush(stderr);
  1827. X    write(2, buf, strlen(buf));
  1828. X    if (tty2) {                /* might be redirected to a file */
  1829. X    r = read(2, buf, sizeof buf);
  1830. X    }
  1831. X    else if (isatty(1)) {        /* this may be new file output */
  1832. X    Fflush(stdout);
  1833. X    write(1, buf, strlen(buf));
  1834. X    r = read(1, buf, sizeof buf);
  1835. X    }
  1836. X    else if ((ttyfd = open("/dev/tty", 2)) >= 0 && isatty(ttyfd)) {
  1837. X                    /* might be deleted or unwriteable */
  1838. X    write(ttyfd, buf, strlen(buf));
  1839. X    r = read(ttyfd, buf, sizeof buf);
  1840. X    Close(ttyfd);
  1841. X    }
  1842. X    else if (isatty(0)) {        /* this is probably patch input */
  1843. X    Fflush(stdin);
  1844. X    write(0, buf, strlen(buf));
  1845. X    r = read(0, buf, sizeof buf);
  1846. X    }
  1847. X    else {                /* no terminal at all--default it */
  1848. X    buf[0] = '\n';
  1849. X    r = 1;
  1850. X    }
  1851. X    if (r <= 0)
  1852. X    buf[0] = 0;
  1853. X    else
  1854. X    buf[r] = '\0';
  1855. X    if (!tty2)
  1856. X    say1(buf);
  1857. X}
  1858. X#endif lint
  1859. X
  1860. X/* How to handle certain events when not in a critical region. */
  1861. X
  1862. Xvoid
  1863. Xset_signals()
  1864. X{
  1865. X    void my_exit();
  1866. X
  1867. X#ifndef lint
  1868. X    if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  1869. X    Signal(SIGHUP, my_exit);
  1870. X    if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  1871. X    Signal(SIGINT, my_exit);
  1872. X#endif
  1873. X}
  1874. X
  1875. X/* How to handle certain events when in a critical region. */
  1876. X
  1877. Xvoid
  1878. Xignore_signals()
  1879. X{
  1880. X#ifndef lint
  1881. X    Signal(SIGHUP, SIG_IGN);
  1882. X    Signal(SIGINT, SIG_IGN);
  1883. X#endif
  1884. X}
  1885. X
  1886. X/* Make sure we'll have the directories to create a file. */
  1887. X
  1888. Xvoid
  1889. Xmakedirs(filename,striplast)
  1890. XReg1 char *filename;
  1891. Xbool striplast;
  1892. X{
  1893. X    char tmpbuf[256];
  1894. X    Reg2 char *s = tmpbuf;
  1895. X    char *dirv[20];
  1896. X    Reg3 int i;
  1897. X    Reg4 int dirvp = 0;
  1898. X
  1899. X    while (*filename) {
  1900. X    if (*filename == '/') {
  1901. X        filename++;
  1902. X        dirv[dirvp++] = s;
  1903. X        *s++ = '\0';
  1904. X    }
  1905. X    else {
  1906. X        *s++ = *filename++;
  1907. X    }
  1908. X    }
  1909. X    *s = '\0';
  1910. X    dirv[dirvp] = s;
  1911. X    if (striplast)
  1912. X    dirvp--;
  1913. X    if (dirvp < 0)
  1914. X    return;
  1915. X    strcpy(buf, "mkdir");
  1916. X    s = buf;
  1917. X    for (i=0; i<=dirvp; i++) {
  1918. X    while (*s) s++;
  1919. X    *s++ = ' ';
  1920. X    strcpy(s, tmpbuf);
  1921. X    *dirv[i] = '/';
  1922. X    }
  1923. X    system(buf);
  1924. X}
  1925. X
  1926. X/* Make filenames more reasonable. */
  1927. X
  1928. Xchar *
  1929. Xfetchname(at,strip_leading,assume_exists)
  1930. Xchar *at;
  1931. Xint strip_leading;
  1932. Xint assume_exists;
  1933. X{
  1934. X    char *s;
  1935. X    char *name;
  1936. X    Reg1 char *t;
  1937. X    char tmpbuf[200];
  1938. X
  1939. X    if (!at)
  1940. X    return Nullch;
  1941. X    s = savestr(at);
  1942. X    for (t=s; isspace(*t); t++) ;
  1943. X    name = t;
  1944. X#ifdef DEBUGGING
  1945. X    if (debug & 128)
  1946. X    say4("fetchname %s %d %d\n",name,strip_leading,assume_exists);
  1947. X#endif
  1948. X    if (strnEQ(name, "/dev/null", 9))    /* so files can be created by diffing */
  1949. X    return Nullch;            /*   against /dev/null. */
  1950. X    for (; *t && !isspace(*t); t++)
  1951. X    if (*t == '/')
  1952. X        if (--strip_leading >= 0)
  1953. X        name = t+1;
  1954. X    *t = '\0';
  1955. X    if (name != s && *s != '/') {
  1956. X    name[-1] = '\0';
  1957. X    if (stat(s, &filestat) && filestat.st_mode & S_IFDIR) {
  1958. X        name[-1] = '/';
  1959. X        name=s;
  1960. X    }
  1961. X    }
  1962. X    name = savestr(name);
  1963. X    Sprintf(tmpbuf, "RCS/%s", name);
  1964. X    free(s);
  1965. X    if (stat(name, &filestat) < 0 && !assume_exists) {
  1966. X    Strcat(tmpbuf, RCSSUFFIX);
  1967. X    if (stat(tmpbuf, &filestat) < 0 && stat(tmpbuf+4, &filestat) < 0) {
  1968. X        Sprintf(tmpbuf, "SCCS/%s%s", SCCSPREFIX, name);
  1969. X        if (stat(tmpbuf, &filestat) < 0 && stat(tmpbuf+5, &filestat) < 0) {
  1970. X        free(name);
  1971. X        name = Nullch;
  1972. X        }
  1973. X    }
  1974. X    }
  1975. X    return name;
  1976. X}
  1977. !STUFFY!FUNK!
  1978. echo ""
  1979. echo "End of kit 2 (of 3)"
  1980. cat /dev/null >kit2isdone
  1981. config=true
  1982. for iskit in 1 2 3; do
  1983.     if test -f kit${iskit}isdone; then
  1984.     echo "You have run kit ${iskit}."
  1985.     else
  1986.     echo "You still need to run kit ${iskit}."
  1987.     config=false
  1988.     fi
  1989. done
  1990. case $config in
  1991.     true)
  1992.     echo "You have run all your kits.  Please read README and then type Configure."
  1993.     chmod 755 Configure
  1994.     ;;
  1995. esac
  1996. : I do not append .signature, but someone might mail this.
  1997. exit
  1998.  
  1999.