summaryrefslogtreecommitdiffstats
path: root/lib/filehighlight/get-color.c
blob: 41224083c67449b94582fed527aa7a54db7e2a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
   File highlight plugin.
   Interface functions. get color pair index for highlighted file.

   Copyright (C) 2009-2024
   Free Software Foundation, Inc.

   Written by:
   Slava Zanko <slavazanko@gmail.com>, 2009.

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <string.h>

#include "lib/global.h"
#include "lib/skin.h"
#include "lib/util.h"           /* is_exe() */
#include "lib/filehighlight.h"
#include "internal.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** forward declarations (file scope functions) *************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/*inline functions */
inline static gboolean
mc_fhl_is_file (const file_entry_t * fe)
{
#if HAVE_S_ISREG == 0
    (void) fe;
#endif
    return S_ISREG (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_file_exec (const file_entry_t * fe)
{
    return is_exe (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_dir (const file_entry_t * fe)
{
#if HAVE_S_ISDIR == 0
    (void) fe;
#endif
    return S_ISDIR (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_link (const file_entry_t * fe)
{
#if HAVE_S_ISLNK == 0
    (void) fe;
#endif
    return S_ISLNK (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_hlink (const file_entry_t * fe)
{
    return (fe->st.st_nlink > 1);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_link_to_dir (const file_entry_t * fe)
{
    return mc_fhl_is_link (fe) && fe->f.link_to_dir != 0;
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_stale_link (const file_entry_t * fe)
{
    return mc_fhl_is_link (fe) ? (fe->f.stale_link != 0) : !mc_fhl_is_file (fe);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_device_char (const file_entry_t * fe)
{
#if HAVE_S_ISCHR == 0
    (void) fe;
#endif
    return S_ISCHR (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_device_block (const file_entry_t * fe)
{
#if HAVE_S_ISBLK == 0
    (void) fe;
#endif
    return S_ISBLK (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_special_socket (const file_entry_t * fe)
{
#if HAVE_S_ISSOCK == 0
    (void) fe;
#endif
    return S_ISSOCK (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_special_fifo (const file_entry_t * fe)
{
#if HAVE_S_ISFIFO == 0
    (void) fe;
#endif
    return S_ISFIFO (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_special_door (const file_entry_t * fe)
{
#if HAVE_S_ISDOOR == 0
    (void) fe;
#endif
    return S_ISDOOR (fe->st.st_mode);
}

/* --------------------------------------------------------------------------------------------- */

inline static gboolean
mc_fhl_is_special (const file_entry_t * fe)
{
    return
        (mc_fhl_is_special_socket (fe) || mc_fhl_is_special_fifo (fe)
         || mc_fhl_is_special_door (fe));
}

/* --------------------------------------------------------------------------------------------- */

static int
mc_fhl_get_color_filetype (const mc_fhl_filter_t * mc_filter, const mc_fhl_t * fhl,
                           const file_entry_t * fe)
{
    gboolean my_color = FALSE;

    (void) fhl;

    switch (mc_filter->file_type)
    {
    case MC_FLHGH_FTYPE_T_FILE:
        if (mc_fhl_is_file (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_FILE_EXE:
        if (mc_fhl_is_file (fe) && mc_fhl_is_file_exec (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_DIR:
        if (mc_fhl_is_dir (fe) || mc_fhl_is_link_to_dir (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_LINK_DIR:
        if (mc_fhl_is_link_to_dir (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_LINK:
        if (mc_fhl_is_link (fe) || mc_fhl_is_hlink (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_HARDLINK:
        if (mc_fhl_is_hlink (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_SYMLINK:
        if (mc_fhl_is_link (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_STALE_LINK:
        if (mc_fhl_is_stale_link (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_DEVICE:
        if (mc_fhl_is_device_char (fe) || mc_fhl_is_device_block (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_DEVICE_BLOCK:
        if (mc_fhl_is_device_block (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_DEVICE_CHAR:
        if (mc_fhl_is_device_char (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_SPECIAL:
        if (mc_fhl_is_special (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_SPECIAL_SOCKET:
        if (mc_fhl_is_special_socket (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_SPECIAL_FIFO:
        if (mc_fhl_is_special_fifo (fe))
            my_color = TRUE;
        break;
    case MC_FLHGH_FTYPE_T_SPECIAL_DOOR:
        if (mc_fhl_is_special_door (fe))
            my_color = TRUE;
        break;
    default:
        break;
    }

    return my_color ? mc_filter->color_pair_index : -1;
}

/* --------------------------------------------------------------------------------------------- */

static int
mc_fhl_get_color_regexp (const mc_fhl_filter_t * mc_filter, const mc_fhl_t * fhl,
                         const file_entry_t * fe)
{
    (void) fhl;

    if (mc_filter->search_condition == NULL)
        return -1;

    if (mc_search_run (mc_filter->search_condition, fe->fname->str, 0, fe->fname->len, NULL))
        return mc_filter->color_pair_index;

    return -1;
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

int
mc_fhl_get_color (const mc_fhl_t * fhl, const file_entry_t * fe)
{
    guint i;
    int ret;

    if (fhl == NULL)
        return NORMAL_COLOR;

    for (i = 0; i < fhl->filters->len; i++)
    {
        mc_fhl_filter_t *mc_filter;

        mc_filter = (mc_fhl_filter_t *) g_ptr_array_index (fhl->filters, i);
        switch (mc_filter->type)
        {
        case MC_FLHGH_T_FTYPE:
            ret = mc_fhl_get_color_filetype (mc_filter, fhl, fe);
            if (ret > 0)
                return -ret;
            break;
        case MC_FLHGH_T_EXT:
        case MC_FLHGH_T_FREGEXP:
            ret = mc_fhl_get_color_regexp (mc_filter, fhl, fe);
            if (ret > 0)
                return -ret;
            break;
        default:
            break;
        }
    }
    return NORMAL_COLOR;
}

/* --------------------------------------------------------------------------------------------- */