~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/config/gimpconfig-path.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* The GIMP -- an image manipulation program
2
 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
 
 *
4
 
 * String substitution utilities for config files
5
 
 * Copyright (C) 2001  Sven Neumann <sven@gimp.org>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <stdio.h>
25
 
#include <string.h>
26
 
 
27
 
#include <glib-object.h>
28
 
 
29
 
#include "libgimpbase/gimpbase.h"
30
 
 
31
 
#include "gimpconfig-path.h"
32
 
 
33
 
#include "gimp-intl.h"
34
 
 
35
 
 
36
 
#define SUBSTS_ALLOC 4
37
 
 
38
 
static gchar        * gimp_config_path_expand_only (const gchar  *path,
39
 
                                                    GError      **error);
40
 
static inline gchar * extract_token                (const gchar **str);
41
 
 
42
 
static inline gchar *
43
 
extract_token (const gchar **str)
44
 
{
45
 
  const gchar *p;
46
 
  gchar       *token;
47
 
 
48
 
  if (strncmp (*str, "${", 2))
49
 
    return NULL;
50
 
 
51
 
  p = *str + 2;
52
 
 
53
 
  while (*p && (*p != '}'))
54
 
    p = g_utf8_next_char (p);
55
 
 
56
 
  if (!p)
57
 
    return NULL;
58
 
 
59
 
  token = g_strndup (*str + 2, g_utf8_pointer_to_offset (*str + 2, p));
60
 
 
61
 
  *str = p + 1; /* after the closing bracket */
62
 
 
63
 
  return token;
64
 
}
65
 
 
66
 
/**
67
 
 * gimp_config_path_expand:
68
 
 * @path: a %NUL-terminated string in UTF-8 encoding
69
 
 * @recode: whether to convert to the filesystem's encoding
70
 
 * @error: return location for errors
71
 
 *
72
 
 * Paths as stored in the gimprc have to be treated special.  The
73
 
 * string may contain special identifiers such as for example
74
 
 * ${gimp_dir} that have to be substituted before use. Also the user's
75
 
 * filesystem may be in a different encoding than UTF-8 (which is what
76
 
 * is used for the gimprc). This function does the variable
77
 
 * substitution for you and can also attempt to convert to the
78
 
 * filesystem encoding.
79
 
 *
80
 
 * Return value: a newly allocated %NUL-terminated string
81
 
 **/
82
 
gchar *
83
 
gimp_config_path_expand (const gchar  *path,
84
 
                         gboolean      recode,
85
 
                         GError      **error)
86
 
{
87
 
  g_return_val_if_fail (path != NULL, NULL);
88
 
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
89
 
 
90
 
  if (recode)
91
 
    {
92
 
      gchar *retval;
93
 
      gchar *expanded = gimp_config_path_expand_only (path, error);
94
 
 
95
 
      if (! expanded)
96
 
        return NULL;
97
 
 
98
 
      retval = g_filename_from_utf8 (expanded, -1, NULL, NULL, error);
99
 
 
100
 
      g_free (expanded);
101
 
 
102
 
      return retval;
103
 
    }
104
 
 
105
 
  return gimp_config_path_expand_only (path, error);
106
 
}
107
 
 
108
 
static gchar *
109
 
gimp_config_path_expand_only (const gchar  *path,
110
 
                              GError      **error)
111
 
{
112
 
  const gchar *p;
113
 
  const gchar *s;
114
 
  gchar       *n;
115
 
  gchar       *token;
116
 
  gchar       *filename = NULL;
117
 
  gchar       *expanded = NULL;
118
 
  gchar      **substs   = NULL;
119
 
  guint        n_substs = 0;
120
 
  gint         length   = 0;
121
 
  gint         i;
122
 
 
123
 
  p = path;
124
 
 
125
 
  while (*p)
126
 
    {
127
 
 
128
 
#ifndef G_OS_WIN32
129
 
      if (*p == '~')
130
 
        {
131
 
          length += strlen (gimp_filename_to_utf8 (g_get_home_dir ()));
132
 
          p += 1;
133
 
        }
134
 
      else
135
 
#endif  /* G_OS_WIN32 */
136
 
 
137
 
      if ((token = extract_token (&p)) != NULL)
138
 
        {
139
 
          for (i = 0; i < n_substs; i++)
140
 
            if (strcmp (substs[2*i], token) == 0)
141
 
              break;
142
 
 
143
 
          if (i < n_substs)
144
 
            {
145
 
              s = substs[2*i+1];
146
 
            }
147
 
          else
148
 
            {
149
 
              s = NULL;
150
 
 
151
 
              if (strcmp (token, "gimp_dir") == 0)
152
 
                s = gimp_directory ();
153
 
              else if (strcmp (token, "gimp_data_dir") == 0)
154
 
                s = gimp_data_directory ();
155
 
              else if (strcmp (token, "gimp_plug_in_dir") == 0 ||
156
 
                       strcmp (token, "gimp_plugin_dir") == 0)
157
 
                s = gimp_plug_in_directory ();
158
 
              else if (strcmp (token, "gimp_sysconf_dir") == 0)
159
 
                s = gimp_sysconf_directory ();
160
 
 
161
 
              if (!s)
162
 
                s = g_getenv (token);
163
 
 
164
 
#ifdef G_OS_WIN32
165
 
              /* The default user gimprc on Windows references
166
 
               * ${TEMP}, but not all Windows installations have that
167
 
               * environment variable, even if it should be kinda
168
 
               * standard. So special-case it.
169
 
               */
170
 
              if (!s && strcmp (token, "TEMP") == 0)
171
 
                s = g_get_tmp_dir ();
172
 
#endif  /* G_OS_WIN32 */
173
 
            }
174
 
 
175
 
          if (!s)
176
 
            {
177
 
              g_set_error (error, 0, 0, _("Cannot expand ${%s}"), token);
178
 
              g_free (token);
179
 
              goto cleanup;
180
 
            }
181
 
 
182
 
          if (n_substs % SUBSTS_ALLOC == 0)
183
 
            substs = g_renew (gchar *, substs, 2 * (n_substs + SUBSTS_ALLOC));
184
 
 
185
 
          substs[2*n_substs]     = token;
186
 
          substs[2*n_substs + 1] = (gchar *) gimp_filename_to_utf8 (s);
187
 
 
188
 
          length += strlen (substs[2*n_substs + 1]);
189
 
 
190
 
          n_substs++;
191
 
        }
192
 
      else
193
 
        {
194
 
          length += g_utf8_skip[(const guchar) *p];
195
 
          p = g_utf8_next_char (p);
196
 
        }
197
 
    }
198
 
 
199
 
  if (n_substs == 0)
200
 
    return g_strdup (path);
201
 
 
202
 
  expanded = g_new (gchar, length + 1);
203
 
 
204
 
  p = path;
205
 
  n = expanded;
206
 
 
207
 
  while (*p)
208
 
    {
209
 
 
210
 
#ifndef G_OS_WIN32
211
 
      if (*p == '~')
212
 
        {
213
 
          *n = '\0';
214
 
          strcat (n, gimp_filename_to_utf8 (g_get_home_dir ()));
215
 
          n += strlen (gimp_filename_to_utf8 (g_get_home_dir ()));
216
 
          p += 1;
217
 
        }
218
 
      else
219
 
#endif  /* G_OS_WIN32 */
220
 
 
221
 
      if ((token = extract_token (&p)) != NULL)
222
 
        {
223
 
          for (i = 0; i < n_substs; i++)
224
 
            {
225
 
              if (strcmp (substs[2*i], token) == 0)
226
 
                {
227
 
                  s = substs[2*i+1];
228
 
 
229
 
                  *n = '\0';
230
 
                  strcat (n, s);
231
 
                  n += strlen (s);
232
 
 
233
 
                  break;
234
 
                }
235
 
            }
236
 
 
237
 
          g_free (token);
238
 
        }
239
 
      else
240
 
        {
241
 
          *n++ = *p++;
242
 
        }
243
 
    }
244
 
 
245
 
  *n = '\0';
246
 
 
247
 
 cleanup:
248
 
  for (i = 0; i < n_substs; i++)
249
 
    g_free (substs[2*i]);
250
 
 
251
 
  g_free (substs);
252
 
  g_free (filename);
253
 
 
254
 
  return expanded;
255
 
}