1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
18
19
20
21
22
23
24
25
31
32
33
34
35
41
42
43
44
45
46
47
48
49
50
51
52
53
58
61
64
65
68
69
70
71
72
73
76
77
78
79
80
81
82
83
84
85
86
87
88
92
93
94
95
96
97
102
103
107
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
256
257
258
#include <string.h>
#include "jimautoconf.h"
#include <jim-subcmd.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#define R_OK 4
#endif
static const char *package_version_1 = "1.0";
/* ... */
int Jim_PackageProvide(Jim_Interp *interp, const char *name, const char *ver, int flags)
{
Jim_HashEntry *he = Jim_FindHashEntry(&interp->packages, name);
if (he && *(const char *)he->u.val) {
if (flags & JIM_ERRMSG) {
Jim_SetResultFormatted(interp, "package \"%s\" was already provided", name);
}if (flags & JIM_ERRMSG) { ... }
return JIM_ERR;
}if (he && *(const char *)he->u.val) { ... }
Jim_ReplaceHashEntry(&interp->packages, name, (char *)ver);
return JIM_OK;
}{ ... }
/* ... */
static char *JimFindPackage(Jim_Interp *interp, Jim_Obj *prefixListObj, const char *pkgName)
{
int i;
char *buf = Jim_Alloc(JIM_PATH_LEN);
int prefixc = Jim_ListLength(interp, prefixListObj);
for (i = 0; i < prefixc; i++) {
Jim_Obj *prefixObjPtr = Jim_ListGetIndex(interp, prefixListObj, i);
const char *prefix = Jim_String(prefixObjPtr);
#ifdef jim_ext_load
snprintf(buf, JIM_PATH_LEN, "%s/%s.so", prefix, pkgName);
if (access(buf, R_OK) == 0) {
return buf;
}if (access(buf, R_OK) == 0) { ... }
/* ... */#endif
if (strcmp(prefix, ".") == 0) {
snprintf(buf, JIM_PATH_LEN, "%s.tcl", pkgName);
}if (strcmp(prefix, ".") == 0) { ... }
else {
snprintf(buf, JIM_PATH_LEN, "%s/%s.tcl", prefix, pkgName);
}else { ... }
if (access(buf, R_OK) == 0) {
return buf;
}if (access(buf, R_OK) == 0) { ... }
}for (i = 0; i < prefixc; i++) { ... }
Jim_Free(buf);
return NULL;
}{ ... }
/* ... */
static int JimLoadPackage(Jim_Interp *interp, const char *name, int flags)
{
int retCode = JIM_ERR;
Jim_Obj *libPathObjPtr = Jim_GetGlobalVariableStr(interp, JIM_LIBPATH, JIM_NONE);
if (libPathObjPtr) {
char *path;
path = JimFindPackage(interp, libPathObjPtr, name);
if (path) {
const char *p;
/* ... */
Jim_PackageProvide(interp, name, "", 0);
p = strrchr(path, '.');
if (p && strcmp(p, ".tcl") == 0) {
Jim_IncrRefCount(libPathObjPtr);
retCode = Jim_EvalFileGlobal(interp, path);
Jim_DecrRefCount(interp, libPathObjPtr);
}if (p && strcmp(p, ".tcl") == 0) { ... }
#ifdef jim_ext_load
else {
retCode = Jim_LoadLibrary(interp, path);
}else { ... }
/* ... */#endif
if (retCode != JIM_OK) {
Jim_DeleteHashEntry(&interp->packages, name);
}if (retCode != JIM_OK) { ... }
Jim_Free(path);
}if (path) { ... }
return retCode;
}if (libPathObjPtr) { ... }
return JIM_ERR;
}{ ... }
int Jim_PackageRequire(Jim_Interp *interp, const char *name, int flags)
{
Jim_HashEntry *he;
Jim_SetEmptyResult(interp);
he = Jim_FindHashEntry(&interp->packages, name);
if (he == NULL) {
int retcode = JimLoadPackage(interp, name, flags);
if (retcode != JIM_OK) {
if (flags & JIM_ERRMSG) {
int len = Jim_Length(Jim_GetResult(interp));
Jim_SetResultFormatted(interp, "%#s%sCan't load package %s",
Jim_GetResult(interp), len ? "\n" : "", name);
}if (flags & JIM_ERRMSG) { ... }
return retcode;
}if (retcode != JIM_OK) { ... }
Jim_PackageProvide(interp, name, package_version_1, 0);
he = Jim_FindHashEntry(&interp->packages, name);
}if (he == NULL) { ... }
Jim_SetResultString(interp, he->u.val, -1);
return JIM_OK;
}{ ... }
/* ... */
static int package_cmd_provide(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
return Jim_PackageProvide(interp, Jim_String(argv[0]), package_version_1, JIM_ERRMSG);
}{ ... }
/* ... */
static int package_cmd_require(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
interp->addStackTrace++;
return Jim_PackageRequire(interp, Jim_String(argv[0]), JIM_ERRMSG);
}{ ... }
/* ... */
static int package_cmd_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_HashTableIterator *htiter;
Jim_HashEntry *he;
Jim_Obj *listObjPtr = Jim_NewListObj(interp, NULL, 0);
htiter = Jim_GetHashTableIterator(&interp->packages);
while ((he = Jim_NextHashEntry(htiter)) != NULL) {
Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, he->key, -1));
}while ((he = Jim_NextHashEntry(htiter)) != NULL) { ... }
Jim_FreeHashTableIterator(htiter);
Jim_SetResult(interp, listObjPtr);
return JIM_OK;
}{ ... }
static const jim_subcmd_type package_command_table[] = {
{
"provide",
"name ?version?",
package_cmd_provide,
1,
2,
...},
{
"require",
"name ?version?",
package_cmd_require,
1,
2,
...},
{
"list",
NULL,
package_cmd_names,
0,
0,
JIM_MODFLAG_HIDDEN
...},
{
"names",
NULL,
package_cmd_names,
0,
0,
...},
{
NULL
...}
...};
int Jim_packageInit(Jim_Interp *interp)
{
Jim_CreateCommand(interp, "package", Jim_SubCmdProc, (void *)package_command_table, NULL);
return JIM_OK;
}{ ... }