-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathparse.c
1261 lines (1089 loc) · 29.1 KB
/
parse.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
/* parse.c - global parser support functions */
/* (c) in 2009-2024 by Volker Barthelmann and Frank Wille */
#include "vasm.h"
int esc_sequences; /* do not handle escape sequences by default */
int nocase_macros; /* macro names are case-insensitive */
int maxmacparams = MAXMACPARAMS;
int maxmacrecurs = MAXMACRECURS;
int msource_disable; /* true: disable source level debugging within macro */
#ifndef MACROHTABSIZE
#define MACROHTABSIZE 0x800
#endif
static hashtable *macrohash;
#ifndef STRUCTHTABSIZE
#define STRUCTHTABSIZE 0x800
#endif
static hashtable *structhash;
static macro *first_macro;
static macro *cur_macro;
static struct namelen *enddir_list;
static size_t enddir_minlen;
static struct namelen *macrdir_list;
static struct namelen *reptdir_list;
static int rept_cnt = -1;
static source *rept_defsrc;
static int rept_defline;
static char *rept_start,*rept_name,*rept_vals;
static section *cur_struct;
static section *struct_prevsect;
char *escape(char *s,char *code)
{
char dummy;
int cnt;
if (*s++ != '\\')
ierror(0);
if (code == NULL)
code = &dummy;
if (!esc_sequences) {
*code='\\';
return s;
}
switch (*s) {
case 'b':
*code='\b';
return s+1;
case 'f':
*code='\f';
return s+1;
case 'n':
*code='\n';
return s+1;
case 'r':
*code='\r';
return s+1;
case 't':
*code='\t';
return s+1;
case '\\':
*code='\\';
return s+1;
case '\"':
*code='\"';
return s+1;
case '\'':
*code='\'';
return s+1;
case 'e':
*code=27;
return s+1;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
*code=0;
cnt=0;
while (*s>='0' && *s<='9' && ++cnt<=3) {
*code = *code*8 + *s-'0';
s++;
}
return s;
case 'x': case 'X':
*code=0;
cnt=0;
s++;
while (++cnt<=2 && ((*s>='0' && *s<='9') ||
(*s>='a' && *s<='f') || (*s>='A' && *s<='F'))) {
if (*s>='0' && *s<='9')
*code = *code*16 + *s-'0';
else if (*s>='a' && *s<='f')
*code = *code*16 + *s-'a' + 10;
else
*code = *code*16 + *s -'A' + 10;
s++;
}
return s;
default:
general_error(35,*s);
return s;
}
}
char *cut_trail_blanks(char *s)
{
while (isspace((unsigned char)*(s-1))) /* cut trailing blanks */
s--;
return s;
}
strbuf *parse_name(int n,char **start)
/* Parses a quoted or unquoted name-string and returns a pointer to a
null-terminated string in one of two temporary buffers. */
{
static strbuf buf[2];
char *s = *start;
char c,*name;
if (*s=='\"' || *s=='\'') {
c = *s++;
name = s;
while (*s && *s!=c)
s++;
cutstr(&buf[n],name,s-name);
if (*s)
s = skip(s+1);
}
#ifdef VASM_CPU_M68K
else if (*s=='<') {
s++;
name = s;
while (*s && *s!='>')
s++;
cutstr(&buf[n],name,s-name);
if (*s)
s = skip(s+1);
}
#endif
else {
name = s;
while (!ISEOL(s) && !isspace((unsigned char)*s) && *s!=',')
s++;
if (s != name) {
cutstr(&buf[n],name,s-name);
s = skip(s);
}
else
return NULL; /* nothing read */
}
*start = s;
return &buf[n];
}
static char *skip_eol(char *s,char *e)
{
while (s<e && *s!='\0' && *s!='\n' && *s!='\r')
s++;
return s;
}
char *skip_line(char *s)
{
while (*s!='\0')
s++;
return s;
}
char *skip_identifier(char *s)
{
char *name = s;
if (ISIDSTART(*s)) {
s++;
while (ISIDCHAR(*s))
s++;
if (s = CHKIDEND(name,s))
if (!ISBADID(name,s-name))
return s;
}
return NULL;
}
strbuf *parse_identifier(int n,char **s)
/* Parses an identifier string, as used for symbol names, and returns a
pointer to a null-terminated string in one of two temporary buffers. */
{
static strbuf buf[EXPBUFNO+1];
char *name = *s;
char *endname;
if (endname = skip_identifier(name)) {
*s = endname;
cutstr(&buf[n],name,endname-name);
return &buf[n];
}
return NULL;
}
strbuf *get_raw_string(char **str,char delim)
/* parse a raw string (i.e. no escape handling) between the given
delimitters, return NULL on error, otherwise update stream pointer */
{
static strbuf buf;
char *p = *str;
char *start;
if (*p++ != delim) {
general_error(6,delim); /* " expected */
return NULL;
}
start = p;
while (*p&&*p!=delim)
p++;
if (!*p) {
general_error(6,delim); /* " expected */
return NULL;
}
cutstr(&buf,start,p-start);
*str = ++p;
return &buf;
}
char *skip_string(char *s,char delim,size_t *size)
/* skip a string, optionally store the size in bytes in size, when not NULL */
{
size_t n = 0;
if (*s != delim)
general_error(6,delim); /* " expected */
else
s++;
while (*s) {
if (*s == '\\') {
s = escape(s,NULL);
}
else {
if (*s++ == delim) {
if (*s == delim)
s++; /* allow """" to be recognized as " */
else
break;
}
}
n++;
}
if (*(s-1) != delim)
n = 1; /* missing closing-quote, so not a string, try as single-char */
if (size)
*size = n;
return s;
}
char *read_string(char *p,char *s,char delim,int width)
/* Read string contents with width bits for each character into a buffer p,
optionally starting with a delim-character, excluding the terminating
character.
When a target-byte (BITSPERBYTE) has space for multiple characters
of width, then they will be compressed into it. */
{
utaddr val = 0;
int bitcnt = 0;
char c;
if (*s == delim)
s++;
while (*s) {
if (*s == '\\') {
s = escape(s,&c);
}
else {
c = *s++;
if (c == delim) {
if (*s == delim)
s++; /* allow """" to be recognized as " */
else
break;
}
}
if (p) {
val <<= width;
val |= (uint8_t)c;
bitcnt += width;
if (bitcnt+width > BITSPERBYTE) {
size_t n = (bitcnt+BITSPERBYTE-1) / BITSPERBYTE;
setval(BIGENDIAN,p,n,val);
p += OCTETS(n);
bitcnt = 0;
}
}
}
if (bitcnt && p) {
val <<= ((BITSPERBYTE - bitcnt) / width) * width;
setval(0,p,1,val);
}
return s;
}
dblock *parse_string(char **str,char delim,int width)
{
size_t size;
dblock *db;
char *s = *str;
if (width & 7)
ierror(0);
if (ISEOL(s))
return NULL;
/* how many bytes do we need for the string? */
skip_string(s,delim,&size);
if (size == 1)
return NULL; /* not a string, so we can use eval_expr() on it */
db = new_dblock();
db->size = (size + (BITSPERBYTE / width) - 1) / (BITSPERBYTE / width);
db->data = db->size ? mymalloc(OCTETS(db->size)) : NULL;
/* now copy the string for real into the dblock */
s = read_string((char *)db->data,s,delim,width);
*str = s;
return db;
}
char *parse_symbol_strbuf(int n,char **s)
/* return ptr to a local/global symbol string in static buffer n, or NULL */
{
strbuf *name;
name = get_local_label(n,s);
if (name == NULL)
name = parse_identifier(n,s);
return name ? name->str : NULL;
}
char *parse_labeldef(char **line,int needcolon)
/* Parse a global or local label definition at the beginning of a line.
Return pointer to its allocate buffer, when valid.
A trailing colon may be required or optional, but becomes mandatory
when label is not starting at first column. */
{
char *s = *line;
char *labname;
if (isspace((unsigned char )*s)) {
s = skip(s);
needcolon = 1; /* colon required, when label doesn't start at 1st column */
}
if (labname = parse_symbol(&s)) {
s = skip(s);
if (*s == ':') {
s++;
needcolon = 0;
}
if (needcolon)
labname = NULL;
else
*line = s;
}
return labname;
}
int check_indir(char *p,char *q)
/* returns true when the whole sequence between p and q starts and ends with */
/* parentheses and there are no unbalanced parentheses within */
{
char c;
int n;
p = skip(p);
if (*p++ != '(')
return 0;
n = 1;
while (n>0 && p<q) {
c = *p++;
if (c == '(')
n++;
else if (c == ')')
n--;
}
if (p < q)
p = skip(p);
return n==0 && p>=q;
}
static struct namelen *dirlist_match(char *s,char *e,struct namelen *list)
/* check if a directive from the list matches the current source location */
{
char *name;
size_t len;
if (!ISIDSTART(*s) || (!isspace((unsigned char )*(s-1)) && *(s-1)!='\0'))
return NULL; /* cannot be start of directive */
name = s++;
while (s<e && ISIDCHAR(*s))
s++;
if (!isspace((unsigned char )*s) && *s!='\0')
return NULL; /* cannot be end of directive */
while (len = list->len) {
if (s-name==len && !strnicmp(name,list->name,len))
return list;
list++;
}
return NULL;
}
static size_t dirlist_minlen(struct namelen *list)
{
size_t minlen;
if (list == NULL)
ierror(0);
for (minlen=list->len; list->len; list++) {
if (list->len < minlen)
minlen = list->len;
}
return minlen;
}
/* add a skipped macro/repeat line to the listing */
static void list_skipped_line(char *p)
{
listing *new = new_listing(cur_src,cur_src->line);
size_t len = p - cur_src->srcptr;
if (len>0 && (*(p-1)=='\n' || *(p-1)=='\r'))
len--;
if (len >= MAXLISTSRC)
len = MAXLISTSRC - 1;
memcpy(new->txt,cur_src->srcptr,len);
new->txt[len] = '\0';
}
/* return the line number of the last real source text instance, i.e.
not the line within a macro or repetition */
int real_line(void)
{
source *src = cur_src;
int line = src->line;
while (src->num_params >= 0) {
line = src->parent_line;
src = src->parent;
if (src == NULL)
ierror(0); /* macro must have a parent */
}
return line;
}
void new_repeat(int rcnt,char *name,char *vals,
struct namelen *reptlist,struct namelen *endrlist)
{
if (cur_macro==NULL && cur_src!=NULL && enddir_list==NULL) {
enddir_list = endrlist;
enddir_minlen = dirlist_minlen(endrlist);
reptdir_list = reptlist;
rept_start = cur_src->srcptr;
rept_name = name ? mystrdup(name) : NULL;
rept_vals = vals;
rept_cnt = rcnt; /* also REPT_IRP or REPT_IRPC */
/* get start-line of repetition in the last real source text */
if (cur_src->defsrc) {
rept_defsrc = cur_src->defsrc;
rept_defline = cur_src->defline + cur_src->line;
}
else {
rept_defsrc = cur_src;
rept_defline = cur_src->line;
}
}
else
ierror(0);
}
int find_macarg_name(source *src,char *name,size_t len)
{
struct macarg *ma;
int idx;
/* named macro arguments */
if (src->macro != NULL) {
for (idx=0,ma=src->argnames; ma!=NULL && idx<maxmacparams;
idx++,ma=ma->argnext) {
/* @@@ case-sensitive comparison? */
if (ma->arglen==len && strncmp(ma->argname,name,len)==0)
return idx;
}
}
/* repeat-loop iterator name */
if (src->irpname != NULL) {
if (strlen(src->irpname)==len && strncmp(src->irpname,name,len)==0) {
/* copy current iterator value to param[MAXMACPARAMS] */
src->param[MAXMACPARAMS] = src->irpvals->argname;
src->param_len[MAXMACPARAMS] = src->irpvals->arglen;
return IRPVAL;
}
}
return -1;
}
/* append a macarg object at the end of the given list */
struct macarg *addmacarg(struct macarg **list,char *start,char *end)
{
struct macarg *lastarg,*newarg;
int len = end-start;
/* count arguments */
if (lastarg = *list) {
while (lastarg->argnext)
lastarg = lastarg->argnext;
}
newarg = mymalloc(sizeof(struct macarg) + len);
newarg->argnext = NULL;
if (start != NULL) {
memcpy(newarg->argname,start,len);
newarg->argname[len] = '\0';
newarg->arglen = len;
}
else
newarg->arglen = MACARG_REQUIRED;
if (lastarg)
lastarg->argnext = newarg;
else
*list = newarg;
return newarg;
}
macro *new_macro(char *name,struct namelen *maclist,struct namelen *endmlist,
char *args)
{
hashdata data;
macro *m;
if (cur_macro==NULL && cur_src!=NULL && enddir_list==NULL) {
if (m = find_macro(name,strlen(name))) {
/* replace the old definition and warn about it */
general_error(88,m->defline,m->defsrc->name); /* macro redefinition */
rem_hashentry(macrohash,name,nocase_macros);
}
m = mymalloc(sizeof(macro));
m->name = mystrdup(name);
if (nocase_macros)
strtolower(m->name);
m->num_argnames = -1;
m->argnames = m->defaults = NULL;
m->recursions = 0;
m->vararg = -1;
m->srcdebug = !msource_disable;
/* remember the start-line of this macro definition in the real source */
if (cur_src->defsrc)
general_error(26,cur_src->name); /* macro definition inside macro */
m->defsrc = cur_src;
m->defline = cur_src->line;
/* looking for name conflicts */
if (find_name_nc(mnemohash,name,&data)) {
int idx;
m->text = cur_src->srcptr;
for (idx=data.idx;
idx<mnemonic_cnt && !stricmp(mnemonics[idx].name,name); idx++) {
if (MNEMONIC_VALID(idx)) {
m->text = NULL;
general_error(51); /* name conflicts with mnemonic */
break;
}
}
}
else if (find_name_nc(dirhash,name,&data)) {
m->text = NULL;
general_error(52); /* name conflicts with directive */
}
else
m->text = cur_src->srcptr;
cur_macro = m;
enddir_list = endmlist;
enddir_minlen = dirlist_minlen(endmlist);
macrdir_list = maclist;
rept_cnt = -1;
rept_start = NULL;
if (args) {
/* named arguments have been given */
struct macarg *ma;
char *end;
int n = 0;
m->num_argnames = 0;
args = skip(args);
while (args!=NULL && !ISEOL(args)) {
end = SKIP_MACRO_ARGNAME(args);
if (end!=NULL && end-args!=0 && m->vararg<0) {
/* add another argument name */
ma = addmacarg(&m->argnames,args,end);
m->num_argnames++;
args = skip(end);
if (end = MACRO_ARG_OPTS(m,n,ma->argname,args))
args = skip(end); /* got defaults and qualifiers */
else
addmacarg(&m->defaults,args,args); /* default is empty */
n++;
}
else {
general_error(42); /* illegal macro argument */
break;
}
args = MACRO_ARG_SEP(args); /* check for separator between names */
}
}
}
else
ierror(0);
return m;
}
macro *find_macro(char *name,int name_len)
{
hashdata data;
if (nocase_macros) {
if (!find_namelen_nc(macrohash,name,name_len,&data))
return NULL;
}
else {
if (!find_namelen(macrohash,name,name_len,&data))
return NULL;
}
return data.ptr;
}
/* check if 'name' is a known macro, then execute macro context */
int execute_macro(char *name,int name_len,char **q,int *q_len,int nq,
char *s)
{
macro *m;
source *src;
struct macarg *ma;
int n;
struct namelen param,arg;
#if MAX_QUALIFIERS>0
char *defq[MAX_QUALIFIERS];
int defq_len[MAX_QUALIFIERS];
#endif
#ifdef NO_MACRO_QUALIFIERS
char *nptr = name;
/* Instruction qualifiers are ignored for macros on this architecture.
So we have to determine the length of the mnemonic again. */
while (*nptr && !isspace((unsigned char)*nptr))
nptr++;
name_len = nptr - name;
nq = 0;
#endif
if ((m = find_macro(name,name_len)) == NULL)
return 0;
/* it's a macro: read arguments and execute it */
if (m->recursions >= maxmacrecurs) {
general_error(56,maxmacrecurs); /* maximum macro recursions reached */
return 0;
}
m->recursions++;
src = new_source(m->name,NULL,m->text,m->size);
src->macro = m;
src->defsrc = m->defsrc;
src->defline = m->defline;
src->argnames = m->argnames;
src->srcdebug = m->srcdebug;
#if MAX_QUALIFIERS>0
/* remember given qualifiers, or use the cpu's default qualifiers */
for (n=0; n<nq; n++) {
src->qual[n] = q[n];
src->qual_len[n] = q_len[n];
}
nq = set_default_qualifiers(defq,defq_len);
for (; n<nq; n++) {
src->qual[n] = defq[n];
src->qual_len[n] = defq_len[n];
}
src->num_quals = nq;
#endif
/* fill in the defaults first */
for (n=0,ma=m->defaults; n<maxmacparams; n++) {
if (ma != NULL) {
src->param[n] = ma->arglen==MACARG_REQUIRED ? NULL : ma->argname;
src->param_len[n] = ma->arglen==MACARG_REQUIRED ? 0 : ma->arglen;
ma = ma->argnext;
}
else {
src->param[n] = emptystr;
src->param_len[n] = 0;
}
}
/* read macro arguments from operand field */
s = skip(s);
n = 0;
while (!ISEOL(s) && n<maxmacparams) {
if (n>=0 && m->vararg==n) {
/* Varargs: take rest of line as argument */
char *start = s;
char *end = s;
while (!ISEOL(s)) {
s++;
if (!isspace((unsigned char)*s))
end = s; /* remember last non-blank character */
}
src->param[n] = start;
src->param_len[n] = end - start;
n++;
break;
}
else if ((s = parse_macro_arg(m,s,¶m,&arg)) != NULL) {
if (arg.len) {
/* argument selected by keyword */
if (n <= 0) {
n = find_macarg_name(src,arg.name,arg.len);
if (n >= 0) {
src->param[n] = param.name;
src->param_len[n] = param.len;
}
else
general_error(72); /* undefined macro argument name */
n = -1;
}
else
general_error(71); /* cannot mix positional and keyword arguments */
}
else {
/* argument selected by position n */
if (n >= 0) {
if (param.len > 0) {
src->param[n] = param.name;
src->param_len[n] = param.len;
}
n++;
}
else
general_error(71); /* cannot mix positional and keyword arguments */
}
}
else
break;
s = skip(s);
if (!(s = MACRO_PARAM_SEP(s))) /* check for separator between params. */
break;
}
if (m->num_argnames >= 0) {
if (n > m->num_argnames)
general_error(87,m->num_argnames); /* additional macro arguments ignored */
n = m->num_argnames; /* named arguments define number of args */
}
if (n > maxmacparams) {
general_error(27,maxmacparams); /* number of args exceeded */
n = maxmacparams;
}
src->num_params = n; /* >=0 indicates macro source */
for (n=0; n<maxmacparams; n++) {
if (src->param[n] == NULL) {
/* required, but missing */
src->param[n] = emptystr;
src->param_len[n] = 0;
general_error(73,n+1); /* required macro argument was left out */
}
}
EXEC_MACRO(src); /* syntax-module dependent initializations */
cur_src = src; /* execute! */
return 1;
}
int leave_macro(void)
{
if (cur_src->macro != NULL) {
end_source(cur_src);
return 0; /* ok */
}
general_error(36); /* no current macro to exit */
return -1;
}
/* remove an already defined macro from the hash table */
int undef_macro(char *name)
{
if (find_macro(name,strlen(name))) {
rem_hashentry(macrohash,name,nocase_macros);
return 1;
}
general_error(68); /* macro does not exist */
return 0;
}
static void start_repeat(char *rept_end)
{
char buf[MAXPATHLEN];
source *src;
char *p;
int i;
reptdir_list = NULL;
if ((rept_cnt<0 && rept_cnt!=REPT_IRP && rept_cnt!=REPT_IRPC) ||
cur_src==NULL || strlen(cur_src->name) + 24 >= MAXPATHLEN)
ierror(0);
if (rept_cnt != 0) {
sprintf(buf,"REPEAT:%s:line %d",rept_defsrc->name,rept_defline);
src = new_source(buf,NULL,rept_start,rept_end-rept_start);
src->irpname = rept_name;
src->irpvals = NULL;
src->defsrc = rept_defsrc;
src->defline = rept_defline;
#ifdef REPTNSYM
src->reptn = 0;
set_internal_abs(REPTNSYM,0);
#endif
switch (rept_cnt) {
case REPT_IRP: /* iterate with comma separated values */
p = rept_vals;
if (!*p) {
addmacarg(&src->irpvals,p,p);
src->repeat = 1;
}
else {
strbuf *buf;
src->repeat = 0;
while (buf = parse_name(0,&p)) {
addmacarg(&src->irpvals,buf->str,buf->str+buf->len);
src->repeat++;
p = skip(p);
if (*p == ',')
p = skip(p+1);
}
}
break;
case REPT_IRPC: /* iterate with each character */
p = rept_vals;
if (!*p) {
addmacarg(&src->irpvals,p,p);
src->repeat = 1;
}
else {
src->repeat = 0;
do {
while (!isspace((unsigned char )*p) && *p!=',' && !ISEOL(p)) {
addmacarg(&src->irpvals,p,p+1);
src->repeat++;
p++;
}
p = skip(p);
if (*p == ',')
p = skip(p+1);
}
while (!ISEOL(p));
}
break;
default: /* iterate rept_cnt times */
src->repeat = (unsigned long)rept_cnt;
break;
}
if (cur_src->num_params >= 0) {
/* repetition in a macro: get parameters */
src->num_params = cur_src->num_params;
for (i=0; i<src->num_params; i++) {
src->param[i] = cur_src->param[i];
src->param_len[i] = cur_src->param_len[i];
}
#if MAX_QUALIFIERS > 0
src->num_quals = cur_src->num_quals;
for (i=0; i<src->num_quals; i++) {
src->qual[i] = cur_src->qual[i];
src->qual_len[i] = cur_src->qual_len[i];
}
#endif
src->argnames = cur_src->argnames;
}
if (src->repeat == 0)
ierror(0);
cur_src = src; /* repeat it */
}
}
static void add_macro(void)
{
if (cur_macro!=NULL && cur_src!=NULL) {
if (cur_macro->text != NULL) {
hashdata data;
cur_macro->size = cur_src->srcptr - cur_macro->text;
cur_macro->next = first_macro;
first_macro = cur_macro;
data.ptr = cur_macro;
add_hashentry(macrohash,cur_macro->name,data);
}
cur_macro = NULL;
}
else
ierror(0);
}
/* copy macro parameter n to line buffer, return -1 when out of space */
int copy_macro_param(source *src,int n,char *d,int len)
{
if (n < 0) {
ierror(0);
}
else if ((n<src->num_params && n<maxmacparams) || n==IRPVAL) {
int i;
if (n == IRPVAL)
n = MAXMACPARAMS;
for (i=0; i<src->param_len[n] && len>0; i++,len--)
*d++ = src->param[n][i];
return i==src->param_len[n] ? i : -1;
}
return 0;
}
/* copy nth macro qualifier to line buffer, return -1 when out of space */
int copy_macro_qual(source *src,int n,char *d,int len)
{
#if MAX_QUALIFIERS > 0
if (n < src->num_quals) {
int i;
for (i=0; i<src->qual_len[n] && len>0; i++,len--)
*d++ = src->qual[n][i];
return i==src->qual_len[n] ? i : -1;
}
#endif
return 0;
}