Use a proper enum for tri-valued variables.

This commit is contained in:
Bruce Momjian 2005-12-12 15:48:04 +00:00
parent 59b89e9cc7
commit f82e2baef6
1 changed files with 48 additions and 53 deletions

View File

@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.22 2005/12/12 15:41:52 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.23 2005/12/12 15:48:04 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -17,6 +17,11 @@
static void help(const char *progname); static void help(const char *progname);
enum trivalue {
TRI_DEFAULT,
TRI_NO,
TRI_YES
};
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -51,7 +56,6 @@ main(int argc, char *argv[])
const char *progname; const char *progname;
int optindex; int optindex;
int c; int c;
char *newuser = NULL; char *newuser = NULL;
char *host = NULL; char *host = NULL;
char *port = NULL; char *port = NULL;
@ -62,16 +66,13 @@ main(int argc, char *argv[])
char *conn_limit = NULL; char *conn_limit = NULL;
bool pwprompt = false; bool pwprompt = false;
char *newpassword = NULL; char *newpassword = NULL;
/* /* Tri-valued variables. */
* Tri-valued variables. -1 is "NO", +1 is enable and 0 uses the enum trivalue createdb = TRI_DEFAULT,
* server default. superuser = TRI_DEFAULT,
*/ createrole = TRI_DEFAULT,
int createdb = 0; inherit = TRI_DEFAULT,
int superuser = 0; login = TRI_DEFAULT,
int createrole = 0; encrypted = TRI_DEFAULT;
int inherit = 0;
int login = 0;
int encrypted = 0;
PQExpBufferData sql; PQExpBufferData sql;
@ -107,36 +108,36 @@ main(int argc, char *argv[])
quiet = true; quiet = true;
break; break;
case 'd': case 'd':
createdb = +1; createdb = TRI_YES;
break; break;
case 'D': case 'D':
createdb = -1; createdb = TRI_NO;
break; break;
case 's': case 's':
case 'a': case 'a':
superuser = +1; superuser = TRI_YES;
break; break;
case 'S': case 'S':
case 'A': case 'A':
superuser = -1; superuser = TRI_NO;
break; break;
case 'r': case 'r':
createrole = +1; createrole = TRI_YES;
break; break;
case 'R': case 'R':
createrole = -1; createrole = TRI_NO;
break; break;
case 'i': case 'i':
inherit = +1; inherit = TRI_YES;
break; break;
case 'I': case 'I':
inherit = -1; inherit = TRI_NO;
break; break;
case 'l': case 'l':
login = +1; login = TRI_YES;
break; break;
case 'L': case 'L':
login = -1; login = TRI_NO;
break; break;
case 'c': case 'c':
conn_limit = optarg; conn_limit = optarg;
@ -145,10 +146,10 @@ main(int argc, char *argv[])
pwprompt = true; pwprompt = true;
break; break;
case 'E': case 'E':
encrypted = +1; encrypted = TRI_YES;
break; break;
case 'N': case 'N':
encrypted = -1; encrypted = TRI_NO;
break; break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
@ -195,16 +196,16 @@ main(int argc, char *argv[])
reply = simple_prompt("Shall the new role be a superuser? (y/n) ", 1, true); reply = simple_prompt("Shall the new role be a superuser? (y/n) ", 1, true);
if (check_yesno_response(reply) == 1) if (check_yesno_response(reply) == 1)
superuser = +1; superuser = TRI_YES;
else else
superuser = -1; superuser = TRI_NO;
} }
if (superuser == +1) if (superuser == TRI_YES)
{ {
/* Not much point in trying to restrict a superuser */ /* Not much point in trying to restrict a superuser */
createdb = +1; createdb = TRI_YES;
createrole = +1; createrole = TRI_YES;
} }
if (createdb == 0) if (createdb == 0)
@ -213,9 +214,9 @@ main(int argc, char *argv[])
reply = simple_prompt("Shall the new role be allowed to create databases? (y/n) ", 1, true); reply = simple_prompt("Shall the new role be allowed to create databases? (y/n) ", 1, true);
if (check_yesno_response(reply) == 1) if (check_yesno_response(reply) == 1)
createdb = +1; createdb = TRI_YES;
else else
createdb = -1; createdb = TRI_NO;
} }
if (createrole == 0) if (createrole == 0)
@ -224,54 +225,48 @@ main(int argc, char *argv[])
reply = simple_prompt("Shall the new role be allowed to create more new roles? (y/n) ", 1, true); reply = simple_prompt("Shall the new role be allowed to create more new roles? (y/n) ", 1, true);
if (check_yesno_response(reply) == 1) if (check_yesno_response(reply) == 1)
createrole = +1; createrole = TRI_YES;
else else
createrole = -1; createrole = TRI_NO;
} }
if (inherit == 0) if (inherit == 0)
{ inherit = TRI_YES;
/* silently default to YES */
inherit = +1;
}
if (login == 0) if (login == 0)
{ login = TRI_YES;
/* silently default to YES */
login = +1;
}
initPQExpBuffer(&sql); initPQExpBuffer(&sql);
printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser)); printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
if (newpassword) if (newpassword)
{ {
if (encrypted == +1) if (encrypted == TRI_YES)
appendPQExpBuffer(&sql, " ENCRYPTED"); appendPQExpBuffer(&sql, " ENCRYPTED");
if (encrypted == -1) if (encrypted == TRI_NO)
appendPQExpBuffer(&sql, " UNENCRYPTED"); appendPQExpBuffer(&sql, " UNENCRYPTED");
appendPQExpBuffer(&sql, " PASSWORD "); appendPQExpBuffer(&sql, " PASSWORD ");
appendStringLiteral(&sql, newpassword, false); appendStringLiteral(&sql, newpassword, false);
} }
if (superuser == +1) if (superuser == TRI_YES)
appendPQExpBuffer(&sql, " SUPERUSER"); appendPQExpBuffer(&sql, " SUPERUSER");
if (superuser == -1) if (superuser == TRI_NO)
appendPQExpBuffer(&sql, " NOSUPERUSER"); appendPQExpBuffer(&sql, " NOSUPERUSER");
if (createdb == +1) if (createdb == TRI_YES)
appendPQExpBuffer(&sql, " CREATEDB"); appendPQExpBuffer(&sql, " CREATEDB");
if (createdb == -1) if (createdb == TRI_NO)
appendPQExpBuffer(&sql, " NOCREATEDB"); appendPQExpBuffer(&sql, " NOCREATEDB");
if (createrole == +1) if (createrole == TRI_YES)
appendPQExpBuffer(&sql, " CREATEROLE"); appendPQExpBuffer(&sql, " CREATEROLE");
if (createrole == -1) if (createrole == TRI_NO)
appendPQExpBuffer(&sql, " NOCREATEROLE"); appendPQExpBuffer(&sql, " NOCREATEROLE");
if (inherit == +1) if (inherit == TRI_YES)
appendPQExpBuffer(&sql, " INHERIT"); appendPQExpBuffer(&sql, " INHERIT");
if (inherit == -1) if (inherit == TRI_NO)
appendPQExpBuffer(&sql, " NOINHERIT"); appendPQExpBuffer(&sql, " NOINHERIT");
if (login == +1) if (login == TRI_YES)
appendPQExpBuffer(&sql, " LOGIN"); appendPQExpBuffer(&sql, " LOGIN");
if (login == -1) if (login == TRI_NO)
appendPQExpBuffer(&sql, " NOLOGIN"); appendPQExpBuffer(&sql, " NOLOGIN");
if (conn_limit != NULL) if (conn_limit != NULL)
appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit); appendPQExpBuffer(&sql, " CONNECTION LIMIT %s", conn_limit);