|
Line 0
Link Here
|
|
|
1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2008 Hans Petter Jansson <hpj@novell.com> |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
/* Parser for shell-script-like key-value files. Far from complete, but |
| 22 |
* deals with a couple of common shell oddities. For instance, the following |
| 23 |
* are parsed correctly: |
| 24 |
* |
| 25 |
* KEY=value\0 |
| 26 |
* KEY = value#comment\0 |
| 27 |
* KEY = " value with spaces" \0 |
| 28 |
* KEY = ' it\'s a value with "embedded" quotes'\0 |
| 29 |
* KEY = "if quotes aren't closed, we assume the string ends at EOL\0 |
| 30 |
* |
| 31 |
* It should be good enough for the config files in /etc/sysconfig/. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "config.h" |
| 35 |
|
| 36 |
#include <unistd.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include <glib.h> |
| 40 |
#include <glib/gi18n.h> |
| 41 |
|
| 42 |
#include "gdm-sysconfig.h" |
| 43 |
|
| 44 |
#define SPACE_CHARS " \t" |
| 45 |
#define KEY_ALLOW_CHARS "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" |
| 46 |
|
| 47 |
static gchar ** |
| 48 |
load_settings_file (const gchar *file_name) |
| 49 |
{ |
| 50 |
GIOChannel *channel; |
| 51 |
GPtrArray *lines; |
| 52 |
gchar *str; |
| 53 |
|
| 54 |
g_debug ("Loading settings from %s", file_name); |
| 55 |
|
| 56 |
channel = g_io_channel_new_file (file_name, "r", NULL); |
| 57 |
if (!channel) { |
| 58 |
g_debug ("Failed to open %s", file_name); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
lines = g_ptr_array_new (); |
| 63 |
|
| 64 |
while (g_io_channel_read_line (channel, &str, NULL, NULL, NULL) != G_IO_STATUS_EOF) { |
| 65 |
if (str) { |
| 66 |
gchar *p0; |
| 67 |
|
| 68 |
/* Remove line separators */ |
| 69 |
|
| 70 |
for (p0 = str + strlen (str) - 1; p0 >= str && strchr ("\r\n", *p0); p0--) |
| 71 |
*p0 = '\0'; |
| 72 |
|
| 73 |
g_ptr_array_add (lines, str); |
| 74 |
g_debug ("%s", str); |
| 75 |
} else { |
| 76 |
g_ptr_array_add (lines, g_strdup ("")); |
| 77 |
g_debug ("", str); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
g_io_channel_shutdown (channel, FALSE, NULL); |
| 82 |
g_io_channel_unref (channel); |
| 83 |
|
| 84 |
g_ptr_array_add (lines, NULL); |
| 85 |
|
| 86 |
return (gchar **) g_ptr_array_free (lines, FALSE); |
| 87 |
} |
| 88 |
|
| 89 |
static gboolean |
| 90 |
save_settings_file (const gchar *file_name, gchar **lines) |
| 91 |
{ |
| 92 |
GIOStatus last_status = G_IO_STATUS_ERROR; |
| 93 |
GIOChannel *channel = NULL; |
| 94 |
gchar *temp_file_name; |
| 95 |
gint i; |
| 96 |
|
| 97 |
temp_file_name = g_strdup_printf ("%s.new.%u", file_name, g_random_int ()); |
| 98 |
|
| 99 |
channel = g_io_channel_new_file (temp_file_name, "w", NULL); |
| 100 |
if (!channel) |
| 101 |
goto out; |
| 102 |
|
| 103 |
if (!lines) |
| 104 |
goto out; |
| 105 |
|
| 106 |
for (i = 0; lines [i]; i++) { |
| 107 |
gsize bytes_written; |
| 108 |
|
| 109 |
if (lines [i] [0] != '\0') |
| 110 |
last_status = g_io_channel_write_chars (channel, |
| 111 |
lines [i], strlen (lines [i]), |
| 112 |
&bytes_written, |
| 113 |
NULL); |
| 114 |
|
| 115 |
if (last_status != G_IO_STATUS_NORMAL) |
| 116 |
break; |
| 117 |
|
| 118 |
last_status = g_io_channel_write_unichar (channel, '\n', NULL); |
| 119 |
|
| 120 |
if (last_status != G_IO_STATUS_NORMAL) |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
out: |
| 125 |
if (channel) { |
| 126 |
g_io_channel_shutdown (channel, FALSE, NULL); |
| 127 |
g_io_channel_unref (channel); |
| 128 |
} |
| 129 |
|
| 130 |
if (last_status == G_IO_STATUS_NORMAL && g_rename (temp_file_name, file_name) != 0) |
| 131 |
last_status = G_IO_STATUS_ERROR; |
| 132 |
|
| 133 |
g_free (temp_file_name); |
| 134 |
return last_status == G_IO_STATUS_NORMAL ? TRUE : FALSE; |
| 135 |
} |
| 136 |
|
| 137 |
static const gchar * |
| 138 |
skip_from_start_to_key (const gchar *line) |
| 139 |
{ |
| 140 |
const gchar *p0; |
| 141 |
|
| 142 |
/* Skip initial space */ |
| 143 |
|
| 144 |
p0 = line + strspn (line, SPACE_CHARS); |
| 145 |
|
| 146 |
/* Ignore comments and other junk */ |
| 147 |
|
| 148 |
if (*p0 && strchr (KEY_ALLOW_CHARS, *p0)) |
| 149 |
return p0; |
| 150 |
|
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
static const gchar * |
| 155 |
skip_from_start_to_value_of_key (const gchar *line, const gchar *key_normal, gint key_len) |
| 156 |
{ |
| 157 |
const gchar *p0, *p1; |
| 158 |
gchar *potential_key_normal; |
| 159 |
gboolean result; |
| 160 |
|
| 161 |
p0 = skip_from_start_to_key (line); |
| 162 |
if (!p0) |
| 163 |
return NULL; |
| 164 |
|
| 165 |
/* There's at least one key-like character, figure out how many */ |
| 166 |
|
| 167 |
p1 = p0 + strspn (p0, KEY_ALLOW_CHARS); |
| 168 |
|
| 169 |
/* Is this the key we're looking for? */ |
| 170 |
|
| 171 |
if (p1 - p0 != key_len) |
| 172 |
return NULL; |
| 173 |
|
| 174 |
potential_key_normal = g_ascii_strdown (p0, p1 - p0); |
| 175 |
result = strcmp (key_normal, potential_key_normal) == 0 ? TRUE : FALSE; |
| 176 |
g_free (potential_key_normal); |
| 177 |
|
| 178 |
if (!result) |
| 179 |
return NULL; |
| 180 |
|
| 181 |
/* It's the right key; skip over key-value separator */ |
| 182 |
|
| 183 |
p0 = p1 + strspn (p1, SPACE_CHARS); |
| 184 |
if (*p0 != '=') |
| 185 |
return NULL; |
| 186 |
|
| 187 |
p0++; |
| 188 |
p0 += strspn (p0, SPACE_CHARS); |
| 189 |
|
| 190 |
return p0; |
| 191 |
} |
| 192 |
|
| 193 |
static const gchar * |
| 194 |
skip_over_value (const gchar *value_start, gchar *quotes_out) |
| 195 |
{ |
| 196 |
const gchar *p0 = value_start; |
| 197 |
const gchar *p1; |
| 198 |
gchar quotes; |
| 199 |
|
| 200 |
/* Is the value quoted? */ |
| 201 |
|
| 202 |
quotes = *p0; |
| 203 |
if (quotes == '\'' || quotes == '"') { |
| 204 |
/* Quoted sequence opened; find closing quote, but skip over escaped ones. If |
| 205 |
* there's no closing quote on this line, assume the EOL closes it. */ |
| 206 |
|
| 207 |
*quotes_out = quotes; |
| 208 |
|
| 209 |
p1 = p0; |
| 210 |
do { |
| 211 |
p1++; |
| 212 |
p1 = strchr (p1, quotes); |
| 213 |
if (!p1) { |
| 214 |
/* Hit EOL */ |
| 215 |
|
| 216 |
p1 = p0 + strlen (p0) - 1; |
| 217 |
break; |
| 218 |
} |
| 219 |
} while (*(p1 - 1) == '\\'); |
| 220 |
} else { |
| 221 |
/* No quotes; look for comment or EOL */ |
| 222 |
|
| 223 |
*quotes_out = 0; |
| 224 |
|
| 225 |
p1 = strchr (p0, '#'); |
| 226 |
if (!p1) |
| 227 |
p1 = p0 + strlen (p0); |
| 228 |
|
| 229 |
for (p1--; p1 >= p0; p1--) |
| 230 |
if (!strchr (SPACE_CHARS, *p1)) |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
return p1 + 1; |
| 235 |
} |
| 236 |
|
| 237 |
static gchar * |
| 238 |
get_value_of_key (const gchar *line, const gchar *key_normal, gint key_len) |
| 239 |
{ |
| 240 |
const gchar *p0, *p1; |
| 241 |
gchar quotes; |
| 242 |
gchar *value; |
| 243 |
gchar *temp; |
| 244 |
|
| 245 |
p0 = skip_from_start_to_value_of_key (line, key_normal, key_len); |
| 246 |
if (!p0) |
| 247 |
return NULL; |
| 248 |
|
| 249 |
p1 = skip_over_value (p0, "es); |
| 250 |
|
| 251 |
if (quotes != 0) { |
| 252 |
if (p1 - p0 > 2) { |
| 253 |
temp = g_strndup (p0 + 1, p1 - p0 - 2); |
| 254 |
value = g_strcompress (temp); |
| 255 |
g_free (temp); |
| 256 |
} else { |
| 257 |
value = g_strdup (""); |
| 258 |
} |
| 259 |
} else { |
| 260 |
temp = g_strndup (p0, p1 - p0); |
| 261 |
value = g_strcompress (temp); |
| 262 |
g_free (temp); |
| 263 |
g_strchomp (value); |
| 264 |
} |
| 265 |
|
| 266 |
return value; |
| 267 |
} |
| 268 |
|
| 269 |
static gchar * |
| 270 |
get_value (const gchar **lines, const gchar *key) |
| 271 |
{ |
| 272 |
gchar *value = NULL; |
| 273 |
gchar *key_normal; |
| 274 |
gint key_len; |
| 275 |
gint i; |
| 276 |
|
| 277 |
g_debug ("Getting value of %s", key); |
| 278 |
|
| 279 |
if (!lines) { |
| 280 |
g_debug ("Missing configuration data"); |
| 281 |
return NULL; |
| 282 |
} |
| 283 |
|
| 284 |
key_normal = g_ascii_strdown (key, -1); |
| 285 |
key_len = strlen (key_normal); |
| 286 |
|
| 287 |
for (i = 0; lines [i]; i++) { |
| 288 |
value = get_value_of_key (lines [i], key_normal, key_len); |
| 289 |
if (value) |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
g_free (key_normal); |
| 294 |
|
| 295 |
g_debug ("Got value of %s: %s", key, value); |
| 296 |
|
| 297 |
return value; |
| 298 |
} |
| 299 |
|
| 300 |
static gchar * |
| 301 |
set_value_of_key (const gchar *line, const gchar *key_normal, gint key_len, const gchar *key, const gchar *value) |
| 302 |
{ |
| 303 |
const gchar *p0, *p1, *p2; |
| 304 |
gchar quotes; |
| 305 |
gchar *escaped_value; |
| 306 |
gint escaped_value_len; |
| 307 |
gchar *new_line; |
| 308 |
gint len; |
| 309 |
|
| 310 |
p0 = skip_from_start_to_value_of_key (line, key_normal, key_len); |
| 311 |
if (!p0) |
| 312 |
return NULL; |
| 313 |
|
| 314 |
escaped_value = g_strescape (value, ""); |
| 315 |
escaped_value_len = strlen (escaped_value); |
| 316 |
|
| 317 |
p1 = skip_over_value (p0, "es); |
| 318 |
p2 = p1 + strlen (p1); |
| 319 |
len = (p0 - line) + escaped_value_len + (p2 - p1); |
| 320 |
|
| 321 |
new_line = g_malloc (len + 1); |
| 322 |
memcpy (new_line, line, p0 - line); |
| 323 |
memcpy (new_line + (p0 - line), escaped_value, escaped_value_len); |
| 324 |
memcpy (new_line + (p0 - line) + escaped_value_len, p1, p2 - p1); |
| 325 |
|
| 326 |
*(new_line + len - 1) = '\0'; |
| 327 |
|
| 328 |
g_free (escaped_value); |
| 329 |
|
| 330 |
return new_line; |
| 331 |
} |
| 332 |
|
| 333 |
static gboolean |
| 334 |
set_value (gchar **lines, const gchar *key, const gchar *value) |
| 335 |
{ |
| 336 |
gboolean result = FALSE; |
| 337 |
gchar *key_normal; |
| 338 |
gint key_len; |
| 339 |
gint i; |
| 340 |
|
| 341 |
if (!lines) |
| 342 |
return FALSE; |
| 343 |
|
| 344 |
key_normal = g_ascii_strdown (key, -1); |
| 345 |
key_len = strlen (key_normal); |
| 346 |
|
| 347 |
for (i = 0; lines [i]; i++) { |
| 348 |
gchar *new_line; |
| 349 |
|
| 350 |
new_line = set_value_of_key (lines [i], key_normal, key_len, key, value); |
| 351 |
if (new_line) { |
| 352 |
g_free (lines [i]); |
| 353 |
lines [i] = new_line; |
| 354 |
result = TRUE; |
| 355 |
break; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
g_free (key_normal); |
| 360 |
|
| 361 |
return result; |
| 362 |
} |
| 363 |
|
| 364 |
gchar ** |
| 365 |
gdm_sysconfig_load_file (const gchar *file_name) |
| 366 |
{ |
| 367 |
g_return_val_if_fail (file_name != NULL, NULL); |
| 368 |
|
| 369 |
return load_settings_file (file_name); |
| 370 |
} |
| 371 |
|
| 372 |
gboolean |
| 373 |
gdm_sysconfig_save_file (const gchar *file_name, const gchar **sysconfig) |
| 374 |
{ |
| 375 |
g_return_val_if_fail (file_name != NULL, FALSE); |
| 376 |
g_return_val_if_fail (sysconfig != NULL, FALSE); |
| 377 |
|
| 378 |
return save_settings_file (file_name, sysconfig); |
| 379 |
} |
| 380 |
|
| 381 |
gchar * |
| 382 |
gdm_sysconfig_get_value (const gchar **sysconfig, const gchar *key) |
| 383 |
{ |
| 384 |
g_return_val_if_fail (sysconfig != NULL, NULL); |
| 385 |
g_return_val_if_fail (key != NULL, NULL); |
| 386 |
|
| 387 |
return get_value (sysconfig, key); |
| 388 |
} |
| 389 |
|
| 390 |
gboolean |
| 391 |
gdm_sysconfig_set_value (gchar **sysconfig, const gchar *key, const gchar *value) |
| 392 |
{ |
| 393 |
g_return_val_if_fail (sysconfig != NULL, FALSE); |
| 394 |
g_return_val_if_fail (key != NULL, FALSE); |
| 395 |
g_return_val_if_fail (value != NULL, FALSE); |
| 396 |
|
| 397 |
return set_value (sysconfig, key, value); |
| 398 |
} |
| 399 |
|
| 400 |
gchar * |
| 401 |
gdm_sysconfig_load_value (const gchar *file_name, const gchar *key) |
| 402 |
{ |
| 403 |
gchar **lines; |
| 404 |
gchar *value; |
| 405 |
|
| 406 |
g_return_val_if_fail (file_name != NULL, NULL); |
| 407 |
g_return_val_if_fail (key != NULL, NULL); |
| 408 |
|
| 409 |
lines = load_settings_file (file_name); |
| 410 |
if (!lines) |
| 411 |
return NULL; |
| 412 |
|
| 413 |
value = get_value (lines, key); |
| 414 |
|
| 415 |
g_strfreev (lines); |
| 416 |
return value; |
| 417 |
} |
| 418 |
|
| 419 |
gboolean |
| 420 |
gdm_sysconfig_save_value (const gchar *file_name, const gchar *key, const gchar *value) |
| 421 |
{ |
| 422 |
gchar **lines; |
| 423 |
gboolean result; |
| 424 |
|
| 425 |
g_return_val_if_fail (file_name != NULL, FALSE); |
| 426 |
g_return_val_if_fail (key != NULL, FALSE); |
| 427 |
g_return_val_if_fail (value != NULL, FALSE); |
| 428 |
|
| 429 |
lines = load_settings_file (file_name); |
| 430 |
if (!lines) |
| 431 |
return FALSE; |
| 432 |
|
| 433 |
result = set_value (lines, key, value); |
| 434 |
if (result) |
| 435 |
result = save_settings_file (file_name, lines); |
| 436 |
|
| 437 |
g_strfreev (lines); |
| 438 |
return result; |
| 439 |
} |