From bd25a29a3766bcf9327c8c8d032d0bb773ca4e6a Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Sun, 28 Feb 2016 12:06:47 +1000 Subject: jim: Jim_ScriptIsComplete() now takes an object Rather than taking a string and a length, it is simpler and more efficient to take a Jim_Obj Signed-off-by: Steve Bennett --- jim-interactive.c | 7 ++----- jim.c | 59 +++++++++++++++++++++++++------------------------------ jim.h | 5 +++-- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/jim-interactive.c b/jim-interactive.c index a3cf6a0..c80e451 100644 --- a/jim-interactive.c +++ b/jim-interactive.c @@ -96,7 +96,6 @@ int Jim_InteractivePrompt(Jim_Interp *interp) const char *result; int reslen; char prompt[20]; - const char *str; if (retcode != JIM_OK) { const char *retcodestr = Jim_ReturnCode(retcode); @@ -116,7 +115,6 @@ int Jim_InteractivePrompt(Jim_Interp *interp) Jim_IncrRefCount(scriptObjPtr); while (1) { char state; - int len; char *line; line = Jim_HistoryGetline(prompt); @@ -134,14 +132,13 @@ int Jim_InteractivePrompt(Jim_Interp *interp) } Jim_AppendString(interp, scriptObjPtr, line, -1); free(line); - str = Jim_GetString(scriptObjPtr, &len); - if (Jim_ScriptIsComplete(str, len, &state)) + if (Jim_ScriptIsComplete(interp, scriptObjPtr, &state)) break; snprintf(prompt, sizeof(prompt), "%c> ", state); } #ifdef USE_LINENOISE - if (strcmp(str, "h") == 0) { + if (strcmp(Jim_String(scriptObjPtr), "h") == 0) { /* built-in history command */ Jim_HistoryShow(); Jim_DecrRefCount(interp, scriptObjPtr); diff --git a/jim.c b/jim.c index b1181a3..b6b6610 100644 --- a/jim.c +++ b/jim.c @@ -1993,32 +1993,6 @@ static Jim_Obj *JimParserGetTokenObj(Jim_Interp *interp, struct JimParserCtx *pc return Jim_NewStringObjNoAlloc(interp, token, len); } -/* Parses the given string to determine if it represents a complete script. - * - * This is useful for interactive shells implementation, for [info complete]. - * - * If 'stateCharPtr' != NULL, the function stores ' ' on complete script, - * '{' on scripts incomplete missing one or more '}' to be balanced. - * '[' on scripts incomplete missing one or more ']' to be balanced. - * '"' on scripts incomplete missing a '"' char. - * '\\' on scripts with a trailing backslash. - * - * If the script is complete, 1 is returned, otherwise 0. - */ -int Jim_ScriptIsComplete(const char *s, int len, char *stateCharPtr) -{ - struct JimParserCtx parser; - - JimParserInit(&parser, s, len, 1); - while (!parser.eof) { - JimParseScript(&parser); - } - if (stateCharPtr) { - *stateCharPtr = parser.missing.ch; - } - return parser.missing.ch == ' '; -} - /* ----------------------------------------------------------------------------- * Tcl Lists parsing * ---------------------------------------------------------------------------*/ @@ -3180,8 +3154,6 @@ static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line) */ static void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr); static void DupScriptInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr); -static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); -static int JimParseCheckMissing(Jim_Interp *interp, int ch); static const Jim_ObjType scriptObjType = { "script", @@ -3284,6 +3256,10 @@ typedef struct ScriptObj int missing; /* Missing char if script failed to parse, (or space or backslash if OK) */ } ScriptObj; +static void JimSetScriptFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); +static int JimParseCheckMissing(Jim_Interp *interp, int ch); +static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr); + void FreeScriptInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) { int i; @@ -3553,6 +3529,27 @@ static void ScriptObjAddTokens(Jim_Interp *interp, struct ScriptObj *script, } +/* Parses the given string object to determine if it represents a complete script. + * + * This is useful for interactive shells implementation, for [info complete]. + * + * If 'stateCharPtr' != NULL, the function stores ' ' on complete script, + * '{' on scripts incomplete missing one or more '}' to be balanced. + * '[' on scripts incomplete missing one or more ']' to be balanced. + * '"' on scripts incomplete missing a '"' char. + * '\\' on scripts with a trailing backslash. + * + * If the script is complete, 1 is returned, otherwise 0. + */ +int Jim_ScriptIsComplete(Jim_Interp *interp, Jim_Obj *scriptObj, char *stateCharPtr) +{ + ScriptObj *script = JimGetScript(interp, scriptObj); + if (stateCharPtr) { + *stateCharPtr = script->missing; + } + return (script->missing == ' '); +} + /** * Sets an appropriate error message for a missing script/expression terminator. * @@ -3675,7 +3672,7 @@ static void JimAddErrorToStack(Jim_Interp *interp, ScriptObj *script); * Note that if there is any possibility that the script is not valid, * call JimScriptValid() to check */ -ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) +static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) { if (objPtr == interp->emptyObj) { /* Avoid converting emptyObj to a script. use nullScriptObj instead. */ @@ -14594,11 +14591,9 @@ static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *arg return JIM_ERR; } else { - int len; - const char *s = Jim_GetString(argv[2], &len); char missing; - Jim_SetResultBool(interp, Jim_ScriptIsComplete(s, len, &missing)); + Jim_SetResultBool(interp, Jim_ScriptIsComplete(interp, argv[2], &missing)); if (missing != ' ' && argc == 4) { Jim_SetVariable(interp, argv[3], Jim_NewStringObj(interp, &missing, 1)); } diff --git a/jim.h b/jim.h index f7f43bc..f41b113 100644 --- a/jim.h +++ b/jim.h @@ -837,8 +837,9 @@ JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg); JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr, const char * const *tablePtr, int *indexPtr, const char *name, int flags); -JIM_EXPORT int Jim_ScriptIsComplete (const char *s, int len, - char *stateCharPtr); +JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp, + Jim_Obj *scriptObj, char *stateCharPtr); + /** * Find a matching name in the array of the given length. * -- cgit v1.1