get-color.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. File highlight plugin.
  3. Interface functions. get color pair index for highlighted file.
  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/skin.h"
  25. #include "lib/util.h" /* is_exe() */
  26. #include "lib/filehighlight.h"
  27. #include "internal.h"
  28. /*** global variables ****************************************************************************/
  29. /*** file scope macro definitions ****************************************************************/
  30. /*** file scope type declarations ****************************************************************/
  31. /*** file scope variables ************************************************************************/
  32. /*** file scope functions ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. /*inline functions */
  35. inline static gboolean
  36. mc_fhl_is_file (file_entry * fe)
  37. {
  38. #if S_ISREG == 0
  39. (void) fe;
  40. #endif
  41. return S_ISREG (fe->st.st_mode);
  42. }
  43. inline static gboolean
  44. mc_fhl_is_file_exec (file_entry * fe)
  45. {
  46. return is_exe (fe->st.st_mode);
  47. }
  48. inline static gboolean
  49. mc_fhl_is_dir (file_entry * fe)
  50. {
  51. #if S_ISDIR == 0
  52. (void) fe;
  53. #endif
  54. return S_ISDIR (fe->st.st_mode);
  55. }
  56. inline static gboolean
  57. mc_fhl_is_link (file_entry * fe)
  58. {
  59. #if S_ISLNK == 0
  60. (void) fe;
  61. #endif
  62. return S_ISLNK (fe->st.st_mode);
  63. }
  64. inline static gboolean
  65. mc_fhl_is_hlink (file_entry * fe)
  66. {
  67. return (fe->st.st_nlink > 1);
  68. }
  69. inline static gboolean
  70. mc_fhl_is_link_to_dir (file_entry * fe)
  71. {
  72. return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
  73. }
  74. inline static gboolean
  75. mc_fhl_is_stale_link (file_entry * fe)
  76. {
  77. return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
  78. }
  79. inline static gboolean
  80. mc_fhl_is_device_char (file_entry * fe)
  81. {
  82. #if S_ISCHR == 0
  83. (void) fe;
  84. #endif
  85. return S_ISCHR (fe->st.st_mode);
  86. }
  87. inline static gboolean
  88. mc_fhl_is_device_block (file_entry * fe)
  89. {
  90. #if S_ISBLK == 0
  91. (void) fe;
  92. #endif
  93. return S_ISBLK (fe->st.st_mode);
  94. }
  95. inline static gboolean
  96. mc_fhl_is_special_socket (file_entry * fe)
  97. {
  98. #if S_ISSOCK == 0
  99. (void) fe;
  100. #endif
  101. return S_ISSOCK (fe->st.st_mode);
  102. }
  103. inline static gboolean
  104. mc_fhl_is_special_fifo (file_entry * fe)
  105. {
  106. #if S_ISFIFO == 0
  107. (void) fe;
  108. #endif
  109. return S_ISFIFO (fe->st.st_mode);
  110. }
  111. inline static gboolean
  112. mc_fhl_is_special_door (file_entry * fe)
  113. {
  114. #if S_ISDOOR == 0
  115. (void) fe;
  116. #endif
  117. return S_ISDOOR (fe->st.st_mode);
  118. }
  119. inline static gboolean
  120. mc_fhl_is_special (file_entry * fe)
  121. {
  122. return
  123. (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
  124. || mc_fhl_is_special_door (fe));
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. static int
  128. mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  129. {
  130. gboolean my_color = FALSE;
  131. (void) fhl;
  132. switch (mc_filter->file_type)
  133. {
  134. case MC_FLHGH_FTYPE_T_FILE:
  135. if (mc_fhl_is_file (fe))
  136. my_color = TRUE;
  137. break;
  138. case MC_FLHGH_FTYPE_T_FILE_EXE:
  139. if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
  140. my_color = TRUE;
  141. break;
  142. case MC_FLHGH_FTYPE_T_DIR:
  143. if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
  144. my_color = TRUE;
  145. break;
  146. case MC_FLHGH_FTYPE_T_LINK_DIR:
  147. if (mc_fhl_is_link_to_dir (fe))
  148. my_color = TRUE;
  149. break;
  150. case MC_FLHGH_FTYPE_T_LINK:
  151. if ((mc_fhl_is_link (fe)) || (mc_fhl_is_hlink (fe)))
  152. my_color = TRUE;
  153. break;
  154. case MC_FLHGH_FTYPE_T_HARDLINK:
  155. if (mc_fhl_is_hlink (fe))
  156. my_color = TRUE;
  157. break;
  158. case MC_FLHGH_FTYPE_T_SYMLINK:
  159. if (mc_fhl_is_link (fe))
  160. my_color = TRUE;
  161. break;
  162. case MC_FLHGH_FTYPE_T_STALE_LINK:
  163. if (mc_fhl_is_stale_link (fe))
  164. my_color = TRUE;
  165. break;
  166. case MC_FLHGH_FTYPE_T_DEVICE:
  167. if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
  168. my_color = TRUE;
  169. break;
  170. case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
  171. if (mc_fhl_is_device_block (fe))
  172. my_color = TRUE;
  173. break;
  174. case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
  175. if (mc_fhl_is_device_char (fe))
  176. my_color = TRUE;
  177. break;
  178. case MC_FLHGH_FTYPE_T_SPECIAL:
  179. if (mc_fhl_is_special (fe))
  180. my_color = TRUE;
  181. break;
  182. case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
  183. if (mc_fhl_is_special_socket (fe))
  184. my_color = TRUE;
  185. break;
  186. case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
  187. if (mc_fhl_is_special_fifo (fe))
  188. my_color = TRUE;
  189. break;
  190. case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
  191. if (mc_fhl_is_special_door (fe))
  192. my_color = TRUE;
  193. break;
  194. }
  195. return (my_color) ? mc_filter->color_pair_index : -1;
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. static int
  199. mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  200. {
  201. (void) fhl;
  202. if (mc_filter->search_condition == NULL)
  203. return -1;
  204. if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
  205. return mc_filter->color_pair_index;
  206. return -1;
  207. }
  208. /* --------------------------------------------------------------------------------------------- */
  209. /* --------------------------------------------------------------------------------------------- */
  210. /*** public functions ****************************************************************************/
  211. /* --------------------------------------------------------------------------------------------- */
  212. int
  213. mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
  214. {
  215. guint i;
  216. mc_fhl_filter_t *mc_filter;
  217. int ret;
  218. if (fhl == NULL)
  219. return NORMAL_COLOR;
  220. for (i = 0; i < fhl->filters->len; i++)
  221. {
  222. mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
  223. switch (mc_filter->type)
  224. {
  225. case MC_FLHGH_T_FTYPE:
  226. ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
  227. if (ret > 0)
  228. return -ret;
  229. break;
  230. case MC_FLHGH_T_EXT:
  231. case MC_FLHGH_T_FREGEXP:
  232. ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
  233. if (ret > 0)
  234. return -ret;
  235. break;
  236. }
  237. }
  238. return NORMAL_COLOR;
  239. }
  240. /* --------------------------------------------------------------------------------------------- */