00001 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 #include "../config.h"
00043 
00044 #include <errno.h>
00045 #include <stdio.h>
00046 #include <stdlib.h>
00047 #include <string.h>
00048 #include <sys/stat.h>
00049 #include <sys/types.h>
00050 #include <unistd.h>
00051 
00052 #include "appconfig.h"
00053 #include "buf.h"
00054 #include "dbapi.h"
00055 #include "luaif.h"
00056 
00058 #define CONFIG_DIRNAME ".gtksql"
00059 
00061 #define CONFIG_FILENAME "gtksql.conf"
00062 
00063 #define CONTACT "http://gtksql.sourceforge.net/"
00064 
00066 #ifdef USE_LUA
00067 #define USAGE   "gtksql [-h] [-f scriptfile] [-s \"script\"]\n\n" \
00068                 "\t-h              Show usage help\n"   \
00069                 "\t-f scriptfile   Run the script in the named file\n"  \
00070                 "\t-s \"script\"     Run the script given on the command line\n"
00071 
00072 #define ARGSPEC "hf:s:"
00073 #else
00074 #define USAGE   "gtksql [-h] \n\n" \
00075                 "\t-h              Show usage help\n"
00076 
00077 #define ARGSPEC "h"
00078 #endif
00079 
00081 static char *cfgDirname = NULL;
00082 
00084 static char *currentDir = NULL;
00085 
00086 
00088 void cfg_show_banner(void)
00089 {
00090     printf("%s Version %s\n%s\n", PACKAGE, VERSION, CONTACT);
00091     printf("Support for:\n");
00092     printf("\t      MySQL:\t");
00093 #ifdef USE_MYSQL
00094     printf("Yes\n");
00095 #else
00096     printf("No\n");
00097 #endif
00098     printf("\t PostgreSQL:\t");
00099 #ifdef USE_POSTGRESQL
00100     printf("Yes\n");
00101 #else
00102     printf("No\n");
00103 #endif
00104 
00105     printf("\tLUA Scripts:\t");
00106 #ifdef USE_LUA
00107     printf("Yes\n");
00108 #else
00109     printf("No\n");
00110 #endif
00111 
00112     printf("\t      XPath:\t");
00113 #ifdef USE_XPATH
00114     printf("Yes\n");
00115 #else
00116     printf("No\n");
00117 #endif
00118 }
00119 
00121 void cfg_show_usage(void)
00122 {
00123     cfg_show_banner();
00124     printf("\nUSAGE: %s\n", USAGE);
00125 }
00126 
00128 static void cfg_set_defaults(void)
00129 {
00130     
00131 }
00132 
00137 int cfg_parse_cmdline(int argc, char *argv[])
00138 {
00139     char c;
00140     while ((c = getopt(argc, argv, ARGSPEC)) != -1) {
00141         switch (c) {
00142         case 'c':
00143             
00144             db_connect_url(optarg);
00145             break;
00146 #ifdef USE_LUA
00147         case 'f':
00148             
00149             luaif_runfile(optarg);
00150             break;
00151         case 's':
00152             
00153             luaif_runstring(optarg);
00154             break;
00155 #endif
00156         case 'h':
00157             cfg_show_usage();
00158             break;
00159         }
00160     }
00161     return 0;
00162 }
00163 
00168 void cfg_init(void)
00169 {
00170     cfg_set_defaults();
00171 }
00172 
00176 char *cfg_get_config_dir(void)
00177 {
00178     char *homedir;
00179     int len;
00180     homedir = getenv("HOME");
00181 
00182     if (!homedir) {
00183         printf("Could not resolve home directory");
00184         return NULL;
00185     }
00186 
00187     
00188     if (currentDir == NULL) {
00189         cfg_set_current_dir(homedir);
00190     }
00191 
00192     len = strlen(homedir) + 1 + strlen(CONFIG_DIRNAME) + 1;
00193     if (cfgDirname)
00194         free(cfgDirname);
00195 
00196     cfgDirname = malloc(len);
00197     if (cfgDirname) {
00198         snprintf(cfgDirname, len, "%s/%s", homedir, CONFIG_DIRNAME);
00199         cfgDirname[len - 1] = '\0';
00200     }
00201     printf("Using config directory %s", cfgDirname);
00202 
00203     if (chdir(cfgDirname) == -1) {     
00204         if (errno == ENOENT) {         
00205             if (mkdir(cfgDirname, 0755) == -1) {
00206                 perror("Could not create config directory");
00207                 free(cfgDirname);
00208                 cfgDirname = NULL;
00209                 return NULL;
00210             }
00211         }
00212         else {
00213             perror("changing to config directory");
00214             free(cfgDirname);
00215             cfgDirname = NULL;
00216             return NULL;
00217         }
00218     }
00219     return cfgDirname;
00220 }
00221 
00224 char *cfg_get_current_dir(void)
00225 {
00226     if (!currentDir)
00227         currentDir = strdup(getenv("HOME"));
00228 
00229     if (!currentDir)
00230         cfg_set_current_dir(".");
00231 
00232     return currentDir;
00233 }
00234 
00235 static ABuf *wsname = NULL;
00238 char *cfg_get_default_wsname(void)
00239 {
00240 #define DEFAULT_WSNAME  "workspace"
00241     wsname = buf_strcpy(wsname, cfg_get_config_dir());
00242     wsname = buf_strcat(wsname, "/");
00243     wsname = buf_strcat(wsname, DEFAULT_WSNAME);
00244 
00245     return wsname->b_dat;
00246 }
00247 
00250 
00251 
00252 
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00270 
00271 
00272 
00273 
00274 
00275 
00276 
00277 
00278 
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00289 void cfg_set_current_dir(const char *dir)
00290 {
00291     if (currentDir != NULL) {
00292         free(currentDir);
00293         currentDir = NULL;
00294     }
00295 
00296     if (dir)
00297         currentDir = strdup(dir);
00298 }