Newer
Older
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Log: code.log; Package: Lisp -*-
;;; ### Some day fix to accept :wild in any pathname component.
;;; **********************************************************************
;;; This code was written as part of the Spice Lisp project at
;;; Carnegie-Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of Spice Lisp, please contact
;;; Scott Fahlman (FAHLMAN@CMUC).
;;; **********************************************************************
;;;
;;; Ugly pathname functions for Spice Lisp.
;;; these functions are part of the standard Spice Lisp environment.
;;;
;;; Written by Jim Large and Rob MacLachlan
;;;
;;; **********************************************************************
(in-package 'lisp)
(export '(pathname pathnamep *default-pathname-defaults* truename
parse-namestring merge-pathnames make-pathname
pathname-host pathname-device pathname-directory
pathname-name pathname-type pathname-version
namestring file-namestring directory-namestring
host-namestring enough-namestring user-homedir-pathname
probe-file rename-file delete-file file-write-date
file-author directory))
(use-package "EXTENSIONS")
(in-package "EXTENSIONS")
(export '(print-directory complete-file ambiguous-files default-directory
file-writable))
(in-package 'lisp)
;;; Pathname structure
;;; *Default-Pathname-defaults* has all values unspecified except for the
;;; host. All pathnames must have a host.
(defvar *default-pathname-defaults* ()
"Set to the default pathname-defaults pathname (Got that?)")
(defun filesys-init ()
(setq *default-pathname-defaults*
(%make-pathname "Mach" nil nil nil nil nil)))
;;; The pathname type is defined with a defstruct.
;;; This declaration implicitly defines the common lisp function Pathnamep
(defstruct (pathname
(:conc-name %pathname-)
(:print-function %print-pathname)
(:constructor
%make-pathname (host device directory name type version))
(:predicate pathnamep))
"Pathname is the structure of the file pathname. It consists of a
host, a device, a directory, a name, and a type."
(host nil :type (or simple-string null))
(device nil :type (or simple-string (member nil :absolute)))
(directory nil :type (or simple-vector null))
(name nil :type (or simple-string null))
(type nil :type (or simple-string null))
(version nil :type (or integer (member nil :newest))))
(defun %print-pathname (s stream d)
(declare (ignore d))
(format stream "#.(pathname ~S)" (namestring s)))
(defun make-pathname (&key defaults (host nil hostp) (device nil devicep)
(directory nil directoryp) (name nil namep)
(type nil typep) (version nil versionp))
"Create a pathname from :host, :device, :directory, :name, :type and :version.
If any field is ommitted, it is obtained from :defaults as though by
merge-pathnames."
(if defaults
(let ((defaults (pathname defaults)))
(unless hostp
(setq host (%pathname-host defaults)))
(unless devicep
(setq device (%pathname-device defaults)))
(unless directoryp
(setq directory (%pathname-directory defaults)))
(unless namep
(setq name (%pathname-name defaults)))
(unless typep
(setq type (%pathname-type defaults)))
(unless versionp
(setq version (%pathname-version defaults))))
(unless hostp
(setq host (%pathname-host *default-pathname-defaults*))))
(when (stringp directory)
(setq directory (%pathname-directory (parse-namestring directory))))
(%make-pathname
(if (stringp host) (coerce host 'simple-string) host)
(if (stringp device) (coerce device 'simple-string) device)
directory
(if (stringp name) (coerce name 'simple-string) name)
(if (stringp type) (coerce type 'simple-string) type)
version))
;;; These can not be done by the accessors because the pathname arg may be
;;; a string or a symbol or etc.
(defun pathname-host (pathname)
"Returns the host slot of pathname. Pathname may be a string, symbol,
or stream."
(%pathname-host (if (pathnamep pathname) pathname (pathname pathname))))
(defun pathname-device (pathname)
"Returns the device slot of pathname. Pathname may be a string, symbol,
or stream."
(%pathname-device (if (pathnamep pathname) pathname (pathname pathname))))
(defun pathname-directory (pathname)
"Returns the directory slot of pathname. Pathname may be a string,
symbol, or stream."
(%pathname-directory (if (pathnamep pathname) pathname (pathname pathname))))
(defun pathname-name (pathname)
"Returns the name slot of pathname. Pathname may be a string,
symbol, or stream."
(%pathname-name (if (pathnamep pathname) pathname (pathname pathname))))
(defun pathname-type (pathname)
"Returns the type slot of pathname. Pathname may be a string,
symbol, or stream."
(%pathname-type (if (pathnamep pathname) pathname (pathname pathname))))
(defun pathname-version (pathname)
"Returns the version slot of pathname. Pathname may be a string,
symbol, or stream."
(%pathname-version (if (pathnamep pathname) pathname (pathname pathname))))
;;;; FSM Compiler
(eval-when (compile eval)
;;; These macros are used in the code emitted by deflex. They refer
;;; to local vars in the defined function.
(defmacro fsm-scan ()
'(progn
(incf pointer)
(setq current-char (if (>= pointer end)
:end
(char string pointer)))))
;;; fsm-emit expands into a form which pushes FLAG onto *deflex-tokens* and
;;; then pushes a string containing the ACCUMULATED characters
(defmacro fsm-emit (flag)
`(progn
(vector-push-extend ,flag *deflex-tokens*)
(vector-push-extend (subseq buffer 0 buffer-index) *deflex-tokens*)
(setq buffer-index 0)))
;;; fsm-dotfix expands into a form which replaces the last EMITTED flag with
;;; FLAG, and the last EMITTED token with its self + "." + the ACCUMULATED
;;; characters.
(defmacro fsm-dotfix (flag)
`(let ((prev-token (vector-pop *deflex-tokens*)))
(setf (aref *deflex-tokens* (1- (fill-pointer *deflex-tokens*))) ,flag)
(vector-push (concatenate 'simple-string
prev-token
"."
(subseq buffer 0 buffer-index))
*deflex-tokens*)
(setq buffer-index 0)))
(defun deflex-arc (arc)
(do ((set (nth 0 arc))
(new-state (nth 2 arc))
(actions (nth 3 arc) (if (member (car actions) '(EMIT DOTFIX))
(cddr actions)
(cdr actions)))
(action-forms
()
(cons (case (car actions)
(ACCUMULATE '(progn
(setf (schar buffer buffer-index) current-char)
(incf buffer-index)))
(SCAN '(fsm-scan))
(EMIT `(fsm-emit ,(nth 1 actions)))
(DOTFIX `(fsm-dotfix ,(nth 1 actions)))
(t (error "Illegal action ~s" (car actions))))
action-forms)))
((null actions)
`(when ,(cond ((eq set ':end) `(eq current-char ,set))
((eq set T) T)
(t
`(and (not (eq current-char :end))
,(if (characterp set)
`(char= current-char ,set)
`(find current-char
(the simple-string ,set))))))
,@(nreverse action-forms)
(go ,new-state)))))
(defun deflex-state (state)
(do ((state-name (car state))
(arc-list (cdr state) (cdr arc-list))
(form-list () (cons (deflex-arc (car arc-list)) form-list)))
((null arc-list)
`(,state-name
,@(nreverse form-list)
(go JAM)))))
(defmacro deflex (name &rest states)
(do ((states states (cdr states))
(body-forms () (append body-forms (deflex-state (car states)))))
((null states)
`(defun ,(concat-pnames 'lex- name)
(string &optional (start 0) (end (length string)))
(declare (simple-string string)
(fixnum start end))
(setf (fill-pointer *deflex-tokens*) 0)
(prog ((current-char (if (> end start)
(char string start) :end))
(pointer start)
(buffer *deflex-buffer*)
(buffer-index 0))
(declare (simple-string buffer))
,@body-forms
WIN (return (values T pointer))
JAM (return (values NIL pointer)))))))
); eval-when (compile eval)
(defvar *deflex-buffer* (make-string 100))
(defvar *deflex-tokens* (make-array 20 :adjustable t :fill-pointer 0))
;;;; FSM for lexing pathnames.
(defconstant ses-name-char
"#@$-+*%0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~")
(defconstant fs-whitespace ;whitespace in filenames
"
")
;;; Lex-ses-file-spec separates a sesame file name into its components
;;;
;;; STRING -- the string containing the file spec.
;;; &optional
;;; START -- the first character to look at (defaults to 0)
;;; END -- 1+ the last character to look at (defaluts to (length STRING))
;;;
;;; Returns multiple values.
;;; 1st -- () for failure or a list of tokens.
;;; 2nd -- the index of the character that stopped the parse
;;;
;;; Tokens are represented by a keyword and a string in *deflex-tokens*.
;;; The first token is of type :logical-name (string is log name) or :absolute.
;;; Subsequent tokens are of type :directory-name, :file-name, :extension, or
;;; the string being a pathname component.
;;;
(deflex ses-file-spec
;;S1 waits for the first non whitespace character
(S1 (fs-whitespace --> S1 (SCAN))
(#\- --> JAM) ;- illegal as start of file name
(#\/ --> S3 (EMIT :absolute SCAN));/ starts an absolute pathname
(#\\ --> S2a (ACCUMULATE SCAN)) ;\ quotes char in name or logname
(#\. --> S5 (EMIT :file-name SCAN)) ;. maybe ends file name
(ses-name-char --> S2 (ACCUMULATE SCAN)) ;first char in a name or logname
(:end --> WIN) ; Null filename is legal.
)
;;S2 accumulates chars in a file name, logical name, or directory name.
(S2 (#\: --> S3 (EMIT :logical-name SCAN)) ;: ends log-name, starts next name
(#\/ --> S3 (EMIT :directory-name SCAN));/ ends directory name,starts next
(#\\ --> S2a (ACCUMULATE SCAN)) ;\ quotes char in name
(#\. --> S5 (EMIT :file-name SCAN)) ;. maybe ends file name
(:end --> WIN (EMIT :file-name))
(ses-name-char --> S2 (ACCUMULATE SCAN))
(fs-whitespace --> S9 (EMIT :file-name SCAN))
(T --> JAM (EMIT :file-name))
)
;;S2a waits for any char following a \ found in S2
(S2a (:END --> JAM)
(T --> S2 (ACCUMULATE SCAN)))
;;S3 accumulates chars in a file name or directory name
(S3 (#\/ --> JAM)
(T --> S3b))
(S3b (#\/ --> S3 (EMIT :directory-name SCAN));/ ends directory name,starts next
(#\. --> S4 (EMIT :file-name SCAN)) ;. maybe ends file name
(#\\ --> S3a (ACCUMULATE SCAN)) ;\ quotes char in name
(:end --> WIN (EMIT :file-name))
(ses-name-char --> S3b (ACCUMULATE SCAN))
(fs-whitespace --> S9 (EMIT :file-name SCAN))
(T --> JAM (EMIT :file-name))
)
;;S3a waits for any char following a \ found in S3
(S3a (:end --> JAM)
(T --> S3b (ACCUMULATE SCAN)))
;;S4 waits for chars in a name following a dot. If it encounters the end of
;; the pathname, then the chars are the extension. Otherwise, the chars are
;; really part of the last token, so we mash them back on with dotfix.
(S4 (#\/ --> S3 (DOTFIX :directory-name SCAN))
(#\\ --> S4a (ACCUMULATE SCAN))
(#\. --> S4 (DOTFIX :file-name SCAN))
(:end --> WIN (EMIT :extension))
(fs-whitespace --> S9 (EMIT :extension SCAN))
(ses-name-char --> S4 (ACCUMULATE SCAN))
(T --> JAM (EMIT :extension))
)
;;S4a waits for any char following a \ found in S4
(S4a (:end --> JAM)
(T --> S4 (ACCUMULATE SCAN)))
;;S5 is the same as S4 but name may also be logical name
(S5 (#\/ --> S3 (DOTFIX :directory-name SCAN))
(#\: --> S3 (DOTFIX :logical-name SCAN))
(#\\ --> S5a (ACCUMULATE SCAN))
(#\. --> S5 (DOTFIX :file-name SCAN))
(:end --> WIN (EMIT :extension))
(fs-whitespace --> S9 (EMIT :extension SCAN))
(ses-name-char --> S5 (ACCUMULATE SCAN))
(T --> JAM (EMIT :extension))
)
;;S5a waits for any char following a \ found in S5
(S5a (:end --> JAM)
(T --> S5 (ACCUMULATE SCAN)))
;;S9 eats trailing whitespace.
(S9 (fs-whitespace --> S9 (SCAN))
(:end --> WIN)
(t --> JAM))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ses-tokens->pathname converts the contents of *deflex-tokens* into a pathname
;;;
(defun ses-tokens->pathname ()
(do ((end (fill-pointer *deflex-tokens*))
(result (make-pathname))
(dirlist ())
(token-val ())
(ix 0 (+ ix 2))
(default t))
((>= ix end)
(setf (%pathname-directory result)
(cond (dirlist
(when default (setf (%pathname-device result) "Default"))
(coerce (the list (nreverse dirlist)) 'simple-vector))
(default nil)
(t '#())))
result)
(declare (list dirlist)
(fixnum ix end))
(setq token-val (aref *deflex-tokens* (1+ ix)))
(case (aref *deflex-tokens* ix)
(:ABSOLUTE
(setf (%pathname-device result) :ABSOLUTE)
(setq default nil))
(:LOGICAL-NAME
(setf (%pathname-device result) token-val)
(setq default nil))
(:DIRECTORY-NAME (push token-val dirlist))
(:FILE-NAME
(unless (zerop (length (the simple-string token-val)))
(setf (%pathname-name result) token-val)))
(:EXTENSION (setf (%pathname-type result) token-val)))))
;;;; PARSE-NAMESTRING and PATHNAME.
(defun parse-namestring (thing &optional host
(defaults *default-pathname-defaults*)
&key (start 0) end (junk-allowed nil))
"Parses a string representation of a pathname into a pathname."
(declare (ignore host defaults))
(let* ((thing (typecase thing
(string (coerce thing 'simple-string))
(pathname (return-from parse-namestring (values thing start)))
(stream (file-name thing))
(symbol (symbol-name thing))
(t (error "This thing is a bad thing for parse-namestring: ~S"
thing))))
(end (or end (length thing))))
(declare (simple-string thing))
(multiple-value-bind (won next-field)
(lex-ses-file-spec thing start end)
(unless (or won junk-allowed)
(error "There's junk in this thing that you gave to parse-namestring:~%~
~4T\"~A\"~%~0,1,V,'.@A" thing (+ next-field 5) #\^))
(values (ses-tokens->pathname) next-field))))
(defun pathname (thing)
"Turns thing into a pathname. Thing may be a string, symbol, stream, or
pathname."
(values (parse-namestring thing)))
;;; Merge-Pathnames -- Public
;;;
;;; Returns a new pathname whose fields are the same as the fields in PATHNAME
;;; except that () fields are filled in from defaults. Type and Version field
;;; are only done if name field has to be done (see manual for explanation).
;;;
(defun merge-pathnames (pathname &optional
(defaults *default-pathname-defaults*)
default-version)
"Fills in unspecified slots of Pathname from Defaults (defaults to
*default-pathname-defaults*). If the version remains unspecified,
gets it from Default-Version."
;;
;; finish hairy argument defaulting
(setq pathname (pathname pathname))
(setq defaults (pathname defaults))
;;
;; make a new pathname
(let ((name (%pathname-name pathname))
(device (%pathname-device pathname)))
(%make-pathname
(or (%pathname-host pathname) (%pathname-host defaults))
(or device (%pathname-device defaults))
(or (%pathname-directory pathname) (%pathname-directory defaults))
(or name (%pathname-name defaults))
(or (%pathname-type pathname) (%pathname-type defaults))
(or (%pathname-version pathname)
(if name
default-version
(or (%pathname-version defaults) default-version))))))
;;;; NAMESTRING and other stringification stuff.
;;; %Dirstring -- Internal
;;;
;;; %Dirstring converts a vector of the form #("foo" "bar" ... "baz") into a
;;; string of the form "foo/bar/ ... /baz/"
(defun %dirstring (dirlist)
(declare (simple-vector dirlist))
(let* ((numdirs (length dirlist))
(length numdirs))
(declare (fixnum numdirs length))
(dotimes (i numdirs)
(incf length (the fixnum (length (svref dirlist i)))))
(do ((result (make-string length))
(index 0 (1+ index))
(position 0))
((= index numdirs) result)
(declare (simple-string result))
(let* ((string (svref dirlist index))
(len (length string))
(end (+ position len)))
(declare (simple-string string)
(fixnum len end))
(replace result string :start1 position :end1 end :end2 len)
(setf (schar result end) #\/)
(setq position (+ end 1))))))
(defun quick-integer-to-string (n)
(cond ((zerop n) "0")
((eql n 1) "1")
((minusp n)
(concatenate 'simple-string "-"
(the simple-string (quick-integer-to-string (- n)))))
(t
(do* ((len (1+ (truncate (integer-length n) 3)))
(res (make-string len))
(i (1- len) (1- i))
(q n)
(r 0))
((zerop q)
(incf i)
(replace res res :start2 i :end2 len)
(%primitive shrink-vector res (- len i)))
(declare (simple-string res)
(fixnum len i r))
(multiple-value-setq (q r) (truncate q 10))
(setf (schar res i) (schar "0123456789" r))))))
(defun %device-string (device)
(cond ((eq device :absolute) "/")
(device
(if (string-equal device "Default")
""
(concatenate 'simple-string (the simple-string device) ":")))
(T "")))
(defun namestring (pathname)
"Returns the full form of PATHNAME as a string."
(setq pathname (pathname pathname))
(let* ((directory (%pathname-directory pathname))
(name (%pathname-name pathname))
(type (%pathname-type pathname))
(result (%device-string (%pathname-device pathname))))
(declare (simple-string result))
(when directory
(setq result (concatenate 'simple-string result
(the simple-string (%dirstring directory)))))
(when name
(setq result (concatenate 'simple-string result
(the simple-string name))))
(when type
(setq result (concatenate 'simple-string result "."
(the simple-string type))))
result))
(defun %ses-get-useful-name (pathname)
"NAMESTRING of pathname ignoring the device slot."
(setq pathname (pathname pathname))
(let* ((directory (%pathname-directory pathname))
(name (%pathname-name pathname))
(type (%pathname-type pathname))
(result ""))
(declare (simple-string result))
(when directory
(setq result (concatenate 'simple-string result
(the simple-string (%dirstring directory)))))
(when name
(setq result (concatenate 'simple-string result
(the simple-string name))))
(when type
(setq result (concatenate 'simple-string result "."
(the simple-string type))))
result))
;;; This function is somewhat bummed to make the Hemlock directory command
;;; is fast.
;;;
(defun file-namestring (pathname)
"Returns the name, type, and version of PATHNAME as a string."
(unless (pathnamep pathname) (setq pathname (pathname pathname)))
(let* ((name (%pathname-name pathname))
(type (%pathname-type pathname))
(result (or name "")))
(declare (simple-string result))
(if type
(concatenate 'simple-string result "." type)
result)))
(defun directory-namestring (pathname)
"Returns the device & directory parts of PATHNAME as a string."
(setq pathname (pathname pathname))
(let* ((directory (%pathname-directory pathname))
(result (%device-string (%pathname-device pathname))))
(declare (simple-string result))
(when directory
(setq result (concatenate 'simple-string result
(the simple-string (%dirstring directory)))))
result))
(defun host-namestring (pathname)
"Returns the host part of PATHNAME as a string."
(setq pathname (pathname pathname))
(%pathname-host pathname))
;;;; ENOUGH-NAMESTRING
(defun enough-namestring (pathname &optional
(defaults *default-pathname-defaults*))
"Returns a string which uniquely identifies PATHNAME w.r.t. DEFAULTS."
(setq pathname (pathname pathname))
(setq defaults (pathname defaults))
(let* ((device (%pathname-device pathname))
(directory (%pathname-directory pathname))
(name (%pathname-name pathname))
(type (%pathname-type pathname))
(result "")
(need-name nil))
(declare (simple-string result))
(when (and device (string-not-equal device (%pathname-device defaults)))
(setq result (%device-string device)))
(when (and directory
(not (equalp directory (%pathname-directory defaults))))
(setq result (concatenate 'simple-string result
(the simple-string (%dirstring directory)))))
(when (and name (string-not-equal name (%pathname-name defaults)))
(setq result (concatenate 'simple-string result
(the simple-string name))
need-name t))
(when (and type (or need-name
(string-not-equal type (%pathname-type defaults))))
(setq result (concatenate 'simple-string result "."
(the simple-string type))))
result))
;;;; TRUENAME and other stuff probing stuff.
;;; Truename -- Public
;;;
;;; Another silly file function trivially different from another function.
;;;
(defun truename (pathname)
"Return the pathname for the actual file described by the pathname
An error is signalled if no such file exists."
(let ((result (probe-file pathname)))
(unless result
(error "The file ~S does not exist." (namestring pathname)))
result))
;;; Do-Search-List -- Internal
;;;
;;; Bind var in turn to each element of search list with the specifed
;;; name.
;;;
(defmacro do-search-list ((var name &optional exit-form) . body)
"Do-Search-List (Var Name [Exit-Form]) {Form}*"
`(dolist (,var (resolve-search-list ,name nil) ,exit-form)
(declare (simple-string ,var))
,@body))
;;; Sub-Probe-File -- Internal
;;;
;;; Does the work of Probe-File, returning an additional value which
;;; indicates whether the name is really a file.
;;;
(defun sub-probe-file (pathname)
(setq pathname (pathname pathname))
(flet ((pathnamify (ns etype)
(declare (simple-string ns))
(case etype
(:entry_remote
(values (parse-namestring (concatenate 'simple-string ns "/"))
:entry_directory))
(t
(values (parse-namestring ns) etype)))))
(let ((log-name (or (%pathname-device pathname) "default")))
(if (eq log-name :absolute)
(let ((namestring (namestring pathname)))
(multiple-value-bind (name etype)
(mach:unix-subtestname namestring)
(if (null name) NIL
(pathnamify name etype))))
(if (null (%pathname-device pathname))
(multiple-value-bind (name etype)
(mach:unix-subtestname
(%ses-get-useful-name pathname))
(if (null name) NIL (pathnamify name etype)))
(let ((namestring (%ses-get-useful-name pathname)))
(do-search-list (entry log-name)
(let ((str (concatenate 'simple-string entry namestring)))
(declare (simple-string str))
(multiple-value-bind (name etype)
(mach:unix-subtestname str)
(when name
(return (pathnamify name etype))))))))))))
;;; Probe-File -- Public
;;;
;;; Just call Sub-Probe-File and return nil when it isn't a file.
;;;
(defun probe-file (pathname)
"Return a pathname which is the truename of the file if it exists, NIL
otherwise. Returns NIL for directories and other non-file entries."
(multiple-value-bind (pn f)
(sub-probe-file pathname)
(if (eq f :entry_file) pn)))
;;; Predict-Name -- Internal
;;;
;;; Predict-Name is a function used by Open to get an absolute pathname
;;; for a file being opened. Returns the truename of the file and
;;; whether it really exists or not.
;;;
(defun predict-name (file-name for-input)
(let* ((pathname (pathname file-name))
(device (%pathname-device pathname))
(truename (probe-file pathname)))
(cond ((eq device :absolute)
(if truename
(values (namestring truename) t)
;; Try again in case file-name is a directory.
(predict-name-with-subtest (namestring pathname))))
((and for-input truename)
(values (namestring truename) t))
(t
(let ((expansion (resolve-search-list (or device "default") t)))
(let ((name (concatenate 'simple-string (car expansion)
(%ses-get-useful-name pathname))))
(declare (simple-string name))
(predict-name-with-subtest name)))))))
(defun predict-name-with-subtest (name)
(let ((gr (mach:unix-subtestname name)))
(if gr
(values gr t)
(values (mach::simplify-file-name name) nil))))
;;; Rename-File -- Public
;;;
;;; If File is a File-Stream, then rename the associated file if it exists,
;;; otherwise just change the name in the stream. If not a file stream, then
;;; just rename the file.
;;;
(defun rename-file (file new-name)
"Rename File to have the specified New-Name. If file is a stream open to a
file, then the associated file is renamed. If the file does not yet exist
then the file is created with the New-Name when the stream is closed."
(if (streamp file)
(let* ((name (file-name file))
(pn (parse-namestring name))
(npn (merge-pathnames new-name pn))
(new (predict-name npn nil)))
(when (mach:quick-subtestname name)
(multiple-value-bind (res err) (mach:Unix-rename name new)
(if (null res) (error "Failed to rename ~A to ~A, unix error: ~A."
name new (mach:get-unix-error-msg err)))))
(file-name file new)
(values npn pn (parse-namestring new)))
(let* ((pn (or (sub-probe-file file)
(error "File to rename does not exist: ~S" file)))
(npn (merge-pathnames new-name pn))
(new (predict-name npn nil)))
(multiple-value-bind (res err) (mach:unix-rename (namestring pn) new)
(if res (values npn pn (parse-namestring new))
(error "Failed to rename ~A to ~A, unix error: ~A."
(namestring pn) new (mach:get-unix-error-msg err)))))))
;;; Delete-File -- Public
;;;
;;; Delete the file, Man.
;;;
(defun delete-file (file)
"Delete the specified file."
(let ((tn (sub-probe-file file)))
(when (streamp file)
(close file :abort t))
(if tn
(let ((ns (namestring tn)))
(multiple-value-bind (res err) (mach:unix-unlink ns)
(if (null res)
(error "Failed to delete ~A, unix error: ~A."
ns (mach:get-unix-error-msg err)))))
(unless (streamp file)
(error "File to be deleted does not exist: ~S" file))))
t)
;;; User-Homedir-Pathname -- Public
;;;
;;; If the user wants a meaningful homedir, she has to define Home:.
;;; Someday, login may do this for us. Since we must always return something,
;;; we just return Default: if it isn't defined.
;;;
(defun user-homedir-pathname (&optional host)
"Returns the home directory of the logged in user as a pathname.
This is obtained from the logical name \"home:\". If this is not defined,
then we return \"default:\""
(declare (ignore host))
(let ((home (cdr (assoc :home *environment-list* :test #'eq))))
(if home
(pathname (if (string-equal home "/") "/"
(concatenate 'simple-string home "/")))
(let ((expansion (if (search-list "home:")
(resolve-search-list "home" t))))
(if expansion
(car expansion)
(make-pathname :device "default"))))))
;;; File-Write-Date -- Public
;;;
(defun file-write-date (file)
"Return file's creation date, or NIL if it doesn't exist."
(let ((tn (sub-probe-file file)))
(when tn
(multiple-value-bind (res dev ino mode nlink uid gid
rdev size atime mtime)
(mach:unix-stat (namestring tn))
(declare (ignore dev ino mode nlink uid gid rdev size atime))
(if (null res) 0
(+ unix-to-universal-time mtime))))))
;;; File-Author -- Public
;;;
(defun file-author (file)
"Returns the file author as a string, or nil if the author cannot be
determined. Signals an error if file doesn't exist."
(let ((filename (truename file)))
(multiple-value-bind (winp dev ino mode nlink uid)
(mach:unix-stat (namestring filename))
(declare (ignore dev ino mode nlink))
(if winp (lookup-login-name uid)))))
;;;; DIRECTORY.
;;; DO-DIRECTORY searches a directory, binding the vars to the name and entry
;;; type. Pattern and All are used in MACH:UNIX-SEARCH-DIRECTORY.
;;;
(defmacro do-directory ((name-var etype-var pattern &optional (all t) result)
. body)
"Do-Directory (Name Entry-Type Pattern [All] [Result]) {Form}*.
If All is non-nil (the default), then Unix dot files to be processed.
Unix dot and dot-dot are never processed."
(let ((file (gensym))
(res (gensym))
(type (gensym))
(p (gensym)))
`(dolist (,file (mach:unix-search-directory ,pattern ,all)
,result)
(declare (simple-string ,file))
(let* ((,p (position #\/ ,file :from-end t))
(,name-var
(if ,p
(subseq ,file (the fixnum (1+ (the fixnum ,p))))
,file)))
(declare (simple-string ,name-var))
(unless (or (string= "." ,name-var) (string= ".." ,name-var))
(let ((,etype-var (multiple-value-bind (,res ,type)
(mach:quick-subtestname ,file)
(declare (ignore ,res))
,type)))
,@body))))))
(defun directory (pathname &key (all t))
"Returns a list of pathnames, one for each file that matches the given
pathname. Supplying :all as nil causes this to ignore Unix dot files. This
never includes Unix dot and dot-dot in the result."
(setq pathname (pathname pathname))
(multiple-value-bind (dir pattern) (find-directory pathname)
(let ((res ()))
(do-directory (name etype pattern all)
(if (eq etype :entry_file)
(let ((last-dot (position #\. name :from-end t)))
(push
(%make-pathname
"Mach" :absolute dir
(if last-dot (subseq name 0 last-dot) name)
(if last-dot (subseq name (1+ last-dot)))
nil)
res))
(push
(%make-pathname
"Mach" :absolute
(concatenate 'simple-vector dir (vector name))
nil nil nil)
res)))
(nreverse res))))
;;; FIND-DIRECTORY returns an absolute directory vector for pathname as a
;;; first argument and an absolute namestring. The namestring includes a
;;; trailing asterisk, wildcard, when the given pathname is immediately
;;; probe-able as a directory.
;;;
(defun find-directory (pathname)
(multiple-value-bind (pn type)
(sub-probe-file pathname)
(if pn
(let ((ns (namestring pn)))
(declare (simple-string ns))
(case type
(:entry_directory
(when (char/= (schar ns (the fixnum (1- (length ns)))) #\/)
(setq ns (concatenate 'simple-string ns "/")))
(values (%pathname-directory (pathname ns))
(concatenate 'simple-string ns "*")))
(t (values (%pathname-directory pn) ns))))
(multiple-value-bind (pn type)
(sub-probe-file
(make-pathname
:directory (%pathname-directory pathname)
:device (%pathname-device pathname)))
(unless pn
(error "Directory does not exist: ~S" pathname))
(let ((ns (namestring pn)))
(case type
(:entry_directory
(values
(%pathname-directory pn)
(concatenate 'simple-string ns (file-namestring pathname))))
(t
(error "~S is not a directory." pathname))))))))
;;;; Printing directories and determining file owner names.
;;; PRINT-DIRECTORY is exported from the EXTENSIONS package.
;;;
(defun print-directory (pathname &optional stream &key all verbose return-list)
"Like Directory, but prints a terse, multi-coloumn directory listing
instead of returning a list of pathnames. When :all is supplied and
non-nil, then Unix dot files are included too (as ls -a). When :vervose
is supplied and non-nil, then a long listing of miscellaneous
information is output one file per line."
(setf pathname (pathname pathname))
(let ((*standard-output* (out-synonym-of stream)))
(if verbose
(print-directory-verbose pathname all return-list)
(print-directory-formatted pathname all return-list))))
(defun print-directory-verbose (pathname all return-list)
(multiple-value-bind (dir pattern) (find-directory pathname)
(declare (ignore dir))
(format t "Directory of ~A :~%" pattern)
(let ((dir-name (directory-namestring pattern))
(result ()))
(do-directory (name etype pattern all (nreverse result))
(let ((slash-name (if (eq etype :entry_file)
name
(concatenate 'simple-string name "/"))))
(declare (simple-string slash-name))
(when return-list
(push (pathname (concatenate 'simple-string dir-name slash-name))
result))
(multiple-value-bind
(reslt dev-or-err ino mode nlink uid gid rdev size atime mtime)
(mach:unix-stat (concatenate 'simple-string dir-name name))
(declare (ignore ino gid rdev atime)
(fixnum uid mode))
(cond (reslt
;;
;; Print characters for file modes.
(macrolet ((frob (bit name &optional sbit sname negate)
`(if ,(if negate
`(not (logbitp ,bit mode))
`(logbitp ,bit mode))
,(if sbit
`(if (logbitp ,sbit mode)
(write-char ,sname)
(write-char ,name))
`(write-char ,name))
(write-char #\-))))
(frob 15 #\d nil nil t)
(frob 8 #\r)
(frob 7 #\w)
(frob 6 #\x 11 #\s)
(frob 5 #\r)
(frob 4 #\w)
(frob 3 #\x 10 #\s)
(frob 2 #\r)
(frob 1 #\w)
(frob 0 #\x))
;;
;; Print the rest.
(multiple-value-bind (sec min hour date month year)
(get-decoded-time)
(declare (ignore sec min hour date month))
(format t "~2D ~8A ~8D ~12A ~A~%"
nlink
(or (lookup-login-name uid) uid)
size
(decode-universal-time-for-files mtime year)
slash-name)))
(t (format t "Couldn't stat ~A -- ~A.~%"
slash-name
(mach:get-unix-error-msg dev-or-err))))))))))
(defun decode-universal-time-for-files (time current-year)
(multiple-value-bind (sec min hour day month year)
(decode-universal-time (+ time unix-to-universal-time))
(declare (ignore sec))
(format nil "~A ~2,' D ~:[ ~D~;~*~2,'0D:~2,'0D~]"
(svref '#("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
"Sep" "Oct" "Nov" "Dec")
(1- month))
day (= current-year year) year hour min)))
(defun print-directory-formatted (pathname all return-list)
(let ((width (or (line-length *standard-output*) 80))
(names ())
(cnt 0)
(max-len 0)
(result ()))
(declare (list names) (fixnum max-len cnt))
;;
;; Get the data.
(multiple-value-bind (dir pattern) (find-directory pathname)
(declare (ignore dir))
(do-directory (name etype pattern all)
(let* ((slash-name (if (eql etype :entry_file)
name
(concatenate 'simple-string name "/")))
(len (length slash-name)))
(declare (simple-string slash-name)
(fixnum len))
(when return-list
(push (pathname (concatenate 'simple-string
(directory-namestring pattern)
slash-name))
result))