CSCI 415/515: Fall 2024
Systems Programming
Project 7: rgrep

Due: Fri, Dec 13 @11:59pm


In this project, you will implement a simple version of grep, in Rust, which we will call rgrep.

This project is for the graduate students. For undergraduate students, it is optional, and counts as extra-credit for your project score.

Name

rgrep - print lines of a file that match a regular expression

Synopsis

rgrep [-B NUM] [-c] [-h] [-i] [-n] [-q] [-v] PATTERN FILE

Options

-B NUM, --before-context NUM

Print NUM lines of leading context before matching lines.

-c, --count

Suppress normal output; instead print a count of matching lines for the input file. With the -v option, count non-matching lines.

-h, --help

Print a usage statement to stdout and exit with status 0.

-i, --ignore-case

Perform a case-insensitive match for STR.

-n, --line-number

Prefix each line of output with the 1-based line number of the file, followed immediately by a colon (e.g., 1:foo).

-q, --quiet

Quiet; do not write anything to stdout. Exit immediately with zero status if any match was found. If a match is not found, exit with a non-zero status.

-v, --invert-match

Invert the sense of matching, to print non-matching lines.

Exit Status

The exit status is 0 if at least one line was matched, and non-zero (conventionally 1 or 2) if a line was not found or an error occurred.

Submitting

Submit your project as a zip file via gradescope. You should use cargo and zip the cargo project:


        cd rgrep
        ls
        (out) Cargo.lock Cargo.toml src
        zip -r submission.zip *

There is no need for Makefile; the automated grader will invoke cargo build.

Rubric

Input Files: alice.txt dorothy.txt

Helper Source Files: You can download a zip file with the starter cargo project. The project lists the following dependencies in Cargo.toml, which you may find helpful:


anyhow = "1.0.79"
clap = { version = "4.5.0", features = ["derive"] }
regex = "1.10.3"

-c, --count option


1.1 No matches (3 pts)


          ./rgrep -c pie alice.txt
          (out) 0
          

Has the above output (stdout).

1.2 No matches exit status (1 pts)


          ./rgrep -c pie alice.txt
          

Exits with nonzero status.

1.3 Match (3 pts)


          ./rgrep -c cake alice.txt
          (out) 3
          

Has the above output (stdout).

1.4 Match exit status (1 pts)


          ./rgrep -c cake alice.txt
          

Exits with status of 0.

-q, --quiet option


2.1 No matches, no output (3 pts)


            ./rgrep -q rainbox alice.txt
        

Prints nothing.

2.2 No matches exit status (1 pts)


          ./rgrep -q rainbox alice.txt
          

Exits with nonzero status.

2.3 Match, no output (3 pts)


          ./rgrep -q golden alice.txt
          

Prints nothing.

2.4 Match exit status (1 pts)


          ./rgrep -q golden alice.txt
          

Exits with status of 0.

-n, --line-number option


3.1 Match (3 pts)

output

          ./rgrep -n key alice.txt
          (out) 107:glass; there was nothing on it except a tiny golden key, and Alice's
          (out) 109:but, alas! either the locks were too large, or the key was too small,
          (out) 113:little golden key in the lock, and to her great delight it fitted!
          (out) 128:back to the table, half hoping she might find another key on it, or at
          (out) 149:flavour of cherry-tart, custard, pine-apple, roast turkey, toffee, and
          (out) 167:door, she found she had forgotten the little golden key, and when she
          (out) 188:Alice, "and if it makes me grow larger, I can reach the key; and if it
          

Has the above output (stdout).

-B, --before-context option


4.1 -B1 (3 pts)

output

          ./rgrep -B1 garden alice.txt
          (out) much larger than a rat-hole: she knelt down and looked along the
          (out) passage into the loveliest garden you ever saw. How she longed to get
          (out) brightened up at the thought that she was now the right size for going
          (out) through the little door into that lovely garden. First, however, she
          (out) After a while, finding that nothing more happened, she decided on going
          (out) into the garden at once; but, alas for poor Alice! when she got to the
          (out) makes me grow smaller, I can creep under the door; so either way I'll
          (out) get into the garden, and I don't care which happens!"
          

Has the above output (stdout).

4.2 -B3 (3 pts)

output

          ./rgrep -B3 cupboards alice.txt
          (out) was going to happen next. First, she tried to look down and make out
          (out) what she was coming to, but it was too dark to see anything; then she
          (out) looked at the sides of the well, and noticed that they were filled with
          (out) cupboards and book-shelves; here and there she saw maps and pictures
          (out) passed; it was labelled "ORANGE MARMALADE", but to her great
          (out) disappointment it was empty: she did not like to drop the jar for fear
          (out) of killing somebody underneath, so managed to put it into one of the
          (out) cupboards as she fell past it.
          

Has the above output (stdout).

Multiple options


5.1 -n -B2 (3 pts)


          ./rgrep -n -B2 mouse alice.txt
          (out) 78:tea-time. Dinah my dear! I wish you were down here with me! There are
          (out) 79:no mice in the air, I'm afraid, but you might catch a bat, and that's
          (out) 80:very like a mouse, you know. But do cats eat bats, I wonder?" And here
          

Has the above output (stdout).

5.2 -c -B2 (3 pts)


           ./rgrep -c -B2 mouse alice.txt
           (out) 1
          

Has the above output (stdout).

-i, --ignore-case option


6.1 Ignore case (5 pts)

output

          ./rgrep -i rabbit alice.txt
          (out) Down the Rabbit-Hole
          (out) picking the daisies, when suddenly a White Rabbit with pink eyes ran
          (out) so _very_ much out of the way to hear the Rabbit say to itself, "Oh
          (out) time it all seemed quite natural); but when the Rabbit actually _took a
          (out) had never before seen a rabbit with either a waistcoat-pocket, or a
          (out) large rabbit-hole under the hedge.
          (out) The rabbit-hole went straight on like a tunnel for some way, and then
          (out) long passage, and the White Rabbit was still in sight, hurrying down
          (out) turned the corner, but the Rabbit was no longer to be seen: she found
          

Has the above output (stdout).

-v, --invert-match option


7.1 Invert match (10 pts)

output

          ./rgrep -v the dorothy.txt
          by wagon many miles. There were four walls, a floor and a roof,
          which made one room; and this room contained a rusty looking cooking
          whirlwinds arose, mighty enough to crush any building in its path. It 
           
          

Has the above output (stdout).

Regexs


8.1 \w{10} (3 pts)

output

          ./rgrep '\w{10}' dorothy.txt
          whirlwinds arose, mighty enough to crush any building in its path. It
          the sky in all directions. The sun had baked the plowed land into a
          they were the same gray color to be seen everywhere. Once the house had
          away, and now the house was as dull and gray as everything else.
          

Has the above output (stdout).

8.2 ^[A-Z] (3 pts)

output

          ./rgrep '^[A-Z]' dorothy.txt
          Dorothy lived in the midst of the great Kansas prairies, with Uncle
          Henry, who was a farmer, and Aunt Em, who was the farmer's wife.
          Their house was small, for the lumber to build it had to be carried
          When Dorothy stood in the doorway and looked around, she could see
          

Has the above output (stdout).

8.2 \?[^ ] (3 pts)

output

          ./rgrep '\?[^ ]' alice.txt
          "without pictures or conversations?"
          many miles I've fallen by this time?" she said aloud. "I must be
          then I wonder what Latitude or Longitude I've got to?" (Alice had no
          country is, you know. Please, Ma'am, is this New Zealand or Australia?"
          falling through the air! Do you think you could manage it?) "And what
          very like a mouse, you know. But do cats eat bats, I wonder?" And here
          dreamy sort of way, "Do cats eat bats? Do cats eat bats?" and
          sometimes, "Do bats eat cats?" for, you see, as she couldn't answer
          "Now, Dinah, tell me the truth: did you ever eat a bat?" when suddenly,
          wonder what I should be like then?" And she tried to fancy what the
          way?", holding her hand on the top of her head to feel which way it was
          

Has the above output (stdout).