Changeset 9770 for lang/scheme

Show
Ignore:
Timestamp:
04/18/08 23:36:02 (8 months ago)
Author:
hayamiz
Message:

gauche-shell: implemented foundation for shell script

Location:
lang/scheme/gauche-shell/trunk
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • lang/scheme/gauche-shell/trunk/shell.scm

    r9601 r9770  
    77 
    88(use srfi-1) 
     9(use srfi-13) 
    910(use file.util) 
    1011(use gauche.collection) 
    1112(use gauche.process) 
    12 (export pwd ls 
    13         $ command) 
     13(use gauche.interactive) 
     14(export pwd ls echo 
     15        $ command c 
     16        define-command define-commands 
     17        rehash 
     18        ) 
    1419 
    15  
    16  
    17 (define pwd current-directory) 
    1820(define-macro ($ arg) 
    1921  `(sys-getenv  
    20     ,(cond 
    21       ((string? arg) arg) 
    22       ((symbol? arg) (symbol->string arg)) 
    23       ((number? arg) (number->string arg)) 
    24       (else (x->string arg))))) 
     22     ,(string-upcase  
     23       (cond 
     24        ((string? arg) arg) 
     25        ((symbol? arg) (symbol->string arg)) 
     26        ((number? arg) (number->string arg)) 
     27        (else (x->string arg)))))) 
    2528 
    26 (define (ls . rest) 
    27   (remove #/^\.{1,2}$/ 
    28           (if (null? rest) 
    29               (directory-list (pwd)) 
    30               (apply directory-list rest)))) 
     29(define-macro (define-command command) 
     30  `(define-macro (,command . args) 
     31     (cons 'command 
     32           (cons ',command 
     33                 args)))) 
     34(define-macro (define-commands . commands) 
     35  `(begin 
     36     ,@(map (lambda (command) 
     37              `(define-command ,command)) 
     38            commands))) 
    3139 
    3240(define-macro (command com . args) 
    33   `(process-output->string-list 
    34     '(,(x->string com) 
    35       ,@(map x->string args)))) 
     41  (if (not (or (symbol? com) (string? com) (keyword? com))) 
     42      (error "Command name must be a symbol, a string, or a keyword.") 
     43      `(c (cons ,(x->string com) 
     44                (map x->string 
     45                     (list ,@(map (lambda (arg) 
     46                                    (if (symbol? arg) 
     47                                        (symbol->string arg) 
     48                                        arg)) 
     49                                  args))))))) 
     50 
     51(define (c command) 
     52  (process-output->string-list command :on-abnormal-exit :ignore)) 
     53 
     54(define-macro (rehash) 
     55  (let* ((pathes (string-split ($ PATH) ":")) 
     56         (commands (map 
     57                    string->symbol 
     58                    (apply 
     59                     append 
     60                     (map (lambda (path) (c `(ls ,path))) 
     61                          (filter file-exists? pathes)))))) 
     62    `(begin 
     63       ,@(map (lambda (command) 
     64                `(define-command ,command)) 
     65              commands) 
     66       (export ,@commands)))) 
    3667 
    3768(provide "shell")