1010 * Written by Peter Eisentraut <peter_e@gmx.net>.
1111 *
1212 * IDENTIFICATION
13- * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.481 2008/11/21 20:14:27 mha Exp $
13+ * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.482 2008/12/02 02:00:32 alvherre Exp $
1414 *
1515 *--------------------------------------------------------------------
1616 */
@@ -4382,16 +4382,17 @@ parse_real(const char *value, double *result)
43824382const char *
43834383config_enum_lookup_by_value (struct config_enum * record ,int val )
43844384{
4385- const struct config_enum_entry * entry = record -> options ;
4386- while (entry && entry -> name )
4385+ const struct config_enum_entry * entry ;
4386+
4387+ for (entry = record -> options ;entry && entry -> name ;entry ++ )
43874388{
43884389if (entry -> val == val )
43894390return entry -> name ;
4390- entry ++ ;
43914391}
4392+
43924393elog (ERROR ,"could not find enum option %d for %s" ,
43934394val ,record -> gen .name );
4394- return NULL ;/* silence compiler */
4395+ return NULL ;/* silence compiler */
43954396}
43964397
43974398
@@ -4400,86 +4401,71 @@ config_enum_lookup_by_value(struct config_enum *record, int val)
44004401 * (case-insensitive).
44014402 * If the enum option is found, sets the retval value and returns
44024403 * true. If it's not found, return FALSE and retval is set to 0.
4403- *
44044404 */
44054405bool
4406- config_enum_lookup_by_name (struct config_enum * record ,const char * value ,int * retval )
4406+ config_enum_lookup_by_name (struct config_enum * record ,const char * value ,
4407+ int * retval )
44074408{
4408- const struct config_enum_entry * entry = record -> options ;
4409-
4410- if (retval )
4411- * retval = 0 ;/* suppress compiler warning */
4412-
4413- while (entry && entry -> name )
4409+ const struct config_enum_entry * entry ;
4410+
4411+ for (entry = record -> options ;entry && entry -> name ;entry ++ )
44144412{
44154413if (pg_strcasecmp (value ,entry -> name )== 0 )
44164414{
44174415* retval = entry -> val ;
44184416return TRUE;
44194417}
4420- entry ++ ;
44214418}
4419+
4420+ * retval = 0 ;
44224421return FALSE;
44234422}
44244423
44254424
44264425/*
44274426 * Return a list of all available options for an enum, excluding
4428- * hidden ones, separated by", " (comma-space) .
4427+ * hidden ones, separated bythe given separator .
44294428 * If prefix is non-NULL, it is added before the first enum value.
44304429 * If suffix is non-NULL, it is added to the end of the string.
44314430 */
44324431static char *
44334432config_enum_get_options (struct config_enum * record ,const char * prefix ,
44344433const char * suffix ,const char * separator )
44354434{
4436- const struct config_enum_entry * entry = record -> options ;
4437- int len = 0 ;
4438- char * hintmsg ;
4439-
4440- if (!entry || !entry -> name )
4441- return NULL ;/* Should not happen */
4435+ const struct config_enum_entry * entry ;
4436+ StringInfoData retstr ;
4437+ int seplen ;
44424438
4443- while (entry && entry -> name )
4444- {
4445- if (!entry -> hidden )
4446- len += strlen (entry -> name )+ strlen (separator );
4447-
4448- entry ++ ;
4449- }
4450-
4451- hintmsg = palloc (len + strlen (prefix )+ strlen (suffix )+ 2 );
4452-
4453- strcpy (hintmsg ,prefix );
4439+ initStringInfo (& retstr );
4440+ appendStringInfoString (& retstr ,prefix );
44544441
4455- entry = record -> options ;
4456- while (entry && entry -> name )
4442+ seplen = strlen ( separator ) ;
4443+ for (entry = record -> options ; entry && entry -> name ; entry ++ )
44574444{
44584445if (!entry -> hidden )
44594446{
4460- strcat ( hintmsg ,entry -> name );
4461- strcat ( hintmsg ,separator );
4447+ appendStringInfoString ( & retstr ,entry -> name );
4448+ appendBinaryStringInfo ( & retstr ,separator , seplen );
44624449}
4463-
4464- entry ++ ;
44654450}
44664451
4467- len = strlen (hintmsg );
4468-
44694452/*
44704453 * All the entries may have been hidden, leaving the string empty
44714454 * if no prefix was given. This indicates a broken GUC setup, since
44724455 * there is no use for an enum without any values, so we just check
44734456 * to make sure we don't write to invalid memory instead of actually
44744457 * trying to do something smart with it.
44754458 */
4476- if (len >=strlen (separator ))
4459+ if (retstr .len >=seplen )
4460+ {
44774461/* Replace final separator */
4478- hintmsg [len - strlen (separator )]= '\0' ;
4462+ retstr .data [retstr .len - seplen ]= '\0' ;
4463+ retstr .len -= seplen ;
4464+ }
44794465
4480- strcat ( hintmsg ,suffix );
4466+ appendStringInfoString ( & retstr ,suffix );
44814467
4482- return hintmsg ;
4468+ return retstr . data ;
44834469}
44844470
44854471/*
@@ -5047,7 +5033,11 @@ set_config_option(const char *name, const char *value,
50475033{
50485034if (!config_enum_lookup_by_name (conf ,value ,& newval ))
50495035{
5050- char * hintmsg = config_enum_get_options (conf ,"Available values: " ,"." ,", " );
5036+ char * hintmsg ;
5037+
5038+ hintmsg = config_enum_get_options (conf ,
5039+ "Available values: " ,
5040+ "." ,", " );
50515041
50525042ereport (elevel ,
50535043(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
@@ -6253,13 +6243,16 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
62536243
62546244/* enumvals */
62556245/* NOTE! enumvals with double quotes in them are not supported! */
6256- values [11 ]= config_enum_get_options ((struct config_enum * )conf ,"{\"" ,"\"}" ,"\",\"" );
6246+ values [11 ]= config_enum_get_options ((struct config_enum * )conf ,
6247+ "{\"" ,"\"}" ,"\",\"" );
62576248
62586249/* boot_val */
6259- values [12 ]= pstrdup (config_enum_lookup_by_value (lconf ,lconf -> boot_val ));
6250+ values [12 ]= pstrdup (config_enum_lookup_by_value (lconf ,
6251+ lconf -> boot_val ));
62606252
62616253/* reset_val */
6262- values [13 ]= pstrdup (config_enum_lookup_by_value (lconf ,lconf -> reset_val ));
6254+ values [13 ]= pstrdup (config_enum_lookup_by_value (lconf ,
6255+ lconf -> reset_val ));
62636256}
62646257break ;
62656258
@@ -6672,8 +6665,8 @@ is_newvalue_equal(struct config_generic * record, const char *newvalue)
66726665struct config_enum * conf = (struct config_enum * )record ;
66736666int newval ;
66746667
6675- return config_enum_lookup_by_name (conf ,newvalue ,& newval )
6676- && * conf -> variable == newval ;
6668+ return config_enum_lookup_by_name (conf ,newvalue ,& newval )&&
6669+ * conf -> variable == newval ;
66776670}
66786671}
66796672