static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { AioFile *af = Jim_CmdPrivData(interp); int wlen; const char *wdata; Jim_Obj *strObj; if (argc == 2) { if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { return -1; } strObj = argv[1]; } else { strObj = argv[0]; } wdata = Jim_GetString(strObj, &wlen); if (fwrite(wdata, 1, wlen, af->fp) == (unsigned)wlen) { if (argc == 2 || putc('\n', af->fp) != EOF) { return JIM_OK; } } JimAioSetError(interp, af->filename); return JIM_ERR; }
static int ioutil_Jim_Command_ls(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "ls ?dir?"); return JIM_ERR; } const char *name = Jim_GetString(argv[1], NULL); DIR *dirp = NULL; dirp = opendir(name); if (dirp == NULL) return JIM_ERR; Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0); for (;; ) { struct dirent *entry = NULL; entry = readdir(dirp); if (entry == NULL) break; if ((strcmp(".", entry->d_name) == 0) || (strcmp("..", entry->d_name) == 0)) continue; Jim_ListAppendElement(interp, objPtr, Jim_NewStringObj(interp, entry->d_name, strlen(entry->d_name))); } closedir(dirp); Jim_SetResult(interp, objPtr); return JIM_OK; }
static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, Jim_Obj **scriptHandlerObj, int argc, Jim_Obj * const *argv) { int scriptlen = 0; if (argc == 0) { /* Return current script */ if (*scriptHandlerObj) { Jim_SetResult(interp, *scriptHandlerObj); } return JIM_OK; } if (*scriptHandlerObj) { /* Delete old handler */ Jim_DeleteFileHandler(interp, af->fp); *scriptHandlerObj = NULL; } /* Now possibly add the new script(s) */ Jim_GetString(argv[0], &scriptlen); if (scriptlen == 0) { /* Empty script, so done */ return JIM_OK; } /* A new script to add */ Jim_IncrRefCount(argv[0]); *scriptHandlerObj = argv[0]; Jim_CreateFileHandler(interp, af->fp, mask, JimAioFileEventHandler, *scriptHandlerObj, JimAioFileEventFinalizer); return JIM_OK; }
static int GetAnimateWindowFlagsFromObj(Jim_Interp *interp, Jim_Obj *listObj, DWORD *flags) { int r = JIM_OK, n, nLength; *flags = 0; Jim_ListLength(interp, listObj, &nLength); if (r == JIM_OK) { for (n = 0; n < nLength; n++) { ANIMATEWINDOWFLAGSMAP *p; Jim_Obj *obj; r = Jim_ListIndex(interp, listObj, n, &obj, 1); for (p = AnimateWindowFlagsMap; r == JIM_OK && p->s != NULL; p++) { size_t len; const char *name = Jim_GetString(obj, &len); if (strncmp(p->s, name, len) == 0) { *flags |= p->f; break; } } if (p->s == NULL) { Jim_SetResultString(interp, "invalid option", -1); return JIM_ERR; } } } return r; }
/* win32.FindWindow title ?class? */ static int Win32_FindWindow(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { const char *title = NULL, *class = NULL; HWND hwnd = NULL; int r = JIM_OK; if (objc < 2 || objc > 3) { Jim_WrongNumArgs(interp, 1, objv, "title ?class?"); return JIM_ERR; } title = Jim_GetString(objv[1], NULL); if (objc == 3) class = Jim_GetString(objv[2], NULL); hwnd = FindWindowA(class, title); if (hwnd == NULL) { Jim_SetResult(interp, Win32ErrorObj(interp, "FindWindow", GetLastError())); r = JIM_ERR; } else { Jim_SetResult(interp, Jim_NewIntObj(interp, (long)hwnd)); } return r; }
/* * Now we try to write big enough code to duplication our array in Jim's * list implementation. Later, we try to load a sample script in Tcl that * could print our list. */ int main(int argc, char **argv) { Jim_Interp *interp; int error; /* Create an interpreter. */ interp = Jim_CreateInterp(); assert(interp != NULL && "couldn't create interpreter"); /* We register base commands, so that we actually implement Tcl. */ Jim_RegisterCoreCommands(interp); /* And initialise any static extensions */ Jim_InitStaticExtensions(interp); /* Register our Jim commands. */ Jim_CreateCommand(interp, "MySampleCommand", MySampleCommandFunc, NULL, NULL); /* Run a script. */ error = Jim_Eval(interp, JIM_PROGRAM); if (error == JIM_ERR) { Jim_MakeErrorMessage(interp); fprintf(stderr, "%s\n", Jim_GetString(Jim_GetResult(interp), NULL)); Jim_FreeInterp(interp); exit(EXIT_FAILURE); } Jim_FreeInterp(interp); return (EXIT_SUCCESS); }
static int aio_cmd_sendto(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { AioFile *af = Jim_CmdPrivData(interp); int wlen; int len; const char *wdata; union sockaddr_any sa; const char *addr = Jim_String(argv[1]); int salen; if (IPV6 && af->addr_family == PF_INET6) { if (JimParseIPv6Address(interp, addr, &sa, &salen) != JIM_OK) { return JIM_ERR; } } else if (JimParseIpAddress(interp, addr, &sa, &salen) != JIM_OK) { return JIM_ERR; } wdata = Jim_GetString(argv[0], &wlen); /* Note that we don't validate the socket type. Rely on sendto() failing if appropriate */ len = sendto(fileno(af->fp), wdata, wlen, 0, &sa.sa, salen); if (len < 0) { JimAioSetError(interp, NULL); return JIM_ERR; } Jim_SetResultInt(interp, len); return JIM_OK; }
/* Capture progress output and return as tcl return value. If the * progress output was empty, return tcl return value. */ static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if (argc != 2) return JIM_ERR; struct log_capture_state *state = command_log_capture_start(interp); /* disable polling during capture. This avoids capturing output * from polling. * * This is necessary in order to avoid accidentally getting a non-empty * string for tcl fn's. */ bool save_poll = jtag_poll_get_enabled(); jtag_poll_set_enabled(false); const char *str = Jim_GetString(argv[1], NULL); int retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__); jtag_poll_set_enabled(save_poll); command_log_capture_finish(state); return retcode; }
/* Everything passing between interpreters must be converted to a string */ static Jim_Obj *JimInterpCopyObj(Jim_Interp *target, Jim_Obj *obj) { const char *rep; int len; rep = Jim_GetString(obj, &len); return Jim_NewStringObj(target, rep, len); }
static int jim_echo(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if (argc != 2) return JIM_ERR; const char *str = Jim_GetString(argv[1], NULL); LOG_USER("%s", str); return JIM_OK; }
es_Status es_eval_tcl(es_Interp *interp, const char *script) { int error = Jim_Eval(interp, script); if (error == JIM_ERR) { Jim_MakeErrorMessage(interp); LOG_ERROR(Tcl,"%s\n", Jim_GetString(Jim_GetResult(interp), NULL)); } return error; }
/* * Parse "interface" keyword. */ static int InterfaceFunc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { struct mystate *softc; struct iface *iface; Jim_Obj *ipobj; const char *ip; const char *name; int error; iface = NULL; ipobj = NULL; ip = name = NULL; error = 0; if (argc != 3) return (JIM_ERR); softc = Jim_CmdPrivData(interp); /* Name of the interface. */ name = Jim_GetString(argv[1], NULL); softc->block_parsing = 1; /* Body of the block */ error = Jim_EvalObj(interp, argv[2]); if (error) { printf("couldn't evaluate\n"); return (JIM_ERR); } softc->block_parsing = 0; /* Take our hidden variable */ ipobj = Jim_GetVariableStr(interp, JCONF_VAR_IP, JIM_NONE); assert(ipobj != NULL); ip = Jim_GetString(ipobj, NULL); if (ip == NULL) { Jim_fprintf(interp, interp->cookie_stdout, "NULL!\n"); return (JIM_ERR); } iface = iface_alloc(ip, name); assert(iface != NULL); INSERT(softc->head, iface); return (JIM_OK); }
/* * If the last character of the result is a newline, then remove * the newline character (the newline would just confuse things). * * Note: Ideally we could do this by just reducing the length of stringrep * by 1, but there is no API for this :-( */ static void JimTrimTrailingNewline(Jim_Interp *interp) { int len; const char *p = Jim_GetString(Jim_GetResult(interp), &len); if (len > 0 && p[len - 1] == '\n') { Jim_SetResultString(interp, p, len - 1); } }
static int Jim_PosixSignalCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int sig; int signum; sighandler_t lastaction; sighandler_t nextaction = SIG_DFL; const char *op; int strlen = 0; if (argc < 2) { Jim_WrongNumArgs(interp, 1, argv, "signame ?action ...?"); return JIM_ERR; } if (Jim_GetEnum(interp, argv[1], signames, &sig, "Signal Names", JIM_ERRMSG) != JIM_OK) return JIM_ERR; signum = signums[sig]; switch (argc) { case 3: if (op = Jim_GetString(argv[2], &strlen),strlen == 0) { return JIM_ERR; } if (strcmp("default",op) == 0) { nextaction = SIG_DFL; } else if (strcmp("ignore",op) == 0) { nextaction = SIG_IGN; } else if (strcmp("debug",op) == 0) { nextaction = Jim_Posix_SigHandler; } else { // this is the place to insert a script! UK } // fall through to query case: case 2: lastaction = signal(signum, nextaction); if (argc==2) signal(signum, lastaction); if (lastaction == SIG_ERR) { return JIM_ERR; } if (lastaction == SIG_DFL) { Jim_SetResultString(interp, "default", -1); return JIM_OK; } if (lastaction == SIG_IGN) { Jim_SetResultString(interp, "ignore", -1); return JIM_OK; } Jim_SetResultString(interp, "function", -1); return JIM_OK; } return JIM_OK; }
static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr) { int len; const char *s = Jim_GetString(objPtr, &len); if (len > 0 && s[len - 1] == '\n') { objPtr->length--; objPtr->bytes[objPtr->length] = '\0'; } }
struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o) { const char *cp = Jim_GetString(o, NULL); struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL; if (NULL == cp) cp = "(unknown)"; if (NULL == t) Jim_SetResultFormatted(interp, "Tap '%s' could not be found", cp); return t; }
static int zylinjtag_Jim_Command_rm(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { int del; if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "rm ?dirorfile?"); return JIM_ERR; } del = 0; if (unlink(Jim_GetString(argv[1], NULL)) == 0) del = 1; if (rmdir(Jim_GetString(argv[1], NULL)) == 0) del = 1; return del ? JIM_OK : JIM_ERR; }
int es_jim_log_cmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if(argc!=4) { WRONG_ARGS(interp,1,argv,"info|warn|error tag message"); } char *lvl = (char*)Jim_GetString(argv[1],NULL); char *src = (char*)Jim_GetString(argv[2],NULL); char *msg = (char*)Jim_GetString(argv[3],NULL); if(!strcmp("info",lvl)) { LOG_INFO(src,msg); return JIM_OK; } if(!strcmp("warn",lvl)) { LOG_WARN(src,msg); return JIM_OK; } if(!strcmp("error",lvl)) { LOG_ERROR(src,msg); return JIM_OK; } RCERROR(interp,"Invalid log level: %s",lvl); }
static jim_wide JimParseAfterId(Jim_Obj *idObj) { int len; const char *tok = Jim_GetString(idObj, &len); jim_wide id; if (strncmp(tok, "after#", 6) == 0 && Jim_StringToWide(tok + 6, &id, 10) == JIM_OK) { /* Got an event by id */ return id; } return -1; }
static int JimELAfterCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { jim_wide ms, id; Jim_Obj *objPtr, *idObjPtr; const char *options[] = { "info", "cancel", "restart", "expire", NULL }; enum {INFO, CANCEL, RESTART, EXPIRE, CREATE }; int option = CREATE ; if (argc < 3) { Jim_WrongNumArgs(interp, 1, argv, "<after milliseconds> script"); return JIM_ERR; } if (Jim_GetWide(interp, argv[1], &ms) != JIM_OK) if (Jim_GetEnum(interp, argv[1], options, &option, "after options", JIM_ERRMSG) != JIM_OK) return JIM_ERR; switch (option) { case CREATE: Jim_IncrRefCount(argv[2]); id = Jim_CreateTimeHandler(interp, ms, JimAfterTimeHandler, argv[2], JimAfterTimeEventFinalizer); objPtr = Jim_NewStringObj(interp, NULL, 0); Jim_AppendString(interp, objPtr, "after#", -1); idObjPtr = Jim_NewIntObj(interp, id); Jim_IncrRefCount(idObjPtr); Jim_AppendObj(interp, objPtr, idObjPtr); Jim_DecrRefCount(interp, idObjPtr); Jim_SetResult(interp, objPtr); return JIM_OK; case CANCEL: { int tlen ; jim_wide remain = 0; const char *tok = Jim_GetString(argv[2], &tlen); if ( sscanf(tok,"after#%lld",&id) == 1) { remain = Jim_DeleteTimeHandler(interp, id); if (remain > -2) { Jim_SetResult(interp, Jim_NewIntObj(interp, remain)); return JIM_OK; } } Jim_SetResultString(interp, "invalid event" , -1); return JIM_ERR; } default: fprintf(stderr,"unserviced option to after %d\n",option); } return JIM_OK; }
/* win32.ShellExecute verb file args */ static int Win32_ShellExecute(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { int r; const char *verb, *file, *parm = NULL; char cwd[MAX_PATH + 1]; if (objc < 3 || objc > 4) { Jim_WrongNumArgs(interp, 1, objv, "verb path ?parameters?"); return JIM_ERR; } verb = Jim_GetString(objv[1], NULL); file = Jim_GetString(objv[2], NULL); GetCurrentDirectoryA(MAX_PATH + 1, cwd); if (objc == 4) parm = Jim_GetString(objv[3], NULL); r = (int)ShellExecuteA(NULL, verb, file, parm, cwd, SW_SHOWNORMAL); if (r < 33) Jim_SetResult(interp, Win32ErrorObj(interp, "ShellExecute", GetLastError())); return (r < 33) ? JIM_ERR : JIM_OK; }
/* find full path to file */ static int jim_find(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if (argc != 2) return JIM_ERR; const char *file = Jim_GetString(argv[1], NULL); char *full_path = find_file(file); if (full_path == NULL) return JIM_ERR; Jim_Obj *result = Jim_NewStringObj(interp, full_path, strlen(full_path)); free(full_path); Jim_SetResult(interp, result); return JIM_OK; }
static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { if (argc != 2) return JIM_ERR; struct log_capture_state *state = command_log_capture_start(interp); const char *str = Jim_GetString(argv[1], NULL); int retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__); command_log_capture_finish(state); return retcode; }
static int Jim_Deflate(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { long level = Z_DEFAULT_COMPRESSION; const char *in; int len; if (argc != 1) { if (Jim_GetLong(interp, argv[1], &level) != JIM_OK) { return JIM_ERR; } } in = Jim_GetString(argv[0], &len); return Jim_Compress(interp, in, len, level, -MAX_WBITS); }
int Jim_GetOpt_String(Jim_GetOptInfo *goi, char **puthere, int *len) { int r; Jim_Obj *o; const char *cp; r = Jim_GetOpt_Obj(goi, &o); if (r == JIM_OK) { cp = Jim_GetString(o, len); if (puthere) { /* remove const */ *puthere = (char *)(cp); } } return r; }
static int JimELVwaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_EventLoop *eventLoop = Jim_CmdPrivData(interp); Jim_Obj *oldValue; int rc; if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "name"); return JIM_ERR; } oldValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE); if (oldValue) { Jim_IncrRefCount(oldValue); } else { /* If a result was left, it is an error */ int len; Jim_GetString(interp->result, &len); if (len) { return JIM_ERR; } } eventLoop->suppress_bgerror = 0; while ((rc = Jim_ProcessEvents(interp, JIM_ALL_EVENTS)) >= 0) { Jim_Obj *currValue; currValue = Jim_GetGlobalVariable(interp, argv[1], JIM_NONE); /* Stop the loop if the vwait-ed variable changed value, * or if was unset and now is set (or the contrary). */ if ((oldValue && !currValue) || (!oldValue && currValue) || (oldValue && currValue && !Jim_StringEqObj(oldValue, currValue))) break; } if (oldValue) Jim_DecrRefCount(interp, oldValue); if (rc == -2) { return JIM_ERR; } Jim_SetEmptyResult(interp); return JIM_OK; }
static int Win32_LoadLibrary(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { HMODULE hLib = NULL; if (objc != 2) { Jim_WrongNumArgs(interp, 1, objv, "path"); return JIM_ERR; } hLib = LoadLibraryA(Jim_GetString(objv[1], NULL)); if (hLib == NULL) { Jim_SetResult(interp, Win32ErrorObj(interp, "LoadLibrary", GetLastError())); return JIM_ERR; } Jim_SetResult(interp, Jim_NewIntObj(interp, (unsigned long)hLib)); return JIM_OK; }
/* dump a single line to the log for the command. * Do nothing in case we are not at debug level 3 */ void script_debug(Jim_Interp *interp, const char *name, unsigned argc, Jim_Obj * const *argv) { if (debug_level < LOG_LVL_DEBUG) return; char *dbg = alloc_printf("command - %s", name); for (unsigned i = 0; i < argc; i++) { int len; const char *w = Jim_GetString(argv[i], &len); char *t = alloc_printf("%s %s", dbg, w); free(dbg); dbg = t; } LOG_DEBUG("%s", dbg); free(dbg); }
static int MySampleCommandFunc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *str; int len; if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "string"); return (JIM_ERR); } str = Jim_GetString(argv[1], &len); assert(str != NULL); printf("%s\n", str); return (JIM_OK); }
static int Jim_PosixSethostnameCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *hostname; int len; if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "hostname"); return JIM_ERR; } hostname = Jim_GetString(argv[1], &len); if (sethostname(hostname, len) == -1) { Jim_PosixSetError(interp); return JIM_ERR; } return JIM_OK; }