home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / PATCH.ICN < prev    next >
Text File  |  1991-07-13  |  2KB  |  89 lines

  1. ############################################################################
  2. #
  3. #    Name:    patch.icn
  4. #
  5. #    Title:    UNIX-like patch(1) procedure
  6. #
  7. #    Author:    Rich Morin
  8. #
  9. #    Date:    June 18, 1990
  10. #
  11. ############################################################################
  12. #
  13. #  This procedure produces a sequence of edited items, reading a source
  14. #  stream (from) and a stream of difference records (diffs), as generated
  15. #  by dif.icn.
  16. #
  17. #  An optional parameter (rev) causes the edits to be made in reverse.
  18. #  This allows an old stream to be regenerated from a new stream and an
  19. #  appropriate stream of difference records.
  20. #
  21. #  The original patch(1) utility was written by Larry Wall, and is used
  22. #  widely in the UNIX community.  See also diffu.icn and patchu.icn, the
  23. #  utility program versions of dif.icn and patch.icn.
  24. #
  25. #  Usage:    patch(old, diff)    # patch old to new via diff
  26. #          patch(new, diff, rev)    # patch new to old via diff
  27. #
  28. ############################################################################
  29. #
  30. #  Requires: co-expressions
  31. #
  32. ############################################################################
  33.  
  34. procedure patch(from, diff, rev)
  35.   local c_diff, c_from, cnte, cnti, i, item, ldr, o
  36.  
  37.   initial {
  38.     i := 1
  39.     o := 2
  40.     if \rev then
  41.       i :=: o
  42.       
  43.     c_diff := create !diff
  44.     c_from := create !from
  45.  
  46.     cnti := item := 0
  47.     ldr  := @c_diff
  48.     cnte := ldr[i].pos
  49.   }
  50.  
  51.   repeat {
  52.  
  53.     while /ldr | cnti < cnte-1 do {        # copy old items
  54.       cnti +:= 1
  55.       if item := @c_from then
  56.         suspend item
  57.       else {
  58.         item := &null
  59.         break
  60.       }
  61.     }
  62.  
  63.     if \ldr then {                # still have edits
  64.       every 1 to *ldr[i].diffs do {        # discard items
  65.         cnti +:= 1
  66.         @c_from | zot_patch("unexpected end of stream")
  67.       }
  68.  
  69.       if *ldr[o].diffs > 0 then            # copy new items
  70.         suspend !ldr[o].diffs
  71.  
  72.       if ldr := @c_diff then            # get next edit
  73.         cnte := ldr[i].pos
  74.       else
  75.         ldr := &null
  76.     }
  77.  
  78.     if /item & /ldr then
  79.       fail
  80.   }
  81.  
  82. end
  83.  
  84.  
  85. procedure zot_patch(msg)            # exit w/ message
  86.   write(&errout, "patch: ", msg)
  87.   exit(1)
  88. end
  89.