ecpg.c 7.38 KB
Newer Older
1
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.49 2001/09/19 14:09:32 meskes Exp $ */
2

3
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
Michael Meskes's avatar
Michael Meskes committed
4
/* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
5
/* Placed under the same license as PostgresSQL */
6

7 8
#include "postgres_fe.h"

9
#include <unistd.h>
10

11
#ifdef HAVE_GETOPT_H
12
#include "getopt.h"
13
#endif
14

15 16 17
extern int	optind;
extern char *optarg;

18
#include "extern.h"
19

20
int			ret_value = 0,
21
			autocommit = 0;
22
struct _include_path *include_paths = NULL;
23
struct cursor *cur = NULL;
24
struct typedefs *types = NULL;
25 26
struct _defines *defines = NULL;

27
static void
28
help(const char *progname)
29
{
30 31
	printf("%s is the PostgreSQL embedded SQL preprocessor for C programs.\n\n",
		   progname);
32
	/* printf is a macro some places; don't #ifdef inside its arguments */
33
#ifdef YYDEBUG
34 35 36
	printf("Usage:\n"
		   "  %s [-d] [-I DIRECTORY] [-o OUTFILE] [-t] file1 [file2...]\n\n",
		   progname);
37
#else
38 39 40
	printf("Usage:\n"
		   "  %s [-I DIRECTORY] [-o OUTFILE] [-t] file1 [file2...]\n\n",
		   progname);
41 42
#endif
	printf("Options:\n");
43
#ifdef YYDEBUG
44
	printf("  -d                   generate parser debug output\n");
45
#endif
46 47 48 49 50 51
	printf("  -I DIRECTORY         search DIRECTORY for include files\n");
	printf("  -o OUTFILE           write result to OUTFILE\n");
	printf("  -t                   turn on autocommit of transactions\n");
	printf("\nIf no output file is specified, the name is formed by adding .c\n"
		   "to the input file name, after stripping off .pgc if present.\n");
	printf("\nReport bugs to <pgsql-bugs@postgresql.org>.\n");
52 53 54
}

static void
55
add_include_path(char *path)
56 57
{
	struct _include_path *ip = include_paths;
58 59 60 61

	include_paths = mm_alloc(sizeof(struct _include_path));
	include_paths->path = path;
	include_paths->next = ip;
62 63
}

Michael Meskes's avatar
Michael Meskes committed
64 65 66 67 68 69 70 71 72 73 74 75
static void
add_preprocessor_define(char *define)
{
	struct _defines *pd = defines;

	defines = mm_alloc(sizeof(struct _defines));
	defines->old = strdup(define);
	defines->new = strdup("");
	defines->pertinent = true;
	defines->next = pd;
}

76 77
int
main(int argc, char *const argv[])
78
{
79 80
	int			fnr,
				c,
Michael Meskes's avatar
Michael Meskes committed
81
				verbose = false,
82 83
				out_option = 0;
	struct _include_path *ip;
84
	char	   *progname;
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	if (!strrchr(argv[0], '/'))
		progname = argv[0];
	else
		progname = strrchr(argv[0], '/') + 1;

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help")==0 || strcmp(argv[1], "-?")==0)
		{
			help(progname);
			exit(0);
		}
		else if (strcmp(argv[1], "--version")==0)
		{
			printf("ecpg (PostgreSQL %s) %d.%d.%d\n", PG_VERSION,
				   MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
			exit(0);
		}
	}
105

106
	add_include_path("/usr/include");
107 108 109 110
	add_include_path(INCLUDE_PATH);
	add_include_path("/usr/local/include");
	add_include_path(".");

111
	while ((c = getopt(argc, argv, "vo:I:tD:d")) != EOF)
112 113 114 115
	{
		switch (c)
		{
			case 'o':
116
				yyout = fopen(optarg, PG_BINARY_W);
117 118 119 120 121
				if (yyout == NULL)
					perror(optarg);
				else
					out_option = 1;
				break;
122 123
			case 'I':
				add_include_path(optarg);
124 125
				break;
			case 't':
Michael Meskes's avatar
Michael Meskes committed
126
				autocommit = 1;
127
				break;
128
			case 'v':
Michael Meskes's avatar
Michael Meskes committed
129
				verbose = true;
130
				break;
Michael Meskes's avatar
Michael Meskes committed
131
			case 'D':
132
				/* XXX not documented */
133 134
				add_preprocessor_define(optarg);
				break;
135
			case 'd':
136
#ifdef YYDEBUG
137
				yydebug = 1;
138 139 140
#else
				fprintf(stderr, "%s: parser debug support (-d) not available\n",
						progname);
141
#endif
142
				break;
143
			default:
144
				fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
145
				return ILLEGAL_OPTION;
146 147 148
		}
	}

Michael Meskes's avatar
Michael Meskes committed
149 150
	if (verbose)
	{
151 152
		fprintf(stderr, "%s, the PostgreSQL embedded C preprocessor, version %d.%d.%d\n",
				progname, MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
Michael Meskes's avatar
Michael Meskes committed
153 154 155
		fprintf(stderr, "exec sql include ... search starts here:\n");
		for (ip = include_paths; ip != NULL; ip = ip->next)
			fprintf(stderr, " %s\n", ip->path);
156 157
		fprintf(stderr, "end of search list\n");
		return 0;
Michael Meskes's avatar
Michael Meskes committed
158
	}
159

160
	if (optind >= argc)			/* no files specified */
161
	{
162 163
		fprintf(stderr, "%s: no input files specified\n", progname);
		fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
164
		return (ILLEGAL_OPTION);
165
	}
166
	else
167
	{
168 169
		/* after the options there must not be anything but filenames */
		for (fnr = optind; fnr < argc; fnr++)
170
		{
171 172
			char	   *output_filename = NULL,
					   *ptr2ext;
173

174
			input_filename = mm_alloc(strlen(argv[fnr]) + 5);
175

176
			strcpy(input_filename, argv[fnr]);
177

Michael Meskes's avatar
Michael Meskes committed
178 179 180
			/* take care of relative paths */
			ptr2ext = strrchr(input_filename, '/');
			ptr2ext = (ptr2ext ? strrchr(ptr2ext, '.') : strrchr(input_filename, '.'));
181

182 183
			/* no extension? */
			if (ptr2ext == NULL)
184
			{
185
				ptr2ext = input_filename + strlen(input_filename);
186

187
				/* no extension => add .pgc */
188
				ptr2ext[0] = '.';
189 190 191 192
				ptr2ext[1] = 'p';
				ptr2ext[2] = 'g';
				ptr2ext[3] = 'c';
				ptr2ext[4] = '\0';
193
			}
194

195
			if (out_option == 0)/* calculate the output name */
196
			{
Marc G. Fournier's avatar
Marc G. Fournier committed
197
				output_filename = strdup(input_filename);
198

199 200 201 202
				ptr2ext = strrchr(output_filename, '.');
				/* make extension = .c */
				ptr2ext[1] = 'c';
				ptr2ext[2] = '\0';
203

204
				yyout = fopen(output_filename, PG_BINARY_W);
205 206
				if (yyout == NULL)
				{
207 208 209
					perror(output_filename);
					free(output_filename);
					free(input_filename);
210 211
					continue;
				}
212 213
			}

214
			yyin = fopen(input_filename, PG_BINARY_R);
215 216 217 218
			if (yyin == NULL)
				perror(argv[fnr]);
			else
			{
Marc G. Fournier's avatar
Marc G. Fournier committed
219
				struct cursor *ptr;
220
				struct _defines *defptr;
221
				struct typedefs *typeptr;
Bruce Momjian's avatar
Bruce Momjian committed
222

223
				/* remove old cursor definitions if any are still there */
224
				for (ptr = cur; ptr != NULL;)
225
				{
226
					struct cursor *this = ptr;
227 228
					struct arguments *l1,
							   *l2;
229

230
					free(ptr->command);
231
					free(ptr->connection);
232
					free(ptr->name);
233
					for (l1 = ptr->argsinsert; l1; l1 = l2)
234 235 236 237
					{
						l2 = l1->next;
						free(l1);
					}
238
					for (l1 = ptr->argsresult; l1; l1 = l2)
239 240 241 242
					{
						l2 = l1->next;
						free(l1);
					}
243 244
					ptr = ptr->next;
					free(this);
245
				}
Michael Meskes's avatar
Michael Meskes committed
246
				cur = NULL;
247

Michael Meskes's avatar
Michael Meskes committed
248
				/* remove non-pertinent old defines as well */
249 250 251 252
				while (defines && !defines->pertinent)
				{
					defptr = defines;
					defines = defines->next;
Michael Meskes's avatar
Michael Meskes committed
253

254 255 256
					free(defptr->new);
					free(defptr->old);
					free(defptr);
Michael Meskes's avatar
Michael Meskes committed
257 258
				}

259
				for (defptr = defines; defptr != NULL; defptr = defptr->next)
260
				{
261
					struct _defines *this = defptr->next;
262

263 264 265 266 267 268 269 270
					if (this && !this->pertinent)
					{
						defptr->next = this->next;

						free(this->new);
						free(this->old);
						free(this);
					}
271
				}
Bruce Momjian's avatar
Bruce Momjian committed
272

273 274 275 276
				/* and old typedefs */
				for (typeptr = types; typeptr != NULL;)
				{
					struct typedefs *this = typeptr;
277

278 279
					free(typeptr->name);
					ECPGfree_struct_member(typeptr->struct_member_list);
Michael Meskes's avatar
Michael Meskes committed
280
					free(typeptr->type);
281 282 283
					typeptr = typeptr->next;
					free(this);
				}
Michael Meskes's avatar
Michael Meskes committed
284
				types = NULL;
285

Michael Meskes's avatar
Michael Meskes committed
286 287 288 289
				/* initialize whenever structures */
				memset(&when_error, 0, sizeof(struct when));
				memset(&when_nf, 0, sizeof(struct when));
				memset(&when_warn, 0, sizeof(struct when));
290

Michael Meskes's avatar
Michael Meskes committed
291 292
				/* and structure member lists */
				memset(struct_member_list, 0, sizeof(struct_member_list));
293

Michael Meskes's avatar
Michael Meskes committed
294 295
				/* finally the actual connection */
				connection = NULL;
296

297 298
				/* initialize lex */
				lex_init();
299

300
				/* we need two includes */
Michael Meskes's avatar
Michael Meskes committed
301
				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/* These three include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n#include <ecpgerrno.h>\n#line 1 \"%s\"\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL, input_filename);
302

303 304
				/* and parse the source */
				yyparse();
305

306 307
				if (yyin != NULL)
					fclose(yyin);
308 309 310
				if (out_option == 0)
					fclose(yyout);
			}
Marc G. Fournier's avatar
Marc G. Fournier committed
311 312 313

			if (output_filename)
				free(output_filename);
314

315
			free(input_filename);
316
		}
317
	}
Michael Meskes's avatar
Michael Meskes committed
318
	return ret_value;
319
}