forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.c
112 lines (98 loc) · 2.99 KB
/
settings.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* settings.c -- goaccess configuration
* Copyright (C) 2010 by Gerardo Orellana <[email protected]>
* GoAccess - An ncurses apache weblog analyzer & interactive viewer
*
* 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.
*
* A copy of the GNU General Public License is attached to this
* source distribution for its full text.
*
* Visit http://goaccess.prosoftcorp.com for new releases.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "commons.h"
static void
set_conf_vars (int key, char *val)
{
switch (key) {
case 1:
color_scheme = atoi (val);
break;
}
}
/*###TODO: allow extra values for every key
* separated by a delimeter */
int
parse_conf_file ()
{
char *val, *c;
int key;
char *user_home;
user_home = getenv ("HOME");
if (user_home == NULL)
user_home = "";
char *path = malloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
if (path == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
"Unable to allocate memory for new window.");
sprintf (path, "%s/.goaccessrc", user_home);
char *conf_file = path;
FILE *file = fopen (conf_file, "r");
if (file == NULL) {
free (path);
return -1;
}
char line[512];
while (fgets (line, sizeof line, file) != NULL) {
int i;
for (i = 0; i < keywords_size (); i++) {
if ((strstr (line, conf_keywords[i][1])) != NULL)
key = atoi (conf_keywords[i][0]);
}
if ((val = strchr (line, ' ')) == NULL) {
free (path);
return -1;
}
for (c = val; *c; c++) {
/* get everything after space */
if (!isspace (c[0])) {
set_conf_vars (key, c);
break;
}
}
}
fclose (file);
free (path);
return 0;
}
void
write_conf_file ()
{
char *user_home;
user_home = getenv ("HOME");
if (user_home == NULL)
user_home = "";
char *path = malloc (snprintf (NULL, 0, "%s/.goaccessrc", user_home) + 1);
if (path == NULL)
error_handler (__PRETTY_FUNCTION__, __FILE__, __LINE__,
"Unable to allocate memory for new window.");
sprintf (path, "%s/.goaccessrc", user_home);
FILE *file;
file = fopen (path, "w");
/* key->value(s) */
fprintf (file, "color_scheme %d", color_scheme);
fclose (file);
free (path);
}