fnparse to get all used functions

For the Compleat Fan
Locked
HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

fnparse to get all used functions

Post by HPW »

Would it be possible to get a command fnparse?

(fnparse "Lsp_Source_String")

which would use the internal newlisp-parser to generate a list of all function-names (without duplicates) used in "Lsp_Source_String" without any evaluating.

Would return something like this:

("set" "define" .... .... "MyFn" "foo")

Background: To be able to check a Lsp-source before loading, that it does not contain malicious, bad Lisp-code. So a app can store a Lisp-Data-Table and can have a Load-Routine which is not a security hole for bad code. Using the internal parser should provide the speed.

Any ideas?
Hans-Peter

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

The internal newlisp parser can be easy exported from newLisp's source code to newlisp's language function (I have this... but better ask Lutz ;-)
It can be used to read s-expression from string into a list.

Next, protecting is a good feature, but it must be issued at run-time, because of 'sym', 'eval', 'fn' etc., without which the lisp code will be poor.

So, this feature must be included in evaluator, as referencing to complete list of allowed symbols for evaluate and for set. No other way will give you a safety.

The bad news is that with dynamic scoping the potential hackers will be glad much either ;-)
WBR, Dmi

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

Maybe it it better to avoid the lsp-format for data-storage,
but it is so easy to handle with save, source and load.

For my project I use a binary, encrypted file with the lisp-stream inside to store my data. Also the lisp-file has a special string-header for versioning/plausibility check.

Of cource also XML could do such job.
Hans-Peter

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Code: Select all

newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.

> (macro-string "(set 'a (+ 1 2))")
((set 'a (+ 1 2))) 
> ((macro-string "(set 'a (+ 1 2))") 0 0)
set 
>
Probably this is wat you want... 'macro-string' is the export of internal 'compileExpression' C-function. It cares about [cmd], [text] and comments.
In other words, it parsing, but don't evaluating.

Logically, I think, if we have a function 'string' that can print s-lists, so we must have a function that can read the s-lists back from string source ;-)
WBR, Dmi

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

If you quote list inside the string, this works:

Code: Select all

> (eval-string "'(set 'a (+ 1 2))")
(set 'a (+ 1 2)) 
> ((eval-string "'(set 'a (+ 1 2))") 0 0)
set 
Fanda

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Code: Select all

newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.

> (eval-string "'a (println 1)")
1
1 
"If you have a loaded gun, one day it will shoot". ;-)
It's too dangerous to use evaluation-based techics for reading data.
Many web holes are based on that already...
WBR, Dmi

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

Would something like this work?

Code: Select all

(define (fnparse SOURCE)
  (letn
    ( LP    "("     ;) left paren
      SP    " "     ;  space
      LPSP  "( "    ;) left paren and space
      NULL  ""      ;  null string

      keep  (lambda (L K) (dolist (x L) (if (= (x 0) LP) (push x K))) K)
      s     (parse (replace LPSP (join (parse SOURCE) SP) LP) SP)
    )
  ;body of letn
    (unique (map (fn (x) (replace LP x NULL)) (keep s))) ))
> (define (bob one two) (+ one two))
(lambda (one two) (+ one two))

> (source 'bob)
"(define (bob one two)\r\n (+ one two))\r\n\r\n"

> (fnparse (source 'bob))
("+" "bob" "define")

Locked