-
Notifications
You must be signed in to change notification settings - Fork 123
/
jim-mk.cpp
2276 lines (1833 loc) · 63.6 KB
/
jim-mk.cpp
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
#include <string.h>
#include <ctype.h>
#include <new>
#include <mk4.h>
#include "jim.h"
#include "jimautoconf.h"
#include "jim-subcmd.h"
extern "C" { /* The whole file is essentially C */
#define MK_PROPERTY_BINARY 'B'
#define MK_PROPERTY_INT 'I'
#define MK_PROPERTY_LONG 'L'
#define MK_PROPERTY_FLOAT 'F'
#define MK_PROPERTY_DOUBLE 'D'
#define MK_PROPERTY_STRING 'S'
#define MK_PROPERTY_VIEW 'V'
#define MK_MODE_ORIGINAL -1
#define MK_MODE_READONLY 0
#define MK_MODE_READWRITE 1
#define MK_MODE_EXTEND 2
#define MK_CMD_LEN 32
#define JIM_CURSOR_SPACE (35+JIM_REFERENCE_TAGLEN + 1 + 20)
#define JIM_POSITION_SPACE 32
#define MK_VERSION_SPACE 16
#define JIM_MK_DESCR_LEN 64 /* Default, will be reallocated if needed */
#define isnamech(c) ( (c) && !strchr(":,[^]!", (c)) )
#ifndef max
#define max(x, y) ((x) >= (y) ? (x) : (y))
#endif
/* utilities */
static int JimCheckMkName(Jim_Interp *interp, Jim_Obj *name, const char *type);
static const char *JimMkTypeName(char type);
static Jim_Obj *JimFromMkDescription(Jim_Interp *interp, const char *descr, const char **endPtr);
static int JimToMkDescription(Jim_Interp *interp, Jim_Obj *obj, char **descrPtr);
static Jim_Obj *JimGetMkValue(Jim_Interp *interp, c4_Cursor cur, const c4_Property &prop);
static int JimSetMkValue(Jim_Interp *interp, c4_Cursor cur, const c4_Property &prop, Jim_Obj *obj);
static int JimPipelineBoundary(int argc, Jim_Obj *const *argv);
/* property object */
static Jim_Obj *JimNewPropertyObj (Jim_Interp *interp, c4_Property prop);
static int JimGetProperty (Jim_Interp *interp, Jim_Obj *obj,
c4_View view, const char *what, const c4_Property **propPtr);
static int JimGetPropertyTyped (Jim_Interp *interp, Jim_Obj *obj,
char type, const c4_Property **propPtr);
static int JimGetNewProperty (Jim_Interp *interp, Jim_Obj *obj,
c4_View view, char type, const c4_Property **propPtr);
static int JimGetProperties (Jim_Interp *interp, int objc, Jim_Obj *const *objv,
c4_View view, c4_View *propsPtr);
static Jim_Obj *JimViewPropertiesList (Jim_Interp *interp, c4_View view);
/* cursor object */
static int JimGetPosition (Jim_Interp *interp, Jim_Obj *obj, c4_View view, int *indexPtr);
static int JimGetCursor (Jim_Interp *interp, Jim_Obj *obj, c4_Cursor *curPtr);
static int JimGetCursorView (Jim_Interp *interp, Jim_Obj *obj,
Jim_Obj **viewObjPtr);
static int JimCursorPos (Jim_Interp *interp, Jim_Obj *obj, Jim_Obj **posObjPtr);
static int JimIncrCursor (Jim_Interp *interp, Jim_Obj *obj, int offset);
static int JimSeekCursor (Jim_Interp *interp, Jim_Obj *obj, Jim_Obj *posObj);
/* Also accepts JIM_ERRMSG */
#define JIM_CURSOR_GET (1 << JIM_PRIV_FLAG_SHIFT)
#define JIM_CURSOR_SET (2 << JIM_PRIV_FLAG_SHIFT)
#define JIM_CURSOR_INSERT (4 << JIM_PRIV_FLAG_SHIFT)
static int JimCheckCursor (Jim_Interp *interp, Jim_Obj *curObj, int flags);
/* view handle */
static Jim_Obj *JimNewViewObj (Jim_Interp *interp, c4_View view);
static int JimGetView (Jim_Interp *interp, Jim_Obj *obj, c4_View *viewPtr);
static void JimPinView (Jim_Interp *interp, Jim_Obj *obj);
/* -------------------------------------------------------------------------
* Utilities
* ------------------------------------------------------------------------- */
static int JimCheckMkName(Jim_Interp *interp, Jim_Obj *name, const char *type)
{
const char *s;
int i, len;
s = Jim_GetString(name, &len);
if (len > 0 && s[0] == '-')
goto err;
for (i = 0; i < len; i++) {
if (!isnamech(s[i]))
goto err;
}
return JIM_OK;
err:
Jim_SetResultFormatted(interp, "expected %s name but got \"%#s\"", type ? type : "property", name);
return JIM_ERR;
}
static const char *const jim_mktype_options[] = {
"-integer",
"-long",
"-float",
"-double",
"-string",
"-subview",
/* FIXME "-binary", */
0
};
static const char *const jim_mktype_names[] = {
"integer",
"long",
"float",
"double",
"string",
"subview",
/* FIXME "binary", */
0
};
static const char jim_mktype_types[] = {
MK_PROPERTY_INT,
MK_PROPERTY_LONG,
MK_PROPERTY_FLOAT,
MK_PROPERTY_DOUBLE,
MK_PROPERTY_STRING,
MK_PROPERTY_VIEW,
/* MK_PROPERTY_BINARY, */
};
#define JIM_MKTYPES ((int)(sizeof(jim_mktype_types) / sizeof(jim_mktype_types[0])))
static const char *JimMkTypeName(char type)
{
int i;
for (i = 0; i < JIM_MKTYPES; i++) {
if (type == jim_mktype_types[i])
return jim_mktype_names[i];
}
return "(unknown type)";
}
static Jim_Obj *JimFromMkDescription(Jim_Interp *interp, const char *descr, const char **endPtr)
{
Jim_Obj *result;
const char *delim;
result = Jim_NewListObj(interp, NULL, 0);
for (;;) {
if (*descr == ']') {
descr++;
break;
}
else if (*descr == '\0')
break;
else if (*descr == ',')
descr++;
delim = strpbrk(descr, ",:[]");
/* JimPanic((!delim, "Invalid Metakit description string")); */
Jim_ListAppendElement(interp, result,
Jim_NewStringObj(interp, descr, delim - descr));
if (delim[0] == '[') {
Jim_ListAppendElement(interp, result,
JimFromMkDescription(interp, delim + 1, &descr));
}
else if (delim[0] == ':') {
Jim_ListAppendElement(interp, result,
Jim_NewStringObj(interp, JimMkTypeName(delim[1]), -1));
descr = delim + 2;
}
else {
/* Seems that Metakit never generates descriptions without type
* tags, but let's handle this just to be safe
*/
Jim_ListAppendElement(interp, result,
Jim_NewStringObj(interp, JimMkTypeName(MK_PROPERTY_STRING), -1));
}
}
if (endPtr)
*endPtr = descr;
return result;
}
/* This allocates the buffer once per user call and stores it in a static
* variable. Recursive calls are distinguished by descrPtr == NULL.
*/
static int JimToMkDescription(Jim_Interp *interp, Jim_Obj *descrObj, char **descrPtr)
{
static char *descr, *outPtr;
static int bufSize;
#define ENLARGE(size) do { \
if ((descr - outPtr) + (size) > bufSize) { \
bufSize = max(2*bufSize, (descr - outPtr) + (size)); \
descr = (char *)Jim_Realloc(descr, bufSize); \
} \
} while(0)
int i, count;
Jim_Obj *name, *struc;
const char *rep;
int len;
count = Jim_ListLength(interp, descrObj);
if (count % 2) {
Jim_SetResultString(interp,
"view description must have an even number of elements", -1);
return JIM_ERR;
}
if (descrPtr) {
descr = (char *)Jim_Alloc(bufSize = JIM_MK_DESCR_LEN);
outPtr = descr;
}
for (i = 0; i < count; i += 2) {
Jim_ListIndex(interp, descrObj, i, &name, 0);
Jim_ListIndex(interp, descrObj, i + 1, &struc, 0);
if (JimCheckMkName(interp, name, NULL) != JIM_OK)
goto err;
rep = Jim_GetString(name, &len);
ENLARGE(len + 3); /* At least :T, or [], */
memcpy(outPtr, rep, len);
outPtr += len;
if (Jim_ListLength(interp, struc) == 1) {
int idx;
if (Jim_GetEnum(interp, struc, jim_mktype_names, &idx,
"property type", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
goto err;
*outPtr++ = ':';
*outPtr++ = jim_mktype_types[idx];
}
else {
*outPtr++ = '[';
if (JimToMkDescription(interp, struc, NULL) != JIM_OK)
goto err;
ENLARGE(2); /* bracket, comma */
*outPtr++ = ']';
}
*outPtr++ = ',';
}
*(--outPtr) = '\0';
#undef ENLARGE
if (descrPtr) {
*descrPtr = (char *)Jim_Realloc(descr, strlen(descr) + 1);
descr = NULL; /* Safety measure */
}
return JIM_OK;
err:
if (descrPtr)
Jim_Free(descr);
return JIM_ERR;
}
static Jim_Obj *JimGetMkValue(Jim_Interp *interp, c4_Cursor cur, const c4_Property &prop)
{
switch (prop.Type()) {
case MK_PROPERTY_INT:
return Jim_NewIntObj(interp, ((c4_IntProp &)prop).Get(*cur));
case MK_PROPERTY_LONG:
return Jim_NewIntObj(interp, ((c4_LongProp &)prop).Get(*cur));
case MK_PROPERTY_FLOAT:
return Jim_NewDoubleObj(interp, ((c4_FloatProp &)prop).Get(*cur));
case MK_PROPERTY_DOUBLE:
return Jim_NewDoubleObj(interp, ((c4_DoubleProp &)prop).Get(*cur));
case MK_PROPERTY_STRING:
return Jim_NewStringObj(interp, ((c4_StringProp &)prop).Get(*cur), -1);
case MK_PROPERTY_VIEW:
return JimNewViewObj(interp, ((c4_ViewProp &)prop).Get(*cur));
case MK_PROPERTY_BINARY:
/* FIXME */
default:
/* FIXME Something more meaningful here? */
return Jim_NewEmptyStringObj(interp);
}
}
static int JimSetMkValue(Jim_Interp *interp, c4_Cursor cur, const c4_Property &prop, Jim_Obj *obj)
{
switch (prop.Type()) {
case MK_PROPERTY_INT: {
jim_wide value;
if (Jim_GetWide(interp, obj, &value) != JIM_OK)
return JIM_ERR;
((c4_IntProp &)prop).Set(*cur, value);
return JIM_OK;
}
case MK_PROPERTY_LONG: {
jim_wide value;
if (Jim_GetWide(interp, obj, &value) != JIM_OK)
return JIM_ERR;
((c4_LongProp &)prop).Set(*cur, value);
return JIM_OK;
}
case MK_PROPERTY_FLOAT: {
double value;
if (Jim_GetDouble(interp, obj, &value) != JIM_OK)
return JIM_ERR;
((c4_FloatProp &)prop).Set(*cur, value);
return JIM_OK;
}
case MK_PROPERTY_DOUBLE: {
double value;
if (Jim_GetDouble(interp, obj, &value) != JIM_OK)
return JIM_ERR;
((c4_DoubleProp &)prop).Set(*cur, value);
return JIM_OK;
}
case MK_PROPERTY_STRING: {
int len;
const char *rep;
rep = Jim_GetString(obj, &len);
if (len != (int)strlen(rep)) {
Jim_SetResultString(interp, "null characters are not allowed in Metakit strings", -1);
return JIM_ERR;
}
((c4_StringProp &)prop).Set(*cur, rep);
return JIM_OK;
}
case MK_PROPERTY_VIEW: {
c4_View value;
if (JimGetView(interp, obj, &value) != JIM_OK)
return JIM_ERR;
((c4_ViewProp &)prop).Set(*cur, value);
}
case MK_PROPERTY_BINARY:
/* FIXME */
default:
Jim_SetResultString(interp, "unsupported Metakit type", -1);
return JIM_ERR;
}
}
static int JimPipelineBoundary(int argc, Jim_Obj *const *argv) {
const char *rep;
int pipe, len;
for (pipe = 0; pipe < argc; pipe++) {
rep = Jim_GetString(argv[pipe], &len);
if (len == 1 && rep[0] == '|')
break;
}
return pipe;
}
/* -------------------------------------------------------------------------
* Property object
* ------------------------------------------------------------------------- */
#define JimPropertyValue(o) ((c4_Property *)((o)->internalRep.ptr))
static void FreePropertyInternalRep(Jim_Interp *interp, Jim_Obj *obj)
{
delete JimPropertyValue(obj);
}
static void DupPropertyInternalRep(Jim_Interp *interp, Jim_Obj *oldObj, Jim_Obj *newObj)
{
newObj->internalRep.ptr = new c4_Property(*JimPropertyValue(oldObj));
newObj->typePtr = oldObj->typePtr;
}
static void UpdateStringOfProperty(Jim_Obj* obj)
{
const char *name = JimPropertyValue(obj)->Name();
int len = strlen(name);
obj->bytes = (char *) Jim_Alloc(len + 1);
memcpy(obj->bytes, name, len + 1);
obj->length = len;
}
static Jim_ObjType propertyObjType = {
"mk.property",
FreePropertyInternalRep,
DupPropertyInternalRep,
UpdateStringOfProperty,
JIM_TYPE_NONE
};
static int JimGetProperty(Jim_Interp *interp, Jim_Obj *obj, c4_View view, const char *name, const c4_Property **propPtr)
{
int index;
if (obj->typePtr == &propertyObjType) {
index = view.FindProperty(JimPropertyValue(obj)->GetId());
}
else {
if (JimCheckMkName(interp, obj, name) != JIM_OK)
return JIM_ERR;
index = view.FindPropIndexByName(Jim_String(obj));
}
if (index != -1) {
*propPtr = &view.NthProperty(index);
return JIM_OK;
}
else {
Jim_SetResultFormatted(interp, "%s \"%#s\" does not exist",
name ? name : "property", obj);
return JIM_ERR;
}
}
static int JimGetPropertyTyped(Jim_Interp *interp, Jim_Obj *obj, char type, const c4_Property **propPtr)
{
c4_Property *prop;
if (obj->typePtr == &propertyObjType) {
if (JimPropertyValue(obj)->Type() != type) {
/* coerce the property type */
prop = new c4_Property(type, JimPropertyValue(obj)->Name());
delete JimPropertyValue(obj);
obj->internalRep.ptr = prop;
}
}
else {
if (JimCheckMkName(interp, obj, NULL) != JIM_OK)
return JIM_ERR;
prop = new c4_Property(type, Jim_String(obj));
Jim_FreeIntRep(interp, obj);
obj->typePtr = &propertyObjType;
obj->internalRep.ptr = (void *)prop;
}
*propPtr = JimPropertyValue(obj);
return JIM_OK;
}
static int JimGetNewProperty(Jim_Interp *interp, Jim_Obj *obj, c4_View view, char type, const c4_Property **propPtr)
{
const c4_Property *newp, *prop;
if (JimGetPropertyTyped(interp, obj, type, &newp) != JIM_OK)
return JIM_ERR;
prop = &view.NthProperty(view.AddProperty(*newp));
if (prop->Type() != newp->Type()) {
Jim_SetResultFormatted(interp, "property \"%#s\" is %s, not %s",
obj, JimMkTypeName(prop->Type()), JimMkTypeName(newp->Type()));
return JIM_ERR;
}
*propPtr = prop;
return JIM_OK;
}
static int JimGetProperties(Jim_Interp *interp, int objc, Jim_Obj *const *objv, c4_View view, c4_View *propsPtr)
{
int i;
const c4_Property *prop;
c4_View props;
for (i = 0; i < objc; i++) {
if (JimGetProperty(interp, objv[i], view, NULL, &prop) != JIM_OK)
return JIM_ERR;
props.AddProperty(*prop);
}
*propsPtr = props;
return JIM_OK;
}
static Jim_Obj *JimNewPropertyObj(Jim_Interp *interp, c4_Property prop)
{
Jim_Obj *obj;
obj = Jim_NewObj(interp);
obj->typePtr = &propertyObjType;
obj->bytes = NULL;
obj->internalRep.ptr = new c4_Property(prop);
return obj;
}
/* -------------------------------------------------------------------------
* Cursor object
* ------------------------------------------------------------------------- */
/* Position ---------------------------------------------------------------- */
/* A normal position if endFlag == 0; otherwise an offset from end+1 (!) */
typedef struct MkPosition {
int index;
int endFlag;
} MkPosition;
/* This is mostly the same as SetIndexFromAny, but preserves more information
* and allows multiple [+-]integer parts.
*/
static int GetPosition(Jim_Interp *interp, Jim_Obj *obj, MkPosition *posPtr)
{
MkPosition pos;
const char *rep;
char *end;
int sign, offset;
rep = Jim_String(obj);
if (strncmp(rep, "end", 3) == 0) {
pos.endFlag = 1;
pos.index = -1;
rep += 3;
}
else {
pos.endFlag = 0;
pos.index = strtol(rep, &end, 10);
if (end == rep)
goto err;
rep = end;
}
while ((rep[0] == '+') || (rep[0] == '-')) {
sign = (rep[0] == '+' ? 1 : -1);
rep++;
offset = strtol(rep, &end, 10);
if (end == rep)
goto err;
pos.index += sign * offset;
rep = end;
}
while (isspace(UCHAR(*rep)))
rep++;
if (*rep != '\0')
goto err;
*posPtr = pos;
return JIM_OK;
err:
Jim_SetResultFormatted(interp, "expected cursor position but got \"%#s\"", obj);
return JIM_ERR;
}
static int PositionIndex(const MkPosition *posPtr, c4_View view)
{
if (posPtr->endFlag)
return view.GetSize() + posPtr->index;
else
return posPtr->index;
}
static int JimGetPosition(Jim_Interp *interp, Jim_Obj *obj, c4_View view, int *indexPtr)
{
MkPosition pos;
if (GetPosition(interp, obj, &pos) != JIM_OK)
return JIM_ERR;
*indexPtr = PositionIndex(&pos, view);
return JIM_OK;
}
/* Cursor type ------------------------------------------------------------- */
typedef struct MkCursor {
MkPosition pos;
Jim_Obj *viewObj;
} MkCursor;
#define JimCursorValue(obj) ((MkCursor *)(obj->internalRep.ptr))
static void FreeCursorInternalRep(Jim_Interp *interp, Jim_Obj *obj)
{
Jim_DecrRefCount(interp, JimCursorValue(obj)->viewObj);
Jim_Free(obj->internalRep.ptr);
}
static void DupCursorInternalRep(Jim_Interp *interp, Jim_Obj *oldObj, Jim_Obj *newObj)
{
newObj->internalRep.ptr = Jim_Alloc(sizeof(MkCursor));
*JimCursorValue(newObj) = *JimCursorValue(oldObj);
Jim_IncrRefCount(JimCursorValue(oldObj)->viewObj);
newObj->typePtr = oldObj->typePtr;
}
static void UpdateStringOfCursor(Jim_Obj *obj)
{
char buf[JIM_CURSOR_SPACE + 1];
MkCursor *curPtr = JimCursorValue(obj);
int idx, len;
len = snprintf(buf, JIM_CURSOR_SPACE + 1, "%s!", Jim_String(curPtr->viewObj));
if (curPtr->pos.endFlag) {
idx = curPtr->pos.index + 1;
if (idx == 0)
len += snprintf(buf + len, JIM_CURSOR_SPACE + 1 - len, "end");
else
len += snprintf(buf + len, JIM_CURSOR_SPACE + 1 - len, "end%+d", idx);
}
else {
len += snprintf(buf + len, JIM_CURSOR_SPACE + 1 - len, "%d",
curPtr->pos.index);
}
obj->bytes = (char *)Jim_Alloc(len + 1);
memcpy(obj->bytes, buf, len + 1);
obj->length = len;
}
static Jim_ObjType cursorObjType = {
"mk.cursor",
FreeCursorInternalRep,
DupCursorInternalRep,
UpdateStringOfCursor,
JIM_TYPE_REFERENCES
};
static int SetCursorFromAny(Jim_Interp *interp, Jim_Obj *obj)
{
const char *rep, *delim;
int len;
Jim_Obj *posObj;
MkCursor cur;
rep = Jim_GetString(obj, &len);
delim = strrchr(rep, '!');
if (!delim) {
Jim_SetResultFormatted(interp, "expected cursor but got \"%#s\"", obj);
return JIM_ERR;
}
cur.viewObj = Jim_NewStringObj(interp, rep, delim - rep);
posObj = Jim_NewStringObj(interp, delim + 1, len - (delim - rep) - 1);
if (GetPosition(interp, posObj, &cur.pos) != JIM_OK) {
Jim_FreeNewObj(interp, posObj);
Jim_FreeNewObj(interp, cur.viewObj);
return JIM_ERR;
}
Jim_FreeIntRep(interp, obj);
Jim_FreeNewObj(interp, posObj);
Jim_IncrRefCount(cur.viewObj);
obj->typePtr = &cursorObjType;
obj->internalRep.ptr = Jim_Alloc(sizeof(MkCursor));
*JimCursorValue(obj) = cur;
return JIM_OK;
}
/* Functions --------------------------------------------------------------- */
static int JimCursorPos(Jim_Interp *interp, Jim_Obj *obj, Jim_Obj **posObjPtr)
{
if (obj->typePtr != &cursorObjType && SetCursorFromAny(interp, obj) != JIM_OK)
return JIM_ERR;
*posObjPtr = Jim_NewStringObj(interp, strrchr(Jim_String(obj), '!') + 1, -1);
return JIM_OK;
}
static int JimGetCursorView(Jim_Interp *interp, Jim_Obj *obj, Jim_Obj **viewObjPtr)
{
if (obj->typePtr != &cursorObjType && SetCursorFromAny(interp, obj) != JIM_OK)
return JIM_ERR;
*viewObjPtr = JimCursorValue(obj)->viewObj;
return JIM_OK;
}
static int JimGetCursor(Jim_Interp *interp, Jim_Obj *obj, c4_Cursor *curPtr)
{
c4_View view;
if (obj->typePtr != &cursorObjType && SetCursorFromAny(interp, obj) != JIM_OK)
return JIM_ERR;
if (JimGetView(interp, JimCursorValue(obj)->viewObj, &view) != JIM_OK)
return JIM_ERR;
if (curPtr)
*curPtr = &view[PositionIndex(&JimCursorValue(obj)->pos, view)];
return JIM_OK;
}
static int JimIncrCursor(Jim_Interp *interp, Jim_Obj *obj, int offset)
{
/* JimPanic((Jim_IsShared(obj), "JimIncrCursor called with shared object")) */
if (obj->typePtr != &cursorObjType && SetCursorFromAny(interp, obj) != JIM_OK)
return JIM_ERR;
Jim_InvalidateStringRep(obj);
JimCursorValue(obj)->pos.index += offset;
return JIM_OK;
}
static int JimSeekCursor(Jim_Interp *interp, Jim_Obj *obj, Jim_Obj *posObj)
{
/* JimPanic((Jim_IsShared(obj), "JimSeekCursor called with shared object")) */
if (obj->typePtr != &cursorObjType && SetCursorFromAny(interp, obj) != JIM_OK)
return JIM_ERR;
Jim_InvalidateStringRep(obj);
return GetPosition(interp, posObj, &JimCursorValue(obj)->pos);
}
static int JimCheckCursor(Jim_Interp *interp, Jim_Obj *curObj, int flags)
{
static c4_View nullView;
c4_Cursor cur = &nullView[0];
int size;
if (JimGetCursor(interp, curObj, &cur) != JIM_OK)
return JIM_ERR;
size = (*cur).Container().GetSize();
if ((flags & JIM_CURSOR_GET) && (cur._index < 0 || cur._index >= size)) {
if (flags & JIM_ERRMSG) {
Jim_SetResultFormatted(interp,
"cursor \"%#s\" does not point to an existing row", curObj);
}
return JIM_ERR;
}
else if ((flags & JIM_CURSOR_SET) && cur._index < 0) {
if (flags & JIM_ERRMSG) {
Jim_SetResultFormatted(interp,
"cursor \"%#s\" points before start of view", curObj);
}
return JIM_ERR;
}
else if ((flags & JIM_CURSOR_INSERT) && (cur._index < 0 || cur._index > size)) {
if (flags & JIM_ERRMSG) {
Jim_SetResultFormatted(interp,
"cursor \"%#s\" does not point to a valid insert position", curObj);
}
return JIM_ERR;
}
return JIM_OK;
}
/* Records ----------------------------------------------------------------- */
static int cursor_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
c4_View view;
c4_Cursor cur = &view[0];
if (JimGetCursor(interp, argv[0], &cur) != JIM_OK)
return JIM_ERR;
if (JimCheckCursor(interp, argv[0], JIM_ERRMSG | JIM_CURSOR_GET) != JIM_OK)
return JIM_ERR;
view = (*cur).Container();
if (argc == 1) { /* Return all properties */
int i, count;
Jim_Obj *result;
result = Jim_NewListObj(interp, NULL, 0);
count = view.NumProperties();
for (i = 0; i < count; i++) {
c4_Property prop = view.NthProperty(i);
Jim_ListAppendElement(interp, result, JimNewPropertyObj(interp, prop));
Jim_ListAppendElement(interp, result, JimGetMkValue(interp, cur, prop));
}
Jim_SetResult(interp, result);
return JIM_OK;
}
else { /* Return a single property */
const c4_Property *propPtr;
int pipe;
pipe = JimPipelineBoundary(argc, argv);
if (pipe == 2) {
/* No type annotation, existing property */
if (JimGetProperty(interp, argv[1], view, NULL, &propPtr) != JIM_OK)
return JIM_ERR;
}
else if (pipe == 3) {
/* Explicit type annotation; the property may be new */
int idx;
if (Jim_GetEnum(interp, argv[1], jim_mktype_options, &idx,
"property type", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
return JIM_ERR;
if (JimGetNewProperty(interp, argv[2], view, jim_mktype_types[idx], &propPtr) != JIM_OK)
return JIM_ERR;
}
else {
Jim_WrongNumArgs(interp, 0, NULL, "cursor get ?-type? ?prop?");
return JIM_ERR;
}
Jim_SetResult(interp, JimGetMkValue(interp, cur, *propPtr));
if (pipe == argc)
return JIM_OK;
else
return Jim_EvalObjPrefix(interp, Jim_GetResult(interp), argc - pipe - 1, argv + pipe + 1);
}
}
static int cursor_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
c4_View view;
c4_Cursor cur = &view[0];
const c4_Property *propPtr;
int i, oldSize;
if (JimGetCursor(interp, argv[0], &cur) != JIM_OK)
return JIM_ERR;
if (JimCheckCursor(interp, argv[0], JIM_ERRMSG | JIM_CURSOR_SET) != JIM_OK)
return JIM_ERR;
view = (*cur).Container();
oldSize = view.GetSize();
if (cur._index >= oldSize)
view.SetSize(cur._index + 1);
if (argc == 2) {
/* Update everything except subviews from a dictionary in argv[1].
* No new properties are permitted.
*/
int objc;
Jim_Obj **objv;
if (Jim_DictPairs(interp, argv[1], &objv, &objc) != JIM_OK)
goto err;
for (i = 0; i < objc; i += 2) {
if (JimGetProperty(interp, objv[i], view, NULL, &propPtr) != JIM_OK ||
JimSetMkValue(interp, cur, *propPtr, objv[i+1]) != JIM_OK)
{
Jim_Free(objv);
goto err;
}
}
}
else {
/* Update everything from argv[1..]. New properties are permitted if
* explicitly typed.
*/
for (i = 1; i < argc; i += 2) {
if (Jim_String(argv[i])[0] == '-') {
int idx;
if (i + 2 >= argc) {
Jim_WrongNumArgs(interp, 2, argv, "?-type? prop value ?...?");
goto err;
}
if (Jim_GetEnum(interp, argv[i], jim_mktype_options, &idx,
"property type", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK)
goto err;
if (JimGetNewProperty(interp, argv[i+1], view, jim_mktype_types[idx], &propPtr) != JIM_OK)
goto err;
i++;
}
else {
if (i + 1 >= argc) {
Jim_WrongNumArgs(interp, 2, argv, "?-type? prop value ?...?");
goto err;
}
if (JimGetProperty(interp, argv[i], view, NULL, &propPtr) != JIM_OK)
goto err;
}
if (JimSetMkValue(interp, cur, *propPtr, argv[i+1]) != JIM_OK)
goto err;
}
}
return JIM_OK;
err:
view.SetSize(oldSize);
return JIM_ERR;
}
static int cursor_cmd_insert(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
c4_View view;
c4_Cursor cur = &view[0];
jim_wide count;
if (JimGetCursor(interp, argv[0], &cur) != JIM_OK)
return JIM_ERR;
if (JimCheckCursor(interp, argv[0], JIM_ERRMSG | JIM_CURSOR_INSERT) != JIM_OK)
return JIM_ERR;
view = (*cur).Container();
if (argc == 1)
count = 1;
else {
if (Jim_GetWide(interp, argv[1], &count) != JIM_OK)
return JIM_ERR;
}
if (count > 0) {
c4_Row empty;
view.InsertAt(cur._index, empty, (int)count);
}
Jim_SetEmptyResult(interp);
return JIM_OK;
}
static int cursor_cmd_remove(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
c4_View view;
c4_Cursor cur = &view[0];
int pos;
jim_wide count;
if (JimGetCursor(interp, argv[0], &cur) != JIM_OK)
return JIM_ERR;
if (JimCheckCursor(interp, argv[0], JIM_ERRMSG | JIM_CURSOR_SET) != JIM_OK)
return JIM_ERR;
view = (*cur).Container();
pos = cur._index;
if (argc == 1)
count = 1;
else {
if (Jim_GetWide(interp, argv[1], &count) != JIM_OK)
return JIM_ERR;
}
if (pos + count < view.GetSize())
count = view.GetSize() - pos;
if (pos < view.GetSize())
view.RemoveAt(pos, (int)count);
return JIM_OK;
}
/* Attributes -------------------------------------------------------------- */
static int cursor_cmd_view(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *viewObj;
if (JimGetCursorView(interp, argv[0], &viewObj) != JIM_OK)
return JIM_ERR;