get-color.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. File highlight plugin.
  3. Interface functions. get color pair index for highlighted file.
  4. Copyright (C) 2009-2023
  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/skin.h"
  24. #include "lib/util.h" /* is_exe() */
  25. #include "lib/filehighlight.h"
  26. #include "internal.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. /*inline functions */
  34. inline static gboolean
  35. mc_fhl_is_file (const file_entry_t * fe)
  36. {
  37. #if HAVE_S_ISREG == 0
  38. (void) fe;
  39. #endif
  40. return S_ISREG (fe->st.st_mode);
  41. }
  42. inline static gboolean
  43. mc_fhl_is_file_exec (const file_entry_t * fe)
  44. {
  45. return is_exe (fe->st.st_mode);
  46. }
  47. inline static gboolean
  48. mc_fhl_is_dir (const file_entry_t * fe)
  49. {
  50. #if HAVE_S_ISDIR == 0
  51. (void) fe;
  52. #endif
  53. return S_ISDIR (fe->st.st_mode);
  54. }
  55. inline static gboolean
  56. mc_fhl_is_link (const file_entry_t * fe)
  57. {
  58. #if HAVE_S_ISLNK == 0
  59. (void) fe;
  60. #endif
  61. return S_ISLNK (fe->st.st_mode);
  62. }
  63. inline static gboolean
  64. mc_fhl_is_hlink (const file_entry_t * fe)
  65. {
  66. return (fe->st.st_nlink > 1);
  67. }
  68. inline static gboolean
  69. mc_fhl_is_link_to_dir (const file_entry_t * fe)
  70. {
  71. return mc_fhl_is_link (fe) && fe->f.link_to_dir;
  72. }
  73. inline static gboolean
  74. mc_fhl_is_stale_link (const file_entry_t * fe)
  75. {
  76. return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
  77. }
  78. inline static gboolean
  79. mc_fhl_is_device_char (const file_entry_t * fe)
  80. {
  81. #if HAVE_S_ISCHR == 0
  82. (void) fe;
  83. #endif
  84. return S_ISCHR (fe->st.st_mode);
  85. }
  86. inline static gboolean
  87. mc_fhl_is_device_block (const file_entry_t * fe)
  88. {
  89. #if HAVE_S_ISBLK == 0
  90. (void) fe;
  91. #endif
  92. return S_ISBLK (fe->st.st_mode);
  93. }
  94. inline static gboolean
  95. mc_fhl_is_special_socket (const file_entry_t * fe)
  96. {
  97. #if HAVE_S_ISSOCK == 0
  98. (void) fe;
  99. #endif
  100. return S_ISSOCK (fe->st.st_mode);
  101. }
  102. inline static gboolean
  103. mc_fhl_is_special_fifo (const file_entry_t * fe)
  104. {
  105. #if HAVE_S_ISFIFO == 0
  106. (void) fe;
  107. #endif
  108. return S_ISFIFO (fe->st.st_mode);
  109. }
  110. inline static gboolean
  111. mc_fhl_is_special_door (const file_entry_t * fe)
  112. {
  113. #if HAVE_S_ISDOOR == 0
  114. (void) fe;
  115. #endif
  116. return S_ISDOOR (fe->st.st_mode);
  117. }
  118. inline static gboolean
  119. mc_fhl_is_special (const file_entry_t * fe)
  120. {
  121. return
  122. (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
  123. || mc_fhl_is_special_door (fe));
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. static int
  127. mc_fhl_get_color_filetype (const mc_fhl_filter_t * mc_filter, const mc_fhl_t * fhl,
  128. const file_entry_t * 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. default:
  195. break;
  196. }
  197. return my_color ? mc_filter->color_pair_index : -1;
  198. }
  199. /* --------------------------------------------------------------------------------------------- */
  200. static int
  201. mc_fhl_get_color_regexp (const mc_fhl_filter_t * mc_filter, const mc_fhl_t * fhl,
  202. const file_entry_t * fe)
  203. {
  204. (void) fhl;
  205. if (mc_filter->search_condition == NULL)
  206. return -1;
  207. if (mc_search_run (mc_filter->search_condition, fe->fname->str, 0, fe->fname->len, NULL))
  208. return mc_filter->color_pair_index;
  209. return -1;
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. /* --------------------------------------------------------------------------------------------- */
  213. /*** public functions ****************************************************************************/
  214. /* --------------------------------------------------------------------------------------------- */
  215. int
  216. mc_fhl_get_color (const mc_fhl_t * fhl, const file_entry_t * fe)
  217. {
  218. guint i;
  219. int ret;
  220. if (fhl == NULL)
  221. return NORMAL_COLOR;
  222. for (i = 0; i < fhl->filters->len; i++)
  223. {
  224. mc_fhl_filter_t *mc_filter;
  225. mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
  226. switch (mc_filter->type)
  227. {
  228. case MC_FLHGH_T_FTYPE:
  229. ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
  230. if (ret > 0)
  231. return -ret;
  232. break;
  233. case MC_FLHGH_T_EXT:
  234. case MC_FLHGH_T_FREGEXP:
  235. ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
  236. if (ret > 0)
  237. return -ret;
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. return NORMAL_COLOR;
  244. }
  245. /* --------------------------------------------------------------------------------------------- */