-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
1369 lines (1116 loc) · 31.8 KB
/
array.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
/*
* array.c - routines for awk arrays.
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2011 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "awk.h"
extern FILE *output_fp;
extern NODE **fmt_list; /* declared in eval.c */
extern array_ptr str_array_func[];
extern array_ptr cint_array_func[];
extern array_ptr int_array_func[];
static size_t SUBSEPlen;
static char *SUBSEP;
static char indent_char[] = " ";
static NODE **null_lookup(NODE *symbol, NODE *subs);
static NODE **null_afunc(NODE *symbol, NODE *subs);
static NODE **null_dump(NODE *symbol, NODE *subs);
static array_ptr null_array_func[] = {
(array_ptr) 0,
(array_ptr) 0,
null_lookup,
null_afunc,
null_afunc,
null_afunc,
null_afunc,
null_afunc,
null_dump,
};
#define MAX_ATYPE 10
static array_ptr *atypes[MAX_ATYPE];
static int num_atypes = 0;
/*
* register_array_func --- add routines to handle arrays.
*
* index 0 : initialization.
* index 1 : check if index is compatible.
* index 8 : array dump, memory and other statistics (do_adump).
*/
int
register_array_func(array_ptr *afunc)
{
if (afunc && num_atypes < MAX_ATYPE) {
if (afunc != str_array_func && ! afunc[1])
return false;
atypes[num_atypes++] = afunc;
if (afunc[0]) /* execute init routine if any */
(void) (*afunc[0])(NULL, NULL);
return true;
}
return false;
}
/* array_init --- register all builtin array types */
void
array_init()
{
(void) register_array_func(str_array_func); /* the default */
if (! do_mpfr) {
(void) register_array_func(int_array_func);
(void) register_array_func(cint_array_func);
}
}
/* make_array --- create an array node */
NODE *
make_array()
{
NODE *array;
getnode(array);
memset(array, '\0', sizeof(NODE));
array->type = Node_var_array;
array->array_funcs = null_array_func;
/* vname, flags, and parent_array not set here */
return array;
}
/* init_array --- (re)initialize an array node */
void
init_array(NODE *symbol)
{
symbol->type = Node_var_array;
symbol->array_funcs = null_array_func;
symbol->buckets = NULL;
symbol->table_size = symbol->array_size = 0;
symbol->array_capacity = 0;
assert(symbol->xarray == NULL);
/* symbol->xarray = NULL; */
/* flags, vname, parent_array not (re)initialized */
}
/* null_lookup: assign type to an empty array. */
static NODE **
null_lookup(NODE *symbol, NODE *subs)
{
int i;
array_ptr *afunc = NULL;
assert(symbol->table_size == 0);
/* Check which array type wants to accept this sub; traverse
* array type list in reverse order.
*/
for (i = num_atypes - 1; i >= 1; i--) {
afunc = atypes[i];
if (afunc[1](symbol, subs) != NULL)
break;
}
if (i == 0 || afunc == NULL)
afunc = atypes[0]; /* default is str_array_func */
symbol->array_funcs = afunc;
/* We have the right type of array; install the subscript */
return symbol->alookup(symbol, subs);
}
/* null_afunc --- dummy function for an empty array */
static NODE **
null_afunc(NODE *symbol ATTRIBUTE_UNUSED, NODE *subs ATTRIBUTE_UNUSED)
{
return NULL;
}
/* null_dump --- dump function for an empty array */
static NODE **
null_dump(NODE *symbol, NODE *subs ATTRIBUTE_UNUSED)
{
fprintf(output_fp, "array `%s' is empty\n", array_vname(symbol));
return NULL;
}
/* r_in_array --- test whether the array element symbol[subs] exists or not,
* return pointer to value if it does.
*/
NODE *
r_in_array(NODE *symbol, NODE *subs)
{
NODE **ret;
ret = symbol->aexists(symbol, subs);
return (ret ? *ret : NULL);
}
/* assoc_copy --- duplicate input array "symbol" */
NODE *
assoc_copy(NODE *symbol, NODE *newsymb)
{
assert(newsymb->vname != NULL);
assoc_clear(newsymb);
(void) symbol->acopy(symbol, newsymb);
newsymb->array_funcs = symbol->array_funcs;
newsymb->flags = symbol->flags;
return newsymb;
}
/* assoc_dump --- dump array */
void
assoc_dump(NODE *symbol, NODE *ndump)
{
if (symbol->adump)
(void) symbol->adump(symbol, ndump);
}
/* make_aname --- construct a 'vname' for a (sub)array */
const char *
make_aname(const NODE *symbol)
{
static char *aname = NULL;
static size_t alen;
static size_t max_alen;
#define SLEN 256
if (symbol->parent_array != NULL) {
size_t slen;
(void) make_aname(symbol->parent_array);
slen = strlen(symbol->vname); /* subscript in parent array */
if (alen + slen + 4 > max_alen) { /* sizeof("[\"\"]") = 4 */
max_alen = alen + slen + 4 + SLEN;
erealloc(aname, char *, (max_alen + 1) * sizeof(char *), "make_aname");
}
alen += sprintf(aname + alen, "[\"%s\"]", symbol->vname);
} else {
alen = strlen(symbol->vname);
if (aname == NULL) {
max_alen = alen + SLEN;
emalloc(aname, char *, (max_alen + 1) * sizeof(char *), "make_aname");
} else if (alen > max_alen) {
max_alen = alen + SLEN;
erealloc(aname, char *, (max_alen + 1) * sizeof(char *), "make_aname");
}
memcpy(aname, symbol->vname, alen + 1);
}
return aname;
}
#undef SLEN
/*
* array_vname --- print the name of the array
*
* Returns a pointer to a statically maintained dynamically allocated string.
* It's appropriate for printing the name once; if the caller wants
* to save it, they have to make a copy.
*/
const char *
array_vname(const NODE *symbol)
{
static char *message = NULL;
static size_t msglen = 0;
char *s;
size_t len;
int n;
const NODE *save_symbol = symbol;
const char *from = _("from %s");
const char *aname;
if (symbol->type != Node_array_ref
|| symbol->orig_array->type != Node_var_array
) {
if (symbol->type != Node_var_array || symbol->parent_array == NULL)
return symbol->vname;
return make_aname(symbol);
}
/* First, we have to compute the length of the string: */
len = 2; /* " (" */
n = 0;
while (symbol->type == Node_array_ref) {
len += strlen(symbol->vname);
n++;
symbol = symbol->prev_array;
}
/* Get the (sub)array name */
if (symbol->parent_array == NULL)
aname = symbol->vname;
else
aname = make_aname(symbol);
len += strlen(aname);
/*
* Each node contributes by strlen(from) minus the length
* of "%s" in the translation (which is at least 2)
* plus 2 for ", " or ")\0"; this adds up to strlen(from).
*/
len += n * strlen(from);
/* (Re)allocate memory: */
if (message == NULL) {
emalloc(message, char *, len, "array_vname");
msglen = len;
} else if (len > msglen) {
erealloc(message, char *, len, "array_vname");
msglen = len;
} /* else
current buffer can hold new name */
/* We're ready to print: */
symbol = save_symbol;
s = message;
/*
* Ancient systems have sprintf() returning char *, not int.
* If you have one of those, use sprintf(..); s += strlen(s) instead.
*/
s += sprintf(s, "%s (", symbol->vname);
for (;;) {
symbol = symbol->prev_array;
if (symbol->type != Node_array_ref)
break;
s += sprintf(s, from, symbol->vname);
s += sprintf(s, ", ");
}
s += sprintf(s, from, aname);
strcpy(s, ")");
return message;
}
/*
* get_array --- proceed to the actual Node_var_array,
* change Node_var_new to an array.
* If canfatal and type isn't good, die fatally,
* otherwise return the final actual value.
*/
NODE *
get_array(NODE *symbol, bool canfatal)
{
NODE *save_symbol = symbol;
bool isparam = false;
if (symbol->type == Node_param_list) {
save_symbol = symbol = GET_PARAM(symbol->param_cnt);
isparam = true;
if (symbol->type == Node_array_ref)
symbol = symbol->orig_array;
}
switch (symbol->type) {
case Node_var_new:
init_array(symbol);
symbol->parent_array = NULL; /* main array has no parent */
/* fall through */
case Node_var_array:
break;
case Node_array_ref:
default:
/* notably Node_var but catches also e.g. a[1] = "x"; a[1][1] = "y" */
if (canfatal) {
if (symbol->type == Node_val)
fatal(_("attempt to use a scalar value as array"));
if (isparam)
fatal(_("attempt to use scalar parameter `%s' as an array"),
save_symbol->vname);
else
fatal(_("attempt to use scalar `%s' as an array"),
save_symbol->vname);
} else
break;
}
return symbol;
}
/* set_SUBSEP --- update SUBSEP related variables when SUBSEP assigned to */
void
set_SUBSEP()
{
SUBSEP_node->var_value = force_string(SUBSEP_node->var_value);
SUBSEP = SUBSEP_node->var_value->stptr;
SUBSEPlen = SUBSEP_node->var_value->stlen;
}
/* concat_exp --- concatenate expression list into a single string */
NODE *
concat_exp(int nargs, bool do_subsep)
{
/* do_subsep is false for Op_concat */
NODE *r;
char *str;
char *s;
size_t len;
size_t subseplen = 0;
int i;
extern NODE **args_array;
if (nargs == 1)
return POP_STRING();
if (do_subsep)
subseplen = SUBSEPlen;
len = 0;
for (i = 1; i <= nargs; i++) {
r = TOP();
if (r->type == Node_var_array) {
while (--i > 0)
DEREF(args_array[i]); /* avoid memory leak */
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(r));
}
r = POP_STRING();
args_array[i] = r;
len += r->stlen;
}
len += (nargs - 1) * subseplen;
emalloc(str, char *, len + 2, "concat_exp");
r = args_array[nargs];
memcpy(str, r->stptr, r->stlen);
s = str + r->stlen;
DEREF(r);
for (i = nargs - 1; i > 0; i--) {
if (subseplen == 1)
*s++ = *SUBSEP;
else if (subseplen > 0) {
memcpy(s, SUBSEP, subseplen);
s += subseplen;
}
r = args_array[i];
memcpy(s, r->stptr, r->stlen);
s += r->stlen;
DEREF(r);
}
return make_str_node(str, len, ALREADY_MALLOCED);
}
/* adjust_fcall_stack: remove subarray(s) of symbol[] from
* function call stack.
*/
static void
adjust_fcall_stack(NODE *symbol, int nsubs)
{
NODE *func, *r, *n;
NODE **sp;
int pcount;
/*
* Solve the nasty problem of disappearing subarray arguments:
*
* function f(c, d) { delete c; .. use non-existent array d .. }
* BEGIN { a[0][0] = 1; f(a, a[0]); .. }
*
* The fix is to convert 'd' to a local empty array; This has
* to be done before clearing the parent array to avoid referring to
* already free-ed memory.
*
* Similar situations exist for builtins accepting more than
* one array argument: split, patsplit, asort and asorti. For example:
*
* BEGIN { a[0][0] = 1; split("abc", a, "", a[0]) }
*
* These cases do not involve the function call stack, and are
* handled individually in their respective routines.
*/
func = frame_ptr->func_node;
if (func == NULL) /* in main */
return;
pcount = func->param_cnt;
sp = frame_ptr->stack;
for (; pcount > 0; pcount--) {
r = *sp++;
if (r->type != Node_array_ref
|| r->orig_array->type != Node_var_array)
continue;
n = r->orig_array;
/* Case 1 */
if (n == symbol
&& symbol->parent_array != NULL
&& nsubs > 0
) {
/* 'symbol' is a subarray, and 'r' is the same subarray:
*
* function f(c, d) { delete c[0]; .. }
* BEGIN { a[0][0] = 1; f(a, a[0]); .. }
*
* But excludes cases like (nsubs = 0):
*
* function f(c, d) { delete c; ..}
* BEGIN { a[0][0] = 1; f(a[0], a[0]); ...}
*/
init_array(r);
r->parent_array = NULL;
r->flags = 0;
continue;
}
/* Case 2 */
for (n = n->parent_array; n != NULL; n = n->parent_array) {
assert(n->type == Node_var_array);
if (n == symbol) {
/* 'r' is a subarray of 'symbol':
*
* function f(c, d) { delete c; .. use d as array .. }
* BEGIN { a[0][0] = 1; f(a, a[0]); .. }
* OR
* BEGIN { a[0][0][0][0] = 1; f(a[0], a[0][0][0]); .. }
*
*/
init_array(r);
r->parent_array = NULL;
r->flags = 0;
break;
}
}
}
}
/* do_delete --- perform `delete array[s]' */
/*
* `symbol' is array
* `nsubs' is no of subscripts
*/
void
do_delete(NODE *symbol, int nsubs)
{
NODE *val, *subs;
int i;
assert(symbol->type == Node_var_array);
subs = val = NULL; /* silence the compiler */
/*
* The force_string() call is needed to make sure that
* the string subscript is reasonable. For example, with it:
*
* $ ./gawk --posix 'BEGIN { CONVFMT="%ld"; delete a[1.233]}'
* gawk: cmd. line:1: fatal: `%l' is not permitted in POSIX awk formats
*
* Without it, the code does not fail.
*/
#define free_subs(n) do { \
NODE *s = PEEK(n - 1); \
if (s->type == Node_val) { \
(void) force_string(s); /* may have side effects. */ \
DEREF(s); \
} \
} while (--n > 0)
if (nsubs == 0) {
/* delete array */
adjust_fcall_stack(symbol, 0); /* fix function call stack; See above. */
assoc_clear(symbol);
return;
}
/* NB: subscripts are in reverse order on stack */
for (i = nsubs; i > 0; i--) {
subs = PEEK(i - 1);
if (subs->type != Node_val) {
free_subs(i);
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(subs));
}
val = in_array(symbol, subs);
if (val == NULL) {
if (do_lint) {
subs = force_string(subs);
lintwarn(_("delete: index `%s' not in array `%s'"),
subs->stptr, array_vname(symbol));
}
/* avoid memory leak, free all subs */
free_subs(i);
return;
}
if (i > 1) {
if (val->type != Node_var_array) {
/* e.g.: a[1] = 1; delete a[1][1] */
free_subs(i);
subs = force_string(subs);
fatal(_("attempt to use scalar `%s[\"%.*s\"]' as an array"),
array_vname(symbol),
(int) subs->stlen,
subs->stptr);
}
symbol = val;
DEREF(subs);
}
}
if (val->type == Node_var_array) {
adjust_fcall_stack(val, nsubs); /* fix function call stack; See above. */
assoc_clear(val);
/* cleared a sub-array, free Node_var_array */
efree(val->vname);
freenode(val);
} else
unref(val);
(void) assoc_remove(symbol, subs);
DEREF(subs);
#undef free_subs
}
/* do_delete_loop --- simulate ``for (iggy in foo) delete foo[iggy]'' */
/*
* The primary hassle here is that `iggy' needs to have some arbitrary
* array index put in it before we can clear the array, we can't
* just replace the loop with `delete foo'.
*/
void
do_delete_loop(NODE *symbol, NODE **lhs)
{
NODE **list;
NODE fl;
if (array_empty(symbol))
return;
fl.flags = AINDEX|ADELETE; /* need a single index */
list = symbol->alist(symbol, & fl);
assert(list != NULL);
unref(*lhs);
*lhs = list[0];
efree(list);
/* blast the array in one shot */
adjust_fcall_stack(symbol, 0);
assoc_clear(symbol);
}
/* value_info --- print scalar node info */
static void
value_info(NODE *n)
{
#define PREC_NUM -1
#define PREC_STR -1
if (n == Nnull_string || n == Null_field) {
fprintf(output_fp, "<(null)>");
return;
}
if ((n->flags & (STRING|STRCUR)) != 0) {
fprintf(output_fp, "<");
fprintf(output_fp, "\"%.*s\"", PREC_STR, n->stptr);
if ((n->flags & (NUMBER|NUMCUR)) != 0) {
#ifdef HAVE_MPFR
if (is_mpg_float(n))
fprintf(output_fp, ":%s",
mpg_fmt("%.*R*g", PREC_NUM, ROUND_MODE, n->mpg_numbr));
else if (is_mpg_integer(n))
fprintf(output_fp, ":%s", mpg_fmt("%Zd", n->mpg_i));
else
#endif
fprintf(output_fp, ":%.*g", PREC_NUM, n->numbr);
}
fprintf(output_fp, ">");
} else {
#ifdef HAVE_MPFR
if (is_mpg_float(n))
fprintf(output_fp, "<%s>",
mpg_fmt("%.*R*g", PREC_NUM, ROUND_MODE, n->mpg_numbr));
else if (is_mpg_integer(n))
fprintf(output_fp, "<%s>", mpg_fmt("%Zd", n->mpg_i));
else
#endif
fprintf(output_fp, "<%.*g>", PREC_NUM, n->numbr);
}
fprintf(output_fp, ":%s", flags2str(n->flags));
if ((n->flags & FIELD) == 0)
fprintf(output_fp, ":%ld", n->valref);
else
fprintf(output_fp, ":");
if ((n->flags & (STRING|STRCUR)) == STRCUR) {
fprintf(output_fp, "][");
fprintf(output_fp, "stfmt=%d, ", n->stfmt);
fprintf(output_fp, "CONVFMT=\"%s\"", n->stfmt <= -1 ? "%ld"
: fmt_list[n->stfmt]->stptr);
}
#undef PREC_NUM
#undef PREC_STR
}
void
indent(int indent_level)
{
int i;
for (i = 0; i < indent_level; i++)
fprintf(output_fp, "%s", indent_char);
}
/* assoc_info --- print index, value info */
void
assoc_info(NODE *subs, NODE *val, NODE *ndump, const char *aname)
{
int indent_level = ndump->alevel;
indent_level++;
indent(indent_level);
fprintf(output_fp, "I: [%s:", aname);
if ((subs->flags & (MPFN|MPZN|INTIND)) == INTIND)
fprintf(output_fp, "<%ld>", (long) subs->numbr);
else
value_info(subs);
fprintf(output_fp, "]\n");
indent(indent_level);
if (val->type == Node_val) {
fprintf(output_fp, "V: [scalar: ");
value_info(val);
} else {
fprintf(output_fp, "V: [");
ndump->alevel++;
ndump->adepth--;
assoc_dump(val, ndump);
ndump->adepth++;
ndump->alevel--;
indent(indent_level);
}
fprintf(output_fp, "]\n");
}
/* do_adump --- dump an array: interface to assoc_dump */
NODE *
do_adump(int nargs)
{
NODE *symbol, *tmp;
static NODE ndump;
long depth = 0;
/* depth < 0, no index and value info.
* = 0, main array index and value info; does not descend into sub-arrays.
* > 0, descends into 'depth' sub-arrays, and prints index and value info.
*/
if (nargs == 2) {
tmp = POP_NUMBER();
depth = get_number_si(tmp);
DEREF(tmp);
}
symbol = POP_PARAM();
if (symbol->type != Node_var_array)
fatal(_("adump: first argument not an array"));
ndump.type = Node_dump_array;
ndump.adepth = depth;
ndump.alevel = 0;
assoc_dump(symbol, & ndump);
return make_number((AWKNUM) 0);
}
/* asort_actual --- do the actual work to sort the input array */
static NODE *
asort_actual(int nargs, SORT_CTXT ctxt)
{
NODE *array, *dest = NULL, *result;
NODE *r, *subs, *s;
NODE **list = NULL, **ptr;
unsigned long num_elems, i;
const char *sort_str;
if (nargs == 3) /* 3rd optional arg */
s = POP_STRING();
else
s = dupnode(Nnull_string); /* "" => default sorting */
s = force_string(s);
sort_str = s->stptr;
if (s->stlen == 0) { /* default sorting */
if (ctxt == ASORT)
sort_str = "@val_type_asc";
else
sort_str = "@ind_str_asc";
}
if (nargs >= 2) { /* 2nd optional arg */
dest = POP_PARAM();
if (dest->type != Node_var_array) {
fatal(ctxt == ASORT ?
_("asort: second argument not an array") :
_("asorti: second argument not an array"));
}
}
array = POP_PARAM();
if (array->type != Node_var_array) {
fatal(ctxt == ASORT ?
_("asort: first argument not an array") :
_("asorti: first argument not an array"));
}
if (dest != NULL) {
for (r = dest->parent_array; r != NULL; r = r->parent_array) {
if (r == array)
fatal(ctxt == ASORT ?
_("asort: cannot use a subarray of first arg for second arg") :
_("asorti: cannot use a subarray of first arg for second arg"));
}
for (r = array->parent_array; r != NULL; r = r->parent_array) {
if (r == dest)
fatal(ctxt == ASORT ?
_("asort: cannot use a subarray of second arg for first arg") :
_("asorti: cannot use a subarray of second arg for first arg"));
}
}
num_elems = array->table_size;
if (num_elems > 0) /* sorting happens inside assoc_list */
list = assoc_list(array, sort_str, ctxt);
DEREF(s);
if (num_elems == 0 || list == NULL) {
/* source array is empty */
if (dest != NULL && dest != array)
assoc_clear(dest);
return make_number((AWKNUM) 0);
}
/*
* Must not assoc_clear() the source array before constructing
* the output array. assoc_list() does not duplicate array values
* which are needed for asort().
*/
if (dest != NULL && dest != array) {
assoc_clear(dest);
result = dest;
} else {
/* use 'result' as a temporary destination array */
result = make_array();
result->vname = array->vname;
result->parent_array = array->parent_array;
}
if (ctxt == ASORTI) {
/* We want the indices of the source array. */
for (i = 1, ptr = list; i <= num_elems; i++, ptr += 2) {
subs = make_number(i);
*assoc_lookup(result, subs) = *ptr;
unref(subs);
}
} else {
/* We want the values of the source array. */
for (i = 1, ptr = list; i <= num_elems; i++) {
subs = make_number(i);
/* free index node */
r = *ptr++;
unref(r);
/* value node */
r = *ptr++;
if (r->type == Node_val)
*assoc_lookup(result, subs) = dupnode(r);
else {
NODE *arr;
arr = make_array();
subs = force_string(subs);
arr->vname = subs->stptr;
subs->stptr = NULL;
subs->flags &= ~STRCUR;
arr->parent_array = array; /* actual parent, not the temporary one. */
*assoc_lookup(result, subs) = assoc_copy(r, arr);
}
unref(subs);
}
}
efree(list);
if (result != dest) {
/* dest == NULL or dest == array */
assoc_clear(array);
*array = *result; /* copy result into array */
freenode(result);
} /* else
result == dest
dest != NULL and dest != array */
return make_number((AWKNUM) num_elems);
}
/* do_asort --- sort array by value */
NODE *
do_asort(int nargs)
{
return asort_actual(nargs, ASORT);
}
/* do_asorti --- sort array by index */
NODE *
do_asorti(int nargs)
{
return asort_actual(nargs, ASORTI);
}
/*
* cmp_strings --- compare two strings; logic similar to cmp_nodes() in eval.c
* except the extra case-sensitive comparison when the case-insensitive
* result is a match.
*/
static int
cmp_strings(const NODE *n1, const NODE *n2)
{
char *s1, *s2;
size_t len1, len2;
int ret;
size_t lmin;
s1 = n1->stptr;
len1 = n1->stlen;
s2 = n2->stptr;
len2 = n2->stlen;
if (len1 == 0)
return len2 == 0 ? 0 : -1;
if (len2 == 0)
return 1;
/* len1 > 0 && len2 > 0 */
lmin = len1 < len2 ? len1 : len2;
if (IGNORECASE) {
const unsigned char *cp1 = (const unsigned char *) s1;
const unsigned char *cp2 = (const unsigned char *) s2;
#if MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
ret = strncasecmpmbs((const unsigned char *) cp1,
(const unsigned char *) cp2, lmin);
} else
#endif
for (ret = 0; lmin-- > 0 && ret == 0; cp1++, cp2++)
ret = casetable[*cp1] - casetable[*cp2];
if (ret != 0)
return ret;
/*
* If case insensitive result is "they're the same",
* use case sensitive comparison to force distinct order.
*/
}
ret = memcmp(s1, s2, lmin);
if (ret != 0 || len1 == len2)
return ret;
return (len1 < len2) ? -1 : 1;
}
/* sort_up_index_string --- qsort comparison function; ascending index strings. */