]> granicus.if.org Git - postgresql/blob - src/bin/scripts/dropuser.c
Internationalize interactive yes/no responses.
[postgresql] / src / bin / scripts / dropuser.c
1 /*-------------------------------------------------------------------------
2  *
3  * dropuser
4  *
5  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
6  * Portions Copyright (c) 1994, Regents of the University of California
7  *
8  * $Header: /cvsroot/pgsql/src/bin/scripts/dropuser.c,v 1.3 2003/05/27 19:36:55 petere Exp $
9  *
10  *-------------------------------------------------------------------------
11  */
12
13 #include "postgres_fe.h"
14 #include "common.h"
15 #include "dumputils.h"
16
17
18 static void help(const char *progname);
19
20
21 int
22 main(int argc, char *argv[])
23 {
24         static struct option long_options[] = {
25                 {"host", required_argument, NULL, 'h'},
26                 {"port", required_argument, NULL, 'p'},
27                 {"username", required_argument, NULL, 'U'},
28                 {"password", no_argument, NULL, 'W'},
29                 {"echo", no_argument, NULL, 'e'},
30                 {"quiet", no_argument, NULL, 'q'},
31                 {"interactive", no_argument, NULL, 'i'},
32                 {NULL, 0, NULL, 0}
33         };
34
35         char       *progname;
36         int                     optindex;
37         int                     c;
38
39         char       *dropuser = NULL;
40         char       *host = NULL;
41         char       *port = NULL;
42         char       *username = NULL;
43         bool            password = false;
44         bool            echo = false;
45         bool            quiet = false;
46         bool            interactive = false;
47
48         PQExpBufferData sql;
49
50         PGconn     *conn;
51         PGresult   *result;
52
53         progname = get_progname(argv[0]);
54         init_nls();
55         handle_help_version_opts(argc, argv, "dropuser", help);
56
57         while ((c = getopt_long(argc, argv, "h:p:U:Weqi", long_options, &optindex)) != -1)
58         {
59                 switch (c)
60                 {
61                         case 'h':
62                                 host = optarg;
63                                 break;
64                         case 'p':
65                                 port = optarg;
66                                 break;
67                         case 'U':
68                                 username = optarg;
69                                 break;
70                         case 'W':
71                                 password = true;
72                                 break;
73                         case 'e':
74                                 echo = true;
75                                 break;
76                         case 'q':
77                                 quiet = true;
78                                 break;
79                         case 'i':
80                                 interactive = true;
81                                 break;
82                         default:
83                                 fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
84                                 exit(1);
85                 }
86         }
87
88         switch (argc - optind)
89         {
90                 case 0:
91                         break;
92                 case 1:
93                         dropuser = argv[optind];
94                         break;
95                 default:
96                         fprintf(stderr, _("%s: too many command-line arguments (first is '%s')\n"),
97                                         progname, argv[optind + 1]);
98                         fprintf(stderr, _("Try '%s --help' for more information.\n"), progname);
99                         exit(1);
100         }
101
102         if (dropuser == NULL)
103                 dropuser = simple_prompt("Enter name of user to drop: ", 128, true);
104
105         if (interactive)
106         {
107                 char       *reply;
108
109                 printf(_("User \"%s\" will be permanently deleted.\n"), dropuser);
110                 reply = simple_prompt("Are you sure? (y/n) ", 1, true);
111                 if (check_yesno_response(reply) != 1)
112                         exit(0);
113         }
114
115         initPQExpBuffer(&sql);
116         appendPQExpBuffer(&sql, "DROP USER %s;\n", fmtId(dropuser));
117
118         conn = connectDatabase("template1", host, port, username, password, progname);
119
120         if (echo)
121                 printf("%s", sql.data);
122         result = PQexec(conn, sql.data);
123
124         if (PQresultStatus(result) != PGRES_COMMAND_OK)
125         {
126                 fprintf(stderr, _("%s: deletion of user %s failed: %s"),
127                                 progname, dropuser, PQerrorMessage(conn));
128                 PQfinish(conn);
129                 exit(1);
130         }
131
132         PQfinish(conn);
133         if (!quiet)
134                 puts("DROP USER");
135         exit(0);
136 }
137
138
139 static void
140 help(const char *progname)
141 {
142         printf(_("%s removes a PostgreSQL user.\n\n"), progname);
143         printf(_("Usage:\n"));
144         printf(_("  %s [OPTION]... [USERNAME]\n"), progname);
145         printf(_("\nOptions:\n"));
146     printf(_("  -e, --echo                show the commands being sent to the server\n"));
147         printf(_("  -i, --interactive         prompt before deleting anything\n"));
148     printf(_("  -q, --quiet               don't write any messages\n"));
149         printf(_("  -h, --host=HOSTNAME       database server host\n"));
150         printf(_("  -p, --port=PORT           database server port\n"));
151         printf(_("  -U, --username=USERNAME   user name to connect as (not the one to drop)\n"));
152         printf(_("  -W, --password            prompt for password to connect\n"));
153         printf(_("  --help                    show this help, then exit\n"));
154         printf(_("  --version                 output version information, then exit\n"));
155         printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
156 }