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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
84
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
135
136
147
148
154
155
156
157
158
159
163
164
165
166
167
168
169
170
171
174
175
176
179
180
181
182
183
184
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
207
208
213
214
218
219
222
223
224
225
229
232
235
236
237
238
239
247
263
271
274
282
283
287
288
289
290
293
296
299
300
301
302
303
310
313
314
315
316
317
322
323
324
325
332
/* ... */
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "jim.h"
#include "jimautoconf.h"
#include "jim-subcmd.h"
8 includes
/* ... */
/* ... */
Jim_Obj *JimCanonicalNamespace(Jim_Interp *interp, Jim_Obj *nsObj, Jim_Obj *nameObj)
{
Jim_Obj *objPtr;
const char *name = Jim_String(nameObj);
assert(nameObj->refCount != 0);
assert(nsObj->refCount != 0);
if (name[0] == ':' && name[1] == ':') {
while (*++name == ':') {
}while (*++name == ':') { ... }
return Jim_NewStringObj(interp, name, -1);
}if (name[0] == ':' && name[1] == ':') { ... }
if (Jim_Length(nsObj) == 0) {
return nameObj;
}if (Jim_Length(nsObj) == 0) { ... }
objPtr = Jim_DuplicateObj(interp, nsObj);
Jim_AppendString(interp, objPtr, "::", 2);
Jim_AppendObj(interp, objPtr, nameObj);
return objPtr;
}{ ... }
int Jim_CreateNamespaceVariable(Jim_Interp *interp, Jim_Obj *varNameObj, Jim_Obj *targetNameObj)
{
int rc;
Jim_IncrRefCount(varNameObj);
Jim_IncrRefCount(targetNameObj);
rc = Jim_SetVariableLink(interp, varNameObj, targetNameObj, interp->topFramePtr);
if (rc == JIM_ERR) {
Jim_SetResultFormatted(interp, "can't define \"%#s\": name refers to an element in an array", varNameObj);
}if (rc == JIM_ERR) { ... }
Jim_DecrRefCount(interp, varNameObj);
Jim_DecrRefCount(interp, targetNameObj);
return rc;
}{ ... }
/* ... */
Jim_Obj *Jim_NamespaceQualifiers(Jim_Interp *interp, Jim_Obj *ns)
{
const char *name = Jim_String(ns);
const char *pt = strrchr(name, ':');
if (pt && pt != name && pt[-1] == ':') {
return Jim_NewStringObj(interp, name, pt - name - 1);
}if (pt && pt != name && pt[-1] == ':') { ... }
else {
return interp->emptyObj;
}else { ... }
}{ ... }
Jim_Obj *Jim_NamespaceTail(Jim_Interp *interp, Jim_Obj *ns)
{
const char *name = Jim_String(ns);
const char *pt = strrchr(name, ':');
if (pt && pt != name && pt[-1] == ':') {
return Jim_NewStringObj(interp, pt + 1, -1);
}if (pt && pt != name && pt[-1] == ':') { ... }
else {
return ns;
}else { ... }
}{ ... }
static Jim_Obj *JimNamespaceCurrent(Jim_Interp *interp)
{
Jim_Obj *objPtr = Jim_NewStringObj(interp, "::", 2);
Jim_AppendObj(interp, objPtr, interp->framePtr->nsObj);
return objPtr;
}{ ... }
static int JimVariableCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int retcode = JIM_OK;
if (argc > 3) {
Jim_WrongNumArgs(interp, 1, argv, "name ?value?");
return JIM_ERR;
}if (argc > 3) { ... }
if (argc > 1) {
Jim_Obj *targetNameObj;
Jim_Obj *localNameObj;
targetNameObj = JimCanonicalNamespace(interp, interp->framePtr->nsObj, argv[1]);
localNameObj = Jim_NamespaceTail(interp, argv[1]);
Jim_IncrRefCount(localNameObj);
if (interp->framePtr->level != 0 || Jim_Length(interp->framePtr->nsObj) != 0) {
retcode = Jim_CreateNamespaceVariable(interp, localNameObj, targetNameObj);
}if (interp->framePtr->level != 0 || Jim_Length(interp->framePtr->nsObj) != 0) { ... }
if (retcode == JIM_OK && argc > 2) {
retcode = Jim_SetVariable(interp, localNameObj, argv[2]);
}if (retcode == JIM_OK && argc > 2) { ... }
Jim_DecrRefCount(interp, localNameObj);
}if (argc > 1) { ... }
return retcode;
}{ ... }
/* ... */
static int Jim_EvalEnsemble(Jim_Interp *interp, const char *basecmd, const char *subcmd, int argc, Jim_Obj *const *argv)
{
Jim_Obj *prefixObj = Jim_NewStringObj(interp, basecmd, -1);
Jim_AppendString(interp, prefixObj, " ", 1);
Jim_AppendString(interp, prefixObj, subcmd, -1);
return Jim_EvalObjPrefix(interp, prefixObj, argc, argv);
}{ ... }
static int JimNamespaceCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *nsObj;
Jim_Obj *objPtr;
int option;
static const char * const options[] = {
"eval", "current", "canonical", "qualifiers", "parent", "tail", "delete",
"origin", "code", "inscope", "import", "export",
"which", "upvar", NULL
...};
enum
{
OPT_EVAL, OPT_CURRENT, OPT_CANONICAL, OPT_QUALIFIERS, OPT_PARENT, OPT_TAIL, OPT_DELETE,
OPT_ORIGIN, OPT_CODE, OPT_INSCOPE, OPT_IMPORT, OPT_EXPORT,
OPT_WHICH, OPT_UPVAR,
...};
if (argc < 2) {
Jim_WrongNumArgs(interp, 1, argv, "subcommand ?arg ...?");
return JIM_ERR;
}if (argc < 2) { ... }
if (Jim_GetEnum(interp, argv[1], options, &option, "subcommand", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) {
return Jim_CheckShowCommands(interp, argv[1], options);
}if (Jim_GetEnum(interp, argv[1], options, &option, "subcommand", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { ... }
switch (option) {
case OPT_EVAL:
if (argc < 4) {
Jim_WrongNumArgs(interp, 2, argv, "name arg ?arg...?");
return JIM_ERR;
}if (argc < 4) { ... }
if (argc == 4) {
objPtr = argv[3];
}if (argc == 4) { ... }
else {
objPtr = Jim_ConcatObj(interp, argc - 3, argv + 3);
}else { ... }
nsObj = JimCanonicalNamespace(interp, interp->framePtr->nsObj, argv[2]);
return Jim_EvalNamespace(interp, objPtr, nsObj);
case OPT_EVAL:
case OPT_CURRENT:
if (argc != 2) {
Jim_WrongNumArgs(interp, 2, argv, "");
return JIM_ERR;
}if (argc != 2) { ... }
Jim_SetResult(interp, JimNamespaceCurrent(interp));
return JIM_OK;
case OPT_CURRENT:
case OPT_CANONICAL:
if (argc > 4) {
Jim_WrongNumArgs(interp, 2, argv, "?current? ?name?");
return JIM_ERR;
}if (argc > 4) { ... }
if (argc == 2) {
Jim_SetResult(interp, interp->framePtr->nsObj);
}if (argc == 2) { ... }
else if (argc == 3) {
Jim_SetResult(interp, JimCanonicalNamespace(interp, interp->framePtr->nsObj, argv[2]));
}else if (argc == 3) { ... }
else {
Jim_SetResult(interp, JimCanonicalNamespace(interp, argv[2], argv[3]));
}else { ... }
return JIM_OK;
case OPT_CANONICAL:
case OPT_QUALIFIERS:
if (argc != 3) {
Jim_WrongNumArgs(interp, 2, argv, "string");
return JIM_ERR;
}if (argc != 3) { ... }
Jim_SetResult(interp, Jim_NamespaceQualifiers(interp, argv[2]));
return JIM_OK;
case OPT_QUALIFIERS:
case OPT_EXPORT:
return JIM_OK;
case OPT_EXPORT:
case OPT_TAIL:
if (argc != 3) {
Jim_WrongNumArgs(interp, 2, argv, "string");
return JIM_ERR;
}if (argc != 3) { ... }
Jim_SetResult(interp, Jim_NamespaceTail(interp, argv[2]));
return JIM_OK;
case OPT_TAIL:
case OPT_PARENT:
if (argc != 2 && argc != 3) {
Jim_WrongNumArgs(interp, 2, argv, "?name?");
return JIM_ERR;
}if (argc != 2 && argc != 3) { ... }
else {
const char *name;
if (argc == 3) {
objPtr = argv[2];
}if (argc == 3) { ... }
else {
objPtr = interp->framePtr->nsObj;
}else { ... }
if (Jim_Length(objPtr) == 0 || Jim_CompareStringImmediate(interp, objPtr, "::")) {
return JIM_OK;
}if (Jim_Length(objPtr) == 0 || Jim_CompareStringImmediate(interp, objPtr, "::")) { ... }
objPtr = Jim_NamespaceQualifiers(interp, objPtr);
name = Jim_String(objPtr);
if (name[0] != ':' || name[1] != ':') {
Jim_SetResultString(interp, "::", 2);
Jim_AppendObj(interp, Jim_GetResult(interp), objPtr);
Jim_IncrRefCount(objPtr);
Jim_DecrRefCount(interp, objPtr);
}if (name[0] != ':' || name[1] != ':') { ... }
else {
Jim_SetResult(interp, objPtr);
}else { ... }
}else { ... }
return JIM_OK;case OPT_PARENT:
}switch (option) { ... }
/* ... */
return Jim_EvalEnsemble(interp, "namespace", options[option], argc - 2, argv + 2);
}{ ... }
int Jim_namespaceInit(Jim_Interp *interp)
{
Jim_PackageProvideCheck(interp, "namespace");
Jim_CreateCommand(interp, "namespace", JimNamespaceCmd, NULL, NULL);
Jim_CreateCommand(interp, "variable", JimVariableCmd, NULL, NULL);
return JIM_OK;
}{ ... }