Browse Source

libgis: define CONFIG_DIR
db.login: updated & interactive mode removed
dbmi/login.c updated


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39879 15284696-431f-4ddb-bdfa-cd5b030d7da7

Martin Landa 15 years ago
parent
commit
20e17bf5d7
3 changed files with 57 additions and 70 deletions
  1. 14 53
      db/db.login/main.c
  2. 3 1
      include/gis.h
  3. 40 16
      lib/db/dbmi_base/login.c

+ 14 - 53
db/db.login/main.c

@@ -4,12 +4,12 @@
  * MODULE:       db.login
  * AUTHOR(S):    Radim Blazek <radim.blazek gmail.com> (original contributor)
  *               Glynn Clements <glynn gclements.plus.com>, Markus Neteler <neteler itc.it>
- * PURPOSE:      
- * COPYRIGHT:    (C) 2004-2006 by the GRASS Development Team
+ * PURPOSE:      Store db login settings
+ * COPYRIGHT:    (C) 2004-2009 by the GRASS Development Team
  *
- *               This program is free software under the GNU General Public
- *               License (>=v2). Read the file COPYING that comes with GRASS
- *               for details.
+ *               This program is free software under the GNU General
+ *               Public License (>=v2). Read the file COPYING that
+ *               comes with GRASS for details.
  *
  *****************************************************************************/
 #include <stdio.h>
@@ -30,28 +30,23 @@ int main(int argc, char *argv[])
 {
     struct Option *driver, *database, *user, *password;
     struct GModule *module;
-
-#ifdef HAVE_TERMIOS_H
-    struct termios tios, tios2;
-#endif
-    char answer[200];
-
+    
     /* Initialize the GIS calls */
     G_gisinit(argv[0]);
 
     module = G_define_module();
     G_add_keyword(_("database"));
-    G_add_keyword(_("SQL"));
+    G_add_keyword(_("connection"));
     module->description = _("Sets user/password for driver/database.");
 
     driver = G_define_standard_option(G_OPT_DB_DRIVER);
     driver->options = db_list_drivers();
     driver->required = YES;
-    driver->answer = db_get_default_driver_name();
+    driver->answer = (char *) db_get_default_driver_name();
 
     database = G_define_standard_option(G_OPT_DB_DATABASE);
     database->required = YES;
-    database->answer = db_get_default_database_name();
+    database->answer = (char *) db_get_default_database_name();
 
     user = G_define_option();
     user->key = "user";
@@ -70,47 +65,13 @@ int main(int argc, char *argv[])
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
-    /* set connection */
-    if (!password->answer && isatty(fileno(stdin))) {
-	for (;;) {
-#ifdef HAVE_TERMIOS_H
-	    tcgetattr(STDIN_FILENO, &tios);
-	    tios2 = tios;
-	    tios2.c_lflag &= ~ECHO;
-	    tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios2);
-#endif
-	    do {
-		fprintf(stderr,
-			_("\nEnter database password for connection\n<%s:%s:user=%s>\n"),
-			driver->answer, database->answer, user->answer);
-		fprintf(stderr, _("Hit RETURN to cancel request\n"));
-		fprintf(stderr, ">");
-		*answer = '\0';
-		G_getl(answer, sizeof(answer), stdin);
-	    } while (0);
-#ifdef HAVE_TERMIOS_H
-	    tcsetattr(STDIN_FILENO, TCSANOW, &tios);
-#endif
-	    G_strip(answer);
-	    if (strlen(answer) == 0) {
-		G_message(_("Exiting. Not changing current settings"));
-		return -1;
-	    }
-	    else {
-		G_message(_("New password set"));
-		password->answer = G_store(answer);
-		break;
-	    }
-	}
-    }
-    if (db_set_login
-	(driver->answer, database->answer, user->answer,
-	 password->answer) == DB_FAILED) {
+    if (db_set_login(driver->answer, database->answer, user->answer,
+		     password->answer) == DB_FAILED) {
 	G_fatal_error(_("Unable to set user/password"));
     }
-
+    
     if (password->answer)
-	G_warning(_("The password was stored in file"));
-
+	G_important_message(_("The password was stored in file (%s/dblogin)"), CONFIG_DIR);
+    
     exit(EXIT_SUCCESS);
 }

+ 3 - 1
include/gis.h

@@ -72,7 +72,9 @@ static const char *GRASS_copyright __attribute__ ((unused))
 #define PROJECTION_OTHER  99
 
 #define PROJECTION_FILE "PROJ_INFO"
-#define UNIT_FILE "PROJ_UNITS"
+#define UNIT_FILE       "PROJ_UNITS"
+
+#define CONFIG_DIR ".grass7"
 
 /* define PI and friends */
 #undef M_PI

+ 40 - 16
lib/db/dbmi_base/login.c

@@ -1,11 +1,27 @@
+/*!
+  \file db/dbmi_base/login.c
+  
+  \brief DBMI Library (base) - login settings
+  
+  (C) 1999-2009 by the GRASS Development Team
+  
+  This program is free software under the GNU General Public
+  License (>=v2). Read the file COPYING that comes with GRASS
+  for details.
+  
+  \author Joel Jones (CERL/UIUC), Radim Blazek
+*/
+
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
+
 #include <grass/gis.h>
 #include <grass/dbmi.h>
+#include <grass/glocale.h>
 
 typedef struct
 {
@@ -27,12 +43,12 @@ static const char *login_filename(void)
 
     if (!file) {
 	file = (char *)malloc(1000);
-	sprintf(file, "%s/.grasslogin6", G_home());
+	sprintf(file, "%s/%s/dblogin", G_home(), CONFIG_DIR);
     }
     return file;
 }
 
-void init_login(LOGIN * login)
+static void init_login(LOGIN * login)
 {
     login->n = 0;
     login->a = 10;
@@ -40,9 +56,8 @@ void init_login(LOGIN * login)
     login->data = (DATA *) malloc(login->a * sizeof(DATA));
 }
 
-void
-add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
-	  const char *pwd)
+static void add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
+		      const char *pwd)
 {
     if (login->n == login->a) {
 	login->a += 10;
@@ -62,7 +77,7 @@ add_login(LOGIN * login, const char *dr, const char *db, const char *usr,
    return: -1 error (cannot read file)
    number of items (0 also if file does not exist)
  */
-int read_file(LOGIN * login)
+static int read_file(LOGIN * login)
 {
     int ret;
     const char *file;
@@ -93,7 +108,7 @@ int read_file(LOGIN * login)
 	G_debug(3, "ret = %d : %s %s %s %s", ret, dr, db, usr, pwd);
 
 	if (ret < 2) {
-	    G_warning("Login file corrupted");
+	    G_warning(_("Login file corrupted"));
 	    continue;
 	}
 
@@ -110,7 +125,7 @@ int read_file(LOGIN * login)
    return: -1 error (cannot read file)
    0 OK
  */
-int write_file(LOGIN * login)
+static int write_file(LOGIN * login)
 {
     int i;
     const char *file;
@@ -146,12 +161,17 @@ int write_file(LOGIN * login)
 
 /*!
    \brief Set user/password for driver/database
+   
+   \param driver driver name
+   \param database database name
+   \param user user name
+   \param password password string
+   
    \return DB_OK
    \return DB_FAILED
  */
-int
-db_set_login(const char *driver, const char *database, const char *user,
-	     const char *password)
+int db_set_login(const char *driver, const char *database, const char *user,
+		 const char *password)
 {
     int i, found;
     LOGIN login;
@@ -193,14 +213,18 @@ db_set_login(const char *driver, const char *database, const char *user,
 }
 
 /*!
-   \brief Get user/password for driver/database
-   if driver/database is not found, user/password are set to NULL
+   \brief Get user/password for driver/database if driver/database is not found, user/password are set to NULL
+
+   \param driver driver name
+   \param database database name
+   \param[out] user name
+   \param[out] password string
+   
    \return DB_OK
    \return DB_FAILED
  */
-int
-db_get_login(const char *driver, const char *database, const char **user,
-	     const char **password)
+int db_get_login(const char *driver, const char *database, const char **user,
+		 const char **password)
 {
     int i;
     LOGIN login;