-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharxfuncs.c
1290 lines (1033 loc) · 29.3 KB
/
arxfuncs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ARexx functions for regina
* Copyright © 2002, Staf Verhaegen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* This files contains functions that are implemented in ARexx
* but that are not standard REXX functions. This file contains
* the functions that can be used on all platforms. amifuncs.c
* contains the ARexx functions that are only usable on the
* amiga platform or compatibles. (not implemented yet)
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "rexx.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#if !defined(__WINS__) && !defined(__EPOC32__)
# include <float.h>
#else
# define DBL_EPSILON 2.2204460492503131e-016
#endif
#include <assert.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
staticstreng(_fname, "F");
staticstreng(_fstem, "FI.F");
#if defined(_AMIGA) || defined(__AROS__)
# if defined(GCC)
# include <memory.h>
# include <sys/exec.h>
# else
# include <exec/memory.h>
# include <proto/exec.h>
# endif
#endif
typedef struct _arexx_tsd_t {
proclevel amilevel;
#ifdef rx_64u
rx_64u a,Xn,c;
#else
unsigned long ah,al,Xnh,Xnl,c;
#endif
} arexx_tsd_t;
#if !defined( HAVE_DIV )
typedef struct _div_t
{
int quot;
int rem;
} div_t;
typedef struct _ldiv_t
{
long quot;
long rem;
} ldiv_t;
div_t div(int x,int y)
{
div_t result;
result.quot = x / y;
result.rem = x % y;
return result;
}
ldiv_t ldiv(long x,long y)
{
ldiv_t result;
result.quot = x / y;
result.rem = x % y;
return result;
}
#endif
/*
* Init thread data for arexx functions.
*/
int init_arexxf( tsd_t *TSD )
{
arexx_tsd_t *at;
if ( TSD->arx_tsd != NULL )
return 1;
if ( ( TSD->arx_tsd = MallocTSD( sizeof( arexx_tsd_t ) ) ) == NULL )
return 0;
at = (arexx_tsd_t *)TSD->arx_tsd;
memset( at, 0, sizeof( arexx_tsd_t ) );
/* glibc's starting value is 0 for the whole Xn, we use a seed of 0x1234ABCD */
#ifdef rx_64u
at->a = rx_mk64u( 0x0005DEECE66D );
at->Xn = rx_mk64u( 0x1234ABCD330E );
at->c = 0xB;
#else
at->ah = 0x5;
at->al = 0xDEECE66Dul;
at->Xnh = 0x1234;
at->Xnl = 0xABCD330Eul;
at->c = 0xB;
#endif
return 1;
}
/*
* The implementation of srand48 and drand48 with fixed values in a thread safe
* manner.
*
* We have to produce a value in the interval [0,1[ (zero until one but
* without one) from a 48 bit unsigned integer. This is done by a division by
* the maximum value corrected by small double. This small double is computed
* from the constant DBL_EPSILON.
*
* / a) 1+e > 1 and e > 0
* DBL_EPSILON = e with both
* \ b) there is no number e', e' < e
*
* We increase the divisor of 2**48-1 by (2**48-1)*DBL_EPSILON and have
* the wanted final divisor. That is with 2**48 - 1 = 281474976710655
*/
#define twoE48m1 281474976710655.0
#define divisor ( twoE48m1 * ( 1.0 + DBL_EPSILON ) )
#ifdef rx_64u
/*
* srand48 sets the upper 32 bit of Xn. The lower 16 bit are set to 330E.
*/
static void rx_srand48( const tsd_t *TSD, unsigned long ul )
{
arexx_tsd_t *at = (arexx_tsd_t *)TSD->arx_tsd;
rx_64u ull;
ull = ul & 0xFFFFFFFF;
ull <<= 16;
at->Xn = ull | 0x330E;
}
/*
* Compute X(n+1) = a * X(n) + c
*/
static double rng( arexx_tsd_t *at )
{
rx_64u Xn1;
Xn1 = at->a * at->Xn + at->c;
at->Xn = Xn1 & rx_mk64u( 0xFFFFFFFFFFFF );
# ifdef _MSC_VER
return (double) (signed __int64) at->Xn;
# else
return (double) at->Xn;
# endif
}
#else
static void rx_srand48( const tsd_t *TSD, unsigned long ul )
{
arexx_tsd_t *at = TSD->arx_tsd;
at->Xnh = ( ul >> 16 ) & 0xFFFF;
at->Xnl = ( ( ul & 0xFFFF ) << 16 ) | 0x330E;
}
static double rng( arexx_tsd_t *at )
{
double retval;
unsigned long Xn1h,Xn1l;
unsigned long h,al,ah,bl,bh;
/*
* Doing 64 bit multiplication and addition by hand.
*
* be H = 2*32.
* be A = ah*H + al, with ah<H and al<H
* be B = bh*H + bl, with bh<H and bl<H
*
* then we can compute A*B as:
*
* (ah*H+al)*(bh*H+bl) = ah*bh*H*H +
* ah*bl*H +
* bh*al*H +
* al*bl
*
* We have to add an additional term c, c small and we operate modulo
* 2**48-1. This keeps life simple because we may throw away the
* term ah*bh*H*H because the number is greater as 2**48 without rest.
*
* Furthermore we don't have to bother about carries in the multiplication
* and addition of ah*bl*H and al*bh*H. Finally the term c is so small that
* al*bl+c won't have any further carrying operation.
*
* Indeed, because we want the lower 16 bit part of ah*bl+bh*al, we can
* compute as usual, add the carry of al*bl+c and that's it.
*
* There is just one lack:
* We need everything of al*bl. So we have to compute as above but with
* 16 bit unsigneds to let the product be littler than 2**32.
*
* Perfrom this 16 bit operations first.
*/
al = at->al & 0xFFFF;
ah = at->al >> 16;
bl = at->Xnl & 0xFFFF;
bh = at->Xnl >> 16;
h = al * bl + at->c;
Xn1l = h & 0xFFFF; /* done lower 16 bit */
/*
* Process the *H, H=16 part. Every overflow in addition will be in the
* 48 bit counted from 0, so the final modulo will cut it. Therefore
* we are allowed to ignore every overflow.
*/
h >>= 16;
h += al * bh + ah * bl;
Xn1l |= (h << 16) & 0xFFFF0000; /* done middle 16 bit */
Xn1h = h >> 16;
Xn1h += ah * bh;
/*
* Now do the ah*bl*H + bh*al+H for the outer 32 bit operation.
*/
Xn1h += at->ah * at->Xnl + at->al * at->Xnh;
at->Xnh = Xn1h & 0xFFFF;
at->Xnl = Xn1l;
retval = at->Xnh;
retval *= 4294967296.0l;
retval += at->Xnl;
return retval;
}
#endif
/*
* Map a random value computed by rng of the range [0,2**48[ to the
* range [0,1[
*/
static double rx_drand48( const tsd_t *TSD )
{
arexx_tsd_t *at = (arexx_tsd_t *)TSD->arx_tsd;
double big;
big = (double) rng( at );
return (double) big / divisor;
}
/*
* Support functions for the ARexx IO functions
*/
/* setamilevel will change the environment to the variables used for open files */
static proclevel setamilevel( tsd_t *TSD )
{
arexx_tsd_t *atsd = (arexx_tsd_t *)TSD->arx_tsd;
proclevel oldlevel = TSD->currlevel;
if (atsd->amilevel!=NULL)
TSD->currlevel = atsd->amilevel;
else
{
char txt[20];
atsd->amilevel = newlevel( TSD, NULL );
TSD->currlevel = atsd->amilevel;
setvalue( TSD, _fname, Str_cre_TSD( TSD, "STDIN" ), -1 );
sprintf( txt, "%p", stdin );
setvalue( TSD, _fstem, Str_cre_TSD( TSD, txt ), -1 );
setvalue( TSD, _fname, Str_cre_TSD( TSD, "STDOUT" ), -1 );
sprintf( txt, "%p", stdout );
setvalue( TSD, _fstem, Str_cre_TSD( TSD, txt ), -1 );
setvalue( TSD, _fname, Str_cre_TSD( TSD, "STDERR" ), -1 );
sprintf( txt, "%p", stderr );
setvalue( TSD, _fstem, Str_cre_TSD( TSD, txt ), -1 );
}
return oldlevel;
}
/* getfile will return the FILE pointer of given name */
static FILE *getfile( tsd_t *TSD, const streng *name )
{
proclevel oldlevel = setamilevel( TSD );
const streng *s;
char *txt;
FILE *file=NULL;
setvalue( TSD, _fname, Str_dup_TSD( TSD, name ), -1 );
if ( isvariable( TSD, _fstem ) )
{
s = getvalue( TSD, _fstem, -1 );
txt = str_of( TSD, s );
sscanf( txt, "%p", &file );
FreeTSD( txt );
}
TSD->currlevel = oldlevel;
return file;
}
/* getfilenames will return a list of all opened files */
static streng *getfilenames( tsd_t *TSD, const streng *sep )
{
proclevel oldlevel = setamilevel( TSD );
streng *retval = NULL, *tmpstr;
int first = 1;
variableptr var;
get_next_variable( TSD, 1 );
for ( var = get_next_variable( TSD, 0);
var != NULL;
var = get_next_variable( TSD, 0) )
{
while ( var != NULL && var->realbox != NULL )
var = var->realbox;
if ( var != NULL && ( (var->flag & (VFLAG_STR | VFLAG_NUM)) || var->stem ) )
{
if ( first )
{
retval = Str_dup_TSD( TSD, var->name );
first = 0;
}
else
{
tmpstr = Str_cat_TSD( TSD, retval, sep );
if ( tmpstr != retval )
{
Free_string_TSD( TSD, retval );
retval = tmpstr;
}
tmpstr = Str_cat_TSD( TSD, retval, var->name );
if ( tmpstr != retval )
{
Free_string_TSD( TSD, retval );
retval = tmpstr;
}
}
}
}
TSD->currlevel = oldlevel;
/* If no variable present return NULL string */
if (first)
retval = nullstringptr();
return retval;
}
/* addfile: store the FILE pointer in a given name */
static void addfile( tsd_t *TSD, const streng *name, FILE *file )
{
proclevel oldlevel = setamilevel( TSD );
char txt[20];
streng *s;
sprintf( txt, "%p", (void *)file );
s = Str_cre_TSD( TSD, txt );
setvalue( TSD, _fname, Str_dup_TSD( TSD, name ), -1 );
setvalue( TSD, _fstem, s, -1 );
TSD->currlevel = oldlevel;
}
/* rmfile: remove a given of open files list */
static void rmfile( tsd_t *TSD, const streng *name )
{
arexx_tsd_t *atsd = (arexx_tsd_t *)TSD->arx_tsd;
proclevel oldlevel = setamilevel( TSD );
TSD->currlevel = atsd->amilevel;
drop_var( TSD, name );
TSD->currlevel = oldlevel;
}
/*
* Implementation of the ARexx IO functions
* See general documentation for more information
* Functions implemented: OPEN, CLOSE, READCH, READLN, WRITECH, WRITELN, EOF, SEEK
*/
streng *arexx_open( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2, parm3;
char *filename;
FILE *file;
int mode;
static const char* modestrings[] = {
"w",
"r+",
"a"
};
checkparam( parm1, 2, 3, "OPEN" );
parm2 = parm1->next;
parm3 = parm2->next;
file = getfile( TSD, parm1->value );
if ( file!=NULL )
{
return int_to_streng( TSD, 0 );
}
filename = str_of( TSD, parm2->value );
if ( parm3==NULL
|| parm3->value==NULL
|| parm3->value->len==0 )
mode=0;
else switch( getoptionchar( TSD, parm3->value, "OPEN", 3, "", "WRA" ) )
{
case 'W':
mode=0;
break;
case 'R':
mode=1;
break;
case 'A':
mode=2;
break;
default:
mode=0;
assert(0);
break;
}
file = fopen( filename, modestrings[mode] );
FreeTSD( filename );
if ( file==NULL )
{
return int_to_streng( TSD, 0 );
}
addfile( TSD, parm1->value, file );
return int_to_streng( TSD, 1);
}
streng *arexx_close( tsd_t *TSD, cparamboxptr parm1 )
{
FILE *file;
checkparam( parm1, 1, 1, "CLOSE" );
file = getfile( TSD, parm1->value );
if ( file==NULL )
return int_to_streng( TSD, 0 );
fclose( file );
rmfile( TSD, parm1->value );
return int_to_streng( TSD, 1 );
}
streng *arexx_writech( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
FILE *file;
int count;
checkparam( parm1, 2, 2, "WRITECH" );
parm2 = parm1->next;
file = getfile( TSD, parm1->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "WRITECH", tmpstr_of( TSD, parm1->value ));
count = fwrite( parm2->value->value, 1, Str_len(parm2->value), file );
return int_to_streng( TSD, count );
}
streng *arexx_writeln( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
char *txt;
FILE *file;
int count;
checkparam( parm1, 2, 2, "WRITELN" );
parm2 = parm1->next;
file = getfile( TSD, parm1->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "WRITELN", tmpstr_of( TSD, parm1->value ) );
txt = str_of( TSD, parm2->value );
count = fprintf(file, "%s\n", txt);
FreeTSD( txt );
return int_to_streng( TSD, count );
}
streng *arexx_seek( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2, parm3;
FILE *file;
int pos, error, wench;
long offset;
checkparam( parm1, 2, 3, "SEEK" );
parm2 = parm1->next;
parm3 = parm2->next;
file = getfile( TSD, parm1->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "SEEK", tmpstr_of( TSD, parm1->value ) );
offset = streng_to_int( TSD, parm2->value, &error );
if (error)
exiterror( ERR_INCORRECT_CALL, 11, "SEEK", 2, tmpstr_of( TSD, parm2->value ) );
if ( parm3==NULL
|| parm3->value==NULL
|| parm3->value->len == 0 )
wench = SEEK_CUR;
else switch( getoptionchar( TSD, parm3->value, "SEEK", 3, "", "CBE" ) )
{
case 'C':
wench = SEEK_CUR;
break;
case 'B':
wench = SEEK_SET;
break;
case 'E':
wench = SEEK_END;
break;
default:
wench = SEEK_CUR;
assert(0);
break;
}
pos = ftell( file );
if ( fseek( file, offset, wench ) != -1 )
{
pos = ftell( file );
}
return int_to_streng( TSD, pos );
}
streng *arexx_readch( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
FILE *file;
checkparam( parm1, 1, 2, "READCH");
parm2 = parm1->next;
file = getfile( TSD, parm1->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "READCH", tmpstr_of( TSD, parm1->value ) );
if ( parm2==NULL )
{
char buffer[2] = { 0, 0 };
buffer[0] = (char)getc( file );
return Str_cre_TSD( TSD, buffer );
}
else
{
int count, error;
streng *ret;
count = streng_to_int( TSD, parm2->value, &error );
if ( error )
exiterror( ERR_INCORRECT_CALL, 11, "READCH", 2, tmpstr_of( TSD, parm2->value ) );
if ( count<=0 )
exiterror( ERR_INCORRECT_CALL, 14, "READCH", 2, tmpstr_of( TSD, parm2->value ) );
ret = Str_makeTSD( count );
count = fread( ret->value, 1, count, file );
if ( count == -1 )
{
/*
* Fixme: What shall happen in this case?
* Setting count to 0 seems a little bit weak for me but better
* than doing more strange things. FGC
*/
count = 0;
}
ret->len = count;
return ret;
}
}
streng *arexx_readln( tsd_t *TSD, cparamboxptr parm )
{
FILE *file;
char buffer[1001];
checkparam( parm, 1, 1, "READLN");
file = getfile( TSD, parm->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "READLN", tmpstr_of( TSD, parm->value ) );
fgets( buffer, 1001, file );
if ( buffer[strlen(buffer)-1]=='\n' )
buffer[strlen(buffer)-1]=0;
return Str_cre_TSD( TSD, buffer );
}
streng *arexx_eof( tsd_t *TSD, cparamboxptr parm )
{
FILE *file;
checkparam( parm, 1, 1, "EOF" );
file = getfile( TSD, parm->value );
if ( file==NULL )
exiterror( ERR_INCORRECT_CALL, 27, "EOF", tmpstr_of( TSD, parm->value ) );
return int_to_streng( TSD, feof( file )!=0 );
}
/*
* Implementation of the additional conversion functions from ARexx
* Functions: B2C, C2B
*/
streng *arexx_b2c( tsd_t *TSD, cparamboxptr parm )
{
parambox parm2;
streng *ret;
checkparam( parm, 1, 1, "B2C" );
parm2.next = NULL;
parm2.value = std_b2x( TSD, parm );
ret = std_x2c( TSD, &parm2 );
Free_string_TSD( TSD, parm2.value );
return ret;
}
streng *arexx_c2b( tsd_t *TSD, cparamboxptr parm )
{
parambox parm2;
streng *ret;
checkparam( parm, 1, 1, "B2C" );
parm2.next = NULL;
parm2.value = std_c2x( TSD, parm );
ret = std_x2b( TSD, &parm2 );
Free_string_TSD( TSD, parm2.value );
return ret;
}
/*
* Implementation of the bitwise function from ARexx
* Functions: BITCHG, BITCLR, BITSET, BITTST, BITCOMP
*/
streng *arexx_bitchg( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
streng *ret;
int bit, error, byte;
div_t dt;
checkparam( parm1, 2, 2, "BITCHG" );
parm2 = parm1->next;
bit = streng_to_int( TSD, parm2->value, &error );
if ( error )
exiterror( ERR_INCORRECT_CALL, 11, "BITCHG", 2, tmpstr_of( TSD, parm2->value ) );
if ( bit<0 )
exiterror( ERR_INCORRECT_CALL, 13, "BITCHG", 2, tmpstr_of( TSD, parm2->value ) );
dt = div( bit, 8 );
byte = parm1->value->len-dt.quot-1;
if ( byte<0 )
exiterror( ERR_INCORRECT_CALL, 0 );
ret = Str_dup_TSD( TSD, parm1->value );
ret->value[byte]^=(char)(1<<dt.rem);
return ret;
}
streng *arexx_bitclr( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
streng *ret;
int bit, error, byte;
div_t dt;
checkparam( parm1, 2, 2, "BITCLR" );
parm2 = parm1->next;
bit = streng_to_int( TSD, parm2->value, &error );
if ( error )
exiterror( ERR_INCORRECT_CALL, 11, "BITCLR", 2, tmpstr_of( TSD, parm2->value ) );
if ( bit<0 )
exiterror( ERR_INCORRECT_CALL, 13, "BITCLR", 2, tmpstr_of( TSD, parm2->value ) );
dt = div( bit, 8 );
byte = parm1->value->len-dt.quot-1;
if ( byte<0 )
exiterror( ERR_INCORRECT_CALL, 0 );
ret = Str_dup_TSD( TSD, parm1->value );
ret->value[byte]&=~(char)(1<<dt.rem);
return ret;
}
streng *arexx_bitset( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
streng *ret;
int bit, error, byte;
div_t dt;
checkparam( parm1, 2, 2, "BITSET" );
parm2 = parm1->next;
bit = streng_to_int( TSD, parm2->value, &error );
if ( error )
exiterror( ERR_INCORRECT_CALL, 11, "BITSET", 2, tmpstr_of( TSD, parm2->value ) );
if ( bit<0 )
exiterror( ERR_INCORRECT_CALL, 13, "BITSET", 2, tmpstr_of( TSD, parm2->value ) );
dt = div( bit, 8 );
byte = parm1->value->len-dt.quot-1;
if ( byte<0 )
exiterror( ERR_INCORRECT_CALL, 0 );
ret = Str_dup_TSD( TSD, parm1->value );
ret->value[byte]|=(char)(1<<dt.rem);
return ret;
}
streng *arexx_bittst( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2;
streng *ret;
int bit, error, byte;
div_t dt;
checkparam( parm1, 2, 2, "BITTST" );
parm2 = parm1->next;
bit = streng_to_int( TSD, parm2->value, &error );
if ( error )
exiterror( ERR_INCORRECT_CALL, 11, "BITTST", 2, tmpstr_of( TSD, parm2->value ) );
if ( bit<0 )
exiterror( ERR_INCORRECT_CALL, 13, "BITTST", 2, tmpstr_of( TSD, parm2->value ) );
dt = div( bit, 8 );
byte = parm1->value->len-dt.quot-1;
if ( byte<0 )
exiterror( ERR_INCORRECT_CALL, 0 );
ret = int_to_streng( TSD, (parm1->value->value[byte] & (char)(1<<dt.rem))!=0 );
return ret;
}
/* Help function for arexx_bitcomp */
static int firstbit(char c)
{
int i;
assert(c!=0);
for ( i=0; i<8; i++)
{
if (c & 1)
return i;
else
c = (char)(c >> 1);
}
return 8;
}
/* This ARexx function has very weird usage of the pad byte,
* the shortest string is padded on the left with this byte
*/
streng *arexx_bitcomp( tsd_t *TSD, cparamboxptr parm1 )
{
cparamboxptr parm2, parm3;
const streng *s1, *s2;
const char *cp1, *cp2;
char pad;
int i;
checkparam( parm1, 2, 3, "BITCOMP" );
parm2 = parm1->next;
/* Make s2 always shorter or equal to s1 */
if ( parm1->value->len < parm2->value->len )
{
s1 = parm2->value;
s2 = parm1->value;
} else {
s1 = parm1->value;
s2 = parm2->value;
}
for ( cp1=s1->value+s1->len-1, cp2=s2->value+s2->len-1, i=0;
cp2 >= s2->value;
cp1--, cp2--, i++ )
{
if ( *cp1 != *cp2 )
return int_to_streng( TSD, i*8 + firstbit( ( char ) ( *cp1 ^ *cp2 ) ) );
}
parm3 = parm2->next;
if ( parm3==NULL || parm3->value==NULL || parm3->value->len==0 )
pad = 0;
else
pad = parm3->value->value[0];
for ( ;
cp1 >= s1->value;
cp1--, i++ )
{
if ( *cp1 != pad )
return int_to_streng( TSD, i*8 + firstbit( ( char ) ( *cp1 ^ pad ) ) );
}
return int_to_streng( TSD, -1 );
}
/*
* Some more misc. ARexx functions
* Functions: COMPRESS, HASH, RANDU, TRIM, UPPER
*/
streng *arexx_hash( tsd_t *TSD, cparamboxptr parm1 )
{
unsigned char *uc;
int i, sum=0;
checkparam( parm1, 1, 1, "HASH" );
uc = (unsigned char *)parm1->value->value;
for ( i=0; i<parm1->value->len; i++)
{
sum = (sum + uc[i]) & 255;
}
return int_to_streng( TSD, sum );
}
streng *arexx_compress( tsd_t *TSD, cparamboxptr parm1 )
{
const char *match;
int i, start;
streng *ret;
checkparam( parm1, 1, 2, "COMPRESS" );
match = ( parm1->next!=NULL ) ? str_of( TSD, parm1->next->value ) : " ";
ret = Str_dup_TSD( TSD, parm1->value );
for ( i=start=0; i<ret->len; i++ )
{
/* Copy char if not found */
if ( strchr( match, ret->value[i] )==NULL )
{
ret->value[start] = ret->value[i];
start++;
}
}
ret->len = start;
if ( parm1->next!=NULL )
FreeTSD( (char *)match );
return ret;
}
static const streng T_str = { 1, 1, "T" };
static const parambox T_parm = { NULL, 0, (streng *)&T_str };
streng *arexx_trim( tsd_t *TSD, cparamboxptr parm1 )
{
parambox parm;
checkparam( parm1, 1, 1, "TRIM" );
parm = *parm1;
parm.next = (paramboxptr)&T_parm;
return std_strip( TSD, parm1 );
}
streng *arexx_upper( tsd_t *TSD, cparamboxptr parms )
{
rx_64 rlength=0, length=0, start=1, i=0 ;
int changecount;
char padch=' ' ;
streng *str=NULL, *ptr=NULL ;
paramboxptr bptr=NULL ;
/*
* Check that we have between 1 and 4 args
* ( str [,start[,length[,pad]]] )
*/
checkparam( parms, 1, 4 , "UPPER" ) ;
str = parms->value ;
rlength = Str_len( str ) ;
/*
* Get starting position, if supplied...
*/
if ( parms->next != NULL
&& parms->next->value )
start = atoposrx64( TSD, parms->next->value, "UPPER", 2 ) ;
/*
* Get length, if supplied...
*/
if ( parms->next != NULL
&& ( (bptr = parms->next->next) != NULL )
&& ( parms->next->next->value ) )
length = atozposrx64( TSD, parms->next->next->value, "UPPER", 3 ) ;
else
length = ( rlength >= start ) ? rlength - start + 1 : 0;
/*
* Get pad character, if supplied...
*/
if ( (bptr )
&& ( bptr->next )
&& ( bptr->next->value ) )
padch = getonechar( TSD, parms->next->next->next->value, "UPPER", 4) ;
/*
* Create our new starting; duplicate of input string
*/
ptr = Str_makeTSD( rlength );
memcpy( Str_val( ptr ), Str_val( str ), Str_len( str ) );
/*
* Determine where to start changing case...
*/
i = ((rlength>=start)?start-1:rlength) ;