Some cleanups of enum-guc code, per comments from Tom.
authorMagnus Hagander <magnus@hagander.net>
Sun, 16 Mar 2008 16:42:44 +0000 (16:42 +0000)
committerMagnus Hagander <magnus@hagander.net>
Sun, 16 Mar 2008 16:42:44 +0000 (16:42 +0000)
src/backend/utils/misc/README
src/backend/utils/misc/guc.c
src/include/utils/guc.h
src/include/utils/guc_tables.h

index e99fd4b22d7a89fe100127faf41a32b5725cc272..eb5d926989eeb7403d101bb516a881e9b86175ba 100644 (file)
@@ -4,7 +4,7 @@ $PostgreSQL$
 GUC IMPLEMENTATION NOTES
 
 The GUC (Grand Unified Configuration) module implements configuration
-variables of multiple types (currently boolean, int, float, and string).
+variables of multiple types (currently boolean, enum, int, float, and string).
 Variable settings can come from various places, with a priority ordering
 determining which setting is used.
 
index 3dc4507f1a7be6793f548374f1a935ee01466c6c..588945096908ddce6b53e0730b59b4634a3a12cd 100644 (file)
@@ -168,6 +168,14 @@ static const char *show_tcp_keepalives_count(void);
 static bool assign_autovacuum_max_workers(int newval, bool doit, GucSource source);
 static bool assign_maxconnections(int newval, bool doit, GucSource source);
 
+static const char *config_enum_lookup_value(struct config_enum *record, int val);
+static bool config_enum_lookup_name(struct config_enum *record, 
+                                                                       const char *value, int *retval);
+static char *config_enum_get_options(struct config_enum *record, 
+                                                                        const char *prefix, const char *suffix);
+
+
+
 /*
  * Options for enum values defined in this module.
  */
@@ -3134,8 +3142,9 @@ InitializeGUCOptions(void)
                                        if (conf->assign_hook)
                                                if (!(*conf->assign_hook) (conf->boot_val, true,
                                                                                                   PGC_S_DEFAULT))
-                                                       elog(FATAL, "failed to initialize %s to %d",
-                                                                conf->gen.name, conf->boot_val);
+                                                       elog(FATAL, "failed to initialize %s to %s",
+                                                                conf->gen.name, 
+                                                                config_enum_lookup_value(conf, conf->boot_val));
                                        *conf->variable = conf->reset_val = conf->boot_val;
                                        break;
                                }
@@ -4230,7 +4239,7 @@ config_enum_lookup_value(struct config_enum *record, int val)
  * Lookup the value for an enum option with the selected name
  * (case-insensitive).
  * If the enum option is found, sets the retval value and returns
- * true. If it's not found, return FALSE and don't touch retval.
+ * true. If it's not found, return FALSE and retval is set to 0.
  *
  */
 static bool
@@ -4243,7 +4252,7 @@ config_enum_lookup_name(struct config_enum *record, const char *value, int *retv
        
        while (entry && entry->name)
        {
-               if (!pg_strcasecmp(value, entry->name))
+               if (pg_strcasecmp(value, entry->name) == 0)
                {
                        *retval = entry->val;
                        return TRUE;
@@ -4255,10 +4264,10 @@ config_enum_lookup_name(struct config_enum *record, const char *value, int *retv
 
 
 /*
- * Returna list of all available options for an enum, separated
+ * Return a list of all available options for an enum, separated
  * by ", " (comma-space).
- * If prefix is gievn, it is added before the first enum value.
- * If suffix is given, it is added to the end of the string.
+ * If prefix is non-NULL, it is added before the first enum value.
+ * If suffix is non-NULL, it is added to the end of the string.
  */
 static char *
 config_enum_get_options(struct config_enum *record, const char *prefix, const char *suffix)
@@ -4895,8 +4904,9 @@ set_config_option(const char *name, const char *value,
                                        {
                                                ereport(elevel,
                                                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                                                errmsg("invalid value for parameter \"%s\": \"%d\"",
-                                                                               name, newval)));
+                                                                errmsg("invalid value for parameter \"%s\": \"%s\"",
+                                                                               name, 
+                                                                               config_enum_lookup_value(conf, newval))));
                                                return false;
                                        }
 
@@ -5592,6 +5602,30 @@ DefineCustomStringVariable(const char *name,
        define_custom_variable(&var->gen);
 }
 
+void
+DefineCustomEnumVariable(const char *name,
+                                                const char *short_desc,
+                                                const char *long_desc,
+                                                int *valueAddr,
+                                                const struct config_enum_entry *options,
+                                                GucContext context,
+                                                GucEnumAssignHook assign_hook,
+                                                GucShowHook show_hook)
+{
+       struct config_enum *var;
+
+       var = (struct config_enum *)
+               init_custom_variable(name, short_desc, long_desc, context,
+                                                        PGC_ENUM, sizeof(struct config_enum));
+       var->variable = valueAddr;
+       var->boot_val = *valueAddr;
+       var->reset_val = *valueAddr;
+       var->options = options;
+       var->assign_hook = assign_hook;
+       var->show_hook = show_hook;
+       define_custom_variable(&var->gen);
+}
+
 void
 EmitWarningsOnPlaceholders(const char *className)
 {
index 1d0a7c1171165de08b28072f0a3fbcf107e9db94..75f258260c946a77d3c1e99cb4628d9ac535da80 100644 (file)
@@ -93,6 +93,16 @@ typedef enum
        PGC_S_SESSION                           /* SET command */
 } GucSource;
 
+/*
+ * Enum values are made up of an array of name-value pairs
+ */
+struct config_enum_entry
+{
+       const char *name;
+       int         val;
+};
+
+
 typedef const char *(*GucStringAssignHook) (const char *newval, bool doit, GucSource source);
 typedef bool (*GucBoolAssignHook) (bool newval, bool doit, GucSource source);
 typedef bool (*GucIntAssignHook) (int newval, bool doit, GucSource source);
@@ -189,6 +199,16 @@ extern void DefineCustomStringVariable(
                                                   GucStringAssignHook assign_hook,
                                                   GucShowHook show_hook);
 
+extern void DefineCustomEnumVariable(
+                                                  const char *name,
+                                                  const char *short_desc,
+                                                  const char *long_desc,
+                                                  int *valueAddr,
+                                                  const struct config_enum_entry *options,
+                                                  GucContext context,
+                                                  GucEnumAssignHook assign_hook,
+                                                  GucShowHook show_hook);
+
 extern void EmitWarningsOnPlaceholders(const char *className);
 
 extern const char *GetConfigOption(const char *name);
index 48a4e81f141b1d13b2a233fb783331d27a63e5a6..215366ef17fee7abbf6d93cdbd1bd059e7ad77fd 100644 (file)
@@ -37,15 +37,6 @@ union config_var_value
        int                     enumval;
 };
 
-/*
- * Enum values are made up of an array of name-value pairs
- */
-struct config_enum_entry
-{
-       const char *name;
-       int                     val;
-};
-
 /*
  * Groupings to help organize all the run-time options for display
  */