home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume38 / hlgrep / part01 < prev    next >
Encoding:
Text File  |  1993-06-19  |  10.3 KB  |  350 lines

  1. Newsgroups: comp.sources.misc
  2. From: mfuhr@cwis.unomaha.edu (Michael Fuhr)
  3. Subject: v38i003:  hlgrep - a highlighting grep, Part01/01
  4. Message-ID: <1993Jun17.180610.8568@sparky.imd.sterling.com>
  5. X-Md4-Signature: c16d301fce2395ca3f1e92cc9b584cab
  6. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  7. Organization: University of Nebraska at Omaha
  8. Date: Thu, 17 Jun 1993 18:06:10 GMT
  9. Approved: kent@sparky.imd.sterling.com
  10.  
  11. Submitted-by: mfuhr@cwis.unomaha.edu (Michael Fuhr)
  12. Posting-number: Volume 38, Issue 3
  13. Archive-name: hlgrep/part01
  14. Environment: perl
  15.  
  16. hlgrep is a perl script that works like grep(1), but highlights the
  17. patterns it finds.  Unlike hgrep(1), hlgrep can highlight patterns with
  18. metacharacters.
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then feed it
  22. # into a shell via "sh file" or similar.  To overwrite existing files,
  23. # type "sh file -c".
  24. # Contents:  README Makefile hlgrep hlgrep.man
  25. # Wrapped by kent@sparky on Thu Jun 17 12:50:25 1993
  26. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  27. echo If this archive is complete, you will see the following message:
  28. echo '          "shar: End of archive 1 (of 1)."'
  29. if test -f 'README' -a "${1}" != "-c" ; then 
  30.   echo shar: Will not clobber existing file \"'README'\"
  31. else
  32.   echo shar: Extracting \"'README'\" \(862 characters\)
  33.   sed "s/^X//" >'README' <<'END_OF_FILE'
  34. Xhlgrep README - 2 Jun 93
  35. X
  36. XDESCRIPTION
  37. X===========
  38. X
  39. Xhlgrep is a perl script that works like grep(1), but highlights the
  40. Xpatterns it finds.  Unlike hgrep(1), hlgrep can highlight patterns with
  41. Xmetacharacters.
  42. X
  43. XHighlighting is done using your terminal's standout capability, but you
  44. Xcan edit hlgrep to use any capability you want.  The current version is
  45. Xhardcoded for the vt100, but you can edit it to use tput(1) to get the
  46. Xcapabilities for any terminal at runtime.
  47. X
  48. X
  49. XINSTALLATION
  50. X============
  51. X
  52. X1.  Edit Makefile if you want to change the default installation
  53. X    directories (/usr/local/bin and /usr/local/man/man1).
  54. X
  55. X2.  Edit hlgrep if you want to add tput capability or use terminal
  56. X    capabilities other than vt100 standout.  Comments will tell you
  57. X    how.
  58. X
  59. X3.  Run "make install".
  60. X
  61. X
  62. XMichael Fuhr
  63. Xmfuhr@cwis.unomaha.edu (but probably not after Summer '93).
  64. END_OF_FILE
  65.   if test 862 -ne `wc -c <'README'`; then
  66.     echo shar: \"'README'\" unpacked with wrong size!
  67.   fi
  68.   # end of 'README'
  69. fi
  70. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  71.   echo shar: Will not clobber existing file \"'Makefile'\"
  72. else
  73.   echo shar: Extracting \"'Makefile'\" \(1267 characters\)
  74.   sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  75. X#------------------------------------------------------------------------------
  76. X# Makefile for hlgrep
  77. X#
  78. X# Configuration:
  79. X#
  80. X#    Change BINDIR to the destination directory for the executable.
  81. X#    Change MANTOP to the top-level manual directory.
  82. X#    Change MANEXT to the manual section.
  83. X#------------------------------------------------------------------------------
  84. X
  85. XBINDIR    = /usr/local/bin
  86. X
  87. XMANTOP    = /usr/local/man
  88. XMANEXT    = 1
  89. X
  90. X#------------------------------------------------------------------------------
  91. X# Shouldn't need to change anything below here.
  92. X#------------------------------------------------------------------------------
  93. X
  94. XHLGREP        = hlgrep
  95. XMANPAGE        = $(HLGREP).man
  96. XMAKEFILE    = Makefile
  97. XREADME        = README
  98. X
  99. XALLFILES    = $(README) $(MAKEFILE) $(HLGREP) $(MANPAGE)
  100. X
  101. XSHARFILE    = $(HLGREP).shar
  102. X
  103. XMANDIR        = $(MANTOP)/man$(MANEXT)
  104. XMANDEST        = $(MANDIR)/$(HLGREP).$(MANEXT)
  105. X
  106. X#------------------------------------------------------------------------------
  107. X# Targets
  108. X#------------------------------------------------------------------------------
  109. X
  110. Xall: $(ALLFILES)
  111. X
  112. Xinstall:
  113. X    install -c -m 755 $(HLGREP) $(BINDIR)
  114. X    install -c -m 644 $(MANPAGE) $(MANDEST)
  115. X
  116. Xshar: $(SHARFILE)
  117. X
  118. X$(SHARFILE): $(ALLFILES)
  119. X    rm -f $(SHARFILE)
  120. X    shar $(ALLFILES) > $(SHARFILE)
  121. X
  122. Xclean:
  123. X    rm -f $(SHARFILE)
  124. END_OF_FILE
  125.   if test 1267 -ne `wc -c <'Makefile'`; then
  126.     echo shar: \"'Makefile'\" unpacked with wrong size!
  127.   fi
  128.   # end of 'Makefile'
  129. fi
  130. if test -f 'hlgrep' -a "${1}" != "-c" ; then 
  131.   echo shar: Will not clobber existing file \"'hlgrep'\"
  132. else
  133.   echo shar: Extracting \"'hlgrep'\" \(3316 characters\)
  134.   sed "s/^X//" >'hlgrep' <<'END_OF_FILE'
  135. X#!/usr/bin/perl
  136. Xeval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  137. X    if 0;
  138. X#------------------------------------------------------------------------------
  139. X# hlgrep - highlight grep
  140. X#
  141. X# See the manpage for more information.
  142. X#------------------------------------------------------------------------------
  143. X
  144. Xrequire "getopts.pl";
  145. X
  146. X($progname = $0) =~ s#.*/##;
  147. X
  148. X#------------------------------------------------------------------------------
  149. X# Define the begin/end standout codes for the terminal.  The hardcoded
  150. X# values are for the vt100 and its kind.  For other terminals, look up
  151. X# the so/se capabilities in /etc/termcap or the smso/rmso capabilities
  152. X# in the terminfo database.  A more robust way would be to get the codes
  153. X# at runtime with something like
  154. X#
  155. X#    $so = `tput smso`;
  156. X#    $se = `tput rmso`;
  157. X#
  158. X# if your system has tput.  The disadvantage of this is the cost of
  159. X# running the two tput processes.  An even better way would be to read
  160. X# the values directly from /etc/termcap or the terminfo database.
  161. X#
  162. X# Feel free to any terminal capability for highlighting.  Here are a few
  163. X# capabilities for the vt100 (some may not work on your terminal):
  164. X#
  165. X#    Name        Termcap/Terminfo    String
  166. X#    ---------    ----------------    ------
  167. X#    Standout         so/smso        \e[7m
  168. X#    Underline         us/smul        \e[4m
  169. X#    Bold             md/bold        \e[1m
  170. X#    Blinking         mb/blink        \e[5m
  171. X#------------------------------------------------------------------------------
  172. X
  173. X$so = "\e[7m";
  174. X$se = "\e[m";
  175. X
  176. X#------------------------------------------------------------------------------
  177. X# Get options and pattern from command line.  Check syntax.
  178. X#------------------------------------------------------------------------------
  179. X
  180. X&Getopts("aDhinw") || exit 2;
  181. X$pattern = shift @ARGV;
  182. X
  183. Xdo { $! = 2; die "Usage: $progname [ -aDhinw ] pattern [ filename ... ]\n" }
  184. X    unless ($pattern);
  185. X
  186. X#------------------------------------------------------------------------------
  187. X# Escape any slashes in the pattern, because we use the slash as the
  188. X# pattern-matching delimiter.
  189. X#------------------------------------------------------------------------------
  190. X
  191. X$pattern =~ s#/#\\/#;
  192. X
  193. X#------------------------------------------------------------------------------
  194. X# Set some variables to be used when building the command to be eval'd.
  195. X#------------------------------------------------------------------------------
  196. X
  197. X$opt_a || do { $maybe_skip = "next"; };
  198. X$opt_h || do { $name = "\$ARGV:" if (@ARGV > 1); };
  199. X$opt_i && do { $sflags = "i"; };
  200. X$opt_n && do { $linenum = "\$.:"; };
  201. X$opt_w && do { $pattern = "\\b($pattern)\\b"; };
  202. X
  203. X#------------------------------------------------------------------------------
  204. X# Generate the code.  Explicitly closing each file will reset the line
  205. X# number ($.) (see eof in the perl manpage).
  206. X#------------------------------------------------------------------------------
  207. X
  208. X$cmd = <<"AMEN";
  209. X\$exit_status = 1;
  210. Xwhile (<>) {
  211. X    if (s/$pattern/$so\$&$se/g$sflags) {
  212. X        \$exit_status = 0;
  213. X    }
  214. X    else {
  215. X        $maybe_skip;
  216. X    }
  217. X    print "$name$linenum\$_";
  218. X} continue {
  219. X    close ARGV if eof;
  220. X}
  221. Xexit \$exit_status;
  222. XAMEN
  223. X
  224. X#------------------------------------------------------------------------------
  225. X# Execute the code.  But print it first if the user wanted.
  226. X#------------------------------------------------------------------------------
  227. X
  228. Xprint STDERR $cmd if $opt_D;
  229. X$| = 1;
  230. Xeval $cmd;
  231. END_OF_FILE
  232.   if test 3316 -ne `wc -c <'hlgrep'`; then
  233.     echo shar: \"'hlgrep'\" unpacked with wrong size!
  234.   fi
  235.   # end of 'hlgrep'
  236. fi
  237. if test -f 'hlgrep.man' -a "${1}" != "-c" ; then 
  238.   echo shar: Will not clobber existing file \"'hlgrep.man'\"
  239. else
  240.   echo shar: Extracting \"'hlgrep.man'\" \(1923 characters\)
  241.   sed "s/^X//" >'hlgrep.man' <<'END_OF_FILE'
  242. X.TH HLGREP 1 "2 Jun 1993"
  243. X.SH NAME
  244. Xhlgrep - highlight grep
  245. X.SH SYNOPSIS
  246. Xhlgrep [ -aDhinw ]
  247. X.I pattern
  248. X[
  249. X.IR filename " .\|.\|. ]"
  250. X.SH DESCRIPTION
  251. X.B hlgrep
  252. Xsearches the given
  253. X.I filenames
  254. X(or the standard input if no files are specified)
  255. Xfor lines containing the regular expression
  256. X.IR pattern 
  257. Xand prints those lines with the matching patterns highlighted.
  258. X.LP
  259. XRegular expressions are the same as those used in
  260. X.BR perl (1).
  261. X.SH OPTIONS
  262. X.TP
  263. X.B \-a
  264. XPrint all lines, not just those that contain matching patterns.
  265. XThis is useful for seeing the patterns in context.
  266. X.TP
  267. X.B \-D
  268. XPrint debugging info.
  269. X.B hlgrep
  270. Xbuilds a command for perl to
  271. X.IR eval .
  272. XThis option causes that command to be printed to standard error before
  273. Xany processing.
  274. X.TP
  275. X.B \-h
  276. XDon't print filenames when more than one file is given on the command line.
  277. X.TP
  278. X.B \-i
  279. XIgnore case.
  280. X.TP
  281. X.B \-n
  282. XPrint line numbers.
  283. X.TP
  284. X.B \-w
  285. XMatch patterns only as complete words.
  286. X.LP
  287. XTypical grep options like
  288. X.B \-l
  289. X(print filenames only),
  290. X.B \-c
  291. X(print count of matching lines only),
  292. X.B \-s
  293. X(set exit status only), and
  294. X.B \-v
  295. X(print lines that don't match) defeat the purpose of this program
  296. X(highlighting) and aren't provided.  Use one of the other greps if
  297. Xyou need them.
  298. X.SH NOTES
  299. X.B hlgrep
  300. Xis written in perl, so your system must have perl installed.  It uses
  301. Xthe package
  302. X.BR getopts.pl ,
  303. Xwhich some versions of perl earlier than 4.036 don't have.
  304. X.SH "SEE ALSO"
  305. X.BR agrep (1),
  306. X.BR egrep (1),
  307. X.BR fgrep (1),
  308. X.BR grep (1),
  309. X.BR hgrep (1),
  310. X.BR perl (1),
  311. X.BR tput (1)
  312. X.SH BUGS
  313. XThe current version of
  314. X.B hlgrep
  315. Xuses hardcoded values for terminal capability strings, but it can
  316. Xbe configured to use
  317. X.BR tput (1)
  318. Xto get terminal capabilities at runtime.
  319. X.LP
  320. XPlease report any other bugs to the author.
  321. X.SH DIAGNOSTICS
  322. XExit status 0 if pattern found, 1 if pattern not found, 2 if syntax
  323. Xerror.
  324. X.SH AUTHOR
  325. XMichael Fuhr (mfuhr@cwis.unomaha.edu, but probably not after
  326. XSummer '93).
  327. END_OF_FILE
  328.   if test 1923 -ne `wc -c <'hlgrep.man'`; then
  329.     echo shar: \"'hlgrep.man'\" unpacked with wrong size!
  330.   fi
  331.   # end of 'hlgrep.man'
  332. fi
  333. echo shar: End of archive 1 \(of 1\).
  334. cp /dev/null ark1isdone
  335. MISSING=""
  336. for I in 1 ; do
  337.     if test ! -f ark${I}isdone ; then
  338.     MISSING="${MISSING} ${I}"
  339.     fi
  340. done
  341. if test "${MISSING}" = "" ; then
  342.     echo You have the archive.
  343.     rm -f ark[1-9]isdone
  344. else
  345.     echo You still must unpack the following archives:
  346.     echo "        " ${MISSING}
  347. fi
  348. exit 0
  349. exit 0 # Just in case...
  350.