4 * Created on: Sep 16, 2010
15 #include "process_parser.h"
17 t_process_arg_list *parse_command_line_args(char *cmd_line_param) {
21 t_process_arg_list *ret_val;
24 if (cmd_line_param != NULL) {
25 ret_val = calloc(1, sizeof(t_process_arg_list));
26 line_pointer = cmd_line_param;
27 while(*line_pointer != '\0') {
28 while((*line_pointer == ' ') ||
29 (*line_pointer == '\t')) {
32 if (*line_pointer != '\0') {
33 if (ret_val->arg_arr == NULL) {
34 ret_val->arg_arr = calloc(1, sizeof(char *));
39 arg_arr = ret_val->arg_arr;
40 arg_arr = realloc(arg_arr, (ret_val->count + 1) * sizeof(char *));
41 ret_val->arg_arr = arg_arr;
45 while((*++line_pointer != '\0') &&
46 (*line_pointer != ' ') &&
47 (*line_pointer != '\t')) {
50 ret_val->arg_arr[ret_val->count] = strndup(start, len);
51 log_debug("ret_val[%d] = %s\n", ret_val->count, ret_val->arg_arr[ret_val->count]);
52 ret_val->count = ret_val->count + 1;
58 // terminate list with null
59 ret_val->arg_arr = realloc(ret_val->arg_arr, (ret_val->count + 1) * sizeof(char *));
60 ret_val->arg_arr[ret_val->count] = NULL;
61 ret_val->count = ret_val->count + 1;
66 t_process_info *parse_process_info(struct uci_option *option_param) {
67 t_process_info *ret_val;
70 if (option_param->type == UCI_TYPE_STRING) {
71 ret_val = calloc(1, sizeof(t_process_info));
72 ret_val->arg_list = parse_command_line_args(option_param->v.string);
77 int add_process_config(t_process_info_list *list_param, struct uci_option *option_param) {
79 t_process_info *new_item;
82 new_item = parse_process_info(option_param);
83 if (new_item != NULL) {
84 if (list_param->last_item != NULL) {
85 list_param->last_item->next = new_item;
86 list_param->last_item = new_item;
89 list_param->first_item = new_item;
90 list_param->last_item = new_item;
92 list_param->count = list_param->count + 1;
98 t_process_info_list *get_process_config_list() {
102 struct uci_context *ctx;
103 struct uci_package *pkg;
104 struct uci_section *section;
105 struct uci_option *option;
106 t_process_info_list *ret_val;
108 ret_val = calloc(1, sizeof(t_process_info_list));
109 ctx = uci_alloc_context();
111 uci_set_confdir(ctx, CONST_SERVICE_DIRECTORY_LOCATION);
113 err_flg = uci_list_configs(ctx, &config_list);
114 if ((err_flg == UCI_OK) &&
115 (config_list != NULL)) {
116 for (config_file = config_list; *config_file != NULL; config_file++) {
117 err_flg = uci_load(ctx, *config_file, &pkg);
118 if (err_flg == UCI_OK) {
119 section = uci_lookup_section(ctx, pkg, "service");
120 if (section != NULL) {
121 option = uci_lookup_option(ctx, section, "ExecStart");
122 switch (option->type) {
123 case UCI_TYPE_STRING:
124 log_info("config file: %s name: %s, value: %s\n", *config_file, option->e.name, option->v.string);
125 add_process_config(ret_val, option);
128 log_error("config file: can not parse parameter value");
132 //int num_sections = _uci_show_package(*config_file, result);
133 //printf("Queried package: %s -> %d\n", *config_file, num_sections);
135 uci_unload(ctx, pkg);
139 for (config_file = config_list; *config_file != NULL; config_file++) {
146 log_error("Failed to read service configurations from directory: %s\n", CONST_SERVICE_DIRECTORY_LOCATION);
148 uci_free_context(ctx);
151 log_error("Failed to read service configurations from directory: %s\n Memory allocation error.", CONST_SERVICE_DIRECTORY_LOCATION);
156 void release_process_config_arg_list(t_process_arg_list *arg_list_param) {
159 for (ii = 0; ii < arg_list_param->count; ii++) {
160 if (arg_list_param->arg_arr[ii] != NULL) {
161 free(arg_list_param->arg_arr[ii]);
162 arg_list_param->arg_arr[ii] = NULL;
165 free(arg_list_param->arg_arr);
166 arg_list_param->count = 0;
167 free(arg_list_param);
170 void release_process_config_list(t_process_info_list *list_param) {
171 t_process_info *item;
172 t_process_info *next;
174 if (list_param != NULL) {
175 item = list_param->first_item;
176 while (item != NULL) {
179 release_process_config_arg_list(item->arg_list);
180 item->arg_list = NULL;
184 list_param->first_item = NULL;
185 list_param->last_item = NULL;