dirname wanted
dirname wanted
Ok, here is a question:
If I am writing several scripts, and I want to put reusable definitions into a separate file, for instance:
/home/scratchy/bin/a.lsp <-- one script
/home/scratchy/bin/b.lsp <-- another script
/home/scratchy/lib/common.lsp <-- reusable stuff
Obviously, in a.lsp and b.lsp I need to locate common.lsp, soft of:
(load (sting (dirname (nth 1 (main-args))) "../lib/common.lsp"))
[correct me if I'm wrong]. However, I wasn't able to find any built-in "dirname" function. If it's not built-in => I would have to implement it myself => I can't put it into common.lsp because I need it before any file is loaded. Duplicating dirname in the beginning of each script is plain ugly. So is there an elegant solution to this problem?
If I am writing several scripts, and I want to put reusable definitions into a separate file, for instance:
/home/scratchy/bin/a.lsp <-- one script
/home/scratchy/bin/b.lsp <-- another script
/home/scratchy/lib/common.lsp <-- reusable stuff
Obviously, in a.lsp and b.lsp I need to locate common.lsp, soft of:
(load (sting (dirname (nth 1 (main-args))) "../lib/common.lsp"))
[correct me if I'm wrong]. However, I wasn't able to find any built-in "dirname" function. If it's not built-in => I would have to implement it myself => I can't put it into common.lsp because I need it before any file is loaded. Duplicating dirname in the beginning of each script is plain ugly. So is there an elegant solution to this problem?
newLISP looks for init.lsp in /usr/share/newlisp/init.lsp on Linux/UNIX or in the newLISP startup directory on Win32. The stuff you find in init.lsp from the distribution is not really required and you can throw it out and replace it with your own stuff.
You could put (load "common.lsp") into your init.lsp file or put those common functions directly into it. This would be one of many ways to get dirname:
(define (dirname path) (if (regex "(.*/).*" path) $1 "./"))
(dirname "/usr/etc/bashrc") => "/usr/etc/")
by default regex is 'greedy' so it will always get as much as it can toward the end of the path. To pick out the filename you could use:
(define (basename path) (last (parse path "/")))
(basename "/usr/etc/bashrc") => "bashrc")
Lutz
Ps: here is another one
(define (dirname path) (join (chop (parse path "/")) "/"))
You could put (load "common.lsp") into your init.lsp file or put those common functions directly into it. This would be one of many ways to get dirname:
(define (dirname path) (if (regex "(.*/).*" path) $1 "./"))
(dirname "/usr/etc/bashrc") => "/usr/etc/")
by default regex is 'greedy' so it will always get as much as it can toward the end of the path. To pick out the filename you could use:
(define (basename path) (last (parse path "/")))
(basename "/usr/etc/bashrc") => "bashrc")
Lutz
Ps: here is another one
(define (dirname path) (join (chop (parse path "/")) "/"))
Last edited by Lutz on Mon Aug 16, 2004 5:25 pm, edited 2 times in total.
So it would take something like this:
(define (dirname path)
(if (= (& (nth 7 (sys-info)) 0x0F) 6)
(if (regex "(.*\\\\).*" path) $1 ".\\")
(if (regex "(.*/).*" path) $1 "./")))
(define *script-dir* (dirname (nth 1 (main-args))))
in the beginning of each script to be able to include an external script:
(load (string *script-dir* "../lib/common.lsp"))
I haven't tested this implementation of dirname enough to guarantee that it works everywhere, but having to duplicate the code it is not cool. What else can I do to convince you that dirname should be a built-in?
(define (dirname path)
(if (= (& (nth 7 (sys-info)) 0x0F) 6)
(if (regex "(.*\\\\).*" path) $1 ".\\")
(if (regex "(.*/).*" path) $1 "./")))
(define *script-dir* (dirname (nth 1 (main-args))))
in the beginning of each script to be able to include an external script:
(load (string *script-dir* "../lib/common.lsp"))
I haven't tested this implementation of dirname enough to guarantee that it works everywhere, but having to duplicate the code it is not cool. What else can I do to convince you that dirname should be a built-in?
I think it is just not used frequently enough. You far more compose path-filenames from their parts, than the other way around. Most popular scripting languages only offer it in a module. In Perl you have to use the File:Basename module, in Python you would have to use the OS.path module. Just put it in your init.lsp.
Lutz
Lutz
>but having to duplicate the code it is not cool.
What's wrong with Lutz advice? When you put your own tools in the initialisation lsp, then you can use it from every of your own scripts.
No duplicate code neccesary. I use this technic to load a compatibility layer to another lisp-dialect.
"Lisp is a programmable programming language."
- John Foderaro, CACM, September 1991
from http://www.paulgraham.com/quotes.html
What's wrong with Lutz advice? When you put your own tools in the initialisation lsp, then you can use it from every of your own scripts.
No duplicate code neccesary. I use this technic to load a compatibility layer to another lisp-dialect.
"Lisp is a programmable programming language."
- John Foderaro, CACM, September 1991
from http://www.paulgraham.com/quotes.html
Hans-Peter
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
Current directory is variable. But the script should be able to figure out where it is located in the filesystem, and without 'dirname', it's impossible to do.(load "./lib/common.lsp") will load from the current directory's lib subdirectory.
Anywhere, at a <i>fixed relative path</i> (e.g. "../lib") to a.lsp and b.lsp. Equivalent functionality in sh can be achieved via:Where do you want common.lsp to be?
#!/bin/sh
source `dirname $0`/../lib/common.sh
The philosophy of sh is to use external programs as much as possible, that is why dirname is not a built-in function in sh.
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
I note:
[nigel@p800 nigel]$ clisp
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2004
[1]> (directory-namestring *LOAD-PATHNAME*)
""
[2]>
and from newlisp
> (exec "dirname $0")
(".")
both in Mandrake 9.1 - they don't seem to add much?
Nigel
PS re *LOAD-PATHNAME* it looks loke a load into the lisp
environment is needed to set it - not the starting of lisp itself?
viz: from hyperpsec
Variable *LOAD-PATHNAME*, *LOAD-TRUENAME*
Value Type:
The value of *load-pathname* must always be a pathname or nil. The value of *load-truename* must always be a physical pathname or nil.
Initial Value:
nil.
Description:
During a call to load, *load-pathname* is bound to the pathname denoted by the the first argument to load, merged against the defaults; that is, it is bound to (pathname (merge-pathnames filespec)). During the same time interval, *load-truename* is bound to the truename of the file being loaded.
[nigel@p800 nigel]$ clisp
i i i i i i i ooooo o ooooooo ooooo ooooo
I I I I I I I 8 8 8 8 8 o 8 8
I \ `+' / I 8 8 8 8 8 8
\ `-+-' / 8 8 8 ooooo 8oooo
`-__|__-' 8 8 8 8 8
| 8 o 8 8 o 8 8
------+------ ooooo 8oooooo ooo8ooo ooooo 8
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2004
[1]> (directory-namestring *LOAD-PATHNAME*)
""
[2]>
and from newlisp
> (exec "dirname $0")
(".")
both in Mandrake 9.1 - they don't seem to add much?
Nigel
PS re *LOAD-PATHNAME* it looks loke a load into the lisp
environment is needed to set it - not the starting of lisp itself?
viz: from hyperpsec
Variable *LOAD-PATHNAME*, *LOAD-TRUENAME*
Value Type:
The value of *load-pathname* must always be a pathname or nil. The value of *load-truename* must always be a physical pathname or nil.
Initial Value:
nil.
Description:
During a call to load, *load-pathname* is bound to the pathname denoted by the the first argument to load, merged against the defaults; that is, it is bound to (pathname (merge-pathnames filespec)). During the same time interval, *load-truename* is bound to the truename of the file being loaded.
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
Hi Scratchy,
Could you post clisp code that uses/prints it?
I can't get it to return anything non-interactive viz if test2.lsp
(load "/home/nigel/test.lsp")
(defconstant *script-dir* (directory-namestring *LOAD-PATHNAME*))
(print "Value is <")
(print *script-dir*)
(print ">")
(exit)
I get
[nigel@p800 nigel]$ clisp test2.lsp
"Value is <"
""
">"
[nigel@p800 nigel]$
same if test2.lsp is
(defconstant *script-dir* (directory-namestring *LOAD-PATHNAME*))
(print "Value is <")
(print *script-dir*)
(print ">")
(exit)
Nigel
Could you post clisp code that uses/prints it?
I can't get it to return anything non-interactive viz if test2.lsp
(load "/home/nigel/test.lsp")
(defconstant *script-dir* (directory-namestring *LOAD-PATHNAME*))
(print "Value is <")
(print *script-dir*)
(print ">")
(exit)
I get
[nigel@p800 nigel]$ clisp test2.lsp
"Value is <"
""
">"
[nigel@p800 nigel]$
same if test2.lsp is
(defconstant *script-dir* (directory-namestring *LOAD-PATHNAME*))
(print "Value is <")
(print *script-dir*)
(print ">")
(exit)
Nigel