fix. (added missing /)
[rrq/newlisp/packnl.git] / incore.lsp
1 ;; This code is intended for an embedded binary with an "archive" of
2 ;; files appended to it. The appended archive is marked by a array of
3 ;; 40 "x", and consists by archive elements the format of
4 ;; "pathname\nsize\nbytes[size]".
5 ;;
6 ;; When there is an in-core archive, then the first member will be
7 ;; invoked as "main script".
8 ;;
9 ;; archives:archives is a hashmap for loadable files
10 ;; core:core is the running binary file
11 ;;
12 ;; The "archive" context holds utility code.
13 ;; archive:main is the name of the first in-core member.
14 ;; archive:incore is the byte position into core for the archive.
15 ;;
16 ;; Use (archives member pathname) to register that the file "member"
17 ;; should be looked up in the ar style archive of the given pathname.
18 ;;
19 ;; Use (archive pathname) to register all members of the nominated ar
20 ;; style archive.
21
22 ;; This is a hashmap of loaded files
23 (new Tree 'archives)
24
25 ;; A copy of the executable
26 (define core:core (read-file "/proc/self/exe"))
27
28 (context 'archive)
29
30 (setf main nil)
31
32 (constant 'AR "/usr/bin/ar" 'TAR "/bin/tar" 'FILE "/usr/bin/file")
33
34 (define (tar-able PATH)
35   (when (file? FILE)
36     (if (exec (format "%s -Z %s" FILE PATH))
37         (find " tar archive" ($it 0)))))
38
39 ;; Install indexes for all members of the given archive file
40 (define (archive:archive PATH)
41   ;;(write-line 2 (string (list 'archive PATH)))
42   (map (fn (F) (archives F PATH) F)
43        (if (directory? PATH) (directory PATH "^[^.]")
44          (ends-with PATH ".a") (exec (format "%s t %s" AR PATH))
45          (tar-able PATH) (exec (format "%s tf %s" TAR PATH))
46          '()
47          )))
48
49 ;; Read a member of an external archive
50 (define (get-stdout CMD)
51   (let ((I (pipe)) (O (pipe)) (DATA "") (ALL "")
52         (SUB (fn (I O)
53                (map close (list (I 1) (O 0) 0))
54                (process CMD (I 0) (O 1)))))
55     (fork (SUB I O))
56     (close (O 1)) (close (I 0)) (close (I 1))
57     (while (> (or (read (O 0) DATA 1000000) 0)) (extend ALL DATA))
58     (close (I 0))
59     ALL))
60
61 ;; Read an archive member. The given PATH is either a string path for
62 ;; an external archive file or the (position length) list for an
63 ;; incore packaged member.
64 (define (get PATH MEMBER)
65   (if (list? PATH) ((PATH 0) (PATH 1) core)
66     (directory? PATH) (read-file (format "%s/%s" PATH MEMBER))
67     (ends-with PATH ".a") (get-stdout (format "%s p %s %s" AR PATH MEMBER))
68     (tar-able PATH) (get-stdout (format "%s xOf %s %s" TAR PATH MEMBER))
69     ))
70
71 ;; Discover and load an in-core archive by means of a marker row of 40
72 ;; "x", and then a series of pathname\nsize\nbytes[size] members.
73 (if (find (dup "x" 40) core nil 391704)
74     (let ((P (+ $it 41))) ; skip marker and newline
75       (constant 'incore P)
76       (while (regex "([^\n]+)\n([^\n]+)\n" core 0 P)
77         (let ((N $1) (S (int $2)) (L (length $0)))
78           (inc P L)
79           (unless main (setf main N))
80           (archives N (list P S))
81           (inc P S))))
82   (write-line 2 (string "not packed")))
83
84 (context MAIN)
85
86 (constant
87    'load
88    (letex ((LOAD load) (CTX '(if (1 (args)) (args 1) MAIN)))
89      (fn () (if (archives (args 0))
90                 (eval-string (archive:get $it (args 0)) CTX)
91               (LOAD (args 0) (eval CTX)))))
92    'read-file
93    (letex ((READ-FILE read-file) (CTX '(if (1 (args)) (args 1) MAIN)))
94      (fn ()
95        (if (archives (args 0)) (archive:get $it (args 0) CTX)
96          (READ-FILE (args 0)))))
97    'file?
98    (letex ((FILE? file?))
99      (fn () (if (archives (args 0)) true (FILE? (args 0)))))
100    )
101
102 (when archive:main (load archive:main) (exit 0))