#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH

(setq args (getopts "USAGE: %0 [options] files...
Outputs the contents of files (or stdin if none given), quoting chars
WWW-style (%20 for espace...)
"
    ("-qn" () quote-nl "also quote newlines")
))

(setq printable-chars (+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789" "_-=+.," (if (not quote-nl) "\n")
))

(defun main (&aux
  )
  (if args
    (dolist (arg args) (with (fd (open arg)) (process-stream fd)))
    (process-stream *standard-input*)
))

(defun process-stream (fd &aux c)
  (catch 'EOF (while t (setq c (read-char fd))
      (if (seek printable-chars c)
	(write-char c)
	(write-string (w3quote c))
))))

(setq w3quote:hexdigits "0123456789abcdef")
(defun w3quote (c &aux (s (copy "%")))
  (if (< c 16) (nconc s "0")
    (put s -1 (get w3quote:hexdigits (/ c 16)))
  )
  (put s -1 (get w3quote:hexdigits (mod c 16)))
)


(main)

;;; EMACS MODES
;;; Local Variables: ***
;;; mode:lisp ***
;;; End: ***

