summaryrefslogtreecommitdiff
path: root/ipl/progs/ipatch.icn
blob: d234d6b8841d8c997319c39dfdb5e6d767af0cb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
############################################################################
#
#	File:     ipatch.icn
#
#	Subject:  Program to patch iconx path in executable
#
#	Author:   Gregg M. Townsend
#
#	Date:     November 15, 2000
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Usage:  ipatch file path
#
#     Ipatch changes the path to iconx, the Icon interpreter, that is
#  embedded in an Icon executable file under Unix.  Icon 9.4 headers are
#  rewritten in the same form.  Because headers from earlier versions of
#  Icon contain no room for expansion, they are rewritten in a different
#  form to accommodate a possibly-longer path.
#
############################################################################
#
#  Requires:  Unix
#
############################################################################

procedure main(args)
   local fname, path, f, header, hlength, pfx

   if *args ~= 2 then
      stop("usage: ", &progname, " file iconx")
   fname := get(args)
   path := get(args)

   f := open(fname, "rwu") | stop("cannot open ", fname, " for writing")
   header := reads(f, 1000) | stop(fname, ": empty file")

   header ? {
     (tab(find("\n[executable Icon binary follows]\n")) & tab(find("\f\n\0"))) |
         stop(fname, ": not an Icon executable")
      hlength := &pos - 1
      tab(1)
      if pfx := tab(find("IXBIN=") + 6) then {
         # Icon 9.4 or later binary
         tab(upto('\n'))
         header := pfx || path || tab(hlength + 1)
         }
      else {
         # Icon 9.3 or earlier binary
         header := "#!/bin/sh" ||
            "\n" ||
            "\nexec ${ICONX-" || path || "} $0 ${1+\"$@\"}" ||
            "\n\n\n\n\n" ||
            "\n[executable Icon binary follows]" ||	# must appear exactly
            "\n"
         }
      }

   if *header + 3 > hlength then
      stop("cannot patch: path is too long to fit")

   if not close(open(path)) then
      write(&errout, "warning: cannot open ", path, "; patching anyway")

   seek(f, 1) | stop("cannot reposition ", fname)
   writes(f, left(header, hlength)) | stop("write failed")
end