Build SDL as linklib.
[AROS-Contrib.git] / regina / tregina.c
blob2e4c8304147371da83ba233df9835d496d73c979
1 /*
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/
22 #include <stdlib.h>
23 #include <stdio.h>
25 #if defined(HAVE_CONFIG_H)
26 # include "config.h"
27 #else
28 # include "configur.h"
29 #endif
31 #include "rexxsaa.h"
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
37 #ifdef HAVE_STRING_H
38 # include <string.h>
39 #endif
41 #ifndef PATH_MAX
42 # define PATH_MAX 1024
43 #endif
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))
50 GlobalFree( strptr );
51 #elif defined(USE_OS2REXX)
52 DosFreeMem( strptr );
53 #else
54 free( strptr );
55 #endif
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 )
69 RXSTRING instore[2];
70 RXSTRING ArgList;
71 FILE *infp;
72 FILE *outfp;
73 short rexxrc=0;
74 int rc, size;
75 RXSTRING retstr;
76 CHAR retbuf[RXAUTOBUFLEN];
78 infp = fopen( infile, "rb" );
79 if ( infp == NULL )
81 perror( "Unable to open input file");
82 exit(1);
84 fseek( infp, 0, SEEK_END );
85 size = ftell( infp );
86 rewind( infp );
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;
92 fclose(infp);
94 MAKERXSTRING( ArgList, "//T", 3 );
95 strcpy( retbuf, "" );
96 MAKERXSTRING( retstr, retbuf, sizeof( retbuf ) );
97 rc = RexxStart(1,
98 &ArgList,
99 infile,
100 instore,
101 "SYSTEM",
102 RXCOMMAND,
103 NULL,
104 &rexxrc,
105 &retstr);
106 if ( rc == 0
107 && instore[1].strptr)
109 outfp = fopen( outfile, "wb" );
110 if ( outfp == NULL )
112 perror( "Unable to create output file");
113 DestroyInstore( instore[1].strptr );
114 exit(1);
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 );
120 exit(1);
122 fclose( outfp );
123 DestroyInstore( instore[1].strptr );
125 return rc;
128 int Execute( char *tokfile, char *srcfile, int ArgCount, PRXSTRING ArgList )
130 RXSTRING instore[2];
131 FILE *tokfp=NULL;
132 FILE *srcfp=NULL;
133 short rexxrc=0;
134 int rc, size;
135 RXSTRING retstr;
136 CHAR retbuf[RXAUTOBUFLEN];
138 if ( srcfile )
140 srcfp = fopen( srcfile, "rb" );
141 if ( srcfp == NULL )
143 perror( "Error opening source file" );
144 return(1);
146 fseek( srcfp, 0, SEEK_END );
147 size = ftell( srcfp );
148 rewind( srcfp );
149 instore[0].strptr = malloc( size );
150 if (instore[0].strptr == NULL)
152 fclose(srcfp);
153 perror( "Error allocating memory for instore[0].strptr" );
154 return(3);
156 fread( instore[0].strptr, size, 1, srcfp );
157 instore[0].strlength = size;
158 fclose(srcfp);
160 else
162 instore[0].strptr = NULL;
163 instore[0].strlength = 0;
165 tokfp = fopen( tokfile, "rb" );
166 if ( tokfp == NULL )
168 perror( "Error opening token file" );
169 if (instore[0].strptr)
170 free( instore[0].strptr );
171 return(1);
173 fseek( tokfp, 0, SEEK_END );
174 size = ftell( tokfp );
175 rewind( tokfp );
176 instore[1].strptr = malloc( size );
177 if ( instore[1].strptr == NULL )
179 perror( "Error allocating memory for instore[1].strptr" );
180 fclose( tokfp );
181 if (instore[0].strptr)
182 free( instore[0].strptr );
183 return(2);
185 fread( instore[1].strptr, size, 1, tokfp );
186 instore[1].strlength = size;
187 fclose( tokfp );
189 strcpy( retbuf, "" );
190 MAKERXSTRING( retstr, retbuf, sizeof( retbuf ) );
191 rc = RexxStart((ArgCount) ? 1 : 0,
192 ArgList,
193 (srcfile) ? srcfile : "<no_source>",
194 instore,
195 "SYSTEM",
196 RXCOMMAND,
197 NULL,
198 &rexxrc,
199 &retstr);
200 if (instore[1].strptr)
201 free( instore[1].strptr );
202 if (instore[0].strptr)
203 free( instore[0].strptr );
204 return rc;
207 int main( int argc, char *argv[])
209 int rc;
210 int i=0;
211 long ArgCount=0;
212 RXSTRING ArgList;
214 if ( argc == 1 )
216 usage( argv[0] );
217 return(0);
219 if ( strcmp( argv[1], "-t" ) == 0 )
221 if ( argc == 4 )
222 rc = Tokenise( argv[2], argv[3] );
223 else
225 (void)fprintf( stderr, "Must supply both srcfile and tokenfile as parameters when tokenising.\n" );
226 exit( 1 );
229 else
232 * Build an argument string, if any.
234 if ( argc > 2 )
236 int len=0;
238 ArgCount = argc - 2;
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] );
246 exit( 1 );
248 strcpy( ArgList.strptr, "" );
249 for ( i = 2; i < argc; i++ )
251 strcat( ArgList.strptr, argv[i] );
252 if ( i != argc )
253 strcat( ArgList.strptr, " " );
255 ArgList.strlength = ArgCount + len - 1;
257 else
259 ArgList.strptr = NULL;
260 ArgList.strlength = 0;
262 rc = Execute( argv[1], NULL, ArgCount, &ArgList );
264 return rc;