4 * GUI helper functions that aren't specific to any particular implementation.
6 * Copyright (c) 2009 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include <glib/gfileutils.h>
23 #include <glib/gstrfuncs.h>
24 #include <glib/gutils.h>
29 static gchar *gui_paths[CONFIG_KEY_MAX];
32 * Test if we need to escape a path to prevent substitution mangling.
33 * @return TRUE if the input value contains any character that doesn't
34 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
37 static gboolean path_needs_escaping( const gchar *value )
39 const gchar *p = value;
41 if( !isalnum(*p) && *p != '.' && *p != '_' &&
42 *p != '@' && *p != '%' && *p != '/' ) {
50 gchar *get_escaped_path( const gchar *value )
52 if( value != NULL && path_needs_escaping(value) ) {
53 /* Escape with "", and backslash the remaining characters:
56 char buf[strlen(value)*2+3];
57 const char *s = value;
61 if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
70 return g_strdup(value);
74 gchar *get_expanded_path( const gchar *input )
76 char result[PATH_MAX];
81 e = result+sizeof(result)-1;
83 gboolean inDQstring = FALSE;
90 return g_strdup(input); /* expansion too long */
99 return g_strdup(input); /* unterminated variable */
103 char *tmp = g_strndup(s, (q-s));
105 char *value = getenv(tmp);
107 if( value != NULL ) {
108 int len = strlen(value);
110 return g_strdup(input);
113 } /* Else, empty string */
116 while( isalnum(*q) || *q == '_' ) {
122 char *tmp = g_strndup(s,q-s);
124 char *value = getenv(tmp);
126 if( value != NULL ) {
127 int len = strlen(value);
129 return g_strdup(input);
135 } else if( c == '\\' ) {
142 } else if( c == '\"' ) {
143 /* Unescaped double-quotes start a DQ string. Although we treat the
144 * string as if it were double-quoted for most purposes anyway, so
145 * this has little effect.
147 inDQstring = !inDQstring;
154 WARN( "Unterminated double-quoted string '%s'", input );
156 return g_strdup(result);
158 gchar *get_absolute_path( const gchar *in_path )
161 if( in_path == NULL ) {
164 if( in_path[0] == '/' || in_path[0] == 0 ) {
165 return g_strdup(in_path);
167 getcwd(tmp, sizeof(tmp));
168 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
172 gchar *get_filename_at( const gchar *at, const gchar *filename )
175 char *p = strrchr( at, '/' );
177 /* No path at all, so just return filename */
178 return g_strdup(filename);
181 return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
185 const gchar *get_gui_path( int key )
187 if( gui_paths[key] == NULL ) {
188 gui_paths[key] = lxdream_get_global_config_path_value(key);
189 /* If no path defined, go with the current working directory */
190 if( gui_paths[key] == NULL ) {
191 gui_paths[key] = get_absolute_path(".");
194 return gui_paths[key];
197 void set_gui_path( int key, const gchar *path )
199 g_free(gui_paths[key]);
200 gui_paths[key] = g_strdup(path);
203 void reset_gui_paths()
206 for( i=0; i < CONFIG_KEY_MAX; i++ ) {
207 g_free(gui_paths[i]);
.