2 static char *RCSid
= "$Id$";
6 * The Regina Rexx Interpreter
7 * Copyright (C) 1992-1994 Anders Christensen <anders@pvv.unit.no>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 # include <sys/stat.h>
45 #if defined(__WATCOMC__) && !defined(__QNX__)
52 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
53 # pragma warning(disable: 4115 4201 4214)
59 # pragma warning(default: 4115 4201 4214)
65 * Since development of Ultrix has ceased, and they never managed to
66 * fix a few things, we want to define a few things, just in order
67 * to kill a few warnings ...
69 #if defined(FIX_PROTOS) && defined(FIX_ALL_PROTOS) && defined(ultrix)
70 int fstat( int fd
, struct stat
*buf
) ;
71 int stat( char *path
, struct stat
*buf
) ;
74 streng
*cms_sleep( tsd_t
*TSD
, cparamboxptr parms
)
76 checkparam( parms
, 1, 1, "SLEEP" ) ;
77 #if defined(WIN32) && (defined(_MSC_VER) || defined(__IBMC__) || defined(__BORLANDC__) || defined(__MINGW32__))
78 Sleep( (int)((myatof(TSD
,parms
->value
))*1000) ) ;
80 #if defined(HAVE_USLEEP)
81 usleep( (int)((myatof(TSD
,parms
->value
))*1000*1000) ) ;
83 sleep( atozpos( TSD
, parms
->value
, "SLEEP", 1 ) ) ;
86 return nullstringptr() ;
90 streng
*cms_makebuf( tsd_t
*TSD
, cparamboxptr parms
)
92 checkparam( parms
, 0, 0 , "MAKEBUF" ) ;
93 return int_to_streng( TSD
,make_buffer( TSD
)) ;
98 streng
*cms_justify( tsd_t
*TSD
, cparamboxptr parms
)
100 int inspace
=0, i
=0, count
=0, between
=0, extra
=0, initial
=0;
101 int spaces
=0, chars
=0, length
=0 ;
102 char *cend
=NULL
, *cp
=NULL
, *cptr
=NULL
, *out
=NULL
, *oend
=NULL
;
104 streng
*result
=NULL
;
106 checkparam( parms
, 2, 3 , "JUSTIFY" ) ;
108 cptr
= parms
->value
->value
;
109 cend
= cptr
+ parms
->value
->len
;
111 length
= atozpos( TSD
, parms
->next
->value
, "JUSTIFY", 2 ) ;
112 if (parms
->next
->next
&& parms
->next
->next
->value
)
113 pad
= getonechar( TSD
, parms
->next
->next
->value
, "JUSTIFY", 3 ) ;
120 for (cp
=cptr
; cp
<cend
; cp
++)
142 if (inspace
&& spaces
)
145 result
= Str_makeTSD( length
) ;
146 if (chars
+spaces
>length
|| spaces
==0)
154 extra
= (length
- chars
) % spaces
;
155 between
= (length
- chars
) / spaces
;
156 initial
= (spaces
- extra
) / 2 ;
160 out
= result
->value
;
161 oend
= out
+ length
;
163 for (; cp
<cend
&& isspace(*cp
); cp
++) ;
164 for (; cp
<cend
&& out
<oend
; cp
++)
168 for (;cp
<cend
&& isspace(*cp
); cp
++) ;
169 for (i
=0; i
<between
&& out
<oend
; i
++)
173 else if (extra
&& out
<oend
)
185 for (; out
<oend
; out
++)
188 assert( out
- result
->value
== length
) ;
189 result
->len
= length
;
196 streng
*cms_find( tsd_t
*TSD
, cparamboxptr parms
)
198 parambox newparms
[3];
200 checkparam( parms
, 2, 3 , "FIND" ) ;
202 /* Rebuild parms but switch the first two parameters */
204 memset(newparms
, 0, sizeof(newparms
) ) ; /* sets dealloc to 0, too */
205 newparms
[0].value
= parms
->next
->value
;
206 newparms
[0].next
= newparms
+ 1;
207 newparms
[1].value
= parms
->value
;
208 if (parms
->next
->next
)
210 newparms
[1].next
= newparms
+ 2;
211 newparms
[2].value
= parms
->next
->next
->value
;
214 return std_wordpos( TSD
, newparms
) ;
218 streng
*cms_index( tsd_t
*TSD
, cparamboxptr parms
)
220 parambox newparms
[3];
222 checkparam( parms
, 2, 3 , "INDEX" ) ;
224 /* Rebuild parms but switch the first two parameters */
226 memset(newparms
, 0, sizeof(newparms
) ) ; /* sets dealloc to 0, too */
227 newparms
[0].value
= parms
->next
->value
;
228 newparms
[0].next
= newparms
+ 1;
229 newparms
[1].value
= parms
->value
;
230 if (parms
->next
->next
)
232 newparms
[1].next
= newparms
+ 2;
233 newparms
[2].value
= parms
->next
->next
->value
;
236 return std_pos( TSD
, newparms
) ;
239 streng
*cms_desbuf( tsd_t
*TSD
, cparamboxptr parms
)
241 checkparam( parms
, 0, 0 , "DESBUF" ) ;
242 return( int_to_streng( TSD
,drop_buffer( TSD
, 0))) ;
246 streng
*cms_buftype( tsd_t
*TSD
, cparamboxptr parms
)
248 checkparam( parms
, 0, 0 , "BUFTYPE" ) ;
250 return (nullstringptr()) ;
254 streng
*cms_dropbuf( tsd_t
*TSD
, cparamboxptr parms
)
258 checkparam( parms
, 0, 1 , "DROPBUF" ) ;
260 buffer
= myatol(TSD
, parms
->value
) ;
262 return( int_to_streng( TSD
,drop_buffer(TSD
, buffer
))) ;
267 /* this part of the code is not used */
269 static int select_file( const struct direct
*entry
)
271 return !(strcmp(entry
->d_name
,filename
)) ;
275 streng
*cms_state( tsd_t
*TSD
, cparamboxptr parms
)
277 struct direct
*names
=NULL
;
278 int last
=0, result
=0 ;
279 char *dir
=NULL
, *string
=NULL
, *retval
=NULL
;
281 checkparam( parms
, 1, 1 , "STATE" ) ;
282 last
= strlen(string
=parms
->value
) ;
283 for (;(string
[last
]!=FILE_SEPARATOR
)&&(last
>0);last
--) ;
286 string
[last
] = '\000' ;
292 result
= scandir(dir
,&names
,&select_file
,NULL
) ;
294 string
[last
] = FILE_SEPARATOR
;
296 /* Ought to open or stat the file to check if it is readable */
298 return int_to_streng( TSD
,result
==1) ;
303 streng
*cms_state( tsd_t
*TSD
, cparamboxptr parms
)
305 /* this is a bit too easy ... but STREAM() function should handle it */
306 streng
*retval
=NULL
;
311 checkparam( parms
, 1, 1 , "STATE" ) ;
312 retval
= Str_makeTSD( BOOL_STR_LENGTH
) ;
314 /* will generate warning under Ultrix, don't care */
315 fn
= str_of(TSD
,parms
->value
);
316 rcode
= stat( fn
, &buffer
) ;
318 return int_to_streng( TSD
,rcode
!=0) ;