summaryrefslogtreecommitdiffstats
path: root/plug-ins/common/file-glob.c
blob: 6ce0e00d0726e51be647ba0e4054e9f25846dd2d (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program 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.
 *
 * This program 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 <https://www.gnu.org/licenses/>.
 */

/* The idea is taken from a plug-in written by George Hartz; the code isn't.
 */

#include "config.h"

#include <string.h>

#include "libgimp/gimp.h"


#define PLUG_IN_PROC "file-glob"


static void      query        (void);
static void      run          (const gchar      *name,
                               gint              nparams,
                               const GimpParam  *param,
                               gint             *nreturn_vals,
                               GimpParam       **return_vals);

static gboolean  glob_match   (const gchar      *pattern,
                               gboolean          filename_encoding,
                               gint             *num_matches,
                               gchar          ***matches);
static gboolean  glob_fnmatch (const gchar      *pattern,
                               const gchar      *string);


const GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,
  NULL,
  query,
  run,
};

MAIN ()

static void
query (void)
{
  static const GimpParamDef glob_args[] =
  {
    { GIMP_PDB_STRING,  "pattern" ,  "The glob pattern (in UTF-8 encoding)" },
    { GIMP_PDB_INT32,   "encoding",  "Encoding of the returned names: "
                                     "{ UTF-8 (0), filename encoding (1) }" }
  };

  static const GimpParamDef glob_return_vals[] =
  {
    { GIMP_PDB_INT32,       "num-files", "The number of returned names" },
    { GIMP_PDB_STRINGARRAY, "files",     "The list of matching names"   }
  };


  gimp_install_procedure (PLUG_IN_PROC,
                          "Returns a list of matching filenames",
                          "This can be useful in scripts and other plug-ins "
                          "(e.g., batch-conversion). See the glob(7) manpage "
                          "for more info. Note however that this isn't a "
                          "full-featured glob implementation. It only handles "
                          "simple patterns like \"/home/foo/bar/*.jpg\".",
                          "Sven Neumann",
                          "Sven Neumann",
                          "2004",
                          NULL,
                          NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (glob_args),
                          G_N_ELEMENTS (glob_return_vals),
                          glob_args,
                          glob_return_vals);
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam values[3];

  *nreturn_vals = 1;
  *return_vals  = values;

  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;

  if (strcmp (name, PLUG_IN_PROC) == 0 && nparams >= 1)
    {
      gchar    **matches;
      gint       num_matches;
      gboolean   filename_encoding = FALSE;

      if (nparams > 1)
        filename_encoding = param[0].data.d_int32 ? TRUE : FALSE;

      if (! glob_match (param[0].data.d_string, filename_encoding,
                        &num_matches, &matches))
        {
          values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
          return;
        }

      *nreturn_vals = 3;

      values[0].type               = GIMP_PDB_STATUS;
      values[0].data.d_status      = GIMP_PDB_SUCCESS;

      values[1].type               = GIMP_PDB_INT32;
      values[1].data.d_int32       = num_matches;

      values[2].type               = GIMP_PDB_STRINGARRAY;
      values[2].data.d_stringarray = matches;
    }
}

static gboolean
glob_match (const gchar   *pattern,
            gboolean       filename_encoding,
            gint          *num_matches,
            gchar       ***matches)
{
  GDir        *dir;
  GPtrArray   *array;
  const gchar *filename;
  gchar       *dirname;
  gchar       *tmp;

  g_return_val_if_fail (pattern != NULL, FALSE);
  g_return_val_if_fail (num_matches != NULL, FALSE);
  g_return_val_if_fail (matches != NULL, FALSE);

  *num_matches = 0;
  *matches     = NULL;

  /*  This is not a complete glob() implementation but rather a very
   *  simplistic approach. However it works for the most common use
   *  case and is better than nothing.
   */

  tmp = g_filename_from_utf8 (pattern, -1, NULL, NULL, NULL);
  if (! tmp)
    return FALSE;

  dirname = g_path_get_dirname (tmp);

  dir = g_dir_open (dirname, 0, NULL);
  g_free (tmp);

  if (! dir)
    {
      g_free (dirname);
      return TRUE;
    }

  /*  check if the pattern has a directory part at all  */
  tmp = g_path_get_basename (pattern);
  if (strcmp (pattern, tmp) == 0)
    {
      g_free (dirname);
      dirname = NULL;
    }
  g_free (tmp);

  array = g_ptr_array_new ();

  for (filename = g_dir_read_name (dir);
       filename;
       filename = g_dir_read_name (dir))
    {
      gchar *path;
      gchar *name;

      if (dirname)
        path = g_build_filename (dirname, filename, NULL);
      else
        path = g_strdup (filename);

      name = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);

      if (name && glob_fnmatch (pattern, name))
        {
          if (filename_encoding)
            {
              g_ptr_array_add (array, path);
              path = NULL;
            }
          else
            {
              g_ptr_array_add (array, name);
              name = NULL;
            }
        }

      g_free (path);
      g_free (name);
    }

  g_dir_close (dir);
  g_free (dirname);

  *num_matches = array->len;
  *matches     = (gchar **) g_ptr_array_free (array, FALSE);

  return TRUE;
}


/*
 * The following code is borrowed from GTK+.
 *
 * GTK+ used to use a old version of GNU fnmatch() that was buggy
 * in various ways and didn't handle UTF-8. The following is
 * converted to UTF-8. To simplify the process of making it
 * correct, this is special-cased to the combinations of flags
 * that gtkfilesel.c uses.
 *
 *   FNM_FILE_NAME   - always set
 *   FNM_LEADING_DIR - never set
 *   FNM_NOESCAPE    - set only on windows
 *   FNM_CASEFOLD    - set only on windows
 */

/* We need to make sure that all constants are defined
 * to properly compile this file
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

static gunichar
get_char (const char **str)
{
  gunichar c = g_utf8_get_char (*str);
  *str = g_utf8_next_char (*str);

#ifdef G_PLATFORM_WIN32
  c = g_unichar_tolower (c);
#endif

  return c;
}

#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
#define DO_ESCAPE 0
#else
#define DO_ESCAPE 1
#endif

static gunichar
get_unescaped_char (const char **str,
                    gboolean    *was_escaped)
{
  gunichar c = get_char (str);

  *was_escaped = DO_ESCAPE && c == '\\';
  if (*was_escaped)
    c = get_char (str);

  return c;
}

/* Match STRING against the filename pattern PATTERN,
 * returning TRUE if it matches, FALSE otherwise.
 */
static gboolean
fnmatch_intern (const gchar *pattern,
                const gchar *string,
                gboolean     component_start,
                gboolean     no_leading_period)
{
  const char *p = pattern, *n = string;

  while (*p)
    {
      const char *last_n = n;

      gunichar c = get_char (&p);
      gunichar nc = get_char (&n);

      switch (c)
        {
        case '?':
          if (nc == '\0')
            return FALSE;
          else if (nc == G_DIR_SEPARATOR)
            return FALSE;
          else if (nc == '.' && component_start && no_leading_period)
            return FALSE;
          break;
        case '\\':
          if (DO_ESCAPE)
            c = get_char (&p);
          if (nc != c)
            return FALSE;
          break;
        case '*':
          if (nc == '.' && component_start && no_leading_period)
            return FALSE;

          {
            const char *last_p = p;

            for (last_p = p, c = get_char (&p);
                 c == '?' || c == '*';
                 last_p = p, c = get_char (&p))
              {
                if (c == '?')
                  {
                    if (nc == '\0')
                      return FALSE;
                    else if (nc == G_DIR_SEPARATOR)
                      return FALSE;
                    else
                      {
                        last_n = n; nc = get_char (&n);
                      }
                  }
              }

            /* If the pattern ends with wildcards, we have a
             * guaranteed match unless there is a dir separator
             * in the remainder of the string.
             */
            if (c == '\0')
              {
                if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
                  return FALSE;
                else
                  return TRUE;
              }

            if (DO_ESCAPE && c == '\\')
              c = get_char (&p);

            for (p = last_p; nc != '\0';)
              {
                if ((c == '[' || nc == c) &&
                    fnmatch_intern (p, last_n,
                                    component_start, no_leading_period))
                  return TRUE;

                component_start = (nc == G_DIR_SEPARATOR);
                last_n = n;
                nc = get_char (&n);
              }

            return FALSE;
          }

        case '[':
          {
            /* Nonzero if the sense of the character class is inverted.  */
            gboolean not;
            gboolean was_escaped;

            if (nc == '\0' || nc == G_DIR_SEPARATOR)
              return FALSE;

            if (nc == '.' && component_start && no_leading_period)
              return FALSE;

            not = (*p == '!' || *p == '^');
            if (not)
              ++p;

            c = get_unescaped_char (&p, &was_escaped);
            for (;;)
              {
                register gunichar cstart = c, cend = c;
                if (c == '\0')
                  /* [ (unterminated) loses.  */
                  return FALSE;

                c = get_unescaped_char (&p, &was_escaped);

                if (!was_escaped && c == '-' && *p != ']')
                  {
                    cend = get_unescaped_char (&p, &was_escaped);
                    if (cend == '\0')
                      return FALSE;

                    c = get_char (&p);
                  }

                if (nc >= cstart && nc <= cend)
                  goto matched;

                if (!was_escaped && c == ']')
                  break;
              }
            if (!not)
              return FALSE;
            break;

          matched:;
            /* Skip the rest of the [...] that already matched.  */
            /* XXX 1003.2d11 is unclear if was_escaped is right.  */
            while (was_escaped || c != ']')
              {
                if (c == '\0')
                  /* [... (unterminated) loses.  */
                  return FALSE;

                c = get_unescaped_char (&p, &was_escaped);
              }
            if (not)
              return FALSE;
          }
          break;

        default:
          if (c != nc)
            return FALSE;
        }

      component_start = (nc == G_DIR_SEPARATOR);
    }

  if (*n == '\0')
    return TRUE;

  return FALSE;
}

static gboolean
glob_fnmatch (const gchar *pattern,
              const gchar *string)
{
  return fnmatch_intern (pattern, string, TRUE, TRUE);
}