1- /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.70 2003/05/14 14:37:35 meskes Exp $ */
1+ /* $Header: /cvsroot/pgsql/src/interfaces/ecpg/preproc/ecpg.c,v 1.71 2003/05/27 14:36:00 meskes Exp $ */
22
33/* New main for ecpg, the PostgreSQL embedded SQL precompiler. */
44/* (C) Michael Meskes <meskes@postgresql.org> Feb 5th, 1998 */
@@ -137,7 +137,11 @@ main(int argc, char *const argv[])
137137switch (c )
138138{
139139case 'o' :
140- yyout = fopen (optarg ,PG_BINARY_W );
140+ if (strcmp (optarg ,"-" )== 0 )
141+ yyout = stdout ;
142+ else
143+ yyout = fopen (optarg ,PG_BINARY_W );
144+
141145if (yyout == NULL )
142146perror (optarg );
143147else
@@ -219,47 +223,62 @@ main(int argc, char *const argv[])
219223char * output_filename = NULL ,
220224* ptr2ext ;
221225
222- input_filename = mm_alloc (strlen (argv [fnr ])+ 5 );
223-
224- strcpy (input_filename ,argv [fnr ]);
226+ /* If argv[fnr] is "-" we have to read from stdin */
227+ if (strcmp (argv [fnr ],"-" )== 0 )
228+ {
229+ input_filename = mm_alloc (strlen ("stdin" )+ 1 );
230+ strcpy (input_filename ,"stdin" );
231+ yyin = stdin ;
232+ }
233+ else
234+ {
235+ input_filename = mm_alloc (strlen (argv [fnr ])+ 5 );
236+ strcpy (input_filename ,argv [fnr ]);
225237
226- /* take care of relative paths */
227- ptr2ext = last_path_separator (input_filename );
228- ptr2ext = (ptr2ext ?strrchr (ptr2ext ,'.' ) :strrchr (input_filename ,'.' ));
238+ /* take care of relative paths */
239+ ptr2ext = last_path_separator (input_filename );
240+ ptr2ext = (ptr2ext ?strrchr (ptr2ext ,'.' ) :strrchr (input_filename ,'.' ));
229241
230- /* no extension? */
231- if (ptr2ext == NULL )
232- {
233- ptr2ext = input_filename + strlen (input_filename );
234-
235- /* no extension => add .pgc */
236- ptr2ext [0 ]= '.' ;
237- ptr2ext [1 ]= 'p' ;
238- ptr2ext [2 ]= 'g' ;
239- ptr2ext [3 ]= 'c' ;
240- ptr2ext [4 ]= '\0' ;
242+ /* no extension? */
243+ if (ptr2ext == NULL )
244+ {
245+ ptr2ext = input_filename + strlen (input_filename );
246+
247+ /* no extension => add .pgc */
248+ ptr2ext [0 ]= '.' ;
249+ ptr2ext [1 ]= 'p' ;
250+ ptr2ext [2 ]= 'g' ;
251+ ptr2ext [3 ]= 'c' ;
252+ ptr2ext [4 ]= '\0' ;
253+ }
254+
255+ yyin = fopen (input_filename ,PG_BINARY_R );
241256}
242257
243258if (out_option == 0 )/* calculate the output name */
244259{
245- output_filename = strdup (input_filename );
260+ if (strcmp (input_filename ,"stdin" )== 0 )
261+ yyout = stdout ;
262+ else
263+ {
264+ output_filename = strdup (input_filename );
246265
247- ptr2ext = strrchr (output_filename ,'.' );
248- /* make extension = .c */
249- ptr2ext [1 ]= 'c' ;
250- ptr2ext [2 ]= '\0' ;
266+ ptr2ext = strrchr (output_filename ,'.' );
267+ /* make extension = .c */
268+ ptr2ext [1 ]= 'c' ;
269+ ptr2ext [2 ]= '\0' ;
251270
252- yyout = fopen (output_filename ,PG_BINARY_W );
253- if (yyout == NULL )
254- {
255- perror (output_filename );
256- free (output_filename );
257- free (input_filename );
258- continue ;
271+ yyout = fopen (output_filename ,PG_BINARY_W );
272+ if (yyout == NULL )
273+ {
274+ perror (output_filename );
275+ free (output_filename );
276+ free (input_filename );
277+ continue ;
278+ }
259279}
260280}
261281
262- yyin = fopen (input_filename ,PG_BINARY_R );
263282if (yyin == NULL )
264283perror (argv [fnr ]);
265284else
@@ -341,7 +360,7 @@ main(int argc, char *const argv[])
341360
342361/* finally the actual connection */
343362connection = NULL ;
344-
363+
345364/* initialize lex */
346365lex_init ();
347366
@@ -355,9 +374,9 @@ main(int argc, char *const argv[])
355374/* and parse the source */
356375yyparse ();
357376
358- if (yyin != NULL )
377+ if (yyin != NULL && yyin != stdin )
359378fclose (yyin );
360- if (out_option == 0 )
379+ if (out_option == 0 && yyout != stdout )
361380fclose (yyout );
362381}
363382