girara
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
config.c
Go to the documentation of this file.
1 /* See LICENSE file for license and copyright information */
2 
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include "datastructures.h"
7 #include "session.h"
8 #include "utils.h"
9 #include "internal.h"
10 
11 #define COMMENT_PREFIX '#'
12 
13 bool
14 girara_config_handle_add(girara_session_t* session, const char* identifier, girara_command_function_t handle)
15 {
16  g_return_val_if_fail(session != NULL, false);
17  g_return_val_if_fail(identifier != NULL, false);
18 
19  /* search for existing config handle */
20  GIRARA_LIST_FOREACH(session->config.handles, girara_config_handle_t*, iter, data)
21  if (strcmp(data->identifier, identifier) == 0) {
22  data->handle = handle;
24  return true;
25  }
26  GIRARA_LIST_FOREACH_END(session->config.handles, girara_config_handle_t*, iter, data);
27 
28  /* add new config handle */
29  girara_config_handle_t* config_handle = g_slice_new(girara_config_handle_t);
30 
31  config_handle->identifier = g_strdup(identifier);
32  config_handle->handle = handle;
33  girara_list_append(session->config.handles, config_handle);
34 
35  return true;
36 }
37 
38 void
39 girara_config_handle_free(girara_config_handle_t* handle)
40 {
41  if (handle == NULL) {
42  return;
43  }
44 
45  g_free(handle->identifier);
46  g_slice_free(girara_config_handle_t, handle);
47 }
48 
49 void
50 girara_config_parse(girara_session_t* session, const char* path)
51 {
52  /* open file */
53  FILE* file = girara_file_open(path, "r");
54 
55  if (file == NULL) {
56  return;
57  }
58 
59  /* read lines */
60  char* line = NULL;
61  unsigned int line_number = 1;
62  while ((line = girara_file_read_line(file)) != NULL) {
63  /* skip empty lines and comments */
64  if (strlen(line) == 0 || line[0] == COMMENT_PREFIX) {
65  free(line);
66  continue;
67  }
68 
69  gchar** argv = NULL;
70  gint argc = 0;
71 
72  girara_list_t* argument_list = girara_list_new();
73  if (argument_list == NULL) {
74  free(line);
75  fclose(file);
76  return;
77  }
78 
79  girara_list_set_free_function(argument_list, g_free);
80  if (g_shell_parse_argv(line, &argc, &argv, NULL) != FALSE) {
81  for(int i = 1; i < argc; i++) {
82  char* argument = g_strdup(argv[i]);
83  girara_list_append(argument_list, (void*) argument);
84  }
85  } else {
86  girara_list_free(argument_list);
87  fclose(file);
88  free(line);
89  return;
90  }
91 
92  /* search for config handle */
93  girara_config_handle_t* handle = NULL;
94  GIRARA_LIST_FOREACH(session->config.handles, girara_config_handle_t*, iter, tmp)
95  handle = tmp;
96  if (strcmp(handle->identifier, argv[0]) == 0) {
97  handle->handle(session, argument_list);
98  break;
99  } else {
100  handle = NULL;
101  }
102  GIRARA_LIST_FOREACH_END(session->config.handles, girara_config_handle_t*, iter, tmp);
103 
104  if (handle == NULL) {
105  girara_warning("Could not process line %d in '%s': Unknown handle '%s'", line_number, path, argv[0]);
106  }
107 
108  line_number++;
109  girara_list_free(argument_list);
110  g_strfreev(argv);
111  free(line);
112  }
113 
114  fclose(file);
115 }