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-2019
   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/strescape.h"
  33 #include "lib/skin.h"
  34 #include "lib/util.h"           /* exist_file() */
  35 #include "lib/filehighlight.h"
  36 
  37 #include "internal.h"
  38 
  39 /*** global variables ****************************************************************************/
  40 
  41 /*** file scope macro definitions ****************************************************************/
  42 
  43 /*** file scope type declarations ****************************************************************/
  44 
  45 /*** file scope variables ************************************************************************/
  46 
  47 /*** file scope functions ************************************************************************/
  48 /* --------------------------------------------------------------------------------------------- */
  49 
  50 static void
  51 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]  */
  52 {
  53     (void) fhl;
  54     mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
  55 }
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 static gboolean
  60 mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
  61 {
  62     mc_fhl_filter_t *mc_filter;
  63 
  64     const gchar *types[] = {
  65         "FILE", "FILE_EXE",
  66         "DIR", "LINK_DIR",
  67         "LINK", "HARDLINK", "SYMLINK",
  68         "STALE_LINK",
  69         "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
  70         "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
  71         NULL
  72     };
  73     int i;
  74     gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
  75 
  76     if (*param_type == '\0')
  77     {
  78         g_free (param_type);
  79         return FALSE;
  80     }
  81 
  82     for (i = 0; types[i] != NULL; i++)
  83     {
  84         if (strcmp (types[i], param_type) == 0)
  85             break;
  86     }
  87     g_free (param_type);
  88     if (types[i] == NULL)
  89         return FALSE;
  90 
  91     mc_filter = g_new0 (mc_fhl_filter_t, 1);
  92     mc_filter->type = MC_FLHGH_T_FTYPE;
  93     mc_filter->file_type = (mc_flhgh_ftype_type) i;
  94     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
  95 
  96     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
  97     return TRUE;
  98 }
  99 
 100 /* --------------------------------------------------------------------------------------------- */
 101 
 102 static gboolean
 103 mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 104 {
 105     mc_fhl_filter_t *mc_filter;
 106     gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
 107 
 108     if (*regexp == '\0')
 109     {
 110         g_free (regexp);
 111         return FALSE;
 112     }
 113 
 114     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 115     mc_filter->type = MC_FLHGH_T_FREGEXP;
 116     mc_filter->search_condition = mc_search_new (regexp, DEFAULT_CHARSET);
 117     mc_filter->search_condition->is_case_sensitive = TRUE;
 118     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 119 
 120     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 121     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 122     g_free (regexp);
 123     return TRUE;
 124 }
 125 
 126 /* --------------------------------------------------------------------------------------------- */
 127 
 128 static gboolean
 129 mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 130 {
 131     mc_fhl_filter_t *mc_filter;
 132     gchar **exts, **exts_orig;
 133     GString *buf;
 134 
 135     exts_orig = mc_config_get_string_list (fhl->config, group_name, "extensions", NULL);
 136     if (exts_orig == NULL || exts_orig[0] == NULL)
 137     {
 138         g_strfreev (exts_orig);
 139         return FALSE;
 140     }
 141 
 142     buf = g_string_sized_new (64);
 143 
 144     for (exts = exts_orig; *exts != NULL; exts++)
 145     {
 146         char *esc_ext;
 147 
 148         esc_ext = strutils_regex_escape (*exts);
 149         if (buf->len != 0)
 150             g_string_append_c (buf, '|');
 151         g_string_append (buf, esc_ext);
 152         g_free (esc_ext);
 153     }
 154     g_strfreev (exts_orig);
 155 
 156     g_string_prepend (buf, ".*\\.(");
 157     g_string_append (buf, ")$");
 158 
 159     mc_filter = g_new0 (mc_fhl_filter_t, 1);
 160     mc_filter->type = MC_FLHGH_T_FREGEXP;
 161     mc_filter->search_condition = mc_search_new_len (buf->str, buf->len, DEFAULT_CHARSET);
 162     mc_filter->search_condition->is_case_sensitive =
 163         mc_config_get_bool (fhl->config, group_name, "extensions_case", FALSE);
 164     mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
 165 
 166     mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
 167     g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
 168     g_string_free (buf, TRUE);
 169     return TRUE;
 170 }
 171 
 172 /* --------------------------------------------------------------------------------------------- */
 173 /*** public functions ****************************************************************************/
 174 /* --------------------------------------------------------------------------------------------- */
 175 
 176 gboolean
 177 mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 178 {
 179     if (fhl == NULL || filename == NULL || !exist_file (filename))
 180         return FALSE;
 181 
 182     if (fhl->config != NULL)
 183         return mc_config_read_file (fhl->config, filename, TRUE, FALSE);
 184 
 185     fhl->config = mc_config_init (filename, TRUE);
 186     return (fhl->config != NULL);
 187 }
 188 
 189 /* --------------------------------------------------------------------------------------------- */
 190 
 191 gboolean
 192 mc_fhl_init_from_standard_files (mc_fhl_t * fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 193 {
 194     gchar *name;
 195     gboolean ok;
 196 
 197     /* ${XDG_CONFIG_HOME}/mc/filehighlight.ini */
 198     name = mc_config_get_full_path (MC_FHL_INI_FILE);
 199     ok = mc_fhl_read_ini_file (fhl, name);
 200     g_free (name);
 201     if (ok)
 202         return TRUE;
 203 
 204     /* ${sysconfdir}/mc/filehighlight.ini  */
 205     name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
 206     ok = mc_fhl_read_ini_file (fhl, name);
 207     g_free (name);
 208     if (ok)
 209         return TRUE;
 210 
 211     /* ${datadir}/mc/filehighlight.ini  */
 212     name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
 213     ok = mc_fhl_read_ini_file (fhl, name);
 214     g_free (name);
 215     return ok;
 216 }
 217 
 218 /* --------------------------------------------------------------------------------------------- */
 219 
 220 gboolean
 221 mc_fhl_parse_ini_file (mc_fhl_t * fhl)
     /* [previous][next][first][last][top][bottom][index][help]  */
 222 {
 223     gchar **group_names, **orig_group_names;
 224     gboolean ok;
 225 
 226     mc_fhl_array_free (fhl);
 227     fhl->filters = g_ptr_array_new ();
 228 
 229     orig_group_names = mc_config_get_groups (fhl->config, NULL);
 230     ok = (*orig_group_names != NULL);
 231 
 232     for (group_names = orig_group_names; *group_names != NULL; group_names++)
 233     {
 234         if (mc_config_has_param (fhl->config, *group_names, "type"))
 235         {
 236             /* parse filetype filter */
 237             mc_fhl_parse_get_file_type_id (fhl, *group_names);
 238         }
 239         if (mc_config_has_param (fhl->config, *group_names, "regexp"))
 240         {
 241             /* parse regexp filter */
 242             mc_fhl_parse_get_regexp (fhl, *group_names);
 243         }
 244         if (mc_config_has_param (fhl->config, *group_names, "extensions"))
 245         {
 246             /* parse extensions filter */
 247             mc_fhl_parse_get_extensions (fhl, *group_names);
 248         }
 249     }
 250 
 251     g_strfreev (orig_group_names);
 252 
 253     return ok;
 254 }
 255 
 256 /* --------------------------------------------------------------------------------------------- */

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