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; }
/** * [interp] creates a new interpreter. */ static int JimInterpCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_Interp *child; char buf[32]; if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, ""); return JIM_ERR; } /* Create the interpreter command */ child = Jim_CreateInterp(); Jim_RegisterCoreCommands(child); Jim_InitStaticExtensions(child); /* Copy some core variables to the new interpreter */ JimInterpCopyVariable(child, interp, "argv", NULL); JimInterpCopyVariable(child, interp, "argc", NULL); JimInterpCopyVariable(child, interp, "argv0", NULL); JimInterpCopyVariable(child, interp, "jim::argv0", NULL); JimInterpCopyVariable(child, interp, "jim::exe", NULL); /* Allow the child interpreter to find the parent */ Jim_SetAssocData(child, "interp.parent", NULL, interp); snprintf(buf, sizeof(buf), "interp.handle%ld", Jim_GetId(interp)); Jim_CreateCommand(interp, buf, JimInterpSubCmdProc, child, JimInterpDelProc); Jim_SetResultString(interp, buf, -1); 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; }
static BOOL Win32_GetLastInputInfo(Jim_Interp *interp, int objc, Jim_Obj *const objv[]) { struct lastinputinfo_t { UINT cbSize; DWORD dwTime; } lii; typedef BOOL (__stdcall *LPFNGETLASTINPUTINFO)(struct lastinputinfo_t *); LPFNGETLASTINPUTINFO lpfnGetLastInputInfo = NULL; HMODULE hLib = (HMODULE)Jim_CmdPrivData(interp); JIM_NOTUSED(objc); JIM_NOTUSED(objv); if (hLib != NULL) lpfnGetLastInputInfo = (LPFNGETLASTINPUTINFO)GetProcAddress(hLib, "GetLastInputInfo"); if (lpfnGetLastInputInfo == NULL) { Jim_SetResultString(interp, "command not available on this platform", -1); return JIM_ERR; } lii.cbSize = sizeof(lii); if (!lpfnGetLastInputInfo(&lii)) { Jim_SetResult(interp, Win32ErrorObj(interp, "GetLastInputInfo", GetLastError())); return JIM_ERR; } Jim_SetResult(interp, Jim_NewIntObj(interp, lii.dwTime)); return JIM_OK; }
int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) { char name[MAX_PATH]; HANDLE handle; if (!GetTempPath(MAX_PATH, name) || !GetTempFileName(name, filename_template ? filename_template : "JIM", 0, name)) { return -1; } handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | (unlink_file ? FILE_FLAG_DELETE_ON_CLOSE : 0), NULL); if (handle == INVALID_HANDLE_VALUE) { goto error; } Jim_SetResultString(interp, name, -1); return _open_osfhandle((int)handle, _O_RDWR | _O_TEXT); error: Jim_SetResultErrno(interp, name); DeleteFile(name); return -1; }
static int clock_cmd_format(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { /* How big is big enough? */ char buf[100]; time_t t; long seconds; const char *format = "%a %b %d %H:%M:%S %Z %Y"; if (argc == 2 || (argc == 3 && !Jim_CompareStringImmediate(interp, argv[1], "-format"))) { return -1; } if (argc == 3) { format = Jim_String(argv[2]); } if (Jim_GetLong(interp, argv[0], &seconds) != JIM_OK) { return JIM_ERR; } t = seconds; /*@@if (strftime(buf, sizeof(buf), format, localtime(&t)) == 0) { Jim_SetResultString(interp, "format string too long", -1); return JIM_ERR; }*/ Jim_SetResultString(interp, buf, -1); return JIM_OK; }
static int array_cmd_set(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int i; int len; Jim_Obj *listObj = argv[1]; Jim_Obj *dictObj; len = Jim_ListLength(interp, listObj); if (len % 2) { Jim_SetResultString(interp, "list must have an even number of elements", -1); return JIM_ERR; } dictObj = Jim_GetVariable(interp, argv[0], JIM_UNSHARED); if (!dictObj) { /* Doesn't exist, so just set the list directly */ return Jim_SetVariable(interp, argv[0], listObj); } if (Jim_IsShared(dictObj)) { dictObj = Jim_DuplicateObj(interp, dictObj); } for (i = 0; i < len; i += 2) { Jim_Obj *nameObj; Jim_Obj *valueObj; Jim_ListIndex(interp, listObj, i, &nameObj, JIM_NONE); Jim_ListIndex(interp, listObj, i + 1, &valueObj, JIM_NONE); Jim_DictAddElement(interp, dictObj, nameObj, valueObj); } return Jim_SetVariable(interp, argv[0], dictObj); }
static int signal_cmd_throw(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int sig = SIGINT; if (argc == 1) { if ((sig = find_signal_by_name(interp, Jim_String(argv[0]))) < 0) { return JIM_ERR; } } /* If the signal is ignored (blocked) ... */ if (siginfo[sig].status == SIGNAL_ACTION_IGNORE) { sigsblocked |= sig_to_bit(sig); return JIM_OK; } /* Just set the signal */ interp->sigmask |= sig_to_bit(sig); /* Set the canonical name of the signal as the result */ Jim_SetResultString(interp, Jim_SignalId(sig), -1); /* And simply say we caught the signal */ return JIM_SIGNAL; }
/** * Given the name of a signal, returns the signal value if found, * or returns -1 (and sets an error) if not found. * We accept -SIGINT, SIGINT, INT or any lowercase version or a number, * either positive or negative. */ static int find_signal_by_name(Jim_Interp *interp, const char *name) { int i; const char *pt = name; /* Remove optional - and SIG from the front of the name */ if (*pt == '-') { pt++; } if (strncasecmp(name, "sig", 3) == 0) { pt += 3; } if (isdigit(UCHAR(pt[0]))) { i = atoi(pt); if (i > 0 && i < MAX_SIGNALS) { return i; } } else { for (i = 1; i < MAX_SIGNALS; i++) { /* Jim_SignalId() returns names such as SIGINT, and * returns "unknown signal id" if unknown, so this will work */ if (strcasecmp(Jim_SignalId(i) + 3, pt) == 0) { return i; } } } Jim_SetResultString(interp, "unknown signal ", -1); Jim_AppendString(interp, Jim_GetResult(interp), name, -1); return -1; }
/** * Creates a channel for fh/fd/filename. * * If fh is not NULL, uses that as the channel (and set AIO_KEEPOPEN). * Otherwise, if fd is >= 0, uses that as the chanel. * Otherwise opens 'filename' with mode 'mode'. * * hdlfmt is a sprintf format for the filehandle. Anything with %ld at the end will do. * mode is used for open or fdopen. * * Creates the command and sets the name as the current result. */ static int JimMakeChannel(Jim_Interp *interp, FILE *fh, int fd, Jim_Obj *filename, const char *hdlfmt, int family, const char *mode) { AioFile *af; char buf[AIO_CMD_LEN]; int OpenFlags = 0; if (filename == NULL) { filename = Jim_NewStringObj(interp, hdlfmt, -1); } Jim_IncrRefCount(filename); if (fh == NULL) { if (fd < 0) { fh = fopen(Jim_String(filename), mode); } else { fh = fdopen(fd, mode); } } else { OpenFlags = AIO_KEEPOPEN; } if (fh == NULL) { JimAioSetError(interp, filename); #if !defined(JIM_ANSIC) if (fd >= 0) { close(fd); } #endif Jim_DecrRefCount(interp, filename); return JIM_ERR; } /* Create the file command */ af = Jim_Alloc(sizeof(*af)); memset(af, 0, sizeof(*af)); af->fp = fh; af->fd = fileno(fh); af->filename = filename; #ifdef FD_CLOEXEC if ((OpenFlags & AIO_KEEPOPEN) == 0) { fcntl(af->fd, F_SETFD, FD_CLOEXEC); } #endif af->OpenFlags = OpenFlags; #ifdef O_NDELAY af->flags = fcntl(af->fd, F_GETFL); #endif af->addr_family = family; snprintf(buf, sizeof(buf), hdlfmt, Jim_GetId(interp)); Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc); Jim_SetResultString(interp, buf, -1); return JIM_OK; }
/* Add this handle to the stack of handles to be freed */ if (!interp->loadHandles) { interp->loadHandles = Jim_Alloc(sizeof(*interp->loadHandles)); Jim_InitStack(interp->loadHandles); } Jim_StackPush(interp->loadHandles, handle); Jim_SetEmptyResult(interp); return JIM_OK; } } if (handle) { dlclose(handle); } return JIM_ERR; } static void JimFreeOneLoadHandle(void *handle) { dlclose(handle); } void Jim_FreeLoadHandles(Jim_Interp *interp) { if (interp->loadHandles) { Jim_FreeStackElements(interp->loadHandles, JimFreeOneLoadHandle); Jim_Free(interp->loadHandles); } } #else /* JIM_DYNLIB */ int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName) { JIM_NOTUSED(interp); JIM_NOTUSED(pathName); Jim_SetResultString(interp, "the Jim binary has no support for [load]", -1); return JIM_ERR; }
static int Jim_Decompress(Jim_Interp *interp, const char *in, int len, long bufsiz, int wbits) { z_stream strm = {0}; void *buf; Jim_Obj *out; int ret; if (JimZlibCheckBufSize(interp, bufsiz)) { return JIM_ERR; } if (inflateInit2(&strm, wbits) != Z_OK) { return JIM_ERR; } /* allocate a buffer - decompression is done in chunks, into this buffer; * when the decompressed data size is given, decompression is faster because * it's done in one pass, with less memcpy() overhead */ buf = Jim_Alloc((int)bufsiz); out = Jim_NewEmptyStringObj(interp); Jim_IncrRefCount(out); strm.next_in = (Bytef*)in; strm.avail_in = (uInt)len; do { do { strm.next_out = buf; strm.avail_out = (uInt)bufsiz; ret = inflate(&strm, Z_NO_FLUSH); switch (ret) { case Z_OK: case Z_STREAM_END: /* append each chunk to the output object */ Jim_AppendString(interp, out, buf, (int)(bufsiz - (long)strm.avail_out)); break; default: Jim_DecrRefCount(interp, out); Jim_Free(buf); inflateEnd(&strm); if (strm.msg != NULL) Jim_SetResultString(interp, strm.msg, -1); return JIM_ERR; } } while (strm.avail_out == 0); } while (ret != Z_STREAM_END); /* free memory used for decompression before we assign the return value */ Jim_Free(buf); inflateEnd(&strm); Jim_SetResult(interp, out); Jim_DecrRefCount(interp, out); return JIM_OK; }
/* *----------------------------------------------------------------------------- * * Jim_ReaddirCmd -- * Implements the rename TCL command: * readdir ?-nocomplain? dirPath * * Results: * Standard TCL result. *----------------------------------------------------------------------------- */ int Jim_ReaddirCmd (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *dirPath; DIR *dirPtr; struct dirent *entryPtr; int nocomplain = 0; if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) { nocomplain = 1; } if (argc != 2 && !nocomplain) { Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath"); return JIM_ERR; } dirPath = Jim_GetString(argv[1 + nocomplain], NULL); dirPtr = opendir (dirPath); if (dirPtr == NULL) { if (nocomplain) { return JIM_OK; } Jim_SetResultString(interp, strerror(errno), -1); return JIM_ERR; } Jim_SetResultString(interp, strerror(errno), -1); Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0)); while ((entryPtr = readdir (dirPtr)) != NULL) { if (entryPtr->d_name [0] == '.') { if (entryPtr->d_name [1] == '\0') { continue; } if ((entryPtr->d_name [1] == '.') && (entryPtr->d_name [2] == '\0')) continue; } Jim_ListAppendElement(interp, Jim_GetResult(interp), Jim_NewStringObj(interp, entryPtr->d_name, -1)); } closedir (dirPtr); 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 void JimAioSetError(Jim_Interp *interp, Jim_Obj *name) { if (name) { Jim_SetResultFormatted(interp, "%#s: %s", name, strerror(errno)); } else { Jim_SetResultString(interp, strerror(errno), -1); } }
static int JimZlibCheckBufSize(Jim_Interp *interp, jim_wide bufsiz) { if ((bufsiz <= 0) || (bufsiz > INT_MAX)) { Jim_SetResultString(interp, "buffer size must be 0 to ", -1); Jim_AppendObj(interp, Jim_GetResult(interp), Jim_NewIntObj(interp, INT_MAX)); return JIM_ERR; } return JIM_OK; }
static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args) { tap_state_t states[8]; if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1))) { Jim_WrongNumArgs(interp, 1, args, "wrong arguments"); return JIM_ERR; } script_debug(interp, "pathmove", argc, args); int i; for (i = 0; i < argc-1; i++) { const char *cp; cp = Jim_GetString(args[i + 1], NULL); states[i] = tap_state_by_name(cp); if (states[i] < 0) { /* update the error message */ Jim_SetResultFormatted(interp,"endstate: %s invalid", cp); return JIM_ERR; } } if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK)) { Jim_SetResultString(interp, "pathmove: jtag execute failed",-1); return JIM_ERR; } jtag_add_pathmove(argc-2, states + 1); if (jtag_execute_queue()!= ERROR_OK) { Jim_SetResultString(interp, "pathmove: failed",-1); return JIM_ERR; } return JIM_OK; }
static int file_cmd_extension(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *path = Jim_String(argv[0]); const char *lastSlash = strrchr(path, '/'); const char *p = strrchr(path, '.'); if (p == NULL || (lastSlash != NULL && lastSlash >= p)) { p = ""; } Jim_SetResultString(interp, p, -1); return JIM_OK; }
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; }
static int file_cmd_tail(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *path = Jim_String(argv[0]); const char *lastSlash = strrchr(path, '/'); if (lastSlash) { Jim_SetResultString(interp, lastSlash + 1, -1); } else { Jim_SetResult(interp, argv[0]); } return JIM_OK; }
static int file_cmd_rootname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *path = Jim_String(argv[0]); const char *lastSlash = strrchr(path, '/'); const char *p = strrchr(path, '.'); if (p == NULL || (lastSlash != NULL && lastSlash > p)) { Jim_SetResult(interp, argv[0]); } else { Jim_SetResultString(interp, path, p - path); } return JIM_OK; }
static int file_cmd_dirname(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { const char *path = Jim_String(argv[0]); const char *p = strrchr(path, '/'); if (!p && path[0] == '.' && path[1] == '.' && path[2] == '\0') { Jim_SetResultString(interp, "..", -1); } else if (!p) { Jim_SetResultString(interp, ".", -1); } else if (p == path) { Jim_SetResultString(interp, "/", -1); } #if defined(__MINGW32__) || defined(_MSC_VER) else if (p[-1] == ':') { /* z:/dir => z:/ */ Jim_SetResultString(interp, path, p - path + 1); } #endif else { Jim_SetResultString(interp, path, p - path); } return JIM_OK; }
static int Win32_GetPerformanceInfo(Jim_Interp *interp, int objc, Jim_Obj *const *objv) { Jim_Obj *a[26]; size_t n = 0; PERFORMANCE_INFORMATION pi; LPFNGETPERFORMANCEINFO lpfnGetPerformanceInfo = NULL; HMODULE hLib = (HMODULE)Jim_CmdPrivData(interp); JIM_NOTUSED(objc); JIM_NOTUSED(objv); if (hLib != NULL) lpfnGetPerformanceInfo = (LPFNGETPERFORMANCEINFO)GetProcAddress(hLib, "GetPerformanceInfo"); if (lpfnGetPerformanceInfo == NULL) { /* should never happen */ Jim_SetResultString(interp, "argh!", -1); return JIM_ERR; } pi.cb = sizeof(pi); if (!lpfnGetPerformanceInfo(&pi, sizeof(pi))) { Jim_SetResult(interp, Win32ErrorObj(interp, "GetPerformanceInfo", GetLastError())); return JIM_ERR; } #define JIMADD(name) \ a[n++] = Jim_NewStringObj(interp, #name, -1); \ a[n++] = Jim_NewIntObj(interp, pi.name ) JIMADD(CommitTotal); JIMADD(CommitLimit); JIMADD(CommitPeak); JIMADD(PhysicalTotal); JIMADD(PhysicalAvailable); JIMADD(SystemCache); JIMADD(KernelTotal); JIMADD(KernelPaged); JIMADD(KernelNonpaged); JIMADD(PageSize); JIMADD(HandleCount); JIMADD(ProcessCount); JIMADD(ThreadCount); #undef JIMADD Jim_SetResult(interp, Jim_NewListObj(interp, a, n)); return JIM_OK; }
/** * Implements the Tcl "transport select" command, choosing the * transport to be used in this debug session from among the * set supported by the debug adapter being used. Return value * is scriptable (allowing "if swd then..." etc). */ static int jim_transport_select(Jim_Interp *interp, int argc, Jim_Obj * const *argv) { switch (argc) { case 1: /* return/display */ if (!session) { LOG_ERROR("session's transport is not selected."); return JIM_ERR; } else { Jim_SetResultString(interp, session->name, -1); return JIM_OK; } break; case 2: /* assign */ if (session) { if (!strcmp(session->name, argv[1]->bytes)) { LOG_WARNING("Transport \"%s\" was already selected", session->name); return JIM_OK; } else { LOG_ERROR("Can't change session's transport after the initial selection was made"); return JIM_ERR; } } /* Is this transport supported by our debug adapter? * Example, "JTAG-only" means SWD is not supported. * * NOTE: requires adapter to have been set up, with * transports declared via C. */ if (!allowed_transports) { LOG_ERROR("Debug adapter doesn't support any transports?"); return JIM_ERR; } for (unsigned i = 0; allowed_transports[i]; i++) { if (strcmp(allowed_transports[i], argv[1]->bytes) == 0) return transport_select(global_cmd_ctx, argv[1]->bytes); } LOG_ERROR("Debug adapter doesn't support '%s' transport", argv[1]->bytes); return JIM_ERR; break; default: Jim_WrongNumArgs(interp, 1, argv, "[too many parameters]"); return JIM_ERR; } }
static int Jim_PosixGethostnameCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { char buf[JIM_HOST_NAME_MAX]; if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, ""); return JIM_ERR; } if (gethostname(buf, JIM_HOST_NAME_MAX) == -1) { Jim_PosixSetError(interp); return JIM_ERR; } Jim_SetResultString(interp, buf, -1); return JIM_OK; }
static int Jim_PosixForkCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { pid_t pid; JIM_NOTUSED(argv); if (argc != 1) { Jim_WrongNumArgs(interp, 1, argv, ""); return JIM_ERR; } if ((pid = fork()) == -1) { Jim_SetResultString(interp, strerror(errno), -1); return JIM_ERR; } Jim_SetResult(interp, Jim_NewIntObj(interp, (jim_wide)pid)); return JIM_OK; }
static int Jim_Compress(Jim_Interp *interp, const char *in, int len, long level, int wbits) { z_stream strm = {0}; Bytef *buf; if ((level != Z_DEFAULT_COMPRESSION) && ((level < Z_NO_COMPRESSION) || (level > Z_BEST_COMPRESSION))) { Jim_SetResultString(interp, "level must be 0 to 9", -1); return JIM_ERR; } if (deflateInit2(&strm, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) { return JIM_ERR; } strm.avail_out = deflateBound(&strm, (uLong)len); if (strm.avail_out > INT_MAX) { deflateEnd(&strm); return JIM_ERR; } buf = (Bytef *)Jim_Alloc((int)strm.avail_out); strm.next_out = buf; strm.next_in = (Bytef *)in; strm.avail_in = (uInt)len; /* always compress in one pass - the return value holds the entire * decompressed data anyway, so there's no reason to do chunked * decompression */ if (deflate(&strm, Z_FINISH) != Z_STREAM_END) { Jim_Free(strm.next_out); deflateEnd(&strm); return JIM_ERR; } deflateEnd(&strm); if (strm.total_out > INT_MAX) { Jim_Free(strm.next_out); return JIM_ERR; } Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, (char *)buf, (int)strm.total_out)); return JIM_OK; }
static int Jim_KillCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { int sig; long pid; Jim_Obj *pidObj; const char *signame; if (argc != 2 && argc != 3) { Jim_WrongNumArgs(interp, 1, argv, "?SIG|-0? pid"); return JIM_ERR; } if (argc == 2) { sig = SIGTERM; pidObj = argv[1]; } else { signame = Jim_String(argv[1]); pidObj = argv[2]; /* Special 'kill -0 pid' to determine if a pid exists */ if (strcmp(signame, "-0") == 0 || strcmp(signame, "0") == 0) { sig = 0; } else { sig = find_signal_by_name(interp, signame); if (sig < 0) { return JIM_ERR; } } } if (Jim_GetLong(interp, pidObj, &pid) != JIM_OK) { return JIM_ERR; } if (kill(pid, sig) == 0) { return JIM_OK; } Jim_SetResultString(interp, "kill: Failed to deliver signal", -1); return JIM_ERR; }
static int file_cmd_normalize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { #ifdef HAVE_REALPATH const char *path = Jim_String(argv[0]); char *newname = Jim_Alloc(MAXPATHLEN + 1); if (realpath(path, newname)) { Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, newname, -1)); return JIM_OK; } else { Jim_Free(newname); Jim_SetResultFormatted(interp, "can't normalize \"%#s\": %s", argv[0], strerror(errno)); return JIM_ERR; } #else Jim_SetResultString(interp, "Not implemented", -1); return JIM_ERR; #endif }
static int JimAioAcceptHelper(Jim_Interp *interp, AioFile *serv_af ) { int sock; int addrlen = sizeof(struct sockaddr_in); AioFile *af; char buf[AIO_CMD_LEN]; Jim_Obj *objPtr; long fileId; fprintf(stderr,"accepting connection for %d \n",serv_af->fd); sock = accept(serv_af->fd,(struct sockaddr*)&serv_af->sa,&addrlen); fprintf(stderr,"done, got %d \n",sock); if (sock < 0) return JIM_ERR; /* Get the next file id */ fprintf(stderr,"getting fileid:"); if (Jim_EvalGlobal(interp, "if {[catch {incr aio.fileId}]} {set aio.fileId 0}") != JIM_OK) return JIM_ERR; objPtr = Jim_GetGlobalVariableStr(interp, "aio.fileId", JIM_ERRMSG); if (objPtr == NULL) return JIM_ERR; if (Jim_GetLong(interp, objPtr, &fileId) != JIM_OK) return JIM_ERR; fprintf(stderr," %ld\n", fileId); /* Create the file command */ af = Jim_Alloc(sizeof(*af)); af->fd = sock; af->fp = fdopen(sock,"r+"); af->OpenFlags = AIO_FDOPEN; af->flags = fcntl(af->fd,F_GETFL); // fprintf(stderr,"hallo\n"); af->rEvent = NULL; af->wEvent = NULL; af->eEvent = NULL; sprintf(buf, "aio.sockstream%ld", fileId); Jim_CreateCommand(interp, buf, JimAioHandlerCommand, af, JimAioDelProc); Jim_SetResultString(interp, buf, -1); fprintf(stderr,"returning\n"); return JIM_OK; }