chicken-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SRFI 193 (Command lines) for Chicken


From: Lassi Kortela
Subject: SRFI 193 (Command lines) for Chicken
Date: Sat, 8 Aug 2020 15:14:26 +0300
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.11.0

Hello,

SRFI 193 is in the last-call period before finalization. Here is an
implementation for Chicken. I'd like to publish it as an egg. The API
is trivial enough that if you like it, you may want it in core.

The code works as is, but has the following issues:

- It's Chicken 5 only. Could it be easily backported to 4?

- It checks for the magic string "csi" to figure out whether running
  in csi or in a standalone program compiled using csc. The check is
  dodgy; what would be a proper way to do it?

- Pathname handling is not cross-platform. It uses `string-append`
  with "/" to build pathnames, and feeds the result to
  `normalize-pathname` to resolve relative pathnames into absolute.
  How does one get the host platform's directory separator, and would
  there be cleaner ways to write the pathname parts in general?

Thanks to anyone who can spare a moment to help a noob :)

;;; ----------------------------------------------------------------------

(module srfi-193
    (command-line command-name command-args script-file script-directory)

  (cond-expand
    (chicken-5
     (import scheme (chicken base) (chicken module) (chicken pathname)
             (rename (only (chicken process-context)
                           current-directory
                           argv command-line-arguments program-name)
                     (argv                   chicken/argv)
(command-line-arguments chicken/command-line-arguments)
                     (program-name           chicken/program-name)))))

  ;;; Fundamental

  ;; csi -script  => "filename.scm"
  ;; csi repl     => #t
  ;; exe from csc => #f
  (define interpreted
    (let ((arg0 (car (chicken/argv))))
      (and (equal? "csi" (pathname-file arg0))
           (if (equal? "csi" (chicken/program-name)) #t
               (chicken/program-name)))))

  (define (script-file)
    (and (string? interpreted)
         (normalize-pathname
          (string-append (current-directory) "/" interpreted))))

  (define command-line
    (make-parameter (if (eqv? #t interpreted) '("")
                        (cons (or (script-file) (chicken/program-name))
                              (chicken/command-line-arguments)))))

  ;;; Derived

  (define (command-name)
    (let ((file (car (command-line))))
      (and (not (equal? "" file))
           (pathname-file file))))

  (define (command-args)
    (cdr (command-line)))

  (define (script-directory)
    (let ((file (script-file)))
      (and file (string-append (pathname-directory file) "/")))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]