/usr/share/acl2-6.5/books/regex/grep-command-line.lisp is in acl2-books-source 6.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 | (in-package "ACL2")
;; (local (include-book "defsum-thms"))
;; (include-book "defsum")
(include-book "tools/defsum" :dir :system)
(include-book "regex-fileio")
(include-book "regex-chartrans")
(program)
(set-state-ok t)
(defsum grep-output-mode
(grep-output-count) ;; -c --count
(grep-output-match) ;; -o --only-matching
(grep-output-line)
(grep-output-filename) ;; -l --files-with-matches
(grep-output-non-matching-filename) ;; -L --files-without-match
(grep-output-silent) ;; -q --quiet --silent
)
(defsum grep-color-mode ;; --color --colour
(grep-color-never)
(grep-color-always)
(grep-color-auto))
(defsum grep-pattern-input
(grep-pattern-string (stringp pattern)) ;; -e --regexp
(grep-pattern-file (stringp filename)) ;; -f --file
(grep-pattern-none-yet))
(defsum grep-translation
(grep-translation-none)
(grep-translation-ignore-case) ;; -i --ignore-case -y
)
(defsum grep-overall-mode
(grep-mode-help) ;; --help
(grep-mode-version) ;; -V --version
(grep-mode-match))
(defsum grep-binary-files-mode ;; --binary-files
(grep-binary-binary)
(grep-binary-text) ;; -a --text
(grep-binary-without-match) ;; -I
)
(defsum grep-directories-mode ;; -d -directories
(grep-directories-read)
(grep-directories-skip)
(grep-directories-recurse) ;; -r -R --recursive
)
(defsum grep-match-mode
(grep-match-default)
(grep-match-line) ;; -x --line-regexp
(grep-match-word) ;; -w --word-regexp
)
(defsum grep-filename-mode
(grep-filename-default) ;; print filenames if there are multiple files
(grep-filename-print-filename)
(grep-filename-no-filename))
(defsum grep-filename-filter
(grep-filter-all)
(grep-filter-exclude (stringp filter)) ;; --exclude
(grep-filter-include (stringp filter)) ;; --include
)
(defsum grep-regexp-mode
(grep-regexp-basic) ;; -G --basic-regexp
(grep-regexp-extended) ;; -E --extended-regexp
(grep-regexp-fixed) ;; -F --fixed-strings
(grep-regexp-perl) ;; -P --perl-regexp
)
(defsum grep-c-l
:updatable t
:compact nil ;; :compact t is not compatible with updatable
(grep-command-line
(grep-output-mode-p outmode)
(grep-pattern-input-p pattern)
(booleanp ignore-case)
(booleanp print-errors) ;; -s --no-messages
(booleanp invert-match) ;; -v --invert-match
(booleanp print-line-nums) ;; -n --line-number
(grep-match-mode-p match-mode)
(integerp before-context) ;; -B --before-context -C --context
(integerp after-context) ;; -A --after-context -C --context
(booleanp context-print-multiple) ;; -C vs -NUM
(grep-color-mode-p color)
(grep-overall-mode-p mode)
(grep-binary-files-mode-p binary-mode)
(booleanp print-byte-offset)
(booleanp read-devices) ;; -D --devices
(grep-directories-mode-p directory-mode)
(grep-filename-mode-p print-filenames) ;; -H --with-filename -h --no-filename
(booleanp line-buffered) ;; --line-buffered
(stringp label) ;; --label
(grep-filename-filter-p filter)
(integerp max-count) ;; -m --max-count
(booleanp msdos-binary) ;; -U --binary
(booleanp unix-byte-offsets) ;; -u --unix-byte-offsets
(booleanp mmap) ;; --mmap
(booleanp filename-null) ;; -Z --null
(booleanp line-null) ;; -z --null-data
(grep-regexp-mode-p regexp-mode)
))
(defconst *grep-command-line-defaults*
(grep-command-line
(grep-output-line)
(grep-pattern-none-yet)
nil ;; case-sensitive
t ;; print errors
nil ;;don't invert match
nil ;; don't print line numbers
(grep-match-default)
0 ;; before context
0 ;; after context
nil ;; don't print context lines more than once
(grep-color-never)
(grep-mode-match)
(grep-binary-binary)
nil ;; don't print byte offset
t ;; read devices as if ordinary files
(grep-directories-read) ;; as if they were ordinary files
(grep-filename-default)
nil ;; no line buffering policy (??)
"" ;; empty label for stdin
(grep-filter-all) ;; include all filenames
-1 ;; no max count
nil ;; not msdos binary mode
nil ;; unix byte offset style
nil ;; no mmap (...)
nil ;; no null byte after filename
nil ;; input lines not null-terminated
(grep-regexp-basic)))
(defun grep-option-parse (tokens cmdline)
(if (atom tokens)
(mv cmdline tokens)
(pattern-match
(car tokens)
((any "--count" "-c")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-outmode
(grep-output-count) cmdline)))
((any "ignore-case" "-i")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-ignore-case
t cmdline)))
((any "--files-with-matches" "-l")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-outmode
(grep-output-filename) cmdline)))
((any "--line-number" "-n")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-print-line-nums
t cmdline)))
((any "--only-matching" "-o")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-outmode
(grep-output-match) cmdline)))
((any "--quiet" "--silent" "-q")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-outmode
(grep-output-match) cmdline)))
((any "--no-messages" "-s")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-print-errors
nil cmdline)))
((any "--invert-match" "-v")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-invert-match
t cmdline)))
((any "--line-regexp" "-x")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-match-mode
(grep-match-line) cmdline)))
((any "--version" "-V")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-mode
(grep-mode-version) cmdline)))
((any "--help")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-mode
(grep-mode-help) cmdline)))
((any "--byte-offset" "-b")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-print-byte-offset
t cmdline)))
((any "--with-filename" "-H")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-print-filenames
(grep-filename-print-filename) cmdline)))
((any "--no-filename" "-h")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-print-filenames
(grep-filename-no-filename) cmdline)))
((any "--line-buffered")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-line-buffered
t cmdline)))
((any "--files-without-match" "-L")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-outmode
(grep-output-non-matching-filename) cmdline)))
((any "--text" "-a")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-binary-mode
(grep-binary-text) cmdline)))
((any "--word-regexp" "-w")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-match-mode
(grep-match-word) cmdline)))
((any "--recursive" "-r" "-R")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-directory-mode
(grep-directories-recurse) cmdline)))
((any "--binary" "-U")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-msdos-binary
t cmdline)))
((any "--unix-byte-offsets" "-u")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-unix-byte-offsets
t cmdline)))
((any "--mmap")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-mmap
t cmdline)))
((any "--null" "-Z")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-filename-null
t cmdline)))
((any "--null-data" "-z")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-line-null
t cmdline)))
((any "--basic-regexp" "-G")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-regexp-mode
(grep-regexp-basic) cmdline)))
((any "--extended-regexp" "-E")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-regexp-mode
(grep-regexp-extended) cmdline)))
((any "--fixed-strings" "-F")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-regexp-mode
(grep-regexp-fixed) cmdline)))
((any "--perl-regexp" "-P")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-regexp-mode
(grep-regexp-perl) cmdline)))
((any "--regexp=" "-e")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-pattern
(grep-pattern-string (cadr tokens)) cmdline)))
((any "--file=" "-f")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-pattern
(grep-pattern-file (cadr tokens)) cmdline)))
((any "--after-context=" "-A")
(mv-let
(num rest pow)
(parse-int (cadr tokens) 0)
(declare (ignore rest pow))
(if num
(grep-option-parse
(cddr tokens)
(update-grep-command-line-after-context
num cmdline))
(mv (er hard? 'top-level "bad after-context")
nil))))
((any "--before-context=" "-B")
(mv-let
(num rest pow)
(parse-int (cadr tokens) 0)
(declare (ignore rest pow))
(if num
(grep-option-parse
(cddr tokens)
(update-grep-command-line-before-context
num cmdline))
(mv (er hard? 'top-level "bad after-context")
nil))))
((any "--context=" "-C")
(mv-let
(num rest pow)
(parse-int (cadr tokens) 0)
(declare (ignore rest pow))
(if num
(grep-option-parse
(cddr tokens)
(update-grep-command-line-before-context
num (update-grep-command-line-after-context
cmdline num)))
(mv (er hard? 'top-level "bad after-context")
nil))))
((any "--color" "--colour")
(grep-option-parse
(cdr tokens)
(update-grep-command-line-color
(grep-color-always) cmdline)))
((any "--color=" "--colour=")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-color
(pattern-match (cadr tokens)
("never" (grep-color-never))
("always" (grep-color-always))
("auto" (grep-color-auto))
(&
(er hard? 'top-level "bad color specification"))) cmdline)))
((any "--binary-files=")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-binary-mode
(pattern-match (cadr tokens)
("binary" (grep-binary-binary))
("text" (grep-binary-text))
("without-match" (grep-binary-without-match))
(&
(er hard? 'top-level "bad binary-files option"))) cmdline)))
((any "--devices=" "-D")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-read-devices
(pattern-match (cadr tokens)
("read" t)
("skip" nil)
(&
(er hard? 'top-level "bad devices option"))) cmdline)))
((any "--directories=" "-d")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-directory-mode
(pattern-match (cadr tokens)
("read" (grep-directories-read))
("skip" (grep-directories-skip))
("recurse" (grep-directories-recurse))
(&
(er hard? 'top-level "bad directories option"))) cmdline)))
((any "--label=")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-label
(cadr tokens) cmdline)))
((any "--include=")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-filter
(grep-filter-include (cadr tokens)) cmdline)))
((any "--exclude=")
(grep-option-parse
(cddr tokens)
(update-grep-command-line-filter
(grep-filter-exclude (cadr tokens)) cmdline)))
((any "--max-count=" "-m")
(mv-let
(num rest pow)
(parse-int (cadr tokens) 0)
(declare (ignore rest pow))
(if num
(grep-option-parse
(cddr tokens)
(update-grep-command-line-max-count
num cmdline))
(mv (er hard? 'top-level "bad max count")
nil))))
((any "-" "--")
(if (atom (cdr tokens))
(mv
(cdr tokens) (update-grep-command-line-pattern
cmdline
(grep-pattern-string "-")))
(mv
(cddr tokens) (update-grep-command-line-pattern
cmdline
(grep-pattern-string (cadr tokens))))))
(&
(cond ((and (>= (length (car tokens)) 1)
(eql (char (car tokens) 0) #\-))
(mv-let
(num rest pow)
(parse-int (car tokens) 1)
(declare (ignore rest pow))
(if num
(grep-option-parse
(cdr tokens)
(update-grep-command-line-after-context
num (update-grep-command-line-before-context
cmdline
num)))
(mv (er hard? 'top-level "Unrecognized option")
nil))))
(t
(if (equal (grep-command-line-pattern cmdline)
(grep-pattern-none-yet))
(mv
(update-grep-command-line-pattern
(grep-pattern-string (car tokens)) cmdline)
(cdr tokens))
(mv cmdline tokens))))))))
(defun grep-display-help ()
(cw "Run man grep or info grep on unix to see command-line options.~%
Some options are not implemented.~%"))
(defun grep-display-version ()
(cw "Grep for ACL2~%"))
(defun pattern-parse-by-mode (string options)
(declare (xargs :guard (and (stringp string)
)))
(let* ((opts (parse-options
(pattern-match
(grep-command-line-regexp-mode options)
((grep-regexp-basic) 'bre)
((grep-regexp-extended) 'ere)
((grep-regexp-fixed) 'fixed)
((grep-regexp-perl)
(er hard? 'top-level "Perl regexps not yet implemented")))
t nil t
(grep-command-line-ignore-case options)))
(string (if (grep-command-line-ignore-case options)
(case-insens-trans string)
string))
(reg (regex-do-parse string opts)))
(if (regex-p reg)
reg
(er hard? 'top-level "Regexp parse error: ~p0" reg))))
(defun parse-pattern-file-channel (channel options state)
(declare (xargs :guard (and (symbolp channel)
(state-p state)
(grep-command-line-p options)
(open-input-channel-p channel :character
state))))
(mv-let (more line state)
(read-line$ channel nil state)
(cond ((and (equal line "")
(not more))
(mv (r-nomatch) state))
((not more)
(mv (pattern-parse-by-mode line options)
state))
(t (mv-let (patt state)
(parse-pattern-file-channel channel options state)
(mv (r-disjunct
(pattern-parse-by-mode line options)
patt)
state))))))
(defun parse-pattern-file (fname options state)
(declare (xargs :guard (and (stringp fname)
(grep-command-line-p options)
(state-p state))))
(mv-let (channel state) (open-input-channel fname :character state)
(if (and (symbolp channel)
(open-input-channel-p channel :character state))
(mv-let (regex state)
(parse-pattern-file-channel channel options state)
(let ((state (close-input-channel channel state)))
(mv regex state)))
(mv (er hard? 'top-level "Bad filename")
state))))
(defun get-pattern (options state)
(declare (xargs :guard (and (grep-command-line-p options)
(state-p state))))
(pattern-match
(grep-command-line-pattern options)
((grep-pattern-string str)
(mv (pattern-parse-by-mode str options) state))
((grep-pattern-file fname)
(parse-pattern-file fname options state))
(& (mv (er hard? 'top-level "No pattern") state))))
;; (defun grep-through-file (pattern options file linenum state)
;; (declare (xargs :guard (and (regex-p pattern)
;; (grep-command-line-p options)
;; (symbolp file)
;; (natp linenum)
;; (state-p state)
;; (open-input-channel-p file :character state))))
;; (mv-let (more line state)
;; (read-line$ file nil state)
;; (let ((trans-line (if (grep-command-line-ignore-case options)
;; (case-insens-trans line)
;; line)))
;; (mv-let (havematch matchstr brs)
;; (match-regex pattern trans-line line)
;; (declare (ignore brs))
;; (mv-let (matches state)
;; (if more
;; (grep-through-file pattern options file
;; (1+ linenum) state)
;; (mv nil state))
;; (if havematch
;; (mv (cons (cons linenum matchstr) matches)
;; state)
;; (mv matches state)))))))
;; (defun print-line-range
;; (start end current fname print-name print-num channel state)
;; (if (and (integerp end)
;; (> current end))
;; state
;; (mv-let (more line state)
;; (read-line$ channel nil state)
;; (if (< current start)
;; (if more
;; (print-line-range start end (1+ current) fname
;; print-name print-num channel state)
;; state)
;; (prog2$
;; (and print-name (cw "~s0:" fname))
;; (prog2$
;; (and print-num (cw "~x0:" current))
;; (prog2$
;; (cw "~s0~%" line)
;; (if more
;; (print-line-range start end (1+ current) fname
;; print-name print-num channel state)
;; state))))))))
;; (defun print-matching-lines-in-file
;; (fname nextline matches print-name options channel state)
;; (let ((print-num (grep-command-line-print-line-nums options))
;; (before-context (grep-command-line-before-context options))
;; (after-context (grep-command-line-after-context options)))
;; (if (atom matches)
;; state
;; (let ((state
;; (print-line-range
;; (max nextline (- (caar matches) before-context))
;; (+ (caar matches) after-context)
;; nextline
;; fname print-name print-num
;; channel state)))
;; (print-matching-lines-in-file
;; fname (+ 1 (caar matches) after-context) (cdr matches) print-name
;; options channel state)))))
;; (defun print-matches-in-file
;; (fname matches print-name options)
;; (if (atom matches)
;; nil
;; (prog2$
;; (and print-name (cw "~s0:" fname))
;; (prog2$
;; (and (grep-command-line-print-line-nums options)
;; (cw "~x0:" (caar matches)))
;; (prog2$
;; (cw "~s0~%" (cdar matches))
;; (print-matches-in-file fname (cdr matches) print-name options))))))
;; (defun print-non-matching-lines-in-file
;; (fname nextline matches print-name options channel state)
;; (let ((print-num (grep-command-line-print-line-nums options))
;; (before-context (grep-command-line-before-context options))
;; (after-context (grep-command-line-after-context options)))
;; (if (atom matches)
;; (print-line-range nextline nil nextline fname
;; print-name print-num channel state)
;; (if (atom (cdr matches))
;; (print-line-range (- (1+ (caar matches)) before-context) nil
;; nextline fname print-name print-num channel state)
;; (let ((state (print-line-range (- (1+ (caar matches)) before-context)
;; (+ (1- (caadr matches)) after-context)
;; nextline fname print-name print-num
;; channel state)))
;; (print-non-matching-lines-in-file
;; fname (+ (caadr matches) after-context) (cdr matches)
;; print-name options channel state))))))
;; (defun print-match-range (start end matches fname print-name print-num)
;; (if (or (atom matches)
;; (< end (caar matches)))
;; nil
;; (prog2$ (if (and (>= (caar matches) start)
;; (stringp (cdar matches)))
;; (prog2$
;; (and print-name (cw "~s0:" fname))
;; (prog2$ (and print-num (cw "~x0:" (caar matches)))
;; (cw "~s0~%" (cdar matches))))
;; nil)
;; (print-match-range start end (cdr matches) fname print-name print-num))))
;; (defun fill-out-matches (matches linenum channel state)
;; (mv-let (more line state)
;; (read-line$ channel nil state)
;; (declare (ignore line))
;; (if (and (consp matches)
;; (equal linenum (caar matches)))
;; (if more
;; (mv-let (rest state)
;; (fill-out-matches
;; (cdr matches) (1+ linenum) channel state)
;; (mv (cons (car matches) rest) state))
;; (mv (list (car matches)) state))
;; (if more
;; (mv-let (rest state)
;; (fill-out-matches
;; matches (1+ linenum) channel state)
;; (mv (cons (list linenum) rest) state))
;; (mv (list (list linenum)) state)))))
;; (defun print-non-matching-matches-in-file
;; (fname matches all-matches print-name options)
;; (let ((print-num (grep-command-line-print-line-nums options))
;; (before-context (grep-command-line-before-context options))
;; (after-context (grep-command-line-after-context options)))
;; (if (atom matches)
;; nil
;; (prog2$ (if (cdar matches)
;; nil
;; (print-match-range
;; (- (caar matches) before-context)
;; (+ (caar matches) after-context)
;; all-matches fname print-name print-num))
;; (print-non-matching-matches-in-file
;; fname (cdr matches) all-matches print-name options)))))
;; (defun print-matches-or-lines-in-file
;; (fname matches print-name options channel state)
;; (pattern-match-list
;; ((grep-command-line-outmode options)
;; (grep-command-line-invert-match options))
;; (((grep-output-line) nil)
;; (print-matching-lines-in-file
;; fname 0 matches print-name options channel state))
;; (((grep-output-line) t)
;; (print-non-matching-lines-in-file
;; fname 0 matches print-name options channel state))
;; (((grep-output-match) nil)
;; (prog2$ (print-matches-in-file
;; fname matches print-name options)
;; state))
;; (((grep-output-match) t)
;; (mv-let (full-matches state)
;; (fill-out-matches matches 0 channel state)
;; (prog2$ (print-non-matching-matches-in-file
;; fname full-matches matches print-name options)
;; state)))
;; (& (prog2$ (er hard? 'top-level "Bad outmode or invert-mode")
;; state))))
;; (defun print-matches (fname matches multi options state)
;; (let ((print-fname (pattern-match
;; (grep-command-line-print-filenames options)
;; ((grep-filename-default) multi)
;; ((grep-filename-print-filename) t)
;; (& nil))))
;; (pattern-match
;; (grep-command-line-outmode options)
;; ((grep-output-count)
;; (prog2$ (if print-fname
;; (cw "~s0:~y1" fname (len matches))
;; (cw "~y1" (len matches)))
;; state))
;; ((grep-output-filename)
;; (prog2$ (if matches
;; (cw "~s0~%" fname)
;; nil)
;; state))
;; ((grep-output-non-matching-filename)
;; (prog2$ (if matches
;; nil
;; (cw "~s0~%" fname))
;; state))
;; ((grep-output-silent)
;; state)
;; (& (mv-let (channel state)
;; (open-input-channel fname :character state)
;; (let ((state (print-matches-or-lines-in-file
;; fname matches print-fname options channel
;; state)))
;; (close-input-channel channel state)))))))
(defun print-line (fname print-name linenum print-num line sep)
(prog2$
(if print-name
(cw "~s0~s1" (if (equal fname "/dev/stdin")
"(standard input)"
fname) sep)
nil)
(prog2$
(if print-num
(cw "~x0~s1" linenum sep)
nil)
(cw "~s0~%" line))))
(defun print-lines (fname print-name print-num oldlines num)
(if (or (zp num) (atom oldlines))
nil
(prog2$
(print-lines fname print-name print-num (cdr oldlines) (1- num))
(print-line fname print-name (caar oldlines) print-num
(cdar oldlines) "-"))))
(defmacro print-for-match (line)
`(prog2$
(print-lines fname print-name print-num oldlines
before-context)
(prog2$
(print-line fname print-name linenum print-num
,line ":")
(if more
(grep-through-file pattern file fname print-name
(1+ linenum) nil after-context
(1+ count) options state)
(mv 0 state)))))
(defmacro print-in-range (line)
`(if (> nextn 0)
(prog2$
(print-line fname print-name linenum print-num
,line "-")
(if more
(grep-through-file pattern file fname print-name (1+ linenum)
nil (1- nextn) count options state)
(mv (if (> count 0) 0 1) state)))
(prog2$
(if (and (= nextn 0)
(or (> before-context 0)
(> after-context 0))
(not (equal fname "/dev/stdin")))
(cw "--~%")
nil)
(if more
(grep-through-file pattern file fname print-name (1+ linenum)
(cons (cons linenum ,line) oldlines)
-1 count options state)
(mv (if (> count 0) 0 1) state)))))
(defmacro count-through-file (count)
`(if more
(grep-through-file pattern file fname print-name
(1+ linenum) oldlines nextn ,count
options state)
(prog2$
(if print-name
(cw "~s0:" fname)
nil)
(prog2$ (cw "~y0~%" ,count)
(mv (if (> ,count 0) 0 1) state)))))
(defun grep-through-file (pattern file fname print-name linenum
oldlines nextn count options state)
(let ((invert (grep-command-line-invert-match options))
(before-context (grep-command-line-before-context options))
(after-context (grep-command-line-after-context options))
(print-num (grep-command-line-print-line-nums options))
(outmode (grep-command-line-outmode options)))
(mv-let (more line state)
(read-line$ file nil state)
(let ((trans-line (if (grep-command-line-ignore-case options)
(case-insens-trans line)
line)))
(mv-let
(havematch matchstr brs)
(match-regex pattern trans-line line)
(declare (ignore brs))
(let ((matched (equal havematch invert)))
(pattern-match-list
(outmode matched)
(((grep-output-line) nil)
(print-for-match line))
(((grep-output-line) t)
(print-in-range line))
(((grep-output-match) nil)
(print-for-match matchstr))
(((grep-output-match) t)
(print-in-range matchstr))
(((grep-output-filename) t)
(prog2$ (cw "~s0~%" fname)
(mv 0 state)))
(((grep-output-filename) nil)
(if more
(grep-through-file pattern file fname print-name
(1+ linenum) oldlines nextn count
options state)
(mv 1 state)))
(((grep-output-count) nil)
(count-through-file (1+ count)))
(((grep-output-count) t)
(count-through-file count))
(((grep-output-non-matching-filename) t)
(mv 0 state))
(((grep-output-non-matching-filename) nil)
(if more
(grep-through-file pattern file fname print-name
(1+ linenum) oldlines nextn count
options state)
(prog2$ (cw "~s0~%" fname)
(mv 1 state))))
(((grep-output-silent) t)
(mv 0 state))
(((grep-output-silent) nil)
(if more
(grep-through-file pattern file fname print-name
(1+ linenum) oldlines nextn count
options state)
(mv 1 state)))
(& (mv (er hard? 'top-level "Bad output mode in options")
state)))))))))
(defun grep-through-files (pattern options files multi state)
(declare (xargs :guard (and (regex-p pattern)
(grep-command-line-p options)
(string-listp files)
(booleanp multi)
(state-p state))))
(if (atom files)
(mv 1 state)
(let* ((file (car files)))
(mv-let (channel state)
(open-input-channel file :character state)
(if (and (symbolp channel)
(open-input-channel-p channel :character state))
(let ((print-fname (pattern-match
(grep-command-line-print-filenames options)
((grep-filename-default) multi)
((grep-filename-print-filename) t)
(& nil))))
(mv-let
(code state)
(grep-through-file pattern channel file
print-fname 0 nil -1 0
options state)
(mv-let
(code2 state)
(grep-through-files pattern options
(cdr files) multi state)
(mv (cond ((or (= code 2)
(= code2 2))
2)
((or (= code 0)
(= code2 0))
0)
(t 1))
state))))
(prog2$ (cw "File couldn't be opened: ~p0~%" file)
(mv 2 state)))))))
(defun find-first-occurrence-after (str char idx)
(declare (xargs :guard (and (stringp str)
(characterp char))))
(cond ((string-index-end idx str)
nil)
((equal (char str idx) char)
idx)
(t
(find-first-occurrence-after str char (1+ idx)))))
(defun grep-split-abbrev-args (arg idx)
(if (string-index-end idx arg)
nil
(mv-let (num rest power)
(parse-int arg idx)
(declare (ignore power))
(if num
(cons (concatenate 'string
"-"
(subseq arg idx rest))
(grep-split-abbrev-args arg rest))
(cons (concatenate 'string
"-"
(subseq arg idx (1+ idx)))
(grep-split-abbrev-args arg (1+ idx)))))))
(defun grep-split-args (args)
(if (atom args)
nil
(let ((arg (car args)))
(cond
((and (> (length arg) 2)
(equal (subseq arg 0 2) "--"))
(let ((equal (find-first-occurrence-after arg #\= 0)))
(if equal
(cons (subseq arg 0 (1+ equal))
(cons (subseq arg (1+ equal) nil)
(grep-split-args (cdr args))))
(cons arg (grep-split-args (cdr args))))))
((and (>= (length arg) 2)
(equal (char arg 0) #\-))
(append (grep-split-abbrev-args arg 1)
(grep-split-args (cdr args))))
(t (cons arg (grep-split-args (cdr args))))))))
(defun grep-exec (args state)
(mv-let
(options files)
(grep-option-parse
(grep-split-args args)
*grep-command-line-defaults*)
(pattern-match
(grep-command-line-mode options)
((grep-mode-help)
(prog2$ (grep-display-help) (mv 0 state)))
((grep-mode-version)
(prog2$ (grep-display-version) (mv 0 state)))
((grep-mode-match)
(if (grep-pattern-none-yet-p (grep-command-line-pattern options))
(prog2$ (grep-display-help) (mv 0 state))
(mv-let (pattern state)
(get-pattern options state)
(let ((files (if (consp files) files (list "/dev/stdin"))))
(grep-through-files pattern options files
(consp (cdr files)) state)))))
(& (prog2$ (er hard? 'top-level "bad top-level mode (should never occur)")
(mv 2 state))))))
|