X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Finiparser%2Finiparser.c;h=343a6d063b5cbd5028934f05e5b63c9f7f567d9c;hp=02a23b7559d482aaa062aeacff1e2c735f3c5006;hb=d7e5521f69859b3df89da09bf50621945f119d97;hpb=54c089a944a796823bfad708429d9c379e39b36a diff --git a/libatalk/iniparser/iniparser.c b/libatalk/iniparser/iniparser.c index 02a23b75..343a6d06 100644 --- a/libatalk/iniparser/iniparser.c +++ b/libatalk/iniparser/iniparser.c @@ -1,4 +1,3 @@ - /*-------------------------------------------------------------------------*/ /** @file iniparser.c @@ -7,15 +6,16 @@ @version 3.0 @brief Parser for ini files. */ -/*--------------------------------------------------------------------------*/ -/* - $Id: iniparser.c,v 2.19 2011-03-02 20:15:13 ndevilla Exp $ - $Revision: 2.19 $ - $Date: 2011-03-02 20:15:13 $ -*/ + /*---------------------------- Includes ------------------------------------*/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif /* HAVE_CONFIG_H */ + #include -#include "iniparser.h" + +#include +#include /*---------------------------- Defines -------------------------------------*/ #define ASCIILINESZ (1024) @@ -38,37 +38,9 @@ typedef enum _line_status_ { /*-------------------------------------------------------------------------*/ /** - @brief Convert a string to lowercase. - @param s String to convert. - @return ptr to statically allocated string. - - This function returns a pointer to a statically allocated string - containing a lowercased version of the input string. Do not free - or modify the returned string! Since the returned string is statically - allocated, it will be modified at each function call (not re-entrant). - */ -/*--------------------------------------------------------------------------*/ -static char * strlwc(char * s) -{ - static char l[ASCIILINESZ+1]; - int i ; - - if (s==NULL) return NULL ; - memset(l, 0, ASCIILINESZ+1); - i=0 ; - while (s[i] && i l) { - if (!isspace((int)*(last-1))) - break ; - last -- ; - } - *last = (char)0; - return (char*)l ; + + while (isspace((int)*s) && *s) s++; + memset(l, 0, ASCIILINESZ+1); + strcpy(l, s); + last = l + strlen(l); + while (last > l) { + if (!isspace((int)*(last-1))) + break ; + last -- ; + } + *last = (char)0; + return (char*)l ; } /*-------------------------------------------------------------------------*/ @@ -116,7 +88,7 @@ static char * strstrip(char * s) This function returns -1 in case of error. */ /*--------------------------------------------------------------------------*/ -int iniparser_getnsec(dictionary * d) +int atalk_iniparser_getnsec(const dictionary * d) { int i ; int nsec ; @@ -147,7 +119,7 @@ int iniparser_getnsec(dictionary * d) This function returns NULL in case of error. */ /*--------------------------------------------------------------------------*/ -char * iniparser_getsecname(dictionary * d, int n) +const char * atalk_iniparser_getsecname(const dictionary * d, int n) { int i ; int foundsec ; @@ -182,7 +154,7 @@ char * iniparser_getsecname(dictionary * d, int n) purposes mostly. */ /*--------------------------------------------------------------------------*/ -void iniparser_dump(dictionary * d, FILE * f) +void atalk_iniparser_dump(const dictionary * d, FILE * f) { int i ; @@ -210,17 +182,17 @@ void iniparser_dump(dictionary * d, FILE * f) It is Ok to specify @c stderr or @c stdout as output files. */ /*--------------------------------------------------------------------------*/ -void iniparser_dump_ini(dictionary * d, FILE * f) +void atalk_iniparser_dump_ini(const dictionary * d, FILE * f) { int i, j ; char keym[ASCIILINESZ+1]; int nsec ; - char * secname ; + const char * secname ; int seclen ; if (d==NULL || f==NULL) return ; - nsec = iniparser_getnsec(d); + nsec = atalk_iniparser_getnsec(d); if (nsec<1) { /* No section in file: dump all keys as they are */ for (i=0 ; isize ; i++) { @@ -231,7 +203,7 @@ void iniparser_dump_ini(dictionary * d, FILE * f) return ; } for (i=0 ; i=0) && ((line[len]=='\n') || (isspace(line[len])))) { @@ -589,40 +587,47 @@ dictionary * iniparser_load(char * ininame) } else { last=0 ; } - switch (iniparser_line(line, section, key, val)) { - case LINE_EMPTY: - case LINE_COMMENT: + switch (atalk_iniparser_line(line, section, key, val)) { + case LINE_EMPTY: + case LINE_COMMENT: break ; - - case LINE_SECTION: - errs = dictionary_set(dict, section, NULL); + case LINE_SECTION: + if (strchr(section, ':') != NULL) + LOG(log_error, logtype_default, "iniparser: syntax error \"%s\" section name must not contain \":\".", section); + errs = atalkdict_set(dict, section, NULL, NULL); break ; - - case LINE_VALUE: - sprintf(tmp, "%s:%s", section, key); - errs = dictionary_set(dict, tmp, val) ; + case LINE_VALUE: + if (strcmp(key, "include") == 0) { + errs = atalkdict_set(dict, section, key, val); + if (errs < 0) { + LOG(log_error, logtype_default, "iniparser: memory allocation failure"); + } + if ((include = fopen(val, "r")) == NULL) { + LOG(log_error, logtype_default, "iniparser: cannot open \"%s\"", val); + continue; + } + in = include; + continue; + } + errs = atalkdict_set(dict, section, key, val) ; break ; - - case LINE_ERROR: - fprintf(stderr, "iniparser: syntax error in %s (%d):\n", - ininame, - lineno); - fprintf(stderr, "-> %s\n", line); + case LINE_ERROR: + LOG(log_error, logtype_default, "iniparser: syntax error in %s (lineno: %d): %s", + ininame, lineno, line); errs++ ; break; - - default: + default: break ; } memset(line, 0, ASCIILINESZ); last=0; if (errs<0) { - fprintf(stderr, "iniparser: memory allocation failure\n"); + LOG(log_error, logtype_default, "iniparser: memory allocation failure"); break ; } } if (errs) { - dictionary_del(dict); + atalkdict_del(dict); dict = NULL ; } fclose(in); @@ -640,9 +645,8 @@ dictionary * iniparser_load(char * ininame) gets out of the current context. */ /*--------------------------------------------------------------------------*/ -void iniparser_freedict(dictionary * d) +void atalk_iniparser_freedict(dictionary * d) { - dictionary_del(d); + atalkdict_del(d); } -/* vim: set ts=4 et sw=4 tw=75 */