Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: exclude users in watch variable?



On Wed, 4 Feb 2015 18:46:06 +0100
Andy Spiegl <zsh.Andy@xxxxxxxxx> wrote:
> "watch=( notme )" excludes myself but is there a way to exclude other
> usernames?
> 
> I was trying "watch=( notme ^user1 ^user2 )" but I am expecting too
> much, right? 

Patterns are easy to add, we might as well do it.  I can't see how this
could be problematic since user names tty names and host names can't
contain pattern characters.

Note you'll need to do it the way suggested in the example: the list
you've got won't work because you're looking for an "or" of 'anyone apart
from me', 'anyone apart from user1', 'anyone apart from user2'.  In
fact, you can basically only use one negative in the list since "or"ing
negatives isn't useful.  I'm not proposing to change this since there
isn't a convenient alternative behaviour.

pws

diff --git a/Doc/Zsh/params.yo b/Doc/Zsh/params.yo
index ee7c054..273be21 100644
--- a/Doc/Zsh/params.yo
+++ b/Doc/Zsh/params.yo
@@ -1472,15 +1472,27 @@ vindex(watch)
 vindex(WATCH)
 item(tt(watch) <S> <Z> (tt(WATCH) <S>))(
 An array (colon-separated list) of login/logout events to report.
+
 If it contains the single word `tt(all)', then all login/logout events
 are reported.  If it contains the single word `tt(notme)', then all
 events are reported as with `tt(all)' except tt($USERNAME).
+
 An entry in this list may consist of a username,
 an `tt(@)' followed by a remote hostname,
-and a `tt(%)' followed by a line (tty).
+and a `tt(%)' followed by a line (tty).  Any of these may
+be a pattern (be sure to quote this during the assignment to
+tt(watch) so that it does not immediately perform file generation);
+the setting of the tt(EXTENDED_GLOB) option is respected.
 Any or all of these components may be present in an entry;
 if a login/logout event matches all of them,
 it is reported.
+
+For example, with the tt(EXTENDED_GLOB) option set, the following:
+
+example(watch=('^(pws|barts)'))
+
+causes reports for activity assoicated with any user other than tt(pws)
+or tt(barts).
 )
 vindex(WATCHFMT)
 item(tt(WATCHFMT))(
diff --git a/Src/watch.c b/Src/watch.c
index 8dea0b4..fe409f9 100644
--- a/Src/watch.c
+++ b/Src/watch.c
@@ -372,6 +372,27 @@ watchlog2(int inout, WATCH_STRUCT_UTMP *u, char *fmt, int prnt, int fini)
     return fmt;
 }
 
+/* See if the watch entry matches */
+
+static int
+watchlog_match(char *teststr, char *actual, int len)
+{
+    int ret = 0;
+    Patprog pprog;
+    char *str = dupstring(teststr);
+
+    tokenize(str);
+
+    if ((pprog = patcompile(str, PAT_STATIC, 0))) {
+	queue_signals();
+	if (pattry(pprog, actual))
+	    ret = 1;
+	unqueue_signals();
+    } else if (!strncmp(actual, teststr, len))
+	ret = 1;
+    return ret;
+}
+
 /* check the List for login/logouts */
 
 /**/
@@ -400,7 +421,7 @@ watchlog(int inout, WATCH_STRUCT_UTMP *u, char **w, char *fmt)
 	    for (vv = v; *vv && *vv != '@' && *vv != '%'; vv++);
 	    sav = *vv;
 	    *vv = '\0';
-	    if (strncmp(u->ut_name, v, sizeof(u->ut_name)))
+	    if (!watchlog_match(v, u->ut_name, sizeof(u->ut_name)))
 		bad = 1;
 	    *vv = sav;
 	    v = vv;
@@ -410,7 +431,7 @@ watchlog(int inout, WATCH_STRUCT_UTMP *u, char **w, char *fmt)
 		for (vv = ++v; *vv && *vv != '@'; vv++);
 		sav = *vv;
 		*vv = '\0';
-		if (strncmp(u->ut_line, v, sizeof(u->ut_line)))
+		if (!watchlog_match(v, u->ut_line, sizeof(u->ut_line)))
 		    bad = 1;
 		*vv = sav;
 		v = vv;
@@ -420,7 +441,7 @@ watchlog(int inout, WATCH_STRUCT_UTMP *u, char **w, char *fmt)
 		for (vv = ++v; *vv && *vv != '%'; vv++);
 		sav = *vv;
 		*vv = '\0';
-		if (strncmp(u->ut_host, v, strlen(v)))
+		if (!watchlog_match(v, u->ut_host, strlen(v)))
 		    bad = 1;
 		*vv = sav;
 		v = vv;




Messages sorted by: Reverse Date, Date, Thread, Author