10 static int parse_config_line(char *line, char **variable, char **value)
12 #define SKIP_UNTIL(_str, _cond) \
13 while (*_str && (_cond)) \
16 SKIP_UNTIL(line, isspace(*line));
22 /* The actual variable name begins here */
25 /* Find where the variable name ends */
26 SKIP_UNTIL(line, !isspace(*line) && *line != '=');
34 /* If we encounter line end already, the line can't be valid */
38 /* Terminate the variable name */
42 /* Find the equal sign */
43 SKIP_UNTIL(line, *line != '=');
49 SKIP_UNTIL(line, isspace(*line));
54 SKIP_UNTIL(line, *line != '\n' && *line != '#');
60 static int store_variable_value_to_plotter_config(const char *variable,
62 struct plotter_config *cfg)
64 #define TEST_STR_STORE_STR_RETURN(_cfg, _variable, _str, _value) \
65 if (!strcmp(_variable, #_str) && !cfg->_str) { \
66 cfg->_str = strdup(_value); \
70 #define TEST_STR_STORE_INT_RETURN(_cfg, _variable, _str, _value) \
71 if (!strcmp(_variable, #_str)) { \
72 cfg->_str = atoi(_value); \
76 TEST_STR_STORE_STR_RETURN(cfg, variable, charger_name, value);
77 TEST_STR_STORE_STR_RETURN(cfg, variable, plotter_scripts_dir, value);
78 TEST_STR_STORE_STR_RETURN(cfg, variable, images_output_dir, value);
79 TEST_STR_STORE_STR_RETURN(cfg, variable, device_path, value);
80 TEST_STR_STORE_STR_RETURN(cfg, variable, log_path, value);
81 TEST_STR_STORE_INT_RETURN(cfg, variable, baudrate, value);
86 static int read_config_file(FILE *file, struct plotter_config *cfg)
89 char *variable, *value;
91 while (fgets(line, sizeof(line), file)) {
92 if (parse_config_line(line, &variable, &value))
95 if (!store_variable_value_to_plotter_config(variable, value,
99 pr_debug("Discarding unsupported config variable %s\n",
106 int populate_config_data_from_file(const char *path,
107 struct plotter_config *cfg)
113 ret = glob(path, GLOB_TILDE, NULL, &globbuf);
117 if (globbuf.gl_pathc == 0) {
118 pr_debug("No config file foud from path %s\n", path);
122 if (globbuf.gl_pathc > 1) {
123 pr_warn("Found multiple config files from path %s, using the first one\n",
127 file = fopen(globbuf.gl_pathv[0], "r");
129 pr_err("Failed to open config file %s: %m\n",
130 globbuf.gl_pathv[0]);
134 return read_config_file(file, cfg);