home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / iksdpy.ksc < prev    next >
Text File  |  2020-01-01  |  7KB  |  202 lines

  1. #!/usr/local/bin/wermit +
  2. #
  3. # i k s d p y  --  IKSD display
  4. #
  5. # Active display of IKSD database.
  6. # Main screen lists all sessions, with option to switch to
  7. # a per-session detail screen.
  8. #
  9. # Illustrates:
  10. # . File record i/o
  11. # . WAIT FILE and \v(kbchar)
  12. # . Arithmetic
  13. # . Hexadecimal decoding
  14. # . Single-keystroke program control
  15. # . Compact substring notation
  16. # . Rudimentary screen formatting
  17. #
  18. # Requires: C-Kermit 7.0 Beta.11 or later.
  19. #
  20. # PRERELEASE:
  21. #   Last update: Tue Dec  7 19:19:03 1999
  22. #   Documentation: iksdb.txt
  23. #
  24. # Author: F. da Cruz, Columbia University, November 1999.
  25.  
  26. local \%c \%n \%p block dbfile rate dir user info
  27.  
  28. ; Configuration (change as needed)...
  29.  
  30. .rate = 4                               ; Screen refresh rate (sec)
  31.  
  32. if def \%1 .dbfile := \%1               ; Database file
  33. if not def dbfile {
  34.     if eq "\v(platform)" "32-bit_Windows" {
  35.     if not equal "\$(winbootdir)" "" .dbfile := \$(winbootdir)/iksd.db
  36.     else if not equal "\$(windir)" "" .dbfile := \$(windir)/iksd.db
  37.     else if not equal "\$(SystemRoot)" "" .dbfile := \$(SystemRoot)/iksd.db
  38.     else .dbfile = c:/iksd.db
  39.     } else {
  40.         .dbfile = /var/log/iksd.db
  41.     }
  42. }
  43.  
  44. ; Operating variables and settings...
  45.  
  46. set case off                            ; Case matters not
  47. set quiet on                            ; No noisy messages
  48. set sleep cancel on                     ; Allow interrupt WAIT from keyboard
  49.  
  50. ; Macros
  51.  
  52. define SHOWHELP {                       ; Main help screen
  53.     cls
  54.     echo {IKSD Display...}
  55.     echo
  56.     echo {Press:}
  57.     echo {  The Space Bar to switch between summary and detail screens.}
  58.     echo {  A digit to view a particular session, 0 through 9, or:}
  59.     echo {    X to enter a session number with two or more digits.}
  60.     echo {    M to return to the Main screen.}
  61.     echo {    N to see the Next session (cycle through sessions).}
  62.     echo {    H to see this Help message.}
  63.     echo {    Q to Quit.}
  64.     set ask-timer 0
  65.     echo
  66.     getc \%9 {Press any key to continue: }
  67.     set ask-timer \m(rate)
  68. }
  69.  
  70. define SHOWRECORD {                     ; Interpret a record
  71.     .pid := \fltrim(\s(block[17:16]),0)
  72.     .flags := \fhex2n(\s(block[1:4]))
  73.     if ( (\m(flags)&1) ) { .\%v = (IN_USE) } else { .\%v = (FREE) }
  74.     if ( (\m(flags)&2) ) { .\%v := \%v(REAL_USER) }
  75.     if ( (\m(flags)&4) ) { .\%v := \%v(LOGGED_IN) }
  76.     cls                                 ; Clear screen and print detail
  77.     echo [\m(dbfile)] Session \%1: \v(time)...
  78.     echo
  79.     echo { Flags: \s(block[1:4]) \%v}
  80.     echo { AuthT: \s(block[5:4])}
  81.     echo { AuthM: \s(block[9:4])}
  82.     echo { State: \s(block[13:4])}
  83.     echo { Lhost: \fhex2ip(\s(block[41:8]))} ; 32+8+1
  84.     echo { Rhost: \fhex2ip(\s(block[57:8]))} ; 48+8+1
  85.     echo { PID:   \m(pid) (\fhex2n(\m(pid)))}
  86.     echo { Start: \s(block[66:17])}
  87.     echo { Last:  \s(block[84:17])}
  88.     echo { User:  \s(block[1025:\fhex2n(\s(block[101:4]))])}
  89.     echo { Dir:   \s(block[2049:\fhex2n(\s(block[105:4]))])}
  90.     echo { Info:  \s(block[3073:\fhex2n(\s(block[109:4]))])}
  91.     echo
  92. }
  93.  
  94. define SHOWDETAIL {                     ; Show details for a session
  95.     local \%k \%v \%x
  96.     if not def \%1 return
  97.     if ( < \fcode({\%1}) 32 ) return
  98.     if eq {\%1} { } .\%1 = 0
  99.     if eq {\%1} {N} .\%1 = 0
  100.     .\%x = \%1
  101.     while true {                        ; Loop in detail screen
  102.         if def \%x switch \%x {         ; What did they type?
  103.       :[0-9]                        ; A digit
  104.               .\%1 := \%x
  105.           break                     ; So that's the session number.
  106.       :H, showhelp,                 ; An 'H' so show help text.
  107.               if not num \%1 return
  108.               break
  109.       :Q, echo Q, exit 0                     ; 'Q' for quit.
  110.           :N, .\%n ::= (\fsize(\m(dbfile))/4096) ; Check for new records
  111.               .\%1 ::= \fmod(\%1+1,\%n)          ; Go to next session
  112.               break
  113.       :X, xecho \13                          ; Long session number
  114.           screen cleol                       ; Clear this line
  115.           set ask-timer 0
  116.           ask \%1 { Enter session number: }  ; Prompt and wait for text
  117.           set ask-timer \m(rate)             ; up to CR with no timeout.
  118.           if not def \%1 .\%1 = 0            ; If CR only, use 0.
  119.               if not numeric \%1 {               ; Check for n-ness.
  120.                   echo Number required
  121.                   return
  122.               }
  123.           break
  124.           :M, return                             ; Return to main screen.
  125.       :default
  126.               echo Not a choice: "\%1"
  127.               pause 1
  128.               return
  129.         }
  130.         if ( > \%1 (\%n-1) || < \%1 0 ) { echo Out of range, pause 1, return }
  131.         .\%k ::= \%1*4096               ; Seek position for desired record
  132.         fclose \%c                      ; Close/Open to force data refresh
  133.         fopen /read /binary \%c \m(dbfile)
  134.         if fail end 1 CLOSE OPEN FAILURE
  135.     fseek \%c \%k                   ; Seek to record
  136.     if fail end 1 Seek failed.
  137.     .\%p := \f_pos(\%c)
  138.     fread /size:4096 \%c block      ; Read it
  139.     if fail end 1 Read failed.
  140.     if ( ! = \f_pos(\%c) \%p+4096 ) exit 1 FATAL - Database corrupt
  141.         showrecord \%1                  ; Show it
  142.     xecho {[0-9] (session digit), H[elp], or Q[uit]: }
  143.     wait 60 file modification \m(dbfile)
  144.     if success continue
  145.     undef \%x
  146.     if ( def \v(kbchar) ) { .\%x := \v(kbchar) } else { getc \%x }
  147.         if not def \%x continue
  148.         if eq { } {\%x} .\%x = M
  149.     }
  150. }
  151.  
  152. ; Main display...
  153.  
  154. undef \%c                               ; Database not yet open
  155. set ask-timer \m(rate)                  ; Refresh rate control
  156.  
  157. while true {                            ; Main loop
  158.     cls
  159.     if ( not def \%c ) {            ; If database not open
  160.     if exist \m(dbfile) {           ; If it exists
  161.         fopen /read /binary \%c \m(dbfile) ; Open it
  162.         if fail {
  163.         echo \m(dbfile) - Open failed: \f_errmsg()
  164.         sleep \m(rate)
  165.         continue
  166.         }
  167.     } else {                        ; It doesn't exist
  168.         echo [\m(dbfile)] - Does not exist: \v(time)...
  169.             sleep \m(rate)
  170.         continue
  171.     }
  172.     }
  173.     echo [\m(dbfile)] \v(time)...       ; Top line - filename & time
  174.     echo
  175.     echo {    Flgs Host........... Start... Last.... (PID)(user)(dir)(what)}
  176.     .\%n = 0
  177.     frewind \%c                         ; Rewind database
  178.     if fail { undef \%c, continue }     ; On failure go gack and reopen it
  179.     while ( ! \f_eof(\%c) ) {           ; Loop to read each record
  180.         fread /size:4096 \%c block
  181.         if fail break
  182.         if ( != \fmod(\f_pos(\%c),4096) 0 ) exit 1 FATAL - Database corrupt
  183.         incr \%p 4096
  184.         .user := \ftrim(\s(block[1025:1024]))
  185.         .dir  := \ftrim(\s(block[2049:1024]))
  186.         .info := \ftrim(\s(block[3073:1024]))
  187.         .\%a := \flpad(\%n,2). \s(block[1:4]) -
  188. \frpad(\fhex2ip(\s(block[57:8])),15)\s(block[74:9])\s(block[92:9]) -
  189. (\fhex2n(\s(block[17:16])))(\m(user))(\m(dir))(\m(info))
  190.     echo \fleft(\%a,\v(cols))
  191.         incr \%n
  192.     }
  193.     echo
  194.     xecho {[0-9] (session digit), H[elp], or Q[uit]: }
  195.     wait 60 file modification \m(dbfile)
  196.     if success continue
  197.     undef \%x
  198.     if ( def \v(kbchar) ) { .\%x := \v(kbchar) } else { getc \%x }
  199.     if ( defined \%x ) showdetail {\%x}
  200.     if ( == \%n 1 ) { fclose \%c, undef \%c } ; Force refresh if only one
  201. }
  202.