-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.c
78 lines (69 loc) · 2.36 KB
/
configuration.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "configuration.h"
#include "string.h"
#include "yawn.h"
#include "action.h"
char * xdg_config_dir( int directory_type ) {
char * config_dir;
if ( directory_type == CONFIG_DIR_HOME ) {
config_dir = getenv( "XDG_CONFIG_HOME" );
if ( config_dir == NULL ) {
config_dir = getenv( "HOME" );
if ( config_dir == NULL ) {
die( "Please define HOME environment variable.\n" );
}
}
}
else {
config_dir = getenv( "XDG_CONFIG_DIRS" );
if ( config_dir == NULL ) {
config_dir = (char*)malloc( 4 * sizeof( char ) );
strcpy( config_dir, "/etc" );
}
}
return config_dir;
}
char * xdg_config_path( char * filename, int directory_type ) {
char * config_path, * basedir = xdg_config_dir( directory_type );
struct stat st;
config_path = (char*)malloc( ( strlen( basedir ) + strlen( "/yawn/" ) + strlen( filename ) ) * sizeof( char ) );
sprintf( config_path, "%s/yawn/%s", basedir, filename );
if ( stat( config_path, &st ) != 0 ) {
if ( directory_type == CONFIG_DIR_HOME ) {
// local configuration file not found. Try reading system configuration file instead.
return xdg_config_path( filename, CONFIG_DIR_SYSTEM );
}
else {
printf( "System configuration file %s not found. Please reinstall yawn.\n", config_path );
exit( 1 );
}
}
return config_path;
}
void configuration_read( char * filename ) {
int i, argc, (*callback)( int, char ** );
char line[ 256 ], section[ 256 ], * argv[ 128 ], *config_path;
config_path = xdg_config_path( "yawn.conf", CONFIG_DIR_HOME );
FILE * fp = fopen( config_path, "r" );
free( config_path );
while ( freadline( fp, line ) != EOF ) {
int length = strlen( line );
if ( !length || line[ 0 ] == '#' ) {
continue;
}
if ( line[ 0 ] == '[' && line[ length - 1 ] == ']' ) {
line[ length - 1 ] = '\0';
strcpy( section, line + 1 );
continue;
}
argc = string_split( line, " ", argv );
callback = get_action( section );
callback( argc, argv );
for ( i = 0; i < argc; ++i ) {
free( argv[ i ] );
}
}
fclose( fp );
}