summaryrefslogtreecommitdiff
path: root/src/interfaces/ecpg/preproc/ecpg.c
blob: 483a7c52fccc1dcd8ef54332b816a1e66d4fafa0 (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
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
/* (C) Michael Meskes <[email protected]> Feb 5th, 1998 */
/* Placed under the same copyright as PostgresSQL */

#include "postgres.h"

#include <stdio.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#else
#include <unistd.h>
#endif
#include <stdlib.h>
#if defined(HAVE_STRING_H)
#include <string.h>
#else
#include <strings.h>
#endif

#include "extern.h"

static void
usage(char *progname)
{
	fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
	fprintf(stderr, "Usage: %s: [-v] [-d] [ -o outout file name] file1 [file2] ...\n", progname);
}

int
main(int argc, char *const argv[])
{
	char		c,
				out_option = 0;
	int			fnr;

	while ((c = getopt(argc, argv, "vdo:")) != EOF)
	{
		switch (c)
		{
			case 'o':
				yyout = fopen(optarg, "w");
				if (yyout == NULL)
					perror(optarg);
				else
					out_option = 1;
				break;
			case 'd':
				debugging = 1;
				break;
			case 'v':
			default:
				usage(argv[0]);
		}
	}

	if (optind >= argc)			/* no files specified */
		usage(argv[0]);
	else
	{
		/* after the options there must not be anything but filenames */
		for (fnr = optind; fnr < argc; fnr++)
		{
			char	   *filename,
					   *ptr2ext;
			int			ext = 0;

			filename = mm_alloc(strlen(argv[fnr]) + 4);

			strcpy(filename, argv[fnr]);

			ptr2ext = strrchr(filename, '.');
			/* no extension or extension not equal .pgc */
			if (ptr2ext == NULL || strcmp(ptr2ext, ".pgc") != 0)
			{
				if (ptr2ext == NULL)
					ext = 1;	/* we need this information a while later */
				ptr2ext = filename + strlen(filename);
				ptr2ext[0] = '.';
			}

			/* make extension = .c */
			ptr2ext[1] = 'c';
			ptr2ext[2] = '\0';

			if (out_option == 0)/* calculate the output name */
			{
				yyout = fopen(filename, "w");
				if (yyout == NULL)
				{
					perror(filename);
					free(filename);
					continue;
				}
			}

			if (ext == 1)
			{
				/* no extension => add .pgc */
				ptr2ext = strrchr(filename, '.');
				ptr2ext[1] = 'p';
				ptr2ext[2] = 'g';
				ptr2ext[3] = 'c';
				ptr2ext[4] = '\0';
				input_filename = filename;
			}
			else
				input_filename = argv[fnr];
			yyin = fopen(input_filename, "r");
			if (yyin == NULL)
				perror(argv[fnr]);
			else
			{
				/* initialize lex */
				lex_init();

				/* we need two includes */
				fprintf(yyout, "/* Processed by ecpg (%d.%d.%d) */\n/*These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);

				/* and parse the source */
				yyparse();

				fclose(yyin);
				if (out_option == 0)
					fclose(yyout);
			}

			free(filename);
		}
	}
	return (0);
}