From 841dd96edf7b7c2a040138b15dc01b864dfff138 Mon Sep 17 00:00:00 2001 From: Ari Johnson Date: Fri, 2 Mar 2007 02:06:57 +0000 Subject: [PATCH] wildgrep() and wildgrepi() --- game/txt/hlp/cobra_func.hlp | 20 ++++++++-------- src/function.c | 2 ++ src/fundb.c | 8 +++++-- src/predicat.c | 48 ++++++++++++++++++++++++++----------- 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/game/txt/hlp/cobra_func.hlp b/game/txt/hlp/cobra_func.hlp index 457a177..a759074 100644 --- a/game/txt/hlp/cobra_func.hlp +++ b/game/txt/hlp/cobra_func.hlp @@ -1483,23 +1483,23 @@ Continued in HELP FOREACH2 See also: match(), matchall(), grab(), regmatch() & GREP() & REGREP() - grep(,,) + grep(,,) + wildgrep(,,) regrep(,,) - grepi(,,) + grepi(,,) regrepi(,,) + wildgrepi(,,) These functions return a list of attributes on containing - (or matching ). is a wildcard pattern for - attribute names to search. - - The list returned is similar to that returned by @grep/list - /= + , matching the wildcard , or matching the regular + expression . is a wildcard pattern for attribute + names to search. Parsing _does_ occur before this function is invoked. Therefore, - "special" characters will need to be escaped out. In grep(), - is NOT wildcard matched. + "special" characters will need to be escaped out. - grep()/regrep() are case-sensitive. grepi()/regrepi() are case-insensitive. + grep()/wildgrep()/regrep() are case-sensitive. + grepi()/wildgrepi()/regrepi() are case-insensitive. & GT() gt(,) diff --git a/src/function.c b/src/function.c index a355d48..b231a95 100644 --- a/src/function.c +++ b/src/function.c @@ -672,6 +672,8 @@ FUNTAB flist[] = { {"WAIT", fun_wait, 2, 2, FN_NOPARSE}, {"WHERE", fun_where, 1, 1, FN_REG}, {"WIDTH", fun_width, 1, 1, FN_REG}, + {"WILDGREP", fun_grep, 3, 3, FN_REG}, + {"WILDGREPI", fun_grep, 3, 3, FN_REG}, {"WIPE", fun_wipe, 1, 1, FN_REG}, {"WORDPOS", fun_wordpos, 2, 3, FN_REG}, {"WORDS", fun_words, 1, 2, FN_REG}, diff --git a/src/fundb.c b/src/fundb.c index e83efdc..2f2a174 100644 --- a/src/fundb.c +++ b/src/fundb.c @@ -1934,6 +1934,8 @@ FUNCTION(fun_isdbref) FUNCTION(fun_grep) { char *tp; + int wild; + int sensitive; dbref it = match_thing(executor, args[0]); if (!GoodObject(it)) { @@ -1949,8 +1951,10 @@ FUNCTION(fun_grep) safe_str(T("#-1 INVALID GREP PATTERN"), buff, bp); return; } - tp = grep_util(executor, it, args[1], args[2], arglens[2], - strcmp(called_as, "GREP")); + + sensitive = !strcmp(called_as, "GREP") || !strcmp(called_as, "WILDGREP"); + wild = !strcmp(called_as, "WILDGREPI") || !strcmp(called_as, "WILDGREP"); + tp = grep_util(executor, it, args[1], args[2], sensitive, wild); add_check("fun_grep.attr_list"); safe_str(tp, buff, bp); mush_free((Malloc_t) tp, "fun_grep.attr_list"); diff --git a/src/predicat.c b/src/predicat.c index 08f748a..66dec55 100644 --- a/src/predicat.c +++ b/src/predicat.c @@ -1287,9 +1287,8 @@ do_verb(dbref player, dbref cause, char *arg1, char **argv) struct guh_args { char *buff; /**< Buffer for output */ char *bp; /**< Pointer to buff's current position */ - char *lookfor; /**< String to grep for */ - int len; /**< Length of lookfor */ - int insensitive; /**< If 1, case-insensitive match; if 0, sensitive */ + char *lookfor; /**< Pattern to grep for */ + int sensitive; /**< If 1, case-sensitive match; if 0, insensitive */ }; static int @@ -1300,14 +1299,16 @@ grep_util_helper(dbref player __attribute__ ((__unused__)), __attribute__ ((__unused__)), ATTR *atr, void *args) { struct guh_args *guh = args; - int found; + int found = 0; char *s; + int len; s = (char *) atr_value(atr); /* warning: static */ + len = strlen(guh->lookfor); found = 0; while (*s && !found) { - if ((!guh->insensitive && !strncmp(guh->lookfor, s, guh->len)) || - (guh->insensitive && !strncasecmp(guh->lookfor, s, guh->len))) + if ((guh->sensitive && !strncmp(guh->lookfor, s, len)) || + (!guh->sensitive && !strncasecmp(guh->lookfor, s, len))) found = 1; else s++; @@ -1320,29 +1321,48 @@ grep_util_helper(dbref player __attribute__ ((__unused__)), return found; } +static int +wildgrep_util_helper(dbref player __attribute__ ((__unused__)), + dbref thing __attribute__ ((__unused__)), + dbref parent __attribute__ ((__unused__)), + char const *pattern + __attribute__ ((__unused__)), ATTR *atr, void *args) +{ + struct guh_args *guh = args; + int found = 0; + + if (quick_wild_new(guh->lookfor, atr_value(atr), guh->sensitive)) { + if (guh->bp != guh->buff) + safe_chr(' ', guh->buff, &guh->bp); + safe_str(AL_NAME(atr), guh->buff, &guh->bp); + found = 1; + } + return found; +} + /** Utility function for grep funtions/commands. * This function returns a list of attributes on an object that * match a name pattern and contain another string. * \param player the enactor. * \param thing object to check attributes on. - * \param pattern wildcard pattern for attributes to check. + * \param pattern wildcard or substring pattern for attributes to check. * \param lookfor string to find within each attribute. - * \param len length of lookfor. - * \param insensitive if 1, case-insensitive matching; if 0, case-sensitive. + * \param sensitive if 1, case-sensitive matching; if 0, case-insensitive. + * \param wild if 1, wildcard matching, if 0, substring * \return string containing list of attribute names with matching data. */ char * grep_util(dbref player, dbref thing, char *pattern, char *lookfor, - int len, int insensitive) + int sensitive, int wild) { struct guh_args guh; guh.buff = (char *) mush_malloc(BUFFER_LEN + 1, "grep_util.buff"); guh.bp = guh.buff; guh.lookfor = lookfor; - guh.len = len; - guh.insensitive = insensitive; - (void) atr_iter_get(player, thing, pattern, 0, grep_util_helper, &guh); + guh.sensitive = sensitive; + (void) atr_iter_get(player, thing, pattern, 0, wild ? wildgrep_util_helper + : grep_util_helper, &guh); *guh.bp = '\0'; return guh.buff; } @@ -1442,7 +1462,7 @@ do_grep(dbref player, char *obj, char *lookfor, int flag, int insensitive) if (!atr_iter_get(player, thing, pattern, 0, grep_helper, &gh)) notify(player, T("No matching attributes.")); } else { - tp = grep_util(player, thing, pattern, lookfor, len, insensitive); + tp = grep_util(player, thing, pattern, lookfor, !insensitive, 0); notify_format(player, T("Matches of '%s' on %s(#%d): %s"), lookfor, Name(thing), thing, tp); mush_free((Malloc_t) tp, "grep_util.buff"); -- 2.30.2