r413 - in trunk/varnish-cache: include lib/libvarnishapi

phk at projects.linpro.no phk at projects.linpro.no
Mon Jul 10 22:49:07 CEST 2006


Author: phk
Date: 2006-07-10 22:49:07 +0200 (Mon, 10 Jul 2006)
New Revision: 413

Modified:
   trunk/varnish-cache/include/varnishapi.h
   trunk/varnish-cache/lib/libvarnishapi/shmlog.c
Log:
Implement -C, -I <regex> and -X <regex> generic options


Modified: trunk/varnish-cache/include/varnishapi.h
===================================================================
--- trunk/varnish-cache/include/varnishapi.h	2006-07-10 20:27:52 UTC (rev 412)
+++ trunk/varnish-cache/include/varnishapi.h	2006-07-10 20:49:07 UTC (rev 413)
@@ -8,7 +8,7 @@
 #define V_DEAD __attribute__ ((noreturn))
 
 /* shmlog.c */
-#define VSL_ARGS	"r:i:x:"
+#define VSL_ARGS	"r:i:x:CI:X:"
 struct VSL_data;
 struct VSL_data *VSL_New(void);
 int VSL_OpenLog(struct VSL_data *vd);

Modified: trunk/varnish-cache/lib/libvarnishapi/shmlog.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2006-07-10 20:27:52 UTC (rev 412)
+++ trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2006-07-10 20:49:07 UTC (rev 413)
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <regex.h>
 #include <sys/mman.h>
 
 #include "shmlog.h"
@@ -25,6 +26,10 @@
 
 	int			ix_opt;
 	unsigned char 		supr[256];
+
+	int			regflags;
+	regex_t			*regincl;
+	regex_t			*regexcl;
 };
 
 #ifndef MAP_HASSEMAPHORE
@@ -89,6 +94,7 @@
 	struct VSL_data *vd;
 
 	vd = calloc(sizeof *vd, 1);
+	vd->regflags = REG_EXTENDED | REG_NOSUB;
 	return (vd);
 }
 
@@ -109,29 +115,28 @@
 	return (0);
 }
 
-unsigned char *
-VSL_NextLog(struct VSL_data *vd)
+/*--------------------------------------------------------------------*/
+
+static unsigned char *
+vsl_nextlog(struct VSL_data *vd)
 {
 	unsigned char *p;
 	int i;
 
 	if (vd->fi != NULL) {
-		while (1) {
-			i = fread(vd->rbuf, 4, 1, vd->fi);
+		i = fread(vd->rbuf, 4, 1, vd->fi);
+		if (i != 1)
+			return (NULL);
+		if (vd->rbuf[1] > 0) {
+			i = fread(vd->rbuf + 4, vd->rbuf[1], 1, vd->fi);
 			if (i != 1)
 				return (NULL);
-			if (vd->rbuf[1] > 0) {
-				i = fread(vd->rbuf + 4, vd->rbuf[1], 1, vd->fi);
-				if (i != 1)
-					return (NULL);
-			}
-			if (!vd->supr[vd->rbuf[0]])
-				return (vd->rbuf);
 		}
+		return (vd->rbuf);
 	}
 
 	p = vd->ptr;
-	for (p = vd->ptr; ; p = vd->ptr) {
+	while (1) {
 		if (*p == SLT_WRAPMARKER) {
 			p = vd->logstart;
 			continue;
@@ -141,8 +146,36 @@
 			return (NULL);
 		}
 		vd->ptr = p + p[1] + 4;
-		if (!vd->supr[p[0]]) 
+		return (p);
+	}
+}
+
+unsigned char *
+VSL_NextLog(struct VSL_data *vd)
+{
+	unsigned char *p;
+	regmatch_t rm;
+	int i;
+
+	while (1) {
+		p = vsl_nextlog(vd);
+		if (p == NULL)
 			return (p);
+		if (vd->supr[p[0]]) 
+			continue;
+		rm.rm_so = 0;
+		rm.rm_eo = p[1];
+		if (vd->regincl != NULL) {
+			i = regexec(vd->regincl, p + 4, 1, &rm, REG_STARTEND);
+			if (i == REG_NOMATCH)
+				continue;
+		}
+		if (vd->regexcl != NULL) {
+			i = regexec(vd->regexcl, p + 4, 1, &rm, REG_STARTEND);
+			if (i != REG_NOMATCH)
+				continue;
+		}
+		return (p);
 	}
 }
 
@@ -165,6 +198,37 @@
 /*--------------------------------------------------------------------*/
 
 static int
+vsl_IX_arg(struct VSL_data *vd, const char *opt, int arg)
+{
+	int i;
+	regex_t **rp;
+	char buf[BUFSIZ];
+
+	if (arg == 'I')
+		rp = &vd->regincl;
+	else
+		rp = &vd->regexcl;
+	if (*rp != NULL) {
+		fprintf(stderr, "Option %c can only be given once", arg);
+		return (-1);
+	}
+	*rp = calloc(sizeof(regex_t), 1);
+	if (*rp == NULL) {
+		perror("malloc");
+		return (-1);
+	}
+	i = regcomp(*rp, opt, vd->regflags);
+	if (i) {
+		regerror(i, *rp, buf, sizeof buf);
+		fprintf(stderr, "%s", buf);
+		return (-1);
+	}
+	return (1);
+}
+
+/*--------------------------------------------------------------------*/
+
+static int
 vsl_ix_arg(struct VSL_data *vd, const char *opt, int arg)
 {
 	int i, j, l;
@@ -219,11 +283,10 @@
 VSL_Arg(struct VSL_data *vd, int arg, const char *opt)
 {
 	switch (arg) {
-	case 'i':
-	case 'x':
-		return (vsl_ix_arg(vd, opt, arg));
-	case 'r':
-		return (vsl_r_arg(vd, opt));
+	case 'i': case 'x': return (vsl_ix_arg(vd, opt, arg));
+	case 'r': return (vsl_r_arg(vd, opt));
+	case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg));
+	case 'C': vd->regflags = REG_ICASE; return (1);
 	default:
 		return (0);
 	}




More information about the varnish-commit mailing list