55 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
66 * Portions Copyright (c) 1994, Regents of the University of California
77 *
8- * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.22 2005/12/12 15:41:52 momjian Exp $
8+ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.23 2005/12/12 15:48:04 momjian Exp $
99 *
1010 *-------------------------------------------------------------------------
1111 */
1717
1818static void help (const char * progname );
1919
20+ enum trivalue {
21+ TRI_DEFAULT ,
22+ TRI_NO ,
23+ TRI_YES
24+ };
2025
2126int
2227main (int argc ,char * argv [])
@@ -51,7 +56,6 @@ main(int argc, char *argv[])
5156const char * progname ;
5257int optindex ;
5358int c ;
54-
5559char * newuser = NULL ;
5660char * host = NULL ;
5761char * port = NULL ;
@@ -62,16 +66,13 @@ main(int argc, char *argv[])
6266char * conn_limit = NULL ;
6367bool pwprompt = false;
6468char * newpassword = NULL ;
65- /*
66- *Tri-valued variables. -1 is "NO", +1 is enable and 0 uses the
67- *server default.
68- */
69- int createdb = 0 ;
70- int superuser = 0 ;
71- int createrole = 0 ;
72- int inherit = 0 ;
73- int login = 0 ;
74- int encrypted = 0 ;
69+ /*Tri-valued variables. */
70+ enum trivalue createdb = TRI_DEFAULT ,
71+ superuser = TRI_DEFAULT ,
72+ createrole = TRI_DEFAULT ,
73+ inherit = TRI_DEFAULT ,
74+ login = TRI_DEFAULT ,
75+ encrypted = TRI_DEFAULT ;
7576
7677PQExpBufferData sql ;
7778
@@ -107,36 +108,36 @@ main(int argc, char *argv[])
107108quiet = true;
108109break ;
109110case 'd' :
110- createdb = +1 ;
111+ createdb = TRI_YES ;
111112break ;
112113case 'D' :
113- createdb = -1 ;
114+ createdb = TRI_NO ;
114115break ;
115116case 's' :
116117case 'a' :
117- superuser = +1 ;
118+ superuser = TRI_YES ;
118119break ;
119120case 'S' :
120121case 'A' :
121- superuser = -1 ;
122+ superuser = TRI_NO ;
122123break ;
123124case 'r' :
124- createrole = +1 ;
125+ createrole = TRI_YES ;
125126break ;
126127case 'R' :
127- createrole = -1 ;
128+ createrole = TRI_NO ;
128129break ;
129130case 'i' :
130- inherit = +1 ;
131+ inherit = TRI_YES ;
131132break ;
132133case 'I' :
133- inherit = -1 ;
134+ inherit = TRI_NO ;
134135break ;
135136case 'l' :
136- login = +1 ;
137+ login = TRI_YES ;
137138break ;
138139case 'L' :
139- login = -1 ;
140+ login = TRI_NO ;
140141break ;
141142case 'c' :
142143conn_limit = optarg ;
@@ -145,10 +146,10 @@ main(int argc, char *argv[])
145146pwprompt = true;
146147break ;
147148case 'E' :
148- encrypted = +1 ;
149+ encrypted = TRI_YES ;
149150break ;
150151case 'N' :
151- encrypted = -1 ;
152+ encrypted = TRI_NO ;
152153break ;
153154default :
154155fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
@@ -195,16 +196,16 @@ main(int argc, char *argv[])
195196
196197reply = simple_prompt ("Shall the new role be a superuser? (y/n) " ,1 , true);
197198if (check_yesno_response (reply )== 1 )
198- superuser = +1 ;
199+ superuser = TRI_YES ;
199200else
200- superuser = -1 ;
201+ superuser = TRI_NO ;
201202}
202203
203- if (superuser == +1 )
204+ if (superuser == TRI_YES )
204205{
205206/* Not much point in trying to restrict a superuser */
206- createdb = +1 ;
207- createrole = +1 ;
207+ createdb = TRI_YES ;
208+ createrole = TRI_YES ;
208209}
209210
210211if (createdb == 0 )
@@ -213,9 +214,9 @@ main(int argc, char *argv[])
213214
214215reply = simple_prompt ("Shall the new role be allowed to create databases? (y/n) " ,1 , true);
215216if (check_yesno_response (reply )== 1 )
216- createdb = +1 ;
217+ createdb = TRI_YES ;
217218else
218- createdb = -1 ;
219+ createdb = TRI_NO ;
219220}
220221
221222if (createrole == 0 )
@@ -224,54 +225,48 @@ main(int argc, char *argv[])
224225
225226reply = simple_prompt ("Shall the new role be allowed to create more new roles? (y/n) " ,1 , true);
226227if (check_yesno_response (reply )== 1 )
227- createrole = +1 ;
228+ createrole = TRI_YES ;
228229else
229- createrole = -1 ;
230+ createrole = TRI_NO ;
230231}
231232
232233if (inherit == 0 )
233- {
234- /* silently default to YES */
235- inherit = +1 ;
236- }
234+ inherit = TRI_YES ;
237235
238236if (login == 0 )
239- {
240- /* silently default to YES */
241- login = +1 ;
242- }
237+ login = TRI_YES ;
243238
244239initPQExpBuffer (& sql );
245240
246241printfPQExpBuffer (& sql ,"CREATE ROLE %s" ,fmtId (newuser ));
247242if (newpassword )
248243{
249- if (encrypted == +1 )
244+ if (encrypted == TRI_YES )
250245appendPQExpBuffer (& sql ," ENCRYPTED" );
251- if (encrypted == -1 )
246+ if (encrypted == TRI_NO )
252247appendPQExpBuffer (& sql ," UNENCRYPTED" );
253248appendPQExpBuffer (& sql ," PASSWORD " );
254249appendStringLiteral (& sql ,newpassword , false);
255250}
256- if (superuser == +1 )
251+ if (superuser == TRI_YES )
257252appendPQExpBuffer (& sql ," SUPERUSER" );
258- if (superuser == -1 )
253+ if (superuser == TRI_NO )
259254appendPQExpBuffer (& sql ," NOSUPERUSER" );
260- if (createdb == +1 )
255+ if (createdb == TRI_YES )
261256appendPQExpBuffer (& sql ," CREATEDB" );
262- if (createdb == -1 )
257+ if (createdb == TRI_NO )
263258appendPQExpBuffer (& sql ," NOCREATEDB" );
264- if (createrole == +1 )
259+ if (createrole == TRI_YES )
265260appendPQExpBuffer (& sql ," CREATEROLE" );
266- if (createrole == -1 )
261+ if (createrole == TRI_NO )
267262appendPQExpBuffer (& sql ," NOCREATEROLE" );
268- if (inherit == +1 )
263+ if (inherit == TRI_YES )
269264appendPQExpBuffer (& sql ," INHERIT" );
270- if (inherit == -1 )
265+ if (inherit == TRI_NO )
271266appendPQExpBuffer (& sql ," NOINHERIT" );
272- if (login == +1 )
267+ if (login == TRI_YES )
273268appendPQExpBuffer (& sql ," LOGIN" );
274- if (login == -1 )
269+ if (login == TRI_NO )
275270appendPQExpBuffer (& sql ," NOLOGIN" );
276271if (conn_limit != NULL )
277272appendPQExpBuffer (& sql ," CONNECTION LIMIT %s" ,conn_limit );