Remove unneeded `struct` keyword for typedef'd structs
[midnight-commander.git] / lib / filehighlight / get-color.c
blob5538be37658b3b0eff158851497636bd7c03e33e
1 /*
2 File highlight plugin.
3 Interface functions. get color pair index for highlighted file.
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
28 #include <string.h>
30 #include "lib/global.h"
31 #include "lib/skin.h"
32 #include "lib/util.h" /* is_exe() */
33 #include "lib/filehighlight.h"
34 #include "internal.h"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 /*** file scope type declarations ****************************************************************/
42 /*** file scope variables ************************************************************************/
44 /*** file scope functions ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
48 /*inline functions */
49 inline static gboolean
50 mc_fhl_is_file (file_entry_t * fe)
52 #if HAVE_S_ISREG == 0
53 (void) fe;
54 #endif
55 return S_ISREG (fe->st.st_mode);
58 inline static gboolean
59 mc_fhl_is_file_exec (file_entry_t * fe)
61 return is_exe (fe->st.st_mode);
64 inline static gboolean
65 mc_fhl_is_dir (file_entry_t * fe)
67 #if HAVE_S_ISDIR == 0
68 (void) fe;
69 #endif
70 return S_ISDIR (fe->st.st_mode);
73 inline static gboolean
74 mc_fhl_is_link (file_entry_t * fe)
76 #if HAVE_S_ISLNK == 0
77 (void) fe;
78 #endif
79 return S_ISLNK (fe->st.st_mode);
82 inline static gboolean
83 mc_fhl_is_hlink (file_entry_t * fe)
85 return (fe->st.st_nlink > 1);
88 inline static gboolean
89 mc_fhl_is_link_to_dir (file_entry_t * fe)
91 return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
94 inline static gboolean
95 mc_fhl_is_stale_link (file_entry_t * fe)
97 return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
100 inline static gboolean
101 mc_fhl_is_device_char (file_entry_t * fe)
103 #if HAVE_S_ISCHR == 0
104 (void) fe;
105 #endif
106 return S_ISCHR (fe->st.st_mode);
109 inline static gboolean
110 mc_fhl_is_device_block (file_entry_t * fe)
112 #if HAVE_S_ISBLK == 0
113 (void) fe;
114 #endif
115 return S_ISBLK (fe->st.st_mode);
118 inline static gboolean
119 mc_fhl_is_special_socket (file_entry_t * fe)
121 #if HAVE_S_ISSOCK == 0
122 (void) fe;
123 #endif
124 return S_ISSOCK (fe->st.st_mode);
127 inline static gboolean
128 mc_fhl_is_special_fifo (file_entry_t * fe)
130 #if HAVE_S_ISFIFO == 0
131 (void) fe;
132 #endif
133 return S_ISFIFO (fe->st.st_mode);
136 inline static gboolean
137 mc_fhl_is_special_door (file_entry_t * fe)
139 #if HAVE_S_ISDOOR == 0
140 (void) fe;
141 #endif
143 return S_ISDOOR (fe->st.st_mode);
147 inline static gboolean
148 mc_fhl_is_special (file_entry_t * fe)
150 return
151 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
152 || mc_fhl_is_special_door (fe));
156 /* --------------------------------------------------------------------------------------------- */
158 static int
159 mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry_t * fe)
161 gboolean my_color = FALSE;
162 (void) fhl;
164 switch (mc_filter->file_type)
166 case MC_FLHGH_FTYPE_T_FILE:
167 if (mc_fhl_is_file (fe))
168 my_color = TRUE;
169 break;
170 case MC_FLHGH_FTYPE_T_FILE_EXE:
171 if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
172 my_color = TRUE;
173 break;
174 case MC_FLHGH_FTYPE_T_DIR:
175 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
176 my_color = TRUE;
177 break;
178 case MC_FLHGH_FTYPE_T_LINK_DIR:
179 if (mc_fhl_is_link_to_dir (fe))
180 my_color = TRUE;
181 break;
182 case MC_FLHGH_FTYPE_T_LINK:
183 if ((mc_fhl_is_link (fe)) || (mc_fhl_is_hlink (fe)))
184 my_color = TRUE;
185 break;
186 case MC_FLHGH_FTYPE_T_HARDLINK:
187 if (mc_fhl_is_hlink (fe))
188 my_color = TRUE;
189 break;
190 case MC_FLHGH_FTYPE_T_SYMLINK:
191 if (mc_fhl_is_link (fe))
192 my_color = TRUE;
193 break;
194 case MC_FLHGH_FTYPE_T_STALE_LINK:
195 if (mc_fhl_is_stale_link (fe))
196 my_color = TRUE;
197 break;
198 case MC_FLHGH_FTYPE_T_DEVICE:
199 if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
200 my_color = TRUE;
201 break;
202 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
203 if (mc_fhl_is_device_block (fe))
204 my_color = TRUE;
205 break;
206 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
207 if (mc_fhl_is_device_char (fe))
208 my_color = TRUE;
209 break;
210 case MC_FLHGH_FTYPE_T_SPECIAL:
211 if (mc_fhl_is_special (fe))
212 my_color = TRUE;
213 break;
214 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
215 if (mc_fhl_is_special_socket (fe))
216 my_color = TRUE;
217 break;
218 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
219 if (mc_fhl_is_special_fifo (fe))
220 my_color = TRUE;
221 break;
222 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
223 if (mc_fhl_is_special_door (fe))
224 my_color = TRUE;
225 break;
226 default:
227 break;
230 return (my_color) ? mc_filter->color_pair_index : -1;
233 /* --------------------------------------------------------------------------------------------- */
235 static int
236 mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry_t * fe)
238 (void) fhl;
239 if (mc_filter->search_condition == NULL)
240 return -1;
242 if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
243 return mc_filter->color_pair_index;
245 return -1;
248 /* --------------------------------------------------------------------------------------------- */
250 /* --------------------------------------------------------------------------------------------- */
251 /*** public functions ****************************************************************************/
252 /* --------------------------------------------------------------------------------------------- */
255 mc_fhl_get_color (mc_fhl_t * fhl, file_entry_t * fe)
257 guint i;
258 int ret;
260 if (fhl == NULL)
261 return NORMAL_COLOR;
263 for (i = 0; i < fhl->filters->len; i++)
265 mc_fhl_filter_t *mc_filter;
267 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
268 switch (mc_filter->type)
270 case MC_FLHGH_T_FTYPE:
271 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
272 if (ret > 0)
273 return -ret;
274 break;
275 case MC_FLHGH_T_EXT:
276 case MC_FLHGH_T_FREGEXP:
277 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
278 if (ret > 0)
279 return -ret;
280 break;
281 default:
282 break;
285 return NORMAL_COLOR;
288 /* --------------------------------------------------------------------------------------------- */