sub.c
14.3 KB
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
#include <stdlib.h>
#include <stdio.h>
#ifndef __N64__
#include <stdarg.h>
#include <string.h>
#endif
#include "sub.h"
/* 環境変数 */
int OPTIA[16];
int OPTIAS;
char NULSTR[1]={0};
char * TGTPYA[16] = {NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR};
int TGTPYAS = 0;
u8 EXEPATHYA[256]={0};
u8 EXENAMEYA[256]={0};
/* モジュール変数 */
u8 __0[1024];
u8 * _S[10] = {NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR,NULSTR};
int _I[10] = {0};
u8 _A[10][256];
u8 * _B[10];
static u8 ptnya[256];
static int sb_endMatcho,sb_startMatcho;
static sb_kakko_No;
/* module functions */
int sbMakePtn( u8 * wordpy );
int sbExtCompare(u8 liney , u8 wordy , u8 flagi);
u8 * sbMatchHead( u8 * linepy ,u8 * wordpy );
int sbPop(void);
void sbPush(int i);
int
sbIsNumeric(char * s)
{
if(s == 0 || *s == 0) return 0;
while( *s != 0 ){
if( *s < 48 || *s > 57 ) return 0;
s++;
}
return 1;
}
int
sbLength(char * s)
{
#if 1
return strlen(s);
#else
int i=0;
if(s == 0) return -1;
while( *(s+i) != 0 ) ++i;
return i;
#endif
}
int
sbIndex(char * str, char target)
{
int hit = -1 , pos = -1;
if ( target == 0 || str == 0) return -1;
while ( *str != null )
{
++pos;
if( *str == target ){
hit = pos;
break;
}
++str;
}
return hit;
}
int
sbIndexR(char * str, char target)
{
int hit = -1 , pos;
if ( target == 0 || str == 0) return -1;
pos = sbLength(str);
while ( --pos > -1 )
if( *(str+pos) == target ){
hit = pos;
break;
};
return hit;
}
void
sbFillZero( void * dest ,size_t size )
{
memset(dest,0,size);
}
/* グローバルヒープに複写して渡す */
u8 *
sbGetSp( int i )
{
if( _I[i] > 255 ) _I[i] = 255;
memcpy( _A[i] , _S[i] , _I[i] );
_A[i][_I[i]] = 0;
return _A[i];
}
void
sbCopySp( u8 * dest ,int i )
{
memcpy( dest , sbGetSp(i) , _I[i] );
_A[i][_I[i]] = 0;
}
void
sbStrCopy(u8 * from ,u8 * __0 ,int length )
{
memcpy( __0 , from , length );
__0[length] = 0;
}
/*
line に対する パターンマッチ操作。
()を使えば一致文字列を取得出来ます。
$s,$w,"^ $" ,k*,.* 等の正規表現が可能です。
返り値
最初の一致文字列が始まる adress
()を10個以上使うとエラーが起きます。
*/
static int sb_stack[10];
static int sb_stck;
int
sbMakePtn( u8 * wordpy )
{
int i=-1,o;
u8 y;
sb_startMatcho=0;sb_endMatcho=0;
y = *wordpy;
if ( y == '^') { y = *++wordpy; sb_startMatcho = 1 ; }
while( y != 0 ){
o=0;
if(y == '['){ /* 反転 */
if( *(wordpy+1) == '^' && ( *(wordpy+3) == ']'
|| *(wordpy + 4) == ']') ){
wordpy += 2; y = *wordpy;
o=1;
}else{
sbErrStop("sorry , not support this pattern");
}
}
if ( y == '$'){
y = *++wordpy ;
if ( y == 'w' ){ ptnya[++i] = 0x80;
}else if( y == 'W' ){ ptnya[++i] = 0x80; ptnya[++i] = 0xA2;
}else if( y == 'd' ){ ptnya[++i] = 0x81;
}else if( y == 'D' ){ ptnya[++i] = 0x81; ptnya[++i] = 0xA2;
}else if( y == 's' ){ ptnya[++i] = 0x82;
}else if( y == 'S' ){ ptnya[++i] = 0x82; ptnya[++i] = 0xA2;
}else if( y == 0 ){ ptnya[++i] = 0; sb_endMatcho = 1 ;
}else if( y > 0x60 && y < 0x7B ) ptnya[++i] = y - 0x20;
else ptnya[++i] = y;
}else if(y == '['){ /* 反転 */
if( *(wordpy+1) == '^' && *(wordpy+3) == ']'){
ptnya[++i] = y;
ptnya[++i] = 0xA2;
wordpy += 3;
}else{
sbErrStop("sorry , not support this pattern");
}
}else if( y > 0x60 && y < 0x7B ){ /* 全て大文字に */
ptnya[++i] = y - 0x20;
}else if( y =='(' ){ ptnya[++i] = 0xA0;
}else if( y ==')' ){ ptnya[++i] = 0xA1;
}else if( y =='?' ){ ptnya[++i] = 0xA3;
}else if( y =='*' ){ ptnya[++i] = 0xA4;
}else if( y =='+' ){ ptnya[++i] = 0xA5;
}else if( y =='.' ){ ptnya[++i] = 0x83;
}else{
ptnya[++i] = y;
}
if(o){
ptnya[++i] = 0xA2;
wordpy += 1;
}
y = *++wordpy;
}
ptnya[++i]=0;
return i;
}
/* 返り値:マッチ位置オフセット
マッチしなければ−1。
マッチした場合は _0 にマッチした文字列が格納されます
*/
int
sbMatch3( u8 * linepy , u8 * wordpy ,u8 * s1 ,u8 * s2 ,u8 * s3 )
{
int ai,searchS,toi;
u8 * ret=0;
sprintf( ptnya , wordpy ,s1 ,s2 ,s3);
toi = searchS = sbLength(linepy);
sbMakePtn( ptnya );
if (sb_startMatcho) toi = 1;
sb_kakko_No = 0;
sb_stck=-1;
for(ai=0; ai < toi ; ++ai)
{
ret = sbMatchHead( linepy + ai , ptnya );
if ( ret != 0 )
if ( !sb_endMatcho || ret == linepy + searchS )
{
_S[0] = linepy + ai;
_I[0] = ret - linepy - ai;
break;
}
}
if ( sb_stck != -1 || sb_kakko_No != 0 ) sbErrStop(" blacket doesn't matches ");
return ret ? (int)(ret - linepy) : -1;
}
/*
一文字マッチ
マッチが成功したら line マッチシッポのアドレスを返す。
失敗の場合は 0
flagi = 1 bit -> reversal
*/
int
sbExtCompare(u8 liney ,u8 wordy ,u8 flagi)
{
u8 dy ,o;
u8 ret;
/* sprintf(_A[9],"246:%d %d %d",liney ,wordy, flagi );
sbErrStopSSI(_A[9],"","",0,0);
*/
if( liney == 0 ) return 0;
if ( wordy < 0x80 ){ /* normal characters */
dy = liney - wordy;
o = ( dy == 0 || ( wordy > 0x40 && wordy < 0x5B && dy == 0x20 ) );
ret = o ^ (0x1 & flagi); /* 反転フラグ */
}else if( wordy < 0xA0 ){ /* original defined characters */
switch ( wordy - 0x80 )
{
case 0: /* $w */
o = ( liney == 0x5F || ( liney > 0x40 && liney < 0x5B)
|| ( liney > 0x60 && liney < 0x7B) );
break;
case 1: /* $d */
o = ( liney >= 0x30 && liney < 0x3A );
break;
case 2: /* $s */
o = ( ( liney > 0x08 && liney < 0x0E ) || liney == 0x20 );
break;
case 3: /* . */
o = ( liney < 0x80 );
break;
default:
sbErrStop("undefined character used");
}
ret = o ^ (0x1 & flagi);
}else
{
sbErrStopI("sbExtCompare: incorrect param",wordy);
}
return ret;
}
/*
マッチした場合は
line のマッチ列末尾アドレス + 1 を返す。
マッチ失敗時は0
*/
u8 *
sbMatchHead(u8 * linepy ,u8 * wordpy )
{
u8 * ret = 0;
int fromi,toi ,i ,repeati ,reversal ,keepi;
u8 y ,nexty, o;
#ifdef DEBUG
nest ++;
#endif
y = *wordpy;
if ( y == 0 )
{
ret = linepy;
}
else if ( y < 0xA0 ) /* not optional character */
{
reversal = 0;
nexty = *(wordpy+1);
if( nexty == 0xA2 )
{
reversal = 1;
nexty = *(wordpy+2);
}
if( nexty == 0 )
{
o = sbExtCompare( *linepy , *wordpy , reversal);
if( o ) ret = linepy + 1;
else ret = 0;
}
else
{
if ( nexty < 0xA2 )
{
o = sbExtCompare( *linepy , *wordpy , reversal );
if( o ) ret = sbMatchHead( linepy + 1 , wordpy + 1 + reversal );
else ret = 0;
}
else
{
switch ( nexty - 0xA0 )
{
case 3: fromi = 0; toi = 1; break; /* ? */
case 4: fromi = 0; toi = SB_MAX_REPEATS ;break; /* * */
case 5: fromi = 1; toi = SB_MAX_REPEATS ;break; /* + */
default:
sbErrStop("sbMatchHead: parseError 6");
}
ret = 0; o = 1;
repeati = fromi;
while( repeati <= toi )
{
for(i=0;i<repeati;++i){
o &= sbExtCompare( *(linepy+i) , *wordpy , reversal);
}
if(!o) break;
ret = sbMatchHead( linepy + repeati , wordpy + 2 + reversal );
if( ret ) break;
repeati++;
}
}
}
}
else if ( y == 0xA0 ) /* parenthesis (kakko) */
{
sbPush(++sb_kakko_No);
_S[sb_kakko_No] = linepy; /* $n 情報 */
ret = sbMatchHead( linepy , wordpy+1 );
sbPop();--sb_kakko_No;
}
else if ( y == 0xA1 ) /* ")" parenthesis (kakkotoji) */
{
keepi = sbPop();
_I[keepi] = linepy - _S[keepi];
ret = sbMatchHead( linepy , wordpy+1 );
sbPush(keepi);
if ( ret==0 ) _I[keepi] = 0;
}
else
{
sbErrStop(" sbMatchHead: non support match distribution ");
}
#ifdef DEBUG
nest--;
#endif
return ret;
}
int
sbGetOpts(char ** agv , int argc , char * arguementative )
{
int _errs=0 , _size , _pos , _i;
void * _argv[64];
char *_p ,_c, from[64],to[64],dummy[64];
int _tmp[64]; /* max 32 */
#ifdef PC
_fullpath(EXEPATHYA,".",256);
_i = sbMatch3(agv[0],"%s",EXEPATHYA,NULSTR,NULSTR);
if( sbMatch3(agv[0] + _i ,"^\\",NULSTR,NULSTR,NULSTR) > -1 ) _i++;
sprintf( EXENAMEYA ,agv[0] + _i );
#else
sprintf( EXENAMEYA ,agv[0] );
#endif
if (sbLength(arguementative)>64) {
fprintf(stderr,"too much options \n");
return 0;
}
for(_i=0;_i<64;_i++)
{
from[_i] = to[_i] = 0;
_tmp[_i] = 0;
if (_i < argc)
_argv[_i] = *(agv+_i); else _argv[_i] = NULSTR;
}
/* デフォ値 */
_pos = -1;
_p = arguementative;
--_p;
while ( *++_p != null)
{
++_pos;
switch(*++_p)
{
case ':':
_tmp[_pos] = (int)NULSTR;
break;
case '-':
_tmp[_pos] = 0;
_tmp[_pos+1] = -1;
break;
default:
_tmp[_pos] = 0;
}
++_pos;
}
_size = argc;
sbShift( (void **) _argv , &_size );
while( _size > 0)
{
_p = sbShift( (void **)_argv , &_size );
if( *_p == '-')
{
while (1)
{
_pos = sbIndex( arguementative , *++_p);
if( _pos >= 0 && (_pos % 2)==0 ){
_c = arguementative[_pos + 1];
switch( _c )
{
case '0': /* none */
_tmp[_pos] = 1 ;
if ( *(_p+1) != null) continue;
break;
case '1': /* numeric */
if( *(++_p) == null)
if(( _p = sbShift( (void **)_argv , &_size )) == null ) ++_errs;
if ( sbIsNumeric(_p) )
_tmp[_pos] = sbStr2Long( _p ); else ++_errs;
break;
case '-': /* area */
if( *(++_p) == null)
if(( _p = sbShift( (void **)_argv , &_size )) == null ) ++_errs;
if ( sbSplit(_p,'-',from,to,dummy) > 2 )
++_errs;
else{
if ( sbIsNumeric(from) )
_tmp[_pos] = sbStr2Long(from);
else
if ( *from == null ) _tmp[_pos] = 0; else ++_errs;
if ( sbIsNumeric(to) )
_tmp[_pos + 1] = sbStr2Long(to);
else
if ( *to == null ) _tmp[_pos +1 ] = -1; else ++_errs;
}
break;
case ':': /* string */
if( *(++_p) == null)
if(( _p = sbShift( (void **)_argv , &_size )) == null ){ ++_errs;}
_tmp[_pos] = (int)_p;
break;
default:
fprintf(stderr,"Illegal switch: %c \n",_c );
++_errs;
}
}else
{
fprintf(stderr,"Unknown option: %c \n",*_p );
++_errs;
break;
}
break;
}
}
else
{
TGTPYA[TGTPYAS] = _p;
TGTPYAS++;
}
}
_pos = -1;
_i=-1;
_p = arguementative;
while ( *_p != null)
{
OPTIA[++_i] = _tmp[++_pos];
if ( *++_p == ':' && OPTIA[_i] == 0) OPTIA[_i] = (int)NULSTR;
++_pos;
if ( *_p == '-') OPTIA[++_i] = _tmp[_pos];
++_p;
}
OPTIAS = _i+1;
return (_errs == 0);
}
/*
* int sbSplit(char * str , char disc , char * first_receipt ,... )
*
* 文字列を指定したディスクリプタで分割します。
* 分割された文字列(ピース)は可変数の引数に格納されます。
* ピースを受け取る文字列変数領域がピースより短い場合の動作は予測できません。
*
* 返り値
* 実際分割したピースの数
*
*/
int
sbSplit(char * str , char disc , char * first_receipt ,... )
{
int inci,starti=0,ci,j;
char * y;
va_list ap;
if( str==0 ) return 0;
if( disc == null ) return 1;
ci = 0;
va_start( ap , first_receipt );
y = first_receipt;
while(( inci = sbIndex( str + starti , disc )) >= 0 ){
for( j = 0 ; j < inci ; j++) *( y + j ) = *( str + starti + j );
*( y + j )=0; /* string end */
starti += j + 1;
++ci;
if (( y = va_arg(ap,char *)) == 0) break;
}
if( y != 0 ){
j = 0;
while(( *( y + j ) = *( str + starti + j )) != null ) ++j;
while( va_arg(ap,char *)!=0 ) continue;
}
va_end(ap);
return ci+1;
}
/*
第一引数に第二引数以降の文字列を連結します。
第一引数のバッファ が不足しているときの動作は
予測できません。
返り値
連結文字列のサイズ
tech
va_list の仕組みを良く知らずに使っているので
動作があやしい( 可変引数の最後に変てこなアドレスへの
ポインタが入ってしまうことがある)ため
最後の引数にNULSTRを指定して下さい。
これにより動作が確実になります。
*/
int
sbJoin(char * receipt , ... )
{
char * y , * store;
int i;
va_list ap;
va_start( ap , receipt );
store = receipt;
receipt += sbLength(store);
y = va_arg(ap,char *);
while( y != null ){
if( *y == 0 ) break;
for(i=0;i<sbLength(y);i++)
{
*receipt = *(y+i);
receipt++;
}
y = va_arg(ap,char *);
}
*receipt = 0;
va_end(ap);
return sbLength( store );
}
void *
sbShift( void **array , int *size )
{
void * store;
int i,max;
if (array == 0) return null;
max = *size;
if ( max>0 ){
store = *array;
for (i=1;i<max;++i) *(array + i - 1) = *(array + i);
--(*size);
}else{
store = null;
}
return store;
}
int
sbPop(void)
{
--sb_stck;
return sb_stack[sb_stck+1];
}
void
sbPush(int i)
{
sb_stack[++sb_stck] = i;
}
long
sbStr2Long(char * str)
{
if (str == 0) return 0;
if( *str == null ){
return 0;
}else{
return strtol( str , (char **)null, 0);
}
}
char
debug_wait(int i)
{
printf( "now fleezed on %d please return to resume.." , i );
return getchar();
}
FILE *
sbBinOpen(char * filename , char * mode)
{
FILE * p;
char str[64],mstr[16];
sprintf(mstr,"%sb",mode);
p = fopen( filename , mstr);
if( p == NULL )
{
sprintf(str,"can't open %s ",filename);
sbErrStop(str);
}
return p;
}
#define FILE_OPEN_MAX 255
static int openi = 0;
FILE *openFileHandleStack[256];
FILE *
sbOpen(char * filename , char * mode)
{
FILE * p;
char str[64];
p = fopen( filename , mode);
if( p == NULL )
{
sprintf(str,"can't open %s ",filename);
sbErrStop(str);
}
openFileHandleStack[openi++] = p;
return p;
}
s32
sbClose(FILE * p)
{
s32 i;
fclose(p);
FORI(openi)
{
if(openFileHandleStack[i] == p)
{
openi--;
openFileHandleStack[i] = openFileHandleStack[openi-1];
return -1;
}
}
return 0;
}
void
sbPutLong(FILE * p ,int num)
{
char y;
int i;
for (i=3;i>=0;i--){
y = (char)(( num >> (i*4) ) & 0xF);
fputc( y , p );
}
}
void
sbErrStopSSI(char * str ,char * s1 ,char * s2 ,int _i ,int stop)
{
char buf[256];
s32 i;
FORI(openi) fclose(openFileHandleStack[i]);
sprintf( buf , "\n%s # %s # %6d # %c \n" ,str,s1,_i,*s2);
fprintf( stdout , buf );
if(stop) exit(0);
}
void
sbErrStopIIS(int linenum ,int i, char * s, int stop)
{
char buf[256];
s32 j;
if( s == 0 ) s = NULSTR;
sprintf( buf , "ES %4d : %8d %s\n" ,linenum,i,s);
fprintf( stdout , buf );
if(stop)
{
FORJ(openi) fclose(openFileHandleStack[j]);
exit(0);
}
}
s32
sbAssertIIS(int linenum ,int i, char * s, int stop)
{
if(!i) sbErrStopIIS(linenum, i, s ,stop);
return !i;
}