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
54
61
62
63
64
65
66
67
70
71
72
73
74
81
82
83
84
85
95
96
97
98
99
100
101
102
103
104
109
110
111
112
116
117
118
123
124
125
126
127
132
133
134
135
136
137
138
139
140
141
142
143
144
152
153
154
155
156
157
158
167
168
169
170
171
172
173
174
175
176
180
181
182
186
189
190
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
214
221
228
235
242
249
256
258
259
260
/* ... */
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <jim-subcmd.h>
6 includes
static int array_cmd_exists(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED);
Jim_SetResultInt(interp, dictObj && Jim_DictSize(interp, dictObj) != -1);
return JIM_OK;
}{ ... }
static int array_cmd_get(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
Jim_Obj *patternObj;
if (!objPtr) {
return JIM_OK;
}if (!objPtr) { ... }
patternObj = (argc == 1) ? NULL : argv[1];
if (patternObj == NULL || Jim_CompareStringImmediate(interp, patternObj, "*")) {
if (Jim_IsList(objPtr) && Jim_ListLength(interp, objPtr) % 2 == 0) {
Jim_SetResult(interp, objPtr);
return JIM_OK;
}if (Jim_IsList(objPtr) && Jim_ListLength(interp, objPtr) % 2 == 0) { ... }
}if (patternObj == NULL || Jim_CompareStringImmediate(interp, patternObj, "*")) { ... }
return Jim_DictMatchTypes(interp, objPtr, patternObj, JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS | JIM_DICTMATCH_VALUES);
}{ ... }
static int array_cmd_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
if (!objPtr) {
return JIM_OK;
}if (!objPtr) { ... }
return Jim_DictMatchTypes(interp, objPtr, argc == 1 ? NULL : argv[1], JIM_DICTMATCH_KEYS, JIM_DICTMATCH_KEYS);
}{ ... }
static int array_cmd_unset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int i;
int len;
Jim_Obj *resultObj;
Jim_Obj *objPtr;
Jim_Obj **dictValuesObj;
if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) {
Jim_UnsetVariable(interp, argv[0], JIM_NONE);
return JIM_OK;
}if (argc == 1 || Jim_CompareStringImmediate(interp, argv[1], "*")) { ... }
objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
if (objPtr == NULL) {
return JIM_OK;
}if (objPtr == NULL) { ... }
dictValuesObj = Jim_DictPairs(interp, objPtr, &len);
if (dictValuesObj == NULL) {
Jim_SetResultString(interp, "", -1);
return JIM_OK;
}if (dictValuesObj == NULL) { ... }
resultObj = Jim_NewDictObj(interp, NULL, 0);
for (i = 0; i < len; i += 2) {
if (!Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) {
Jim_DictAddElement(interp, resultObj, dictValuesObj[i], dictValuesObj[i + 1]);
}if (!Jim_StringMatchObj(interp, argv[1], dictValuesObj[i], 0)) { ... }
}for (i = 0; i < len; i += 2) { ... }
Jim_SetVariable(interp, argv[0], resultObj);
return JIM_OK;
}{ ... }
static int array_cmd_size(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr;
int len = 0;
objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
if (objPtr) {
len = Jim_DictSize(interp, objPtr);
if (len < 0) {
Jim_SetResultInt(interp, 0);
return JIM_OK;
}if (len < 0) { ... }
}if (objPtr) { ... }
Jim_SetResultInt(interp, len);
return JIM_OK;
}{ ... }
static int array_cmd_stat(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *objPtr = Jim_GetVariable(interp, argv[0], JIM_NONE);
if (objPtr) {
return Jim_DictInfo(interp, objPtr);
}if (objPtr) { ... }
Jim_SetResultFormatted(interp, "\"%#s\" isn't an array", argv[0], NULL);
return JIM_ERR;
}{ ... }
static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int i;
int len;
Jim_Obj *listObj = argv[1];
Jim_Obj *dictObj;
len = Jim_ListLength(interp, listObj);
if (len % 2) {
Jim_SetResultString(interp, "list must have an even number of elements", -1);
return JIM_ERR;
}if (len % 2) { ... }
dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED);
if (!dictObj) {
return Jim_SetVariable(interp, argv[0], listObj);
}if (!dictObj) { ... }
else if (Jim_DictSize(interp, dictObj) < 0) {
return JIM_ERR;
}else if (Jim_DictSize(interp, dictObj) < 0) { ... }
if (Jim_IsShared(dictObj)) {
dictObj = Jim_DuplicateObj(interp, dictObj);
}if (Jim_IsShared(dictObj)) { ... }
for (i = 0; i < len; i += 2) {
Jim_Obj *nameObj;
Jim_Obj *valueObj;
Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE);
Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE);
Jim_DictAddElement(interp, dictObj, nameObj, valueObj);
}for (i = 0; i < len; i += 2) { ... }
return Jim_SetVariable(interp, argv[0], dictObj);
}{ ... }
static const jim_subcmd_type array_command_table[] = {
{ "exists",
"arrayName",
array_cmd_exists,
1,
1,
...},
{ "get",
"arrayName ?pattern?",
array_cmd_get,
1,
2,
...},
{ "names",
"arrayName ?pattern?",
array_cmd_names,
1,
2,
...},
{ "set",
"arrayName list",
array_cmd_set,
2,
2,
...},
{ "size",
"arrayName",
array_cmd_size,
1,
1,
...},
{ "stat",
"arrayName",
array_cmd_stat,
1,
1,
...},
{ "unset",
"arrayName ?pattern?",
array_cmd_unset,
1,
2,
...},
{ NULL
...}
...};
int Jim_arrayInit(Jim_Interp *interp)
{
Jim_PackageProvideCheck(interp, "array");
Jim_CreateCommand(interp, "array", Jim_SubCmdProc, (void *)array_command_table, NULL);
return JIM_OK;
}{ ... }