Removed calls to strerror() function from own libsamba implementation.
[midnight-commander.git] / src / filehighlight / get-color.c
bloba56811d0a1910b7fced8e0dbb3c4d0bbbf16ccdb
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 "../src/global.h"
33 #include "../src/util.h"
34 #include "../src/skin/skin.h"
35 #include "../src/filehighlight/fhl.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 /*** file scope functions ************************************************************************/
47 /* --------------------------------------------------------------------------------------------- */
48 /*inline functions*/
49 inline static gboolean
50 mc_fhl_is_file (file_entry * fe)
52 #if 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 * fe)
61 return is_exe (fe->st.st_mode);
64 inline static gboolean
65 mc_fhl_is_dir (file_entry * fe)
67 #if 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 * fe)
76 #if S_ISLNK == 0
77 (void) fe;
78 #endif
79 return S_ISLNK (fe->st.st_mode);
82 inline static gboolean
83 mc_fhl_is_link_to_dir (file_entry * fe)
85 return mc_fhl_is_link (fe) && (fe->f.link_to_dir);
88 inline static gboolean
89 mc_fhl_is_stale_link (file_entry * fe)
91 return mc_fhl_is_link (fe) ? fe->f.stale_link : !mc_fhl_is_file (fe);
94 inline static gboolean
95 mc_fhl_is_device_char (file_entry * fe)
97 #if S_ISCHR == 0
98 (void) fe;
99 #endif
100 return S_ISCHR (fe->st.st_mode);
103 inline static gboolean
104 mc_fhl_is_device_block (file_entry * fe)
106 #if S_ISBLK == 0
107 (void) fe;
108 #endif
109 return S_ISBLK (fe->st.st_mode);
112 inline static gboolean
113 mc_fhl_is_special_socket (file_entry * fe)
115 #if S_ISSOCK == 0
116 (void) fe;
117 #endif
118 return S_ISSOCK (fe->st.st_mode);
121 inline static gboolean
122 mc_fhl_is_special_fifo (file_entry * fe)
124 #if S_ISFIFO == 0
125 (void) fe;
126 #endif
127 return S_ISFIFO (fe->st.st_mode);
130 inline static gboolean
131 mc_fhl_is_special_door (file_entry * fe)
133 #if S_ISDOOR == 0
134 (void) fe;
135 #endif
137 return S_ISDOOR (fe->st.st_mode);
141 inline static gboolean
142 mc_fhl_is_special (file_entry * fe)
144 return
145 (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe) || mc_fhl_is_special_door (fe)
150 /* --------------------------------------------------------------------------------------------- */
152 static int
153 mc_fhl_get_color_filetype (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
155 gboolean my_color = FALSE;
156 (void) fhl;
158 switch (mc_filter->file_type) {
159 case MC_FLHGH_FTYPE_T_FILE:
160 if (mc_fhl_is_file (fe))
161 my_color = TRUE;
162 break;
163 case MC_FLHGH_FTYPE_T_FILE_EXE:
164 if ((mc_fhl_is_file (fe)) && (mc_fhl_is_file_exec (fe)))
165 my_color = TRUE;
166 break;
167 case MC_FLHGH_FTYPE_T_DIR:
168 if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
169 my_color = TRUE;
170 break;
171 case MC_FLHGH_FTYPE_T_LINK_DIR:
172 if (mc_fhl_is_link_to_dir (fe))
173 my_color = TRUE;
174 break;
175 case MC_FLHGH_FTYPE_T_LINK:
176 if (mc_fhl_is_link (fe))
177 my_color = TRUE;
178 break;
179 case MC_FLHGH_FTYPE_T_HARDLINK:
180 /*TODO: hanlde it */
181 if (mc_fhl_is_link (fe))
182 my_color = TRUE;
183 break;
184 case MC_FLHGH_FTYPE_T_SYMLINK:
185 /*TODO: hanlde it */
186 if (mc_fhl_is_link (fe))
187 my_color = TRUE;
188 break;
189 case MC_FLHGH_FTYPE_T_STALE_LINK:
190 if (mc_fhl_is_stale_link (fe))
191 my_color = TRUE;
192 break;
193 case MC_FLHGH_FTYPE_T_DEVICE:
194 if ((mc_fhl_is_device_char (fe)) || (mc_fhl_is_device_block (fe)))
195 my_color = TRUE;
196 break;
197 case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
198 if (mc_fhl_is_device_block (fe))
199 my_color = TRUE;
200 break;
201 case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
202 if (mc_fhl_is_device_char (fe))
203 my_color = TRUE;
204 break;
205 case MC_FLHGH_FTYPE_T_SPECIAL:
206 if (mc_fhl_is_special (fe))
207 my_color = TRUE;
208 break;
209 case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
210 if (mc_fhl_is_special_socket (fe))
211 my_color = TRUE;
212 break;
213 case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
214 if (mc_fhl_is_special_fifo (fe))
215 my_color = TRUE;
216 break;
217 case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
218 if (mc_fhl_is_special_door (fe))
219 my_color = TRUE;
220 break;
223 return (my_color) ? mc_filter->color_pair_index : -1;
226 /* --------------------------------------------------------------------------------------------- */
228 static int
229 mc_fhl_get_color_regexp (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, file_entry * fe)
231 (void) fhl;
232 if (mc_filter->search_condition == NULL)
233 return -1;
235 if (mc_search_run (mc_filter->search_condition, fe->fname, 0, strlen (fe->fname), NULL))
236 return mc_filter->color_pair_index;
238 return -1;
241 /* --------------------------------------------------------------------------------------------- */
243 /* --------------------------------------------------------------------------------------------- */
244 /*** public functions ****************************************************************************/
245 /* --------------------------------------------------------------------------------------------- */
249 mc_fhl_get_color (mc_fhl_t * fhl, file_entry * fe)
251 guint i;
252 mc_fhl_filter_t *mc_filter;
253 int ret;
255 if (fhl == NULL)
256 return NORMAL_COLOR;
258 for (i = 0; i < fhl->filters->len; i++) {
259 mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
260 switch (mc_filter->type) {
261 case MC_FLHGH_T_FTYPE:
262 ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
263 if (ret > 0)
264 return -ret;
265 break;
266 case MC_FLHGH_T_EXT:
267 case MC_FLHGH_T_FREGEXP:
268 ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
269 if (ret > 0)
270 return -ret;
271 break;
274 return NORMAL_COLOR;
277 /* --------------------------------------------------------------------------------------------- */