1 | ;;;-*-Mode: LISP; Package: CCL -*- |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 1994-2001 Digitool, Inc |
---|
4 | ;;; This file is part of OpenMCL. |
---|
5 | ;;; |
---|
6 | ;;; OpenMCL is licensed under the terms of the Lisp Lesser GNU Public |
---|
7 | ;;; License , known as the LLGPL and distributed with OpenMCL as the |
---|
8 | ;;; file "LICENSE". The LLGPL consists of a preamble and the LGPL, |
---|
9 | ;;; which is distributed with OpenMCL as the file "LGPL". Where these |
---|
10 | ;;; conflict, the preamble takes precedence. |
---|
11 | ;;; |
---|
12 | ;;; OpenMCL is referenced in the preamble as the "LIBRARY." |
---|
13 | ;;; |
---|
14 | ;;; The LLGPL is also available online at |
---|
15 | ;;; http://opensource.franz.com/preamble.html |
---|
16 | |
---|
17 | (in-package "CCL") |
---|
18 | |
---|
19 | (eval-when (eval compile) |
---|
20 | (require 'defstruct-macros)) |
---|
21 | |
---|
22 | (defun short-site-name () |
---|
23 | "Return a string with the abbreviated site name, or NIL if not known." |
---|
24 | (or *short-site-name* "unspecified")) |
---|
25 | |
---|
26 | (defun long-site-name () |
---|
27 | "Return a string with the long form of the site name, or NIL if not known." |
---|
28 | (or *long-site-name* "unspecified")) |
---|
29 | |
---|
30 | (defun machine-instance () |
---|
31 | "Return a string giving the name of the local machine." |
---|
32 | (%uname 1)) |
---|
33 | |
---|
34 | |
---|
35 | (defun machine-type () |
---|
36 | "Returns a string describing the type of the local machine." |
---|
37 | (%uname 4)) |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | (defloadvar *machine-version* nil) |
---|
42 | |
---|
43 | (defun machine-version () |
---|
44 | "Return a string describing the version of the computer hardware we |
---|
45 | are running on, or NIL if we can't find any useful information." |
---|
46 | (or *machine-version* |
---|
47 | (setq *machine-version* |
---|
48 | #+darwin-target |
---|
49 | (block darwin-machine-version |
---|
50 | (%stack-block ((mib 8)) |
---|
51 | (setf (%get-long mib 0) #$CTL_HW |
---|
52 | (%get-long mib 4) #$HW_MODEL) |
---|
53 | (%stack-block ((res 256) |
---|
54 | (reslen target::node-size)) |
---|
55 | (setf (%get-byte res 0) 0 |
---|
56 | (%get-natural reslen 0) 256) |
---|
57 | (if (zerop (#_sysctl mib 2 res reslen (%null-ptr) 0)) |
---|
58 | (return-from darwin-machine-version (%get-cstring res)))))) |
---|
59 | #+linux-target |
---|
60 | (with-open-file (f "/proc/cpuinfo" :if-does-not-exist nil) |
---|
61 | (when f |
---|
62 | (flet ((cpu-info-match (target line) |
---|
63 | (let* ((targetlen (length target)) |
---|
64 | (linelen (length line))) |
---|
65 | (if (and (> linelen targetlen) |
---|
66 | (string= target line |
---|
67 | :end2 targetlen)) |
---|
68 | (let* ((colonpos (position #\: line))) |
---|
69 | (when colonpos |
---|
70 | (string-trim " " |
---|
71 | (subseq line (1+ colonpos))))))))) |
---|
72 | (do* ((line (read-line f nil nil) |
---|
73 | (read-line f nil nil)) |
---|
74 | (target #+ppc-target "machine" |
---|
75 | #+x86-target "model name")) |
---|
76 | ((null line)) |
---|
77 | (let* ((matched (cpu-info-match target line))) |
---|
78 | (when matched (return matched))))))) |
---|
79 | #+freebsd-target |
---|
80 | (%stack-block ((ret 512) |
---|
81 | (mib (* (record-length :uint)))) |
---|
82 | (setf (%get-unsigned-long mib 0) |
---|
83 | #$CTL_HW |
---|
84 | (%get-unsigned-long mib (record-length :uint)) |
---|
85 | #$HW_MODEL) |
---|
86 | (rlet ((oldsize :uint 512)) |
---|
87 | (if (eql 0 (#_sysctl mib 2 ret oldsize (%null-ptr) 0)) |
---|
88 | (%get-cstring ret) |
---|
89 | 1))) |
---|
90 | ))) |
---|
91 | |
---|
92 | |
---|
93 | (defun software-type () |
---|
94 | "Return a string describing the supporting software." |
---|
95 | (%uname 0)) |
---|
96 | |
---|
97 | |
---|
98 | (defun software-version () |
---|
99 | "Return a string describing version of the supporting software, or NIL |
---|
100 | if not available." |
---|
101 | (%uname 2)) |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | ;;; Yawn. |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | (defmethod documentation (thing doc-id) |
---|
114 | (%get-documentation thing doc-id)) |
---|
115 | |
---|
116 | (defun set-documentation (thing doc-id new) |
---|
117 | (setf (documentation thing doc-id) new)) |
---|
118 | |
---|
119 | (defmethod (setf documentation) (new thing doc-id) |
---|
120 | (%put-documentation thing doc-id new)) |
---|
121 | |
---|
122 | |
---|
123 | (defmethod documentation ((symbol symbol) (doc-type (eql 'function))) |
---|
124 | (let* ((def (fboundp symbol))) ; FBOUNDP returns info about definition |
---|
125 | (when def |
---|
126 | (%get-documentation def t)))) |
---|
127 | |
---|
128 | (defmethod (setf documentation) ((new t) |
---|
129 | (symbol symbol) |
---|
130 | (doc-type (eql 'function))) |
---|
131 | (let* ((def (fboundp symbol))) ; FBOUNDP returns info about definition |
---|
132 | (when def |
---|
133 | (%put-documentation def |
---|
134 | t |
---|
135 | new)) |
---|
136 | new)) |
---|
137 | |
---|
138 | (defmethod documentation ((symbol symbol) (doc-type (eql 'setf))) |
---|
139 | (call-next-method)) |
---|
140 | |
---|
141 | (defmethod (setf documentation) ((new t) |
---|
142 | (symbol symbol) |
---|
143 | (doc-type (eql 'setf))) |
---|
144 | (call-next-method)) |
---|
145 | |
---|
146 | |
---|
147 | (defmethod documentation ((symbol symbol) (doc-type (eql 'variable))) |
---|
148 | (call-next-method)) |
---|
149 | |
---|
150 | (defmethod (setf documentation) ((new t) |
---|
151 | (symbol symbol) |
---|
152 | (doc-type (eql 'variable))) |
---|
153 | (call-next-method)) |
---|
154 | |
---|
155 | (defmethod documentation ((symbol symbol) (doc-type (eql 'compiler-macro))) |
---|
156 | (call-next-method)) |
---|
157 | |
---|
158 | (defmethod (setf documentation) ((new t) |
---|
159 | (symbol symbol) |
---|
160 | (doc-type (eql 'compiler-macro))) |
---|
161 | (call-next-method)) |
---|
162 | |
---|
163 | (defmethod documentation ((symbol symbol) (doc-type (eql 'type))) |
---|
164 | (let* ((class (find-class symbol nil))) |
---|
165 | (if class |
---|
166 | (documentation class doc-type) |
---|
167 | (call-next-method)))) |
---|
168 | |
---|
169 | (defmethod (setf documentation) (new (symbol symbol) (doc-type (eql 'type))) |
---|
170 | (let* ((class (find-class symbol nil))) |
---|
171 | (if class |
---|
172 | (setf (documentation class doc-type) new) |
---|
173 | (call-next-method)))) |
---|
174 | |
---|
175 | (defmethod documentation ((symbol symbol) (doc-type (eql 'method-combination))) |
---|
176 | (let* ((mci (method-combination-info symbol))) |
---|
177 | (if mci |
---|
178 | (documentation mci doc-type)))) |
---|
179 | |
---|
180 | (defmethod (setf documentation) ((new t) |
---|
181 | (symbol symbol) |
---|
182 | (doc-type (eql 'method-combination))) |
---|
183 | (let* ((mci (method-combination-info symbol))) |
---|
184 | (if mci |
---|
185 | (setf (documentation mci doc-type) new)))) |
---|
186 | |
---|
187 | |
---|
188 | (defmethod documentation ((symbol symbol) (doc-type (eql 'structure))) |
---|
189 | (let* ((class (find-class symbol nil))) |
---|
190 | (if (typep class 'structure-class) |
---|
191 | (documentation class 'type) |
---|
192 | (call-next-method)))) |
---|
193 | |
---|
194 | (defmethod (setf documentation) ((new t) |
---|
195 | (symbol symbol) |
---|
196 | (doc-type (eql 'structure))) |
---|
197 | (let* ((class (find-class symbol nil))) |
---|
198 | (if (typep class 'structure-class) |
---|
199 | (setf (documentation class 'type) new) |
---|
200 | (call-next-method)))) |
---|
201 | |
---|
202 | (defmethod documentation ((p package) (doc-type (eql 't))) |
---|
203 | (call-next-method)) |
---|
204 | |
---|
205 | (defmethod (setf documentation) ((new t) (p package) (doc-type (eql 't))) |
---|
206 | (call-next-method)) |
---|
207 | |
---|
208 | (defmethod documentation ((f function) (doc-type (eql 't))) |
---|
209 | (call-next-method)) |
---|
210 | |
---|
211 | (defmethod (setf documentation) ((new t) (f function) (doc-type (eql 't))) |
---|
212 | (call-next-method)) |
---|
213 | |
---|
214 | (defmethod documentation ((f function) (doc-type (eql 'function))) |
---|
215 | (documentation f t)) |
---|
216 | |
---|
217 | (defmethod (setf documentation) ((new t) |
---|
218 | (f function) |
---|
219 | (doc-type (eql 'function))) |
---|
220 | (setf (documentation f t) new)) |
---|
221 | |
---|
222 | (defmethod documentation ((l cons) (doc-type (eql 'function))) |
---|
223 | (let* ((name (setf-function-spec-name l))) |
---|
224 | (if name |
---|
225 | (documentation name doc-type) |
---|
226 | (%get-documentation l doc-type)))) |
---|
227 | |
---|
228 | (defmethod (setf documentation) ((new t) (l cons) (doc-type (eql 'function))) |
---|
229 | (let* ((name (setf-function-spec-name l))) |
---|
230 | (if name |
---|
231 | (setf (documentation name doc-type) new) |
---|
232 | (%put-documentation l doc-type new)))) |
---|
233 | |
---|
234 | |
---|
235 | (defmethod documentation ((l cons) (doc-type (eql 'compiler-macro))) |
---|
236 | (let* ((name (setf-function-spec-name l))) |
---|
237 | (if name |
---|
238 | (documentation name doc-type) |
---|
239 | (%get-documentation l doc-type)))) |
---|
240 | |
---|
241 | (defmethod (setf documentation) ((new t) (l cons) (doc-type (eql 'compiler-macr0))) |
---|
242 | (let* ((name (setf-function-spec-name l))) |
---|
243 | (if name |
---|
244 | (setf (documentation name doc-type) new) |
---|
245 | (%put-documentation l doc-type new)))) |
---|
246 | |
---|
247 | |
---|
248 | (defmethod documentation ((m method-combination) |
---|
249 | (doc-type (eql 'method-combination))) |
---|
250 | (call-next-method)) |
---|
251 | |
---|
252 | (defmethod (setf documentation) ((new t) |
---|
253 | (m method-combination) |
---|
254 | (doc-type (eql 'method-combination))) |
---|
255 | (call-next-method)) |
---|
256 | |
---|
257 | (defmethod documentation ((m method-combination) |
---|
258 | (doc-type (eql t))) |
---|
259 | (documentation m 'method-combination)) |
---|
260 | |
---|
261 | (defmethod (setf documentation) ((new t) |
---|
262 | (m method-combination) |
---|
263 | (doc-type (eql t))) |
---|
264 | (setf (documentation m 'method-combination) new)) |
---|
265 | |
---|
266 | (defmethod documentation ((m standard-method) |
---|
267 | (doc-type (eql t))) |
---|
268 | (call-next-method)) |
---|
269 | |
---|
270 | (defmethod (setf documentation) ((new t) |
---|
271 | (m standard-method) |
---|
272 | (doc-type (eql t))) |
---|
273 | (call-next-method)) |
---|
274 | |
---|
275 | (defmethod documentation ((c standard-class) (doc-type (eql 'type))) |
---|
276 | (call-next-method)) |
---|
277 | |
---|
278 | (defmethod (setf documentation) ((new t) |
---|
279 | (c standard-class) |
---|
280 | (doc-type (eql 'type))) |
---|
281 | (call-next-method)) |
---|
282 | |
---|
283 | (defmethod documentation ((c standard-class) (doc-type (eql 't))) |
---|
284 | (documentation c 'type)) |
---|
285 | |
---|
286 | (defmethod (setf documentation) ((new t) |
---|
287 | (c standard-class) |
---|
288 | (doc-type (eql 't))) |
---|
289 | (setf (documentation c 'type) new)) |
---|
290 | |
---|
291 | (defmethod documentation ((c structure-class) (doc-type (eql 'type))) |
---|
292 | (call-next-method)) |
---|
293 | |
---|
294 | (defmethod (setf documentation) ((new t) |
---|
295 | (c structure-class) |
---|
296 | (doc-type (eql 'type))) |
---|
297 | (call-next-method)) |
---|
298 | |
---|
299 | (defmethod documentation ((c structure-class) (doc-type (eql 't))) |
---|
300 | (documentation c 'type)) |
---|
301 | |
---|
302 | (defmethod (setf documentation) ((new t) |
---|
303 | (c structure-class) |
---|
304 | (doc-type (eql 't))) |
---|
305 | (setf (documentation c 'type) new)) |
---|
306 | |
---|
307 | ;;; This is now deprecated; things which call it should stop doing so. |
---|
308 | (defun set-documentation (symbol doc-type string) |
---|
309 | (setf (documentation symbol doc-type) string)) |
---|
310 | |
---|
311 | (defun set-function-info (symbol info) |
---|
312 | (let* ((doc-string (if (consp info) (car info) info))) |
---|
313 | (if (and *save-doc-strings* (stringp doc-string)) |
---|
314 | (set-documentation symbol 'function doc-string))) |
---|
315 | (let* ((cons (assq symbol *nx-globally-inline*)) |
---|
316 | (lambda-expression (if (consp info) (cdr info)))) |
---|
317 | (if (and (proclaimed-inline-p symbol) |
---|
318 | (not (compiler-special-form-p symbol)) |
---|
319 | (lambda-expression-p lambda-expression) |
---|
320 | (let* ((lambda-list (cadr lambda-expression))) |
---|
321 | (and (not (memq '&lap lambda-list)) |
---|
322 | (not (memq '&method lambda-list)) |
---|
323 | (not (memq '&lexpr lambda-list))))) |
---|
324 | (if cons |
---|
325 | (%rplacd cons lambda-expression) |
---|
326 | (push (cons symbol lambda-expression) *nx-globally-inline*)) |
---|
327 | (if cons (setq *nx-globally-inline* (delete cons *nx-globally-inline*))))) |
---|
328 | symbol) |
---|
329 | |
---|
330 | |
---|
331 | (setf (documentation 'if 'function) |
---|
332 | "If Predicate Then [Else] |
---|
333 | If Predicate evaluates to non-null, evaluate Then and returns its values, |
---|
334 | otherwise evaluate Else and return its values. Else defaults to NIL.") |
---|
335 | |
---|
336 | (setf (documentation 'progn 'function) |
---|
337 | "progn form* |
---|
338 | Evaluates each FORM and returns the value(s) of the last FORM.") |
---|
339 | |
---|
340 | (defmethod documentation ((thing character-encoding) (doc-type (eql t))) |
---|
341 | (character-encoding-documentation thing)) |
---|
342 | |
---|
343 | (defmethod (setf documentation) (new (thing character-encoding) (doc-type (eql t))) |
---|
344 | (check-type new (or null string)) |
---|
345 | (setf (character-encoding-documentation thing) new)) |
---|
346 | |
---|
347 | (defmethod documentation ((thing symbol) (doc-type (eql 'character-encoding))) |
---|
348 | (let* ((encoding (lookup-character-encoding (intern (string thing) :keyword)))) |
---|
349 | (when encoding |
---|
350 | (documentation encoding t)))) |
---|
351 | |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | #| |
---|
356 | (setf (documentation 'car 'variable) "Preferred brand of automobile") |
---|
357 | (documentation 'car 'variable) |
---|
358 | (setf (documentation 'foo 'structure) "the structure is grand.") |
---|
359 | (documentation 'foo 'structure) |
---|
360 | (setf (documentation 'foo 'variable) "the metasyntactic remarker") |
---|
361 | (documentation 'foo 'variable) |
---|
362 | (setf (documentation 'foo 'obscure) "no one really knows what it means") |
---|
363 | (documentation 'foo 'obscure) |
---|
364 | (setf (documentation 'foo 'structure) "the structure is solid") |
---|
365 | (documentation 'foo 'function) |
---|
366 | ||# |
---|
367 | |
---|
368 | ;; |
---|
369 | |
---|
370 | (defparameter *report-time-function* nil |
---|
371 | "If non-NULL, should be a function which accepts the following |
---|
372 | keyword arguments: |
---|
373 | :FORM the form that was executed |
---|
374 | :RESULTS a list of all values returned by the execution of FORM |
---|
375 | :ELAPSED-TIME total elapsed (real) time, in milliseconds |
---|
376 | :USER-TIME elapsed user time, in milliseconds |
---|
377 | :SYSTEM-TIME elapsed system time, in milliseconds |
---|
378 | :GC-TIME total real time spent in the GC, in milliseconds |
---|
379 | :BYTES-ALLOCATED total bytes allocated |
---|
380 | :MINOR-PAGE-FAULTS minor page faults |
---|
381 | :MAJOR-PAGE-FAULTS major page faults |
---|
382 | :SWAPS swaps") |
---|
383 | |
---|
384 | |
---|
385 | (defun standard-report-time (&key form results elapsed-time user-time |
---|
386 | system-time gc-time bytes-allocated |
---|
387 | minor-page-faults major-page-faults |
---|
388 | swaps) |
---|
389 | (let* ((s *trace-output*) |
---|
390 | (cpu-count (cpu-count))) |
---|
391 | (format s "~&~S took ~:D milliseconds (~,3F seconds) to run ~%~20twith ~D available CPU core~P." |
---|
392 | form elapsed-time (/ elapsed-time internal-time-units-per-second) cpu-count cpu-count) |
---|
393 | (format s "~&During that period, ~:D milliseconds (~,3F seconds) were spent in user mode" user-time (/ user-time internal-time-units-per-second)) |
---|
394 | (format s "~& ~:D milliseconds (~,3F seconds) were spent in system mode" system-time (/ system-time internal-time-units-per-second)) |
---|
395 | (unless (eql gc-time 0) |
---|
396 | (format s |
---|
397 | "~%~:D milliseconds (~,3F seconds) was spent in GC." |
---|
398 | gc-time (/ gc-time internal-time-units-per-second))) |
---|
399 | (unless (eql 0 bytes-allocated) |
---|
400 | (format s "~% ~:D bytes of memory allocated." bytes-allocated)) |
---|
401 | (when (or (> minor-page-faults 0) |
---|
402 | (> major-page-faults 0) |
---|
403 | (> swaps 0)) |
---|
404 | (format s |
---|
405 | "~% ~:D minor page faults, ~:D major page faults, ~:D swaps." |
---|
406 | minor-page-faults major-page-faults swaps)) |
---|
407 | (format s "~&") |
---|
408 | (values-list results))) |
---|
409 | |
---|
410 | (defun report-time (form thunk) |
---|
411 | (flet ((integer-size-in-bytes (i) |
---|
412 | (if (typep i 'fixnum) |
---|
413 | 0 |
---|
414 | (* (logand (+ 2 (uvsize i)) (lognot 1)) 4)))) |
---|
415 | (rlet ((start :rusage) |
---|
416 | (stop :rusage) |
---|
417 | (timediff :timeval)) |
---|
418 | (let* ((initial-real-time (get-internal-real-time)) |
---|
419 | (initial-gc-time (gctime)) |
---|
420 | (initial-consed (total-bytes-allocated)) |
---|
421 | (initial-overhead (integer-size-in-bytes initial-consed))) |
---|
422 | (%%rusage start) |
---|
423 | (let* ((results (multiple-value-list (funcall thunk)))) |
---|
424 | (declare (dynamic-extent results)) |
---|
425 | (%%rusage stop) |
---|
426 | (let* ((new-consed (total-bytes-allocated)) |
---|
427 | (bytes-consed |
---|
428 | (- new-consed (+ initial-overhead initial-consed))) |
---|
429 | (elapsed-real-time |
---|
430 | (- (get-internal-real-time) initial-real-time)) |
---|
431 | (elapsed-gc-time (- (gctime) initial-gc-time)) |
---|
432 | (elapsed-user-time |
---|
433 | (progn |
---|
434 | (%sub-timevals timediff |
---|
435 | (pref stop :rusage.ru_utime) |
---|
436 | (pref start :rusage.ru_utime)) |
---|
437 | (timeval->milliseconds timediff))) |
---|
438 | (elapsed-system-time |
---|
439 | (progn |
---|
440 | (%sub-timevals timediff |
---|
441 | (pref stop :rusage.ru_stime) |
---|
442 | (pref start :rusage.ru_stime)) |
---|
443 | (timeval->milliseconds timediff))) |
---|
444 | (elapsed-minor (- (pref stop :rusage.ru_minflt) |
---|
445 | (pref start :rusage.ru_minflt))) |
---|
446 | (elapsed-major (- (pref stop :rusage.ru_majflt) |
---|
447 | (pref start :rusage.ru_majflt))) |
---|
448 | (elapsed-swaps (- (pref stop :rusage.ru_nswap) |
---|
449 | (pref start :rusage.ru_nswap)))) |
---|
450 | (funcall (or *report-time-function* |
---|
451 | #'standard-report-time) |
---|
452 | :form form |
---|
453 | :results results |
---|
454 | :elapsed-time elapsed-real-time |
---|
455 | :user-time elapsed-user-time |
---|
456 | :system-time elapsed-system-time |
---|
457 | :gc-time elapsed-gc-time |
---|
458 | :bytes-allocated bytes-consed |
---|
459 | :minor-page-faults elapsed-minor |
---|
460 | :major-page-faults elapsed-major |
---|
461 | :swaps elapsed-swaps))))))) |
---|
462 | |
---|
463 | |
---|
464 | |
---|
465 | |
---|
466 | ;;; site names and machine-instance is in the init file. |
---|
467 | |
---|
468 | (defun add-feature (symbol) |
---|
469 | "Not CL but should be." |
---|
470 | (if (symbolp symbol) |
---|
471 | (if (not (memq symbol *features*)) |
---|
472 | (setq *features* (cons symbol *features*))))) |
---|
473 | |
---|
474 | ;;; (dotimes (i 5000) (declare (fixnum i)) (add-feature 'junk)) |
---|
475 | |
---|
476 | |
---|
477 | |
---|
478 | |
---|
479 | ;;; Misc string functions |
---|
480 | |
---|
481 | |
---|
482 | (defun string-left-trim (char-bag string &aux end) |
---|
483 | "Given a set of characters (a list or string) and a string, returns |
---|
484 | a copy of the string with the characters in the set removed from the |
---|
485 | left end." |
---|
486 | (setq string (string string)) |
---|
487 | (setq end (length string)) |
---|
488 | (do ((index 0 (%i+ index 1))) |
---|
489 | ((or (eq index end) (not (find (aref string index) char-bag))) |
---|
490 | (subseq string index end)))) |
---|
491 | |
---|
492 | (defun string-right-trim (char-bag string &aux end) |
---|
493 | "Given a set of characters (a list or string) and a string, returns |
---|
494 | a copy of the string with the characters in the set removed from the |
---|
495 | right end." |
---|
496 | (setq string (string string)) |
---|
497 | (setq end (length string)) |
---|
498 | (do ((index (%i- end 1) (%i- index 1))) |
---|
499 | ((or (%i< index 0) (not (find (aref string index) char-bag))) |
---|
500 | (subseq string 0 (%i+ index 1))))) |
---|
501 | |
---|
502 | (defun string-trim (char-bag string &aux end) |
---|
503 | "Given a set of characters (a list or string) and a string, returns a |
---|
504 | copy of the string with the characters in the set removed from both |
---|
505 | ends." |
---|
506 | (setq string (string string)) |
---|
507 | (setq end (length string)) |
---|
508 | (let ((left-end) (right-end)) |
---|
509 | (do ((index 0 (%i+ index 1))) |
---|
510 | ((or (eq index end) (not (find (aref string index) char-bag))) |
---|
511 | (setq left-end index))) |
---|
512 | (do ((index (%i- end 1) (%i- index 1))) |
---|
513 | ((or (%i< index left-end) (not (find (aref string index) char-bag))) |
---|
514 | (setq right-end index))) |
---|
515 | (subseq string left-end (%i+ right-end 1)))) |
---|
516 | |
---|
517 | |
---|
518 | |
---|
519 | (defun copy-symbol (symbol &optional (copy-props nil) &aux new-symbol def) |
---|
520 | "Make and return a new uninterned symbol with the same print name |
---|
521 | as SYMBOL. If COPY-PROPS is false, the new symbol is neither bound |
---|
522 | nor fbound and has no properties, else it has a copy of SYMBOL's |
---|
523 | function, value and property list." |
---|
524 | (setq new-symbol (make-symbol (symbol-name symbol))) |
---|
525 | (when copy-props |
---|
526 | (when (boundp symbol) |
---|
527 | (set new-symbol (symbol-value symbol))) |
---|
528 | (when (setq def (fboundp symbol)) |
---|
529 | ;;;Shouldn't err out on macros/special forms. |
---|
530 | (%fhave new-symbol def)) |
---|
531 | (set-symbol-plist new-symbol (copy-list (symbol-plist symbol)))) |
---|
532 | new-symbol) |
---|
533 | |
---|
534 | |
---|
535 | (defvar %gentemp-counter 0 |
---|
536 | "Counter for generating unique GENTEMP symbols.") |
---|
537 | |
---|
538 | (defun gentemp (&optional (prefix "T") (package *package*)) |
---|
539 | "Creates a new symbol interned in package PACKAGE with the given PREFIX." |
---|
540 | (loop |
---|
541 | (let* ((new-pname (%str-cat (ensure-simple-string prefix) |
---|
542 | (%integer-to-string %gentemp-counter))) |
---|
543 | (sym (find-symbol new-pname package))) |
---|
544 | (if sym |
---|
545 | (setq %gentemp-counter (%i+ %gentemp-counter 1)) |
---|
546 | (return (values (intern new-pname package))))))) ; 1 value. |
---|
547 | |
---|
548 | |
---|
549 | |
---|
550 | |
---|
551 | (defun add-gc-hook (hook-function &optional (which-hook :pre-gc)) |
---|
552 | (ecase which-hook |
---|
553 | (:pre-gc |
---|
554 | (pushnew hook-function *pre-gc-hook-list*) |
---|
555 | (setq *pre-gc-hook* #'(lambda () |
---|
556 | (dolist (hook *pre-gc-hook-list*) |
---|
557 | (funcall hook))))) |
---|
558 | (:post-gc |
---|
559 | (pushnew hook-function *post-gc-hook-list*) |
---|
560 | (setq *post-gc-hook* #'(lambda () |
---|
561 | (dolist (hook *post-gc-hook-list*) |
---|
562 | (funcall hook)))))) |
---|
563 | hook-function) |
---|
564 | |
---|
565 | (defun remove-gc-hook (hook-function &optional (which-hook :pre-gc)) |
---|
566 | (ecase which-hook |
---|
567 | (:pre-gc |
---|
568 | (unless (setq *pre-gc-hook-list* (delq hook-function *pre-gc-hook-list*)) |
---|
569 | (setq *pre-gc-hook* nil))) |
---|
570 | (:post-gc |
---|
571 | (unless (setq *post-gc-hook-list* (delq hook-function *post-gc-hook-list*)) |
---|
572 | (setq *post-gc-hook* nil))))) |
---|
573 | |
---|
574 | |
---|
575 | |
---|
576 | |
---|
577 | |
---|
578 | |
---|
579 | (defun find-method-by-names (name qualifiers specializers) |
---|
580 | (let ((gf (fboundp name))) |
---|
581 | (when gf |
---|
582 | (if (not (standard-generic-function-p gf)) |
---|
583 | (error "~S is not a generic-function." gf) |
---|
584 | (let ((methods (%gf-methods gf))) |
---|
585 | (when methods |
---|
586 | (let* ((spec-len (length (%method-specializers (car methods)))) |
---|
587 | (new-specs (make-list spec-len :initial-element (find-class t)))) |
---|
588 | (declare (dynamic-extent new-specs)) |
---|
589 | (do ((specs specializers (cdr specs)) |
---|
590 | (nspecs new-specs (cdr nspecs))) |
---|
591 | ((or (null specs) (null nspecs))) |
---|
592 | (let ((s (car specs))) |
---|
593 | (rplaca nspecs (if (consp s) s (find-class s nil))))) |
---|
594 | (find-method gf qualifiers new-specs nil)))))))) |
---|
595 | |
---|
596 | |
---|
597 | |
---|
598 | (defun get-string-from-user (prompt) |
---|
599 | (with-terminal-input |
---|
600 | (format *query-io* "~&~a " prompt) |
---|
601 | (force-output *query-io*) |
---|
602 | (clear-input *query-io*) |
---|
603 | (values (read-line *query-io*)))) |
---|
604 | |
---|
605 | |
---|
606 | (defun select-item-from-list (list &key (window-title "Select one of the following") |
---|
607 | (table-print-function #'prin1) |
---|
608 | &allow-other-keys) |
---|
609 | (block get-answer |
---|
610 | (with-terminal-input |
---|
611 | (format *query-io* "~a:~%" window-title) |
---|
612 | (loop |
---|
613 | (catch :redisplay |
---|
614 | (do* ((l list (cdr l)) |
---|
615 | (i 0 (1+ i)) |
---|
616 | (item (car l) (car l))) |
---|
617 | ((null l)) |
---|
618 | (declare (fixnum i)) |
---|
619 | (format *query-io* "~& ~d: " i) |
---|
620 | (funcall table-print-function item *query-io*)) |
---|
621 | (loop |
---|
622 | (fresh-line *query-io*) |
---|
623 | (let* ((string (get-string-from-user "Selection [number,q,r,?]:")) |
---|
624 | (value (ignore-errors |
---|
625 | (let* ((*package* *keyword-package*)) |
---|
626 | (read-from-string string nil))))) |
---|
627 | (cond ((eq value :q) (throw :cancel t)) |
---|
628 | ((eq value :r) (throw :redisplay t)) |
---|
629 | ((eq value :?) |
---|
630 | (format *query-io* "~%Enter the number of the selection, ~% r to redisplay, ~% q to cancel or ~% ? to show this message again.")) |
---|
631 | ((and (typep value 'unsigned-byte) |
---|
632 | (< value (length list))) |
---|
633 | (return-from get-answer (list (nth value list)))))))))))) |
---|
634 | |
---|
635 | ;;; There should ideally be some way to override the UI (such as |
---|
636 | ;;; it is ...) here. |
---|
637 | ;;; More generally, this either |
---|
638 | ;;; a) shouldn't exist, or |
---|
639 | ;;; b) should do more sanity-checking |
---|
640 | (defun choose-file-dialog (&key file-types (prompt "File name:")) |
---|
641 | (%choose-file-dialog t prompt file-types)) |
---|
642 | |
---|
643 | (defun choose-new-file-dialog (&key prompt) |
---|
644 | (%choose-file-dialog nil prompt nil)) |
---|
645 | |
---|
646 | (defun %choose-file-dialog (must-exist prompt file-types) |
---|
647 | (loop |
---|
648 | (let* ((namestring (get-string-from-user prompt)) |
---|
649 | (pathname (ignore-errors (pathname namestring))) |
---|
650 | (exists (and pathname (probe-file pathname)))) |
---|
651 | (when (and (if must-exist exists) |
---|
652 | (or (null file-types) |
---|
653 | (member (pathname-type pathname) |
---|
654 | file-types :test #'equal))) |
---|
655 | (return pathname)) |
---|
656 | (if (not exists) |
---|
657 | (format *query-io* "~&~s does not exist." namestring) |
---|
658 | (format *query-io* "~&Type of ~s is not one of ~{~a~}" |
---|
659 | namestring file-types))))) |
---|
660 | |
---|
661 | (defparameter *overwrite-dialog-hook* nil) |
---|
662 | (defun overwrite-dialog (filename prompt) |
---|
663 | (if *overwrite-dialog-hook* |
---|
664 | (funcall *overwrite-dialog-hook* filename prompt) |
---|
665 | t)) |
---|
666 | |
---|
667 | ;;; Might want to have some other entry for, e.g., the inspector |
---|
668 | ;;; and to let it get its hands on the list header returned by |
---|
669 | ;;; disassemble-ppc-function. Maybe disassemble-ppc-function |
---|
670 | ;;; should take care of "normalizing" the code-vector ? |
---|
671 | (defun disassemble (thing) |
---|
672 | "Disassemble the compiled code associated with OBJECT, which can be a |
---|
673 | function, a lambda expression, or a symbol with a function definition. If |
---|
674 | it is not already compiled, the compiler is called to produce something to |
---|
675 | disassemble." |
---|
676 | (#+ppc-target ppc-xdisassemble |
---|
677 | #+x8664-target x8664-xdisassemble |
---|
678 | (require-type (function-for-disassembly thing) 'compiled-function))) |
---|
679 | |
---|
680 | (defun function-for-disassembly (thing) |
---|
681 | (let* ((fun thing)) |
---|
682 | ;; CLHS says that DISASSEMBLE should signal a type error if its |
---|
683 | ;; argument isn't a function designator. Hard to imagine any |
---|
684 | ;; code depending on that ... |
---|
685 | ;;(when (typep fun 'standard-method) (setq fun (%method-function fun))) |
---|
686 | (when (or (symbolp fun) |
---|
687 | (and (consp fun) (neq (%car fun) 'lambda))) |
---|
688 | (setq fun (fboundp thing)) |
---|
689 | (when (and (symbolp thing) (not (functionp fun))) |
---|
690 | (setq fun (macro-function thing)))) |
---|
691 | (if (typep fun 'compiled-lexical-closure) |
---|
692 | (setq fun (closure-function fun))) |
---|
693 | (when (lambda-expression-p fun) |
---|
694 | (setq fun (compile-named-function fun nil))) |
---|
695 | fun)) |
---|
696 | |
---|
697 | (%fhave 'df #'disassemble) |
---|