mirror of
https://github.com/postgres/postgres.git
synced 2025-07-12 21:01:52 +03:00
Here's my next patch to bring ecpg to version 1.1. It now correctly
handles all transaction commands and the exec sql include command. Michael Meskes
This commit is contained in:
@ -9,6 +9,8 @@
|
||||
#include <getopt.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
extern int optind;
|
||||
extern char *optarg;
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#if defined(HAVE_STRING_H)
|
||||
@ -19,19 +21,37 @@
|
||||
|
||||
#include "extern.h"
|
||||
|
||||
struct _include_path *include_paths;
|
||||
|
||||
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);
|
||||
fprintf(stderr, "Usage: %s: [-v] [-d] [-I include path] [ -o output file name] file1 [file2] ...\n", progname);
|
||||
}
|
||||
|
||||
static void
|
||||
add_include_path(char * path)
|
||||
{
|
||||
struct _include_path *ip = include_paths;
|
||||
|
||||
include_paths = mm_alloc(sizeof(struct _include_path));
|
||||
include_paths->path = path;
|
||||
include_paths->next = ip;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *const argv[])
|
||||
{
|
||||
int fnr, c, out_option = 0;
|
||||
struct _include_path *ip;
|
||||
|
||||
add_include_path("/usr/include");
|
||||
add_include_path(INCLUDE_PATH);
|
||||
add_include_path("/usr/local/include");
|
||||
add_include_path(".");
|
||||
|
||||
while ((c = getopt(argc, argv, "vdo:")) != EOF)
|
||||
while ((c = getopt(argc, argv, "vdo:I:")) != EOF)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
@ -45,64 +65,71 @@ main(int argc, char *const argv[])
|
||||
case 'd':
|
||||
debugging = 1;
|
||||
break;
|
||||
case 'I':
|
||||
add_include_path(optarg);
|
||||
break;
|
||||
case 'v':
|
||||
fprintf(stderr, "ecpg - the postgresql preprocessor, version: %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION, PATCHLEVEL);
|
||||
fprintf(stderr, "exec sql include ... search starts here:\n");
|
||||
for (ip = include_paths; ip != NULL; ip = ip->next)
|
||||
fprintf(stderr, " %s\n", ip->path);
|
||||
fprintf(stderr, "End of search list.\n");
|
||||
return (0);
|
||||
default:
|
||||
usage(argv[0]);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
|
||||
if (optind >= argc) /* no files specified */
|
||||
{
|
||||
usage(argv[0]);
|
||||
return(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* after the options there must not be anything but filenames */
|
||||
for (fnr = optind; fnr < argc; fnr++)
|
||||
{
|
||||
char *filename,
|
||||
*ptr2ext;
|
||||
int ext = 0;
|
||||
char *output_filename, *ptr2ext;
|
||||
|
||||
filename = mm_alloc(strlen(argv[fnr]) + 4);
|
||||
input_filename = mm_alloc(strlen(argv[fnr]) + 5);
|
||||
|
||||
strcpy(filename, argv[fnr]);
|
||||
strcpy(input_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)
|
||||
ptr2ext = strrchr(input_filename, '.');
|
||||
/* no extension? */
|
||||
if (ptr2ext == NULL)
|
||||
{
|
||||
ptr2ext = input_filename + strlen(input_filename);
|
||||
|
||||
/* no extension => add .pgc */
|
||||
ptr2ext = strrchr(filename, '.');
|
||||
ptr2ext[0] = '.';
|
||||
ptr2ext[1] = 'p';
|
||||
ptr2ext[2] = 'g';
|
||||
ptr2ext[3] = 'c';
|
||||
ptr2ext[4] = '\0';
|
||||
input_filename = filename;
|
||||
}
|
||||
else
|
||||
input_filename = argv[fnr];
|
||||
|
||||
if (out_option == 0)/* calculate the output name */
|
||||
{
|
||||
output_filename = strdup(input_filename);
|
||||
|
||||
ptr2ext = strrchr(output_filename, '.');
|
||||
/* make extension = .c */
|
||||
ptr2ext[1] = 'c';
|
||||
ptr2ext[2] = '\0';
|
||||
|
||||
yyout = fopen(output_filename, "w");
|
||||
if (yyout == NULL)
|
||||
{
|
||||
perror(output_filename);
|
||||
free(output_filename);
|
||||
free(input_filename);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
yyin = fopen(input_filename, "r");
|
||||
if (yyin == NULL)
|
||||
perror(argv[fnr]);
|
||||
@ -117,12 +144,14 @@ main(int argc, char *const argv[])
|
||||
/* and parse the source */
|
||||
yyparse();
|
||||
|
||||
fclose(yyin);
|
||||
if (yyin != NULL)
|
||||
fclose(yyin);
|
||||
if (out_option == 0)
|
||||
fclose(yyout);
|
||||
}
|
||||
|
||||
free(filename);
|
||||
free(output_filename);
|
||||
free(input_filename);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
|
Reference in New Issue
Block a user