/** * [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; }
/** * 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; }
static int JimSqliteOpenCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { sqlite3 *db; char buf[60]; int r; if (argc != 2) { Jim_WrongNumArgs(interp, 1, argv, "dbname"); return JIM_ERR; } r = sqlite3_open(Jim_String(argv[1]), &db); if (r != SQLITE_OK) { Jim_SetResultString(interp, sqlite3_errmsg(db), -1); sqlite3_close(db); return JIM_ERR; } /* Create the file command */ snprintf(buf, sizeof(buf), "sqlite.handle%ld", Jim_GetId(interp)); Jim_CreateCommand(interp, buf, JimSqliteHandlerCommand, db, JimSqliteDelProc); Jim_SetResult(interp, Jim_MakeGlobalNamespaceName(interp, Jim_NewStringObj(interp, buf, -1))); return JIM_OK; }