-
-
Notifications
You must be signed in to change notification settings - Fork 293
/
main.c
113 lines (95 loc) · 3.43 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/****************************************************************************
*
* 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: Store db login settings
* COPYRIGHT: (C) 2004-2015 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.
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <grass/config.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#endif
#include <grass/gis.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
int main(int argc, char *argv[])
{
struct Option *driver, *database, *user, *password, *host, *port;
struct Flag *print;
struct GModule *module;
/* Initialize the GIS calls */
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("database"));
G_add_keyword(_("connection settings"));
module->description = _("Sets user/password for DB driver/database.");
module->overwrite = TRUE;
driver = G_define_standard_option(G_OPT_DB_DRIVER);
driver->options = db_list_drivers();
driver->required = YES;
driver->answer = (char *) db_get_default_driver_name();
database = G_define_standard_option(G_OPT_DB_DATABASE);
database->required = YES;
database->answer = (char *) db_get_default_database_name();
user = G_define_option();
user->key = "user";
user->type = TYPE_STRING;
user->required = NO;
user->multiple = NO;
user->description = _("Username");
user->guisection = _("Settings");
password = G_define_option();
password->key = "password";
password->type = TYPE_STRING;
password->required = NO;
password->multiple = NO;
password->description = _("Password");
password->guisection = _("Settings");
host = G_define_option();
host->key = "host";
host->type = TYPE_STRING;
host->required = NO;
host->multiple = NO;
host->label = _("Hostname");
host->description = _("Relevant only for pg and mysql driver");
host->guisection = _("Settings");
port = G_define_option();
port->key = "port";
port->type = TYPE_STRING;
port->required = NO;
port->multiple = NO;
port->label = _("Port");
port->description = _("Relevant only for pg and mysql driver");
port->guisection = _("Settings");
print = G_define_flag();
print->key = 'p';
print->description = _("Print connection settings and exit");
print->guisection = _("Print");
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
if (print->answer) {
/* print all settings to standard output and exit */
db_get_login_dump(stdout);
exit(EXIT_SUCCESS);
}
if (db_set_login2(driver->answer, database->answer, user->answer,
password->answer, host->answer, port->answer,
G_get_overwrite()) == DB_FAILED) {
G_fatal_error(_("Unable to set user/password"));
}
if (password->answer)
G_important_message(_("The password was stored in file (%s%cdblogin)"),
G_config_path(), HOST_DIRSEP);
exit(EXIT_SUCCESS);
}