Skip to content

Commit 68be23e

Browse files
committed
allows exppp output filename (or stdout) to be specified with -o
messages on stdout will corrupt a schema being printed to stdout, so replace all printf( ... ) and fprintf( stdout, ... ) with fprintf( stderr, ... )
1 parent 89c428b commit 68be23e

File tree

16 files changed

+133
-112
lines changed

16 files changed

+133
-112
lines changed

include/exppp/exppp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern SC_EXPPP_EXPORT bool exppp_reference_info; /**< if true, add co
1212
extern SC_EXPPP_EXPORT bool exppp_preserve_comments; /**< if true, preserve comments where possible */
1313
extern SC_EXPPP_EXPORT char * exppp_output_filename; /**< force output filename */
1414
extern SC_EXPPP_EXPORT bool exppp_output_filename_reset; /**< if true, force output filename */
15+
extern SC_EXPPP_EXPORT bool exppp_print_to_stdout; /**< if true, print to stdout */
1516

1617
SC_EXPPP_EXPORT void EXPRESSout( Express e );
1718

include/express/express.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ extern SC_EXPRESS_EXPORT int EXPRESS_fail PROTO( ( Express ) );
259259
extern SC_EXPRESS_EXPORT int EXPRESS_succeed PROTO( ( Express ) );
260260
extern void EXPRESSinit_init PROTO( ( void ) );
261261
extern SC_EXPRESS_EXPORT void EXPRESSusage( int _exit ); /**< exit unless _exit is non-zero */
262+
extern SC_EXPRESS_EXPORT const char * get_filename(); /**< return pointer to file name given on the command line */
262263
extern SC_EXPRESS_EXPORT void build_complex( Express );
263264

264265
#endif /*EXPRESS_H*/

src/exppp/exppp-main.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
static void
77
exppp_usage() {
8-
fprintf( stderr, "usage: %s [-a|A] [-v] [-d #] [-p <object_type>] {-w|-i <warning>} express_file\n", EXPRESSprogram_name );
8+
fprintf( stderr, "usage: %s [-a|A] [-v] [-d #] [-p <object_type>] {-w|-i <warning>} [-o [file|--]] express_file\n", EXPRESSprogram_name );
99
fprintf( stderr, "where\t-a or -A causes output to be alphabetized\n" );
10-
fprintf( stderr, "where\t-v produces a version description\n" );
10+
fprintf( stderr, "\t-v produces a version description\n" );
11+
fprintf( stderr, "\t-o specifies the name of the output file (-- for stdout)\n" );
1112
fprintf( stderr, "\t-d turns on debugging (\"-d 0\" describes this further\n" );
1213
fprintf( stderr, "\t-p turns on printing when processing certain objects (see below)\n" );
1314
fprintf( stderr, "\t-w warning enable\n" );
@@ -30,19 +31,25 @@ exppp_usage() {
3031
}
3132

3233
int Handle_Exppp_Args( int i, char * arg ) {
33-
( void ) arg; /* quell unused param warning */
34-
35-
if( ( ( char )i == 'a' ) || ( ( char )i == 'A' ) ) {
34+
if( tolower( ( char )i ) == 'a' ) {
3635
exppp_alphabetize = true;
37-
} else {
38-
exppp_alphabetize = false;
36+
return 0;
37+
} else if( tolower( (char)i ) == 'o' ) {
38+
if( !strcmp( "--", arg ) ) {
39+
exppp_print_to_stdout = true;
40+
return 0;
41+
}
42+
exppp_output_filename_reset = false;
43+
exppp_output_filename = arg;
44+
return 0;
3945
}
40-
return 0;
46+
return 1;
4147
}
4248

4349
void EXPRESSinit_init( void ) {
50+
exppp_alphabetize = false;
4451
EXPRESSbackend = EXPRESSout;
4552
ERRORusage_function = exppp_usage;
46-
strcat( EXPRESSgetopt_options, "aA" );
53+
strcat( EXPRESSgetopt_options, "aAo:" );
4754
EXPRESSgetopt = Handle_Exppp_Args;
4855
}

src/exppp/exppp.c

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ char * expheader[] = {
9393
0
9494
};
9595

96-
bool exppp_output_filename_reset; /* if true, force output filename */
97-
96+
bool exppp_output_filename_reset = true; /* if true, force output filename */
97+
bool exppp_print_to_stdout = false;
9898
bool exppp_alphabetize = false;
9999

100100
bool exppp_terse = false;
@@ -254,59 +254,65 @@ char * SCHEMAout( Schema s ) {
254254
#define PP_SMALL_BUF_SZ 80
255255
char buf[PP_SMALL_BUF_SZ];
256256
char * p;
257-
FILE * f;
258257
int level = 0;
259258
char ** hp;
260259
bool described = false;
261-
262-
if( exppp_output_filename_reset ) {
263-
exppp_output_filename = 0;
264-
}
265-
266-
if( exppp_output_filename ) {
267-
strcpy( filename, exppp_output_filename );
260+
if( exppp_print_to_stdout ) {
261+
exppp_fp = stdout;
268262
} else {
269-
/* when there is only a single file, allow user to find */
270-
/* out what it is */
271-
exppp_output_filename = filename;
272-
exppp_output_filename_reset = true;
263+
FILE * f;
264+
if( exppp_output_filename_reset ) {
265+
exppp_output_filename = 0;
266+
}
273267

274-
/* since we have to generate a filename, make sure we don't */
275-
/* overwrite a valuable file */
268+
if( exppp_output_filename ) {
269+
if( !strcmp( get_filename(), exppp_output_filename ) ) {
270+
fprintf(stderr, "Error: input filename and output filename are the same (%s)", exppp_output_filename );
271+
exit( EXIT_FAILURE );
272+
}
273+
strcpy( filename, exppp_output_filename );
274+
} else {
275+
/* when there is only a single file, allow user to find */
276+
/* out what it is */
277+
exppp_output_filename = filename;
278+
exppp_output_filename_reset = true;
279+
280+
/* since we have to generate a filename, make sure we don't */
281+
/* overwrite a valuable file */
276282

277-
sprintf( filename, "%s.exp", s->symbol.name );
283+
sprintf( filename, "%s.exp", s->symbol.name );
278284

279-
if( 0 != ( f = fopen( filename, "r" ) ) ) {
280-
fgets( buf, PP_SMALL_BUF_SZ, f );
281-
if( 0 != ( p = strchr( buf, '\n' ) ) ) {
282-
*p = '\0';
285+
if( 0 != ( f = fopen( filename, "r" ) ) ) {
286+
fgets( buf, PP_SMALL_BUF_SZ, f );
287+
if( 0 != ( p = strchr( buf, '\n' ) ) ) {
288+
*p = '\0';
289+
}
290+
if( streq( buf, expheader[0] ) ) {
291+
unlink( filename );
292+
} else {
293+
fprintf( stderr, "%s: %s already exists and appears to be hand-written\n",
294+
EXPRESSprogram_name, filename );
295+
/* strcat(bp,".pp");*/
296+
strcat( filename, ".pp" );
297+
fprintf( stderr, "%s: writing schema file %s instead\n",
298+
EXPRESSprogram_name, filename );
299+
described = true;
300+
}
283301
}
284-
if( streq( buf, expheader[0] ) ) {
285-
unlink( filename );
286-
} else {
287-
fprintf( stderr, "%s: %s already exists and appears to be hand-written\n",
288-
EXPRESSprogram_name, filename );
289-
/* strcat(bp,".pp");*/
290-
strcat( filename, ".pp" );
291-
fprintf( stderr, "%s: writing schema file %s instead\n",
292-
EXPRESSprogram_name, filename );
293-
described = true;
302+
if( f ) {
303+
fclose( f );
294304
}
295305
}
296-
if( f ) {
297-
fclose( f );
298-
}
299-
}
300-
error_sym.filename = filename;
306+
error_sym.filename = filename;
301307

302-
if( !described && !exppp_terse ) {
303-
fprintf( stdout, "%s: writing schema file %s\n", EXPRESSprogram_name, filename );
304-
}
305-
if( !( exppp_fp = f = fopen( filename, "w" ) ) ) {
306-
ERRORreport( ERROR_file_unwriteable, filename, strerror( errno ) );
307-
return 0;
308+
if( !described && !exppp_terse ) {
309+
fprintf( stdout, "%s: writing schema file %s\n", EXPRESSprogram_name, filename );
310+
}
311+
if( !( exppp_fp = f = fopen( filename, "w" ) ) ) {
312+
ERRORreport( ERROR_file_unwriteable, filename, strerror( errno ) );
313+
return 0;
314+
}
308315
}
309-
310316
error_sym.line = 1;
311317
/* print our header - generated by exppp, don't edit, etc */
312318
for( hp = expheader; *hp; hp++ ) {

src/express/dict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void DICTprint( Dictionary dict ) {
5151
HASHlistinit( dict, &de );
5252

5353
while( 0 != ( e = ( HASHlist( &de ) ) ) ) {
54-
printf( "key <%s> data <%s> line <%d> <\"%c\" %s> <%s>\n",
54+
fprintf( stderr, "key <%s> data <%s> line <%d> <\"%c\" %s> <%s>\n",
5555
e->key, e->data, e->symbol->line, e->type,
5656
OBJget_type( e->type ), e->symbol->filename );
5757
}

src/express/expparse.y

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ entity_header ::= TOK_ENTITY TOK_IDENTIFIER(A).
839839
Entity e = ENTITYcreate(A.symbol);
840840

841841
if (print_objects_while_running & OBJ_ENTITY_BITS) {
842-
fprintf(stdout, "parse: %s (entity)\n", A.symbol->name);
842+
fprintf( stderr, "parse: %s (entity)\n", A.symbol->name);
843843
}
844844

845845
PUSH_SCOPE(e, A.symbol, OBJ_ENTITY);
@@ -1213,7 +1213,7 @@ fh_push_scope ::= TOK_IDENTIFIER(A).
12131213
Function f = ALGcreate(OBJ_FUNCTION);
12141214
tag_count = 0;
12151215
if (print_objects_while_running & OBJ_FUNCTION_BITS) {
1216-
fprintf(stdout, "parse: %s (function)\n", A.symbol->name);
1216+
fprintf( stderr, "parse: %s (function)\n", A.symbol->name);
12171217
}
12181218
PUSH_SCOPE(f, A.symbol, OBJ_FUNCTION);
12191219
}
@@ -1802,7 +1802,7 @@ ph_push_scope ::= TOK_IDENTIFIER(A).
18021802
tag_count = 0;
18031803

18041804
if (print_objects_while_running & OBJ_PROCEDURE_BITS) {
1805-
fprintf(stdout, "parse: %s (procedure)\n", A.symbol->name);
1805+
fprintf( stderr, "parse: %s (procedure)\n", A.symbol->name);
18061806
}
18071807

18081808
PUSH_SCOPE(p, A.symbol, OBJ_PROCEDURE);
@@ -1998,7 +1998,7 @@ rh_start(A) ::= TOK_RULE rh_get_line(B) TOK_IDENTIFIER(C) TOK_FOR
19981998
Rule r = ALGcreate(OBJ_RULE);
19991999

20002000
if (print_objects_while_running & OBJ_RULE_BITS) {
2001-
fprintf(stdout, "parse: %s (rule)\n", C.symbol->name);
2001+
fprintf( stderr, "parse: %s (rule)\n", C.symbol->name);
20022002
}
20032003

20042004
PUSH_SCOPE(r, C.symbol, OBJ_RULE);
@@ -2034,7 +2034,7 @@ schema_header ::= TOK_SCHEMA TOK_IDENTIFIER(A) semicolon.
20342034
Schema schema = ( Schema ) DICTlookup(CURRENT_SCOPE->symbol_table, A.symbol->name);
20352035

20362036
if (print_objects_while_running & OBJ_SCHEMA_BITS) {
2037-
fprintf(stdout, "parse: %s (schema)\n", A.symbol->name);
2037+
fprintf( stderr, "parse: %s (schema)\n", A.symbol->name);
20382038
}
20392039

20402040
if (EXPRESSignore_duplicate_schemas && schema) {

src/express/expr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ Type EXPresolve_op_dot( Expression expr, Scope scope ) {
391391
resolved_all( expr );
392392
return( item->type );
393393
} else {
394-
printf( "EXPresolved_op_dot: attribute not an attribute?\n" );
394+
fprintf( stderr, "EXPresolved_op_dot: attribute not an attribute?\n" );
395395
ERRORabort( 0 );
396396
}
397397

@@ -414,7 +414,7 @@ Type EXPresolve_op_dot( Expression expr, Scope scope ) {
414414
return( Type_Bad );
415415
}
416416
if( DICT_type != OBJ_VARIABLE ) {
417-
printf( "EXPresolved_op_dot: attribute not an attribute?\n" );
417+
fprintf( stderr, "EXPresolved_op_dot: attribute not an attribute?\n" );
418418
ERRORabort( 0 );
419419
}
420420

src/express/express.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,11 @@ static Express PARSERrun( char * filename, FILE * fp ) {
628628
parseData.scanner = scanner;
629629

630630
if( print_objects_while_running & OBJ_PASS_BITS ) {
631-
fprintf( stdout, "parse (pass %d)\n", EXPRESSpass );
631+
fprintf( stderr, "parse (pass %d)\n", EXPRESSpass );
632632
}
633633

634634
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
635-
fprintf( stdout, "parse: %s (schema file)\n", filename );
635+
fprintf( stderr, "parse: %s (schema file)\n", filename );
636636
}
637637

638638
SCAN_lex_init( filename, fp );
@@ -775,7 +775,7 @@ Schema EXPRESSfind_schema( Dictionary modeldict, char * name ) {
775775
char lower[MAX_SCHEMA_FILENAME_SIZE]; /* avoid lowerizing original */
776776

777777
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
778-
fprintf( stdout, "pass %d: %s (schema reference)\n",
778+
fprintf( stderr, "pass %d: %s (schema reference)\n",
779779
EXPRESSpass, name );
780780
}
781781

@@ -794,15 +794,15 @@ Schema EXPRESSfind_schema( Dictionary modeldict, char * name ) {
794794
LISTdo( EXPRESS_path, dir, Dir * )
795795
sprintf( dir->leaf, "%s.exp", lower );
796796
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
797-
fprintf( stdout, "pass %d: %s (schema file?)\n",
797+
fprintf( stderr, "pass %d: %s (schema file?)\n",
798798
EXPRESSpass, dir->full );
799799
}
800800
fp = fopen( dir->full, "r" );
801801
if( fp ) {
802802
Express express;
803803

804804
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
805-
fprintf( stdout, "pass %d: %s (schema file found)\n",
805+
fprintf( stderr, "pass %d: %s (schema file found)\n",
806806
EXPRESSpass, dir->full );
807807
}
808808

@@ -818,7 +818,7 @@ Schema EXPRESSfind_schema( Dictionary modeldict, char * name ) {
818818
return 0;
819819
} else {
820820
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
821-
fprintf( stdout, "pass %d: %s (schema file not found), errno = %d\n", EXPRESSpass, dir->full, errno );
821+
fprintf( stderr, "pass %d: %s (schema file not found), errno = %d\n", EXPRESSpass, dir->full, errno );
822822
}
823823
}
824824
LISTod
@@ -890,7 +890,7 @@ void EXPRESSresolve( Express model ) {
890890

891891
EXPRESSpass++;
892892
if( print_objects_while_running & OBJ_PASS_BITS ) {
893-
fprintf( stdout, "pass %d: resolving schema references\n", EXPRESSpass );
893+
fprintf( stderr, "pass %d: resolving schema references\n", EXPRESSpass );
894894
}
895895

896896
/* connect the real schemas to all the rename clauses */
@@ -902,7 +902,7 @@ void EXPRESSresolve( Express model ) {
902902

903903
LISTdo( PARSEnew_schemas, print_schema, Schema ) {
904904
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
905-
fprintf( stdout, "pass %d: %s (schema)\n",
905+
fprintf( stderr, "pass %d: %s (schema)\n",
906906
EXPRESSpass, print_schema->symbol.name );
907907
}
908908

@@ -924,7 +924,7 @@ void EXPRESSresolve( Express model ) {
924924

925925
EXPRESSpass++;
926926
if( print_objects_while_running & OBJ_PASS_BITS ) {
927-
fprintf( stdout, "pass %d: resolving objects references to other schemas\n", EXPRESSpass );
927+
fprintf( stderr, "pass %d: resolving objects references to other schemas\n", EXPRESSpass );
928928
}
929929

930930
/* connect the object in each rename clause to the real object */
@@ -935,7 +935,7 @@ void EXPRESSresolve( Express model ) {
935935
}
936936

937937
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
938-
fprintf( stdout, "pass %d: %s (schema)\n",
938+
fprintf( stderr, "pass %d: %s (schema)\n",
939939
EXPRESSpass, schema->symbol.name );
940940
}
941941

@@ -960,7 +960,7 @@ void EXPRESSresolve( Express model ) {
960960
/* defined types */
961961
EXPRESSpass++;
962962
if( print_objects_while_running & OBJ_PASS_BITS ) {
963-
fprintf( stdout, "pass %d: resolving sub and supertypes\n", EXPRESSpass );
963+
fprintf( stderr, "pass %d: resolving sub and supertypes\n", EXPRESSpass );
964964
}
965965

966966
DICTdo_type_init( model->symbol_table, &de, OBJ_SCHEMA );
@@ -977,7 +977,7 @@ void EXPRESSresolve( Express model ) {
977977
/* resolve types */
978978
EXPRESSpass++;
979979
if( print_objects_while_running & OBJ_PASS_BITS ) {
980-
fprintf( stdout, "pass %d: resolving types\n", EXPRESSpass );
980+
fprintf( stderr, "pass %d: resolving types\n", EXPRESSpass );
981981
}
982982

983983
SCOPEresolve_types( model );
@@ -991,7 +991,7 @@ void EXPRESSresolve( Express model ) {
991991
/* type resolution */
992992
EXPRESSpass++;
993993
if( print_objects_while_running & OBJ_PASS_BITS ) {
994-
fprintf( stdout, "pass %d: resolving implied USE's\n", EXPRESSpass );
994+
fprintf( stderr, "pass %d: resolving implied USE's\n", EXPRESSpass );
995995
}
996996

997997
DICTdo_type_init( model->symbol_table, &de, OBJ_SCHEMA );
@@ -1001,7 +1001,7 @@ void EXPRESSresolve( Express model ) {
10011001
}
10021002

10031003
if( print_objects_while_running & OBJ_SCHEMA_BITS ) {
1004-
fprintf( stdout, "pass %d: %s (schema)\n",
1004+
fprintf( stderr, "pass %d: %s (schema)\n",
10051005
EXPRESSpass, schema->symbol.name );
10061006
}
10071007

@@ -1022,7 +1022,7 @@ void EXPRESSresolve( Express model ) {
10221022

10231023
EXPRESSpass++;
10241024
if( print_objects_while_running & OBJ_PASS_BITS ) {
1025-
fprintf( stdout, "pass %d: resolving expressions and statements\n", EXPRESSpass );
1025+
fprintf( stderr, "pass %d: resolving expressions and statements\n", EXPRESSpass );
10261026
}
10271027

10281028
SCOPEresolve_expressions_statements( model );

0 commit comments

Comments
 (0)