home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95 / review.ksc < prev    next >
Text File  |  2020-01-01  |  6KB  |  186 lines

  1. # REVIEW
  2. #
  3. # Interactively prompts you with each filename that matches the argument
  4. # (or all filenames in the current directory if no argument given), giving
  5. # you a chance to type it, delete it, edit it, rename it, copy it, etc.
  6. # In UNIX, wildcards are expanded by the shell on the command line;
  7. # elsewhere, this script expands (a single) wildcard argument itself.
  8. #
  9. # Usage: TAKE REVIEW.KSC [ filespec ]
  10. #
  11. # Hint: Use forward slashes, not backslashes, in filespec.
  12. #
  13. # Illustrates:
  14. #  . Processing command-line arguments in "kerbang" scripts.
  15. #  . FOR and WHILE loops and SWITCH statements.
  16. #  . Nested loops.
  17. #  . Assignment of wildcard expansion to an array.
  18. #  . Sorting an array.
  19. #  . Reading commands from keyboard and matching them to a keyword list.
  20. #  . Getting and displaying information about files.
  21. #  . TYPE command switches like /HEAD, /TAIL, /COUNT, and /SEARCH.
  22. #
  23. # C-Kermit 7.0 or later or K95 1.1.19 or later required.
  24. #
  25. # Author: F. da Cruz, Columbia University
  26. #
  27. # V 1.0, 16 Apr 1999 - First version.
  28. # V 2.0, 26 Apr 1999 - Adds keyword table, HEAD, TAIL, SEARCH, etc;
  29. #                      accepts a file list on the command line
  30. # V 3.0, 20 May 1999 - Adds LIST command,
  31. #                      speedups via ARRAY COPY and \tablelook().
  32. #
  33. local file p \%i \%j \%a \%x &a[] \&c[] newname pattern ; Local variables
  34.  
  35. if < \v(xversion) 1119 echo K95 1.1.19 required, exit
  36.  
  37. ; Command keyword list
  38. ;
  39. dcl \&c[] = -
  40.   copy count delete edit head help info list next -
  41.   previous quit rename run search send tail type
  42.  
  43. echo Reviewing files in \v(dir)...  ; Greeting
  44. sort &c                             ; Put commands in alphabetical order.
  45. .\%n ::= \v(argc) - 1               ; How many arguments on command line.
  46.  
  47. switch \%n {
  48.   :0, .\%n := \ffiles(*,&a), break       ; No args, do * in current directory.
  49.   :1, .\%n := \ffiles(\&_[1],&a), break  ; 1 arg, expand it.
  50.   :default, array copy &_[] &a[]         ; More than 1 arg, use them literally.
  51. }
  52.  
  53. ; Now file list is in global array \&a[].
  54.  
  55. echo \%n file(s) match.
  56. sort &a                             ; Sort the file list
  57. echo
  58. echo Type "help" for help.          ; Give instructions
  59. echo
  60.  
  61. set case off                        ; Case doesn't matter in commands
  62.  
  63. define DOCOMMAND {                  ; Execute a command: \%1 is the command
  64.     switch \%1 {                    ; Handle the command
  65.       :next                         ; Proceed to next file
  66.      set flag on
  67.      break
  68.       :delete               
  69.      delete /verbose \m(file)   ; Delete this file
  70.      if success {               ; If it was deleted
  71.          .file = (DELETED)      ; replace its array entry
  72.          .\&a[\%i] := \m(file)
  73.          set flag on            ; and go on to next file
  74.      }
  75.      break
  76.       :quit                         ; Quit
  77.          stop 1 Bye.
  78.       :type                         ; Type this file
  79.      type /page \m(file)
  80.      break
  81.       :count                        ; Count lines in this file
  82.      type /count \m(file)
  83.      break
  84.       :head                         ; Show first lines of this file
  85.      type /head \m(file)
  86.      break
  87.       :tail                         ; Show last lines of this file
  88.      type /tail \m(file)
  89.      break
  90.       :list
  91.      type /page /prefix:{\\flpad(\\v(ty_ln),3). } \m(file)
  92.      break
  93.       :edit                         ; View this file in the editor
  94.      edit \m(file)
  95.      break
  96.       :previous                     ; Back to previous file
  97.      if ( > \%i 1 ) {           ; (yes we modify the loop variable)
  98.          decrement \%i 2
  99.          set flag on
  100.      }
  101.      break
  102.       :run                          ; Run this file
  103.      run \m(file)
  104.      break
  105.       :info                         ; Print info about this file
  106.      if equal "\m(file)" "(DELETED)" break
  107.      if equal "\m(file)" "(RENAMED)" break
  108.      echo \fperm(\m(file)) \fsize(\m(file)) \fdate(\m(file)) \m(file)
  109.      break
  110.       :rename                       ; Rename this file
  111.      ask newname { Rename \m(file) to: }
  112.      if not def newname break
  113.      rename /verbose \m(file) \m(newname)
  114.      if success {               ; Handled like DELETE (q.v.)
  115.          .file = (RENAMED)
  116.          .\&a[\%i] := \m(file)
  117.          set flag on
  118.      }
  119.      break
  120.       :copy                         ; Copy this file
  121.      ask newname { Copy \m(file) to: }
  122.      if def newname { copy /verbose \m(file) \m(newname) }
  123.      break
  124.       :search
  125.      ask pattern { Pattern(s) to search for: }
  126.      if not def pattern break
  127.      type /match:\m(pattern) \m(file)
  128.      break
  129.       :send                         ; Send the file
  130.      send \m(file)
  131.      break
  132.       :help                         ; Print help message
  133.      echo
  134.      echo \fbasename(\v(cmdfile)) Commands:
  135.      echo { copy     - Copy this file.}
  136.      echo { count    - Count lines in this file.}
  137.      echo { delete   - Delete this file.}
  138.      echo { edit     - Edit this file.}
  139.      echo { head     - Show top 10 lines of this file.}
  140.      echo { help     - Print this message.}
  141.      echo { info     - Show info about this file.}
  142.      echo { list     - List this file with line numbers.}
  143.      echo { next     - Next file (default).}
  144.      echo { previous - Previous file.}
  145.      echo { rename   - Rename this file.}
  146.      echo { run      - Run this file.}
  147.      echo { send     - Send this file.}
  148.      echo { search   - Search this file for patterns.}
  149.      echo { tail     - Show the last 10 lines of this file.}
  150.      echo { type     - Type this file.}
  151.      echo { quit     - Quit from \fbasename(\v(cmdfile))}
  152.      echo {Commands may be abbreviated.}
  153.      echo
  154.      break
  155.       :default
  156.      echo "\%a" - not a valid command - type "help" for help.
  157.     }
  158. }
  159.  
  160. for \%i 1 \%n 1 {                            ; Loop for all files
  161.     set flag off                             ; Flag ON means go to next file
  162.     asg file \fcontents(\&a[\%i])
  163.     if directory \m(file) asg file \m(file) (DIRECTORY)
  164.     while not flag {                         ; Loop for each file
  165.         asg p \flpad(\%i,3). \freplace(\m(file),\\,/)
  166.     ask \%a { \m(p): }                   ; Prompt filename & get command
  167.     if not def \%a {
  168.             def \%x next                     ; Default command is "next"
  169.         } else {
  170.             .\%x := \ftablelook(\%a,&c)      ; Look up what they typed
  171.         if ( = \%x -1 ) {                ; Not found
  172.         echo No such command: "\%a"
  173.         continue
  174.         }
  175.         if ( = \%x -2 ) {                ; Ambiguous
  176.         echo Ambiguous: "\%a"
  177.         continue
  178.         }
  179.         .\%x := \&c[\%x]                 ; Good - get full name
  180.         }
  181.         docommand \%x                        ; Execute it
  182.     }
  183. }
  184. echo
  185. end 0 Bye.
  186.