home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / findstr.icn < prev    next >
Text File  |  2002-03-26  |  2KB  |  79 lines

  1. ############################################################################
  2. #
  3. #    File:     findstr.icn
  4. #
  5. #    Subject:  Program to find embedded character strings
  6. #
  7. #    Author:   Robert J. Alexander
  8. #
  9. #    Date:     March 26, 2002
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  Utility filter to list character strings embedded in data files (e.g.
  18. #  object files).
  19. #
  20. #    findstr -options file...
  21. #
  22. #    -l length    minimum string size to be printed (default 3)
  23. #    -c chars    a string of characters (besides the standard ASCII
  24. #            printable characters) to be considered part of a
  25. #            string
  26. #
  27. #  Icon string escape sequences can be used to specify the -c option.
  28. #
  29. #  Multiple files can be specified as arguments, and will be processed
  30. #  in sequence.
  31. #
  32.  
  33. link options,escape
  34.  
  35. procedure main(arg)
  36.    local c, f, fn, header, min_string_size, okchars, opt, s, istring
  37.    #
  38.    #  Process command line options and file names.
  39.    #
  40.    opt := options(arg,"l+c:")
  41.    if *arg = 0 then stop("Usage: findstr -options file..._
  42.       \n_
  43.       \n-l length\tminimum string size to be printed (default 3)_
  44.       \n-c chars\ta string of characters (besides the standard ASCII_
  45.       \n\t\tprintable characters) to be considered part of a string_
  46.       \n")
  47.    #
  48.    #  Define minimum string size to print.
  49.    #
  50.    min_string_size := \opt["l"] | 3    # default min string size = 3
  51.    #
  52.    #  Define characters that can be in strings.
  53.    #
  54.    okchars := cset(&ascii[33:-1])    # normal ASCII printable characters
  55.    okchars ++:= istring(\opt["c"])    # additional chars supplied by user
  56.    #
  57.    #  Loop to process files.
  58.    #
  59.    every fn := !arg do {
  60.       f := open(fn,"u") | stop("Can't open input file \"",fn,"\"")
  61.       #
  62.       #  Now find and print the strings.
  63.       #
  64.       header := if *arg > 1 then fn || ": " else ""
  65.       s := ""
  66.       while c := reads(f) do {
  67.      if any(okchars,c) then s ||:= c
  68.      else {
  69.         if *s >=  min_string_size then write(header,image(s))
  70.         s := ""
  71.         }
  72.      }
  73.       #
  74.       #  Close this file.
  75.       #
  76.       close(f)
  77.       }
  78. end
  79.