FYI: I do a lot of web programming. In some cases, I'm writing
for remote servers where I have complete control, including
secure shell and root access. In other cases, I enjoy the cooperation
of a sysadmin who will do anything for me as long as it is safe.
However, there are domain hosters who will not install a lesser-known
binary, such as rebol or newlisp at a system directory, but will allow
an install under a domain path, such as public_html, so I might
have newlisp running at /home/bin and modules at some arbitrary
directory path.
Thus I wanted an interface that was simple and took care of searching
paths in the background.
First code is the startup module itself. Second a simple CGI script
and lastly the output from the script.
I'd welcome comments and caveats that would further enlighten me
on coding techniques.
TIA
tim
;;---------------------------------------------------------------------------------------------
Code: Select all
;; initialization code
;; Search in the following order
;; 1)current directory
;; 2)/usr/share/newlisp/modules/
;; 3)Any other hard-coded paths
(setq sys-path '(""
"/usr/share/newlisp/modules"
"/home/http/run/libraries/newlisp"))
;; -------------------------------------------------------------
;; If a 'symbol does not have an extension, add a default ".lsp"
;; -------------------------------------------------------------
(define (lsp-extend symbol)
(setq fname (string symbol)) ;; just in case a symbol is passed
(cond
((< (length(parse fname ".")) 2) ;; no extension
(append fname ".lsp")) ;; add default
(fname)))
;; -------------------------------------------------------------
;; search for file along directories in 'sys-path
;; return full path to file or nil
;; -------------------------------------------------------------
(define (path-found f)
(setq file (lsp-extend f)) ;; confirm extension
(dolist
(path sys-path) ;; search existing paths, same dir first
(if(= path "") ;; check wd first
(setq test (append (real-path) "/" file))
(setq test (append path "/" file))) ;; full path
(if (file? test) ;; we're outta here!
(throw test))))
;; -------------------------------------------------------------
;; If found, load file, if not throw error
;; -------------------------------------------------------------
(define (load-from-path file)
(setq res (catch(path-found file)))
(cond
(res (load res)) ;; path found
((throw-error ;; abort with error message
(append file " could not be found in 'sys-path"))))
res)
;; -------------------------------------------------------------
;; @module usr
;; @syntax (modules "module1 module2 module3 ...")
;; If a file name does not have an extension, .lsp is appended
;; @example (modules "cgi ftp zlib mylib")
;; @return list of loaded files
;; TODO:
;; add further arguments, thus module names are
;; 'words' in a string => 'modstring
;; provide code for a symbol list as an alternative to
;; a string of modules
;; -------------------------------------------------------------
(define (modules modstring)
(setq loaded '()) ;; list of loaded modules
(dolist
(ms (parse modstring))
(push ;; accumulate list of loaded modules
(load-from-path ms) loaded))
loaded)
Code: Select all
#!/usr/bin/newlisp -c
;; http://localhost/cgi-bin/newlisp/testcgi.lsp
(println "Content-type: text/html\n")
(define resources "cgi ftp cgi1")
(define (main)
(load "usr.lsp")
(setq loaded(modules resources))
(println "<br>$HOME: " $HOME)
(println "<br>Loaded: " loaded)
(exit))
(catch (main) 'err)
(if err(println "<pre>ERROR: " err))
$HOME: /home/http/
Loaded: ("/home/http/run/newlisp/cgi1.lsp" "/usr/share/newlisp/modules/ftp.lsp" "/usr/share/newlisp/modules/cgi.lsp")