4 * Revision 1.1 2001/04/04 05:43:39 wang
5 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
7 * Revision 1.6 1999/11/26 13:13:47 bnv
8 * Added: Windows CE support.
9 * Changed: To use the new macros.
11 * Revision 1.5 1999/03/10 16:55:02 bnv
14 * Revision 1.4 1999/02/10 15:43:36 bnv
15 * Long file name support for Win95/98/NT
17 * Revision 1.3 1999/01/22 17:29:17 bnv
18 * Added the xxxBINARY options in the STREAM function
20 * Revision 1.2 1998/11/06 08:58:10 bnv
21 * Corrected: real numbers with mantissa zero (integer)
22 * are treated as integers
24 * Revision 1.1 1998/07/02 17:34:50 bnv
46 int file_size
; /* file size in filelist structure */
48 /* there are two types of files, std unix files and rexx files */
49 /* std unix files like old BRexx have one position pointer */
50 /* rexx files have 4 position pointers */
54 PLstr name
; /* IN STRUCTURE */
59 /* ------------------------* RxInitFiles *------------------------ */
65 file
= (struct files_st
*)
66 MALLOC( FILE_INC
* sizeof(struct files_st
), "FILE");
68 for (i
=0; i
<file_size
; i
++) {
75 LPMALLOC(file
[i
].name
);
76 Lscpy(file
[i
].name
,"<STDIN>"); file
[i
].f
= STDIN
;
80 LPMALLOC(file
[i
].name
);
81 Lscpy(file
[i
].name
,"<STDOUT>"); file
[i
].f
= STDOUT
;
85 LPMALLOC(file
[i
].name
);
86 Lscpy(file
[i
].name
,"<STDERR>"); file
[i
].f
= STDERR
;
89 #if defined(MSDOS) && !defined(__WIN32__) && !defined(_MSC_VER)
91 LPMALLOC(file
[i
].name
);
92 Lscpy(file
[i
].name
,"<STDAUX>"); file
[i
].f
= stdaux
;
96 LPMALLOC(file
[i
].name
);
97 Lscpy(file
[i
].name
,"<STDPRN>"); file
[i
].f
= stdprn
;
102 /* ------------------------* RxDoneFiles *------------------------ */
107 for (i
=0;i
<file_size
;i
++) {
108 /* is it system file? */
109 if (file
[i
].name
!= NULL
) {
110 if (LSTR(*(file
[i
].name
))[0]!='<') /* system file */
118 /* -------------------------* find_file *------------------------- */
120 find_file( const PLstr fn
)
123 #if defined(MSDOS) || defined(WCE)
127 /* search to see if it is a number */
128 if ((LTYPE(*fn
)==LSTRING_TY
) && (_Lisnum(fn
) == LINTEGER_TY
))
131 if (LTYPE(*fn
) == LINTEGER_TY
)
134 if (LTYPE(*fn
) == LREAL_TY
)
137 if (IN_RANGE(0,j
,file_size
-1))
138 if (file
[j
].name
!= NULL
) return j
;
142 #if defined(MSDOS) || defined(WCE)
143 LINITSTR(str
); Lfx(&str
,LLEN(*fn
));
146 /* Make case insensity search */
149 for (i
=0; i
<file_size
; i
++)
150 if (file
[i
].name
!= NULL
)
151 if (!Lstrcmp(&str
, file
[i
].name
)) {
157 for (i
=0; i
<file_size
; i
++)
158 if (file
[i
].name
!= NULL
)
159 if (!Lstrcmp(fn
, file
[i
].name
))
165 /* ------------------------* find_empty *------------------------- */
170 for (i
=0; i
<file_size
; i
++)
171 if (file
[i
].name
==NULL
)
175 file_size
+= FILE_INC
;
176 /* then allocate some more space */
177 file
= (struct files_st
*)
178 REALLOC( file
, file_size
* sizeof(struct files_st
));
179 for (j
=i
; j
<file_size
; j
++) {
186 /* -------------------------* open_file *------------------------- */
188 open_file( const PLstr fn
, const char *mode
)
195 LINITSTR(str
); Lfx(&str
,LLEN(*fn
));
200 if ((file
[i
].f
=FOPEN(LSTR(str
),mode
))==NULL
) {
204 LPMALLOC(file
[i
].name
);
205 #if defined(MSDOS) || defined(WCE)
206 /* For MSDOS or 32bit DOS store the name in uppercase */
209 Lstrcpy(file
[i
].name
,&str
);
215 /* -------------------------* close_file *------------------------ */
217 close_file( const int f
)
220 r
= FCLOSE(file
[f
].f
);
222 LPFREE(file
[f
].name
);
227 /* --------------------------------------------------------------- */
228 /* OPEN( file, mode ) */
229 /* --------------------------------------------------------------- */
233 if (ARGN
!= 2) Lerror(ERR_INCORRECT_CALL
, 0 );
234 must_exist(1); L2STR(ARG1
);
235 must_exist(2); L2STR(ARG2
);
236 Llower(ARG2
); LASCIIZ(*ARG2
);
237 Licpy(ARGR
, open_file(ARG1
,LSTR(*ARG2
)));
240 /* --------------------------------------------------------------- */
242 /* --------------------------------------------------------------- */
249 Lerror(ERR_INCORRECT_CALL
, 0);
251 if (i
==-1) Lerror(ERR_FILE_NOT_OPENED
,0 );
253 Licpy(ARGR
,close_file(i
));
256 /* --------------------------------------------------------------- */
258 /* --------------------------------------------------------------- */
264 Lerror(ERR_INCORRECT_CALL
, 0);
269 Licpy(ARGR
,((FEOF(file
[i
].f
))?1:0));
272 /* --------------------------------------------------------------- */
274 /* --------------------------------------------------------------- */
280 Lerror(ERR_INCORRECT_CALL
, 0);
285 Licpy(ARGR
,(FFLUSH(file
[i
].f
)));
288 /* --------------------------------------------------------------- */
289 /* STREAM(file[,[option][,command]]) */
290 /* --------------------------------------------------------------- */
298 if (!IN_RANGE(1,ARGN
,3))
299 Lerror(ERR_INCORRECT_CALL
, 0);
306 option
= l2u
[(byte
)LSTR(*ARG2
)[0]];
308 option
= 'S'; /* Status */
310 /* only with option='C' we must have a third argument */
311 if (option
!= 'C' && exist(3))
312 Lerror(ERR_INCORRECT_CALL
, 0);
315 case 'C': /* command */
317 Lerror(ERR_INCORRECT_CALL
, 0);
318 LINITSTR(cmd
); Lfx(&cmd
,LLEN(*ARG3
));
319 Lstrip(&cmd
,ARG3
,LBOTH
,' ');
322 if (!Lcmp(&cmd
,"READ")) {
323 if (i
>=0) close_file(i
);
324 i
= open_file(ARG1
,"r");
325 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
327 if (!Lcmp(&cmd
,"READBINARY")) {
328 if (i
>=0) close_file(i
);
329 i
= open_file(ARG1
,"rb");
330 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
332 if (!Lcmp(&cmd
,"WRITE")) {
333 if (i
>=0) close_file(i
);
334 i
= open_file(ARG1
,"w");
335 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
337 if (!Lcmp(&cmd
,"WRITEBINARY")) {
338 if (i
>=0) close_file(i
);
339 i
= open_file(ARG1
,"wb");
340 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
342 if (!Lcmp(&cmd
,"APPEND")) {
343 if (i
>=0) close_file(i
);
344 i
= open_file(ARG1
,"a+");
345 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
347 if (!Lcmp(&cmd
,"APPENDBINARY")) {
348 if (i
>=0) close_file(i
);
349 i
= open_file(ARG1
,"ab+");
350 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
352 if (!Lcmp(&cmd
,"UPDATE")) {
353 if (i
>=0) close_file(i
);
354 i
= open_file(ARG1
,"r+");
355 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
357 if (!Lcmp(&cmd
,"UPDATEBINARY")) {
358 if (i
>=0) close_file(i
);
359 i
= open_file(ARG1
,"rb+");
360 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
362 if (!Lcmp(&cmd
,"CREATE")) {
363 if (i
>=0) close_file(i
);
364 i
= open_file(ARG1
,"w+");
365 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
367 if (!Lcmp(&cmd
,"CREATEBINARY")) {
368 if (i
>=0) close_file(i
);
369 i
= open_file(ARG1
,"wb+");
370 if (i
==-1) Lerror(ERR_CANT_OPEN_FILE
,0);
372 if (!Lcmp(&cmd
,"CLOSE")) {
373 if (i
>=0) close_file(i
);
375 if (!Lcmp(&cmd
,"FLUSH")) {
376 if (i
>=0) FFLUSH(file
[i
].f
);
378 if (!Lcmp(&cmd
,"RESET")) {
379 if (i
>=0) FSEEK( file
[i
].f
, 0L, SEEK_SET
);
381 Lerror(ERR_INCORRECT_CALL
, 0);
386 case 'D': /* get a description */
387 case 'S': /* status */
389 Lscpy(ARGR
,"UNKNOWN");
392 Lscpy(ARGR
,"NOTREADY");
399 Lerror(ERR_INCORRECT_CALL
, 0);
403 /* --------------------------------------------------------------- */
405 /* --------------------------------------------------------------- */
407 /* --------------------------------------------------------------- */
409 R_charslines( const int func
)
414 Lerror(ERR_INCORRECT_CALL
, 0);
417 if (LLEN(*ARG1
)) i
= find_file(ARG1
);
418 if (i
==-1) i
= open_file(ARG1
,"r+");
420 Lerror(ERR_CANT_OPEN_FILE
,0);
423 Licpy(ARGR
,Lchars(file
[i
].f
));
426 Licpy(ARGR
,Llines(file
[i
].f
));
429 /* --------------------------------------------------------------- */
430 /* CHARIN((file)(,(start)(,length))) */
431 /* --------------------------------------------------------------- */
432 /* LINEIN((file)(,(line)(,count))) */
433 /* --------------------------------------------------------------- */
435 R_charlinein( const int func
)
440 if (!IN_RANGE(1,ARGN
,3))
441 Lerror(ERR_INCORRECT_CALL
, 0);
444 if (LLEN(*ARG1
)) i
= find_file(ARG1
);
445 if (i
==-1) i
= open_file(ARG1
,"r+");
447 Lerror(ERR_CANT_OPEN_FILE
,0);
448 get_oiv(2,start
,LSTARTPOS
);
451 if (func
== f_charin
)
452 Lcharin(file
[i
].f
,ARGR
,start
,length
);
454 if (func
== f_linein
)
455 Llinein(file
[i
].f
,ARGR
,&(file
[i
].line
),start
,length
);
458 /* --------------------------------------------------------------- */
459 /* CHAROUT((file)(,(string)(,start))) */
460 /* --------------------------------------------------------------- */
461 /* LINEOUT((file)(,(string)(,start))) */
462 /* --------------------------------------------------------------- */
464 R_charlineout( const int func
)
470 if (!IN_RANGE(1,ARGN
,3))
471 Lerror(ERR_INCORRECT_CALL
, 0);
474 if (LLEN(*ARG1
)) i
= find_file(ARG1
);
476 i
= open_file(ARG1
,"r+");
477 if (i
==-1) i
= open_file(ARG1
,"w+");
480 Lerror(ERR_CANT_OPEN_FILE
,0);
486 str
= &(NullStr
->key
);
488 get_oiv(3,start
,LSTARTPOS
);
490 if (func
== f_charout
) {
491 Lcharout(file
[i
].f
,str
,start
);
492 Licpy(ARGR
,LLEN(*ARG2
));
494 if (func
== f_lineout
)
495 Licpy(ARGR
,Llineout(file
[i
].f
,str
,&(file
[i
].line
),start
));
497 } /* R_charlineout */
499 /* --------------------------------------------------------------- */
500 /* WRITE( (file)(, string(,))) */
501 /* --------------------------------------------------------------- */
507 if (!IN_RANGE(1,ARGN
,3))
508 Lerror(ERR_INCORRECT_CALL
, 0);
511 if (LLEN(*ARG1
)) i
= find_file(ARG1
);
512 if (i
==-1) i
= open_file(ARG1
,"w");
514 Lerror(ERR_CANT_OPEN_FILE
,0);
516 Lwrite(file
[i
].f
,ARG2
,FALSE
);
517 Licpy(ARGR
, LLEN(*ARG2
));
519 FPUTC('\n',file
[i
].f
);
523 FPUTC('\n',file
[i
].f
);
528 /* --------------------------------------------------------------- */
529 /* READ( (file)(,length) ) */
530 /* length can be a number declaring number of bytes to read */
531 /* or an option 'file', 'line' or 'char' */
532 /* --------------------------------------------------------------- */
539 if (!IN_RANGE(0,ARGN
,2))
540 Lerror(ERR_INCORRECT_CALL
, 0);
543 if (LLEN(*ARG1
)) i
= find_file(ARG1
);
544 if (i
==-1) i
= open_file(ARG1
,"r");
546 Lerror(ERR_CANT_OPEN_FILE
,0);
549 /* search to see if it is a number */
550 if ((LTYPE(*ARG2
)==LSTRING_TY
) && (_Lisnum(ARG2
) == LINTEGER_TY
))
553 if (LTYPE(*ARG2
) == LINTEGER_TY
)
554 l
= (int)LINT(*ARG2
);
556 if (LTYPE(*ARG2
) == LREAL_TY
)
559 if (LTYPE(*ARG2
) == LSTRING_TY
) {
560 switch (l2u
[(byte
)LSTR(*ARG2
)[0]]) {
571 Lerror(ERR_INCORRECT_CALL
, 0);
574 Lerror(ERR_INCORRECT_CALL
, 0);
578 Lread(file
[i
].f
, ARGR
, l
);
581 /* --------------------------------------------------------------- */
582 /* SEEK( file (,offset (,"TOF","CUR","EOF"))) */
583 /* --------------------------------------------------------------- */
591 if (!IN_RANGE(1,ARGN
,3))
592 Lerror(ERR_INCORRECT_CALL
, 0);
593 must_exist(1); i
= find_file(ARG1
);
595 Lerror( ERR_FILE_NOT_OPENED
, 0);
601 switch (l2u
[(byte
)LSTR(*ARG3
)[0]]) {
612 Lerror(ERR_INCORRECT_CALL
, 0 );
615 FSEEK( file
[i
].f
, l
, SEEK
);
617 Licpy(ARGR
, FTELL(file
[i
].f
));