static JSBool rpmaug_match(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { void * ptr = JS_GetInstancePrivate(cx, obj, &rpmaugClass, NULL); rpmaug aug = ptr; JSBool ok = JS_FALSE; const char * _path = NULL; char ** _matches = NULL; int nmatches; _METHOD_DEBUG_ENTRY(_debug); if (!(ok = JS_ConvertArguments(cx, argc, argv, "s", &_path))) goto exit; nmatches = rpmaugMatch(aug, _path, &_matches); if (nmatches <= 0) { /* not found */ *rval = JSVAL_VOID; goto exit; } else { JSObject *o; jsval v; int i; *rval = OBJECT_TO_JSVAL(o=JS_NewArrayObject(cx, 0, NULL)); for (i = 0; i < nmatches; i++) { v = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, _matches[i])); ok = JS_SetElement(cx, o, i, &v); _matches[i] = _free(_matches[i]); } _matches = _free(_matches); } ok = JS_TRUE; exit: return ok; }
static int child_count(const char *path) { char *q = ls_pattern(path); int cnt; if (q == NULL) return 0; cnt = rpmaugMatch(NULL, q, NULL); free(q); return cnt; }
static char *readline_path_generator(const char *text, int state) { static int current = 0; static char **children = NULL; static int nchildren = 0; if (state == 0) { char *end = strrchr(text, SEP); char *path; if (end == NULL) { if ((path = strdup("/*")) == NULL) return NULL; } else { end += 1; path = xcalloc(1, end - text + 2); if (path == NULL) return NULL; strncpy(path, text, end - text); strcat(path, "*"); } for (;current < nchildren; current++) free((void *) children[current]); free((void *) children); nchildren = rpmaugMatch(NULL, path, &children); current = 0; free(path); } while (current < nchildren) { char *child = children[current]; current += 1; if (!strncmp(child, text, strlen(text))) { if (child_count(child) > 0) { char *c = realloc(child, strlen(child)+2); if (c == NULL) return NULL; child = c; strcat(child, "/"); } rl_filename_completion_desired = 1; rl_completion_append_character = '\0'; return child; } else { free(child); } } return NULL; }