get-color.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_link_to_dir (file_entry * fe)
  66. {
  67. return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
  68. }
  69. inline static gboolean
  70. mc_fhl_is_stale_link (file_entry * fe)
  71. {
  72. return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
  73. }
  74. inline static gboolean
  75. mc_fhl_is_device_char (file_entry * fe)
  76. {
  77. #if S_ISCHR == 0
  78. (void) fe;
  79. #endif
  80. return S_ISCHR (fe->st.st_mode);
  81. }
  82. inline static gboolean
  83. mc_fhl_is_device_block (file_entry * fe)
  84. {
  85. #if S_ISBLK == 0
  86. (void) fe;
  87. #endif
  88. return S_ISBLK (fe->st.st_mode);
  89. }
  90. inline static gboolean
  91. mc_fhl_is_special_socket (file_entry * fe)
  92. {
  93. #if S_ISSOCK == 0
  94. (void) fe;
  95. #endif
  96. return S_ISSOCK (fe->st.st_mode);
  97. }
  98. inline static gboolean
  99. mc_fhl_is_special_fifo (file_entry * fe)
  100. {
  101. #if S_ISFIFO == 0
  102. (void) fe;
  103. #endif
  104. return S_ISFIFO (fe->st.st_mode);
  105. }
  106. inline static gboolean
  107. mc_fhl_is_special_door (file_entry * fe)
  108. {
  109. #if S_ISDOOR == 0
  110. (void) fe;
  111. #endif
  112. return S_ISDOOR (fe->st.st_mode);
  113. }
  114. inline static gboolean
  115. mc_fhl_is_special (file_entry * fe)
  116. {
  117. return
  118. (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
  119. || mc_fhl_is_special_door (fe));
  120. }
  121. /* --------------------------------------------------------------------------------------------- */
  122. static int
  123. mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  124. {
  125. gboolean my_color = FALSE;
  126. (void) fhl;
  127. switch (mc_filter->file_type)
  128. {
  129. case MC_FLHGH_FTYPE_T_FILE:
  130. if (mc_fhl_is_file (fe))
  131. my_color = TRUE;
  132. break;
  133. case MC_FLHGH_FTYPE_T_FILE_EXE:
  134. if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
  135. my_color = TRUE;
  136. break;
  137. case MC_FLHGH_FTYPE_T_DIR:
  138. if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
  139. my_color = TRUE;
  140. break;
  141. case MC_FLHGH_FTYPE_T_LINK_DIR:
  142. if (mc_fhl_is_link_to_dir (fe))
  143. my_color = TRUE;
  144. break;
  145. case MC_FLHGH_FTYPE_T_LINK:
  146. if (mc_fhl_is_link (fe))
  147. my_color = TRUE;
  148. break;
  149. case MC_FLHGH_FTYPE_T_HARDLINK:
  150. /*TODO: hanlde it */
  151. if (mc_fhl_is_link (fe))
  152. my_color = TRUE;
  153. break;
  154. case MC_FLHGH_FTYPE_T_SYMLINK:
  155. /*TODO: hanlde it */
  156. if (mc_fhl_is_link (fe))
  157. my_color = TRUE;
  158. break;
  159. case MC_FLHGH_FTYPE_T_STALE_LINK:
  160. if (mc_fhl_is_stale_link (fe))
  161. my_color = TRUE;
  162. break;
  163. case MC_FLHGH_FTYPE_T_DEVICE:
  164. if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
  165. my_color = TRUE;
  166. break;
  167. case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
  168. if (mc_fhl_is_device_block (fe))
  169. my_color = TRUE;
  170. break;
  171. case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
  172. if (mc_fhl_is_device_char (fe))
  173. my_color = TRUE;
  174. break;
  175. case MC_FLHGH_FTYPE_T_SPECIAL:
  176. if (mc_fhl_is_special (fe))
  177. my_color = TRUE;
  178. break;
  179. case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
  180. if (mc_fhl_is_special_socket (fe))
  181. my_color = TRUE;
  182. break;
  183. case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
  184. if (mc_fhl_is_special_fifo (fe))
  185. my_color = TRUE;
  186. break;
  187. case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
  188. if (mc_fhl_is_special_door (fe))
  189. my_color = TRUE;
  190. break;
  191. }
  192. return (my_color) ? mc_filter->color_pair_index : -1;
  193. }
  194. /* --------------------------------------------------------------------------------------------- */
  195. static int
  196. mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  197. {
  198. (void) fhl;
  199. if (mc_filter->search_condition == NULL)
  200. return -1;
  201. if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
  202. return mc_filter->color_pair_index;
  203. return -1;
  204. }
  205. /* --------------------------------------------------------------------------------------------- */
  206. /* --------------------------------------------------------------------------------------------- */
  207. /*** public functions ****************************************************************************/
  208. /* --------------------------------------------------------------------------------------------- */
  209. int
  210. mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
  211. {
  212. guint i;
  213. mc_fhl_filter_t *mc_filter;
  214. int ret;
  215. if (fhl == NULL)
  216. return NORMAL_COLOR;
  217. for (i = 0; i < fhl->filters->len; i++)
  218. {
  219. mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
  220. switch (mc_filter->type)
  221. {
  222. case MC_FLHGH_T_FTYPE:
  223. ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
  224. if (ret > 0)
  225. return -ret;
  226. break;
  227. case MC_FLHGH_T_EXT:
  228. case MC_FLHGH_T_FREGEXP:
  229. ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
  230. if (ret > 0)
  231. return -ret;
  232. break;
  233. }
  234. }
  235. return NORMAL_COLOR;
  236. }
  237. /* --------------------------------------------------------------------------------------------- */