2009-09-02 03:07:50 +04:00
|
|
|
/*
|
|
|
|
File highlight plugin.
|
2009-09-10 16:14:18 +04:00
|
|
|
Reading and parse rules from ini-files
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2015-01-07 10:06:55 +03:00
|
|
|
Copyright (C) 2009-2015
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2009-09-02 03:07:50 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009.
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2009-09-02 03:07:50 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2009-09-02 03:07:50 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-09-02 03:07:50 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-21 13:30:08 +03:00
|
|
|
#include "lib/fileloc.h"
|
2010-01-21 16:31:29 +03:00
|
|
|
#include "lib/strescape.h"
|
2010-01-21 16:06:15 +03:00
|
|
|
#include "lib/skin.h"
|
2010-11-11 16:58:29 +03:00
|
|
|
#include "lib/util.h" /* exist_file() */
|
2010-01-21 16:06:15 +03:00
|
|
|
#include "lib/filehighlight.h"
|
2010-01-21 16:31:29 +03:00
|
|
|
|
2009-09-02 03:07:50 +04:00
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_fill_color_info (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, const gchar * group_name)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
2009-09-04 18:22:49 +04:00
|
|
|
(void) fhl;
|
2009-10-03 01:26:27 +04:00
|
|
|
mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
|
2009-09-02 03:07:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
|
|
|
mc_fhl_filter_t *mc_filter;
|
|
|
|
|
|
|
|
const gchar *types[] = {
|
|
|
|
"FILE", "FILE_EXE",
|
|
|
|
"DIR", "LINK_DIR",
|
|
|
|
"LINK", "HARDLINK", "SYMLINK",
|
|
|
|
"STALE_LINK",
|
|
|
|
"DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
|
|
|
|
"SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (*param_type == '\0')
|
|
|
|
{
|
2009-09-02 03:07:50 +04:00
|
|
|
g_free (param_type);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
for (i = 0; types[i] != NULL; i++)
|
|
|
|
{
|
2009-09-02 03:07:50 +04:00
|
|
|
if (strcmp (types[i], param_type) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_free (param_type);
|
|
|
|
if (types[i] == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
mc_filter = g_new0 (mc_fhl_filter_t, 1);
|
|
|
|
mc_filter->type = MC_FLHGH_T_FTYPE;
|
|
|
|
mc_filter->file_type = (mc_flhgh_ftype_type) i;
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
|
2009-09-02 03:07:50 +04:00
|
|
|
|
|
|
|
g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
|
|
|
mc_fhl_filter_t *mc_filter;
|
|
|
|
gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (*regexp == '\0')
|
|
|
|
{
|
2009-09-02 03:07:50 +04:00
|
|
|
g_free (regexp);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mc_filter = g_new0 (mc_fhl_filter_t, 1);
|
|
|
|
mc_filter->type = MC_FLHGH_T_FREGEXP;
|
2013-09-09 16:55:01 +04:00
|
|
|
mc_filter->search_condition = mc_search_new (regexp, -1, DEFAULT_CHARSET);
|
2010-03-31 11:48:53 +04:00
|
|
|
mc_filter->search_condition->is_case_sensitive = TRUE;
|
2009-09-02 03:07:50 +04:00
|
|
|
mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
|
2009-09-02 03:07:50 +04:00
|
|
|
g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
|
|
|
|
g_free (regexp);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
|
|
|
mc_fhl_filter_t *mc_filter;
|
|
|
|
gchar **exts, **exts_orig;
|
2010-11-04 14:49:35 +03:00
|
|
|
GString *buf;
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2014-05-04 12:25:18 +04:00
|
|
|
exts_orig = mc_config_get_string_list (fhl->config, group_name, "extensions", NULL);
|
2010-11-04 14:49:35 +03:00
|
|
|
if (exts_orig == NULL || exts_orig[0] == NULL)
|
|
|
|
{
|
2009-09-02 03:07:50 +04:00
|
|
|
g_strfreev (exts_orig);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-04 14:49:35 +03:00
|
|
|
buf = g_string_sized_new (64);
|
2014-05-04 12:25:18 +04:00
|
|
|
|
2010-11-04 14:49:35 +03:00
|
|
|
for (exts = exts_orig; *exts != NULL; exts++)
|
|
|
|
{
|
|
|
|
char *esc_ext;
|
|
|
|
|
2009-09-02 03:07:50 +04:00
|
|
|
esc_ext = strutils_regex_escape (*exts);
|
|
|
|
if (buf->len != 0)
|
|
|
|
g_string_append_c (buf, '|');
|
|
|
|
g_string_append (buf, esc_ext);
|
|
|
|
g_free (esc_ext);
|
|
|
|
}
|
|
|
|
g_strfreev (exts_orig);
|
2010-11-04 14:49:35 +03:00
|
|
|
|
|
|
|
g_string_prepend (buf, ".*\\.(");
|
2009-09-02 03:07:50 +04:00
|
|
|
g_string_append (buf, ")$");
|
|
|
|
|
|
|
|
mc_filter = g_new0 (mc_fhl_filter_t, 1);
|
|
|
|
mc_filter->type = MC_FLHGH_T_FREGEXP;
|
2013-09-09 16:55:01 +04:00
|
|
|
mc_filter->search_condition = mc_search_new (buf->str, buf->len, DEFAULT_CHARSET);
|
2010-03-31 11:48:53 +04:00
|
|
|
mc_filter->search_condition->is_case_sensitive =
|
2009-10-03 01:26:27 +04:00
|
|
|
mc_config_get_bool (fhl->config, group_name, "extensions_case", TRUE);
|
2009-09-02 03:07:50 +04:00
|
|
|
mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
|
2009-09-02 03:07:50 +04:00
|
|
|
g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
|
|
|
|
g_string_free (buf, TRUE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
|
|
|
|
{
|
|
|
|
if (fhl == NULL || filename == NULL || !exist_file (filename))
|
|
|
|
return FALSE;
|
|
|
|
|
2010-11-04 15:04:01 +03:00
|
|
|
if (fhl->config != NULL)
|
2012-07-28 14:19:27 +04:00
|
|
|
return mc_config_read_file (fhl->config, filename, TRUE, FALSE);
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2012-07-28 14:19:27 +04:00
|
|
|
fhl->config = mc_config_init (filename, TRUE);
|
2010-11-04 15:04:01 +03:00
|
|
|
return (fhl->config != NULL);
|
2009-09-02 03:07:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
gboolean
|
2010-11-04 14:56:10 +03:00
|
|
|
mc_fhl_init_from_standard_files (mc_fhl_t * fhl)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
|
|
|
gchar *name;
|
2010-11-08 15:55:25 +03:00
|
|
|
gboolean ok;
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2010-12-30 22:10:20 +03:00
|
|
|
/* ${XDG_CONFIG_HOME}/mc/filehighlight.ini */
|
2011-11-24 02:28:07 +04:00
|
|
|
name = mc_config_get_full_path (MC_FHL_INI_FILE);
|
2010-11-08 15:55:25 +03:00
|
|
|
ok = mc_fhl_read_ini_file (fhl, name);
|
2009-09-02 03:07:50 +04:00
|
|
|
g_free (name);
|
2010-11-08 15:55:25 +03:00
|
|
|
if (ok)
|
|
|
|
return TRUE;
|
2009-09-02 03:07:50 +04:00
|
|
|
|
|
|
|
/* ${sysconfdir}/mc/filehighlight.ini */
|
2011-02-10 18:02:54 +03:00
|
|
|
name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
|
2010-11-08 15:55:25 +03:00
|
|
|
ok = mc_fhl_read_ini_file (fhl, name);
|
2009-09-02 03:07:50 +04:00
|
|
|
g_free (name);
|
2010-11-08 15:55:25 +03:00
|
|
|
if (ok)
|
|
|
|
return TRUE;
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2010-11-08 15:55:25 +03:00
|
|
|
/* ${datadir}/mc/filehighlight.ini */
|
2011-02-10 18:02:54 +03:00
|
|
|
name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
|
2010-11-08 15:55:25 +03:00
|
|
|
ok = mc_fhl_read_ini_file (fhl, name);
|
2009-09-02 03:07:50 +04:00
|
|
|
g_free (name);
|
2010-11-08 15:55:25 +03:00
|
|
|
return ok;
|
2009-09-02 03:07:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
gboolean
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_fhl_parse_ini_file (mc_fhl_t * fhl)
|
2009-09-02 03:07:50 +04:00
|
|
|
{
|
2009-09-04 18:22:49 +04:00
|
|
|
gchar **group_names, **orig_group_names;
|
2014-05-04 12:05:30 +04:00
|
|
|
gboolean ok;
|
2009-09-02 03:07:50 +04:00
|
|
|
|
|
|
|
mc_fhl_array_free (fhl);
|
|
|
|
fhl->filters = g_ptr_array_new ();
|
|
|
|
|
2014-05-04 12:05:30 +04:00
|
|
|
orig_group_names = mc_config_get_groups (fhl->config, NULL);
|
|
|
|
ok = (*orig_group_names != NULL);
|
2009-09-02 03:07:50 +04:00
|
|
|
|
2014-05-04 12:05:30 +04:00
|
|
|
for (group_names = orig_group_names; *group_names != NULL; group_names++)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
if (mc_config_has_param (fhl->config, *group_names, "type"))
|
|
|
|
{
|
2009-09-10 16:14:18 +04:00
|
|
|
/* parse filetype filter */
|
|
|
|
mc_fhl_parse_get_file_type_id (fhl, *group_names);
|
2009-10-03 01:26:27 +04:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
if (mc_config_has_param (fhl->config, *group_names, "regexp"))
|
|
|
|
{
|
2009-09-10 16:14:18 +04:00
|
|
|
/* parse regexp filter */
|
|
|
|
mc_fhl_parse_get_regexp (fhl, *group_names);
|
2009-10-03 01:26:27 +04:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
if (mc_config_has_param (fhl->config, *group_names, "extensions"))
|
|
|
|
{
|
2009-09-10 16:14:18 +04:00
|
|
|
/* parse extensions filter */
|
|
|
|
mc_fhl_parse_get_extensions (fhl, *group_names);
|
2009-09-02 03:07:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
g_strfreev (orig_group_names);
|
2014-05-04 12:05:30 +04:00
|
|
|
|
|
|
|
return ok;
|
2009-09-02 03:07:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|