Debian Codegraph @

src/lsp/mislib.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 (in-package "SYSTEM")
12
13 (defun logical-pathname-translations (p)
14 (or (si:pathname-translations p)
15 (error 'simple-type-error
16 :datum p
17 :expected-type 'logical-pathname
18 :format-control "logical host not yet defined: ~S"
19 :format-arguments (list p))))
20
21 (defsetf logical-pathname-translations si:pathname-translations)
22
23 (defun load-logical-pathname-translations (host)
24 "Search for a logical pathname named host, if not already defined. If already
25 defined no attempt to find or load a definition is attempted and NIL is
26 returned. If host is not already defined, but definition is found and loaded
27 successfully, T is returned, else error."
28 (declare (type string host)
29 (ext:check-arguments-type))
30 (let ((*autoload-translations* nil))
31 (unless (or (string-equal host "sys")
32 (si::pathname-translations host))
33 (with-open-file (in-str (make-pathname :defaults "sys:"
34 :name (string-downcase host)
35 :type "translations"))
36 (if *load-verbose*
37 (format *error-output*
38 ";; Loading pathname translations from ~A~%"
39 (namestring (truename in-str))))
40 (setf (logical-pathname-translations host) (read in-str)))
41 t)))
42
43 (defparameter *do-time-level* -1)
44
45 (defun do-time (closure)
46 #-boehm-gc
47 (let* ((real-start (get-internal-real-time))
48 (run-start (get-internal-run-time))
49 gc-start
50 bytes-consed
51 real-end
52 run-end
53 gc-end)
54 ;; Garbage collection forces counters to be updated
55 (si::gc t)
56 (setf gc-start (si::gc-time))
57 (multiple-value-prog1
58 (funcall closure)
59 (setq run-end (get-internal-run-time)
60 real-end (get-internal-real-time)
61 gc-end (si::gc-time))
62 (format *trace-output*
63 "real time : ~,3F secs~%~
64 run time : ~,3F secs~%~
65 GC time : ~,3F secs~%"
66 (/ (- real-end real-start) internal-time-units-per-second)
67 (/ (- run-end run-start) internal-time-units-per-second)
68 (/ (- gc-end gc-start) internal-time-units-per-second))))
69 #+boehm-gc
70 (let* ((*do-time-level* (1+ *do-time-level*))
71 real-start
72 run-start
73 consed-start
74 gc-no-start
75 real-end
76 run-end
77 consed-end
78 gc-no-end)
79 ;; Garbage collection forces the value of counters to be updated
80 (si::gc t)
81 ;; If there are no nested calls, we just reset the counters
82 (when (zerop *do-time-level*) (si::gc-stats 0))
83 ;; but in general we copy the previous values.
84 (multiple-value-setq (consed-start gc-no-start) (gc-stats t))
85 (setq real-start (get-internal-real-time)
86 run-start (get-internal-run-time))
87 (multiple-value-prog1
88 (funcall closure)
89 (setq run-end (get-internal-run-time)
90 real-end (get-internal-real-time))
91 ;; Garbage collection forces the value of counters to be updated
92 (si::gc t)
93 (multiple-value-setq (consed-end gc-no-end) (gc-stats nil))
94 (fresh-line *trace-output*)
95 (format *trace-output*
96 "real time : ~,3F secs~%~
97 run time : ~,3F secs~%~
98 gc count : ~D times~%~
99 consed : ~D bytes~%"
100 (/ (- real-end real-start) internal-time-units-per-second)
101 (/ (- run-end run-start) internal-time-units-per-second)
102 (- gc-no-end gc-no-start)
103 (- consed-end consed-start)))))
104
105 (defmacro time (form)
106 "Syntax: (time form)
107 Evaluates FORM, outputs the realtime and runtime used for the evaluation to
108 *TRACE-OUTPUT*, and then returns all values of FORM."
109 `(do-time #'(lambda () ,form)))
110
111 (defun leap-year-p (y)
112 (declare (si::c-local))
113 (and (zerop (mod y 4))
114 (or (not (zerop (mod y 100))) (zerop (mod y 400)))))
115
116 (defun number-of-days-from-1900 (y)
117 (declare (si::c-local))
118 (let ((y1 (1- y)))
119 (+ (* (- y 1900) 365)
120 (floor y1 4) (- (floor y1 100)) (floor y1 400)
121 -460)))
122
123 (defconstant month-startdays #(0 31 59 90 120 151 181 212 243 273 304 334 365))
124
125 #-ecl-min
126 (ffi:clines "
127 #include <time.h>
128 ")
129
130 #-ecl-min
131 (defun get-local-time-zone ()
132 "Returns the number of hours West of Greenwich for the local time
133 zone and a boolean indicating whether Daylight Saving Time is in
134 effect."
135 (declare (si::c-local))
136 (ffi::c-inline () () (values :object :object) "
137 {
138 cl_fixnum mw;
139 #if 0 && defined(HAVE_TZSET)
140 tzset();
141 mw = timezone/60;
142 #else
143 struct tm ltm, gtm;
144 time_t when = time(0) /*0L*/;
145
146 ltm = *localtime(&when);
147 gtm = *gmtime(&when);
148
149 mw = (gtm.tm_min + 60 * gtm.tm_hour) - (ltm.tm_min + 60 * ltm.tm_hour);
150
151 if ((gtm.tm_wday + 1) % 7 == ltm.tm_wday)
152 mw -= 24*60;
153 else if (gtm.tm_wday == (ltm.tm_wday + 1) % 7)
154 mw += 24*60;
155 if (ltm.tm_isdst)
156 mw += 60;
157 #endif
158 @(return 0) = ecl_make_ratio(ecl_make_fixnum(mw),ecl_make_fixnum(60));
159 @(return 1) = ltm.tm_isdst ? ECL_T : ECL_NIL;
160 }"
161 :one-liner nil))
162
163 (defun recode-universal-time (sec min hour day month year tz dst)
164 (declare (si::c-local))
165 (let ((days (+ (if (and (leap-year-p year) (> month 2)) 1 0)
166 (1- day)
167 (svref month-startdays (1- month))
168 (number-of-days-from-1900 year))))
169 (+ sec (* 60 (+ min (* 60 (+ tz dst hour (* 24 days))))))))
170
171 (defun decode-universal-time (orig-ut &optional (tz nil tz-p) &aux (dstp nil))
172 "Args: (integer &optional (timezone (si::get-local-time-zone)))
173 Returns as nine values the day-and-time represented by INTEGER. See GET-
174 DECODED-TIME."
175 (loop
176 (let* ((ut orig-ut) sec min hour day month year dow days)
177 (unless tz
178 (setq tz (get-local-time-zone)))
179 (decf ut (round (* (+ tz (if dstp -1 0)) 3600)))
180 (multiple-value-setq (ut sec) (floor ut 60))
181 (multiple-value-setq (ut min) (floor ut 60))
182 (multiple-value-setq (days hour) (floor ut 24))
183 (setq dow (mod days 7))
184 (setq year (+ 1900 (floor days 366))) ; Guess!
185 (do ((x))
186 ((< (setq x (- days (number-of-days-from-1900 year)))
187 (if (leap-year-p year) 366 365))
188 (setq day (1+ x)))
189 (incf year))
190 (when (leap-year-p year)
191 (cond ((= day 60) (setf month 2 day 29))
192 ((> day 60) (decf day))))
193 (unless month
194 (setq month (position day month-startdays :test #'<=)
195 day (- day (svref month-startdays (1- month)))))
196 (if (and (not tz-p) (daylight-saving-time-p orig-ut year))
197 (setf tz-p t dstp t)
198 (return (values sec min hour day month year dow dstp tz))))))
199
200 (defun encode-universal-time (sec min hour day month year &optional tz)
201 "Args: (second minute hour date month year
202 &optional (timezone (si::get-local-time-zone)))
203 Returns an integer that represents the given day-and-time. See
204 GET-DECODED-TIME."
205 (when (<= 0 year 99)
206 ;; adjust to year in the century within 50 years of this year
207 (multiple-value-bind (sec min hour day month this-year dow dstp tz)
208 (get-decoded-time)
209 (declare (ignore sec min hour day month dow dstp tz))
210 (incf year (* 100 (ceiling (- this-year year 50) 100)))))
211 (let ((dst 0))
212 (unless tz
213 (setq tz (rational (get-local-time-zone)))
214 (when (daylight-saving-time-p (recode-universal-time sec min hour day month year tz -1) year)
215 ;; assume DST applies, and check if at corresponging UT it applies.
216 ;; There is an ambiguity between midnight and 1 o'clock on the day
217 ;; when time reverts from DST to solar:
218 ;; 12:01 on that day could be either 11:01 UT (before the switch) or
219 ;; 12:01 UT (after the switch). We opt for the former.
220 (setf dst -1)))
221 (recode-universal-time sec min hour day month year tz dst)))
222
223 (defun daylight-saving-time-p (universal-time year)
224 "Returns T if Daylight Saving Time applies to the local time zone at
225 Universal Time UT, which defaults to the current time."
226 (declare (si::c-local))
227 ;; Some systems cannot deal with dates before 1-1-1970 and no POSIX
228 ;; system will be able to handle dates beyond 2038. We must
229 ;; therefore restrict the time to the interval that can handled by
230 ;; the timezone database.
231 (let* ((utc-1-1-1970 2208988800)
232 (unix-time (- universal-time utc-1-1-1970)))
233 (cond ((minusp unix-time)
234 ;; For dates before 1970 we shift to 1980/81 to guess the daylight
235 ;; saving times.
236 (setf unix-time
237 (+ (if (leap-year-p year)
238 #.(encode-universal-time 0 0 0 1 1 1980 0)
239 #.(encode-universal-time 0 0 0 1 1 1981 0))
240 (- universal-time (encode-universal-time 0 0 0 1 1 year 0) utc-1-1-1970))))
241 ((not (fixnump unix-time))
242 ;; Same if date is too big: we shift to year 2035/36, like SBCL does.
243 (setf unix-time
244 (+ (if (leap-year-p year)
245 #.(encode-universal-time 0 0 0 1 1 2032 0)
246 #.(encode-universal-time 0 0 0 1 1 2033 0))
247 (- universal-time (encode-universal-time 0 0 0 1 1 year 0) utc-1-1-1970)))))
248 #-ecl-min
249 (ffi::c-inline (unix-time) (#+long-long :unsigned-long-long #-long-long :unsigned-long) :bool "
250 {
251 time_t when = (#0);
252 struct tm *ltm = localtime(&when);
253 @(return) = (ltm != NULL) && ltm->tm_isdst;
254 }"
255 :one-liner nil)))
256
257 (defun get-decoded-time ()
258 "Args: ()
259 Returns the current day-and-time as nine values:
260 second (0 - 59)
261 minute (0 - 59)
262 hour (0 - 23)
263 date (1 - 31)
264 month (1 - 12)
265 year (Christian, not Japanese long-live-Emperor)
266 day of week (0 for Mon, .. 6 for Sun)
267 summer time or not (T or NIL)
268 time zone (-9 in Japan)
269 Sunday is the *last* day of the week!!"
270 (decode-universal-time (get-universal-time)))
271
272 (defun ensure-directories-exist (pathname &key verbose (mode #o777))
273 "Args: (ensure-directories pathname &key :verbose)
274 Creates tree of directories specified by the given pathname. Outputs
275 (VALUES pathname created)
276 where CREATED is true only if we succeeded on creating all directories."
277 (let* ((created nil)
278 (full-pathname (merge-pathnames pathname))
279 d)
280 (when (typep full-pathname 'logical-pathname)
281 (setf full-pathname (translate-logical-pathname full-pathname)))
282 (when (or (wild-pathname-p full-pathname :directory)
283 (wild-pathname-p full-pathname :host)
284 (wild-pathname-p full-pathname :device))
285 (error 'file-error :pathname pathname))
286 ;; Here we have already a full pathname. We set our own
287 ;; *default-pathname-defaults* to avoid that the user's value,
288 ;; which may contain names or types, clobbers our computations.
289 (let ((*default-pathname-defaults*
290 (make-pathname :name nil :type nil :directory nil
291 :defaults full-pathname)))
292 (dolist (item (pathname-directory full-pathname))
293 (setf d (nconc d (list item)))
294 (let* ((p (make-pathname :directory d :defaults *default-pathname-defaults*)))
295 (unless (symbolp item)
296 (let* ((ps (namestring p))
297 (newly-created (si::mkdir ps mode)))
298 (when newly-created
299 (setf created t)
300 (when verbose
301 (format t "~%;;; Making directory ~A" ps)))))))
302 (values pathname created))))
303
304 (defmacro with-hash-table-iterator ((iterator package) &body body)
305 "Syntax: (with-hash-table-iterator (iterator package) &body body)
306 Loop over the elements of a hash table. ITERATOR is a lexically bound function
307 that outputs three values
308 (VALUES entry-p key value)
309 ENTRY-P is true only if KEY and VALUE denote a pair of key and value of the
310 hash table; otherwise it signals that we have reached the end of the hash table."
311 `(let ((,iterator (hash-table-iterator ,package)))
312 (macrolet ((,iterator () (list 'funcall ',iterator)))
313 ,@body)))
314
315 (defun sharp-!-reader (stream subchar arg)
316 (declare (ignore arg subchar))
317 (read-line stream)
318 (values))
319
320 (set-dispatch-macro-character #\# #\! 'sharp-!-reader)
321
322 (defun si::simple-program-error (message &rest datum)
323 (signal-simple-error 'program-error nil message datum))
324
325 #-ecl-min
326 (defun make-stream-from-fd (fd direction &key buffering
327 element-type
328 (external-format :default)
329 (name "FD-STREAM"))
330 (check-type name string "name must be a string.")
331 (macrolet ((c-const (string) `(ffi:c-inline () () :int ,string :one-liner t)))
332 (let* ((smm-mode
333 (ecase direction
334 (:input (c-const "ecl_smm_input"))
335 (:output (c-const "ecl_smm_output"))
336 ((:io :input-output) (c-const "ecl_smm_io"))
337 #+:wsock (:input-wsock (c-const "ecl_smm_input_wsock"))
338 #+:wsock (:output-wsock (c-const "ecl_smm_output_wsock"))
339 #+:wsock ((:io-wsock :input-output-wsock) (c-const "ecl_smm_io_wsock"))
340 #+:wsock ((:io-wcon :input-output-wcon) (c-const "ecl_smm_io_wcon"))))
341 ;; if external-format is not NIL, flags are ignored
342 (external-format (unless (subtypep element-type 'integer) external-format))
343 (stream (ffi:c-inline (name fd smm-mode element-type external-format)
344 (t :int :int t t) stream
345 "
346 ecl_make_stream_from_fd(#0,#1,(enum ecl_smmode)#2,
347 ecl_normalize_stream_element_type(#3),
348 ECL_STREAM_BINARY,
349 #4)"
350 :one-liner t)))
351 (when buffering
352 (si::set-buffering-mode stream buffering))
353 stream)))