get-color.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. File highlight plugin.
  3. Interface functions. get color pair index for highlighted file.
  4. Copyright (C) 2009, 2011
  5. The 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 (file_entry * fe)
  36. {
  37. #if 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 (file_entry * fe)
  44. {
  45. return is_exe (fe->st.st_mode);
  46. }
  47. inline static gboolean
  48. mc_fhl_is_dir (file_entry * fe)
  49. {
  50. #if 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 (file_entry * fe)
  57. {
  58. #if 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 (file_entry * fe)
  65. {
  66. return (fe->st.st_nlink > 1);
  67. }
  68. inline static gboolean
  69. mc_fhl_is_link_to_dir (file_entry * 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 (file_entry * 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 (file_entry * fe)
  80. {
  81. #if 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 (file_entry * fe)
  88. {
  89. #if 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 (file_entry * fe)
  96. {
  97. #if 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 (file_entry * fe)
  104. {
  105. #if 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 (file_entry * fe)
  112. {
  113. #if 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 (file_entry * 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 (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  128. {
  129. gboolean my_color = FALSE;
  130. (void) fhl;
  131. switch (mc_filter->file_type)
  132. {
  133. case MC_FLHGH_FTYPE_T_FILE:
  134. if (mc_fhl_is_file (fe))
  135. my_color = TRUE;
  136. break;
  137. case MC_FLHGH_FTYPE_T_FILE_EXE:
  138. if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
  139. my_color = TRUE;
  140. break;
  141. case MC_FLHGH_FTYPE_T_DIR:
  142. if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
  143. my_color = TRUE;
  144. break;
  145. case MC_FLHGH_FTYPE_T_LINK_DIR:
  146. if (mc_fhl_is_link_to_dir (fe))
  147. my_color = TRUE;
  148. break;
  149. case MC_FLHGH_FTYPE_T_LINK:
  150. if ((mc_fhl_is_link (fe)) || (mc_fhl_is_hlink (fe)))
  151. my_color = TRUE;
  152. break;
  153. case MC_FLHGH_FTYPE_T_HARDLINK:
  154. if (mc_fhl_is_hlink (fe))
  155. my_color = TRUE;
  156. break;
  157. case MC_FLHGH_FTYPE_T_SYMLINK:
  158. if (mc_fhl_is_link (fe))
  159. my_color = TRUE;
  160. break;
  161. case MC_FLHGH_FTYPE_T_STALE_LINK:
  162. if (mc_fhl_is_stale_link (fe))
  163. my_color = TRUE;
  164. break;
  165. case MC_FLHGH_FTYPE_T_DEVICE:
  166. if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
  167. my_color = TRUE;
  168. break;
  169. case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
  170. if (mc_fhl_is_device_block (fe))
  171. my_color = TRUE;
  172. break;
  173. case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
  174. if (mc_fhl_is_device_char (fe))
  175. my_color = TRUE;
  176. break;
  177. case MC_FLHGH_FTYPE_T_SPECIAL:
  178. if (mc_fhl_is_special (fe))
  179. my_color = TRUE;
  180. break;
  181. case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
  182. if (mc_fhl_is_special_socket (fe))
  183. my_color = TRUE;
  184. break;
  185. case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
  186. if (mc_fhl_is_special_fifo (fe))
  187. my_color = TRUE;
  188. break;
  189. case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
  190. if (mc_fhl_is_special_door (fe))
  191. my_color = TRUE;
  192. break;
  193. }
  194. return (my_color) ? mc_filter->color_pair_index : -1;
  195. }
  196. /* --------------------------------------------------------------------------------------------- */
  197. static int
  198. mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
  199. {
  200. (void) fhl;
  201. if (mc_filter->search_condition == NULL)
  202. return -1;
  203. if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
  204. return mc_filter->color_pair_index;
  205. return -1;
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. /* --------------------------------------------------------------------------------------------- */
  209. /*** public functions ****************************************************************************/
  210. /* --------------------------------------------------------------------------------------------- */
  211. int
  212. mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
  213. {
  214. guint i;
  215. mc_fhl_filter_t *mc_filter;
  216. int ret;
  217. if (fhl == NULL)
  218. return NORMAL_COLOR;
  219. for (i = 0; i < fhl->filters->len; i++)
  220. {
  221. mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
  222. switch (mc_filter->type)
  223. {
  224. case MC_FLHGH_T_FTYPE:
  225. ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
  226. if (ret > 0)
  227. return -ret;
  228. break;
  229. case MC_FLHGH_T_EXT:
  230. case MC_FLHGH_T_FREGEXP:
  231. ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
  232. if (ret > 0)
  233. return -ret;
  234. break;
  235. }
  236. }
  237. return NORMAL_COLOR;
  238. }
  239. /* --------------------------------------------------------------------------------------------- */