root/lib/filehighlight/ini-file-read.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_fhl_parse_fill_color_info
  2. mc_fhl_parse_get_file_type_id
  3. mc_fhl_parse_get_regexp
  4. mc_fhl_parse_get_extensions
  5. mc_fhl_read_ini_file
  6. mc_fhl_init_from_standard_files
  7. mc_fhl_parse_ini_file

   1 /*
   2    File highlight plugin.
   3    Reading and parse rules from ini-files
   4 
   5    Copyright (C) 2009-2024
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Slava Zanko <slavazanko@gmail.com>, 2009.
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 #include <config.h>
  28 #include <string.h>
  29 
  30 #include "lib/global.h"
  31 #include "lib/fileloc.h"
  32 #include "lib/strutil.h"
  33 #include "lib/skin.h"
  34 #include "lib/util.h"           /* exist_file() */
  35 
  36 #include "lib/filehighlight.h"
  37 
  38 #include "internal.h"
  39 
  40 /*** global variables ****************************************************************************/
  41 
  42 /*** file scope macro definitions ****************************************************************/
  43 
  44 /*** file scope type declarations ****************************************************************/
  45 
  46 /*** forward declarations (file scope functions) *************************************************/
  47 
  48 /*** file scope variables ************************************************************************/
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 /*** file scope functions ************************************************************************/
  52 /* --------------------------------------------------------------------------------------------- */
  53 
  54 static void
  55 mc_fhl_parse_fill_color_info (mc_fhl_filter_t *mc_filter, mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
  56 {
  57     (void) fhl;
  58 
  59     mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
  60 }
  61 
  62 /* --------------------------------------------------------------------------------------------- */
  63 
  64 static gboolean
  65 mc_fhl_parse_get_file_type_id (mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
  66 {
  67     mc_fhl_filter_t *mc_filter;
  68 
  69     const gchar *types[] = {
  70         "FILE", "FILE_EXE",
  71         "DIR", "LINK_DIR",
  72         "LINK", "HARDLINK", "SYMLINK",
  73         "STALE_LINK",
  74         "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
  75         "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
  76         NULL
  77     };
  78     int i;
  79     gchar *param_type;
  80 
  81     param_type = mc_config_get_string (fhl->config, group_name, "type", "");
  82     if (*param_type == '\0')
  83     {
  84         g_free (param_type);
  85         return FALSE;
  86     }
  87 
  88     for (i = 0; types[i] != NULL; i++)
  89         if (strcmp (types[i], param_type) == 0)
  90             break;
  91 
  92     g_free (param_type);
  93 
  94     if (types[i] == NULL)
  95         return FALSE;
  96 
  97     mc_filter = g_new0 (mc_fhl_filter_t, 1);
  98     mc_filter->type = MC_FLHGH_T_FTYPE;
  99     mc_filter->file_type = (mc_flhgh_ftype_type) i;
 100     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 101 
 102     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 103 
 104     return TRUE;
 105 }
 106 
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 static gboolean
 110 mc_fhl_parse_get_regexp (mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     mc_fhl_filter_t *mc_filter;
 113     gchar *regexp;
 114 
 115     regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
 116     if (*regexp == '\0')
 117     {
 118         g_free (regexp);
 119         return FALSE;
 120     }
 121 
 122     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 123     mc_filter->type = MC_FLHGH_T_FREGEXP;
 124     mc_filter->search_condition = mc_search_new (regexp, DEFAULT_CHARSET);
 125     mc_filter->search_condition->is_case_sensitive = TRUE;
 126     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 127 
 128     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 129     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 130     g_free (regexp);
 131 
 132     return TRUE;
 133 }
 134 
 135 /* --------------------------------------------------------------------------------------------- */
 136 
 137 static gboolean
 138 mc_fhl_parse_get_extensions (mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     mc_fhl_filter_t *mc_filter;
 141     gchar **exts, **exts_orig;
 142     GString *buf;
 143 
 144     exts_orig = mc_config_get_string_list (fhl->config, group_name, "extensions", NULL);
 145     if (exts_orig == NULL || exts_orig[0] == NULL)
 146     {
 147         g_strfreev (exts_orig);
 148         return FALSE;
 149     }
 150 
 151     buf = g_string_sized_new (64);
 152 
 153     for (exts = exts_orig; *exts != NULL; exts++)
 154     {
 155         char *esc_ext;
 156 
 157         esc_ext = str_regex_escape (*exts);
 158         if (buf->len != 0)
 159             g_string_append_c (buf, '|');
 160         g_string_append (buf, esc_ext);
 161         g_free (esc_ext);
 162     }
 163 
 164     g_strfreev (exts_orig);
 165 
 166     g_string_prepend (buf, ".*\\.(");
 167     g_string_append (buf, ")$");
 168 
 169     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 170     mc_filter->type = MC_FLHGH_T_FREGEXP;
 171     mc_filter->search_condition = mc_search_new_len (buf->str, buf->len, DEFAULT_CHARSET);
 172     mc_filter->search_condition->is_case_sensitive =
 173         mc_config_get_bool (fhl->config, group_name, "extensions_case", FALSE);
 174     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 175 
 176     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 177     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 178     g_string_free (buf, TRUE);
 179 
 180     return TRUE;
 181 }
 182 
 183 /* --------------------------------------------------------------------------------------------- */
 184 /*** public functions ****************************************************************************/
 185 /* --------------------------------------------------------------------------------------------- */
 186 
 187 gboolean
 188 mc_fhl_read_ini_file (mc_fhl_t *fhl, const gchar *filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 189 {
 190     if (fhl == NULL || filename == NULL || !exist_file (filename))
 191         return FALSE;
 192 
 193     if (fhl->config != NULL)
 194         return mc_config_read_file (fhl->config, filename, TRUE, FALSE);
 195 
 196     fhl->config = mc_config_init (filename, TRUE);
 197 
 198     return (fhl->config != NULL);
 199 }
 200 
 201 /* --------------------------------------------------------------------------------------------- */
 202 
 203 gboolean
 204 mc_fhl_init_from_standard_files (mc_fhl_t *fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 205 {
 206     gchar *name;
 207     gboolean ok;
 208 
 209     /* ${XDG_CONFIG_HOME}/mc/filehighlight.ini */
 210     name = mc_config_get_full_path (MC_FHL_INI_FILE);
 211     ok = mc_fhl_read_ini_file (fhl, name);
 212     g_free (name);
 213     if (ok)
 214         return TRUE;
 215 
 216     /* ${sysconfdir}/mc/filehighlight.ini  */
 217     name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
 218     ok = mc_fhl_read_ini_file (fhl, name);
 219     g_free (name);
 220     if (ok)
 221         return TRUE;
 222 
 223     /* ${datadir}/mc/filehighlight.ini  */
 224     name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
 225     ok = mc_fhl_read_ini_file (fhl, name);
 226     g_free (name);
 227     return ok;
 228 }
 229 
 230 /* --------------------------------------------------------------------------------------------- */
 231 
 232 gboolean
 233 mc_fhl_parse_ini_file (mc_fhl_t *fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 234 {
 235     gchar **group_names, **orig_group_names;
 236     gboolean ok;
 237 
 238     mc_fhl_array_free (fhl);
 239     fhl->filters = g_ptr_array_new_with_free_func (mc_fhl_filter_free);
 240 
 241     orig_group_names = mc_config_get_groups (fhl->config, NULL);
 242     ok = (*orig_group_names != NULL);
 243 
 244     for (group_names = orig_group_names; *group_names != NULL; group_names++)
 245     {
 246         if (mc_config_has_param (fhl->config, *group_names, "type"))
 247         {
 248             /* parse filetype filter */
 249             mc_fhl_parse_get_file_type_id (fhl, *group_names);
 250         }
 251         if (mc_config_has_param (fhl->config, *group_names, "regexp"))
 252         {
 253             /* parse regexp filter */
 254             mc_fhl_parse_get_regexp (fhl, *group_names);
 255         }
 256         if (mc_config_has_param (fhl->config, *group_names, "extensions"))
 257         {
 258             /* parse extensions filter */
 259             mc_fhl_parse_get_extensions (fhl, *group_names);
 260         }
 261     }
 262 
 263     g_strfreev (orig_group_names);
 264 
 265     return ok;
 266 }
 267 
 268 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */