1 /* $Id$ */
2 /* Copyright (c) 2014-2015 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS System libSystem */
4 /* This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
15
16
17
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include "System/config.h"
22 #include "System/error.h"
23
24 #ifndef PROGNAME
25 # define PROGNAME "configctl"
26 #endif
27
28
29 /* configctl */
30 /* private */
31 /* prototypes */
32 static int _configctl(int verbose, int write, char const * filename, int argc,
33 char * argv[]);
34 static int _configctl_list(char const * filename);
35
36 static void _configctl_print(int verbose, char const * section,
37 char const * variable, char const * value);
38
39 static int _configctl_error(char const * progname, int ret);
40 static int _usage(void);
41
42
43 /* functions */
44 /* configctl */
45 static int _configctl_do(Config * config, int verbose, char const * section,
46 char const * key, char const * value);
47
_configctl(int verbose,int write,char const * filename,int argc,char * argv[])48 static int _configctl(int verbose, int write, char const * filename, int argc,
49 char * argv[])
50 {
51 int ret = 0;
52 Config * config;
53 int i;
54 char * section;
55 char * key;
56 char * value = NULL;
57
58 if((config = config_new()) == NULL)
59 return -_configctl_error(PROGNAME, 1);
60 if(config_load(config, filename) != 0)
61 {
62 config_delete(config);
63 return -_configctl_error(PROGNAME, 1);
64 }
65 for(i = 0; i < argc; i++)
66 {
67 section = argv[i];
68 if((key = strchr(section, '.')) == NULL)
69 {
70 key = section;
71 section = NULL;
72 }
73 else
74 *(key++) = '\0';
75 if(write && (value = strchr(key, '=')) != NULL)
76 *(value++) = '\0';
77 ret |= _configctl_do(config, verbose, section, key, value);
78 }
79 if(ret == 0 && write && config_save(config, filename) != 0)
80 ret = -_configctl_error(PROGNAME, 1);
81 config_delete(config);
82 return ret;
83 }
84
_configctl_do(Config * config,int verbose,char const * section,char const * key,char const * value)85 static int _configctl_do(Config * config, int verbose, char const * section,
86 char const * key, char const * value)
87 {
88 char const * p;
89
90 if(value == NULL)
91 {
92 if((p = config_get(config, section, key)) == NULL)
93 return -_configctl_error(PROGNAME, 1);
94 _configctl_print(verbose, section, key, p);
95 }
96 else
97 {
98 if(config_set(config, section, key, value) != 0)
99 return -_configctl_error(PROGNAME, 1);
100 _configctl_print(verbose, section, key, value);
101 }
102 return 0;
103 }
104
105
106 /* configctl_list */
107 static void _list_foreach(String const * section, void * data);
108 static void _list_foreach_section(String const * variable, String const * value,
109 void * data);
110
_configctl_list(char const * filename)111 static int _configctl_list(char const * filename)
112 {
113 Config * config;
114
115 if((config = config_new()) == NULL)
116 return -_configctl_error(PROGNAME, 1);
117 if(config_load(config, filename) != 0)
118 {
119 config_delete(config);
120 return -_configctl_error(PROGNAME, 1);
121 }
122 config_foreach(config, _list_foreach, config);
123 config_delete(config);
124 return 0;
125 }
126
_list_foreach(String const * section,void * data)127 static void _list_foreach(String const * section, void * data)
128 {
129 Config * config = data;
130
131 config_foreach_section(config, section, _list_foreach_section,
132 (void *)section);
133 }
134
_list_foreach_section(String const * variable,String const * value,void * data)135 static void _list_foreach_section(String const * variable, String const * value,
136 void * data)
137 {
138 String const * section = data;
139
140 _configctl_print(1, section, variable, value);
141 }
142
143
144 /* configctl_print */
_configctl_print(int verbose,char const * section,char const * variable,char const * value)145 static void _configctl_print(int verbose, char const * section,
146 char const * variable, char const * value)
147 {
148 if(verbose < 0)
149 return;
150 printf("%s%s%s%s%s\n", (verbose > 0 && section != NULL) ? section : "",
151 (verbose > 0 && section != NULL && section[0] != '\0')
152 ? "." : "",
153 (verbose > 0) ? variable : "",
154 (verbose > 0) ? "=" : "",
155 (value != NULL) ? value : "");
156 }
157
158
159 /* configctl_error */
_configctl_error(char const * progname,int ret)160 static int _configctl_error(char const * progname, int ret)
161 {
162 error_print(progname);
163 return ret;
164 }
165
166
167 /* usage */
_usage(void)168 static int _usage(void)
169 {
170 fputs("Usage: " PROGNAME " -f filename -a\n"
171 " " PROGNAME " -f filename [-qv] [section.]key...\n"
172 " " PROGNAME " -w -f filename [-qv] [section.]key[=value]...\n"
173 " -a\tList every key of every section available\n"
174 " -f\tFilename to parse or update\n"
175 " -q\tQuiet mode\n"
176 " -v\tVerbose mode\n"
177 " -w\tSet and save values\n", stderr);
178 return 1;
179 }
180
181
182 /* public */
183 /* functions */
184 /* main */
main(int argc,char * argv[])185 int main(int argc, char * argv[])
186 {
187 int o;
188 int list = 0;
189 int verbose = 0;
190 int write = 0;
191 char const * filename = NULL;
192
193 while((o = getopt(argc, argv, "af:qvw")) != -1)
194 switch(o)
195 {
196 case 'a':
197 list = 1;
198 break;
199 case 'f':
200 filename = optarg;
201 break;
202 case 'q':
203 verbose = -1;
204 break;
205 case 'v':
206 verbose = 1;
207 break;
208 case 'w':
209 write = 1;
210 break;
211 default:
212 return _usage();
213 }
214 if(list)
215 {
216 if(verbose != 0 || write != 0 || filename == NULL
217 || optind != argc)
218 return _usage();
219 return _configctl_list(filename) ? 0 : 2;
220 }
221 if(filename == NULL || optind == argc)
222 return _usage();
223 return (_configctl(verbose, write, filename, argc - optind,
224 &argv[optind]) == 0) ? 0 : 2;
225 }
226