JScheme Scriptometer

This file contains JScheme examples of Scriptometer scripts.

You can execute JScheme scripts directly on UNIX by using a first line like:

#! /usr/bin/env runjscheme
The code is executed and the command line arguments are in the vector ARGS. Here are some simiple examples:

scriptometer.zip is a .zip archive of this directory.


the smallest running program

""


print a simple string on stdout

(display "Hello World\n")


access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument)


(print ARGS)


access environment variable

Java (as of JDK 1.4) does not provide direct access to environment variables, but you can get them by executing a OS specific command.
(load "using/run.scm")
(define getvar
  (let ((table (Properties.)))
    (for-each*
     (lambda (r) (let ((it (.split r "=")))
                   (.put table (vector-ref it 0) (vector-ref it 1))))
     (BufferedReader (inputReader (if (.startsWith ($ "os.name") "Windows")
                                      (run (cmd cmd /c set))
                                      (run (cmd /bin/sh set))))))
    (lambda (name) (.get table name))))
(print (getvar "HOME"))  
      


return exit code error (non zero) if a file does not exist

(if (not (.exists (java.io.File. "/etc/mtab"))) (System.exit 1))


return exit code error (non zero) if a file is not readable

(if (not (.canWrite (java.io.File. "/etc/mtab"))) (System.exit 1))


print integers in a simple formatted string

(let ((a 1)
      (b 2))
  (print {[a] + [b] = [(+ a b)]}))

call an external program and check the return value


(load "elf/run.scm")
(define (system . args)  ; More like Python.
  (out (run (cmd ,@args))))

(if (system "false") (system "echo" "false failed")
  (system "echo" "done"))


remove #-comments from a file (modifying the file, i.e. in place)

(load "using/run.scm")
(import "java.io.File")
(let ((file (vector-ref ARGS 1)))
  (let ((out (File.createTempFile "foo" "bar")))
    (print out)
    (call-with-output-file out
      (lambda (s)
        (for-each* (lambda (r) (.println s (.replaceFirst r "#.*" "")))
                   (BufferedReader  (File. file)))
        (.close s)))
    (.renameTo out (File. file))))

find and compile .c files into .o when the .o is old or absent

(import "java.io.File")
(define (s->o srcDir classDir fromtype totype)
  ;; Source file to object file converter.
  (lambda (file)
    ;; Converts a .java file in srcDir into a .class file in classDir.
    (let ((f (.toString (relativize srcDir file))))
      (File.  classDir
	      (string-append (.substring f 0
					 (- (.length f) (.length fromtype)))
			     totype)))))

(define (needsUpdate? s->o)
  (lambda (jf)
    ;; Does .java file jf need to be recompiled?
    (let ((cf (s->o jf)))
      (or (not (.exists cf)) (<= (.lastModified cf) (.lastModified jf))))))

(let* ((dir (java.io.File. "."))
      (s->o (s->o dir dir ".c" ".o"))
      (update? (needsUpdate? s->o)))
  (for-each (lambda (c)
              (if (update? c)
                  (let ((o (s->o c)))
                    (display {compiling [c] to [o]\n})
                    (out (run (cmd gcc -c -o ,c ,o))))))
            (files* (java.io.File. ".") (isFile ".c"))))


grep with -F -i -h handling, usage, grep'ing many files

(load "using/run.scm")
(define (any p xs) (and (pair? xs) (or (p (car xs)) (any p (cdr xs)))))

(define (forLines f files)
    (for-each (lambda (file) (for-each* f (BufferedReader (File. file))))
              files))

(define (show x) (display x) (newline))

(define (grep-F -i re files)
  (let ((lines (vector->list (.split re "\n")))
        (f (if -i .equalsIgnoreCase .equals)))
    (forLines (lambda (r) (if (any (lambda (p) (f p r)) lines) (show r)))
              files)))

(define (grep re files)
  (let ((p (Pattern.compile re)))
    (forLines (lambda (r) (if (.find (.matcher p r)) (show r)))
              files)))
  
(let* ((args (cdr (vector->list ARGS)))
       (-F (member "-F" args))
       (-i (member "-i" args))
       (-h (member "-h" args))
       (args (filter (lambda (a) (not (.startsWith a "-"))) args))
       (re (if -i {[(?i)][(car args)]} (car args)))
       (files (cdr args)))
  (cond ((or -h (null? args))
         (display {usage: grep [-F] [-i] regexp file ...\n}))
        (-F (grep-F -i re files))
        (else (grep re files))))