4 #include "jimautoconf.h"
5 #include "jim-subcmd.h"
7 static void JimInterpDelProc(Jim_Interp
*interp
, void *privData
)
9 Jim_FreeInterp((Jim_Interp
*)privData
);
12 /* Everything passing between interpreters must be converted to a string */
13 static Jim_Obj
*JimInterpCopyObj(Jim_Interp
*target
, Jim_Obj
*obj
)
18 rep
= Jim_GetString(obj
, &len
);
19 return Jim_NewStringObj(target
, rep
, len
);
22 #define JimInterpCopyResult(to, from) Jim_SetResult((to), JimInterpCopyObj((to), Jim_GetResult((from))))
24 static int interp_cmd_eval(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
27 Jim_Interp
*child
= Jim_CmdPrivData(interp
);
29 Jim_Obj
*targetScriptObj
;
31 scriptObj
= Jim_ConcatObj(interp
, argc
, argv
);
32 targetScriptObj
= JimInterpCopyObj(child
, scriptObj
);
33 Jim_FreeNewObj(interp
, scriptObj
);
35 Jim_IncrRefCount(targetScriptObj
);
36 ret
= Jim_EvalObj(child
, targetScriptObj
);
37 Jim_DecrRefCount(child
, targetScriptObj
);
39 JimInterpCopyResult(interp
, child
);
43 static int interp_cmd_delete(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
45 return Jim_DeleteCommand(interp
, Jim_String(argv
[0]));
48 static void JimInterpDelAlias(Jim_Interp
*interp
, void *privData
)
50 Jim_Interp
*parent
= Jim_GetAssocData(interp
, "interp.parent");
51 Jim_DecrRefCount(parent
, (Jim_Obj
*)privData
);
54 static int JimInterpAliasProc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
57 Jim_Interp
*parent
= Jim_GetAssocData(interp
, "interp.parent");
58 Jim_Obj
*targetPrefixObj
= Jim_CmdPrivData(interp
);
59 Jim_Obj
*targetScriptObj
;
63 /* Build the complete command */
64 targetScriptObj
= Jim_DuplicateObj(parent
, targetPrefixObj
);
65 for (i
= 1; i
< argc
; i
++) {
66 Jim_ListAppendElement(parent
, targetScriptObj
,
67 JimInterpCopyObj(parent
, argv
[i
]));
70 Jim_IncrRefCount(targetScriptObj
);
71 ret
= Jim_EvalObj(parent
, targetScriptObj
);
72 Jim_DecrRefCount(parent
, targetScriptObj
);
74 JimInterpCopyResult(interp
, parent
);
78 static int interp_cmd_alias(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
80 Jim_Interp
*child
= Jim_CmdPrivData(interp
);
81 Jim_Obj
*aliasPrefixList
;
83 /* The prefix list will be held inside the child, but it still belongs
87 aliasPrefixList
= Jim_NewListObj(interp
, argv
+ 1, argc
- 1);
88 Jim_IncrRefCount(aliasPrefixList
);
90 Jim_CreateCommand(child
, Jim_String(argv
[0]), JimInterpAliasProc
, aliasPrefixList
, JimInterpDelAlias
);
94 static const jim_subcmd_type interp_command_table
[] = {
100 /* Description: Concat the args and evaluate the script in the interpreter */
107 JIM_MODFLAG_FULLARGV
,
108 /* Description: Delete this interpreter */
111 "childcmd parentcmd ?arg ...?",
115 /* Description: Create an alias which refers to a script in the parent interpreter */
120 static int JimInterpSubCmdProc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
122 return Jim_CallSubCmd(interp
, Jim_ParseSubCmd(interp
, interp_command_table
, argc
, argv
), argc
, argv
);
125 static void JimInterpCopyVariable(Jim_Interp
*target
, Jim_Interp
*source
, const char *var
, const char *default_value
)
127 Jim_Obj
*value
= Jim_GetGlobalVariableStr(source
, var
, JIM_NONE
);
130 str
= value
? Jim_String(value
) : default_value
;
132 Jim_SetGlobalVariableStr(target
, var
, Jim_NewStringObj(target
, str
, -1));
137 * [interp] creates a new interpreter.
139 static int JimInterpCommand(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
145 Jim_WrongNumArgs(interp
, 1, argv
, "");
149 /* Create the interpreter command */
150 child
= Jim_CreateInterp();
151 Jim_RegisterCoreCommands(child
);
152 Jim_InitStaticExtensions(child
);
154 /* Copy some core variables to the new interpreter */
155 JimInterpCopyVariable(child
, interp
, "argv", NULL
);
156 JimInterpCopyVariable(child
, interp
, "argc", NULL
);
157 JimInterpCopyVariable(child
, interp
, "argv0", NULL
);
158 JimInterpCopyVariable(child
, interp
, "jim::argv0", NULL
);
159 JimInterpCopyVariable(child
, interp
, "jim::exe", NULL
);
161 /* Allow the child interpreter to find the parent */
162 Jim_SetAssocData(child
, "interp.parent", NULL
, interp
);
164 snprintf(buf
, sizeof(buf
), "interp.handle%ld", Jim_GetId(interp
));
165 Jim_CreateCommand(interp
, buf
, JimInterpSubCmdProc
, child
, JimInterpDelProc
);
166 Jim_SetResult(interp
, Jim_MakeGlobalNamespaceName(interp
, Jim_NewStringObj(interp
, buf
, -1)));
170 int Jim_interpInit(Jim_Interp
*interp
)
172 if (Jim_PackageProvide(interp
, "interp", "1.0", JIM_ERRMSG
))
175 Jim_CreateCommand(interp
, "interp", JimInterpCommand
, NULL
, NULL
);