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-2025
   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 <https://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",      "STALE_LINK",    //
  73         "DEVICE",  "DEVICE_BLOCK",   "DEVICE_CHAR",                   //
  74         "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",  //
  75         NULL,
  76     };
  77     int i;
  78     gchar *param_type;
  79 
  80     param_type = mc_config_get_string (fhl->config, group_name, "type", "");
  81     if (*param_type == '\0')
  82     {
  83         g_free (param_type);
  84         return FALSE;
  85     }
  86 
  87     for (i = 0; types[i] != NULL; i++)
  88         if (strcmp (types[i], param_type) == 0)
  89             break;
  90 
  91     g_free (param_type);
  92 
  93     if (types[i] == NULL)
  94         return FALSE;
  95 
  96     mc_filter = g_new0 (mc_fhl_filter_t, 1);
  97     mc_filter->type = MC_FLHGH_T_FTYPE;
  98     mc_filter->file_type = (mc_flhgh_ftype_type) i;
  99     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 100 
 101     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 102 
 103     return TRUE;
 104 }
 105 
 106 /* --------------------------------------------------------------------------------------------- */
 107 
 108 static gboolean
 109 mc_fhl_parse_get_regexp (mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 110 {
 111     mc_fhl_filter_t *mc_filter;
 112     gchar *regexp;
 113 
 114     regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
 115     if (*regexp == '\0')
 116     {
 117         g_free (regexp);
 118         return FALSE;
 119     }
 120 
 121     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 122     mc_filter->type = MC_FLHGH_T_FREGEXP;
 123     mc_filter->search_condition = mc_search_new (regexp, DEFAULT_CHARSET);
 124     mc_filter->search_condition->is_case_sensitive = TRUE;
 125     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 126 
 127     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 128     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 129     g_free (regexp);
 130 
 131     return TRUE;
 132 }
 133 
 134 /* --------------------------------------------------------------------------------------------- */
 135 
 136 static gboolean
 137 mc_fhl_parse_get_extensions (mc_fhl_t *fhl, const gchar *group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 138 {
 139     mc_fhl_filter_t *mc_filter;
 140     gchar **exts, **exts_orig;
 141     GString *buf;
 142 
 143     exts_orig = mc_config_get_string_list (fhl->config, group_name, "extensions", NULL);
 144     if (exts_orig == NULL || exts_orig[0] == NULL)
 145     {
 146         g_strfreev (exts_orig);
 147         return FALSE;
 148     }
 149 
 150     buf = g_string_sized_new (64);
 151 
 152     for (exts = exts_orig; *exts != NULL; exts++)
 153     {
 154         char *esc_ext;
 155 
 156         esc_ext = str_regex_escape (*exts);
 157         if (buf->len != 0)
 158             g_string_append_c (buf, '|');
 159         g_string_append (buf, esc_ext);
 160         g_free (esc_ext);
 161     }
 162 
 163     g_strfreev (exts_orig);
 164 
 165     g_string_prepend (buf, ".*\\.(");
 166     g_string_append (buf, ")$");
 167 
 168     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 169     mc_filter->type = MC_FLHGH_T_FREGEXP;
 170     mc_filter->search_condition = mc_search_new_len (buf->str, buf->len, DEFAULT_CHARSET);
 171     mc_filter->search_condition->is_case_sensitive =
 172         mc_config_get_bool (fhl->config, group_name, "extensions_case", FALSE);
 173     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 174 
 175     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 176     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 177     g_string_free (buf, TRUE);
 178 
 179     return TRUE;
 180 }
 181 
 182 /* --------------------------------------------------------------------------------------------- */
 183 /*** public functions ****************************************************************************/
 184 /* --------------------------------------------------------------------------------------------- */
 185 
 186 gboolean
 187 mc_fhl_read_ini_file (mc_fhl_t *fhl, const gchar *filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 188 {
 189     if (fhl == NULL || filename == NULL || !exist_file (filename))
 190         return FALSE;
 191 
 192     if (fhl->config != NULL)
 193         return mc_config_read_file (fhl->config, filename, TRUE, FALSE);
 194 
 195     fhl->config = mc_config_init (filename, TRUE);
 196 
 197     return (fhl->config != NULL);
 198 }
 199 
 200 /* --------------------------------------------------------------------------------------------- */
 201 
 202 gboolean
 203 mc_fhl_init_from_standard_files (mc_fhl_t *fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 204 {
 205     gchar *name;
 206     gboolean ok;
 207 
 208     // ${XDG_CONFIG_HOME}/mc/filehighlight.ini
 209     name = mc_config_get_full_path (MC_FHL_INI_FILE);
 210     ok = mc_fhl_read_ini_file (fhl, name);
 211     g_free (name);
 212     if (ok)
 213         return TRUE;
 214 
 215     // ${sysconfdir}/mc/filehighlight.ini
 216     name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
 217     ok = mc_fhl_read_ini_file (fhl, name);
 218     g_free (name);
 219     if (ok)
 220         return TRUE;
 221 
 222     // ${datadir}/mc/filehighlight.ini
 223     name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
 224     ok = mc_fhl_read_ini_file (fhl, name);
 225     g_free (name);
 226     return ok;
 227 }
 228 
 229 /* --------------------------------------------------------------------------------------------- */
 230 
 231 gboolean
 232 mc_fhl_parse_ini_file (mc_fhl_t *fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 233 {
 234     gchar **group_names, **orig_group_names;
 235     gboolean ok;
 236 
 237     mc_fhl_array_free (fhl);
 238     fhl->filters = g_ptr_array_new_with_free_func (mc_fhl_filter_free);
 239 
 240     orig_group_names = mc_config_get_groups (fhl->config, NULL);
 241     ok = (*orig_group_names != NULL);
 242 
 243     for (group_names = orig_group_names; *group_names != NULL; group_names++)
 244     {
 245         if (mc_config_has_param (fhl->config, *group_names, "type"))
 246         {
 247             // parse filetype filter
 248             mc_fhl_parse_get_file_type_id (fhl, *group_names);
 249         }
 250         if (mc_config_has_param (fhl->config, *group_names, "regexp"))
 251         {
 252             // parse regexp filter
 253             mc_fhl_parse_get_regexp (fhl, *group_names);
 254         }
 255         if (mc_config_has_param (fhl->config, *group_names, "extensions"))
 256         {
 257             // parse extensions filter
 258             mc_fhl_parse_get_extensions (fhl, *group_names);
 259         }
 260     }
 261 
 262     g_strfreev (orig_group_names);
 263 
 264     return ok;
 265 }
 266 
 267 /* --------------------------------------------------------------------------------------------- */

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