guile-user
[Top][All Lists]
Advanced

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

Re: Help


From: Barak Bar Oryon
Subject: Re: Help
Date: Wed, 31 Jan 2001 09:28:12 +0200 (IST)

Well this is the code:
#!/usr/local/bin/guile -s
!#
;; -*- scheme -*-

(use-modules (ice-9 expect))

(define out-port (current-output-port))

(define dynamic-expect
  (lambda(expect-thunk command-thunk)
    (let ((pipe1 (pipe))
          (pipe2 (pipe)))
      (setvbuf (car pipe1) _IONBF)
      (setvbuf (cdr pipe1) _IONBF)
      (setvbuf (car pipe2) _IONBF)
      (setvbuf (cdr pipe2) _IONBF)
      (let ((pid (primitive-fork)))
        (if (zero? pid)
          ;; I'm the child
          (with-input-from-port (car pipe2)
            (lambda()
              (with-output-to-port (cdr pipe1) command-thunk)))
          ;; I'm still me :)
          (begin (expect-thunk (cdr pipe2) (car pipe1) pid)
                 (waitpid pid)))))))

(define me
  (lambda(out in pid)
    (call-with-current-continuation
     (lambda(exit)
       (let ((expect-port in)
             (expect-timeout 4)
             (expect-timeout-proc
              (lambda (s)
                (display (string-append "Times up! " s "\n"))
                (kill pid SIGKILL)
                (exit 1)))
                     (expect-eof-proc
                      (lambda (s) (display "Reached the end of the file!\n")))
                     ;; (expect-char-proc display)
                     (expect-strings-compile-flags (logior regexp/newline 
regexp/icase))
                     (expect-strings-exec-flags 0))
         (letrec ((expect-loop (lambda(i)
                                 (if (zero? i)
                                   '()
                                   (begin
                                     (expect-strings
                                      ("ame \\(gandalf:barak\\):" =>
                                       (lambda(v)
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "ok \n" out)))
                                      ("assword:" =>
                                       (lambda(v)
                                         (newline)
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "mypassword\n" out)))
                                      ("ftp>" =>
                                       (lambda(v)
                                         (newline)
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "ls\n" out))))
                                     (flush-all-ports)
                                     (expect-loop (- i 1)))))))
           (expect-loop 3)))))))
    
(define dummy-ftp
  (lambda()
    (display "ame (gandalf:barak):")
    (flush-all-ports)
    (display (string-append "ftp got [" (read) "]") out-port)
    (display "assword:")
    (flush-all-ports)
    (display (string-append "ftp got [" (read) "]") out-port)
    (display "ftp>")
    (flush-all-ports)
    (display (string-append "ftp got [" (read) "]\n") out-port)
    ))
     
(define proc
  (lambda()
    (move->fdes (current-input-port) 0)
    (move->fdes (current-output-port) 1)
    ;; (dummy-ftp)
    (execlp "ftp")
    ))
  

(dynamic-expect me proc)

The output with the dummy-ftp is:
address@hidden scm]$ ./ports.scm 
expect got [ame (gandalf:barak):]
ftp got [ok]
expect got [assword:]
ftp got [mypassword]
expect got [ftp>]
ftp got [ls]
and the output with the real ftp is:
address@hidden scm]$ ./ports.scm 
Times up! 

Thanks,
Barak.




reply via email to

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