[PATCH] Continuation lines

Holger Weiss holger at CIS.FU-Berlin.DE
Sat Dec 8 00:17:11 CET 2007


The attached patch adds support for using continuation lines throughout
all configuration files: If the last character before a newline is a
backslash, the backslash will be stripped, and if the second last
character is not a backslash, the following line will be appended prior
to parsing.

base/utils.c (and cgi/cgiutils.c) had an mmap_fgets_multiline() function
already, but the function wasn't used anywhere and it didn't work out of
the box.

Holger
-------------- next part --------------
Index: base/config.c
===================================================================
RCS file: /cvsroot/nagios/nagios/base/config.c,v
retrieving revision 1.108
diff -u -r1.108 config.c
--- base/config.c	13 Nov 2007 01:27:19 -0000	1.108
+++ base/config.c	7 Dec 2007 22:48:19 -0000
@@ -277,7 +277,7 @@
 		my_free(value);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		current_line=thefile->current_line;
@@ -1403,7 +1403,7 @@
 		my_free(value);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		current_line=thefile->current_line;
Index: base/utils.c
===================================================================
RCS file: /cvsroot/nagios/nagios/base/utils.c,v
retrieving revision 1.213
diff -u -r1.213 utils.c
--- base/utils.c	13 Nov 2007 01:27:20 -0000	1.213
+++ base/utils.c	7 Dec 2007 22:48:19 -0000
@@ -3347,46 +3347,42 @@
 /* gets one line of input from an mmap()'ed file (may be contained on more than one line in the source file) */
 char *mmap_fgets_multiline(mmapfile *temp_mmapfile){
 	char *buf=NULL;
+	char *newbuf=NULL;
 	char *tempbuf=NULL;
+	int continuation;
+	int backslash;
+	int size=2;	/* trailing newline and nul-termination */
 	int len=0;
-	int len2=0;
 
-	if(temp_mmapfile==NULL)
+	if(temp_mmapfile==NULL || (buf=malloc(size))==NULL)
 		return NULL;
 
-	while(1){
-
+	do{
+		if((tempbuf=mmap_fgets(temp_mmapfile))==NULL){
+			my_free(buf);
+			return NULL;
+			}
+		if((len=strlen(tempbuf))>0 && tempbuf[len-1]=='\n')
+			len--;	/* strip the trailing newline */
+		if((backslash=(len>0 && tempbuf[len-1]=='\\')))
+			len--;	/* strip the trailing backslash */
+		continuation=(backslash && (len==0 || tempbuf[len-1]!='\\'));
+
+		if((newbuf=realloc(buf,size+len))==NULL){
+			my_free(tempbuf);
+			my_free(buf);
+			return NULL;
+			}
+		buf=newbuf;
+		memcpy(buf+size-2,tempbuf,len);
 		my_free(tempbuf);
+		size+=len;
+		}while(continuation);
 
-		if((tempbuf=mmap_fgets(temp_mmapfile))==NULL)
-			break;
-
-		if(buf==NULL){
-			len=strlen(tempbuf);
-			if((buf=(char *)malloc(len+1))==NULL)
-				break;
-			memcpy(buf,tempbuf,len);
-			buf[len]='\x0';
-		        }
-		else{
-			len=strlen(tempbuf);
-			len2=strlen(buf);
-			if((buf=(char *)realloc(buf,len+len2+1))==NULL)
-				break;
-			strcat(buf,tempbuf);
-			len+=len2;
-			buf[len]='\x0';
-		        }
-
-		/* we shouldn't continue to the next line... */
-		if(!(len>0 && buf[len-1]=='\\' && (len==1 || buf[len-2]!='\\')))
-			break;
-	        }
-
-	my_free(tempbuf);
-
+	buf[size-2]='\n';
+	buf[size-1]='\0';
 	return buf;
-        }
+	}
 
 
 
Index: cgi/cgiauth.c
===================================================================
RCS file: /cvsroot/nagios/nagios/cgi/cgiauth.c,v
retrieving revision 1.8
diff -u -r1.8 cgiauth.c
--- cgi/cgiauth.c	15 Aug 2007 17:03:00 -0000	1.8
+++ cgi/cgiauth.c	7 Dec 2007 22:48:20 -0000
@@ -111,7 +111,7 @@
 			free(input);
 
 			/* read the next line */
-			if((input=mmap_fgets(thefile))==NULL)
+			if((input=mmap_fgets_multiline(thefile))==NULL)
 				break;
 
 			strip(input);
Index: cgi/cgiutils.c
===================================================================
RCS file: /cvsroot/nagios/nagios/cgi/cgiutils.c,v
retrieving revision 1.71
diff -u -r1.71 cgiutils.c
--- cgi/cgiutils.c	10 Nov 2007 23:34:24 -0000	1.71
+++ cgi/cgiutils.c	7 Dec 2007 22:48:20 -0000
@@ -288,7 +288,7 @@
 		free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		strip(input);
@@ -448,7 +448,7 @@
 		free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		strip(input);
@@ -1301,46 +1301,42 @@
 /* gets one line of input from an mmap()'ed file (may be contained on more than one line in the source file) */
 char *mmap_fgets_multiline(mmapfile *temp_mmapfile){
 	char *buf=NULL;
+	char *newbuf=NULL;
 	char *tempbuf=NULL;
-	int len;
-	int len2;
+	int continuation;
+	int backslash;
+	int size=2;	/* trailing newline and nul-termination */
+	int len=0;
 
-	if(temp_mmapfile==NULL)
+	if(temp_mmapfile==NULL || (buf=malloc(size))==NULL)
 		return NULL;
 
-	while(1){
-
-		free(tempbuf);
-
-		if((tempbuf=mmap_fgets(temp_mmapfile))==NULL)
-			break;
-
-		if(buf==NULL){
-			len=strlen(tempbuf);
-			if((buf=(char *)malloc(len+1))==NULL)
-				break;
-			memcpy(buf,tempbuf,len);
-			buf[len]='\x0';
-		        }
-		else{
-			len=strlen(tempbuf);
-			len2=strlen(buf);
-			if((buf=(char *)realloc(buf,len+len2+1))==NULL)
-				break;
-			strcat(buf,tempbuf);
-			len+=len2;
-			buf[len]='\x0';
-		        }
-
-		/* we shouldn't continue to the next line... */
-		if(!(len>0 && buf[len-1]=='\\' && (len==1 || buf[len-2]!='\\')))
-			break;
-	        }
-
-	free(tempbuf);
+	do{
+		if((tempbuf=mmap_fgets(temp_mmapfile))==NULL){
+			my_free(buf);
+			return NULL;
+			}
+		if((len=strlen(tempbuf))>0 && tempbuf[len-1]=='\n')
+			len--;	/* strip the trailing newline */
+		if((backslash=(len>0 && tempbuf[len-1]=='\\')))
+			len--;	/* strip the trailing backslash */
+		continuation=(backslash && (len==0 || tempbuf[len-1]!='\\'));
+
+		if((newbuf=realloc(buf,size+len))==NULL){
+			my_free(tempbuf);
+			my_free(buf);
+			return NULL;
+			}
+		buf=newbuf;
+		memcpy(buf+size-2,tempbuf,len);
+		my_free(tempbuf);
+		size+=len;
+		}while(continuation);
 
+	buf[size-2]='\n';
+	buf[size-1]='\0';
 	return buf;
-        }
+	}
 
 
 
Index: xdata/xodtemplate.c
===================================================================
RCS file: /cvsroot/nagios/nagios/xdata/xodtemplate.c,v
retrieving revision 1.170
diff -u -r1.170 xodtemplate.c
--- xdata/xodtemplate.c	10 Nov 2007 23:34:27 -0000	1.170
+++ xdata/xodtemplate.c	7 Dec 2007 22:48:21 -0000
@@ -192,7 +192,7 @@
 			my_free(input);
 
 			/* get the next line */
-			if((input=mmap_fgets(thefile))==NULL)
+			if((input=mmap_fgets_multiline(thefile))==NULL)
 				break;
 
 			/* strip input */
@@ -450,7 +450,7 @@
 		my_free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		/* strip input */
@@ -694,7 +694,7 @@
 		my_free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		current_line++;
Index: xdata/xpddefault.c
===================================================================
RCS file: /cvsroot/nagios/nagios/xdata/xpddefault.c,v
retrieving revision 1.43
diff -u -r1.43 xpddefault.c
--- xdata/xpddefault.c	13 Nov 2007 21:56:09 -0000	1.43
+++ xdata/xpddefault.c	7 Dec 2007 22:48:21 -0000
@@ -95,7 +95,7 @@
 		my_free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		/* skip blank lines and comments */
Index: xdata/xrddefault.c
===================================================================
RCS file: /cvsroot/nagios/nagios/xdata/xrddefault.c,v
retrieving revision 1.58
diff -u -r1.58 xrddefault.c
--- xdata/xrddefault.c	11 Nov 2007 04:09:10 -0000	1.58
+++ xdata/xrddefault.c	7 Dec 2007 22:48:21 -0000
@@ -119,7 +119,7 @@
 		my_free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		strip(input);
Index: xdata/xsddefault.c
===================================================================
RCS file: /cvsroot/nagios/nagios/xdata/xsddefault.c,v
retrieving revision 1.64
diff -u -r1.64 xsddefault.c
--- xdata/xsddefault.c	10 Nov 2007 22:54:39 -0000	1.64
+++ xdata/xsddefault.c	7 Dec 2007 22:48:22 -0000
@@ -152,7 +152,7 @@
 		my_free(input);
 
 		/* read the next line */
-		if((input=mmap_fgets(thefile))==NULL)
+		if((input=mmap_fgets_multiline(thefile))==NULL)
 			break;
 
 		strip(input);
@@ -180,7 +180,7 @@
 				my_free(input2);
 
 				/* read the next line */
-				if((input2=mmap_fgets(thefile2))==NULL)
+				if((input2=mmap_fgets_multiline(thefile2))==NULL)
 					break;
 
 				strip(input2);
-------------- next part --------------
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list