ini-file-read.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. File highlight plugin.
  3. Reading and parse rules from ini-files
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include <string.h>
  23. #include "lib/global.h"
  24. #include "lib/fileloc.h"
  25. #include "lib/strescape.h"
  26. #include "lib/skin.h"
  27. #include "lib/filehighlight.h"
  28. #include "src/main.h"
  29. #include "internal.h"
  30. /*** global variables ****************************************************************************/
  31. extern mc_skin_t mc_skin__default;
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  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 = mc_config_get_string (fhl->config, group_name, "type", "");
  59. if (*param_type == '\0') {
  60. g_free (param_type);
  61. return FALSE;
  62. }
  63. for (i = 0; types[i] != NULL; i++) {
  64. if (strcmp (types[i], param_type) == 0)
  65. break;
  66. }
  67. g_free (param_type);
  68. if (types[i] == NULL)
  69. return FALSE;
  70. mc_filter = g_new0 (mc_fhl_filter_t, 1);
  71. mc_filter->type = MC_FLHGH_T_FTYPE;
  72. mc_filter->file_type = (mc_flhgh_ftype_type) i;
  73. mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
  74. g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
  75. return TRUE;
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. static gboolean
  79. mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
  80. {
  81. mc_fhl_filter_t *mc_filter;
  82. gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
  83. if (*regexp == '\0') {
  84. g_free (regexp);
  85. return FALSE;
  86. }
  87. mc_filter = g_new0 (mc_fhl_filter_t, 1);
  88. mc_filter->type = MC_FLHGH_T_FREGEXP;
  89. mc_filter->search_condition = mc_search_new (regexp, -1);
  90. mc_filter->search_condition->is_case_sensitive = TRUE;
  91. mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
  92. mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
  93. g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
  94. g_free (regexp);
  95. return TRUE;
  96. }
  97. /* --------------------------------------------------------------------------------------------- */
  98. static gboolean
  99. mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
  100. {
  101. mc_fhl_filter_t *mc_filter;
  102. gchar **exts, **exts_orig;
  103. gchar *esc_ext;
  104. gsize exts_size;
  105. GString *buf = g_string_new ("");
  106. exts_orig = exts =
  107. mc_config_get_string_list (fhl->config, group_name, "extensions", &exts_size);
  108. if (exts_orig == NULL)
  109. return FALSE;
  110. if (exts_orig[0] == NULL) {
  111. g_strfreev (exts_orig);
  112. return FALSE;
  113. }
  114. while (*exts != NULL) {
  115. esc_ext = strutils_regex_escape (*exts);
  116. if (buf->len != 0)
  117. g_string_append_c (buf, '|');
  118. g_string_append (buf, esc_ext);
  119. g_free (esc_ext);
  120. exts++;
  121. }
  122. g_strfreev (exts_orig);
  123. esc_ext = g_string_free (buf, FALSE);
  124. buf = g_string_new (".*\\.(");
  125. g_string_append (buf, esc_ext);
  126. g_string_append (buf, ")$");
  127. g_free (esc_ext);
  128. mc_filter = g_new0 (mc_fhl_filter_t, 1);
  129. mc_filter->type = MC_FLHGH_T_FREGEXP;
  130. mc_filter->search_condition = mc_search_new (buf->str, -1);
  131. mc_filter->search_condition->is_case_sensitive =
  132. mc_config_get_bool (fhl->config, group_name, "extensions_case", TRUE);
  133. mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
  134. mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
  135. g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
  136. g_string_free (buf, TRUE);
  137. return TRUE;
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. /*** public functions ****************************************************************************/
  141. /* --------------------------------------------------------------------------------------------- */
  142. gboolean
  143. mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
  144. {
  145. if (fhl == NULL || filename == NULL || !exist_file (filename))
  146. return FALSE;
  147. if (fhl->config) {
  148. if (!mc_config_read_file (fhl->config, filename))
  149. return FALSE;
  150. return TRUE;
  151. }
  152. fhl->config = mc_config_init (filename);
  153. if (fhl->config == NULL)
  154. return FALSE;
  155. return TRUE;
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. gboolean
  159. mc_fhl_init_from_standart_files (mc_fhl_t * fhl)
  160. {
  161. gchar *name;
  162. /* ${datadir}/mc/filehighlight.ini */
  163. name = concat_dir_and_file (mc_home_alt, MC_FHL_INI_FILE);
  164. if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
  165. g_free (name);
  166. return FALSE;
  167. }
  168. g_free (name);
  169. /* ${sysconfdir}/mc/filehighlight.ini */
  170. name = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
  171. if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
  172. g_free (name);
  173. return FALSE;
  174. }
  175. g_free (name);
  176. /* ~/.mc/filehighlight.ini */
  177. name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
  178. if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
  179. g_free (name);
  180. return FALSE;
  181. }
  182. g_free (name);
  183. return TRUE;
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. gboolean
  187. mc_fhl_parse_ini_file (mc_fhl_t * fhl)
  188. {
  189. gchar **group_names, **orig_group_names;
  190. mc_fhl_array_free (fhl);
  191. fhl->filters = g_ptr_array_new ();
  192. orig_group_names = group_names = mc_config_get_groups (fhl->config, NULL);
  193. if (group_names == NULL)
  194. return FALSE;
  195. while (*group_names) {
  196. if (mc_config_has_param (fhl->config, *group_names, "type")) {
  197. /* parse filetype filter */
  198. mc_fhl_parse_get_file_type_id (fhl, *group_names);
  199. }
  200. if (mc_config_has_param (fhl->config, *group_names, "regexp")) {
  201. /* parse regexp filter */
  202. mc_fhl_parse_get_regexp (fhl, *group_names);
  203. }
  204. if (mc_config_has_param (fhl->config, *group_names, "extensions")) {
  205. /* parse extensions filter */
  206. mc_fhl_parse_get_extensions (fhl, *group_names);
  207. }
  208. group_names++;
  209. }
  210. g_strfreev (orig_group_names);
  211. return TRUE;
  212. }
  213. /* --------------------------------------------------------------------------------------------- */