+/* 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_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;
+}
+
+static size_t openocd_jim_fwrite(const void *_ptr, size_t size, size_t n, void *cookie)
+{
+ size_t nbytes;
+ const char *ptr;
+ Jim_Interp *interp;
+
+ /* make it a char easier to read code */
+ ptr = _ptr;
+ interp = cookie;
+ nbytes = size * n;
+ if (ptr == NULL || interp == NULL || nbytes == 0) {
+ return 0;
+ }
+
+ /* do we have to chunk it? */
+ if (ptr[nbytes] == 0)
+ {
+ /* no it is a C style string */
+ LOG_USER_N("%s", ptr);
+ return strlen(ptr);
+ }
+ /* GRR we must chunk - not null terminated */
+ while (nbytes) {
+ char chunk[128+1];
+ int x;
+
+ x = nbytes;
+ if (x > 128) {
+ x = 128;
+ }
+ /* copy it */
+ memcpy(chunk, ptr, x);
+ /* terminate it */
+ chunk[n] = 0;
+ /* output it */
+ LOG_USER_N("%s", chunk);
+ ptr += x;
+ nbytes -= x;
+ }
+
+ return n;
+}
+
+static size_t openocd_jim_fread(void *ptr, size_t size, size_t n, void *cookie)
+{
+ /* TCL wants to read... tell him no */
+ return 0;
+}
+
+static int openocd_jim_vfprintf(void *cookie, const char *fmt, va_list ap)
+{
+ char *cp;
+ int n;
+ Jim_Interp *interp;
+
+ n = -1;
+ interp = cookie;
+ if (interp == NULL)
+ return n;
+
+ cp = alloc_vprintf(fmt, ap);
+ if (cp)
+ {
+ LOG_USER_N("%s", cp);
+ n = strlen(cp);
+ free(cp);
+ }
+ return n;
+}
+
+static int openocd_jim_fflush(void *cookie)
+{
+ /* nothing to flush */
+ return 0;
+}
+
+static char* openocd_jim_fgets(char *s, int size, void *cookie)
+{
+ /* not supported */
+ errno = ENOTSUP;
+ return NULL;
+}
+
+static int jim_capture(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
+{
+ if (argc != 2)
+ return JIM_ERR;
+ int retcode;
+ const char *str = Jim_GetString(argv[1], NULL);
+
+ /* capture log output and return it */
+ Jim_Obj *tclOutput = Jim_NewStringObj(interp, "", 0);
+ /* a garbage collect can happen, so we need a reference count to this object */
+ Jim_IncrRefCount(tclOutput);
+
+ log_add_callback(tcl_output, tclOutput);
+
+ retcode = Jim_Eval_Named(interp, str, __THIS__FILE__, __LINE__ );
+
+ log_remove_callback(tcl_output, tclOutput);
+
+ /* We dump output into this local variable */
+ Jim_SetResult(interp, tclOutput);
+ Jim_DecrRefCount(interp, tclOutput);
+
+ return retcode;
+}
+