Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Sep 2023 15:52:39 GMT
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: f4b4a10abb26 - main - syslogd: Move selector parsing into its own function
Message-ID:  <202309281552.38SFqdOL022216@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=f4b4a10abb2688f0fef9a0cf15117b10a7f2d398

commit f4b4a10abb2688f0fef9a0cf15117b10a7f2d398
Author:     Jake Freeland <jfree@FreeBSD.org>
AuthorDate: 2023-09-01 02:50:45 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-09-28 15:51:53 +0000

    syslogd: Move selector parsing into its own function
    
    Clean up the cfline() function by moving selector parsing into its
    own function.
    
    Reviewed by:    markj
    MFC after:      3 weeks
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D41374
---
 usr.sbin/syslogd/syslogd.c | 210 ++++++++++++++++++++++-----------------------
 1 file changed, 104 insertions(+), 106 deletions(-)

diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c
index 40c233f02d7f..6c07d60b484f 100644
--- a/usr.sbin/syslogd/syslogd.c
+++ b/usr.sbin/syslogd/syslogd.c
@@ -2865,6 +2865,107 @@ error:
 	return (NULL);
 }
 
+static const char *
+parse_selector(const char *p, struct filed *f)
+{
+	int i, pri;
+	int pri_done = 0, pri_cmp = 0, pri_invert = 0;
+	char *bp, buf[LINE_MAX], ebuf[100];
+	const char *q;
+
+	/* find the end of this facility name list */
+	for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.';)
+		continue;
+
+	/* get the priority comparison */
+	if (*q == '!') {
+		pri_invert = 1;
+		q++;
+	}
+	while (!pri_done) {
+		switch (*q) {
+			case '<':
+				pri_cmp |= PRI_LT;
+				q++;
+				break;
+			case '=':
+				pri_cmp |= PRI_EQ;
+				q++;
+				break;
+			case '>':
+				pri_cmp |= PRI_GT;
+				q++;
+				break;
+			default:
+				pri_done++;
+				break;
+		}
+	}
+
+	/* collect priority name */
+	for (bp = buf; *q != '\0' && !strchr("\t,; ", *q); )
+		*bp++ = *q++;
+	*bp = '\0';
+
+	/* skip cruft */
+	while (strchr(",;", *q))
+		q++;
+
+	/* decode priority name */
+	if (*buf == '*') {
+		pri = LOG_PRIMASK;
+		pri_cmp = PRI_LT | PRI_EQ | PRI_GT;
+	} else {
+		/* Ignore trailing spaces. */
+		for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--)
+			buf[i] = '\0';
+
+		pri = decode(buf, prioritynames);
+		if (pri < 0) {
+			errno = 0;
+			(void)snprintf(ebuf, sizeof ebuf,
+			    "unknown priority name \"%s\"", buf);
+			logerror(ebuf);
+			free(f);
+			return (NULL);
+		}
+	}
+	if (!pri_cmp)
+		pri_cmp = UniquePriority ? PRI_EQ : (PRI_EQ | PRI_GT);
+	if (pri_invert)
+		pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT;
+
+	/* scan facilities */
+	while (*p != '\0' && !strchr("\t.; ", *p)) {
+		for (bp = buf; *p != '\0' && !strchr("\t,;. ", *p); )
+			*bp++ = *p++;
+		*bp = '\0';
+
+		if (*buf == '*') {
+			for (i = 0; i < LOG_NFACILITIES; i++) {
+				f->f_pmask[i] = pri;
+				f->f_pcmp[i] = pri_cmp;
+			}
+		} else {
+			i = decode(buf, facilitynames);
+			if (i < 0) {
+				errno = 0;
+				(void)snprintf(ebuf, sizeof ebuf,
+				    "unknown facility name \"%s\"",
+				    buf);
+				logerror(ebuf);
+				free(f);
+				return (NULL);
+			}
+			f->f_pmask[i >> 3] = pri;
+			f->f_pcmp[i >> 3] = pri_cmp;
+		}
+		while (*p == ',' || *p == ' ')
+			p++;
+	}
+	return (q);
+}
+
 /*
  * Crack a configuration file line
  */
@@ -2874,9 +2975,8 @@ cfline(const char *line, const char *prog, const char *host,
 {
 	struct filed *f;
 	struct addrinfo hints, *res;
-	int error, i, pri;
+	int error, i;
 	const char *p, *q;
-	char *bp, buf[LINE_MAX], ebuf[100];
 	bool syncfile;
 
 	dprintf("cfline(\"%s\", f, \"%s\", \"%s\", \"%s\")\n", line, prog,
@@ -2931,110 +3031,8 @@ cfline(const char *line, const char *prog, const char *host,
 	}
 
 	/* scan through the list of selectors */
-	for (p = line; *p && *p != '\t' && *p != ' ';) {
-		int pri_done;
-		int pri_cmp;
-		int pri_invert;
-
-		/* find the end of this facility name list */
-		for (q = p; *q && *q != '\t' && *q != ' ' && *q++ != '.'; )
-			continue;
-
-		/* get the priority comparison */
-		pri_cmp = 0;
-		pri_done = 0;
-		pri_invert = 0;
-		if (*q == '!') {
-			pri_invert = 1;
-			q++;
-		}
-		while (!pri_done) {
-			switch (*q) {
-			case '<':
-				pri_cmp |= PRI_LT;
-				q++;
-				break;
-			case '=':
-				pri_cmp |= PRI_EQ;
-				q++;
-				break;
-			case '>':
-				pri_cmp |= PRI_GT;
-				q++;
-				break;
-			default:
-				pri_done++;
-				break;
-			}
-		}
-
-		/* collect priority name */
-		for (bp = buf; *q && !strchr("\t,; ", *q); )
-			*bp++ = *q++;
-		*bp = '\0';
-
-		/* skip cruft */
-		while (strchr(",;", *q))
-			q++;
-
-		/* decode priority name */
-		if (*buf == '*') {
-			pri = LOG_PRIMASK;
-			pri_cmp = PRI_LT | PRI_EQ | PRI_GT;
-		} else {
-			/* Ignore trailing spaces. */
-			for (i = strlen(buf) - 1; i >= 0 && buf[i] == ' '; i--)
-				buf[i] = '\0';
-
-			pri = decode(buf, prioritynames);
-			if (pri < 0) {
-				errno = 0;
-				(void)snprintf(ebuf, sizeof ebuf,
-				    "unknown priority name \"%s\"", buf);
-				logerror(ebuf);
-				free(f);
-				return;
-			}
-		}
-		if (!pri_cmp)
-			pri_cmp = (UniquePriority)
-				  ? (PRI_EQ)
-				  : (PRI_EQ | PRI_GT)
-				  ;
-		if (pri_invert)
-			pri_cmp ^= PRI_LT | PRI_EQ | PRI_GT;
-
-		/* scan facilities */
-		while (*p && !strchr("\t.; ", *p)) {
-			for (bp = buf; *p && !strchr("\t,;. ", *p); )
-				*bp++ = *p++;
-			*bp = '\0';
-
-			if (*buf == '*') {
-				for (i = 0; i < LOG_NFACILITIES; i++) {
-					f->f_pmask[i] = pri;
-					f->f_pcmp[i] = pri_cmp;
-				}
-			} else {
-				i = decode(buf, facilitynames);
-				if (i < 0) {
-					errno = 0;
-					(void)snprintf(ebuf, sizeof ebuf,
-					    "unknown facility name \"%s\"",
-					    buf);
-					logerror(ebuf);
-					free(f);
-					return;
-				}
-				f->f_pmask[i >> 3] = pri;
-				f->f_pcmp[i >> 3] = pri_cmp;
-			}
-			while (*p == ',' || *p == ' ')
-				p++;
-		}
-
-		p = q;
-	}
+	for (p = line; *p != '\0' && *p != '\t' && *p != ' ';)
+		p = parse_selector(p, f);
 
 	/* skip to action part */
 	while (*p == '\t' || *p == ' ')



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202309281552.38SFqdOL022216>