[Top][All Lists]

[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)
              (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)
       (let ((expect-port in)
             (expect-timeout 4)
              (lambda (s)
                (display (string-append "Times up! " s "\n"))
                (kill pid SIGKILL)
                (exit 1)))
                      (lambda (s) (display "Reached the end of the file!\n")))
                     ;; (expect-char-proc display)
                     (expect-strings-compile-flags (logior regexp/newline 
                     (expect-strings-exec-flags 0))
         (letrec ((expect-loop (lambda(i)
                                 (if (zero? i)
                                      ("ame \\(gandalf:barak\\):" =>
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "ok \n" out)))
                                      ("assword:" =>
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "mypassword\n" out)))
                                      ("ftp>" =>
                                         (display (string-append "expect got [" 
v "]\n"))
                                         (display "ls\n" out))))
                                     (expect-loop (- i 1)))))))
           (expect-loop 3)))))))
(define dummy-ftp
    (display "ame (gandalf:barak):")
    (display (string-append "ftp got [" (read) "]") out-port)
    (display "assword:")
    (display (string-append "ftp got [" (read) "]") out-port)
    (display "ftp>")
    (display (string-append "ftp got [" (read) "]\n") out-port)
(define proc
    (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! 


reply via email to

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