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

Started by axtens, April 13, 2009, 07:52:36 PM

Previous topic - Next topic

axtens

G'day everyone



With this code

;margs.lsp
(dolist (i (main-args)) (println $idx ": " i))
(exit)


If I evaluate it using the interpreter I get the following

>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

>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

(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

#1
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

   ;; 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

#2
Quote from: "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.



...



-- 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.


(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.
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)



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.