Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

appconfig.c

Go to the documentation of this file.
00001 
00016 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00017  * Copyright (C) 1998-2003  Lionel ULMER, Darryl Luff
00018  * $Id: appconfig_8c-source.html,v 1.5 2004/05/09 09:31:54 darryll Exp $
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00033  *
00034  * Some config items are stored in a gtk resource file int he standard gtk format.
00035  * But gtk doesnt seem to allow you to put arbitrary things in there. So the 
00036  * remainder of things are stored in a simple text config file.
00037  *
00038  * This module handles the differences transparently, so the application doesn't need
00039  * to know about these seperate files.
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     /* Dont have to do anything. Defaults hard-coded. */
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             /* Connect to a URL. */
00144             db_connect_url(optarg);
00145             break;
00146 #ifdef USE_LUA
00147         case 'f':
00148             /* Run a script file. */
00149             luaif_runfile(optarg);
00150             break;
00151         case 's':
00152             /* Run a script directly. */
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     /* Set the current dir if it's still NULL. */
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) {     /* couldnt change to it */
00204         if (errno == ENOENT) {         /* didnt exist, try and create it */
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 static char *cfg_get_filename(void)
00252 {
00253     gchar *dir;
00254     int len;
00255     if (!cfgname) {
00256         dir = cfg_get_config_dir();
00257 
00258         len = strlen(dir) + strlen(CONFIG_FILENAME) + 2;
00259         cfgname = g_malloc(len);
00260         if (!cfgname)
00261             return NULL;
00262 
00263         snprintf(cfgname, len, "%s/%s", dir, CONFIG_FILENAME);
00264     }
00265     return cfgname;
00266 }
00267 */
00268 
00270 /*static void cfg_print(void)
00271 {
00272     printf("currentWS.changed = %d", ws_get_changed());
00273     printf("currentWS.dbname = %s\n", ws_get_db_name());
00274     printf("currentWS.dbtype = %s\n", ws_get_db_type());
00275     printf("currentWS.filename = %s\n", ws_get_filename());
00276     printf("currentWS.host = %s\n", ws_get_host());
00277     printf("currentWS.user = %s\n", ws_get_user());
00278     printf("currentWS.pass = %s\n", ws_get_pass());
00279     printf("currentWS.connection = %p\n", ws_get_connection());
00280     printf("currentWS.toolbar_conf = %d", ws_get_toolbarconf());
00281     printf("currentWS.windowX = %d", ws_get_window_x());
00282     printf("currentWS.windowY = %d", ws_get_window_y());
00283     printf("currentWS.windowWidth = %d", ws_get_window_width());
00284     printf("currentWS.windowHeight = %d", ws_get_window_height());
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 }

Generated on Sun May 9 19:19:15 2004 for GtkSQL by doxygen1.2.18