-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsvalue.cpp
More file actions
1564 lines (1431 loc) · 45.7 KB
/
Copy pathsvalue.cpp
File metadata and controls
1564 lines (1431 loc) · 45.7 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
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <assert.h>
#include <utility>
#include <type_traits>
#include "src/std.h"
#include "src/simul_efun.h"
#include "array.h"
#include "buffer.h"
#include "functional.h"
#include "mapping.h"
#include "object.h"
#include "svalue.h"
#include "include/function.h"
extern "C" {
extern void dealloc_object (object_t *, const char* caller);
extern void dealloc_array (array_t *);
extern void dealloc_mapping (mapping_t *);
extern void dealloc_class (array_t *);
extern void dealloc_funp (funptr_t *);
}
namespace {
void retain_svalue_payload(svalue_t *sv) noexcept {
if (sv == nullptr)
{
return;
}
auto view = lpc::svalue_view::from(sv);
if (view.is_string())
{
if (view.is_malloc())
{
INC_COUNTED_REF(view.malloc_string());
}
else if (view.is_shared())
{
INC_COUNTED_REF(view.shared_string());
}
return;
}
if (sv->type & T_REFED)
{
sv->u.refed->ref++;
}
}
void release_retained_svalue(svalue_t *sv, const char *caller) noexcept {
if (sv == nullptr)
{
return;
}
auto view = lpc::svalue_view::from(sv);
if (view.is_string() || (sv->type & T_REFED))
{
free_svalue(sv, caller);
}
}
} // namespace
/* ========== C API IMPLEMENTATION ========== */
/**
* Free the data that an svalue is pointing to. Not the svalue itself.
* The caller argument is used for debugging purposes, to know which function is freeing the svalue.
*/
void free_svalue (svalue_t * v, const char* caller) {
assert (v != NULL);
if (v->type == T_STRING)
{
free_string_svalue (v);
}
else if (v->type & T_REFED)
{
if (!(--v->u.refed->ref))
{
switch (v->type)
{
case T_OBJECT:
dealloc_object (v->u.ob, caller);
break;
case T_CLASS:
dealloc_class (v->u.arr);
break;
case T_ARRAY:
dealloc_array (v->u.arr);
break;
case T_BUFFER:
if (v->u.buf != &null_buf)
FREE ((char *) v->u.buf);
break;
case T_MAPPING:
dealloc_mapping (v->u.map);
break;
case T_FUNCTION:
dealloc_funp (v->u.fp);
break;
}
}
}
else if (v->type == T_ERROR_HANDLER)
{
(*v->u.error_handler) ();
}
}
/**
* Free several svalues, and free up the space used by the svalues.
* The svalues must be sequentially located.
*/
void free_some_svalues (svalue_t * v, int num) {
while (num--)
free_svalue (v + num, "free_some_svalues");
FREE (v);
}
void assign_svalue (svalue_t * dest, svalue_t * v) {
/* First deallocate the previous value. */
free_svalue (dest, "assign_svalue");
assign_svalue_no_free (dest, v);
}
/**
* Assign to a svalue.
* This is done either when element in array, or when to an identifier
* (as all identifiers are kept in a array pointed to by the object).
*/
void assign_svalue_no_free (svalue_t * to, const svalue_t * from) {
DEBUG_CHECK (from == 0, "Attempt to assign_svalue() from a null ptr.\n");
DEBUG_CHECK (to == 0, "Attempt to assign_svalue() to a null ptr.\n");
*to = *from;
retain_svalue_payload (to);
}
/*
* Copies an array of svalues to another location, which should be
* free space.
*/
void copy_some_svalues (svalue_t * dest, svalue_t * v, int num) {
while (num--)
assign_svalue_no_free (dest + num, v + num);
}
int svalue_string_lexcmp(const svalue_t *lhs, const svalue_t *rhs) {
auto lhs_view = lpc::const_svalue_view::from(lhs);
auto rhs_view = lpc::const_svalue_view::from(rhs);
size_t lhs_len = lhs_view.length();
size_t rhs_len = rhs_view.length();
size_t prefix_len = lhs_len < rhs_len ? lhs_len : rhs_len;
int cmp = 0;
if (prefix_len != 0)
{
cmp = memcmp(lhs_view.c_str(), rhs_view.c_str(), prefix_len);
if (cmp != 0)
{
return cmp;
}
}
if (lhs_len < rhs_len)
{
return -1;
}
if (lhs_len > rhs_len)
{
return 1;
}
return 0;
}
#define NULL_MSG "0"
typedef unsigned int format_info;
/*
* Format of format_info:
* 00000000 0000xxxx : argument type:
* 0000 : type not found yet;
* 0001 : error type not found;
* 0010 : percent sign, null argument;
* 0011 : LPC datatype;
* 0100 : string;
* 1000 : integer;
* 1001 : char;
* 1010 : octal;
* 1011 : hex;
* 1100 : HEX;
* 1101 : float;
* 00000000 00xx0000 : justification:
* 00 : right;
* 01 : centre;
* 10 : left;
* 00000000 xx000000 : positive pad char:
* 00 : none;
* 01 : ' ';
* 10 : '+';
* 0000000x 00000000 : array mode?
* 000000x0 00000000 : column mode?
* 00000x00 00000000 : table mode?
*/
#define INFO_T 0xF
#define INFO_T_ERROR 0x1
#define INFO_T_NULL 0x2
#define INFO_T_LPC 0x3
#define INFO_T_STRING 0x4
#define INFO_T_INT 0x8
#define INFO_T_CHAR 0x9
#define INFO_T_OCT 0xA
#define INFO_T_HEX 0xB
#define INFO_T_C_HEX 0xC
#define INFO_T_FLOAT 0xD
#define INFO_J 0x30
#define INFO_J_CENTRE 0x10
#define INFO_J_LEFT 0x20
#define INFO_PP 0xC0
#define INFO_PP_SPACE 0x40
#define INFO_PP_PLUS 0x80
#define INFO_ARRAY 0x100
#define INFO_COLS 0x200
#define INFO_TABLE 0x400
#define ERR_BUFF_OVERFLOW 0x1 /* buffer overflowed */
#define ERR_TO_FEW_ARGS 0x2 /* more arguments spec'ed than passed */
#define ERR_INVALID_STAR 0x3 /* invalid arg to * */
#define ERR_PRES_EXPECTED 0x4 /* expected presision not found */
#define ERR_INVALID_FORMAT_STR 0x5 /* error in format string */
#define ERR_INCORRECT_ARG_S 0x6 /* invalid arg to %s */
#define ERR_CST_REQUIRES_FS 0x7 /* field size not given for c/t */
#define ERR_BAD_INT_TYPE 0x8 /* bad integer type... */
#define ERR_UNDEFINED_TYPE 0x9 /* undefined type found */
#define ERR_QUOTE_EXPECTED 0xA /* expected ' not found */
#define ERR_UNEXPECTED_EOS 0xB /* fs terminated unexpectedly */
#define ERR_NULL_PS 0xC /* pad string is null */
#define ERR_ARRAY_EXPECTED 0xD /* Yep! You guessed it. */
#define ADD_CHAR(x) {\
outbuf_addchar(&obuff, x);\
if (obuff.real_size == USHRT_MAX) sprintf_error(ERR_BUFF_OVERFLOW); \
}
#define GET_NEXT_ARG {\
if (++cur_arg >= argc) sprintf_error(ERR_TO_FEW_ARGS); \
carg = (argv+cur_arg);\
}
typedef struct pad_info_s {
const char *what;
int len;
} pad_info_t;
typedef struct tab_data_s {
const char *start;
const char *cur;
} tab_data_t;
/* slash here means 'or' */
typedef struct ColumnSlashTable {
union CSTData
{
const char *col; /* column data */
tab_data_t *tab; /* table data */
}
d; /* d == data */
unsigned short int nocols; /* number of columns in table *sigh* */
pad_info_t *pad;
unsigned int start; /* starting cursor position */
unsigned int size; /* column/table width */
unsigned int remainder; /* extra space needed to fill out to width */
int pres; /* presision */
format_info info; /* formatting data */
struct ColumnSlashTable *next;
} cst; /* Columns Slash Tables */
static outbuffer_t obuff;
static cst *csts = 0;
static int cur_arg; /* current arg number */
static svalue_t clean = { T_NUMBER, 0, {0} };
static void sprintf_error (int) NO_RETURN;
static void add_justified (const char *str, size_t slen, pad_info_t * pad, int fs, format_info finfo, short int trailing);
static int add_column (cst ** column, int trailing);
static int add_table (cst ** table);
/**
* Signal an LPC sprintf() error. Note that we call error, so this routine never returns.
* Anything that has been allocated should be somewhere it can be found and freed later.
*/
static void sprintf_error (int which) {
char lbuf[2048];
const char *err;
switch (which)
{
case ERR_BUFF_OVERFLOW:
err = "BUFF_SIZE overflowed...";
break;
case ERR_TO_FEW_ARGS:
err = "More arguments specified than passed.";
break;
case ERR_INVALID_STAR:
err = "Incorrect argument type to *.";
break;
case ERR_PRES_EXPECTED:
err = "Expected presision not found.";
break;
case ERR_INVALID_FORMAT_STR:
err = "Error in format string.";
break;
case ERR_INCORRECT_ARG_S:
err = "Incorrect argument to type %%s.";
break;
case ERR_CST_REQUIRES_FS:
err = "Column/table mode requires a field size.";
break;
case ERR_BAD_INT_TYPE:
err = "!feature - bad integer type!";
break;
case ERR_UNDEFINED_TYPE:
err = "!feature - undefined type!";
break;
case ERR_QUOTE_EXPECTED:
err = "Quote expected in format string.";
break;
case ERR_UNEXPECTED_EOS:
err = "Unexpected end of format string.";
break;
case ERR_NULL_PS:
err = "Null pad string specified.";
break;
case ERR_ARRAY_EXPECTED:
err = "Array expected.";
break;
default:
err = "undefined error in (s)printf!\n";
break;
}
snprintf (lbuf, sizeof (lbuf), "(s)printf(): %s (arg: %d)\n", err, cur_arg);
error (lbuf);
}
static void add_pad (pad_info_t * pad, size_t len) {
char *p;
int padlen;
if (outbuf_extend (&obuff, len) != len)
sprintf_error (ERR_BUFF_OVERFLOW);
p = obuff.buffer + obuff.real_size;
obuff.real_size += len;
p[len] = 0;
if (pad && (padlen = pad->len))
{
char *end;
const char *pstr = pad->what;
int i;
char c;
for (i = 0, end = p + len; p < end; i++)
{
if (i == padlen)
i = 0;
if ((c = pstr[i]) == '\\')
{
/* guaranteed to have a valid char next */
*p++ = pstr[++i];
}
else
*p++ = c;
}
}
else
memset (p, ' ', len);
}
static void add_nstr (const char *str, size_t len) {
if (outbuf_extend (&obuff, len) != len)
sprintf_error (ERR_BUFF_OVERFLOW);
memcpy (obuff.buffer + obuff.real_size, str, len);
obuff.real_size += len;
obuff.buffer[obuff.real_size] = 0;
}
/**
* Adds the string "str" to the buff after justifying it within field size "fs".
* Field size "fs" is adjusted to take into account any ANSI escape sequences
* within "str".
* "trailing" is a flag which is set if trailing justification is to be done.
* "str" is added verbatim.
* trailing is, of course, ignored in the case of right justification.
*
* @param str The string to be justified.
* @param slen The length of the string.
* @param pad The pad info to use.
* @param fs The field size.
* @param finfo The format info.
* @param trailing A flag indicating if trailing justification is to be done.
*/
static void add_justified (const char *str, size_t slen, pad_info_t * pad, int fs, format_info finfo, short int trailing) {
/* compensate field size by adding count of characters for ANSI escape sequences */
const char *pstr;
for (pstr = str; pstr - str < (ptrdiff_t)slen;)
{
if (*pstr == '\x1B') /* ESC */
{
pstr++;
fs += 2;
if (*pstr++ == '[')
{
while (isdigit (*pstr) || *pstr == ';')
{
pstr++;
fs++;
}
pstr++;
fs++;
}
continue;
}
pstr++;
}
fs -= (int)slen;
if (fs <= 0)
{
add_nstr (str, slen);
}
else
{
/* add paddings */
int i;
switch (finfo & INFO_J)
{
case INFO_J_LEFT:
add_nstr (str, slen);
if (trailing)
add_pad (pad, fs);
break;
case INFO_J_CENTRE:
i = fs / 2 + fs % 2;
add_pad (pad, i);
add_nstr (str, slen);
if (trailing)
add_pad (pad, fs - i);
break;
default:
/* std (s)printf defaults to right
* justification */
add_pad (pad, fs);
add_nstr (str, slen);
}
}
}
/*
* Adds "column" to the buffer.
* Returns 0 is column not finished.
* Returns 1 if column completed.
* Returns 2 if column completed has a \n at the end.
*/
static int add_column (cst ** column, int trailing) {
unsigned int done;
char c;
unsigned int space = (unsigned int)-1;
int ret;
cst *col = *column; /* always holds (*column) */
const char *col_d = col->d.col; /* always holds (col->d.col) */
done = 0;
/* find a good spot to break the line */
while ((c = col_d[done]) && c != '\n')
{
if (c == ' ')
space = done;
if (++done == (unsigned int)col->pres)
{
if (space != (unsigned int)-1)
{
c = col_d[done];
if (c != '\n' && c != ' ' && c)
done = space;
}
break;
}
}
add_justified (col_d, done, col->pad,
col->size, col->info, trailing || col->next);
col_d += done;
ret = 1;
if (*col_d == '\n')
{
col_d++;
ret = 2;
}
col->d.col = col_d;
/*
* if the next character is a NULL then take this column out of
* the list.
*/
if (!(*col_d))
{
cst *temp;
temp = col->next;
if (col->pad)
FREE (col->pad);
FREE (col);
*column = temp;
return ret;
}
return 0;
} /* end of add_column() */
/*
* Adds "table" to the buffer.
* Returns 0 if table not completed.
* Returns 1 if table completed.
*/
static int add_table (cst ** table) {
int done, i;
cst *tab = *table; /* always (*table) */
tab_data_t *tab_d = tab->d.tab; /* always tab->d.tab */
const char *tab_di; /* always tab->d.tab[i].cur */
ptrdiff_t end;
for (i = 0; i < tab->nocols && (tab_di = tab_d[i].cur); i++)
{
end = tab_d[i + 1].start - tab_di - 1;
for (done = 0; done != end && tab_di[done] != '\n'; done++)
;
add_justified (tab_di, (done > (int)tab->size ? (int)tab->size : done),
tab->pad, tab->size, tab->info,
tab->pad || (i < tab->nocols - 1) || tab->next);
if (done >= end - 1)
{
tab_di = 0;
}
else
{
tab_di += done + 1; /* inc'ed next line ... */
}
tab_d[i].cur = tab_di;
}
if (tab->pad)
{
while (i++ < tab->nocols)
{
add_pad (tab->pad, tab->size);
}
add_pad (tab->pad, tab->remainder);
}
if (!tab_d[0].cur)
{
cst *temp;
temp = tab->next;
if (tab->pad)
FREE (tab->pad);
if (tab_d)
FREE (tab_d);
FREE (tab);
*table = temp;
return 1;
}
return 0;
} /* end of add_table() */
static int get_curpos () {
char *p1, *p2;
if (!obuff.buffer)
return 0;
p1 = obuff.buffer + obuff.real_size - 1;
p2 = p1;
while (p2 > obuff.buffer && *p2 != '\n')
p2--;
if (*p2 != '\n')
return (int)(p1 - p2 + 1);
else
return (int)(p1 - p2);
}
/* We can't use a pointer to a local in a table or column, since it
* could get overwritten by another on the same line.
*/
pad_info_t* make_pad (pad_info_t * p) {
pad_info_t *x;
if (p->len == 0)
return 0;
x = ALLOCATE (pad_info_t, TAG_TEMPORARY, "make_pad");
x->what = p->what;
x->len = p->len;
return x;
}
/**
* Output number num to outbuf as decimal.
*/
static void numadd (outbuffer_t * outbuf, int64_t num) {
int64_t i;
size_t num_l; /* length of num as a string */
int nve; /* true if num negative */
size_t space;
size_t chop;
char *p;
if (num < 0)
{
num = llabs (num);
nve = 1;
}
else
nve = 0;
for (i = num / 10, num_l = nve + 1; i; i /= 10, num_l++);
if ((space = outbuf_extend (outbuf, num_l)))
{
chop = num_l - space;
while (chop--)
num /= 10; /* lose that last digits that got chopped */
p = outbuf->buffer + outbuf->real_size;
outbuf->real_size += space;
p[space] = 0;
if (nve)
{
*p++ = '-';
space--;
}
while (space--)
{
p[space] = (num % 10) + '0';
num /= 10;
}
}
}
static void add_space (outbuffer_t * outbuf, int indent) {
size_t l;
if ((l = outbuf_extend (outbuf, indent)))
{
memset (outbuf->buffer + outbuf->real_size, ' ', l);
*(outbuf->buffer + outbuf->real_size + l) = 0;
outbuf->real_size += l;
}
}
static int guard = 0;
/**
* Converts any LPC datatype into an arbitrary string format
* and returns a pointer to this string.
* Scary number of parameters for a recursive function.
*/
void svalue_to_string (svalue_t * obj, outbuffer_t * outbuf, int indent, char delimiter, int flags) {
int i;
/* prevent an infinite recursion on self-referential structures */
if (indent > 20)
{
outbuf_add (outbuf, "...");
return;
}
if (indent > 0 &&
0==(flags & SV2STR_NOINDENT) &&
0==(flags & SV2STR_DONEINDENT))
add_space (outbuf, indent);
flags &= ~SV2STR_DONEINDENT; /* do not pass this down recursion */
switch (obj->type)
{
case T_INVALID:
outbuf_add (outbuf, "T_INVALID");
break;
case T_LVALUE:
outbuf_add (outbuf, "lvalue: ");
svalue_to_string (obj->u.lvalue, outbuf, indent + 2, delimiter, flags);
break;
case T_NUMBER:
numadd (outbuf, obj->u.number);
break;
case T_REAL:
outbuf_addv (outbuf, "%g", obj->u.real);
break;
case T_STRING:
{
const char* str;
outbuf_add (outbuf, "\"");
for (str = SVALUE_STRPTR(obj); *str; ++str)
{
switch (*str)
{
case '\t':
outbuf_add (outbuf, "\\t");
break;
case '\n':
outbuf_add (outbuf, "\\n");
break;
case '\r':
outbuf_add (outbuf, "\\r");
break;
case '\b':
outbuf_add (outbuf, "\\b");
break;
case '\x7':
outbuf_add (outbuf, "\\a");
break;
case '\x1b':
outbuf_add (outbuf, "\\e");
break;
case '\\':
outbuf_add (outbuf, "\\\\");
break;
case '"':
outbuf_add (outbuf, "\\\"");
break;
default:
if (iscntrl (*str))
outbuf_addv (outbuf, "\\0%o", *str);
else
outbuf_addchar (outbuf, *str);
break;
}
}
outbuf_add (outbuf, "\"");
break;
}
case T_CLASS:
{
int n = obj->u.arr->size;
outbuf_add (outbuf, "CLASS( ");
numadd (outbuf, n);
outbuf_add (outbuf, n == 1 ? " element\n" : " elements\n");
for (i = 0; i < (obj->u.arr->size) - 1; i++)
svalue_to_string (&(obj->u.arr->item[i]), outbuf, indent + 2, ',', flags);
svalue_to_string (&(obj->u.arr->item[i]), outbuf, indent + 2, 0, flags);
if (0==(flags & SV2STR_NONEWLINE))
outbuf_addchar (outbuf, '\n');
if (indent > 0 && 0==(flags & SV2STR_NOINDENT))
add_space (outbuf, indent);
outbuf_add (outbuf, " )");
break;
}
case T_ARRAY:
if (!(obj->u.arr->size))
{
outbuf_add (outbuf, "({ })");
}
else
{
outbuf_add (outbuf, "({");
if (0==(flags & SV2STR_NONEWLINE))
outbuf_addchar (outbuf, '\n');
#if 0
outbuf_add (outbuf, "({ /* sizeof() == ");
numadd (outbuf, obj->u.arr->size);
outbuf_add (outbuf, " */\n");
#endif
for (i = 0; i < obj->u.arr->size; i++)
{
svalue_to_string (
&(obj->u.arr->item[i]), outbuf, indent + 2,
(i==obj->u.arr->size-1 && (flags & SV2STR_NOINDENT))?0:',',
flags
);
}
if (indent > 0 && 0==(flags & SV2STR_NOINDENT))
add_space (outbuf, indent);
outbuf_add (outbuf, "})");
}
break;
case T_BUFFER:
outbuf_add (outbuf, "<buffer>");
break;
case T_FUNCTION:
{
outbuf_add (outbuf, "(: ");
switch (obj->u.fp->hdr.type)
{
case FP_LOCAL | FP_NOT_BINDABLE:
outbuf_add (outbuf, function_name (obj->u.fp->hdr.owner->prog, obj->u.fp->f.local.index));
break;
case FP_SIMUL:
outbuf_add (outbuf, simuls[obj->u.fp->f.simul.index].func->name);
break;
case FP_FUNCTIONAL:
case FP_FUNCTIONAL | FP_NOT_BINDABLE:
{
char buf[10];
int n = obj->u.fp->f.functional.num_arg;
outbuf_add (outbuf, "<code>(");
for (i = 1; i < n; i++)
{
snprintf (buf, sizeof (buf), "$%i, ", i);
outbuf_add (outbuf, buf);
}
if (n)
{
snprintf (buf, sizeof (buf), "$%i", n);
outbuf_add (outbuf, buf);
}
outbuf_add (outbuf, ")");
break;
}
case FP_EFUN:
{
i = obj->u.fp->f.efun.index;
outbuf_add (outbuf, instrs[i].name);
break;
}
}
if (obj->u.fp->hdr.args)
{
for (i = 0; i < obj->u.fp->hdr.args->size; i++)
{
outbuf_add (outbuf, ", ");
svalue_to_string (&(obj->u.fp->hdr.args->item[i]), outbuf, indent, 0, flags);
}
}
}
outbuf_add (outbuf, " :)");
break;
case T_MAPPING:
if (!(obj->u.map->count))
{
outbuf_add (outbuf, "([ ])");
}
else
{
int count = 0;
outbuf_add (outbuf, "([");
if (0==(flags & SV2STR_NONEWLINE))
outbuf_addchar (outbuf, '\n');
#if 0
outbuf_add (outbuf, "([ /* ");
numadd (outbuf, obj->u.map->count);
outbuf_add (outbuf, " element(s) */\n");
#endif
for (i = 0; i <= (int) (obj->u.map->table_size); i++)
{
mapping_node_t *elm;
for (elm = obj->u.map->table[i]; elm; elm = elm->next)
{
svalue_to_string (&(elm->values[0]), outbuf, indent + 2, ':', flags | SV2STR_NONEWLINE);
svalue_to_string (
&(elm->values[1]), outbuf, indent + 2,
((++count==obj->u.map->count) && (flags & SV2STR_NOINDENT)) ? 0 : ',',
flags | SV2STR_DONEINDENT
);
}
}
if (indent > 0 && 0==(flags & SV2STR_NOINDENT))
add_space (outbuf, indent);
outbuf_add (outbuf, "])");
}
break;
case T_OBJECT:
{
svalue_t *temp;
if (obj->u.ob->flags & O_DESTRUCTED)
{
numadd (outbuf, 0);
break;
}
outbuf_add (outbuf, obj->u.ob->name);
if (!get_error_state (ES_STACK_FULL))
{
push_object (obj->u.ob);
guard = 1;
temp = APPLY_SLOT_SAFE_MASTER_CALL (APPLY_OBJECT_NAME, 1);
guard = 0;
if (temp && temp != (svalue_t *) - 1 && (temp->type == T_STRING))
{
outbuf_add (outbuf, " (\"");
outbuf_add (outbuf, SVALUE_STRPTR(temp));
outbuf_add (outbuf, "\")");
}
APPLY_SLOT_FINISH_CALL();
}
break;
}
default:
outbuf_add (outbuf, "!ERROR: garbage svalue!");
} /* end of switch (obj->type) */
if (delimiter)
{
outbuf_addchar (outbuf, delimiter);
if (0==(flags & SV2STR_NONEWLINE))
outbuf_addchar (outbuf, '\n');
else
outbuf_addchar (outbuf, ' ');
}
} /* end of svalue_to_string() */
/*
* THE (s)printf() function.
* It returns a pointer to it's internal buffer (or a string in the text
* segment) thus, the string must be copied if it has to survive after
* this function is called again, or if it's going to be modified (esp.
* if it risks being free()ed).
*/
char* string_print_formatted (const char *format_str, int argc, svalue_t * argv) {
format_info finfo;
svalue_t *carg; /* current arg */
unsigned int nelemno = 0; /* next offset into array */
unsigned int fpos; /* position in format_str */
int fs; /* field size */
int pres; /* presision */
pad_info_t pad; /* fs pad string */
unsigned int i;
char *retvalue;
int last;
/* prevent recursion, since many of our important variables are global */
if (guard)
{
guard = 0;
error ("Illegal to use sprintf() within the object_name() master call.\n");
}
/* free anything that is sitting around here from errors */
if (clean.type != T_NUMBER)
{
free_svalue (&clean, "sprintf error");
clean.type = T_NUMBER;
}
/* The string we construct */
if (obuff.buffer)
FREE_MSTR (obuff.buffer);
outbuf_zero (&obuff);
/* Table info */
while (csts)
{
cst *next = csts->next;
if (!(csts->info & INFO_COLS) && csts->d.tab)
FREE (csts->d.tab);
FREE (csts);
csts = next;
}
cur_arg = -1;
last = 0;
for (fpos = 0; 1; fpos++)
{
char c = format_str[fpos];
if (c == '\n' || !c)
{
int column_stat = 0;
if (last != (int)fpos)
{
add_nstr (format_str + last, fpos - last);
last = fpos + 1;
}
else
last++;
if (!csts)
{