-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.c
875 lines (773 loc) · 26.6 KB
/
string.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
/****************************************************************************
*
* This code is Public Domain.
*
* ========================================================================
*
* Description: string macro processing routines.
*
* functions:
* - CatStrDir handle CATSTR directive ( also TEXTEQU )
* - SetTextMacro handle EQU directive if expression is text
* - SubStrDir handle SUBSTR directive
* - SizeStrDir handle SIZESTR directive
* - InStrDir handle INSTR directive
* - CatStrFunc handle @CatStr function
* - SubStrFunc handle @SubStr function
* - SizeStrFunc handle @SizeStr function
* - InStrFunc handle @InStr function
*
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "expreval.h"
#include "equate.h"
#include "input.h"
#include "tokenize.h"
#include "macro.h"
#include "condasm.h"
#include "fastpass.h"
#include "listing.h"
#ifdef DEBUG_OUT
static uint_32 catstrcnt;
static uint_32 substrcnt;
static uint_32 sizstrcnt;
static uint_32 instrcnt;
static uint_32 equcnt;
#endif
/* generic parameter names. In case the parameter name is
* displayed in an error message ("required parameter %s missing")
* v2.05: obsolete
*/
//static const char * parmnames[] = {"p1","p2","p3"};
void TextItemError( struct asm_tok *item )
/****************************************/
{
if ( item->token == T_STRING && *item->string_ptr == '<' ) {
EmitError( MISSING_ANGLE_BRACKET_OR_BRACE_IN_LITERAL );
return;
}
/* v2.05: better error msg if (text) symbol isn't defined */
if ( item->token == T_ID ) {
struct asym *sym = SymSearch( item->string_ptr );
if ( sym == NULL || sym->state == SYM_UNDEFINED ) {
EmitErr( SYMBOL_NOT_DEFINED, item->string_ptr );
return;
}
}
EmitError( TEXT_ITEM_REQUIRED );
return;
}
/* CATSTR directive.
* defines a text equate
* syntax <name> CATSTR [<string>,...]
* TEXTEQU is an alias for CATSTR
*/
ret_code CatStrDir( int i, struct asm_tok tokenarray[] )
/******************************************************/
{
struct asym *sym;
int count;
char *p;
/* struct expr opndx; */
#ifdef DEBUG_OUT
catstrcnt++;
#endif
DebugMsg1(("CatStrDir(%u) enter\n", i ));
#if 0 /* can't happen */
/* syntax must be <id> CATSTR textitem[,textitem,...] */
if ( i != 1 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
if ( tokenarray[0].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[0].string_ptr );
return( ERROR );
}
#endif
i++; /* go past CATSTR/TEXTEQU */
/* v2.08: don't copy to temp buffer */
//*StringBufferEnd = NULLC;
/* check correct syntax and length of items */
for ( count = 0; i < Token_Count; ) {
DebugMsg1(("CatStrDir(%s): item[%u]=%s delim=0x%x\n", tokenarray[0].string_ptr, i, tokenarray[i].string_ptr, tokenarray[i].string_delim ));
if ( tokenarray[i].token != T_STRING || tokenarray[i].string_delim != '<' ) {
DebugMsg(("CatStrDir: error, not a <>-literal: %s\n", tokenarray[i].tokpos ));
TextItemError( &tokenarray[i] );
return( ERROR );
}
/* v2.08: using tokenarray.stringlen is not quite correct, since some chars
* are stored in 2 bytes (!) */
if ( ( count + tokenarray[i].stringlen ) >= MAX_LINE_LEN ) {
DebugMsg(("CatStrDir: error, literal too long: %u + %u >= %u\n", count, tokenarray[i].stringlen, MAX_LINE_LEN ));
EmitError( STRING_OR_TEXT_LITERAL_TOO_LONG );
return( ERROR );
}
/* v2.08: don't copy to temp buffer */
//strcpy( StringBufferEnd + count, tokenarray[i].string_ptr );
count = count + tokenarray[i].stringlen;
i++;
if ( ( tokenarray[i].token != T_COMMA ) &&
( tokenarray[i].token != T_FINAL ) ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
i++;
}
sym = SymSearch( tokenarray[0].string_ptr );
if ( sym == NULL ) {
sym = SymCreate( tokenarray[0].string_ptr );
DebugMsg1(( "CatStrDir: new symbol %s created\n", sym->name));
} else if( sym->state == SYM_UNDEFINED ) {
/* v2.01: symbol has been used already. Using
* a textmacro before it has been defined is
* somewhat problematic.
*/
sym_remove_table( &SymTables[TAB_UNDEF], (struct dsym *)sym );
#if FASTPASS
SkipSavedState(); /* further passes must be FULL! */
#endif
EmitWarn( 2, TEXT_MACRO_USED_PRIOR_TO_DEFINITION, sym->name );
} else if( sym->state != SYM_TMACRO ) {
/* it is defined as something else, get out */
DebugMsg(( "CatStrDir(%s) exit, symbol redefinition\n", sym->name));
EmitErr( SYMBOL_REDEFINITION, sym->name );
return( ERROR );
}
sym->state = SYM_TMACRO;
sym->isdefined = TRUE;
#if FASTMEM==0
if ( sym->string_ptr )
LclFree( sym->string_ptr );
sym->string_ptr = (char *)LclAlloc( count + 1 );
#else
/* v2.08: reuse string space if fastmem is on */
if ( sym->total_size < ( count+1 ) ) {
LclFree( sym->string_ptr ); /* is a noop if fastmem is on */
sym->string_ptr = (char *)LclAlloc( count + 1 );
sym->total_size = count + 1;
}
#endif
/* v2.08: don't use temp buffer */
//memcpy( sym->string_ptr, StringBufferEnd, count + 1 );
for ( i = 2, p = sym->string_ptr; i < Token_Count; i += 2 ) {
memcpy( p, tokenarray[i].string_ptr, tokenarray[i].stringlen );
p += tokenarray[i].stringlen;
}
*p = NULLC;
DebugMsg1(("CatStrDir(%s) (new) value: >%s<\n", sym->name, sym->string_ptr ));
if ( ModuleInfo.list )
LstWrite( LSTTYPE_EQUATE, 0, sym );
return( NOT_ERROR );
}
/*
* used by EQU if the value to be assigned to a symbol is text.
*/
struct asym *SetTextMacro( struct asm_tok tokenarray[], struct asym *sym, const char *name, const char *value )
/*************************************************************************************************************/
{
int count;
#ifdef DEBUG_OUT
equcnt++;
#endif
#if 0 /* FASTPASS */
/* there's no need to set the value if FASTPASS is on, because
* the input are just preprocessed lines.
* this check is probably obsolete by now, since it won't be called
* ever if pass > 1. Also, even with fastpass it's sometimes necessary
* to do a full second pass, including source preprocessing.
*/
if ( Parse_Pass != PASS_1 )
return( sym );
#endif
if ( sym == NULL )
sym = SymCreate( name );
else if ( sym->state == SYM_UNDEFINED ) {
sym_remove_table( &SymTables[TAB_UNDEF], (struct dsym *)sym );
#if FASTPASS
/* the text macro was referenced before being defined.
* this is valid usage, but it requires a full second pass.
* just simply deactivate the fastpass feature for this module!
*/
SkipSavedState();
#endif
EmitWarn( 2, TEXT_MACRO_USED_PRIOR_TO_DEFINITION, sym->name );
} else if ( sym->state != SYM_TMACRO ) {
EmitErr( SYMBOL_REDEFINITION, name );
return( NULL );
}
sym->state = SYM_TMACRO;
sym->isdefined = TRUE;
if ( tokenarray[2].token == T_STRING && tokenarray[2].string_delim == '<' && tokenarray[3].token == T_FINAL ) {
value = tokenarray[2].string_ptr;
count = tokenarray[2].stringlen;
} else {
char *p = StringBufferEnd;
/*
the original source is used, since the tokenizer has
deleted some information. it's important to double '!' found inside
the string.
*/
//while ( isspace( *value ) ) value++; /* probably obsolete */
count = strlen( value );
/* skip trailing spaces */
if ( count ) {
for ( ; count; count-- )
if ( isspace( *( value + count - 1 ) ) == FALSE )
break;
}
for ( ; count; count-- ) {
if ( *value == '!' || *value == '<' || *value == '>' )
*p++ = '!';
*p++ = *value++;
}
*p = NULLC;
value = StringBufferEnd;
count = p - StringBufferEnd;
}
#if FASTMEM==0
if ( sym->string_ptr )
LclFree( sym->string_ptr );
sym->string_ptr = (char *)LclAlloc( count + 1 );
#else
if ( sym->total_size < ( count + 1 ) ) {
LclFree( sym->string_ptr ); /* is a noop if fastmem is on */
sym->string_ptr = (char *)LclAlloc( count + 1 );
sym->total_size = count + 1;
}
#endif
memcpy( sym->string_ptr, value, count + 1 );
DebugMsg1(( "SetTextMacro(%s): value is >%s<, exit\n", sym->name, sym->string_ptr ));
return( sym );
}
/* create a (predefined) text macro.
* used to create @code, @data, ...
*/
struct asym *AddPredefinedText( const char *name, const char *value )
/*******************************************************************/
{
struct asym *sym;
DebugMsg1(("AddPredefinedText(%s): >%s<\n", name, value ));
/* v2.08: ignore previous setting */
if ( NULL == ( sym = SymSearch( name ) ) )
sym = SymCreate( name );
sym->state = SYM_TMACRO;
sym->isdefined = TRUE;
sym->predefined = TRUE;
sym->string_ptr = (char *)value;
/* to ensure that a new buffer is used if the string is modified */
sym->total_size = 0;
return( sym );
}
/* SubStr()
* defines a text equate.
* syntax: name SUBSTR <string>, pos [, size]
*/
ret_code SubStrDir( int i, struct asm_tok tokenarray[] )
/******************************************************/
{
struct asym *sym;
char *name;
char *p;
char *newvalue;
int pos;
int size;
int cnt;
bool chksize;
struct expr opndx;
DebugMsg1(("SubStrDir entry\n"));
#ifdef DEBUG_OUT
substrcnt++;
#endif
/* at least 5 items are needed
* 0 1 2 3 4 5 6
* ID SUBSTR SRC_ID , POS [, LENGTH]
*/
#if 0 /* can't happen */
if ( i != 1 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
if ( tokenarray[0].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[0].string_ptr );
return( ERROR );
}
#endif
name = tokenarray[0].string_ptr;
i++; /* go past SUBSTR */
/* third item must be a string */
if ( tokenarray[i].token != T_STRING || tokenarray[i].string_delim != '<' ) {
DebugMsg(("SubStrDir: error, no text item\n"));
TextItemError( &tokenarray[i] );
return( ERROR );
}
p = tokenarray[i].string_ptr;
i++;
DebugMsg1(("SubStrDir(%s): src=>%s<\n", name, p));
if ( tokenarray[i].token != T_COMMA ) {
EmitError( EXPECTING_COMMA );
return( ERROR );
}
i++;
/* get pos, must be a numeric value and > 0 */
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR ) {
DebugMsg(("SubStrDir(%s): invalid pos value\n", name));
return( ERROR );
}
/* v2.04: "string" constant allowed as second argument */
//if ( opndx.kind != EXPR_CONST || opndx.string != NULL ) {
if ( opndx.kind != EXPR_CONST ) {
DebugMsg(("SubStrDir(%s): pos value is not a constant\n", name));
EmitError( CONSTANT_EXPECTED );
return( ERROR );
}
/* pos is expected to be 1-based */
pos = opndx.value;
if ( pos <= 0 ) {
EmitError( POSITIVE_VALUE_EXPECTED );
return( ERROR );
}
if ( tokenarray[i].token != T_FINAL ) {
if ( tokenarray[i].token != T_COMMA ) {
EmitError( EXPECTING_COMMA );
return( ERROR );
}
i++;
/* get size, must be a constant */
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR ) {
DebugMsg(("SubStrDir(%s): invalid size value\n", name));
return( ERROR );
}
/* v2.04: string constant ok */
//if ( opndx.kind != EXPR_CONST || opndx.string != NULL ) {
if ( opndx.kind != EXPR_CONST ) {
DebugMsg(("SubStrDir(%s): size value is not a constant\n", name));
EmitError( CONSTANT_EXPECTED );
return( ERROR );
}
size = opndx.value;
if ( tokenarray[i].token != T_FINAL ) {
DebugMsg(("SubStrDir(%s): additional items found\n", name));
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
if ( size < 0 ) {
EmitError( COUNT_MUST_BE_POSITIVE_OR_ZERO );
return( ERROR );
}
chksize = TRUE;
} else {
size = -1;
chksize = FALSE;
}
cnt = pos;
/* position p to start of substring */
for ( pos--; pos > 0 && *p ; pos--, p++ )
if ( *p == '!' && *(p+1) != NULLC )
p++;
if ( *p == NULLC ) {
EmitErr( INDEX_PAST_END_OF_STRING, cnt );
return( ERROR );
}
if ( *p == '!' && *(p+1) != NULLC )
p++;
for ( newvalue = p, cnt = size; *p && cnt; cnt--, p++ )
if ( *p == '!' && *(p+1) != NULLC )
p++;
/* v2.04: check added */
if ( chksize && cnt ) {
EmitError( COUNT_VALUE_TOO_LARGE );
return( ERROR );
}
sym = SymSearch( name );
/* if we've never seen it before, put it in */
if( sym == NULL ) {
sym = SymCreate( name );
} else if( sym->state == SYM_UNDEFINED ) {
/* it was referenced before being defined. This is
* a bad idea for preprocessor text items, because it
* will require a full second pass!
*/
sym_remove_table( &SymTables[TAB_UNDEF], (struct dsym *)sym );
#if FASTPASS
SkipSavedState();
EmitWarn( 2, TEXT_MACRO_USED_PRIOR_TO_DEFINITION, sym->name );
#endif
} else if( sym->state != SYM_TMACRO ) {
/* it is defined as something incompatible, get out */
DebugMsg(( "SubStrDir(%s) error, incompatible type\n", sym->name));
EmitErr( SYMBOL_REDEFINITION, sym->name );
return( ERROR );
}
sym->state = SYM_TMACRO;
sym->isdefined = TRUE;
size = p - newvalue;
p = newvalue;
#if FASTMEM==0
if ( sym->string_ptr )
LclFree( sym->string_ptr );
sym->string_ptr = (char *)LclAlloc( size + 1 );
#else
if ( sym->total_size < ( size + 1 ) ) {
LclFree( sym->string_ptr );
sym->string_ptr = LclAlloc ( size + 1 );
sym->total_size = size + 1;
}
#endif
memcpy( sym->string_ptr, p, size );
*(sym->string_ptr + size) = NULLC;
DebugMsg1(("SubStrDir(%s): result=>%s<\n", sym->name, sym->string_ptr ));
LstWrite( LSTTYPE_EQUATE, 0, sym );
return( NOT_ERROR );
}
/* SizeStr()
* defines a numeric variable which contains size of a string
*/
ret_code SizeStrDir( int i, struct asm_tok tokenarray[] )
/*******************************************************/
{
struct asym *sym;
int sizestr;
DebugMsg1(("SizeStrDir entry\n"));
#ifdef DEBUG_OUT
sizstrcnt++;
#endif
if ( i != 1 ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
#if 0 /* this is checked in ParseLine() */
if ( tokenarray[0].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[0].string_ptr );
return( ERROR );
}
#endif
if ( tokenarray[2].token != T_STRING || tokenarray[2].string_delim != '<' ) {
TextItemError( &tokenarray[2] );
return( ERROR );
}
if ( Token_Count > 3 ) {
DebugMsg(("SizeStrDir: syntax error, name=%s, Token_Count=%u\n", tokenarray[0].string_ptr, Token_Count));
EmitErr( SYNTAX_ERROR_EX, tokenarray[3].string_ptr );
return( ERROR );
}
sizestr = GetLiteralValue( StringBufferEnd, tokenarray[2].string_ptr );
if ( sym = CreateVariable( tokenarray[0].string_ptr, sizestr ) ) {
DebugMsg1(("SizeStrDir(%s) exit, value=%u\n", tokenarray[0].string_ptr, sizestr));
LstWrite( LSTTYPE_EQUATE, 0, sym );
return( NOT_ERROR );
}
return( ERROR );
}
/* InStr()
* defines a numeric variable which contains position of substring.
* syntax:
* name INSTR [pos,]string, substr
*/
ret_code InStrDir( int i, struct asm_tok tokenarray[] )
/*****************************************************/
{
struct asym *sym;
int sizestr;
int j;
/* int commas; */
char *src;
char *p;
char *q;
char *string1;
struct expr opndx;
int start = 1;
int strpos;
DebugMsg1(("InStrDir entry\n"));
#ifdef DEBUG_OUT
instrcnt++;
#endif
if ( i != 1) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
#if 0 /* this is checked in ParseLine() */
if ( tokenarray[0].token != T_ID ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[0].string_ptr );
return( ERROR );
}
#endif
i++; /* go past INSTR */
if ( tokenarray[i].token != T_STRING || tokenarray[i].string_delim != '<' ) {
if ( EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 ) == ERROR )
return( ERROR );
if ( opndx.kind != EXPR_CONST ) {
EmitError( CONSTANT_EXPECTED );
return( ERROR );
}
start = opndx.value;
if ( start <= 0 ) {
/* v2.05: don't change the value. if it's invalid, the result
* is to be 0. Emit a level 3 warning instead.
*/
//start = 1;
EmitWarn( 3, POSITIVE_VALUE_EXPECTED );
}
if ( tokenarray[i].token != T_COMMA ) {
EmitError( EXPECTING_COMMA );
return( ERROR );
}
i++; /* skip comma */
}
if ( tokenarray[i].token != T_STRING || tokenarray[i].string_delim != '<' ) {
TextItemError( &tokenarray[i] );
return( ERROR );
}
/* to compare the strings, the "visible" format is needed, since
* the possible '!' operators inside the strings is optional and
* must be ignored.
*/
src = StringBufferEnd;
sizestr = GetLiteralValue( src, tokenarray[i].string_ptr );
DebugMsg1(("InStrDir: first string >%s< \n", src ));
if ( start > sizestr ) {
EmitErr( INDEX_PAST_END_OF_STRING, start );
return( ERROR );
}
p = src + start - 1;
i++;
if ( tokenarray[i].token != T_COMMA ) {
EmitError( EXPECTING_COMMA );
return( ERROR );
}
i++;
if ( tokenarray[i].token != T_STRING || tokenarray[i].string_delim != '<' ) {
TextItemError( &tokenarray[i] );
return( ERROR );
}
q = GetAlignedPointer( src, sizestr );
j = GetLiteralValue( q, tokenarray[i].string_ptr );
DebugMsg1(("InStrDir: second string >%s< \n", q ));
i++;
if ( tokenarray[i].token != T_FINAL ) {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
strpos = 0;
/* v2.05: check for start > 0 added */
/* v2.08: check for j > 0 added */
if ( ( start > 0 ) && ( sizestr >= j ) && j && ( string1 = strstr( p, q ) ))
strpos = string1 - src + 1;
if ( sym = CreateVariable( tokenarray[0].string_ptr, strpos ) ) {
DebugMsg1(("InStrDir(%s) exit, value=%u\n", tokenarray[0].string_ptr, strpos));
LstWrite( LSTTYPE_EQUATE, 0, sym );
return ( NOT_ERROR );
}
return( ERROR );
}
/* internal @CatStr macro function */
static ret_code CatStrFunc( struct macro_instance *mi, char *buffer, struct asm_tok tokenarray[] )
/************************************************************************************************/
{
#ifdef DEBUG_OUT
int cnt = 0;
#endif
int i;
char *p;
DebugMsg1(("@CatStr( %s )\n", mi->parm_array[0] ? mi->parm_array[0] : "NULL" ));
for ( p = mi->parm_array[0]; mi->parmcnt; mi->parmcnt-- ) {
DebugMsg1(("@CatStr.%u: >%s<\n", cnt++, p ));
i = strlen( p );
memcpy( buffer, p, i );
p = GetAlignedPointer( p, i );
buffer += i;
}
*buffer = NULLC;
return( NOT_ERROR );
}
/* convert string to a number */
static ret_code GetNumber( char *string, int *pi, struct asm_tok tokenarray[] )
/*****************************************************************************/
{
struct expr opndx;
int i;
int last;
last = Tokenize( string, Token_Count+1, tokenarray, TOK_RESCAN );
i = Token_Count+1;
if( EvalOperand( &i, tokenarray, last, &opndx, 0 ) == ERROR ) {
return( ERROR );
}
if( opndx.kind != EXPR_CONST || opndx.quoted_string != NULL || tokenarray[i].token != T_FINAL ) {
EmitErr( SYNTAX_ERROR_EX, string );
return( ERROR );
}
*pi = opndx.value;
return( NOT_ERROR );
}
/* internal @InStr macro function
* the result is returned as string in current radix
*/
static ret_code InStrFunc( struct macro_instance *mi, char *buffer, struct asm_tok tokenarray[] )
/***********************************************************************************************/
{
int pos = 1;
char *p;
uint_32 found;
DebugMsg1(("@InStr( %s, %s, %s)\n",
mi->parm_array[0] ? mi->parm_array[0] : "",
mi->parm_array[1] ? mi->parm_array[1] : "",
mi->parm_array[2] ? mi->parm_array[2] : "" ));
/* init buffer with "0" */
*buffer = '0';
*(buffer+1) = NULLC;
if ( mi->parm_array[0] ) {
if ( GetNumber( mi->parm_array[0], &pos, tokenarray ) == ERROR )
return( ERROR );
if ( pos == 0 )
pos++;
}
if ( pos > strlen( mi->parm_array[1] ) ) {
EmitErr( INDEX_PAST_END_OF_STRING, pos );
return( ERROR );
}
/* v2.08: if() added, empty searchstr is to return 0 */
if ( *(mi->parm_array[2]) != NULLC ) {
p = strstr( mi->parm_array[1] + pos - 1, mi->parm_array[2] );
if ( p ) {
found = p - mi->parm_array[1] + 1;
myltoa( found, buffer, ModuleInfo.radix, FALSE, TRUE );
}
}
DebugMsg1(( "@InStrFunc()=>%s<\n", buffer ));
return( NOT_ERROR );
}
/* internal @SizeStr macro function
* the result is returned as string in current radix
*/
static ret_code SizeStrFunc( struct macro_instance *mi, char *buffer, struct asm_tok tokenarray[] )
/*************************************************************************************************/
{
DebugMsg1(("@SizeStr(%s)\n", mi->parm_array[0] ? mi->parm_array[0] : "" ));
if ( mi->parm_array[0] )
myltoa( strlen( mi->parm_array[0] ), buffer, ModuleInfo. radix, FALSE, TRUE );
else {
buffer[0] = '0';
buffer[1] = NULLC;
}
return( NOT_ERROR );
}
/* internal @SubStr macro function */
static ret_code SubStrFunc( struct macro_instance *mi, char *buffer, struct asm_tok tokenarray[] )
/************************************************************************************************/
{
int pos;
int size;
char *src = mi->parm_array[0];
DebugMsg1(("@SubStr( %s, %s, %s)\n",
src ? src : "",
mi->parm_array[1] ? mi->parm_array[1] : "",
mi->parm_array[2] ? mi->parm_array[2] : "" ));
if ( GetNumber( mi->parm_array[1], &pos, tokenarray ) == ERROR )
return( ERROR );
if (pos <= 0)
pos = 1;
size = strlen( src );
if ( pos > size ) {
EmitErr( INDEX_PAST_END_OF_STRING, pos );
return( ERROR );
}
if ( mi->parm_array[2] ) {
if ( GetNumber( mi->parm_array[2], &size, tokenarray ) == ERROR )
return( ERROR );
if ( size < 0 ) {
EmitError( COUNT_MUST_BE_POSITIVE_OR_ZERO );
return( ERROR );
}
} else {
size = size - pos + 1;
}
for( src = src+pos-1 ; size && *src ; size-- )
*buffer++ = *src++;
*buffer = NULLC;
if ( size ) {
EmitError( COUNT_VALUE_TOO_LARGE );
return( ERROR );
}
return( NOT_ERROR );
}
/* string macro initialization
* this proc is called once per module
*/
void StringInit( void )
/*********************/
{
int i;
struct dsym *macro;
DebugMsg(( "StringInit() enter\n" ));
#ifdef DEBUG_OUT
catstrcnt = 0;
substrcnt = 0;
sizstrcnt = 0;
instrcnt = 0;
equcnt = 0;
#endif
/* add @CatStr() macro func */
macro = CreateMacro( "@CatStr" );
macro->sym.isdefined = TRUE;
macro->sym.predefined = TRUE;
macro->sym.func_ptr = CatStrFunc;
macro->sym.isfunc = TRUE;
/* v2.08: @CatStr() changed to VARARG */
macro->sym.mac_vararg = TRUE;
macro->e.macroinfo->parmcnt = 1;
macro->e.macroinfo->parmlist = LclAlloc( sizeof( struct mparm_list ) * 1 );
macro->e.macroinfo->parmlist[0].deflt = NULL;
macro->e.macroinfo->parmlist[0].required = FALSE;
/* add @InStr() macro func */
macro = CreateMacro( "@InStr" );
macro->sym.isdefined = TRUE;
macro->sym.predefined = TRUE;
macro->sym.func_ptr = InStrFunc;
macro->sym.isfunc = TRUE;
macro->e.macroinfo->parmcnt = 3;
macro->e.macroinfo->autoexp = 1; /* param 1 (pos) is expanded */
macro->e.macroinfo->parmlist = LclAlloc(sizeof( struct mparm_list) * 3);
for (i = 0; i < 3; i++) {
macro->e.macroinfo->parmlist[i].deflt = NULL;
//macro->e.macroinfo->parmlist[i].label = parmnames[i];
macro->e.macroinfo->parmlist[i].required = (i != 0);
}
/* add @SizeStr() macro func */
macro = CreateMacro( "@SizeStr" );
macro->sym.isdefined = TRUE;
macro->sym.predefined = TRUE;
macro->sym.func_ptr = SizeStrFunc;
macro->sym.isfunc = TRUE;
macro->e.macroinfo->parmcnt = 1;
macro->e.macroinfo->parmlist = LclAlloc(sizeof( struct mparm_list));
macro->e.macroinfo->parmlist[0].deflt = NULL;
//macro->e.macroinfo->parmlist[0].label = parmnames[0];
/* macro->e.macroinfo->parmlist[0].required = TRUE; */
/* the string parameter is NOT required, '@SizeStr()' is valid */
macro->e.macroinfo->parmlist[0].required = FALSE;
/* add @SubStr() macro func */
macro = CreateMacro( "@SubStr" );
macro->sym.isdefined = TRUE;
macro->sym.predefined = TRUE;
macro->sym.func_ptr = SubStrFunc;
macro->sym.isfunc = TRUE;
macro->e.macroinfo->parmcnt = 3;
macro->e.macroinfo->autoexp = 2 + 4; /* param 2 (pos) and 3 (size) are expanded */
macro->e.macroinfo->parmlist = LclAlloc(sizeof( struct mparm_list) * 3);
for (i = 0; i < 3; i++) {
macro->e.macroinfo->parmlist[i].deflt = NULL;
//macro->e.macroinfo->parmlist[i].label = parmnames[i];
macro->e.macroinfo->parmlist[i].required = (i < 2);
}
return;
}
#ifdef DEBUG_OUT
void StringFini( void )
/*********************/
{
if ( Options.quiet == FALSE )
printf("invokation CATSTR=%u SUBSTR=%u SIZESTR=%u INSTR=%u EQU(text)=%u\n", catstrcnt, substrcnt, sizstrcnt, instrcnt, equcnt );
}
#endif