From 335bd0d855dc2cd0b2b6c1f666fd7402afb591ca Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 9 Mar 2010 16:37:48 +0000 Subject: bogom 1.9.2 import --- conf.c | 365 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 conf.c (limited to 'conf.c') diff --git a/conf.c b/conf.c new file mode 100644 index 0000000..ab91625 --- /dev/null +++ b/conf.c @@ -0,0 +1,365 @@ +/* $Id$ */ + +/* +* conf.c, configuration reader and parser +* Copyright (C) 2004-2007 Juan J. Martinez +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License Version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* +*/ + +#include +#include +#include +#include +#include + +#include "libmilter/mfapi.h" +#include "conf.h" + +#define new_string_list(x) do {\ + x=(struct string_list *) \ + malloc(sizeof(struct string_list));\ + x->n=NULL;\ + } while(0) + +static char * pstrncpy(char *, const char *, size_t); +static int parse_string(char *p); +static int parse_qstring(char *p); +static int parse_bool(char *p); +static char * parse_conf(struct conftoken *conf, char *p); + +static const char rcsid[]="$Id$"; + +/* +* strncpy alike function that parses scaped quotes +*/ +static char * +pstrncpy(char *d, const char *s, size_t l) +{ + size_t i, j; + + for(i=0, j=0; in=conf[i].sl; + conf[i].sl=t; + } + + conf[i].sl->s=(char *)malloc(len+1); + if(!conf[i].sl->s) + { + fprintf(stderr, "malloc\n"); + return p; + } + pstrncpy(conf[i].sl->s, p, len); + p+=len+1; + break; + } + break; + } + + if(conf[i].word) + { + while(isspace(*p)) + p++; + + if(!p[0]) + return NULL; + + fprintf(stderr, "parse error\n"); + return p; + } + + fprintf(stderr, "unknown token\n"); + + p[len]=0; + return p; +} + +/* +* reads and parses the configuration file +*/ +int +read_conf(const char *filename, struct conftoken *conf) +{ + FILE *fd; + char buffer[1024]; + char *ret; + int line, i; + + fd=fopen(filename, "r"); + if(!fd) + return 0; + + line=1; + while(!feof(fd)) + { + i=0; + *buffer=0; + do + { + if(i>1023) + { + fclose(fd); + fprintf(stderr, "conf line %i is too long\n", + line); + return 1; + } + + fscanf(fd, "%c", &buffer[i]); + + } while(buffer[i++]!='\n' && !feof(fd)); + + buffer[i]=0; + + ret=parse_conf(conf, buffer); + if(ret) + { + fclose(fd); + fprintf(stderr, "conf error at line %i, near: %s\n", + line, ret); + return 1; + } + + line++; + } + + fclose(fd); + + return 0; +} + +/* EOF */ -- cgit v1.2.3