aboutsummaryrefslogtreecommitdiffstats
path: root/src/shvar.c
blob: 9b1d8f92e0cd8908ffc98acb1216673d9fc8eafc (plain)
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
Diffstat (limited to 'po/lt.po')
-rw-r--r--po/lt.po14
1 files changed, 9 insertions, 5 deletions
diff --git a/po/lt.po b/po/lt.po
index 7ae9ac70..4876d81e 100644
--- a/po/lt.po
+++ b/po/lt.po
@@ -1,17 +1,17 @@
-#
+#
# Translators:
# Dimitris Glezos <glezos@indifex.com>, 2011.
msgid ""
msgstr ""
"Project-Id-Version: fedora-initscripts\n"
-"PO-Revision-Date: 2012-02-14 14:49+0000\n"
+"PO-Revision-Date: 2012-03-06 08:28+0000\n"
"Last-Translator: notting <notting@redhat.com>\n"
-"Language-Team: Lithuanian (http://www.transifex.net/projects/p/fedora/language/lt/)\n"
+"Language-Team: Lithuanian (http://www.transifex.com/projects/p/fedora/language/lt/)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8-bit\n"
"Language: lt\n"
-"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2)\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
#: /etc/rc.d/init.d/ez-ipupdate:76
msgid "Reloading $prog for $ez_name: "
@@ -1105,6 +1105,10 @@ msgstr ""
msgid "CTDB is already running"
msgstr ""
+#: /etc/ppp/ip-up.ipv6to4:186
+msgid "Error occured while calculating the IPv6to4 prefix"
+msgstr ""
+
#: /etc/rc.d/init.d/fetch-crl-boot:57
msgid "fetch-crl-boot lockfile present"
msgstr ""
@@ -1206,7 +1210,7 @@ msgid ""
"reload|condrestart|once|genconfig}"
msgstr ""
-#: /etc/sysconfig/network-scripts/ifup-ipv6:296 /etc/ppp/ip-up.ipv6to4:186
+#: /etc/sysconfig/network-scripts/ifup-ipv6:296
msgid "Error occurred while calculating the IPv6to4 prefix"
msgstr ""
='#n218'>218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/*
 * shvar.c
 *
 * Implementation of non-destructively reading/writing files containing
 * only shell variable declarations and full-line comments.
 *
 * Includes explicit inheritance mechanism intended for use with
 * Red Hat Linux ifcfg-* files.  There is no protection against
 * inheritance loops; they will generally cause stack overflows.
 * Furthermore, they are only intended for one level of inheritance;
 * the value setting algorithm assumes this.
 *
 * Copyright 1999 Red Hat, Inc.
 *
 * This 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "shvar.h"

/* Open the file <name>, return shvarFile on success, NULL on failure */
shvarFile *
svNewFile(char *name)
{
    shvarFile *s = NULL;
    int closefd = 0;

    s = g_malloc0(sizeof(shvarFile));

    s->fd = open(name, O_RDWR); /* NOT O_CREAT */
    if (s->fd == -1) {
	/* try read-only */
	s->fd = open(name, O_RDONLY); /* NOT O_CREAT */
	if (s->fd) closefd = 1;
    }
    s->fileName = g_strdup(name);

    if (s->fd != -1) {
	struct stat buf;
	char *p, *q;

	if (fstat(s->fd, &buf) < 0) goto bail;
	s->arena = g_malloc0(buf.st_size + 1);

	if (read(s->fd, s->arena, buf.st_size) < 0) goto bail;

	for(p = s->arena; (q = strchr(p, '\n')) != NULL; p = q + 1) {
		s->lineList = g_list_append(s->lineList, g_strndup(p, q - p));
	}

	if (closefd) {
	    close(s->fd);
	    s->fd = -1;
	}
    }

    return s;

bail:
    if (s->fd != -1) close(s->fd);
    if (s->arena) g_free (s->arena);
    if (s->fileName) g_free (s->fileName);
    g_free (s);
    return NULL;
}

/* remove escaped characters in place */
static void
unescape(char *s) {
    int len, i;

    len = strlen(s);
    if ((s[0] == '"' || s[0] == '\'') && s[0] == s[len-1]) {
	i = len - 2;
	memmove(s, s+1, i);
	s[i+1] = '\0';
	len = i;
    }
    for (i = 0; i < len; i++) {
	if (s[i] == '\\') {
	    memmove(s+i, s+i+1, len-(i+1));
	    len--;
	}
	s[len] = '\0';
    }
}


/* create a new string with all necessary characters escaped.
 * caller must free returned string
 */
static const char escapees[] = "\"'\\$~`";	/* must be escaped */
static const char spaces[] = " \t";		/* only require "" */
static char *
escape(char *s) {
    char *new;
    int i, j, mangle = 0, space = 0;
    int newlen, slen;
    static int esclen, splen;

    if (!esclen) esclen = strlen(escapees);
    if (!splen) splen = strlen(spaces);
    for (i = 0; i < esclen; i++) {
	if (strchr(s, escapees[i])) mangle++;
    }
    for (i = 0; i < splen; i++) {
	if (strchr(s, spaces[i])) space++;
    }
    if (!mangle && !space) return strdup(s);

    slen = strlen(s);
    newlen = slen + mangle + 3;	/* 3 is extra ""\0 */
    new = g_malloc(newlen);
    if (!new) return NULL;

    new[0] = '"';
    for (i = 0, j = 1; i < slen; i++, j++) {
	if (strchr(escapees, s[i])) {
	    new[j++] = '\\';
	}
	new[j] = s[i];
    }
    new[j] = '"';

    return new;
}

/* Get the value associated with the key, and leave the current pointer
 * pointing at the line containing the value.  The char* returned MUST
 * be freed by the caller.
 */
char *
svGetValue(shvarFile *s, char *key)
{
    char *value = NULL;
    char *line;
    char *keyString;
    int len;

    g_assert(s);
    g_assert(key);

    keyString = g_malloc0(strlen(key) + 2);
    strcpy(keyString, key);
    keyString[strlen(key)] = '=';
    len = strlen(keyString);

    for (s->current = s->lineList; s->current; s->current = s->current->next) {
	line = s->current->data;
	if (!strncmp(keyString, line, len)) {
	    value = g_strdup(line + len);
	    unescape(value);
	    break;
	}
    }
    g_free(keyString);

    if (value) {
	if (value[0]) {
	    return value;
	} else {
	    g_free(value);
	    return NULL;
	}
    }
    if (s->parent) value = svGetValue(s->parent, key);
    return value;
}

/* return 1 if <key> resolves to any truth value (e.g. "yes", "y", "true")
 * return 0 if <key> resolves to any non-truth value (e.g. "no", "n", "false")
 * return <default> otherwise
 */
int
svTrueValue(shvarFile *s, char *key, int def)
{
    char *tmp;
    int returnValue = def;

    tmp = svGetValue(s, key);
    if (!tmp) return returnValue;

    if ( (!strcasecmp("yes", tmp)) ||
	 (!strcasecmp("true", tmp)) ||
	 (!strcasecmp("t", tmp)) ||
	 (!strcasecmp("y", tmp)) ) returnValue = 1;
    else
    if ( (!strcasecmp("no", tmp)) ||
	 (!strcasecmp("false", tmp)) ||
	 (!strcasecmp("f", tmp)) ||
	 (!strcasecmp("n", tmp)) ) returnValue = 0;

    g_free (tmp);
    return returnValue;
}


/* Set the variable <key> equal to the value <value>.
 * If <key> does not exist, and the <current> pointer is set, append
 * the key=value pair after that line.  Otherwise, prepend the pair
 * to the top of the file.  Here's the algorithm, as the C code
 * seems to be rather dense:
 *
 * if (value == NULL), then:
 *     if val2 (parent): change line to key= or append line key=
 *     if val1 (this)  : delete line
 *     else noop
 * else use this table:
 *                                val2
 *             NULL              value               other
 * v   NULL    append line       noop                append line
 * a
 * l   value   noop              noop                noop
 * 1
 *     other   change line       delete line         change line
 *
 * No changes are ever made to the parent config file, only to the
 * specific file passed on the command line.
 *
 */
void
svSetValue(shvarFile *s, char *key, char *value)
{
    char *val1 = NULL, *val2 = NULL;
    char *keyValue;

    g_assert(s);
    g_assert(key);
    /* value may be NULL */

    if (value) value = escape(value);
    keyValue = g_strdup_printf("%s=%s", key, value ? value : "");

    val1 = svGetValue(s, key);
    if (val1 && value && !strcmp(val1, value)) goto bail;
    if (s->parent) val2 = svGetValue(s->parent, key);

    if (!value) {
	/* delete value somehow */
	if (val2) {
	    /* change/append line to get key= */
	    if (s->current) s->current->data = keyValue;
	    else s->lineList = g_list_append(s->lineList, keyValue);
	    s->freeList = g_list_append(s->freeList, keyValue);
	    s->modified = 1;
	} else if (val1) {
	    /* delete line */
	    s->lineList = g_list_remove_link(s->lineList, s->current);
	    g_list_free_1(s->current);
	    s->modified = 1;
	    goto bail; /* do not need keyValue */
	}
	goto end;
    }

    if (!val1) {
	if (val2 && !strcmp(val2, value)) goto end;
	/* append line */
	s->lineList = g_list_append(s->lineList, keyValue);
	s->freeList = g_list_append(s->freeList, keyValue);
	s->modified = 1;
	goto end;
    }

    /* deal with a whole line of noops */
    if (val1 && !strcmp(val1, value)) goto end;

    /* At this point, val1 && val1 != value */
    if (val2 && !strcmp(val2, value)) {
	/* delete line */
	s->lineList = g_list_remove_link(s->lineList, s->current);
	g_list_free_1(s->current);
	s->modified = 1;
	goto bail; /* do not need keyValue */
    } else {
	/* change line */
	if (s->current) s->current->data = keyValue;
	else s->lineList = g_list_append(s->lineList, keyValue);
	s->freeList = g_list_append(s->freeList, keyValue);
	s->modified = 1;
    }

end:
    if (value) free(value);
    if (val1) free(val1);
    if (val2) free(val2);
    return;

bail:
    if (keyValue) free (keyValue);
    goto end;
}

/* Write the current contents iff modified.  Returns -1 on error
 * and 0 on success.  Do not write if no values have been modified.
 * The mode argument is only used if creating the file, not if
 * re-writing an existing file, and is passed unchanged to the
 * open() syscall.
 */
int
svWriteFile(shvarFile *s, int mode)
{
    FILE *f;
    int tmpfd;

    if (s->modified) {
	if (s->fd == -1)
	    s->fd = open(s->fileName, O_WRONLY|O_CREAT, mode);
	if (s->fd == -1)
	    return -1;
	if (ftruncate(s->fd, 0) < 0)
	    return -1;

	tmpfd = dup(s->fd);
	f = fdopen(tmpfd, "w");
	fseek(f, 0, SEEK_SET);
	for (s->current = s->lineList; s->current; s->current = s->current->next) {
	    char *line = s->current->data;
	    fprintf(f, "%s\n", line);
	}
	fclose(f);
    }

    return 0;
}

 
/* Close the file descriptor (if open) and delete the shvarFile.
 * Returns -1 on error and 0 on success.
 */
int
svCloseFile(shvarFile *s)
{

    g_assert(s);

    if (s->fd != -1) close(s->fd);

    g_free(s->arena);
    for (s->current = s->freeList; s->current; s->current = s->current->next) {
        g_free(s->current->data);
    }
    g_free(s->fileName);
    g_list_free(s->freeList);
    g_list_free(s->lineList); /* implicitly frees s->current */
    g_free(s);
    return 0;
}