/* -*-pgsql-c-*- */
/*
 * Scanner for the configuration file
 *
 * Copyright (c) 2000-2007, PostgreSQL Global Development Group
 *
 * $PostgreSQL: pgsql/src/backend/utils/misc/guc-file.l,v 1.48 2007/03/12 22:09:27 petere Exp $
 */

%{

#include "postgres.h"

#include <ctype.h>
#include <unistd.h>

#include "miscadmin.h"
#include "storage/fd.h"
#include "utils/guc.h"


/* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
#undef fprintf
#define fprintf(file, fmt, msg)  ereport(ERROR, (errmsg_internal("%s", msg)))

enum {
	GUC_ID = 1,
	GUC_STRING = 2,
	GUC_INTEGER = 3,
	GUC_REAL = 4,
	GUC_EQUALS = 5,
	GUC_UNQUOTED_STRING = 6,
	GUC_QUALIFIED_ID = 7,
	GUC_EOL = 99,
	GUC_ERROR = 100
};

struct name_value_pair
{
	char       *name;
	char       *value;
	struct name_value_pair *next;
};

static unsigned int ConfigFileLineno;

/* flex fails to supply a prototype for yylex, so provide one */
int GUC_yylex(void);

static bool ParseConfigFile(const char *config_file, const char *calling_file,
							int depth, GucContext context, int elevel,
							struct name_value_pair **head_p,
							struct name_value_pair **tail_p);
static void free_name_value_list(struct name_value_pair * list);
static char *GUC_scanstr(const char *s);

static char *custom_variable_classes_backup;

%}

%option 8bit
%option never-interactive
%option nodefault
%option nounput
%option noyywrap
%option prefix="GUC_yy"


SIGN            ("-"|"+")
DIGIT           [0-9]
HEXDIGIT        [0-9a-fA-F]

UNIT_LETTER     [a-zA-Z]

INTEGER         {SIGN}?({DIGIT}+|0x{HEXDIGIT}+){UNIT_LETTER}*

EXPONENT        [Ee]{SIGN}?{DIGIT}+
REAL            {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?

LETTER          [A-Za-z_\200-\377]
LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]

ID              {LETTER}{LETTER_OR_DIGIT}*
QUALIFIED_ID    {ID}"."{ID}

UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
STRING          \'([^'\\\n]|\\.|\'\')*\'

%%

\n              ConfigFileLineno++; return GUC_EOL;
[ \t\r]+        /* eat whitespace */
#.*             /* eat comment (.* matches anything until newline) */

{ID}            return GUC_ID;
{QUALIFIED_ID}  return GUC_QUALIFIED_ID;
{STRING}        return GUC_STRING;
{UNQUOTED_STRING} return GUC_UNQUOTED_STRING;
{INTEGER}       return GUC_INTEGER;
{REAL}          return GUC_REAL;
=               return GUC_EQUALS;

.               return GUC_ERROR;

%%



/*
 * Exported function to read and process the configuration file. The
 * parameter indicates in what context the file is being read --- either
 * postmaster startup (including standalone-backend startup) or SIGHUP.
 * All options mentioned in the configuration file are set to new values.
 * If an error occurs, no values will be changed.
 */
void
ProcessConfigFile(GucContext context)
{
	int			elevel;
	struct name_value_pair *item, *head, *tail;
	int						i;
	bool				   *in_conffile = NULL;
	const char			   *var;
	bool					success = false;

	Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);

	if (context == PGC_SIGHUP)
	{
		/*
		 * To avoid cluttering the log, only the postmaster bleats loudly
		 * about problems with the config file.
		 */
		elevel = IsUnderPostmaster ? DEBUG2 : LOG;
	}
	else
		elevel = ERROR;

	head = tail = NULL;

	/*
	 * We do not know whether we will read the configuration file
	 * without error.  If we encounter an error we have to restore the
	 * previous value of the custom_variable_classes variable for
	 * consistency.  Therefore we have to save its value.
	 */
	var = GetConfigOption("custom_variable_classes");
	if (var)
		custom_variable_classes_backup = pstrdup(var);
	set_config_option("custom_variable_classes", NULL, context,
					  PGC_S_DEFAULT, false, true);

	if (!ParseConfigFile(ConfigFileName, NULL,
						 0, context, elevel,
						 &head, &tail))
		goto cleanup_list;

	/* Check if all options are valid */
	for (item = head; item; item = item->next)
	{
		char *sep = strchr(item->name, GUC_QUALIFIER_SEPARATOR);
		if (sep && !is_custom_class(item->name, sep - item->name))
		{
			ereport(elevel,
				(errcode(ERRCODE_UNDEFINED_OBJECT),
				 errmsg("unrecognized configuration parameter \"%s\"",
						item->name)));
			goto cleanup_list;
		}

		if (!set_config_option(item->name, item->value, context,
							   PGC_S_FILE, false, false))
			goto cleanup_list;
	}


	/*
	 * Mark all variables as not showing up in the config file.  The
	 * allocation has to take place after ParseConfigFile() since this
	 * function can change num_guc_variables due to custom variables.
	 * It would be easier to add a new field or status bit to struct
	 * conf_generic, but that way we would expose internal information
	 * that is just needed here in the following few lines.  The price
	 * to pay for this separation are a few more loops over the set of
	 * configuration options, but those are expected to be rather few
	 * and we only have to pay the cost at SIGHUP.  We initialize
	 * in_conffile only here because set_config_option() makes
	 * guc_variables grow with custom variables.
	 */
	in_conffile = guc_malloc(elevel, num_guc_variables * sizeof(bool));
	if (!in_conffile)
		goto cleanup_list;
	for (i = 0; i < num_guc_variables; i++)
		in_conffile[i] = false;

	for (item = head; item; item = item->next)
	{
		/*
		 * After set_config_option() the variable name item->name is
		 * known to exist.
		 */
		Assert(guc_get_index(item->name) >= 0);
		in_conffile[guc_get_index(item->name)] = true;
	}

	for (i = 0; i < num_guc_variables; i++)
	{
		struct config_generic *gconf = guc_variables[i];
		if (!in_conffile[i] && strchr(gconf->name, GUC_QUALIFIER_SEPARATOR))
		{
			/* handle custom variables here */
			int j;

			/*
			 * guc_delete_variable will re-sort the array of
			 * configuration options and decrease num_guc_variables by
			 * one.
			 */
			guc_delete_custom_variable(gconf->name);

			/*
			 * Since the array is always sorted we just have to shift
			 * all boolean values after position i by one position to
			 * the left.
			 */
			for (j = i; j < num_guc_variables; j++)
				in_conffile[j] = in_conffile[j+1];

			/*
			 * Decrease the loop variable to re-test the entry at the
			 * current position that is now a different one.
			 */
			i--;

			continue;
		}
		else if (!in_conffile[i] && gconf->source == PGC_S_FILE)
		{
			if (gconf->context < PGC_SIGHUP)
				ereport(elevel,
					(errcode(ERRCODE_CANT_CHANGE_RUNTIME_PARAM),
							 errmsg("parameter \"%s\" cannot be changed after server start; configuration file change ignored",
									gconf->name)));
			else
			{
					/* prepare */
					GucStack   *stack;
					if (gconf->reset_source == PGC_S_FILE)
						gconf->reset_source = PGC_S_DEFAULT;
					for (stack = gconf->stack; stack; stack = stack->prev)
						if (stack->source == PGC_S_FILE)
							stack->source = PGC_S_DEFAULT;
					/* apply the default */
					set_config_option(gconf->name, NULL, context,
									  PGC_S_DEFAULT, false, true);
			}
		}
		else if (!in_conffile[i] && gconf->reset_source == PGC_S_FILE)
		{
			/*------
			 * Change the reset_val to default_val.  Here's an
			 * example: In the configuration file we have
			 *
			 * seq_page_cost = 3.00
			 *
			 * Now we execute in a session
			 *
			 * SET seq_page_cost TO 4.00;
			 *
			 * Then we remove this option from the configuration file
			 * and send SIGHUP. Now when you execute
			 *
			 * RESET seq_page_cost;
			 *
			 * it should fall back to 1.00 (the default value for
			 * seq_page_cost) and not to 3.00 (which is the current
			 * reset_val).
			 */

			switch (gconf->vartype)
			{
				case PGC_BOOL:
					{
						struct config_bool *conf;
						conf = (struct config_bool *) gconf;
						conf->reset_val = conf->boot_val;
						break;
					}
				case PGC_INT:
					{
						struct config_int *conf;
						conf = (struct config_int *) gconf;
						conf->reset_val = conf->boot_val;
						break;
					}
				case PGC_REAL:
					{
						struct config_real *conf;
						conf = (struct config_real *) gconf;
						conf->reset_val = conf->boot_val;
						break;
					}
				case PGC_STRING:
					{
						struct config_string   *conf;
						conf = (struct config_string *) gconf;
						/*
						 * We can cast away the const here because we
						 * won't free the address.  It is protected by
						 * set_string_field() and string_field_used().
						 */
						conf->reset_val = (char *) conf->boot_val;
						break;
					}
			}
		}
	}

	/* If we got here all the options checked out okay, so apply them. */
	for (item = head; item; item = item->next)
		set_config_option(item->name, item->value, context,
						  PGC_S_FILE, false, true);

	/*
	 * Reset variables to the value of environment variables
	 * (PGC_S_ENV_VAR overrules PGC_S_FILE).  PGPORT is ignored,
	 * because it cannot be changed without restart.
	 */
	var = getenv("PGDATESTYLE");
	if (var != NULL)
		set_config_option("datestyle", var, context,
						  PGC_S_ENV_VAR, false, true);

	var = getenv("PGCLIENTENCODING");
	if (var != NULL)
		set_config_option("client_encoding", var, context,
						  PGC_S_ENV_VAR, false, true);

	success = true;

 cleanup_list:
	free(in_conffile);
	free_name_value_list(head);
	if (!success)
		set_config_option("custom_variable_classes",
						  custom_variable_classes_backup,
						  context, PGC_S_FILE, false, true);
	if (custom_variable_classes_backup)
	{
		pfree(custom_variable_classes_backup);
		custom_variable_classes_backup = NULL;
	}
}


/*
 * Read and parse a single configuration file.  This function recurses
 * to handle "include" directives.
 *
 * Input parameters:
 *	config_file: absolute or relative path of file to read
 *	calling_file: absolute path of file containing the "include" directive,
 *		or NULL at outer level (config_file must be absolute at outer level)
 *	depth: recursion depth (used only to prevent infinite recursion)
 *	context: GucContext passed to ProcessConfigFile()
 *	elevel: error logging level determined by ProcessConfigFile()
 * Output parameters:
 *	head_p, tail_p: head and tail of linked list of name/value pairs
 *
 * *head_p and *tail_p must be initialized to NULL before calling the outer
 * recursion level.  On exit, they contain a list of name-value pairs read
 * from the input file(s).
 *
 * Returns TRUE if successful, FALSE if an error occurred.  The error has
 * already been ereport'd, it is only necessary for the caller to clean up
 * its own state and release the name/value pairs list.
 *
 * Note: if elevel >= ERROR then an error will not return control to the
 * caller, and internal state such as open files will not be cleaned up.
 * This case occurs only during postmaster or standalone-backend startup,
 * where an error will lead to immediate process exit anyway; so there is
 * no point in contorting the code so it can clean up nicely.
 */
static bool
ParseConfigFile(const char *config_file, const char *calling_file,
				int depth, GucContext context, int elevel,
				struct name_value_pair **head_p,
				struct name_value_pair **tail_p)
{
	bool		OK = true;
	char		abs_path[MAXPGPATH];
	FILE	   *fp;
	YY_BUFFER_STATE lex_buffer;
	int			token;

	/*
	 * Reject too-deep include nesting depth.  This is just a safety check
	 * to avoid dumping core due to stack overflow if an include file loops
	 * back to itself.  The maximum nesting depth is pretty arbitrary.
	 */
	if (depth > 10)
	{
		ereport(elevel,
				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
				 errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
						config_file)));
		return false;
	}

	/*
	 * If config_file is a relative path, convert to absolute.  We consider
	 * it to be relative to the directory holding the calling file.
	 */
	if (!is_absolute_path(config_file))
	{
		Assert(calling_file != NULL);
		strlcpy(abs_path, calling_file, sizeof(abs_path));
		get_parent_directory(abs_path);
		join_path_components(abs_path, abs_path, config_file);
		canonicalize_path(abs_path);
		config_file = abs_path;
	}

	fp = AllocateFile(config_file, "r");
	if (!fp)
	{
		ereport(elevel,
				(errcode_for_file_access(),
				 errmsg("could not open configuration file \"%s\": %m",
						config_file)));
		return false;
	}

	/*
	 * Parse
	 */
	lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
	yy_switch_to_buffer(lex_buffer);

	ConfigFileLineno = 1;

	/* This loop iterates once per logical line */
	while ((token = yylex()))
	{
		char	   *opt_name, *opt_value;

		if (token == GUC_EOL)	/* empty or comment line */
			continue;

		/* first token on line is option name */
		if (token != GUC_ID && token != GUC_QUALIFIED_ID)
			goto parse_error;
		opt_name = pstrdup(yytext);

		/* next we have an optional equal sign; discard if present */
		token = yylex();
		if (token == GUC_EQUALS)
			token = yylex();

		/* now we must have the option value */
		if (token != GUC_ID &&
			token != GUC_STRING && 
			token != GUC_INTEGER && 
			token != GUC_REAL && 
			token != GUC_UNQUOTED_STRING)
			goto parse_error;
		if (token == GUC_STRING)	/* strip quotes and escapes */
			opt_value = GUC_scanstr(yytext);
		else
			opt_value = pstrdup(yytext);

		/* now we'd like an end of line, or possibly EOF */
		token = yylex();
		if (token != GUC_EOL && token != 0)
			goto parse_error;

		/* OK, process the option name and value */
		if (pg_strcasecmp(opt_name, "include") == 0)
		{
			/*
			 * An include directive isn't a variable and should be processed
			 * immediately.
			 */
			unsigned int save_ConfigFileLineno = ConfigFileLineno;

			if (!ParseConfigFile(opt_value, config_file,
								 depth + 1, context, elevel,
								 head_p, tail_p))
			{
				pfree(opt_name);
				pfree(opt_value);
				OK = false;
				goto cleanup_exit;
			}
			yy_switch_to_buffer(lex_buffer);
			ConfigFileLineno = save_ConfigFileLineno;
			pfree(opt_name);
			pfree(opt_value);
		}
		else if (pg_strcasecmp(opt_name, "custom_variable_classes") == 0)
		{
			/*
			 * This variable must be processed first as it controls
			 * the validity of other variables; so apply immediately.
			 */
			if (!set_config_option(opt_name, opt_value, context,
								   PGC_S_FILE, false, true))
			{
				pfree(opt_name);
				pfree(opt_value);

				/* We assume the error message was logged already. */
				OK = false;
				goto cleanup_exit;
			}
		}

		if (pg_strcasecmp(opt_name, "include") != 0)
		{
			/* append to list */
			struct name_value_pair *item;

			item = palloc(sizeof *item);
			item->name = opt_name;
			item->value = opt_value;
			item->next = NULL;
			if (*head_p == NULL)
				*head_p = item;
			else
				(*tail_p)->next = item;
			*tail_p = item;
		}

		/* break out of loop if read EOF, else loop for next line */
		if (token == 0)
			break;
	}

	/* successful completion of parsing */
	goto cleanup_exit;

 parse_error:
	if (token == GUC_EOL || token == 0)
		ereport(elevel,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("syntax error in file \"%s\" line %u, near end of line",
						config_file, ConfigFileLineno - 1)));
	else
		ereport(elevel,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("syntax error in file \"%s\" line %u, near token \"%s\"", 
						config_file, ConfigFileLineno, yytext)));
	OK = false;

cleanup_exit:
	yy_delete_buffer(lex_buffer);
	FreeFile(fp);
	return OK;
}


/*
 * Free a list of name/value pairs, including the names and the values
 */
static void
free_name_value_list(struct name_value_pair *list)
{
	struct name_value_pair *item;

	item = list;
	while (item)
	{
		struct name_value_pair *next = item->next;

		pfree(item->name);
		pfree(item->value);
		pfree(item);
		item = next;
	}
}


/*
 *		scanstr
 *
 * Strip the quotes surrounding the given string, and collapse any embedded
 * '' sequences and backslash escapes.
 *
 * the string returned is palloc'd and should eventually be pfree'd by the
 * caller.
 */
static char *
GUC_scanstr(const char *s)
{
	char	   *newStr;
	int			len,
				i,
				j;

	Assert(s != NULL && s[0] == '\'');
	len = strlen(s);
	Assert(len >= 2);
	Assert(s[len-1] == '\'');

	/* Skip the leading quote; we'll handle the trailing quote below */
	s++, len--;

	/* Since len still includes trailing quote, this is enough space */
	newStr = palloc(len);

	for (i = 0, j = 0; i < len; i++)
	{
		if (s[i] == '\\')
		{
			i++;
			switch (s[i])
			{
				case 'b':
					newStr[j] = '\b';
					break;
				case 'f':
					newStr[j] = '\f';
					break;
				case 'n':
					newStr[j] = '\n';
					break;
				case 'r':
					newStr[j] = '\r';
					break;
				case 't':
					newStr[j] = '\t';
					break;
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
					{
						int			k;
						long		octVal = 0;

						for (k = 0;
							 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
							 k++)
							octVal = (octVal << 3) + (s[i + k] - '0');
						i += k - 1;
						newStr[j] = ((char) octVal);
					}
					break;
				default:
					newStr[j] = s[i];
					break;
			}					/* switch */
		}
		else if (s[i] == '\'' && s[i+1] == '\'')
		{
			/* doubled quote becomes just one quote */
			newStr[j] = s[++i];
		}
		else
			newStr[j] = s[i];
		j++;
	}

	/* We copied the ending quote to newStr, so replace with \0 */
	Assert(j > 0 && j <= len);
	newStr[--j] = '\0';

	return newStr;
}
