1998-02-05 16:46:43 +01:00
|
|
|
/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
|
|
|
|
/* (C) Michael Meskes <meskes@debian.org> Feb 5th, 1998 */
|
|
|
|
/* Placed under the same copyright as PostgresSQL */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1998-02-06 14:32:34 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
1998-02-05 16:46:43 +01:00
|
|
|
|
|
|
|
extern void lex_init(void);
|
1998-02-10 17:44:17 +01:00
|
|
|
extern FILE *yyin, *yyout;
|
|
|
|
extern char * input_filename;
|
|
|
|
extern int yyparse(void);
|
1998-02-06 14:32:34 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(char *progname)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s: [ -o outout file name] file1 [file2] ...\n", progname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *const argv[])
|
1998-02-05 16:46:43 +01:00
|
|
|
{
|
1998-02-10 17:44:17 +01:00
|
|
|
char c,
|
|
|
|
out_option = 0;
|
1998-02-06 14:32:34 +01:00
|
|
|
int fnr;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, "o:")) != EOF)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'o':
|
|
|
|
yyout = fopen(optarg, "w");
|
|
|
|
if (yyout == NULL)
|
|
|
|
perror(optarg);
|
|
|
|
else
|
|
|
|
out_option = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* after the options there must not be anything but filenames */
|
|
|
|
for (fnr = optind; fnr < argc; fnr++)
|
|
|
|
{
|
|
|
|
char *filename,
|
|
|
|
*ptr2ext;
|
|
|
|
|
|
|
|
filename = malloc(strlen(argv[fnr]) + 2);
|
|
|
|
if (filename == NULL)
|
|
|
|
{
|
|
|
|
perror("malloc");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(filename, argv[fnr]);
|
|
|
|
|
|
|
|
ptr2ext = strrchr(filename, '.');
|
|
|
|
/* no extension or extension not equal .pgc */
|
1998-02-10 17:44:17 +01:00
|
|
|
if (ptr2ext == NULL || strcmp(ptr2ext, ".pgc") != 0)
|
|
|
|
{
|
1998-02-06 14:32:34 +01:00
|
|
|
ptr2ext = filename + strlen(filename);
|
|
|
|
ptr2ext[0] = '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make extension = .c */
|
|
|
|
ptr2ext[1] = 'c';
|
|
|
|
ptr2ext[2] = '\0';
|
|
|
|
|
1998-02-10 17:44:17 +01:00
|
|
|
if (out_option == 0) /* calculate the output name */
|
1998-02-06 14:32:34 +01:00
|
|
|
{
|
|
|
|
yyout = fopen(filename, "w");
|
1998-02-10 17:44:17 +01:00
|
|
|
if (yyout == NULL)
|
|
|
|
{
|
1998-02-06 14:32:34 +01:00
|
|
|
perror(filename);
|
|
|
|
free(filename);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-02-10 17:44:17 +01:00
|
|
|
yyin = fopen(input_filename = argv[fnr], "r");
|
1998-02-06 14:32:34 +01:00
|
|
|
if (yyin == NULL)
|
|
|
|
{
|
|
|
|
perror(argv[fnr]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* initialize lex */
|
|
|
|
lex_init();
|
|
|
|
|
|
|
|
/* we need two includes everytime */
|
|
|
|
fprintf(yyout, "/* These two include files are added by the preprocessor */\n#include <ecpgtype.h>\n#include <ecpglib.h>\n");
|
|
|
|
|
|
|
|
/* and parse the source */
|
|
|
|
yyparse();
|
|
|
|
|
|
|
|
fclose(yyin);
|
|
|
|
if (out_option == 0)
|
1998-02-10 17:44:17 +01:00
|
|
|
fclose(yyout);
|
1998-02-06 14:32:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(filename);
|
|
|
|
}
|
|
|
|
return (0);
|
1998-02-05 16:46:43 +01:00
|
|
|
}
|