Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 1 | # 'grep' |
| 2 | |
Guido van Rossum | 5fca8a1 | 1991-12-31 00:00:35 +0000 | [diff] [blame] | 3 | import regex |
| 4 | from regex_syntax import * |
Guido van Rossum | 217a5fa | 1990-12-26 15:40:07 +0000 | [diff] [blame] | 5 | import string |
| 6 | |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 7 | opt_show_where = 0 |
| 8 | opt_show_filename = 0 |
| 9 | opt_show_lineno = 1 |
Guido van Rossum | 5fca8a1 | 1991-12-31 00:00:35 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 6a1f089 | 1992-04-06 14:03:53 +0000 | [diff] [blame] | 11 | def grep(pat, *files): |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 12 | return ggrep(RE_SYNTAX_GREP, pat, files) |
Guido van Rossum | 5fca8a1 | 1991-12-31 00:00:35 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 6a1f089 | 1992-04-06 14:03:53 +0000 | [diff] [blame] | 14 | def egrep(pat, *files): |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 15 | return ggrep(RE_SYNTAX_EGREP, pat, files) |
Guido van Rossum | 5fca8a1 | 1991-12-31 00:00:35 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 6a1f089 | 1992-04-06 14:03:53 +0000 | [diff] [blame] | 17 | def emgrep(pat, *files): |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 18 | return ggrep(RE_SYNTAX_EMACS, pat, files) |
| 19 | |
| 20 | def ggrep(syntax, pat, files): |
| 21 | if len(files) == 1 and type(files[0]) == type([]): |
| 22 | files = files[0] |
| 23 | global opt_show_filename |
| 24 | opt_show_filename = (len(files) != 1) |
Guido van Rossum | 5fca8a1 | 1991-12-31 00:00:35 +0000 | [diff] [blame] | 25 | syntax = regex.set_syntax(syntax) |
| 26 | try: |
| 27 | prog = regex.compile(pat) |
| 28 | finally: |
| 29 | syntax = regex.set_syntax(syntax) |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 30 | for filename in files: |
| 31 | fp = open(filename, 'r') |
| 32 | lineno = 0 |
| 33 | while 1: |
| 34 | line = fp.readline() |
| 35 | if not line: break |
| 36 | lineno = lineno + 1 |
| 37 | if prog.search(line) >= 0: |
| 38 | showline(filename, lineno, line, prog) |
| 39 | fp.close() |
| 40 | |
Guido van Rossum | 8566e47 | 1997-10-22 20:44:58 +0000 | [diff] [blame] | 41 | def pgrep(pat, *files): |
| 42 | if len(files) == 1 and type(files[0]) == type([]): |
| 43 | files = files[0] |
| 44 | global opt_show_filename |
| 45 | opt_show_filename = (len(files) != 1) |
| 46 | import re |
| 47 | prog = re.compile(pat) |
| 48 | for filename in files: |
| 49 | fp = open(filename, 'r') |
| 50 | lineno = 0 |
| 51 | while 1: |
| 52 | line = fp.readline() |
| 53 | if not line: break |
| 54 | lineno = lineno + 1 |
| 55 | if prog.search(line): |
| 56 | showline(filename, lineno, line, prog) |
| 57 | fp.close() |
| 58 | |
Guido van Rossum | e9cde31 | 1992-01-12 23:33:52 +0000 | [diff] [blame] | 59 | def showline(filename, lineno, line, prog): |
| 60 | if line[-1:] == '\n': line = line[:-1] |
| 61 | if opt_show_lineno: |
| 62 | prefix = string.rjust(`lineno`, 3) + ': ' |
| 63 | else: |
| 64 | prefix = '' |
| 65 | if opt_show_filename: |
| 66 | prefix = filename + ': ' + prefix |
| 67 | print prefix + line |
| 68 | if opt_show_where: |
| 69 | start, end = prog.regs()[0] |
| 70 | line = line[:start] |
| 71 | if '\t' not in line: |
| 72 | prefix = ' ' * (len(prefix) + start) |
| 73 | else: |
| 74 | prefix = ' ' * len(prefix) |
| 75 | for c in line: |
| 76 | if c <> '\t': c = ' ' |
| 77 | prefix = prefix + c |
| 78 | if start == end: prefix = prefix + '\\' |
| 79 | else: prefix = prefix + '^'*(end-start) |
| 80 | print prefix |