girara
|
00001 /* See LICENSE file for license and copyright information */ 00002 00003 #include <stdlib.h> 00004 #include <string.h> 00005 00006 #include "datastructures.h" 00007 #include "session.h" 00008 #include "utils.h" 00009 #include "internal.h" 00010 00011 #define COMMENT_PREFIX '#' 00012 00013 bool 00014 girara_config_handle_add(girara_session_t* session, const char* identifier, girara_command_function_t handle) 00015 { 00016 g_return_val_if_fail(session != NULL, false); 00017 g_return_val_if_fail(identifier != NULL, false); 00018 00019 /* search for existing config handle */ 00020 GIRARA_LIST_FOREACH(session->config.handles, girara_config_handle_t*, iter, data) 00021 if (strcmp(data->identifier, identifier) == 0) { 00022 data->handle = handle; 00023 girara_list_iterator_free(iter); 00024 return true; 00025 } 00026 GIRARA_LIST_FOREACH_END(session->config.handles, girara_config_handle_t*, iter, data); 00027 00028 /* add new config handle */ 00029 girara_config_handle_t* config_handle = g_slice_new(girara_config_handle_t); 00030 00031 config_handle->identifier = g_strdup(identifier); 00032 config_handle->handle = handle; 00033 girara_list_append(session->config.handles, config_handle); 00034 00035 return true; 00036 } 00037 00038 void 00039 girara_config_handle_free(girara_config_handle_t* handle) 00040 { 00041 if (handle == NULL) { 00042 return; 00043 } 00044 00045 g_free(handle->identifier); 00046 g_slice_free(girara_config_handle_t, handle); 00047 } 00048 00049 void 00050 girara_config_parse(girara_session_t* session, const char* path) 00051 { 00052 /* open file */ 00053 FILE* file = girara_file_open(path, "r"); 00054 00055 if (file == NULL) { 00056 return; 00057 } 00058 00059 /* read lines */ 00060 char* line = NULL; 00061 unsigned int line_number = 1; 00062 while ((line = girara_file_read_line(file)) != NULL) { 00063 /* skip empty lines and comments */ 00064 if (strlen(line) == 0 || line[0] == COMMENT_PREFIX) { 00065 free(line); 00066 continue; 00067 } 00068 00069 gchar** argv = NULL; 00070 gint argc = 0; 00071 00072 girara_list_t* argument_list = girara_list_new(); 00073 if (argument_list == NULL) { 00074 free(line); 00075 fclose(file); 00076 return; 00077 } 00078 00079 girara_list_set_free_function(argument_list, g_free); 00080 if (g_shell_parse_argv(line, &argc, &argv, NULL) != FALSE) { 00081 for(int i = 1; i < argc; i++) { 00082 char* argument = g_strdup(argv[i]); 00083 girara_list_append(argument_list, (void*) argument); 00084 } 00085 } else { 00086 girara_list_free(argument_list); 00087 fclose(file); 00088 free(line); 00089 return; 00090 } 00091 00092 /* search for config handle */ 00093 girara_config_handle_t* handle = NULL; 00094 GIRARA_LIST_FOREACH(session->config.handles, girara_config_handle_t*, iter, tmp) 00095 handle = tmp; 00096 if (strcmp(handle->identifier, argv[0]) == 0) { 00097 handle->handle(session, argument_list); 00098 break; 00099 } else { 00100 handle = NULL; 00101 } 00102 GIRARA_LIST_FOREACH_END(session->config.handles, girara_config_handle_t*, iter, tmp); 00103 00104 if (handle == NULL) { 00105 girara_warning("Could not process line %d in '%s': Unknown handle '%s'", line_number, path, argv[0]); 00106 } 00107 00108 line_number++; 00109 girara_list_free(argument_list); 00110 g_strfreev(argv); 00111 free(line); 00112 } 00113 00114 fclose(file); 00115 }