How do I detect if a script is "linked" or not

Q&A's, tips, howto's
Locked
axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

How do I detect if a script is "linked" or not

Post by axtens »

G'day everyone

With this code

Code: Select all

;margs.lsp
(dolist (i (main-args)) (println $idx ": " i))
(exit)
If I evaluate it using the interpreter I get the following

Code: Select all

>newlisp margs.lsp 1 2 3
0: newlisp
1: margs.lsp
2: 1
3: 2
4: 3
but if I link it using link.lsp I get the similar stuff but in different places

Code: Select all

>emargs 1 2 3
0: emargs
1: 1
2: 2
3: 3
This is problematic, particularly as I tried to link the twitter.lsp code yesterday and had to tweak the code to make the main-args work properly. It would be better, I reckon, to be able to write into the script itself a means of detecting whether it's running interpreted or linked.

I suppose one could check for the "newlisp" in (main-args 0) but v10.3.4 could have a predicate (linked?), viz

Code: Select all

(if (linked?) (println (uppercase (main-args 1))) (println (uppercase (main-args 2))))
Am I missing something? It happens all the time.

Kind regards,
Bruce.

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: How do I detect if a script is "linked" or not

Post by xytroxon »

Change the link.lsp program so that after the script to be appended is loaded into memory, push a (set 'link.lsp? true) flag to the front of the code string, before the string is written to the .exe file being created.

Insert the one line below marked ;<-- add this line
to your copy of link.lsp

Code: Select all

    ;; open the new exe for update
    (set 'handle (open newExeName "u"))
    (search handle "@@@@@@@@")

    ;; this field gets read by the newLISP startup
    (write-buffer handle buff 4)
    (set 'buff (read-file lispSourceName))
(push {(set 'link.lsp? true)} buff) ;<-- add this line
    (set 'keylen (pack "ld" (length buff)))
    (write-buffer handle keylen 4)
Then:

(if link.lsp? (... etc.

Notes:
1. I used a period in link.lsp? to help clarify that it is the link.lsp program that you are checking for. Change it if you wish.

2. You can also add other custom newLISP code between the braces { }

3. I have not tested this code.

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

axtens
Posts: 28
Joined: Mon Apr 06, 2009 12:23 pm
Location: Perth, WA Australia
Contact:

Re: How do I detect if a script is "linked" or not

Post by axtens »

xytroxon wrote:Change the link.lsp program so that after the script to be appended is loaded into memory, push a (set 'link.lsp? true) flag to the front of the code string, before the string is written to the .exe file being created.

...

-- xytroxon
Therein lies the difference between the guru and the disciple.

My solution was as below, and happens entirely within the script being developed rather than in link.lsp.

Code: Select all

(define (linked?) 
	(not (starts-with (lower-case (main-args 0)) "newlisp"))
)

(setq command_line (if (linked?) (main-args) (rest (main-args))))
With this approach I end up with a list, command_line, that works properly no matter what, as below.

Code: Select all

S:\>type linkage.lsp
(define (linked?) (not (starts-with (lower-case (main-args 0)) "newlisp")))
(setq command_line (if (linked?) (main-args) (rest (main-args))))
(println command_line)
(exit)

Code: Select all

S:\>newlisp linkage.lsp "newLISP rules" OK!
("linkage.lsp" "newLISP rules" "OK!")

S:\>linkage.exe "newLISP rules" OK!
("linkage.exe" "newLISP rules" "OK!")
So thanks very much xytroxon, you've been really helpful. Jam your stuff and my stuff together, and we have the solution.

Kind regards,
Bruce.

Locked