'C' reloads configuration.
[shell-fm.git] / source / settings.c
blob4c9d1d37888307b50417822cc50534f57350746a
1 /*
2 Copyright (C) 2006 by Jonas Kramer
3 Published under the terms of the GNU General Public License (GPL).
4 */
6 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
17 #include "hash.h"
18 #include "getln.h"
19 #include "settings.h"
21 struct hash rc; /* settings read from ~/.shell-fm.rc */
23 int settings(const char * path, int first) {
24 int retval = !0;
25 FILE * fd = fopen(path, "r");
26 unsigned nline = 0;
27 char * line = NULL;
28 unsigned size = 0;
30 if(first)
31 memset(& rc, 0, sizeof(struct hash));
33 if(!fd)
34 return 0;
36 while(!feof(fd)) {
37 char * ptr;
39 if(!getln(& line, & size, fd))
40 continue;
42 ++nline;
44 if(strlen(line) < 2)
45 continue;
47 ptr = line;
48 while((ptr = strchr(ptr, '#')) != NULL)
49 if(ptr == line || ptr[-1] != '\\')
50 * ptr = (char) 0;
51 else
52 if(ptr[-1] == '\\') {
53 unsigned restlen = strlen(ptr);
54 ptr[-1] = '#';
55 if(restlen > 1)
56 memmove(ptr, ptr + 1, restlen - 1);
57 ptr[restlen] = (char) 0;
60 if(strlen(line) > 1) {
61 char key[64] = { 0 }, value[256] = { 0 };
63 if(sscanf(line, "%63[^= \t] = %255[^\r\n]", key, value) == 2)
64 set(& rc, key, value);
65 else {
66 fprintf(stderr, "%s, line %d invalid.\n", path, nline);
67 retval = 0;
72 fclose(fd);
73 if(size)
74 free(line);
76 return retval;
80 void makercd(void) {
81 mkdir(rcpath(""), 0755);
82 mkdir(rcpath("cache"), 0755);
86 const char * rcpath(const char * file) {
87 static char path[4096] = { 0 };
88 memset(path, 0, sizeof(path));
90 if(getenv("SHELL_FM_HOME") != NULL) {
91 snprintf(path, sizeof(path), "%s/%s", getenv("SHELL_FM_HOME"), file);
93 else {
94 snprintf(path, sizeof(path), "%s/.shell-fm/%s", getenv("HOME"), file);
97 return path;