6 #include "system-info-tool.h"
7 #include "system-info-tool-get.h"
8 #include "system-info-tool-set.h"
10 #define KEY_PREFIX "http://"
13 const struct db db[DB_END] = {
14 /* { dbpath, dbname, ticker } */
15 [DB_DEFAULT_RO] = { SYSTEM_INFO_DB_RO_PATH, "Platform", 'A' },
16 [DB_HAL_RO] = { SYSTEM_INFO_HAL_DB_RO_PATH, "HAL", 'B' },
17 [DB_DEFAULT_RW] = { SYSTEM_INFO_DB_RW_PATH, "User-defined", 'C' },
20 static const char *__type_to_string[SYSTEM_INFO_TYPE_MAX] = {
21 [SYSTEM_INFO_BOOL] = "bool",
22 [SYSTEM_INFO_INT] = "int",
23 [SYSTEM_INFO_DOUBLE] = "double",
24 [SYSTEM_INFO_STRING] = "string",
27 const char *type_to_string(system_info_type_e type)
29 if (type < SYSTEM_INFO_TYPE_MIN || type >= SYSTEM_INFO_TYPE_MAX)
32 return __type_to_string[type];
35 system_info_type_e string_to_type(const char *type)
37 if (strncasecmp(type, "bool", sizeof("bool")) == 0)
38 return SYSTEM_INFO_BOOL;
39 else if (strncasecmp(type, "int", sizeof("int")) == 0)
40 return SYSTEM_INFO_INT;
41 else if (strncasecmp(type, "double", sizeof("double")) == 0)
42 return SYSTEM_INFO_DOUBLE;
43 else if (strncasecmp(type, "string", sizeof("string")) == 0)
44 return SYSTEM_INFO_STRING;
46 return SYSTEM_INFO_TYPE_MAX;
49 int convert_raw_key(const char *rawkey, char *buffer, int len)
53 if (!rawkey || !buffer)
56 if (strstr(rawkey, KEY_PREFIX) == rawkey)
57 retval = snprintf(buffer, len, "%s", rawkey + strlen(KEY_PREFIX));
59 retval = snprintf(buffer, len, "%s", rawkey);
67 void print_value(struct value value)
69 if (value.type == SYSTEM_INFO_BOOL)
70 printf("type=bool, value=%d\n", value.b);
71 else if (value.type == SYSTEM_INFO_INT)
72 printf("type=int, value=%d\n", value.i);
73 else if (value.type == SYSTEM_INFO_DOUBLE)
74 printf("type=double, value=%f\n", value.d);
75 else if (value.type == SYSTEM_INFO_STRING)
76 printf("type=string, value=%s\n", value.s);
78 printf("type=inavlid, error\n");
81 static void system_info_tool_show_help(void)
84 printf(" system-info-tool [OPTION]\n\n");
86 system_info_tool_get_help();
87 system_info_tool_set_help();
88 printf(" -h|--help Show this help\n");
91 int main(int argc, char *argv[])
102 system_info_tool_show_help();
106 runtime_str = getenv("RUNTIME_TYPE");
108 for (int i = 0; runtime[i].lang != LANG_MAX; ++i) {
109 if (strncmp(runtime[i].runtime_type, runtime_str, strlen(runtime_str) + 1) == 0) {
110 runtime_env = runtime[i].lang;
116 const struct option long_option[] = {
117 { "get", required_argument, NULL, 'g' },
118 { "verbose", no_argument, NULL, 'v' },
119 { "list-all", no_argument, NULL, 'l' },
120 { "set" , no_argument, NULL, 's' }, /* requires 3 arguments */
121 { "clear", required_argument, NULL, 'c' },
122 { "clear-all", no_argument, NULL, 'C' },
123 { "help", no_argument, NULL, 'h' },
128 opt = getopt_long(argc, argv, "g:vlsc:Ch", long_option, NULL);
133 SYSINFO_G_LIST_APPEND(keys, strdup(optarg));
139 for (int i = DB_START; i < DB_END; ++i)
140 system_info_tool_list_all(i);
143 retval = system_info_tool_set(argc, argv);
145 printf("Failed to set key\n");
146 system_info_tool_set_help();
149 optind += 3; /* manually consume 3 arguments */
152 system_info_tool_clear_key(optarg);
155 system_info_tool_clear_all();
158 system_info_tool_show_help();
161 system_info_tool_show_help();
166 SYSINFO_G_LIST_FOREACH(keys, elem, key) {
167 system_info_tool_get(key, verbose);