ini-file-read.c 8.2 KB

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