fnparse to get all used functions

Started by HPW, November 03, 2005, 10:29:35 PM

Previous topic - Next topic

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

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

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

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

#4
If you quote list inside the string, this works:


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


Fanda

Dmi

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

#6
Would something like this work?
(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)rn  (+ one two))rnrn"



> (fnparse (source 'bob))

("+" "bob" "define")