1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * bonobo-activation-sysconf: a simple utility to manipulate |
---|
4 | * activation configuration files. |
---|
5 | * |
---|
6 | * Copyright (C) 2000 Eazel, Inc. |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License as |
---|
10 | * published by the Free Software Foundation; either version 2 of the |
---|
11 | * License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library 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 GNU |
---|
16 | * General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this library; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | * |
---|
22 | * Authors: Mathieu Lacage <mathieu@eazel.com> |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | #include "config.h" |
---|
27 | #include <string.h> |
---|
28 | #include <glib.h> |
---|
29 | #include <libxml/tree.h> |
---|
30 | #include <libxml/parser.h> |
---|
31 | #include <libxml/xmlmemory.h> |
---|
32 | #include <popt.h> /* popt :) */ |
---|
33 | |
---|
34 | #include <bonobo-activation/bonobo-activation.h> |
---|
35 | #include "bonobo-activation/bonobo-activation-i18n.h" |
---|
36 | #include "server/object-directory-config-file.h" |
---|
37 | |
---|
38 | static xmlDocPtr |
---|
39 | open_file (void) |
---|
40 | { |
---|
41 | char *config_file; |
---|
42 | xmlDocPtr doc; |
---|
43 | |
---|
44 | config_file = g_strconcat ( |
---|
45 | SERVER_CONFDIR, SERVER_CONFIG_FILE, NULL); |
---|
46 | |
---|
47 | doc = xmlParseFile (config_file); |
---|
48 | |
---|
49 | return doc; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | static void |
---|
54 | save_file (xmlDocPtr doc) |
---|
55 | { |
---|
56 | char *config_file; |
---|
57 | |
---|
58 | |
---|
59 | config_file = g_strconcat ( |
---|
60 | SERVER_CONFDIR, SERVER_CONFIG_FILE, NULL); |
---|
61 | if (xmlSaveFile (config_file, doc) == -1) { |
---|
62 | g_print (_("Could not save configuration file.\n")); |
---|
63 | g_print (_("Please, make sure you have permissions to write " |
---|
64 | "to '%s'.\n"), config_file); |
---|
65 | } else { |
---|
66 | g_print (_("Successfully wrote configuration file.\n")); |
---|
67 | } |
---|
68 | g_free (config_file); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | static void |
---|
73 | display_config_path (void) |
---|
74 | { |
---|
75 | char *config_file; |
---|
76 | |
---|
77 | config_file = g_strconcat ( |
---|
78 | SERVER_CONFDIR, SERVER_CONFIG_FILE, NULL); |
---|
79 | |
---|
80 | g_print (_("configuration file is:\n %s\n"), config_file); |
---|
81 | |
---|
82 | g_free (config_file); |
---|
83 | } |
---|
84 | |
---|
85 | static xmlNodePtr |
---|
86 | get_root_first_child (xmlDocPtr doc) |
---|
87 | { |
---|
88 | if (doc == NULL) |
---|
89 | return NULL; |
---|
90 | if (doc->xmlRootNode == NULL) |
---|
91 | return NULL; |
---|
92 | return doc->xmlRootNode->xmlChildrenNode; |
---|
93 | } |
---|
94 | |
---|
95 | static void |
---|
96 | add_directory (const char *directory) |
---|
97 | { |
---|
98 | xmlDocPtr doc; |
---|
99 | xmlNodePtr search_node; |
---|
100 | gboolean is_already_there; |
---|
101 | |
---|
102 | is_already_there = FALSE; |
---|
103 | doc = open_file (); |
---|
104 | |
---|
105 | /* make sure the directory we want to add is not already |
---|
106 | in the config file */ |
---|
107 | search_node = get_root_first_child (doc); |
---|
108 | while (search_node != NULL) { |
---|
109 | if (strcmp (search_node->name, "searchpath") == 0) { |
---|
110 | xmlNodePtr item_node; |
---|
111 | item_node = search_node->xmlChildrenNode; |
---|
112 | while (item_node != NULL) { |
---|
113 | if (strcmp (item_node->name, "item") == 0) { |
---|
114 | char *dir_path; |
---|
115 | dir_path = xmlNodeGetContent (item_node); |
---|
116 | if (strcmp (dir_path, directory) == 0) { |
---|
117 | is_already_there = TRUE; |
---|
118 | g_print (_("%s already in configuration file\n"), |
---|
119 | directory); |
---|
120 | |
---|
121 | } |
---|
122 | xmlFree (dir_path); |
---|
123 | } |
---|
124 | item_node = item_node->next; |
---|
125 | } |
---|
126 | } |
---|
127 | search_node = search_node->next; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | if (!is_already_there) { |
---|
132 | xmlNodePtr new_node; |
---|
133 | |
---|
134 | /* add the directory to the config file */ |
---|
135 | search_node = get_root_first_child (doc); |
---|
136 | |
---|
137 | if (search_node == NULL) |
---|
138 | g_print (_("there is not a properly structured configuration file\n")); |
---|
139 | else { |
---|
140 | /* go to the first searchpath node */ |
---|
141 | while (strcmp (search_node->name, "searchpath") != 0) { |
---|
142 | search_node = search_node->next; |
---|
143 | } |
---|
144 | new_node = xmlNewDocNode (doc, NULL, "item", directory); |
---|
145 | xmlAddChild (search_node, new_node); |
---|
146 | |
---|
147 | save_file (doc); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | xmlFreeDoc (doc); |
---|
152 | } |
---|
153 | |
---|
154 | static void |
---|
155 | remove_directory (const char *directory) |
---|
156 | { |
---|
157 | xmlDocPtr doc; |
---|
158 | xmlNodePtr search_node; |
---|
159 | |
---|
160 | doc = open_file (); |
---|
161 | |
---|
162 | search_node = get_root_first_child (doc); |
---|
163 | while (search_node != NULL) { |
---|
164 | if (strcmp (search_node->name, "searchpath") == 0) { |
---|
165 | xmlNodePtr item_node; |
---|
166 | item_node = search_node->xmlChildrenNode; |
---|
167 | while (item_node != NULL) { |
---|
168 | if (strcmp (item_node->name, "item") == 0) { |
---|
169 | char *dir_path; |
---|
170 | dir_path = xmlNodeGetContent (item_node); |
---|
171 | if (strcmp (dir_path, directory) == 0) { |
---|
172 | if (strcmp (dir_path, directory) == 0) { |
---|
173 | xmlDocPtr doc; |
---|
174 | doc = item_node->doc; |
---|
175 | xmlUnlinkNode (item_node); |
---|
176 | xmlFreeNode (item_node); |
---|
177 | save_file (doc); |
---|
178 | xmlFree (dir_path); |
---|
179 | return; |
---|
180 | } |
---|
181 | } |
---|
182 | xmlFree (dir_path); |
---|
183 | } |
---|
184 | item_node = item_node->next; |
---|
185 | } |
---|
186 | } |
---|
187 | search_node = search_node->next; |
---|
188 | } |
---|
189 | |
---|
190 | xmlFreeDoc (doc); |
---|
191 | } |
---|
192 | |
---|
193 | static void |
---|
194 | display_directories (void) |
---|
195 | { |
---|
196 | xmlDocPtr doc; |
---|
197 | xmlNodePtr search_node; |
---|
198 | |
---|
199 | doc = open_file (); |
---|
200 | |
---|
201 | g_print (_("Bonobo-activation configuration file contains:\n")); |
---|
202 | |
---|
203 | search_node = get_root_first_child (doc); |
---|
204 | while (search_node != NULL) { |
---|
205 | if (strcmp (search_node->name, "searchpath") == 0) { |
---|
206 | xmlNodePtr item_node; |
---|
207 | item_node = search_node->xmlChildrenNode; |
---|
208 | while (item_node != NULL) { |
---|
209 | if (strcmp (item_node->name, "item") == 0) { |
---|
210 | char *dir_path; |
---|
211 | dir_path = xmlNodeGetContent (item_node); |
---|
212 | g_print (" %s\n", dir_path); |
---|
213 | xmlFree (dir_path); |
---|
214 | } |
---|
215 | item_node = item_node->next; |
---|
216 | } |
---|
217 | } |
---|
218 | search_node = search_node->next; |
---|
219 | } |
---|
220 | xmlFreeDoc (doc); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | #define REMOVE_DIRECTORY_OPERATION 1 |
---|
226 | #define ADD_DIRECTORY_OPERATION 2 |
---|
227 | #define DISPLAY_DIRECTORIES_OPERATION 3 |
---|
228 | #define DISPLAY_CONFIG_PATH_OPERATION 4 |
---|
229 | |
---|
230 | struct poptOption oaf_sysconf_popt_options[] = { |
---|
231 | {"remove-directory", '\0', POPT_ARG_STRING, NULL, |
---|
232 | REMOVE_DIRECTORY_OPERATION, |
---|
233 | N_("Directory to remove from configuration file"), N_("directory path")}, |
---|
234 | {"add-directory", '\0', POPT_ARG_STRING, NULL, |
---|
235 | ADD_DIRECTORY_OPERATION, |
---|
236 | N_("Directory to add to configuration file"), N_("directory path")}, |
---|
237 | {"display-directories", '\0', POPT_ARG_NONE, NULL, |
---|
238 | DISPLAY_DIRECTORIES_OPERATION, |
---|
239 | N_("Display directories in configuration file"), NULL}, |
---|
240 | {"config-file-path", '\0', POPT_ARG_NONE, NULL, |
---|
241 | DISPLAY_CONFIG_PATH_OPERATION, |
---|
242 | N_("Display path to configuration file"), NULL}, |
---|
243 | POPT_AUTOHELP |
---|
244 | {NULL} |
---|
245 | }; |
---|
246 | |
---|
247 | int main (int argc, char **argv) |
---|
248 | { |
---|
249 | poptContext context; |
---|
250 | int popt_option; |
---|
251 | |
---|
252 | /* init nls */ |
---|
253 | bindtextdomain (PACKAGE, SERVER_LOCALEDIR); |
---|
254 | textdomain (PACKAGE); |
---|
255 | |
---|
256 | /* init popt */ |
---|
257 | context = poptGetContext ("oaf-sysconf", argc, (const char **)argv, |
---|
258 | oaf_sysconf_popt_options, 0); |
---|
259 | |
---|
260 | popt_option = poptGetNextOpt (context); |
---|
261 | if (popt_option <= -1) { |
---|
262 | poptPrintHelp (context, stderr, 0); |
---|
263 | poptFreeContext (context); |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | while (popt_option != -1) { |
---|
268 | const char *arg; |
---|
269 | arg = (const char *)poptGetOptArg (context); |
---|
270 | switch (popt_option) { |
---|
271 | |
---|
272 | case REMOVE_DIRECTORY_OPERATION: |
---|
273 | remove_directory (arg); |
---|
274 | break; |
---|
275 | |
---|
276 | case ADD_DIRECTORY_OPERATION: |
---|
277 | add_directory (arg); |
---|
278 | break; |
---|
279 | |
---|
280 | case DISPLAY_DIRECTORIES_OPERATION: |
---|
281 | display_directories (); |
---|
282 | break; |
---|
283 | |
---|
284 | case DISPLAY_CONFIG_PATH_OPERATION: |
---|
285 | display_config_path (); |
---|
286 | break; |
---|
287 | |
---|
288 | } |
---|
289 | popt_option = poptGetNextOpt (context); |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | poptFreeContext (context); |
---|
294 | |
---|
295 | return 0; |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | |
---|