2 * Copyright 2003-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include <driver_settings.h>
17 bool gVerbose
= false;
19 // Just add your test settings here, they will be picked
22 const char *kSettings
[] = {
31 "keyA {\ndisabled\n}\n",
33 "keyA = b { keyB=d =e { keyC \"f g\"; keyD h } }"
37 // needed by the driver_settings implementation, but not part of
38 // the original R5 libroot.so
41 strnlen(char const *string
, size_t count
)
43 const char *pos
= string
;
45 while (count
-- && pos
[0] != '\0')
52 /** Concatenates the source string to the destination, writes
53 * as much as "maxLength" bytes to the dest string.
54 * Always null terminates the string as long as maxLength is
55 * larger than the dest string.
56 * Returns the length of the string that it tried to create
57 * to be able to easily detect string truncation.
61 strlcat(char *dest
, const char *source
, size_t maxLength
)
63 size_t destLength
= strnlen(dest
, maxLength
);
65 // This returns the wrong size, but it's all we can do
66 if (maxLength
== destLength
)
67 return destLength
+ strlen(source
);
70 maxLength
-= destLength
;
73 for (; i
< maxLength
- 1 && source
[i
]; i
++) {
79 return destLength
+ i
+ strlen(source
+ i
);
87 put_level_space(int32 level
)
95 dump_parameter(const driver_parameter
¶meter
, int32 level
)
97 put_level_space(level
);
98 printf("\"%s\" =", parameter
.name
);
100 for (int32 i
= 0; i
< parameter
.value_count
; i
++)
101 printf(" \"%s\"", parameter
.values
[i
]);
104 for (int32 i
= 0; i
< parameter
.parameter_count
; i
++)
105 dump_parameter(parameter
.parameters
[i
], level
+ 1);
110 dump_settings(const driver_settings
&settings
)
112 for (int32 i
= 0; i
< settings
.parameter_count
; i
++)
113 dump_parameter(settings
.parameters
[i
], 0);
118 print_settings(void *handle
)
121 size_t bufferSize
= sizeof(buffer
);
123 if (get_driver_settings_string(handle
, buffer
, &bufferSize
, false) < B_OK
) {
124 fprintf(stderr
, "Could not get settings string (standard)\n");
127 puts(" ----- standard");
129 puts(" ----- standard reparsed");
131 void *reparsedHandle
= parse_driver_settings_string(buffer
);
132 if (reparsedHandle
== NULL
) {
133 fprintf(stderr
, "Could not reparse settings\n");
136 const driver_settings
*settings
= get_driver_settings(reparsedHandle
);
137 dump_settings(*settings
);
139 delete_driver_settings(reparsedHandle
);
141 bufferSize
= sizeof(buffer
);
142 if (get_driver_settings_string(handle
, buffer
, &bufferSize
, true) < B_OK
) {
143 fprintf(stderr
, "Could not get settings string (flat)\n");
148 puts(" ----- flat reparsed");
150 reparsedHandle
= parse_driver_settings_string(buffer
);
151 if (reparsedHandle
== NULL
) {
152 fprintf(stderr
, "Could not reparse settings\n");
155 settings
= get_driver_settings(reparsedHandle
);
156 dump_settings(*settings
);
158 delete_driver_settings(reparsedHandle
);
163 check_settings_string(uint32 num
)
165 const char *string
= kSettings
[num
];
167 printf("\n--------- settings %ld -----------\n", num
);
169 void *handle
= parse_driver_settings_string(string
);
170 if (handle
== NULL
) {
171 fprintf(stderr
, "Could not parse settings 1\n");
174 const driver_settings
*settings
= get_driver_settings(handle
);
177 puts(" ----- original");
179 puts(" ----- parsed");
180 dump_settings(*settings
);
183 print_settings(handle
);
184 delete_driver_settings(handle
);
189 load_settings(const char *name
)
191 void *handle
= load_driver_settings(name
);
192 if (handle
== NULL
) {
193 fprintf(stderr
, "Could not load settings \"%s\"\n", name
);
197 const driver_settings
*settings
= get_driver_settings(handle
);
198 if (settings
== NULL
) {
199 fprintf(stderr
, "Could not get settings from loaded file \"%s\"\n", name
);
203 printf("settings \"%s\" loaded successfully\n", name
);
205 dump_settings(*settings
);
208 if (unload_driver_settings(handle
) < B_OK
)
209 fprintf(stderr
, "Could not unload driver settings \"%s\"\n", name
);
214 main(int argc
, char **argv
)
216 BDirectory
directory("/boot/home/config/settings/kernel/drivers");
217 if (directory
.InitCheck() != B_OK
) {
218 fprintf(stderr
, "Could not open directory: %s\n", strerror(directory
.InitCheck()));
222 // yes, I know I am lazy...
223 if (argc
> 1 && !strcmp(argv
[1], "-v"))
227 while (directory
.GetNextRef(&ref
) == B_OK
) {
228 load_settings(ref
.name
);
231 // load additional files specified on the command line
233 for (int32 i
= 1; i
< argc
; i
++) {
234 const char *name
= argv
[i
];
238 // make path absolute, so that load_driver_settings()
239 // doesn't search in the standard directory
241 load_settings(path
.Path());
244 // check fixed settings strings
246 for (uint32 i
= 0; i
< sizeof(kSettings
) / sizeof(char *); i
++)
247 check_settings_string(i
);