Added locale setting before calling dpkg to fix "link to" parsing on non-C locales
[midnight-commander.git] / lib / filehighlight / ini-file-read.c
blobfabe9700423fecca52babf3b6c76d76c2e8ab7ff
1 /*
2 File highlight plugin.
3 Reading and parse rules from ini-files
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <string.h>
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/strescape.h"
34 #include "lib/skin.h"
35 #include "lib/util.h" /* exist_file() */
36 #include "lib/filehighlight.h"
38 #include "internal.h"
40 /*** global variables ****************************************************************************/
42 extern mc_skin_t mc_skin__default;
44 /*** file scope macro definitions ****************************************************************/
46 /*** file scope type declarations ****************************************************************/
48 /*** file scope variables ************************************************************************/
50 /*** file scope functions ************************************************************************/
51 /* --------------------------------------------------------------------------------------------- */
53 static void
54 mc_fhl_parse_fill_color_info (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, const gchar * group_name)
56 (void) fhl;
57 mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
60 /* --------------------------------------------------------------------------------------------- */
62 static gboolean
63 mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
65 mc_fhl_filter_t *mc_filter;
67 const gchar *types[] = {
68 "FILE", "FILE_EXE",
69 "DIR", "LINK_DIR",
70 "LINK", "HARDLINK", "SYMLINK",
71 "STALE_LINK",
72 "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
73 "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
74 NULL
76 int i;
77 gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
79 if (*param_type == '\0')
81 g_free (param_type);
82 return FALSE;
85 for (i = 0; types[i] != NULL; i++)
87 if (strcmp (types[i], param_type) == 0)
88 break;
90 g_free (param_type);
91 if (types[i] == NULL)
92 return FALSE;
94 mc_filter = g_new0 (mc_fhl_filter_t, 1);
95 mc_filter->type = MC_FLHGH_T_FTYPE;
96 mc_filter->file_type = (mc_flhgh_ftype_type) i;
97 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
99 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
100 return TRUE;
103 /* --------------------------------------------------------------------------------------------- */
105 static gboolean
106 mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
108 mc_fhl_filter_t *mc_filter;
109 gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
111 if (*regexp == '\0')
113 g_free (regexp);
114 return FALSE;
117 mc_filter = g_new0 (mc_fhl_filter_t, 1);
118 mc_filter->type = MC_FLHGH_T_FREGEXP;
119 mc_filter->search_condition = mc_search_new (regexp, -1);
120 mc_filter->search_condition->is_case_sensitive = TRUE;
121 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
123 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
124 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
125 g_free (regexp);
126 return TRUE;
129 /* --------------------------------------------------------------------------------------------- */
131 static gboolean
132 mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
134 mc_fhl_filter_t *mc_filter;
135 gchar **exts, **exts_orig;
136 gsize exts_size;
137 GString *buf;
139 exts_orig = exts =
140 mc_config_get_string_list (fhl->config, group_name, "extensions", &exts_size);
142 if (exts_orig == NULL || exts_orig[0] == NULL)
144 g_strfreev (exts_orig);
145 return FALSE;
148 buf = g_string_sized_new (64);
149 for (exts = exts_orig; *exts != NULL; exts++)
151 char *esc_ext;
153 esc_ext = strutils_regex_escape (*exts);
154 if (buf->len != 0)
155 g_string_append_c (buf, '|');
156 g_string_append (buf, esc_ext);
157 g_free (esc_ext);
159 g_strfreev (exts_orig);
161 g_string_prepend (buf, ".*\\.(");
162 g_string_append (buf, ")$");
164 mc_filter = g_new0 (mc_fhl_filter_t, 1);
165 mc_filter->type = MC_FLHGH_T_FREGEXP;
166 mc_filter->search_condition = mc_search_new (buf->str, -1);
167 mc_filter->search_condition->is_case_sensitive =
168 mc_config_get_bool (fhl->config, group_name, "extensions_case", TRUE);
169 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
171 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
172 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
173 g_string_free (buf, TRUE);
174 return TRUE;
177 /* --------------------------------------------------------------------------------------------- */
178 /*** public functions ****************************************************************************/
179 /* --------------------------------------------------------------------------------------------- */
181 gboolean
182 mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
184 if (fhl == NULL || filename == NULL || !exist_file (filename))
185 return FALSE;
187 if (fhl->config != NULL)
188 return mc_config_read_file (fhl->config, filename, FALSE);
190 fhl->config = mc_config_init (filename);
191 return (fhl->config != NULL);
194 /* --------------------------------------------------------------------------------------------- */
196 gboolean
197 mc_fhl_init_from_standard_files (mc_fhl_t * fhl)
199 gchar *name;
200 gboolean ok;
202 /* ${XDG_CONFIG_HOME}/mc/filehighlight.ini */
203 name = g_build_filename (mc_config_get_data_path (), MC_FHL_INI_FILE, (char *) NULL);
204 ok = mc_fhl_read_ini_file (fhl, name);
205 g_free (name);
206 if (ok)
207 return TRUE;
209 /* ${sysconfdir}/mc/filehighlight.ini */
210 name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
211 ok = mc_fhl_read_ini_file (fhl, name);
212 g_free (name);
213 if (ok)
214 return TRUE;
216 /* ${datadir}/mc/filehighlight.ini */
217 name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
218 ok = mc_fhl_read_ini_file (fhl, name);
219 g_free (name);
220 return ok;
223 /* --------------------------------------------------------------------------------------------- */
225 gboolean
226 mc_fhl_parse_ini_file (mc_fhl_t * fhl)
228 gchar **group_names, **orig_group_names;
230 mc_fhl_array_free (fhl);
231 fhl->filters = g_ptr_array_new ();
233 orig_group_names = group_names = mc_config_get_groups (fhl->config, NULL);
235 if (group_names == NULL)
236 return FALSE;
238 while (*group_names)
241 if (mc_config_has_param (fhl->config, *group_names, "type"))
243 /* parse filetype filter */
244 mc_fhl_parse_get_file_type_id (fhl, *group_names);
246 if (mc_config_has_param (fhl->config, *group_names, "regexp"))
248 /* parse regexp filter */
249 mc_fhl_parse_get_regexp (fhl, *group_names);
251 if (mc_config_has_param (fhl->config, *group_names, "extensions"))
253 /* parse extensions filter */
254 mc_fhl_parse_get_extensions (fhl, *group_names);
256 group_names++;
259 g_strfreev (orig_group_names);
260 return TRUE;
263 /* --------------------------------------------------------------------------------------------- */