9e77db93efe06db5c81e050265abcc7f3dd561b7
[rrq/lsp-utils.git] / lsp-misc / misc.lsp
1 (define (prog1 X) X)
2 (global 'prog1)
3
4 (define (die N)
5   (when (args) (write-line 2 (join (map string (args)) " ")))
6   (and N (exit N)))
7 (global 'die)
8
9 (define (char2hex STR)
10   (join (map (curry format "%2x") (map char (explode STR)))))
11 (global 'char2hex)
12
13 ;; Print binary byte as octal or as ASCII character [32-126]
14 (define (octal-byte x)
15   (if (and (> x 31) (< x 127)) (char x) (format "\\%o" x)))
16 (global 'octal-byte)
17
18 ;; Print string as binary octals
19 (define (octals-string S)
20   (join (map octal-byte (unpack (dup "b" (length S)) S))))
21 (global 'octals-string)
22
23 ;; Return byte code as printable or as code.
24 (define (human-byte B)
25   (if (and (> B 32) (< B 127)) (char B) B))
26 (global 'human-byte)
27
28 ;; Return a packed encoding of a list of bytes, joining string elements
29 (define (human-bytes BL)
30   (let ((OUT '()) (X nil))
31     (dolist (B (map human-byte BL))
32       (if (string? B) (if X (extend X B) (setf X B))
33         (begin (when (string? X) (push X OUT -1))
34              (push B OUT -1)
35              (setf X nil))))
36     (when (string? X) (push X OUT -1))
37     OUT))
38 (global 'human-bytes)
39
40 "misc.lsp"