]> granicus.if.org Git - postgresql/blob - src/backend/utils/misc/guc-file.l
Make all our flex and bison files use %option prefix or %name-prefix
[postgresql] / src / backend / utils / misc / guc-file.l
1 /* -*-pgsql-c-*- */
2 /*
3  * Scanner for the configuration file
4  *
5  * Copyright (c) 2000-2006, PostgreSQL Global Development Group
6  *
7  * $PostgreSQL: pgsql/src/backend/utils/misc/guc-file.l,v 1.37 2006/03/07 01:03:12 tgl Exp $
8  */
9
10 %{
11
12 #include "postgres.h"
13
14 #include <ctype.h>
15 #include <unistd.h>
16
17 #include "miscadmin.h"
18 #include "storage/fd.h"
19 #include "utils/guc.h"
20
21
22 /* Avoid exit() on fatal scanner errors (a bit ugly -- see yy_fatal_error) */
23 #undef fprintf
24 #define fprintf(file, fmt, msg)  ereport(ERROR, (errmsg_internal("%s", msg)))
25
26 enum {
27         GUC_ID = 1,
28         GUC_STRING = 2,
29         GUC_INTEGER = 3,
30         GUC_REAL = 4,
31         GUC_EQUALS = 5,
32         GUC_UNQUOTED_STRING = 6,
33         GUC_QUALIFIED_ID = 7,
34         GUC_EOL = 99,
35         GUC_ERROR = 100
36 };
37
38 struct name_value_pair
39 {
40         char       *name;
41         char       *value;
42         struct name_value_pair *next;
43 };
44
45 static unsigned int ConfigFileLineno;
46
47 /* flex fails to supply a prototype for yylex, so provide one */
48 int GUC_yylex(void);
49
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);
56
57 %}
58
59 %option 8bit
60 %option never-interactive
61 %option nodefault
62 %option nounput
63 %option noyywrap
64 %option prefix="GUC_yy"
65
66
67 SIGN            ("-"|"+")
68 DIGIT           [0-9]
69 HEXDIGIT        [0-9a-fA-F]
70
71 INTEGER         {SIGN}?({DIGIT}+|0x{HEXDIGIT}+)
72
73 EXPONENT        [Ee]{SIGN}?{DIGIT}+
74 REAL            {SIGN}?{DIGIT}*"."{DIGIT}*{EXPONENT}?
75
76 LETTER          [A-Za-z_\200-\377]
77 LETTER_OR_DIGIT [A-Za-z_0-9\200-\377]
78
79 ID              {LETTER}{LETTER_OR_DIGIT}*
80 QUALIFIED_ID    {ID}"."{ID}
81
82 UNQUOTED_STRING {LETTER}({LETTER_OR_DIGIT}|[-._:/])*
83 STRING          \'([^'\\\n]|\\.|\'\')*\'
84
85 %%
86
87 \n              ConfigFileLineno++; return GUC_EOL;
88 [ \t\r]+        /* eat whitespace */
89 #.*             /* eat comment (.* matches anything until newline) */
90
91 {ID}            return GUC_ID;
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;
97 =               return GUC_EQUALS;
98
99 .               return GUC_ERROR;
100
101 %%
102
103
104
105 /*
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.
111  */
112 void
113 ProcessConfigFile(GucContext context)
114 {
115         int                     elevel;
116         struct name_value_pair *item, *head, *tail;
117
118         Assert(context == PGC_POSTMASTER || context == PGC_SIGHUP);
119
120         if (context == PGC_SIGHUP)
121         {
122                 /*
123                  * To avoid cluttering the log, only the postmaster bleats loudly
124                  * about problems with the config file.
125                  */
126                 elevel = IsUnderPostmaster ? DEBUG2 : LOG;
127         }
128         else
129                 elevel = ERROR;
130
131         head = tail = NULL;
132
133         if (!ParseConfigFile(ConfigFileName, NULL,
134                                                  0, context, elevel,
135                                                  &head, &tail))
136                 goto cleanup_list;
137
138         /* Check if all options are valid */
139         for (item = head; item; item = item->next)
140         {
141                 if (!set_config_option(item->name, item->value, context,
142                                                            PGC_S_FILE, false, false))
143                         goto cleanup_list;
144         }
145
146         /* If we got here all the options checked out okay, so apply them. */
147         for (item = head; item; item = item->next)
148         {
149                 set_config_option(item->name, item->value, context,
150                                                   PGC_S_FILE, false, true);
151         }
152
153  cleanup_list:
154         free_name_value_list(head);
155 }
156
157
158 /*
159  * Read and parse a single configuration file.  This function recurses
160  * to handle "include" directives.
161  *
162  * Input parameters:
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()
169  * Output parameters:
170  *      head_p, tail_p: head and tail of linked list of name/value pairs
171  *
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).
175  *
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.
179  *
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.
185  */
186 static bool
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)
191 {
192         bool            OK = true;
193         char            abs_path[MAXPGPATH];
194         FILE       *fp;
195         YY_BUFFER_STATE lex_buffer;
196         int                     token;
197
198         /*
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.
202          */
203         if (depth > 10)
204         {
205                 ereport(elevel,
206                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
207                                  errmsg("could not open configuration file \"%s\": maximum nesting depth exceeded",
208                                                 config_file)));
209                 return false;
210         }
211
212         /*
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.
215          */
216         if (!is_absolute_path(config_file))
217         {
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;
224         }
225
226         fp = AllocateFile(config_file, "r");
227         if (!fp)
228         {
229                 ereport(elevel,
230                                 (errcode_for_file_access(),
231                                  errmsg("could not open configuration file \"%s\": %m",
232                                                 config_file)));
233                 return false;
234         }
235
236         /*
237          * Parse
238          */
239         lex_buffer = yy_create_buffer(fp, YY_BUF_SIZE);
240         yy_switch_to_buffer(lex_buffer);
241
242         ConfigFileLineno = 1;
243
244         /* This loop iterates once per logical line */
245         while ((token = yylex()))
246         {
247                 char       *opt_name, *opt_value;
248
249                 if (token == GUC_EOL)   /* empty or comment line */
250                         continue;
251
252                 /* first token on line is option name */
253                 if (token != GUC_ID && token != GUC_QUALIFIED_ID)
254                         goto parse_error;
255                 opt_name = pstrdup(yytext);
256
257                 /* next we have an optional equal sign; discard if present */
258                 token = yylex();
259                 if (token == GUC_EQUALS)
260                         token = yylex();
261
262                 /* now we must have the option value */
263                 if (token != GUC_ID &&
264                         token != GUC_STRING && 
265                         token != GUC_INTEGER && 
266                         token != GUC_REAL && 
267                         token != GUC_UNQUOTED_STRING)
268                         goto parse_error;
269                 if (token == GUC_STRING)        /* strip quotes and escapes */
270                         opt_value = GUC_scanstr(yytext);
271                 else
272                         opt_value = pstrdup(yytext);
273
274                 /* now we'd like an end of line, or possibly EOF */
275                 token = yylex();
276                 if (token != GUC_EOL && token != 0)
277                         goto parse_error;
278
279                 /* OK, process the option name and value */
280                 if (pg_strcasecmp(opt_name, "include") == 0)
281                 {
282                         /*
283                          * An include directive isn't a variable and should be processed
284                          * immediately.
285                          */
286                         unsigned int save_ConfigFileLineno = ConfigFileLineno;
287
288                         if (!ParseConfigFile(opt_value, config_file,
289                                                                  depth + 1, context, elevel,
290                                                                  head_p, tail_p))
291                         {
292                                 pfree(opt_name);
293                                 pfree(opt_value);
294                                 OK = false;
295                                 goto cleanup_exit;
296                         }
297                         yy_switch_to_buffer(lex_buffer);
298                         ConfigFileLineno = save_ConfigFileLineno;
299                         pfree(opt_name);
300                         pfree(opt_value);
301                 }
302                 else if (pg_strcasecmp(opt_name, "custom_variable_classes") == 0)
303                 {
304                         /*
305                          * This variable must be processed first as it controls
306                          * the validity of other variables; so apply immediately.
307                          */
308                         if (!set_config_option(opt_name, opt_value, context,
309                                                                    PGC_S_FILE, false, true))
310                         {
311                                 pfree(opt_name);
312                                 pfree(opt_value);
313                                 /* we assume error message was logged already */
314                                 OK = false;
315                                 goto cleanup_exit;
316                         }
317                         pfree(opt_name);
318                         pfree(opt_value);
319                 }
320                 else
321                 {
322                         /* append to list */
323                         struct name_value_pair *item;
324
325                         item = palloc(sizeof *item);
326                         item->name = opt_name;
327                         item->value = opt_value;
328                         item->next = NULL;
329                         if (*head_p == NULL)
330                                 *head_p = item;
331                         else
332                                 (*tail_p)->next = item;
333                         *tail_p = item;
334                 }
335
336                 /* break out of loop if read EOF, else loop for next line */
337                 if (token == 0)
338                         break;
339         }
340
341         /* successful completion of parsing */
342         goto cleanup_exit;
343
344  parse_error:
345         if (token == GUC_EOL || token == 0)
346                 ereport(elevel,
347                                 (errcode(ERRCODE_SYNTAX_ERROR),
348                                  errmsg("syntax error in file \"%s\" line %u, near end of line",
349                                                 config_file, ConfigFileLineno - 1)));
350         else
351                 ereport(elevel,
352                                 (errcode(ERRCODE_SYNTAX_ERROR),
353                                  errmsg("syntax error in file \"%s\" line %u, near token \"%s\"", 
354                                                 config_file, ConfigFileLineno, yytext)));
355         OK = false;
356
357 cleanup_exit:
358         yy_delete_buffer(lex_buffer);
359         FreeFile(fp);
360         return OK;
361 }
362
363
364 /*
365  * Free a list of name/value pairs, including the names and the values
366  */
367 static void
368 free_name_value_list(struct name_value_pair *list)
369 {
370         struct name_value_pair *item;
371
372         item = list;
373         while (item)
374         {
375                 struct name_value_pair *next = item->next;
376
377                 pfree(item->name);
378                 pfree(item->value);
379                 pfree(item);
380                 item = next;
381         }
382 }
383
384
385 /*
386  *              scanstr
387  *
388  * Strip the quotes surrounding the given string, and collapse any embedded
389  * '' sequences and backslash escapes.
390  *
391  * the string returned is palloc'd and should eventually be pfree'd by the
392  * caller.
393  */
394 static char *
395 GUC_scanstr(const char *s)
396 {
397         char       *newStr;
398         int                     len,
399                                 i,
400                                 j;
401
402         Assert(s != NULL && s[0] == '\'');
403         len = strlen(s);
404         Assert(len >= 2);
405         Assert(s[len-1] == '\'');
406
407         /* Skip the leading quote; we'll handle the trailing quote below */
408         s++, len--;
409
410         /* Since len still includes trailing quote, this is enough space */
411         newStr = palloc(len);
412
413         for (i = 0, j = 0; i < len; i++)
414         {
415                 if (s[i] == '\\')
416                 {
417                         i++;
418                         switch (s[i])
419                         {
420                                 case 'b':
421                                         newStr[j] = '\b';
422                                         break;
423                                 case 'f':
424                                         newStr[j] = '\f';
425                                         break;
426                                 case 'n':
427                                         newStr[j] = '\n';
428                                         break;
429                                 case 'r':
430                                         newStr[j] = '\r';
431                                         break;
432                                 case 't':
433                                         newStr[j] = '\t';
434                                         break;
435                                 case '0':
436                                 case '1':
437                                 case '2':
438                                 case '3':
439                                 case '4':
440                                 case '5':
441                                 case '6':
442                                 case '7':
443                                         {
444                                                 int                     k;
445                                                 long            octVal = 0;
446
447                                                 for (k = 0;
448                                                          s[i + k] >= '0' && s[i + k] <= '7' && k < 3;
449                                                          k++)
450                                                         octVal = (octVal << 3) + (s[i + k] - '0');
451                                                 i += k - 1;
452                                                 newStr[j] = ((char) octVal);
453                                         }
454                                         break;
455                                 default:
456                                         newStr[j] = s[i];
457                                         break;
458                         }                                       /* switch */
459                 }
460                 else if (s[i] == '\'' && s[i+1] == '\'')
461                 {
462                         /* doubled quote becomes just one quote */
463                         newStr[j] = s[++i];
464                 }
465                 else
466                         newStr[j] = s[i];
467                 j++;
468         }
469
470         /* We copied the ending quote to newStr, so replace with \0 */
471         Assert(j > 0 && j <= len);
472         newStr[--j] = '\0';
473
474         return newStr;
475 }