[Nagios-devel] [PATCH] Continuation lines
-
Guest
[Nagios-devel] [PATCH] Continuation lines
--XOIedfhf+7KOe/yw
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
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
--XOIedfhf+7KOe/yw
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="continuation_lines.diff"
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 +28
...[email truncated]...
This post was automatically imported from historical nagios-devel mailing list archives
Original poster: [email protected]