; -*- jscheme -*- ; hello world ; print a simple string on stdout (display "Hello World\n") ; argv ; access command line parameters (no segmentation fault accepted, nor silent exception, so some languages must explicitly check the presence of the argument) (print ARGS) ; env ; access environment variable (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")) ; test file exists ; return exit code error (non zero) if a file does not exist (if (not (.exists (java.io.File. "/etc/mtab"))) (System.exit 1)) ; test file readable ; return exit code error (non zero) if a file is not readable (if (not (.canWrite (java.io.File. "/etc/mtab"))) (System.exit 1)) ; formatting ; print integers in a simple formatted string (let ((a 1) (b 2)) (print {[a] + [b] = [(+ a b)]})) ; sed in place ; 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)))) ; compile what must be ; 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) (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 ; 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))))