/[baselayout]/trunk/src/librc-misc.c
Gentoo

Contents of /trunk/src/librc-misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3007 - (show annotations) (download) (as text)
Mon Oct 8 11:07:39 2007 UTC (6 years, 11 months ago) by uberlord
File MIME type: text/x-csrc
File size: 4088 byte(s)
Move rc_env_filter and rc_env_config out of librc and into rc
1 /*
2 rc-misc.c
3 rc misc functions
4 Copyright 2007 Gentoo Foundation
5 */
6
7 #include "librc.h"
8
9 bool rc_env_bool (const char *var)
10 {
11 char *v;
12
13 if (! var)
14 return (false);
15
16 if (! (v = getenv (var))) {
17 errno = ENOENT;
18 return (false);
19 }
20
21 if (strcasecmp (v, "true") == 0 ||
22 strcasecmp (v, "y") == 0 ||
23 strcasecmp (v, "yes") == 0 ||
24 strcasecmp (v, "1") == 0)
25 return (true);
26
27 if (strcasecmp (v, "false") != 0 &&
28 strcasecmp (v, "n") != 0 &&
29 strcasecmp (v, "no") != 0 &&
30 strcasecmp (v, "0") != 0)
31 errno = EINVAL;
32
33 return (false);
34 }
35 librc_hidden_def(rc_env_bool)
36
37 char *rc_strcatpaths (const char *path1, const char *paths, ...)
38 {
39 va_list ap;
40 int length;
41 int i;
42 char *p;
43 char *path;
44 char *pathp;
45
46 if (! path1 || ! paths)
47 return (NULL);
48
49 length = strlen (path1) + strlen (paths) + 1;
50 if (*paths != '/')
51 length ++;
52
53 va_start (ap, paths);
54 while ((p = va_arg (ap, char *)) != NULL) {
55 if (*p != '/')
56 length ++;
57 length += strlen (p);
58 }
59 va_end (ap);
60
61 pathp = path = rc_xmalloc (length * sizeof (char *));
62 memset (path, 0, length);
63 i = strlen (path1);
64 memcpy (path, path1, i);
65 pathp += i;
66 if (*paths != '/')
67 *pathp ++ = '/';
68 i = strlen (paths);
69 memcpy (pathp, paths, i);
70 pathp += i;
71
72 va_start (ap, paths);
73 while ((p = va_arg (ap, char *)) != NULL) {
74 if (*p != '/')
75 *pathp ++= '/';
76 i = strlen (p);
77 memcpy (pathp, p, i);
78 pathp += i;
79 }
80 va_end (ap);
81
82 *pathp++ = 0;
83
84 return (path);
85 }
86 librc_hidden_def(rc_strcatpaths)
87
88
89 char **rc_config_load (const char *file)
90 {
91 char **list = NULL;
92 FILE *fp;
93 char buffer[RC_LINEBUFFER];
94 char *p;
95 char *token;
96 char *line;
97 char *linep;
98 char *linetok;
99 int i = 0;
100 bool replaced;
101 char *entry;
102 char *newline;
103
104 if (! (fp = fopen (file, "r")))
105 return (NULL);
106
107 while (fgets (buffer, RC_LINEBUFFER, fp)) {
108 p = buffer;
109
110 /* Strip leading spaces/tabs */
111 while ((*p == ' ') || (*p == '\t'))
112 p++;
113
114 if (! p || strlen (p) < 3 || p[0] == '#')
115 continue;
116
117 /* Get entry */
118 token = strsep (&p, "=");
119
120 if (! token)
121 continue;
122
123 entry = rc_xstrdup (token);
124
125 /* Preserve shell coloring */
126 if (*p == '$')
127 token = p;
128 else
129 do {
130 /* Bash variables are usually quoted */
131 token = strsep (&p, "\"\'");
132 } while ((token) && (strlen (token) == 0));
133
134 /* Drop a newline if that's all we have */
135 i = strlen (token) - 1;
136 if (token[i] == 10)
137 token[i] = 0;
138
139 i = strlen (entry) + strlen (token) + 2;
140 newline = rc_xmalloc (i);
141 snprintf (newline, i, "%s=%s", entry, token);
142
143 replaced = false;
144 /* In shells the last item takes precedence, so we need to remove
145 any prior values we may already have */
146 STRLIST_FOREACH (list, line, i) {
147 char *tmp = rc_xstrdup (line);
148 linep = tmp;
149 linetok = strsep (&linep, "=");
150 if (strcmp (linetok, entry) == 0) {
151 /* We have a match now - to save time we directly replace it */
152 free (list[i - 1]);
153 list[i - 1] = newline;
154 replaced = true;
155 free (tmp);
156 break;
157 }
158 free (tmp);
159 }
160
161 if (! replaced) {
162 rc_strlist_addsort (&list, newline);
163 free (newline);
164 }
165 free (entry);
166 }
167 fclose (fp);
168
169 return (list);
170 }
171 librc_hidden_def(rc_config_load)
172
173 char *rc_config_value (char **list, const char *entry)
174 {
175 char *line;
176 int i;
177 char *p;
178
179 STRLIST_FOREACH (list, line, i) {
180 p = strchr (line, '=');
181 if (p && strncmp (entry, line, p - line) == 0)
182 return (p += 1);
183 }
184
185 return (NULL);
186 }
187 librc_hidden_def(rc_config_value)
188
189 char **rc_config_list (const char *file)
190 {
191 FILE *fp;
192 char buffer[RC_LINEBUFFER];
193 char *p;
194 char *token;
195 char **list = NULL;
196
197 if (! (fp = fopen (file, "r")))
198 return (NULL);
199
200 while (fgets (buffer, RC_LINEBUFFER, fp)) {
201 p = buffer;
202
203 /* Strip leading spaces/tabs */
204 while ((*p == ' ') || (*p == '\t'))
205 p++;
206
207 /* Get entry - we do not want comments */
208 token = strsep (&p, "#");
209 if (token && (strlen (token) > 1)) {
210 /* Stip the newline if present */
211 if (token[strlen (token) - 1] == '\n')
212 token[strlen (token) - 1] = 0;
213
214 rc_strlist_add (&list, token);
215 }
216 }
217 fclose (fp);
218
219 return (list);
220 }
221 librc_hidden_def(rc_config_list)

  ViewVC Help
Powered by ViewVC 1.1.20