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

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