Cleanup: avoid warnings for mc-library on x86_64
[midnight-commander.git] / lib / filehighlight / get-color.c
blob7ad0efdd8baa21a06a04f77904d48d0cf5731d6d
1 /*
2 File highlight plugin.
3 Interface functions. get color pair index for highlighted file.
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <string.h>
32 #include "lib/global.h"
33 #include "lib/skin.h"
34 #include "lib/filehighlight.h"
35 #include "internal.h"
37 /*** global variables ****************************************************************************/
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 /*** file scope functions ************************************************************************/
46 /* --------------------------------------------------------------------------------------------- */
47 /*inline functions*/
48 inline static gboolean
49 mc_fhl_is_file (file_entry * fe)
51 #if S_ISREG == 0
52 (void) fe;
53 #endif
54 return S_ISREG (fe->st.st_mode);
57 inline static gboolean
58 mc_fhl_is_file_exec (file_entry * fe)
60 return is_exe (fe->st.st_mode);
63 inline static gboolean
64 mc_fhl_is_dir (file_entry * fe)
66 #if S_ISDIR == 0
67 (void) fe;
68 #endif
69 return S_ISDIR (fe->st.st_mode);
72 inline static gboolean
73 mc_fhl_is_link (file_entry * fe)
75 #if S_ISLNK == 0
76 (void) fe;
77 #endif
78 return S_ISLNK (fe->st.st_mode);
81 inline static gboolean
82 mc_fhl_is_link_to_dir (file_entry * fe)
84 return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
87 inline static gboolean
88 mc_fhl_is_stale_link (file_entry * fe)
90 return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
93 inline static gboolean
94 mc_fhl_is_device_char (file_entry * fe)
96 #if S_ISCHR == 0
97 (void) fe;
98 #endif
99 return S_ISCHR (fe->st.st_mode);
102 inline static gboolean
103 mc_fhl_is_device_block (file_entry * fe)
105 #if S_ISBLK == 0
106 (void) fe;
107 #endif
108 return S_ISBLK (fe->st.st_mode);
111 inline static gboolean
112 mc_fhl_is_special_socket (file_entry * fe)
114 #if S_ISSOCK == 0
115 (void) fe;
116 #endif
117 return S_ISSOCK (fe->st.st_mode);
120 inline static gboolean
121 mc_fhl_is_special_fifo (file_entry * fe)
123 #if S_ISFIFO == 0
124 (void) fe;
125 #endif
126 return S_ISFIFO (fe->st.st_mode);
129 inline static gboolean
130 mc_fhl_is_special_door (file_entry * fe)
132 #if S_ISDOOR == 0
133 (void) fe;
134 #endif
136 return S_ISDOOR (fe->st.st_mode);
140 inline static gboolean
141 mc_fhl_is_special (file_entry * fe)
143 return
144 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe) || mc_fhl_is_special_door (fe)
149 /* --------------------------------------------------------------------------------------------- */
151 static int
152 mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
154 gboolean my_color = FALSE;
155 (void) fhl;
157 switch (mc_filter->file_type) {
158 case MC_FLHGH_FTYPE_T_FILE:
159 if (mc_fhl_is_file (fe))
160 my_color = TRUE;
161 break;
162 case MC_FLHGH_FTYPE_T_FILE_EXE:
163 if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
164 my_color = TRUE;
165 break;
166 case MC_FLHGH_FTYPE_T_DIR:
167 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
168 my_color = TRUE;
169 break;
170 case MC_FLHGH_FTYPE_T_LINK_DIR:
171 if (mc_fhl_is_link_to_dir (fe))
172 my_color = TRUE;
173 break;
174 case MC_FLHGH_FTYPE_T_LINK:
175 if (mc_fhl_is_link (fe))
176 my_color = TRUE;
177 break;
178 case MC_FLHGH_FTYPE_T_HARDLINK:
179 /*TODO: hanlde it */
180 if (mc_fhl_is_link (fe))
181 my_color = TRUE;
182 break;
183 case MC_FLHGH_FTYPE_T_SYMLINK:
184 /*TODO: hanlde it */
185 if (mc_fhl_is_link (fe))
186 my_color = TRUE;
187 break;
188 case MC_FLHGH_FTYPE_T_STALE_LINK:
189 if (mc_fhl_is_stale_link (fe))
190 my_color = TRUE;
191 break;
192 case MC_FLHGH_FTYPE_T_DEVICE:
193 if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
194 my_color = TRUE;
195 break;
196 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
197 if (mc_fhl_is_device_block (fe))
198 my_color = TRUE;
199 break;
200 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
201 if (mc_fhl_is_device_char (fe))
202 my_color = TRUE;
203 break;
204 case MC_FLHGH_FTYPE_T_SPECIAL:
205 if (mc_fhl_is_special (fe))
206 my_color = TRUE;
207 break;
208 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
209 if (mc_fhl_is_special_socket (fe))
210 my_color = TRUE;
211 break;
212 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
213 if (mc_fhl_is_special_fifo (fe))
214 my_color = TRUE;
215 break;
216 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
217 if (mc_fhl_is_special_door (fe))
218 my_color = TRUE;
219 break;
222 return (my_color) ? mc_filter->color_pair_index : -1;
225 /* --------------------------------------------------------------------------------------------- */
227 static int
228 mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
230 (void) fhl;
231 if (mc_filter->search_condition == NULL)
232 return -1;
234 if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
235 return mc_filter->color_pair_index;
237 return -1;
240 /* --------------------------------------------------------------------------------------------- */
242 /* --------------------------------------------------------------------------------------------- */
243 /*** public functions ****************************************************************************/
244 /* --------------------------------------------------------------------------------------------- */
248 mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
250 guint i;
251 mc_fhl_filter_t *mc_filter;
252 int ret;
254 if (fhl == NULL)
255 return NORMAL_COLOR;
257 for (i = 0; i < fhl->filters->len; i++) {
258 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
259 switch (mc_filter->type) {
260 case MC_FLHGH_T_FTYPE:
261 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
262 if (ret > 0)
263 return -ret;
264 break;
265 case MC_FLHGH_T_EXT:
266 case MC_FLHGH_T_FREGEXP:
267 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
268 if (ret > 0)
269 return -ret;
270 break;
273 return NORMAL_COLOR;
276 /* --------------------------------------------------------------------------------------------- */