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.
27 static gchar *gui_paths[CONFIG_KEY_MAX];
30 * Test if we need to escape a path to prevent substitution mangling.
31 * @return TRUE if the input value contains any character that doesn't
32 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
35 static gboolean path_needs_escaping( const gchar *value )
37 const gchar *p = value;
39 if( !isalnum(*p) && *p != '.' && *p != '_' &&
40 *p != '@' && *p != '%' && *p != '/' ) {
48 gchar *get_escaped_path( const gchar *value )
50 if( value != NULL && path_needs_escaping(value) ) {
51 /* Escape with "", and backslash the remaining characters:
54 char buf[strlen(value)*2+3];
55 const char *s = value;
59 if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
68 return g_strdup(value);
72 gchar *get_expanded_path( const gchar *input )
74 char result[PATH_MAX];
79 e = result+sizeof(result)-1;
81 gboolean inDQstring = FALSE;
88 return g_strdup(input); /* expansion too long */
97 return g_strdup(input); /* unterminated variable */
101 char *tmp = g_strndup(s, (q-s));
103 char *value = getenv(tmp);
105 if( value != NULL ) {
106 int len = strlen(value);
108 return g_strdup(input);
111 } /* Else, empty string */
114 while( isalnum(*q) || *q == '_' ) {
120 char *tmp = g_strndup(s,q-s);
122 char *value = getenv(tmp);
124 if( value != NULL ) {
125 int len = strlen(value);
127 return g_strdup(input);
133 } else if( c == '\\' ) {
140 } else if( c == '\"' ) {
141 /* Unescaped double-quotes start a DQ string. Although we treat the
142 * string as if it were double-quoted for most purposes anyway, so
143 * this has little effect.
145 inDQstring = !inDQstring;
152 WARN( "Unterminated double-quoted string '%s'", input );
154 return g_strdup(result);
156 gchar *get_absolute_path( const gchar *in_path )
159 if( in_path == NULL ) {
162 if( in_path[0] == '/' || in_path[0] == 0 ) {
163 return g_strdup(in_path);
165 getcwd(tmp, sizeof(tmp));
166 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
170 gchar *get_filename_at( const gchar *at, const gchar *filename )
172 char *p = strrchr( at, '/' );
174 /* No path at all, so just return filename */
175 return g_strdup(filename);
178 return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
182 const gchar *get_gui_path( int key )
184 if( gui_paths[key] == NULL ) {
185 gui_paths[key] = lxdream_get_global_config_path_value(key);
186 /* If no path defined, go with the current working directory */
187 if( gui_paths[key] == NULL ) {
188 gui_paths[key] = get_absolute_path(".");
191 return gui_paths[key];
194 void set_gui_path( int key, const gchar *path )
196 g_free(gui_paths[key]);
197 gui_paths[key] = g_strdup(path);
200 void reset_gui_paths()
203 for( i=0; i < CONFIG_KEY_MAX; i++ ) {
204 g_free(gui_paths[i]);
.