Search
lxdream.org :: lxdream/src/config.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 450:207461e79f21
next458:cbb2dd12daeb
author nkeynes
date Wed Oct 17 11:26:45 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Split config management out to config.[ch]
Manage config filename
Check home dir + sysconfdir for conf file
Initial work on a path settings dialog
view annotate diff log raw
     1 /**
     2  * $Id: config.c,v 1.1 2007-10-17 11:26:45 nkeynes Exp $
     3  *
     4  * User configuration support
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <unistd.h>
    20 #include <stdio.h>
    21 #include <errno.h>
    22 #include <stdlib.h>
    23 #include <string.h>
    24 #include <glib/gstrfuncs.h>
    25 #include "dream.h"
    26 #include "config.h"
    27 #include "maple/maple.h"
    29 gboolean lxdream_load_config_file( const gchar *filename );
    30 gboolean lxdream_save_config_file( const gchar *filename );
    31 gboolean lxdream_load_config_stream( FILE *f );
    32 gboolean lxdream_save_config_stream( FILE *f );
    34 static struct lxdream_config_entry global_config[] =
    35     {{ "bios", CONFIG_TYPE_FILE, "dcboot.rom" },
    36      { "flash", CONFIG_TYPE_FILE, "dcflash.rom" },
    37      { "default path", CONFIG_TYPE_PATH, "." },
    38      { "save path", CONFIG_TYPE_PATH, "save" },
    39      { "bootstrap", CONFIG_TYPE_FILE, "IP.BIN" },
    40      { NULL, CONFIG_TYPE_NONE }};
    42 static struct lxdream_config_entry serial_config[] =
    43     {{ "device", CONFIG_TYPE_FILE, "/dev/ttyS1" },
    44      { NULL, CONFIG_TYPE_NONE }};
    46 struct lxdream_config_group lxdream_config_root[] = 
    47     {{ "global", global_config },
    48      { "controllers", NULL },
    49      { "serial", serial_config },
    50      { NULL, CONFIG_TYPE_NONE }};
    52 static gchar *lxdream_config_load_filename = NULL;
    53 static gchar *lxdream_config_save_filename = NULL;
    55 gboolean lxdream_find_config()
    56 {
    57     char *home = getenv("HOME");
    58     if( lxdream_config_save_filename == NULL ) {
    59 	lxdream_config_save_filename = g_strdup_printf("%s/.%s", home, DEFAULT_CONFIG_FILENAME);
    60     }
    61     if( lxdream_config_load_filename == NULL ) {
    62 	if( access(lxdream_config_save_filename, R_OK) == 0 ) {
    63 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    64 	} else if( access( PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    65 	    lxdream_config_load_filename = g_strdup(PACKAGE_CONF_DIR "/" DEFAULT_CONFIG_FILENAME);
    66 	} else if( access( "./" DEFAULT_CONFIG_FILENAME, R_OK ) == 0 ) {
    67 	    lxdream_config_load_filename = g_strdup("./" DEFAULT_CONFIG_FILENAME);
    68 	} else {
    69 	    lxdream_config_load_filename = g_strdup(lxdream_config_save_filename);
    70 	}	
    71     } 
    72 }
    74 void lxdream_set_config_filename( const gchar *filename )
    75 {
    76     if( lxdream_config_load_filename != NULL ) {
    77 	g_free(lxdream_config_load_filename);
    78     }
    79     lxdream_config_load_filename = g_strdup(filename);
    80     if( lxdream_config_save_filename != NULL ) {
    81 	g_free(lxdream_config_save_filename);
    82     }
    83     lxdream_config_save_filename = g_strdup(filename);
    84 }
    86 void lxdream_set_default_config( )
    87 {
    88     struct lxdream_config_group *group = lxdream_config_root;
    89     while( group->key != NULL ) {
    90 	struct lxdream_config_entry *param = group->params;
    91 	if( param != NULL ) {
    92 	    while( param->key != NULL ) {
    93 		if( param->value != param->default_value ) {
    94 		    if( param->value != NULL )
    95 			free( param->value );
    96 		    param->value = (gchar *)param->default_value;
    97 		}
    98 		param++;
    99 	    }
   100 	}
   101 	group++;
   102     }
   103     maple_detach_all();
   104 }
   106 const gchar *lxdream_get_config_value( int key )
   107 {
   108     return global_config[key].value;
   109 }
   111 void lxdream_set_config_value( int key, const gchar *value )
   112 {
   113     struct lxdream_config_entry *param = &global_config[key];
   114     if( param->value != param->default_value && param->value != NULL ) {
   115 	free( param->value );
   116     }
   117     param->value = g_strdup(value);
   118 }
   120 gboolean lxdream_load_config( )
   121 {
   122     if( lxdream_config_load_filename == NULL ) {
   123 	lxdream_find_config();
   124     }
   125     return lxdream_load_config_file(lxdream_config_load_filename);
   126 }
   128 gboolean lxdream_save_config( )
   129 {
   130     if( lxdream_config_save_filename == NULL ) {
   131 	lxdream_find_config();
   132     }
   133     return lxdream_save_config_file(lxdream_config_save_filename);
   134 }
   136 gboolean lxdream_load_config_file( const gchar *filename )
   137 {
   138     FILE *f;
   139     gboolean result;
   141     if( access(filename, F_OK) != 0 ) {
   142 	INFO( "Configuration file '%s' does not exist, creating from defaults" );
   143 	lxdream_set_default_config();
   144 	lxdream_save_config();
   145     }
   147     f = fopen(filename, "ro");
   148     if( f == NULL ) {
   149 	ERROR( "Unable to open configuration file '%s': %s", filename, strerror(errno) );
   150 	lxdream_set_default_config();
   151 	return FALSE;
   152     }
   154     result = lxdream_load_config_stream( f );
   155     fclose(f);
   156     return result;
   157 }
   159 gboolean lxdream_load_config_stream( FILE *f )
   160 {
   162     char buf[512];
   163     int maple_device = -1, maple_subdevice = -1;
   164     struct lxdream_config_group devgroup;
   165     struct lxdream_config_group *group = NULL;
   166     maple_device_t device = NULL;
   167     lxdream_set_default_config();
   169     while( fgets( buf, sizeof(buf), f ) != NULL ) {
   170 	g_strstrip(buf);
   171 	if( buf[0] == '#' )
   172 	    continue;
   173 	if( *buf == '[' ) {
   174 	    char *p = strchr(buf, ']');
   175 	    if( p != NULL ) {
   176 		struct lxdream_config_group *tmp_group;
   177 		maple_device = maple_subdevice = -1;
   178 		*p = '\0';
   179 		g_strstrip(buf+1);
   180 		tmp_group = &lxdream_config_root[0];
   181 		while( tmp_group->key != NULL ) {
   182 		    if( strcasecmp(tmp_group->key, buf+1) == 0 ) {
   183 			group = tmp_group;
   184 			break;
   185 		    }
   186 		    tmp_group++;
   187 		}
   188 	    }
   189 	} else if( group != NULL ) {
   190 	    char *value = strchr( buf, '=' );
   191 	    if( value != NULL ) {
   192 		struct lxdream_config_entry *param = group->params;
   193 		*value = '\0';
   194 		value++;
   195 		g_strstrip(buf);
   196 		g_strstrip(value);
   197 		if( strcmp(group->key,"controllers") == 0  ) {
   198 		    if( g_strncasecmp( buf, "device ", 7 ) == 0 ) {
   199 			maple_device = strtoul( buf+7, NULL, 0 );
   200 			if( maple_device < 0 || maple_device > 3 ) {
   201 			    ERROR( "Device number must be between 0..3 (not '%s')", buf+7);
   202 			    continue;
   203 			}
   204 			maple_subdevice = 0;
   205 			device = maple_new_device( value );
   206 			if( device == NULL ) {
   207 			    ERROR( "Unrecognized device '%s'", value );
   208 			} else {
   209 			    devgroup.key = "controllers";
   210 			    devgroup.params = maple_get_device_config(device);
   211 			    maple_attach_device( device, maple_device, maple_subdevice );
   212 			    group = &devgroup;
   213 			}
   214 			continue;
   215 		    } else if( g_strncasecmp( buf, "subdevice ", 10 ) == 0 ) {
   216 			maple_subdevice = strtoul( buf+10, NULL, 0 );
   217 			if( maple_device == -1 ) {
   218 			    ERROR( "Subdevice not allowed without primary device" );
   219 			} else if( maple_subdevice < 1 || maple_subdevice > 5 ) {
   220 			    ERROR( "Subdevice must be between 1..5 (not '%s')", buf+10 );
   221 			} else if( (device = maple_new_device(value)) == NULL ) {
   222 			    ERROR( "Unrecognized subdevice '%s'", value );
   223 			} else {
   224 			    devgroup.key = "controllers";
   225 			    devgroup.params = maple_get_device_config(device);
   226 			    maple_attach_device( device, maple_device, maple_subdevice );
   227 			    group = &devgroup;
   228 			}
   229 			continue;
   230 		    }
   231 		}
   232 		while( param->key != NULL ) {
   233 		    if( strcasecmp( param->key, buf ) == 0 ) {
   234 			param->value = g_strdup(value);
   235 			break;
   236 		    }
   237 		    param++;
   238 		}
   239 	    }
   240 	}
   241     }
   242     return TRUE;
   243 }
   245 gboolean lxdream_save_config_file( const gchar *filename )
   246 {
   247     FILE *f = fopen(filename, "wo");
   248     gboolean result;
   249     if( f == NULL ) {
   250 	ERROR( "Unable to open '%s': %s", filename, strerror(errno) );
   251 	return FALSE;
   252     }
   253     result = lxdream_save_config_stream(f);
   254     fclose(f);
   255     return TRUE;
   256 }    
   258 gboolean lxdream_save_config_stream( FILE *f )
   259 {
   260     struct lxdream_config_group *group = &lxdream_config_root[0];
   262     while( group->key != NULL ) {
   263 	struct lxdream_config_entry *entry = group->params;
   264 	fprintf( f, "[%s]\n", group->key );
   266 	if( entry != NULL ) {
   267 	    while( entry->key != NULL ) {
   268 		fprintf( f, "%s = %s\n", entry->key, entry->value );
   269 		entry++;
   270 	    }
   271 	} else if( strcmp(group->key, "controllers") == 0 ) {
   272 	    int i,j;
   273 	    for( i=0; i<4; i++ ) {
   274 		for( j=0; j<6; j++ ) {
   275 		    maple_device_t dev = maple_get_device( i, j );
   276 		    if( dev != NULL ) {
   277 			if( j == 0 )
   278 			    fprintf( f, "Device %d = %s\n", i, dev->device_class->name );
   279 			else 
   280 			    fprintf( f, "Subdevice %d = %s\n", j, dev->device_class->name );
   281 			entry = dev->get_config(dev);
   282 			while( entry->key != NULL ) {
   283 			    fprintf( f, "%*c%s = %s\n", j==0?4:8, ' ',entry->key, entry->value );
   284 			    entry++;
   285 			}
   286 		    }
   287 		}
   288 	    }
   289 	}
   290 	fprintf( f, "\n" );
   291 	group++;
   292     }
   293     return TRUE;
   294 }
.