-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.scm
More file actions
745 lines (634 loc) · 18.6 KB
/
support.scm
File metadata and controls
745 lines (634 loc) · 18.6 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
;;;; support-library for bones
;
; - needs match.scm and pp.scm
(define (id x) x)
(define (read-forms file-or-port . reader)
((if (string? file-or-port)
call-with-input-file
(lambda (fp p) (p fp)))
file-or-port
(let ((rd (optional reader read)))
(lambda (port)
(let loop ((xs '()))
(let ((x (rd port)))
(if (eof-object? x)
`(begin ,@(reverse xs))
(loop (cons x xs)))))))))
(define (emit . xs)
(for-each display xs))
(define (stringify x)
(cond ((symbol? x) (symbol->string x))
((string? x) x)
((number? x) (number->string x))
((char? x) (string x))
(else (error "can't stringify" x))))
(define (symbolify x)
(cond ((symbol? x) x)
((string? x) (string->symbol x))
((char? x) (string->symbol (string x)))
(else (error "can't symbolify" x))))
(define (listify x)
(if (list? x)
x
(list x)))
(define (join xs . sep)
(let ((sep (optional sep ""))
(out (open-output-string)))
(let loop ((xs xs))
(cond ((null? xs) "")
((null? (cdr xs))
(display (car xs) out)
(get-output-string out))
(else
(display (car xs) out)
(display sep out)
(loop (cdr xs)))))))
(define (every pred lst)
(let loop ((lst lst))
(cond ((null? lst))
((not (pred (car lst))) #f)
(else (loop (cdr lst))))))
(define (any pred lst)
(let loop ((lst lst))
(cond ((null? lst) #f)
((pred (car lst)))
(else (loop (cdr lst))))))
(define-syntax inc!
(syntax-rules ()
((_ v) (inc! v 1))
((_ v n)
(let ((n2 (+ v n)))
(set! v n2)
n2))))
(define-syntax dec!
(syntax-rules ()
((_ v) (dec! v 1))
((_ v n)
(let ((n2 (- v n)))
(set! v n2)
n2))))
(define (const c) (lambda _ c))
(define (compl f) (lambda (x) (not (f x))))
(define (o . fs)
(lambda (x)
(let loop ((fs fs))
(if (null? fs)
x
((car fs) (loop (cdr fs)))))))
(define (foldl proc init lst)
(let loop ((lst lst) (r init))
(if (null? lst)
r
(loop (cdr lst) (proc r (car lst))))))
(define (foldr proc init lst)
(let loop ((lst lst))
(if (null? lst)
init
(proc (car lst) (loop (cdr lst))))))
(define (flip proc)
(lambda (x y) (proc y x)))
(define (equal=? x y)
(let loop ((x x) (y y))
(cond ((eq? x y))
((number? x) (and (number? y) (= x y)))
((pair? x)
(and (pair? y)
(loop (car x) (car y))
(loop (cdr x) (cdr y))))
((vector? x)
(and (vector? y)
(let ((xlen (vector-length x))
(ylen (vector-length y)))
(and (= xlen ylen)
(let loop2 ((i 0))
(cond ((>= i xlen) #t)
((loop (vector-ref x i) (vector-ref y i))
(loop2 (+ i 1)))
(else #f)))))))
((string? x)
(and (string? y)
(string=? x y)))
(else (eqv? x y))))) ; does not recurse into disjoint types!
(define (alist-cons x y z) (cons (cons x y) z))
(define (make-list n . init)
(let ((x (optional init #f)))
(let loop ((n n) (lst '()))
(if (<= n 0)
lst
(loop (sub1 n) (cons x lst))))))
(define (symbol<? s1 s2)
(string<? (symbol->string s1) (symbol->string s2)))
(define (symbol-append . ss)
(string->symbol
(apply string-append (map symbol->string ss))))
(define (car+cdr x) (values (car x) (cdr x)))
;; is lst1 a tail of lst2?
(define (tail? lst1 lst2)
(cond ((null? lst2) (null? lst1))
((eq? lst1 lst2))
(else (tail? lst1 (cdr lst2)))))
;; is lst1 a sublist of lst2?
(define (sublist? lst1 lst2)
(define (follow a b)
(cond ((null? a))
((null? b) #f)
((equal=? (car a) (car b)) (follow (cdr a) (cdr b)))
(else #f)))
(let loop ((lst2 lst2))
(cond ((null? lst2) #f)
((follow lst1 lst2))
(else (loop (cdr lst2))))))
(define (butlast lst)
(let loop ((lst lst))
(if (null? (cdr lst))
'()
(cons (car lst) (loop (cdr lst))))))
(define (last lst)
(let loop ((lst lst))
(if (null? (cdr lst))
(car lst)
(loop (cdr lst)))))
(define (last-pair lst)
(let loop ((lst lst))
(if (null? (cdr lst))
lst
(loop (cdr lst)))))
(define (filter pred lst)
(foldr (lambda (x r) (if (pred x) (cons x r) r)) '() lst))
(define (filter-map pred lst)
(foldr (lambda (x r)
(cond ((pred x) => (lambda (y) (cons y r)))
(else r)))
'()
lst))
(define (append-map proc lst)
(foldr (lambda (x r) (append (proc x) r)) '() lst))
(define (find pred lst)
(let loop ((lst lst))
(cond ((null? lst) #f)
((pred (car lst)) (car lst))
(else (loop (cdr lst))))))
(define (find-tail pred ls)
(let lp ((ls ls))
(cond ((null? ls) #f)
((pred (car ls)) ls)
(else (lp (cdr ls))))))
(define (position pred lst)
(let loop ((lst lst) (i 0))
(cond ((null? lst) #f)
((pred (car lst)) i)
(else (loop (cdr lst) (+ i 1))))))
(define (posq x lst)
(position (cut eq? x <>) lst))
(define (append! . lists) ; SRFI-1 ref. impl.
;; First, scan through lists looking for a non-empty one.
(let lp ((lists lists) (prev '()))
(if (not (pair? lists)) prev
(let ((first (car lists))
(rest (cdr lists)))
(if (not (pair? first)) (lp rest first)
;; Now, do the splicing.
(let lp2 ((tail-cons (last-pair first))
(rest rest))
(if (pair? rest)
(let ((next (car rest))
(rest (cdr rest)))
(set-cdr! tail-cons next)
(lp2 (if (pair? next) (last-pair next) tail-cons)
rest))
first)))))))
(define (adjoin lst . xs)
(foldl (lambda (r x) (if (member x r) r (cons x r))) lst xs)) ; uses equal?, not equal=?
(define (difference ls . lss)
(foldl
(lambda (ls lst)
(filter (compl (cut member <> lst)) ls))
ls
lss))
(define (union . lss)
(foldl
(lambda (ls lst)
(foldl
(lambda (ls x)
(if (any (lambda (y) (equal=? y x)) ls)
ls
(cons x ls)))
ls lst))
'() lss))
(define (intersection ls1 . lss)
(filter (lambda (x)
(every (lambda (lis) (member x lis)) lss)) ; uses equal?, not equal=?
ls1))
(define (delete x lst)
(filter (lambda (y) (not (equal=? x y))) lst))
(define (delete-duplicates lis)
(let recur ((lis lis))
(if (null? lis) lis
(let* ((x (car lis))
(tail (cdr lis))
(new-tail (recur (delete x tail))))
(if (equal=? tail new-tail) lis (cons x new-tail))))))
(define (iota n . start+step)
(let-optionals start+step ((start 0) (step 1))
(let loop ((i start) (n n))
(if (<= n 0)
'()
(cons i (loop (add1 i) (sub1 n)))))))
(define (print* . xs)
(apply emit xs))
(define (show x)
(pp x)
x)
(define (concatenate lst) (apply append lst))
(define (interleave lst delim)
(if (or (null? lst) (null? (cdr lst)))
lst
(cons
(car lst)
(let loop ((lst (cdr lst)))
(if (null? lst)
'()
(cons delim (cons (car lst) (loop (cdr lst)))))))))
(define (split-at i lst) ; allows dotted lists
(let loop ((lst lst) (i i) (head '()))
(if (or (<= i 0) (not (pair? lst)))
(values (reverse head) lst)
(loop (cdr lst) (sub1 i) (cons (car lst) head)))))
(define (partition pred lst)
(let loop ((yes '()) (no '()) (lst lst))
(cond ((null? lst) (values (reverse yes) (reverse no)))
((pred (car lst)) (loop (cons (car lst) yes) no (cdr lst)))
(else (loop yes (cons (car lst) no) (cdr lst))))))
(define (span pred lst) ; allows dotted lists and premature end
(let loop ((lst lst) (head '()))
(cond ((not (pair? lst)) (values (reverse head) lst))
((pred (car lst)) (loop (cdr lst) (cons (car lst) head)))
(else (values (reverse head) lst)))))
(define (take n lst) ; allows dotted lists and premature end
(let loop ((lst lst) (n n))
(cond ((or (null? lst) (zero? n)) '())
(else (cons (car lst) (loop (cdr lst) (sub1 n)))))))
(define (drop n lst) ; allows dotted lists and premature end
(let loop ((lst lst) (n n))
(cond ((or (null? lst) (zero? n)) lst)
(else (loop (cdr lst) (sub1 n))))))
(define (cons* first . rest)
(let recur ((x first) (rest rest))
(if (pair? rest)
(cons x (recur (car rest) (cdr rest)))
x)))
(define (option k args . def)
(cond ((memq k args) => cadr)
(else (optional def #f))))
(define (collect-options opt options)
(let loop ((opts options))
(cond ((memq opt opts) =>
(lambda (p)
(cons (cadr p) (loop (cddr p)))))
(else '()))))
(define (flatten lst)
(cond ((null? lst) '())
((not (pair? lst)) (list lst))
((null? (car lst)) (cdr lst))
((pair? (car lst))
(append (flatten (car lst)) (flatten (cdr lst))))
(else (cons (car lst) (flatten (cdr lst))))))
(define (chomp str . chr)
(let ((chr (optional chr #\newline))
(len (string-length str)))
(if (and (positive? len) (char=? chr (string-ref str (- len 1))))
(substring str 0 (- len 1))
str)))
(define (read-line . port)
(let ((port (optional port (current-input-port))))
(let loop ((lst '()) (cr #f))
(let ((c (read-char port)))
(cond ((eof-object? c)
(if (null? lst)
c
(list->string (reverse lst))))
((char=? #\return c)
(loop (if cr (cons c lst) lst) #t))
((char=? #\newline c)
(list->string (reverse lst)))
(else (loop (cons c (if cr (cons #\return lst) lst)) #f)))))))
(define (read-file in . reader)
(let ((port (if (string? in) (open-input-file in) in))
(reader (optional reader read)))
(let loop ((all '()))
(match (reader port)
((? eof-object?)
(begin0
(reverse all)
(when (string? in) (close-input-port port))))
(x (loop (cons x all)))))))
(define (write-line str . port)
(let ((port (optional port (current-output-port))))
(display (chomp str) port)
(newline port)))
(define (read-all . in)
(let* ((in (optional in (current-input-port)))
(port (if (string? in) (open-input-file in) in))
(out (open-output-string)))
(let loop ()
(let ((c (read-string 4096 port)))
(cond ((eof-object? c)
(begin0
(get-output-string out)
(when (string? in) (close-input-port port))))
(else
(display c out)
(loop)))))))
(define (dribble . args)
(for-each
(cut display <> (current-error-port))
args)
(newline (current-error-port)))
(define (sleep secs)
(system (string-append "sleep " (number->string secs))))
(define sub1 (cut - <> 1))
(define add1 (cut + <> 1))
(define (numberize x)
(cond ((number? x) x)
((boolean? x) (if x 1 0))
((string? x) (string->number x))
((symbol? x) (numberize (symbol->string x)))
((char? x) (numberize (string x)))
(else (error "can not convert to number" x))))
(define (string-split str . opts)
(let-optionals opts ((delims " \n\t")
(keep #f))
(let ((len (string-length str))
(dlen (string-length delims))
(first #f) )
(define (add from to last)
(let ((node (cons (substring str from to) '())))
(if first
(set-cdr! last node)
(set! first node) )
node) )
(let loop ((i 0) (last #f) (from 0))
(cond ((>= i len)
(when (or (> i from) keep) (add from i last))
(or first '()) )
(else
(let ((c (string-ref str i)))
(let scan ((j 0))
(cond ((>= j dlen) (loop (+ i 1) last from))
((char=? c (string-ref delims j))
(let ((i2 (+ i 1)))
(if (or (> i from) keep)
(loop i2 (add from i last) i2)
(loop i2 last i2) ) ) )
(else (scan (+ j 1))) ) ) ) ) ) ) ) ) )
(define-syntax-rule (push! x v)
(set! v (cons x v)))
(define-syntax-rule (pop! v)
(let ((x (car v)))
(set! v (cdr v))
x))
(define (delete-file* fn)
(if (file-exists? fn)
(begin (delete-file fn) fn)
#f))
(define (scan str pred . args)
(let ((len (string-length str)))
(let-optionals args ((i 0) (step 1))
(let loop ((i i))
(cond ((or (negative? i) (>= i len)) #f)
((pred (string-ref str i)) i)
(else (loop (+ i step))))))))
(define (eql x) (cut equal=? <> x))
(define (padl str n . fill)
(let ((fill (optional fill #\space))
(len (string-length str)))
(if (> len n)
str
(string-append (make-string (- n len) fill) str))))
(define (padr str n . fill)
(let ((fill (optional fill #\space))
(len (string-length str)))
(if (> len n)
str
(string-append str (make-string (- n len) fill)))))
(define (pad str n . fill)
(let ((fill (optional fill #\space))
(len (string-length str)))
(if (> len n)
str
(let ((m (quotient (- n len) 2)))
(string-append
(make-string m fill)
str
(make-string (- n len m) fill))))))
(define (trim str . delims)
(let* ((delims (optional delims " \n\t"))
(delims (if (string? delims) (string->list delims) delims))
(pred (lambda (c) (not (memv c delims))))
(len (string-length str))
(p1 (or (scan str pred) len))
(p2 (or (scan str pred (sub1 len) -1) 0)))
(if (> p1 p2)
""
(substring str p1 (add1 p2)))))
(define (absolute-pathname? pathname)
(and (positive? (string-length pathname))
(char=? #\/ (string-ref pathname 0))))
(define (directory? fn)
(and (file-exists? fn)
(zero? (system (string-append "test -d " (qs fn))))))
(define (basename str)
(let ((len (string-length str)))
(cond ((scan str (cut char=? #\/ <>) (sub1 len) -1) =>
(lambda (p)
(if (= p (sub1 len))
#f
(substring str (add1 p) len))))
(else str))))
(define (dirname str)
(let ((len (string-length str)))
(cond ((scan str (cut char=? #\/ <>) (sub1 len) -1) => (lambda (p) (substring str 0 p)))
(else #f))))
(define (strip-suffix str)
(let ((len (string-length str)))
(cond ((scan str (cut char=? #\. <>) (sub1 len) -1) =>
(cut substring str 0 <>))
(else str))))
(define (replace-suffix suf str)
(string-append (strip-suffix str) "." suf))
(define (with-input-from-port port thunk)
(parameterize ((current-input-port port)) (thunk)))
(define (with-output-to-port port thunk)
(parameterize ((current-output-port port)) (thunk)))
(define (call-with-input-string str proc)
(let ((in (open-input-string str)))
(begin0 (proc in) (close-input-port in))))
(define (call-with-output-string proc)
(let ((out (open-output-string)))
(proc out)
(begin0
(get-output-string out)
(close-output-port out))))
(define gentemp
(let ((counter 0))
(lambda prefix
(string-append (optional prefix "T") (number->string (inc! counter))))))
(define (atom? x)
(or (null? x) (not (pair? x))))
(define (vector-resize vec size . init)
(let ((new (make-vector size (optional init #f)))
(from (min (vector-length vec) size)))
(do ((i 0 (add1 i)))
((>= i from) new)
(vector-set! new i (vector-ref vec i)))))
(define (vector-copy! from to . opts)
(let-optionals opts ((start1 0)
(end1 (vector-length from))
(start2 0)
(end2 (+ start2 (- end1 start1))))
(let ((count (- end1 start1)))
(do ((i 0 (add1 i)))
((>= i count))
(vector-set! to (+ i start2) (vector-ref from (+ start1 i)))))))
(define (copy-list lst) (map id lst))
(define HOME (get-environment-variable "HOME"))
(define (ep path . base)
(let ((len (string-length path)))
;; "base" is ignored when path begins with #\~
(if (zero? len)
base
(case (string-ref path 0)
((#\~) (string-append HOME (substring path 1 len)))
((#\/) path) ;XXX ignores Windows device names
(else (string-append (optional base (current-directory)) "/" path))))))
(define (qs str)
(apply
string-append
"'"
(append
(map (lambda (c)
(if (char=? #\' c)
"'\\''"
(string c)))
(string->list str))
(list "'"))))
(define *temporary-files* '())
(define temporary-directory
(make-parameter
(or (get-environment-variable "TMPDIR")
(get-environment-variable "TMP")
(get-environment-variable "TEMP")
"/tmp")))
(define (with-temporary-files thunk)
(fluid-let ((*temporary-files* *temporary-files*))
(let ((tmpfiles *temporary-files*))
(call-with-values thunk
(lambda results
(let loop ((ts *temporary-files*))
(if (or (null? ts) (eq? ts tmpfiles))
(apply values results)
(begin
(delete-file* (car ts))
(loop (cdr ts))))))))))
(define make-temporary-filename
(let ((count 0))
(lambda args
(let-optionals args ((prefix "tmp")
(extension #f))
(set! count (+ count 1))
(string-append
(temporary-directory)
"/" prefix
"." (number->string (current-second))
"." (number->string (current-process-id))
"." (number->string count)
(if extension
(string-append "." extension)
""))))))
(define (temporary-file . args)
(let-optionals args ((prefix "tmp") (suffix #f))
(let ((tmp (make-temporary-filename prefix suffix)))
(push! tmp *temporary-files*)
tmp)))
(define run-verbose (make-parameter #f))
(define run-dry-run (make-parameter #f))
(define (execute cmd)
(define (build-command cmd)
(cond ((string? cmd) cmd)
((number? cmd) (number->string cmd))
((char? cmd) (string cmd))
((symbol? cmd) (symbol->string cmd))
((list? cmd) (join (map build-command cmd) " "))
(else (error "invalid command part" cmd))))
(let ((cmd (build-command cmd)))
(when (run-verbose)
(with-output-to-port (current-error-port)
(cut print " " cmd)))
(if (run-dry-run)
0
(system cmd))))
(define (check-status s . msg)
(if (zero? s)
s
(error (optional msg "executing command failed with non-zero exit status") s)))
(define-syntax-rule (run cmd ...)
(values (check-status (execute `cmd) 'cmd) ...))
(define-syntax-rule (run* cmd ...)
(values (execute `cmd) ...))
(define-syntax-rule (capture cmd ...)
(parameterize ((run-verbose #f))
(with-temporary-files
(lambda ()
(values
(let ((tmp (temporary-file)))
(check-status (execute `(cmd > ,(qs tmp))) 'cmd)
(trim (with-input-from-file tmp read-all)))
...)))))
(define-syntax-rule (capture-lines cmd ...)
(parameterize ((run-verbose #f))
(with-temporary-files
(lambda ()
(values
(let ((tmp (temporary-file)))
(check-status (execute `(cmd > ,(qs tmp))) 'cmd)
(read-file tmp read-line))
...)))))
(define (system-software)
(let ((s #f))
(lambda ()
(or s
(let ((ss (string->symbol (capture (uname)))))
(set! s ss)
s)))))
(define system-architecture
(let ((s #f))
(lambda ()
(or s
(let ((sa (string->symbol (capture (uname "-m")))))
(set! s sa)
s)))))
(define (file-executable? fn)
(zero? (run* (test "-x" ,(qs fn)))))
(define (file-size fn)
(string->number
(case (system-software)
((Darwin) (capture (stat "-f" "\"%z\"" ,(qs fn))))
(else (capture (stat "-c" "\"%s\"" ,(qs fn)))))))
(define (file-modification-time fn)
(string->number
(case (system-software)
((Darwin) (capture (stat "-f" "\"%c\"" ,(qs fn))))
(else (capture (stat "-c" "\"%Y\"" ,(qs fn)))))))
(define (limited maxdepth exp)
(define (walk x d)
(if (> d maxdepth)
'...
(cond ((vector? x) (list->vector (walk (vector->list x) d)))
((pair? x)
(let loop ((x x) (n maxdepth))
(cond ((null? x) '())
((zero? n) '(...))
((pair? x) (cons (walk (car x) (+ d 1)) (loop (cdr x) (- n 1))))
(else x))))
(else x))))
(walk exp 1))