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