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

Q&A's, tips, howto's
Locked
CaveGuy
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio
Contact:

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

Post 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")
Bob the Caveguy aka Lord High Fixer.

Locked