3 * Scanner for the configuration file
5 * Copyright (c) 2000-2006, PostgreSQL Global Development Group
7 * $PostgreSQL: pgsql/src/backend/utils/misc/guc-file.l,v 1.37 2006/03/07 01:03:12 tgl Exp $
17 #include "miscadmin.h"
18 #include "storage/fd.h"
19 #include "utils/guc.h"
22 /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
24 #define fprintf(file, fmt, msg) ereport(ERROR, (errmsg_internal("%s", msg)))
32 GUC_UNQUOTED_STRING = 6,
38 struct name_value_pair
42 struct name_value_pair *next;
45 static unsigned int ConfigFileLineno;
47 /* flex fails to supply a prototype for yylex, so provide one */
50 static bool ParseConfigFile(const char *config_file, const char *calling_file,
51 int depth, GucContext context, int elevel,
52 struct name_value_pair **head_p,
53 struct name_value_pair **tail_p);
54 static void free_name_value_list(struct name_value_pair * list);
55 static char *GUC_scanstr(const char *s);
60 %option never-interactive
64 %option prefix="GUC_yy"
71 INTEGER {SIGN}?({DIGIT}+|0x{HEXDIGIT}+)
73 EXPONENT [Ee]{SIGN}?{DIGIT}+
74 REAL {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
76 LETTER [A-Za-z_\200-\377]
77 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
79 ID {LETTER}{LETTER_OR_DIGIT}*
80 QUALIFIED_ID {ID}"."{ID}
82 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
83 STRING \'([^'\\\n]|\\.|\'\')*\'
87 \n ConfigFileLineno++; return GUC_EOL;
88 [ \t\r]+ /* eat whitespace */
89 #.* /* eat comment (.* matches anything until newline) */
92 {QUALIFIED_ID} return GUC_QUALIFIED_ID;
93 {STRING} return GUC_STRING;
94 {UNQUOTED_STRING} return GUC_UNQUOTED_STRING;
95 {INTEGER} return GUC_INTEGER;
96 {REAL} return GUC_REAL;
106 * Exported function to read and process the configuration file. The
107 * parameter indicates in what context the file is being read --- either
108 * postmaster startup (including standalone-backend startup) or SIGHUP.
109 * All options mentioned in the configuration file are set to new values.
110 * If an error occurs, no values will be changed.
113 ProcessConfigFile(GucContext context)
116 struct name_value_pair *item, *head, *tail;
118 Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
120 if (context == PGC_SIGHUP)
123 * To avoid cluttering the log, only the postmaster bleats loudly
124 * about problems with the config file.
126 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
133 if (!ParseConfigFile(ConfigFileName, NULL,
138 /* Check if all options are valid */
139 for (item = head; item; item = item->next)
141 if (!set_config_option(item->name, item->value, context,
142 PGC_S_FILE, false, false))
146 /* If we got here all the options checked out okay, so apply them. */
147 for (item = head; item; item = item->next)
149 set_config_option(item->name, item->value, context,
150 PGC_S_FILE, false, true);
154 free_name_value_list(head);
159 * Read and parse a single configuration file. This function recurses
160 * to handle "include" directives.
163 * config_file: absolute or relative path of file to read
164 * calling_file: absolute path of file containing the "include" directive,
165 * or NULL at outer level (config_file must be absolute at outer level)
166 * depth: recursion depth (used only to prevent infinite recursion)
167 * context: GucContext passed to ProcessConfigFile()
168 * elevel: error logging level determined by ProcessConfigFile()
170 * head_p, tail_p: head and tail of linked list of name/value pairs
172 * *head_p and *tail_p must be initialized to NULL before calling the outer
173 * recursion level. On exit, they contain a list of name-value pairs read
174 * from the input file(s).
176 * Returns TRUE if successful, FALSE if an error occurred. The error has
177 * already been ereport'd, it is only necessary for the caller to clean up
178 * its own state and release the name/value pairs list.
180 * Note: if elevel >= ERROR then an error will not return control to the
181 * caller, and internal state such as open files will not be cleaned up.
182 * This case occurs only during postmaster or standalone-backend startup,
183 * where an error will lead to immediate process exit anyway; so there is
184 * no point in contorting the code so it can clean up nicely.
187 ParseConfigFile(const char *config_file, const char *calling_file,
188 int depth, GucContext context, int elevel,
189 struct name_value_pair **head_p,
190 struct name_value_pair **tail_p)
193 char abs_path[MAXPGPATH];
195 YY_BUFFER_STATE lex_buffer;
199 * Reject too-deep include nesting depth. This is just a safety check
200 * to avoid dumping core due to stack overflow if an include file loops
201 * back to itself. The maximum nesting depth is pretty arbitrary.
206 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
207 errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
213 * If config_file is a relative path, convert to absolute. We consider
214 * it to be relative to the directory holding the calling file.
216 if (!is_absolute_path(config_file))
218 Assert(calling_file != NULL);
219 StrNCpy(abs_path, calling_file, MAXPGPATH);
220 get_parent_directory(abs_path);
221 join_path_components(abs_path, abs_path, config_file);
222 canonicalize_path(abs_path);
223 config_file = abs_path;
226 fp = AllocateFile(config_file, "r");
230 (errcode_for_file_access(),
231 errmsg("could not open configuration file \"%s\": %m",
239 lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
240 yy_switch_to_buffer(lex_buffer);
242 ConfigFileLineno = 1;
244 /* This loop iterates once per logical line */
245 while ((token = yylex()))
247 char *opt_name, *opt_value;
249 if (token == GUC_EOL) /* empty or comment line */
252 /* first token on line is option name */
253 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
255 opt_name = pstrdup(yytext);
257 /* next we have an optional equal sign; discard if present */
259 if (token == GUC_EQUALS)
262 /* now we must have the option value */
263 if (token != GUC_ID &&
264 token != GUC_STRING &&
265 token != GUC_INTEGER &&
267 token != GUC_UNQUOTED_STRING)
269 if (token == GUC_STRING) /* strip quotes and escapes */
270 opt_value = GUC_scanstr(yytext);
272 opt_value = pstrdup(yytext);
274 /* now we'd like an end of line, or possibly EOF */
276 if (token != GUC_EOL && token != 0)
279 /* OK, process the option name and value */
280 if (pg_strcasecmp(opt_name, "include") == 0)
283 * An include directive isn't a variable and should be processed
286 unsigned int save_ConfigFileLineno = ConfigFileLineno;
288 if (!ParseConfigFile(opt_value, config_file,
289 depth + 1, context, elevel,
297 yy_switch_to_buffer(lex_buffer);
298 ConfigFileLineno = save_ConfigFileLineno;
302 else if (pg_strcasecmp(opt_name, "custom_variable_classes") == 0)
305 * This variable must be processed first as it controls
306 * the validity of other variables; so apply immediately.
308 if (!set_config_option(opt_name, opt_value, context,
309 PGC_S_FILE, false, true))
313 /* we assume error message was logged already */
323 struct name_value_pair *item;
325 item = palloc(sizeof *item);
326 item->name = opt_name;
327 item->value = opt_value;
332 (*tail_p)->next = item;
336 /* break out of loop if read EOF, else loop for next line */
341 /* successful completion of parsing */
345 if (token == GUC_EOL || token == 0)
347 (errcode(ERRCODE_SYNTAX_ERROR),
348 errmsg("syntax error in file \"%s\" line %u, near end of line",
349 config_file, ConfigFileLineno - 1)));
352 (errcode(ERRCODE_SYNTAX_ERROR),
353 errmsg("syntax error in file \"%s\" line %u, near token \"%s\"",
354 config_file, ConfigFileLineno, yytext)));
358 yy_delete_buffer(lex_buffer);
365 * Free a list of name/value pairs, including the names and the values
368 free_name_value_list(struct name_value_pair *list)
370 struct name_value_pair *item;
375 struct name_value_pair *next = item->next;
388 * Strip the quotes surrounding the given string, and collapse any embedded
389 * '' sequences and backslash escapes.
391 * the string returned is palloc'd and should eventually be pfree'd by the
395 GUC_scanstr(const char *s)
402 Assert(s != NULL && s[0] == '\'');
405 Assert(s[len-1] == '\'');
407 /* Skip the leading quote; we'll handle the trailing quote below */
410 /* Since len still includes trailing quote, this is enough space */
411 newStr = palloc(len);
413 for (i = 0, j = 0; i < len; i++)
448 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
450 octVal = (octVal << 3) + (s[i + k] - '0');
452 newStr[j] = ((char) octVal);
460 else if (s[i] == '\'' && s[i+1] == '\'')
462 /* doubled quote becomes just one quote */
470 /* We copied the ending quote to newStr, so replace with \0 */
471 Assert(j > 0 && j <= len);