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.36 2006/03/05 15:58:49 momjian 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
70 INTEGER {SIGN}?({DIGIT}+|0x{HEXDIGIT}+)
72 EXPONENT [Ee]{SIGN}?{DIGIT}+
73 REAL {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
75 LETTER [A-Za-z_\200-\377]
76 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
78 ID {LETTER}{LETTER_OR_DIGIT}*
79 QUALIFIED_ID {ID}"."{ID}
81 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
82 STRING \'([^'\\\n]|\\.|\'\')*\'
86 \n ConfigFileLineno++; return GUC_EOL;
87 [ \t\r]+ /* eat whitespace */
88 #.* /* eat comment (.* matches anything until newline) */
91 {QUALIFIED_ID} return GUC_QUALIFIED_ID;
92 {STRING} return GUC_STRING;
93 {UNQUOTED_STRING} return GUC_UNQUOTED_STRING;
94 {INTEGER} return GUC_INTEGER;
95 {REAL} return GUC_REAL;
105 * Exported function to read and process the configuration file. The
106 * parameter indicates in what context the file is being read --- either
107 * postmaster startup (including standalone-backend startup) or SIGHUP.
108 * All options mentioned in the configuration file are set to new values.
109 * If an error occurs, no values will be changed.
112 ProcessConfigFile(GucContext context)
115 struct name_value_pair *item, *head, *tail;
117 Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
119 if (context == PGC_SIGHUP)
122 * To avoid cluttering the log, only the postmaster bleats loudly
123 * about problems with the config file.
125 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
132 if (!ParseConfigFile(ConfigFileName, NULL,
137 /* Check if all options are valid */
138 for (item = head; item; item = item->next)
140 if (!set_config_option(item->name, item->value, context,
141 PGC_S_FILE, false, false))
145 /* If we got here all the options checked out okay, so apply them. */
146 for (item = head; item; item = item->next)
148 set_config_option(item->name, item->value, context,
149 PGC_S_FILE, false, true);
153 free_name_value_list(head);
158 * Read and parse a single configuration file. This function recurses
159 * to handle "include" directives.
162 * config_file: absolute or relative path of file to read
163 * calling_file: absolute path of file containing the "include" directive,
164 * or NULL at outer level (config_file must be absolute at outer level)
165 * depth: recursion depth (used only to prevent infinite recursion)
166 * context: GucContext passed to ProcessConfigFile()
167 * elevel: error logging level determined by ProcessConfigFile()
169 * head_p, tail_p: head and tail of linked list of name/value pairs
171 * *head_p and *tail_p must be initialized to NULL before calling the outer
172 * recursion level. On exit, they contain a list of name-value pairs read
173 * from the input file(s).
175 * Returns TRUE if successful, FALSE if an error occurred. The error has
176 * already been ereport'd, it is only necessary for the caller to clean up
177 * its own state and release the name/value pairs list.
179 * Note: if elevel >= ERROR then an error will not return control to the
180 * caller, and internal state such as open files will not be cleaned up.
181 * This case occurs only during postmaster or standalone-backend startup,
182 * where an error will lead to immediate process exit anyway; so there is
183 * no point in contorting the code so it can clean up nicely.
186 ParseConfigFile(const char *config_file, const char *calling_file,
187 int depth, GucContext context, int elevel,
188 struct name_value_pair **head_p,
189 struct name_value_pair **tail_p)
192 char abs_path[MAXPGPATH];
194 YY_BUFFER_STATE lex_buffer;
198 * Reject too-deep include nesting depth. This is just a safety check
199 * to avoid dumping core due to stack overflow if an include file loops
200 * back to itself. The maximum nesting depth is pretty arbitrary.
205 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
206 errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
212 * If config_file is a relative path, convert to absolute. We consider
213 * it to be relative to the directory holding the calling file.
215 if (!is_absolute_path(config_file))
217 Assert(calling_file != NULL);
218 StrNCpy(abs_path, calling_file, MAXPGPATH);
219 get_parent_directory(abs_path);
220 join_path_components(abs_path, abs_path, config_file);
221 canonicalize_path(abs_path);
222 config_file = abs_path;
225 fp = AllocateFile(config_file, "r");
229 (errcode_for_file_access(),
230 errmsg("could not open configuration file \"%s\": %m",
238 lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
239 yy_switch_to_buffer(lex_buffer);
241 ConfigFileLineno = 1;
243 /* This loop iterates once per logical line */
244 while ((token = yylex()))
246 char *opt_name, *opt_value;
248 if (token == GUC_EOL) /* empty or comment line */
251 /* first token on line is option name */
252 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
254 opt_name = pstrdup(yytext);
256 /* next we have an optional equal sign; discard if present */
258 if (token == GUC_EQUALS)
261 /* now we must have the option value */
262 if (token != GUC_ID &&
263 token != GUC_STRING &&
264 token != GUC_INTEGER &&
266 token != GUC_UNQUOTED_STRING)
268 if (token == GUC_STRING) /* strip quotes and escapes */
269 opt_value = GUC_scanstr(yytext);
271 opt_value = pstrdup(yytext);
273 /* now we'd like an end of line, or possibly EOF */
275 if (token != GUC_EOL && token != 0)
278 /* OK, process the option name and value */
279 if (pg_strcasecmp(opt_name, "include") == 0)
282 * An include directive isn't a variable and should be processed
285 unsigned int save_ConfigFileLineno = ConfigFileLineno;
287 if (!ParseConfigFile(opt_value, config_file,
288 depth + 1, context, elevel,
296 yy_switch_to_buffer(lex_buffer);
297 ConfigFileLineno = save_ConfigFileLineno;
301 else if (pg_strcasecmp(opt_name, "custom_variable_classes") == 0)
304 * This variable must be processed first as it controls
305 * the validity of other variables; so apply immediately.
307 if (!set_config_option(opt_name, opt_value, context,
308 PGC_S_FILE, false, true))
312 /* we assume error message was logged already */
322 struct name_value_pair *item;
324 item = palloc(sizeof *item);
325 item->name = opt_name;
326 item->value = opt_value;
331 (*tail_p)->next = item;
335 /* break out of loop if read EOF, else loop for next line */
340 /* successful completion of parsing */
344 if (token == GUC_EOL || token == 0)
346 (errcode(ERRCODE_SYNTAX_ERROR),
347 errmsg("syntax error in file \"%s\" line %u, near end of line",
348 config_file, ConfigFileLineno - 1)));
351 (errcode(ERRCODE_SYNTAX_ERROR),
352 errmsg("syntax error in file \"%s\" line %u, near token \"%s\"",
353 config_file, ConfigFileLineno, yytext)));
357 yy_delete_buffer(lex_buffer);
364 * Free a list of name/value pairs, including the names and the values
367 free_name_value_list(struct name_value_pair *list)
369 struct name_value_pair *item;
374 struct name_value_pair *next = item->next;
387 * Strip the quotes surrounding the given string, and collapse any embedded
388 * '' sequences and backslash escapes.
390 * the string returned is palloc'd and should eventually be pfree'd by the
394 GUC_scanstr(const char *s)
401 Assert(s != NULL && s[0] == '\'');
404 Assert(s[len-1] == '\'');
406 /* Skip the leading quote; we'll handle the trailing quote below */
409 /* Since len still includes trailing quote, this is enough space */
410 newStr = palloc(len);
412 for (i = 0, j = 0; i < len; i++)
447 s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
449 octVal = (octVal << 3) + (s[i + k] - '0');
451 newStr[j] = ((char) octVal);
459 else if (s[i] == '\'' && s[i+1] == '\'')
461 /* doubled quote becomes just one quote */
469 /* We copied the ending quote to newStr, so replace with \0 */
470 Assert(j > 0 && j <= len);