aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/misc/guc-file.l
blob: c056af9ace0616710d3afbe194e6d9ee4cf9d5b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* -*-pgsql-c-*- */
/*
 * Scanner for the configuration file
 *
 * Copyright 2000 by PostgreSQL Global Development Group
 *
 * $Header: /cvsroot/pgsql/src/backend/utils/misc/guc-file.l,v 1.3 2000/06/04 01:44:34 petere Exp $
 */

%{

#include "postgres.h"

#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

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

#define CONFIG_FILENAME "postgresql.conf"

static unsigned ConfigFileLineno;

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

#if defined(yywrap)
#undef yywrap
#endif /* yywrap */

#define YY_USER_INIT (ConfigFileLineno = 1)
#define YY_NO_UNPUT

/* prototype, so compiler is happy with our high warnings setting */
int GUC_yylex(void);

%}

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

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

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}*
/*
 * FIXME: This string syntax is nice and all but of course the quotes
 * need to be stripped before we can make any use of the string value.
 * There is a function in parser/scansup.c that does this but it uses
 * palloc and there might be a little more magic needed to get it to
 * work right. Now there are no string options, and if there were then
 * the unquoted (`ID') tokens should still work. Of course this only
 * affects the configuration file.
 */
STRING          \'([^'\n]|\\.)*'

%%

\n              ConfigFileLineno++; return GUC_EOL;
[ \t\r]+        /* eat whitespace */
#.*$            /* eat comment */

{ID}            return GUC_ID;
{STRING}        return GUC_STRING;
{INTEGER}       return GUC_INTEGER;
{REAL}          return GUC_REAL;
=               return GUC_EQUALS;

.               return GUC_ERROR;

%%


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



/*
 * 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 *save;

		save = item->next;
		free(item->name);
		free(item->value);
		free(item);
		item = save;
	}
}


/*
 * Official function to read and process the configuration file. The
 * parameter indicates in what context the file is being read
 * (postmaster startup, backend startup, or SIGHUP). All options
 * mentioned in the configuration file are set to new values. This
 * function does not return if an error occurs. If an error occurs, no
 * values will be changed.
 */
void
ProcessConfigFile(GucContext context)
{
	int token, parse_state;
	char *opt_name, *opt_value;
	char *filename;
	struct stat stat_buf;
	struct name_value_pair *item, *head, *tail;
	int elevel;
	FILE * fp;

	Assert(context == PGC_POSTMASTER || context == PGC_BACKEND || context == PGC_SIGHUP);
	Assert(DataDir);
	elevel = (context == PGC_SIGHUP) ? DEBUG : ERROR;

	/*
	 * Open file
	 */
	filename = malloc(strlen(DataDir) + strlen(CONFIG_FILENAME) + 2);
	if (filename == NULL)
	{
		elog(elevel, "out of memory");
		return;
	}
	sprintf(filename, "%s/" CONFIG_FILENAME, DataDir);

    fp = AllocateFile(filename, "r");
    if (!fp)
    {
		free(filename);
        /* File not found is fine */
        if (errno != ENOENT)
            elog(elevel, "could not read configuration file `" CONFIG_FILENAME "': %s", strerror(errno));
		return;
    }

    /*
     * Check if the file is group or world writeable. If so, reject.
     */
    if (fstat(fileno(fp), &stat_buf) == -1)
	{
		FreeFile(fp);
		free(filename);
        elog(elevel, "could not stat configuration file `" CONFIG_FILENAME "': %s", strerror(errno));
		return;
	}

    if (stat_buf.st_mode & (S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH))
	{
		FreeFile(fp);
		free(filename);
        elog(elevel, "configuration file `" CONFIG_FILENAME "' has wrong permissions");
		return;
	}

	/*
	 * Parse
	 */
	yyin = fp;
    parse_state = 0;
	head = tail = NULL;
	opt_name = opt_value = NULL;

    while((token = yylex()))
        switch(parse_state)
        {
            case 0: /* no previous input */
                if (token == GUC_EOL) /* empty line */
                    continue;
                if (token != GUC_ID)
                    goto parse_error;
                opt_name = strdup(yytext);
				if (opt_name == NULL)
					goto out_of_memory;
                parse_state = 1;
                break;

            case 1: /* found name */
                /* ignore equals sign */
                if (token == GUC_EQUALS)
                    token = yylex();

                if (token != GUC_ID && token != GUC_STRING && token != GUC_INTEGER && token != GUC_REAL)
                    goto parse_error;
                opt_value = strdup(yytext);
				if (opt_value == NULL)
					goto out_of_memory;
                parse_state = 2;
                break;

            case 2: /* now we'd like an end of line */
				if (token != GUC_EOL)
					goto parse_error;

				/* append to list */
				item = malloc(sizeof *item);
				if (item == NULL)
					goto out_of_memory;
				item->name = opt_name;
				item->value = opt_value;
				item->next = NULL;

				if (!head)
					tail = head = item;
				else
				{
					tail->next = item;
					tail = item;
				}

                parse_state = 0;
                break;
        }

	FreeFile(fp);
	free(filename);

	/*
	 * Check if all options are valid
	 */
    for(item = head; item; item=item->next)
	{
		if (!set_config_option(item->name, item->value, context, false))
			goto cleanup_exit;
	}

    /* If we got here all the options parsed okay. */
	for(item = head; item; item=item->next)
		set_config_option(item->name, item->value, context, true);

 cleanup_exit:
	free_name_value_list(head);
	return;

 parse_error:
	FreeFile(fp);
	free(filename);
	free_name_value_list(head);
	elog(elevel, CONFIG_FILENAME ":%u: syntax error", ConfigFileLineno);
	return;

 out_of_memory:
	FreeFile(fp);
	free(filename);
	free_name_value_list(head);
	elog(elevel, "out of memory");
	return;
}



int
yywrap(void)
{
	return 1;
}