Page 1 of 1

(fix-args) for (main-args) offset with -x linked programs

PostPosted: Fri Jan 09, 2015 5:40 pm
by CaveGuy
In the following example (fix-args) fixes the main-arg offset problem with using input args with -x linked programs.

Using the offset of 2 from the original example:
Code: Select all
;; uppercase.lsp - Link example
  (println (upper-case ((main-args) 2)))
  (println "main-args " (main-args))
  (exit)


~# newlisp uppercase.lsp uppercase-command
UPPERCASE-COMMAND
main-args ("newlisp" "uppercase.lsp" "uppercase-command")

works as expected but errors out when linked.

~# ./uppercase uppercase-linked
ERR: invalid list index

changing the offset to 1 unsurprisingly converts the wrong string when not linked.

Code: Select all
;; uppercase.lsp - Link example
  (println (upper-case ((main-args) 1)))
  (println "main-args " (main-args))
  (exit)


~# newlisp -x uppercase.lsp uppercase

root@ubuntu1:~# newlisp uppercase.lsp uppercase-command
UPPERCASE.LSP
main-args ("newlisp" "uppercase.lsp" "uppercase-command")

The addition and use of (fix-args) solves the problem in all cases.

Code: Select all
#!/usr/bin/env /usr/bin/newlisp
;; uppercase.lsp - Link example
(define (fix-args) (if (find "newlisp" (lower-case (main-args 0))) (rest (main-args)) (main-args)))
(define (run)
   (println (upper-case ((fix-args) 1)))
   (println "main-args " (main-args))
   (println "fix-args " (fix-args)"\n"))
(run)
(exit)


~# newlisp -x uppercase.lsp uppercase
~# ./uppercase uppercase-linked

UPPERCASE-LINKED
main-args ("./uppercase" "uppercase-linked")
fix-args ("./uppercase" "uppercase-linked")

~# ./uppercase.lsp uppercase.shell
UPPERCASE.SHELL
main-args ("/usr/bin/newlisp" "./uppercase.lsp" "uppercase.xxx")
fix-args ("./uppercase.lsp" "uppercase.xxx")

~# newlisp uppercase.lsp uppercase-command
UPPERCASE-COMMAND
main-args ("newlisp" "uppercase.lsp" "uppercase-command")
fix-args ("uppercase.lsp" "uppercase-command")