File: base.lisp

package info (click to toggle)
albert 0.4.10.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,556 kB
  • ctags: 2,014
  • sloc: lisp: 13,587; ansic: 7,729; xml: 843; makefile: 99; sh: 28
file content (330 lines) | stat: -rw-r--r-- 9,988 bytes parent folder | download | duplicates (2)
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
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: ApiSpec-Base -*-


#|

DESC: apispec/base.lisp - takes care of basic functionality for the whole system
Copyright (c) 2000 - Stig Erik Sand

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

|#

(in-package :apispec-base)

(defparameter *verbose-operation* t 
  "boolean value to specify if we're verbose or not")

(defparameter *submarine-quiet* nil
  "Will we try to suppress any output, even warnings?")

(defvar *default-albert-output* *standard-output* "Where will info and various albert output go.")

(defvar *relative-calling-directory* "./")

(defmacro when-verbose (&body the-body)
  "DOES NOT RETURN A PROPER VALUE.  AVOID FOR MOST STUFF (partially fixed)"
  `(when *verbose-operation*
     (prog1
	 (progn ,@the-body)
       (force-output))))

(defmacro unless-quiet (&body the-body)
  "Unless *submarine-quiet* is true, it executes THE-BODY."
  `(unless *submarine-quiet*
    ,@the-body))

;; turn into a deftype later
(defun nonboolsym? (sym)
  "returns T if SYM is a symbol but not T or NIL."
  (and sym (not (eq sym t)) (symbolp sym)))

(defun proper-list? (obj)
  "Returns T if OBJ is a proper list or NIL."
  (and (listp obj) (null (last obj 0))))

(defmacro albert-warn (str &rest args)
  "Works as WARN if *submarine-quiet* is NIL."
  `(unless-quiet
    (warn ,str ,@args)))

(defun albert-info (str &rest args)
  "Prints a format-string STR with args ARGS to *default-albert-output* with albert-prefix."
  (prog2
      (format *default-albert-output* "~&Albert: ")
      (apply #'format (cons *default-albert-output*
			    (cons str args)))
    (format *default-albert-output* "~%")))

#||
(eval-when (:compile-toplevel :compile-toplevel :load-toplevel)
  (defmacro strcat (&rest args)
    "Macro for concatenating SIMPLE-BASE-STRINGs"
    #+allegro
    `(concatenate 'string ,@args)
    #-allegro
    `(the simple-base-string (concatenate 'simple-base-string ,@args))
    ))
  
(declaim (inline mystrcat))
(defun mystrcat (x y)
  "Basically catenates strings and tries to stringify arguments to be sure"
  (concatenate 'simple-base-string (the simple-base-string (string x)) 
	       (the simple-base-string (string y))))

#+cmu
(defmacro its-name (obj)
  "convenient way to print out info when developing"  
  `(class-name (class-of ,obj)))

#-cmu
(eval-when (:execute :compile-toplevel :load-toplevel)
  (defun its-name (obj)

||#

(defun strcat (&rest args)
  (apply #'concatenate 'string args))

(defun its-name (obj)
  "convenient way to print out info when developing"  
  (class-name (class-of obj)))  

(defun mystrcat (x y)
  "Basically catenates strings and tries to stringify arguments to be sure"
  (concatenate 'string (string x) (string y)))

(defvar *keyword-package* (find-package :keyword))

(defmacro concat-pnames (&rest args) 
  "concatenates strings or symbols and returns an interned
symbol which can be passed to e.g defun (as name of function)."

  (let ((str (gensym))
	(case-fun
	 #+allegro
	  (ecase excl:*current-case-mode*
	    (:case-sensitive-lower   'nstring-downcase)
	    (:case-insensitive-upper 'nstring-upcase))
	  #-allegro
	  (ecase (cl:readtable-case cl:*readtable*)
	    (:downcase 'nstring-downcase)
	    (:upcase 'nstring-upcase))
	  ))

    `(let ((,str (,case-fun (reduce #'mystrcat (list ,@args)))))
       (if (and (plusp (length ,str)) (eql (char ,str 0) #\:))
	   (intern (subseq ,str 1) *keyword-package*)
	   (intern ,str)
	   ))
    ))

#-clisp
(defun eat-file (filename)
  "eats a whole file and returns it as a string.
If the filename argument is a string, it is made into a pathname
and merged with the current path, if it is a pathname it is
used directly."
  
  (declare (optimize (safety 0) (speed 3) (debug 0)))

  (let ((the-file-path (etypecase filename
			 (string (merge-pathnames (pathname filename)))
			 (pathname filename))))
  
    (with-open-file (str the-file-path :direction :input)
      (let ((buffer (make-array (file-length str)
				:element-type (stream-element-type str)
				;; :allocation :old
				)))
	#+clisp
	(system::read-char-sequence buffer str)
      
	;; NOTE poplog has no read-sequence
	#-clisp
	(read-sequence buffer str)

	;; <kmr>   for parsing the results of eat-file, you call octets-to-
	;;  string for (and allegro ics), but this needs to be called for any allegro. also,
	;;  you might want to put that post-processing in eat-file itself

	#+allegro (excl:octets-to-string buffer)
	#-allegro buffer
	))))

;; submitted by s madhu
#+clisp
(defun eat-file (filename)
  "eats a whole file and returns it as a string.
If the filename argument is a string, it is made into a pathname
and merged with the current path, if it is a pathname it is
used directly."

  (declare (optimize (safety 0) (speed 3) (debug 0)))

  (with-open-file (s filename :direction :input :element-type 
		     '(unsigned-byte 8))
    (let* ((len (file-length s))
	   (buf (make-array len :element-type '(unsigned-byte 8)))
	   (pos (read-sequence buf s)))
      (assert (= pos len))
      (map 'string #'code-char buf))))




(defun wipe-file (filename)
  "deletes the file named by filename"
  (let ((fptr (probe-file filename)))
    (when fptr
      (when-verbose
	  (format t "Wiping file ~a~%" filename))
      (delete-file fptr))
    ))



(defgeneric get-element-name (xmlobj)
  (:documentation "Returns a string with the element name of the xml-object"))


(defun figure-out-fname (fname)
  "should be made into one line later.."
  (let* ((fname (pathname fname))
	 (result (merge-pathnames fname)))
;;    (warn "FNAME is ~a vs ~s" fname (namestring result))
    (namestring result)))



;;; this is mean and ugly.. find a better way later
#-(or allegro cmu)
(defvar *tempfile-cnt* 19)
(defun make-temporary-filename ()
  "Returns the name of a temporary filename"
  #+allegro (system:make-temp-file-name)
  #+cmu
  (system::pick-temporary-file-name)
  #-(or allegro cmu) 
  (let* ((num (incf *tempfile-cnt*))
	 (filename (format nil "/tmp/sds-tempfile-~a" num))
	 (retval (pathname filename)))
    filename)
  )

(defun run-external-program (program args &key outfile )
  " Should return the int that the external program returns.. "

;;  (warn "outfile is ~s" outfile)
  
  (let ((the-cmd ""))
    #+(or allegro clisp lispworks)
    (setq the-cmd (concatenate 'string program " " args (if outfile 
							    (concatenate 'string " > " outfile)
							    "")))
    
    #+allegro (excl:run-shell-command the-cmd :wait t)
    #+clisp (ext:shell the-cmd)
    #+lispworks (system::call-system the-cmd)
    #+:cmu (let ((res (extensions:run-program program (list args) 
					      :output outfile :wait t 
					      :if-output-exists :supersede)))
;;	   (warn "Exec of {~a} Res was ~a ~a"
;;		 (list program args outfile)
;;		 res (extensions:process-exit-code res))
	   (extensions:process-exit-code res))

    #+:sbcl (let ((res (sb-ext:run-program program (list args) 
					   :output outfile :wait t 
					   :if-output-exists :supersede)))
	      (sb-ext:process-exit-code res))

    ;; not sure of this one though, fix outfile
    #+poplog (progn
	       (require :run-unix-program)
	       (poplog::run-unix-program program :args args))
  ))


(defgeneric verify-object (obj context)
  (:documentation "Verifies an object given a context to verify under."))

(defgeneric verify-object-actual (obj context when)
  (:documentation "Implementation of VERIFY-OBJECT"))

;;(defmethod-with-warn verify-object (obj context))
(defmethod verify-object (obj context)
  (declare (ignore context))
  (warn "Trying to verify ~a, but can't" (its-name obj)))

(defgeneric register-object (obj context)
  (:documentation "registers the object with the given context"))

(defmethod register-object (obj context)
  (declare (ignore context))
  (warn "No REGISTER-OBJECT written for ~a [~a]" (its-name obj) obj))

(defgeneric register-object-actual (obj context when)
  (:documentation "Implementation of REGISTER-OBJECT"))


(defgeneric do-iteration (node function context &key recursive &allow-other-keys)
  (:documentation "Iteration function which all other quasi-iterative
functions should use. The function argument should take three arguments,
the node, the context and an argument specifying when it was called 
(:before or :after kids are processed for parents or :in for leaves). 
The return value \\underline{can} be a keyword which can have special meaning. 
One such keyword is :cancel which will stop the iteration."))

#||
(defmethod do-iteration ((node cons) function context 
			 &key (recursive t) &allow-other-keys)
  (if recursive
      (dolist (x node)
	(funcall function x context :in))
      (funcall function (car node) context :in))
  
  t)
||#

;;(defmethod-with-warn do-iteration (node function context 
;;					&key (recursive t) &allow-other-keys))

(defmethod do-iteration (node function context 
			      &key (recursive t) &allow-other-keys)
  (declare (ignore recursive))
  (warn "No handler for (DO-ITERATION ~a ~a ~a)" 
	(its-name node) (its-name function) (its-name context)))
  

(defun make-sure-dirs-exist (dir)
  "mostly a call to ENSURE-DIRECTORIES-EXIST,
but stops errors from floating out.. returns NIL instead."
  (let ((the-path (merge-pathnames (etypecase dir
				     (pathname dir)
				     (string (pathname dir))))))

    (handler-case
	(ensure-directories-exist the-path
				  :verbose *verbose-operation*)
      (file-error (co)
	(warn "Something went wrong [~a] during directory creation [~s], returning NIL."
	      co the-path)
	nil))))

;;(trace make-sure-dirs-exist)


(defun ensure-dir-name (str)
  "Makes sure the str has a / suffix"
  (etypecase str
    (string
     (if (and str (not (eq (char str (1- (length str))) #\/)))
	 (concatenate 'string str "/")
	 str))
    ;; bad
    (pathname str)
    ))