home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / remoteaccess < prev    next >
Text File  |  2020-01-01  |  7KB  |  192 lines

  1. #!/usr/local/bin/kermit
  2.  
  3. ; r e m o t e a c c e s s
  4. ; How to conduct a dialog with a user when Kermit does not have a controlling
  5. ; terminal; for example, when Kermit has accepted an incoming Internet ("set
  6. ; host *") or modem ("answer") connection, or when a Kermit script is started
  7. ; under inetd.  This script shows how to handle echoing and rudimentary
  8. ; keyboard editing:
  9. ;
  10. ; . Prints a prompt and waits for a line of text.
  11. ; . Ordinary characters echo.
  12. ; . Rubout, Delete, or Backspace removes the rightmost character from the line.
  13. ; . Ctrl-U removes the current line, if any.
  14. ; . Ctrl-R repaints the current line.
  15. ; . The prompt is protected.
  16. ;
  17. ; To make it more interesting, lines are interpreted as commands, validated,
  18. ; and executed.
  19. ;
  20. ; Note that when printing lines to the user's screen, both carriage (\13)
  21. ; and linefeed (\10) are included for when there is no terminal driver
  22. ; to supply them.  In case there IS a terminal driver, the SET INPUT ECHO OFF
  23. ; command allows the script to work there too.
  24. ;
  25. ; F. da Cruz, Columbia University, January 2003
  26.  
  27. ; First make a command keyword table and then sort it for \tablelook().
  28. ;
  29. declare \&k[] = exit:5 quit:5 help:2 ?:2 echo:1 send:3 list:4 directory:4
  30. sort &k
  31.  
  32. ; Make an array of help strings keyed on keyword value.
  33. ;
  34. dcl \&h[5]
  35. .\&h[1] = ECHO [text] echos its arguments.
  36. .\&h[2] = HELP prints this message; Synonym: "?".
  37. .\&h[3] = SEND filename [as-name] sends the given file under the given name.
  38. .\&h[4] = LIST [filespec] lists files; Synonym: DIRECTORY.
  39. .\&h[5] = EXIT exits; Synonym: QUIT.
  40.  
  41. ; Define command service routines
  42.  
  43. define DOEXIT {                        ; Service routine for EXIT command
  44.     output "\13\10Bye.\13\10"
  45.     sleep 1
  46.     exit
  47. }
  48.  
  49. define DOSEND {                        ; Service routine for SEND command
  50.     if not defined \%1 {
  51.         output "Filename required\13\10"
  52.         end 1
  53.     } else if not exist \%1 {
  54.         output "File not found: "\%1"\13\10"
  55.         end 1
  56.     }
  57.     send \%1 \%2
  58. }
  59.  
  60. define DOLIST {                        ; Service routine for LIST command
  61.     local \&d[] \%i \%f \%m
  62.     directory /array:&d \%1            ; Get filenames into array &d[]
  63.     output "\13\10"
  64.     if not \fdim(&d) {
  65.         output "(No files match)\13\10"
  66.     } else {
  67.         .\%m := 0                      ; Quickly get length of longest name
  68.         for \%i 1 \fdim(&d) 1 {
  69.             .\%m := \fmax(\%m,\flen(\&d[\%i]))
  70.         }
  71.         incr \%m
  72.         for \%i 1 \fdim(&d) 1 {        ; Print listing
  73.             .\%f := \&d[\%i]
  74.             if not readable \%f continue
  75.             output "  \frpad(\%f,\%m) \flpad(\fsize(\%f),9)  \fdate(\%f)\13\10"
  76.         }
  77.     }
  78.     output "\13\10"
  79. }
  80.  
  81. define DOHELP {                        ; Service routine for HELP command
  82.     local \%i
  83.     output "\13\10"
  84.     output "Commands are:\13\10\13\10"
  85.     for \%i 1 \fdim(&h) 1 {
  86.         output "  \&h[\%i]\13\10"
  87.     }
  88.     output "\13\{10}Commands may be abbreviated.\13\10"
  89.     output "\13\10"
  90. }
  91.  
  92. ; Command parser and dispatcher
  93.  
  94. define DOCOMMAND {
  95.     if not def \%1 return              ; Ignore blank lines
  96.     void \fsplit(\%1,&a,\32)           ; Split line into words
  97.     .\%k := \ftablelook(\&a[1],&k)     ; Look up first word
  98.     switch \%k {                       ; Handle lookup error
  99.       :-1
  100.         output "Command not found: "\&a[1]"\13\10"
  101.         output "Type HELP for help\13\10"
  102.         continue
  103.       :-2
  104.         output "Ambiguous: "\&a[1]"\13\10"
  105.         output "Type HELP for help\13\10"
  106.         continue
  107.     }
  108.     switch \fword(\&k[\%k],2,:) {      ; Dispatch on keyword value
  109.       :1                               ; ECHO prints its arguments
  110.         output "\fjoin(&a[2:])\13\10"
  111.         break
  112.       :2                               ; HELP prints help text
  113.         dohelp
  114.         break
  115.       :3                               ; SEND sends a file
  116.         dosend \&a[2] \&a[3]           ; with an optional as-name
  117.         output "\13\10"
  118.         break
  119.       :4                               ; LIST prints a file list
  120.         dolist \&a[2]
  121.         break
  122.       :5                               ; QUIT and EXIT quit
  123.         doexit
  124.     }
  125. }
  126.  
  127. set case off                           ; Commands are case-independent
  128. set input echo off                     ; In case there is a controlling tty
  129. set root .                             ; Restrict users to this directory tree
  130.  
  131. define prompt "Command: "              ; Define prompt
  132. output "\13\10"                        ; Start on a new line
  133. output "Type HELP for help.\13\10"     ; Greet
  134.  
  135. ; Loop to read lines and pass them to the command interpreter.
  136. ; This is where we handle echoing and editing.
  137.  
  138. while true {                           ; Outer loop for lines
  139.     undef buf                          ; Line buffer
  140.     .\%n = 0                           ; Line length
  141.     output "\m(prompt)"                ; Prompt
  142.     set flag off                       ; Inner loop control
  143.     while not flag {                   ; Inner loop for characters
  144.         input -1                       ; Get a character
  145.         if fail stop 1 INPUT Error     ; Make sure we did
  146.         .\%c := \v(inchar)             ; This is the character
  147.         .\%x := \fcode(\%c)            ; This is its code
  148.         switch \%x {                   ; switch on its code
  149.           :3                           ; Ctrl-C
  150.             doexit                     ; Exit immediately
  151.           :13                          ; Carriage Return
  152.           :10                          ; or Line Feed
  153.           :12                          ; or Form Feed
  154.             output "\13\10"            ; Echo CRLF
  155.             set flag on                ; Set inner-loop exit flag
  156.             docommand "\m(buf)"        ; Process the user's command
  157.             continue
  158.           :8                           ; Backspace
  159.           :127                         ; or Rubout
  160.             if not def buf {           ; Nothing to delete
  161.                 output "\7"            ; just beep
  162.                 continue
  163.             }
  164.             output "\8 \8"             ; Erase char from user's screen
  165.             decr \%n                   ; And from line buffer
  166.             .buf := \s(buf[1:\%n])
  167.             continue
  168.           :18                          ; Ctrl-R
  169.             output "\13\10"            ; Refresh line
  170.             output "\m(prompt)\m(buf)"
  171.             continue            
  172.           :21                          ; Ctrl-U
  173.             for \%i 1 \%n 1 {          ; Erase line from screen
  174.                 output "\8 \8"
  175.             }
  176.             .\%n = 0
  177.             undef buf
  178.             continue
  179.           :9                           ; Ctrl-I (Tab)
  180.             .\%c := \32                ; Convert to space
  181.             .\%x := 32                 ; and fall thru.
  182.           :default                     ; Anything else
  183.             if > \%x 31 {              ; If char is printable
  184.                 output \%c             ; echo it
  185.                 .buf := \m(buf)\%c     ; add it to line buffer
  186.                 incr \%n               ; and count it
  187.             } else output "\7"         ; Beep for nonprintables
  188.         }          
  189.     }
  190. }
  191.