From NetHackWiki
Jump to navigation Jump to search

grep is a Unix command-line tool to search in files using regular expression.

In NetHack, most of the useful source is in src/*.c and include/*.h, thus you can grep the sources like this:

$ cd /path/to/nethack-3.4.3
$ grep -n regular-expression */*.{c,h}

Here is an example of searching for "Drow" in an unbuilt copy of the SLASH'EM sources:

$ cd /path/to/slashem-0.0.7E7F2
$ grep -n Drow */*.{c,h}
src/u_init.c:1343:          /* Drows can recognize all droven objects */
src/uhitm.c:40:/* Used to control whether Drow's sleep attack should succeed. */
include/you.h:356:      /* KMH -- Drow is now its own role... */

It is sometimes useful to use grep to search for in-game messages. Want to know what a "wrenching sensation" is? Try this...

$ cd /path/to/nethack-3.4.3
$ grep -n 'wrenching sensation' */*.{c,h}
src/sit.c:248:                      You_feel("a wrenching sensation.");
src/spell.c:116:                You_feel("a wrenching sensation.");
src/teleport.c:847:             You_feel("a wrenching sensation.");
src/teleport.c:869:         You_feel("a wrenching sensation.");

It can sometimes be difficult to find something successfully with grep. For example, a grep on 'You feel a wrenching sensation' fails because NetHack uses the You_feel function for those messages.

Sometimes, the use of printf-style formatting also impedes your search. For example, suppose that you get the message "You feel that eating the little dog was a bad idea.". You grep for it but get no results:

$ grep -n 'eating the little dog' */*.{c,h}

You suspect that the monster is variable. You try again, searching for "bad idea" somewhere in the same line after "eating":

$ grep -n 'eating.*bad idea' */*.{c,h}
src/eat.c:486:              You_feel("that eating the %s was a bad idea.", mons[pm].mname);

You may also want to check in all files (including top-level ones:

$ grep 'HACKDIR' $(find .)

A look around eat.c#line486 (in vanilla 3.4.3) shows the following:

        switch(pm) {
            case PM_LITTLE_DOG:
            case PM_DOG:
            case PM_LARGE_DOG:
            case PM_KITTEN:
            case PM_HOUSECAT:
            case PM_LARGE_CAT:
                if (!CANNIBAL_ALLOWED()) {
                    You_feel("that eating the %s was a bad idea.", mons[pm].mname);
                    HAggravate_monster |= FROMOUTSIDE;

From this, you guess that message means that you now have the intrinsic of aggravate monster, and that eating a little dog, dog, large dog, kitten, housecat, or large cat causes it.

If you need to find in what file a certain function is defined, you can grep for the function name, prefixed with a caret, for example:

$ grep ^delobj *.c