Search
lxdream.org :: lxdream/src/config.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/config.c
changeset 1036:af7b0c5905dd
prev1034:7044e01148f0
next1038:f220d18c0615
author nkeynes
date Wed Jun 24 06:06:40 2009 +0000 (10 years ago)
permissions -rw-r--r--
last change Support shell substitutions in config paths
Keep track of last folder in file dialogs
Fix out-of-dateness in GTK path dialog
file annotate diff log raw
1.1 --- a/src/config.c Wed Jun 24 02:41:12 2009 +0000
1.2 +++ b/src/config.c Wed Jun 24 06:06:40 2009 +0000
1.3 @@ -18,9 +18,11 @@
1.4
1.5 #include <unistd.h>
1.6 #include <stdio.h>
1.7 +#include <ctype.h>
1.8 #include <errno.h>
1.9 #include <stdlib.h>
1.10 #include <string.h>
1.11 +#include <wordexp.h>
1.12 #include <glib/gmem.h>
1.13 #include <glib/gstrfuncs.h>
1.14 #include "dream.h"
1.15 @@ -136,7 +138,7 @@
1.16 maple_detach_all();
1.17 }
1.18
1.19 -const gchar *lxdream_get_config_value( int key )
1.20 +const gchar *lxdream_get_global_config_value( int key )
1.21 {
1.22 return global_config[key].value;
1.23 }
1.24 @@ -144,7 +146,7 @@
1.25 GList *lxdream_get_global_config_list_value( int key )
1.26 {
1.27 GList *result = NULL;
1.28 - const gchar *str = lxdream_get_config_value( key );
1.29 + const gchar *str = lxdream_get_global_config_value( key );
1.30 if( str != NULL ) {
1.31 gchar **strv = g_strsplit(str, ":",0);
1.32 int i;
1.33 @@ -161,7 +163,7 @@
1.34 if( list == NULL ) {
1.35 lxdream_set_global_config_value( key, NULL );
1.36 } else {
1.37 - GList *ptr;
1.38 + const GList *ptr;
1.39 int size = 0;
1.40
1.41 for( ptr = list; ptr != NULL; ptr = g_list_next(ptr) ) {
1.42 @@ -177,6 +179,100 @@
1.43 }
1.44 }
1.45
1.46 +gchar *get_expanded_path( const gchar *input )
1.47 +{
1.48 + wordexp_t we;
1.49 + if( input == NULL ) {
1.50 + return NULL;
1.51 + }
1.52 + memset(&we,0,sizeof(we));
1.53 + int result = wordexp(input, &we, WRDE_NOCMD);
1.54 + if( result != 0 || we.we_wordc == 0 ) {
1.55 + /* On failure, return the original input unchanged */
1.56 + return g_strdup(input);
1.57 + } else {
1.58 + /* On success, concatenate all 'words' together into a single
1.59 + * space-separated string
1.60 + */
1.61 + int length = we.we_wordc, i;
1.62 + gchar *result, *p;
1.63 +
1.64 + for( i=0; i<we.we_wordc; i++ ) {
1.65 + length += strlen(we.we_wordv[i]);
1.66 + }
1.67 + p = result = g_malloc(length);
1.68 + for( i=0; i<we.we_wordc; i++ ) {
1.69 + if( i != 0 )
1.70 + *p++ = ' ';
1.71 + strcpy( p, we.we_wordv[i] );
1.72 + p += strlen(p);
1.73 + }
1.74 + wordfree(&we);
1.75 + return result;
1.76 + }
1.77 +}
1.78 +
1.79 +/**
1.80 + * Test if we need to escape a path to prevent substitution mangling.
1.81 + * @return TRUE if the input value contains any character that doesn't
1.82 + * match [a-zA-Z0-9._@%/] (this will escape slightly more than it needs to,
1.83 + * but is safe)
1.84 + */
1.85 +gboolean path_needs_escaping( const gchar *value )
1.86 +{
1.87 + const gchar *p = value;
1.88 + while( *p ) {
1.89 + if( !isalnum(*p) && *p != '.' && *p != '_' &&
1.90 + *p != '@' && *p != '%' && *p != '/' ) {
1.91 + return TRUE;
1.92 + }
1.93 + p++;
1.94 + }
1.95 + return FALSE;
1.96 +}
1.97 +
1.98 +gchar *get_escaped_path( const gchar *value )
1.99 +{
1.100 + if( value != NULL && path_needs_escaping(value) ) {
1.101 + /* Escape with "", and backslash the remaining characters:
1.102 + * \ " $ `
1.103 + */
1.104 + char buf[strlen(value)*2+3];
1.105 + const char *s = value;
1.106 + char *p = buf;
1.107 + *p++ = '\"';
1.108 + while( *s ) {
1.109 + if( *s == '\\' || *s == '"' || *s == '$' || *s == '`' ) {
1.110 + *p++ = '\\';
1.111 + }
1.112 + *p++ = *s++;
1.113 + }
1.114 + *p++ = '\"';
1.115 + *p = '\0';
1.116 + return g_strdup(buf);
1.117 + } else {
1.118 + return g_strdup(value);
1.119 + }
1.120 +}
1.121 +
1.122 +gchar *lxdream_get_global_config_path_value( int key )
1.123 +{
1.124 + const gchar *str = lxdream_get_global_config_value(key);
1.125 + if( str == NULL ) {
1.126 + return NULL;
1.127 + } else {
1.128 + return get_expanded_path(str);
1.129 + }
1.130 +}
1.131 +
1.132 +const gchar *lxdream_set_global_config_path_value( int key, const gchar *value )
1.133 +{
1.134 + gchar *temp = get_escaped_path(value);
1.135 + lxdream_set_global_config_value(key,temp);
1.136 + g_free(temp);
1.137 + return lxdream_get_global_config_value(key);
1.138 +}
1.139 +
1.140 void lxdream_set_config_value( lxdream_config_entry_t param, const gchar *value )
1.141 {
1.142 if( param->value != value ) {
1.143 @@ -192,7 +288,7 @@
1.144 lxdream_set_config_value(&global_config[key], value);
1.145 }
1.146
1.147 -const struct lxdream_config_entry * lxdream_get_config_entry( int key )
1.148 +const struct lxdream_config_entry * lxdream_get_global_config_entry( int key )
1.149 {
1.150 return &global_config[key];
1.151 }
.