2005-08-30 13:58:48 +00:00
/*
* Unix SMB / CIFS implementation .
* Copyright ( C ) Jelmer Vernooij 2005
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* 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 0213 9 , USA .
*/
# include "includes.h"
# include "dlinklist.h"
# include "param/generic.h"
2005-08-30 14:44:33 +00:00
# include "system/filesys.h"
2005-08-30 13:58:48 +00:00
struct param_section * param_get_section ( struct param_context * ctx , const char * name )
{
struct param_section * sect ;
if ( name = = NULL )
name = GLOBAL_NAME ;
for ( sect = ctx - > sections ; sect ; sect = sect - > next ) {
if ( ! strcasecmp_m ( sect - > name , name ) )
return sect ;
}
return NULL ;
}
struct param * param_section_get ( struct param_section * section , const char * name )
{
struct param * p ;
for ( p = section - > parameters ; p ; p = p - > next ) {
2005-08-30 14:44:33 +00:00
if ( strcasecmp_m ( p - > name , name ) = = 0 )
2005-08-30 13:58:48 +00:00
return p ;
}
return NULL ;
}
struct param * param_get ( struct param_context * ctx , const char * section_name , const char * name )
{
struct param_section * section = param_get_section ( ctx , section_name ) ;
if ( section = = NULL )
return NULL ;
return param_section_get ( section , name ) ;
}
/* Look up parameter. If it is not found, add it */
static struct param * param_get_add ( struct param_context * ctx , const char * section_name , const char * name )
{
struct param_section * section ;
struct param * p ;
section = param_get_section ( ctx , section_name ) ;
if ( section = = NULL ) {
section = talloc_zero ( ctx , struct param_section ) ;
2005-08-31 14:26:20 +00:00
if ( section = = NULL )
return NULL ;
2005-08-30 13:58:48 +00:00
section - > name = talloc_strdup ( section , section_name ) ;
DLIST_ADD ( ctx - > sections , section ) ;
}
p = param_section_get ( section , name ) ;
if ( p = = NULL ) {
p = talloc_zero ( section , struct param ) ;
2005-08-31 14:26:20 +00:00
if ( p = = NULL )
return NULL ;
2005-08-30 13:58:48 +00:00
p - > name = talloc_strdup ( p , name ) ;
DLIST_ADD ( section - > parameters , p ) ;
}
return p ;
}
const char * param_get_string ( struct param_context * ctx , const char * section , const char * param )
{
struct param * p = param_get ( ctx , section , param ) ;
if ( p = = NULL )
return NULL ;
return p - > value ;
}
2005-08-30 14:44:33 +00:00
int param_set_string ( struct param_context * ctx , const char * section , const char * param , const char * value )
2005-08-30 13:58:48 +00:00
{
struct param * p = param_get_add ( ctx , section , param ) ;
2005-08-30 14:44:33 +00:00
if ( p = = NULL )
return - 1 ;
2005-08-30 13:58:48 +00:00
p - > value = talloc_strdup ( p , value ) ;
2005-08-30 14:44:33 +00:00
return 0 ;
2005-08-30 13:58:48 +00:00
}
const char * * param_get_string_list ( struct param_context * ctx , const char * section , const char * param ,
const char * separator )
{
struct param * p = param_get ( ctx , section , param ) ;
if ( p = = NULL )
return NULL ;
2005-08-30 14:44:33 +00:00
if ( separator = = NULL )
separator = LIST_SEP ;
2005-08-30 13:58:48 +00:00
if ( p - > list_value = = NULL ) {
p - > list_value = str_list_make ( ctx , p - > value , separator ) ;
}
return p - > list_value ;
}
2005-08-30 16:09:38 +00:00
int param_set_string_list ( struct param_context * ctx , const char * section , const char * param , const char * * list )
2005-08-30 13:58:48 +00:00
{
struct param * p = param_get_add ( ctx , section , param ) ;
p - > value = str_list_join ( p , list , ' ' ) ;
p - > list_value = str_list_copy ( p , list ) ;
2005-08-30 16:09:38 +00:00
return 0 ;
2005-08-30 13:58:48 +00:00
}
int param_get_int ( struct param_context * ctx , const char * section , const char * param , int default_v )
{
const char * value = param_get_string ( ctx , section , param ) ;
if ( value )
return strtol ( value , NULL , 0 ) ;
return default_v ;
}
void param_set_int ( struct param_context * ctx , const char * section , const char * param , int value )
{
struct param * p = param_get_add ( ctx , section , param ) ;
2005-08-31 14:26:20 +00:00
if ( ! p )
return ;
2005-08-30 13:58:48 +00:00
p - > value = talloc_asprintf ( p , " %d " , value ) ;
}
unsigned long param_get_ulong ( struct param_context * ctx , const char * section , const char * param , unsigned long default_v )
{
const char * value = param_get_string ( ctx , section , param ) ;
if ( value )
return strtoul ( value , NULL , 0 ) ;
return default_v ;
}
void param_set_ulong ( struct param_context * ctx , const char * section , const char * name , unsigned long value )
{
struct param * p = param_get_add ( ctx , section , name ) ;
2005-08-31 14:26:20 +00:00
if ( ! p )
return ;
2005-08-30 13:58:48 +00:00
p - > value = talloc_asprintf ( p , " %lu " , value ) ;
}
static BOOL param_sfunc ( const char * name , void * _ctx )
{
struct param_context * ctx = _ctx ;
struct param_section * section = param_get_section ( ctx , name ) ;
if ( section = = NULL ) {
section = talloc_zero ( ctx , struct param_section ) ;
2005-08-31 14:26:20 +00:00
if ( section = = NULL )
return False ;
2005-08-30 13:58:48 +00:00
section - > name = talloc_strdup ( section , name ) ;
DLIST_ADD ( ctx - > sections , section ) ;
}
2005-08-30 14:44:33 +00:00
/* Make sure this section is on top of the list for param_pfunc */
2005-08-30 13:58:48 +00:00
DLIST_PROMOTE ( ctx - > sections , section ) ;
return True ;
}
static BOOL param_pfunc ( const char * name , const char * value , void * _ctx )
{
struct param_context * ctx = _ctx ;
struct param * p = param_section_get ( ctx - > sections , name ) ;
if ( ! p ) {
p = talloc_zero ( ctx - > sections , struct param ) ;
2005-08-31 14:26:20 +00:00
if ( p = = NULL )
return False ;
2005-08-30 13:58:48 +00:00
p - > name = talloc_strdup ( p , name ) ;
p - > value = talloc_strdup ( p , value ) ;
DLIST_ADD ( ctx - > sections - > parameters , p ) ;
} else { /* Replace current value */
talloc_free ( p - > value ) ;
p - > value = talloc_strdup ( p , value ) ;
}
return True ;
}
2005-08-30 14:44:33 +00:00
struct param_context * param_init ( TALLOC_CTX * mem_ctx )
2005-08-30 13:58:48 +00:00
{
2005-08-30 14:44:33 +00:00
return talloc_zero ( mem_ctx , struct param_context ) ;
}
2005-08-30 13:58:48 +00:00
2005-08-30 14:44:33 +00:00
int param_read ( struct param_context * ctx , const char * fn )
{
2005-08-30 13:58:48 +00:00
ctx - > sections = talloc_zero ( ctx , struct param_section ) ;
2005-08-31 14:26:20 +00:00
if ( ctx - > sections = = NULL )
return - 1 ;
2005-08-30 13:58:48 +00:00
ctx - > sections - > name = talloc_strdup ( ctx - > sections , " global " ) ;
if ( ! pm_process ( fn , param_sfunc , param_pfunc , ctx ) ) {
2005-08-30 14:44:33 +00:00
return - 1 ;
2005-08-30 13:58:48 +00:00
}
2005-08-30 14:44:33 +00:00
return 0 ;
2005-08-30 13:58:48 +00:00
}
2005-08-30 14:44:33 +00:00
int param_write ( struct param_context * ctx , const char * fn )
2005-08-30 13:58:48 +00:00
{
2005-09-15 19:52:13 +00:00
int file ;
2005-08-30 13:58:48 +00:00
struct param_section * section ;
2005-08-30 14:44:33 +00:00
if ( fn = = NULL | | ctx = = NULL )
2005-08-30 13:58:48 +00:00
return - 1 ;
2005-09-15 19:52:13 +00:00
file = open ( fn , O_WRONLY | O_CREAT , 0755 ) ;
2005-08-30 14:44:33 +00:00
2005-09-15 19:52:13 +00:00
if ( file = = - 1 )
2005-08-30 13:58:48 +00:00
return - 1 ;
for ( section = ctx - > sections ; section ; section = section - > next ) {
struct param * param ;
2005-09-15 19:52:13 +00:00
fdprintf ( file , " [%s] \n " , section - > name ) ;
2005-08-30 13:58:48 +00:00
for ( param = section - > parameters ; param ; param = param - > next ) {
2005-09-15 19:52:13 +00:00
fdprintf ( file , " \t %s = %s \n " , param - > name , param - > value ) ;
2005-08-30 13:58:48 +00:00
}
2005-09-15 19:52:13 +00:00
fdprintf ( file , " \n " ) ;
2005-08-30 13:58:48 +00:00
}
2005-09-15 19:52:13 +00:00
close ( file ) ;
2005-08-30 14:44:33 +00:00
2005-08-30 13:58:48 +00:00
return 0 ;
}