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

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