2 * Copyright (C) 2002 Mark Hessling <M.Hessling@qut.edu.au>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * Mark Hessling M.Hessling@qut.edu.au http://www.lightlink.com/hessling/
25 #if defined(HAVE_CONFIG_H)
28 # include "configur.h"
42 # define PATH_MAX 1024
45 void DestroyInstore( char *strptr
)
47 #if defined(REXXFREEMEMORY)
48 RexxFreeMemory( strptr
);
49 #elif defined(WIN32) && (defined(USE_OREXX) || defined(USE_WINREXX) || defined(USE_QUERCUS))
51 #elif defined(USE_OS2REXX)
58 void usage( char *prog
)
60 fprintf( stderr
, "Usage:\n" );
61 fprintf( stderr
, " To create a file of tokenised code...\n" );
62 fprintf( stderr
, " %s -t srcfile tokenfile\n", prog
);
63 fprintf( stderr
, " To execute a file of tokenised code...\n" );
64 fprintf( stderr
, " %s tokenfile [Rexx program arguments]\n", prog
);
67 int Tokenise( char *infile
, char *outfile
)
76 CHAR retbuf
[RXAUTOBUFLEN
];
78 infp
= fopen( infile
, "rb" );
81 perror( "Unable to open input file");
84 fseek( infp
, 0, SEEK_END
);
87 instore
[0].strptr
= malloc( size
);
88 fread( instore
[0].strptr
, size
, 1, infp
);
89 instore
[0].strlength
= size
;
90 instore
[1].strptr
= NULL
;
91 instore
[1].strlength
= 0;
94 MAKERXSTRING( ArgList
, "//T", 3 );
96 MAKERXSTRING( retstr
, retbuf
, sizeof( retbuf
) );
107 && instore
[1].strptr
)
109 outfp
= fopen( outfile
, "wb" );
112 perror( "Unable to create output file");
113 DestroyInstore( instore
[1].strptr
);
116 if ( fwrite( instore
[1].strptr
, 1, instore
[1].strlength
, outfp
) != instore
[1].strlength
)
118 perror( "Unable to write output file");
119 DestroyInstore( instore
[1].strptr
);
123 DestroyInstore( instore
[1].strptr
);
128 int Execute( char *tokfile
, char *srcfile
, int ArgCount
, PRXSTRING ArgList
)
136 CHAR retbuf
[RXAUTOBUFLEN
];
140 srcfp
= fopen( srcfile
, "rb" );
143 perror( "Error opening source file" );
146 fseek( srcfp
, 0, SEEK_END
);
147 size
= ftell( srcfp
);
149 instore
[0].strptr
= malloc( size
);
150 if (instore
[0].strptr
== NULL
)
153 perror( "Error allocating memory for instore[0].strptr" );
156 fread( instore
[0].strptr
, size
, 1, srcfp
);
157 instore
[0].strlength
= size
;
162 instore
[0].strptr
= NULL
;
163 instore
[0].strlength
= 0;
165 tokfp
= fopen( tokfile
, "rb" );
168 perror( "Error opening token file" );
169 if (instore
[0].strptr
)
170 free( instore
[0].strptr
);
173 fseek( tokfp
, 0, SEEK_END
);
174 size
= ftell( tokfp
);
176 instore
[1].strptr
= malloc( size
);
177 if ( instore
[1].strptr
== NULL
)
179 perror( "Error allocating memory for instore[1].strptr" );
181 if (instore
[0].strptr
)
182 free( instore
[0].strptr
);
185 fread( instore
[1].strptr
, size
, 1, tokfp
);
186 instore
[1].strlength
= size
;
189 strcpy( retbuf
, "" );
190 MAKERXSTRING( retstr
, retbuf
, sizeof( retbuf
) );
191 rc
= RexxStart((ArgCount
) ? 1 : 0,
193 (srcfile
) ? srcfile
: "<no_source>",
200 if (instore
[1].strptr
)
201 free( instore
[1].strptr
);
202 if (instore
[0].strptr
)
203 free( instore
[0].strptr
);
207 int main( int argc
, char *argv
[])
219 if ( strcmp( argv
[1], "-t" ) == 0 )
222 rc
= Tokenise( argv
[2], argv
[3] );
225 (void)fprintf( stderr
, "Must supply both srcfile and tokenfile as parameters when tokenising.\n" );
232 * Build an argument string, if any.
239 for ( i
= 2; i
< argc
; i
++ )
241 len
+= strlen( (char *)argv
[i
] );
243 if ( ( ArgList
.strptr
= (char *)malloc( len
+ 1 + ArgCount
) ) == NULL
)
245 (void)fprintf( stderr
, "%s: out of memory\n", argv
[0] );
248 strcpy( ArgList
.strptr
, "" );
249 for ( i
= 2; i
< argc
; i
++ )
251 strcat( ArgList
.strptr
, argv
[i
] );
253 strcat( ArgList
.strptr
, " " );
255 ArgList
.strlength
= ArgCount
+ len
- 1;
259 ArgList
.strptr
= NULL
;
260 ArgList
.strlength
= 0;
262 rc
= Execute( argv
[1], NULL
, ArgCount
, &ArgList
);