filename | src/lxpaths.c |
changeset | 1205:a486ac64f34b |
prev | 1056:d0896e6530d6 |
next | 1283:2cbafe321d6f |
author | nkeynes |
date | Mon Jan 30 20:11:08 2012 +1000 (11 years ago) |
permissions | -rw-r--r-- |
last change | Replace wordexp() with a hand-coded env-var substitution. More portable, and avoids bugs with some wordexp() implementations |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * GUI helper functions that aren't specific to any particular implementation.
5 *
6 * Copyright (c) 2009 Nathan Keynes.
7 *
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.
12 *
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.
17 */
19 #include <ctype.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <glib/gstrfuncs.h>
23 #include <glib/gutils.h>
25 #include "gui.h"
26 #include "config.h"
28 static gchar *gui_paths[CONFIG_KEY_MAX];
30 /**
31 * Test if we need to escape a path to prevent substitution mangling.
32 * @return TRUE if the input value contains any character that doesn't
33 * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
34 * but is safe)
35 */
36 static gboolean path_needs_escaping( const gchar *value )
37 {
38 const gchar *p = value;
39 while( *p ) {
40 if( !isalnum(*p) && *p != '.' && *p != '_' &&
41 *p != '@' && *p != '%' && *p != '/' ) {
42 return TRUE;
43 }
44 p++;
45 }
46 return FALSE;
47 }
49 gchar *get_escaped_path( const gchar *value )
50 {
51 if( value != NULL && path_needs_escaping(value) ) {
52 /* Escape with "", and backslash the remaining characters:
53 * \ " $ `
54 */
55 char buf[strlen(value)*2+3];
56 const char *s = value;
57 char *p = buf;
58 *p++ = '\"';
59 while( *s ) {
60 if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
61 *p++ = '\\';
62 }
63 *p++ = *s++;
64 }
65 *p++ = '\"';
66 *p = '\0';
67 return g_strdup(buf);
68 } else {
69 return g_strdup(value);
70 }
71 }
73 gchar *get_expanded_path( const gchar *input )
74 {
75 char result[PATH_MAX];
77 char *d, *e;
78 const char *s;
79 d = result;
80 e = result+sizeof(result)-1;
81 s = input;
83 if( input == NULL )
84 return NULL;
86 while( *s ) {
87 if( d == e ) {
88 return g_strdup(input); /* expansion too long */
89 }
90 char c = *s++;
91 if( c == '$' ) {
92 if( *s == '{' ) {
93 s++;
94 const char *q = s;
95 while( *q != '}' ) {
96 if( ! *q ) {
97 return g_strdup(input); /* unterminated variable */
98 }
99 q++;
100 }
101 char *tmp = g_strndup(s, (q-s));
102 s = q+1;
103 char *value = getenv(tmp);
104 g_free(tmp);
105 if( value != NULL ) {
106 int len = strlen(value);
107 if( d + len > e )
108 return g_strdup(input);
109 strcpy(d, value);
110 d+=len;
111 } /* Else, empty string */
112 } else {
113 const char *q = s;
114 while( isalnum(*q) || *q == '_' ) {
115 q++;
116 }
117 if( q == s ) {
118 *d++ = '$';
119 } else {
120 char *tmp = g_strndup(s,q-s);
121 s = q;
122 char *value = getenv(tmp);
123 g_free(tmp);
124 if( value != NULL ) {
125 int len = strlen(value);
126 if( d + len > e )
127 return g_strdup(input);
128 strcpy(d, value);
129 d += len;
130 }
131 }
132 }
133 } else if( c == '\\' ) {
134 c = *s++;
135 if( c ) {
136 *d++ = c;
137 } else {
138 *d++ = '\\';
139 }
140 } else {
141 *d++ = c;
142 }
143 }
144 *d = '\0';
145 return g_strdup(result);
146 }
147 gchar *get_absolute_path( const gchar *in_path )
148 {
149 char tmp[PATH_MAX];
150 if( in_path == NULL ) {
151 return NULL;
152 }
153 if( in_path[0] == '/' || in_path[0] == 0 ) {
154 return g_strdup(in_path);
155 } else {
156 getcwd(tmp, sizeof(tmp));
157 return g_strdup_printf("%s%c%s", tmp, G_DIR_SEPARATOR, in_path);
158 }
159 }
161 gchar *get_filename_at( const gchar *at, const gchar *filename )
162 {
163 char tmp[PATH_MAX];
164 char *p = strrchr( at, '/' );
165 if( p == NULL ) {
166 /* No path at all, so just return filename */
167 return g_strdup(filename);
168 } else {
169 int off = p-at;
170 return g_strdup_printf("%.*s%c%s", off, at, G_DIR_SEPARATOR, filename );
171 }
172 }
174 const gchar *get_gui_path( int key )
175 {
176 if( gui_paths[key] == NULL ) {
177 gui_paths[key] = lxdream_get_global_config_path_value(key);
178 /* If no path defined, go with the current working directory */
179 if( gui_paths[key] == NULL ) {
180 gui_paths[key] = get_absolute_path(".");
181 }
182 }
183 return gui_paths[key];
184 }
186 void set_gui_path( int key, const gchar *path )
187 {
188 g_free(gui_paths[key]);
189 gui_paths[key] = g_strdup(path);
190 }
192 void reset_gui_paths()
193 {
194 int i;
195 for( i=0; i < CONFIG_KEY_MAX; i++ ) {
196 g_free(gui_paths[i]);
197 gui_paths[i] = NULL;
198 }
199 }
.