Problem: How to get program's startup directory - And a solution
I seem to remember one or more posts asking how to get the program’s startup directory. Seems that it is nice to be able to put your EXE file anywhere. Then on running your EXE, your EXE will create whatever other directories and files it needs relative to itself.
I think the solution to getting the program’s startup directory has been to hardcode the full path of wherever you want your EXE to be. If you later want to run from someplace else, you need to recode your DEFCONSTANT to the new location.
In LispWorks, you can get all the command line arguments as a list. The list is this global sys:*line-arguments-list*. Generally, (first sys:*line-arguments-list*) is the full path to your executable, including the executable. If you do a DELIVER, then run you executable from the command line, then you probably will not get the full path. Instead, you could wind up with just the executable’s name and EXE extension. What you get is whatever you typed at the command line to invoke your executable. And likely, you changed the current directory to your executable’s directory, then typed something like
my-executable arg-1 arg-2
You did not type the full path; therefore, you don’t get the full path from (first sys:*line-arguments-list*).
A problem I have with getting the full path from (first sys:*line-arguments-list*) is that it never works from the Listener in the LispWorks Integrated Development Environment. What you get is LispWorks’s startup directory. Then when you do a DELIVER, the executable will get its own startup directory. My problem is that the debugging environment won’t work the same as the runtime environment. And I think LispWorks making the startup directory match the executable’s would not be so simple. It would not be so simple because of the question, “What should LispWorks present as the debugging startup directory?” There’s no executable yet. Maybe LispWorks could use the directory of one of the source code files? If so, which one?
A solution is to get the startup directory from a named pipe. I made a program “dir-of-” that creates a named pipe from which a program can get dir-of-’s startup directory. Then, the program gets the same startup directory from the same place regardless of its running inside LispWorks or running on its own having been delivered.
dir-of- takes a single, optional argument: a suffix to its name for naming the pipe. If you do not provide the suffix, then the name of the pipe is dir-of-.
I tried running it from a Windows shortcut because you can set up the shortcut to provide a command-line argument. Here is the content of a batch file (dir-of-logger.bat) from which I ran it:
dir-of- logger
When I click on the dir-of-logger.bat, these instructions display:
========== begin display ==========
Provides a named-pipe from which programs can read this program's
startup directory path.
Usage:
dir-of [pipe-name-suffix]
where [pipe-name-suffix] is a string appended onto pipe name dir-of-.
To read this program's startup directory, a program
Connects to pipe "dir-of-logger" as client. Reads from pipe "dir-of-logger". Then closes pipe "dir-of-logger".
To see the directory path from a Windows command line, type
more < \\.\pipe\\dir-of-logger
Reading pipe dir-of-logger will yield
D:\Users\rlewi\Documents\Accounting, Indinfer\
Press enter to close pipe and exit.
========== end display ==========
I was wondering whether named pipes are used much anymore. At the command line, I typed:
dir \\.\pipe\\*
I was surprised to see 89 named pipes listed. Although I am a bit troubled seeing each line listed with “The parameter is incorrect.” When I start up dir-of-, I get 90 named pipes listed.
When I copy and paste from the dir-of- instructions:
more < \\.\pipe\\dir-of-logger
I see the directory
D:\Users\rlewi\Documents\Accounting, Indinfer\
WHY I AM POSTING
- I am interested in presenting the solution and source code for testing and critique. I likely will benefit from having bugs revealed, a better understanding of Lisp style conventions, and better approaches. I may benefit from corrections of my misunderstandings that may be apparent to others.
- I am interested in contributing the source code to the lisp-hug community for their helping me get started in LispWorks.
- If we get this far and LispWorks is interested, dir-of- could become part of LispWorks’s distributions.
Someone might tell me of a solution that is better than this named pipes solution.
I have not tried this kind of thing before, to donate software with source code. Would anyone tell me how I should do it? Do I need to reference some standard, appropriate license? Would I just ZIP the executable and source code, then attach to an email to lisp-hug@lispworks?
Ron Lewis
Baltimore, MD 21215-3551
USA