bitu_plugin_t * bitu_plugin_ctx_find_for_cmdline (bitu_plugin_ctx_t *plugin_ctx, const char *cmdline) { void *iter; bitu_plugin_t *plugin = NULL; /* Iterating over all loaded plugins and looking for one that matches * the received command line. The first one that matches will be * returned. */ if ((iter = hashtable_iter (plugin_ctx->plugins)) == NULL) return NULL; do { plugin = hashtable_iter_value (iter); if (plugin && plugin->match && plugin->match (cmdline)) return plugin; } while ((iter = hashtable_iter_next (plugin_ctx->plugins, iter))); /* It was not possible to match the command line in any plugin. We'll * have to parse the command line, get the plugin name and try to * execute it. */ if (bitu_util_extract_params (cmdline, NULL, NULL, NULL) == TA_OK) if ((plugin = bitu_plugin_ctx_find (plugin_ctx, cmdline)) != NULL) return plugin; return NULL; }
static void printHashtable (hashtable_t* hashtablePtr) { long i; hashtable_iter_t it; printf("["); hashtable_iter_reset(&it, hashtablePtr); while (hashtable_iter_hasNext(&it, hashtablePtr)) { printf("%li ", *((long*)(hashtable_iter_next(&it, hashtablePtr)))); } puts("]"); /* Low-level to see structure */ for (i = 0; i < hashtablePtr->numBucket; i++) { list_iter_t it; printf("%2li: [", i); list_iter_reset(&it, hashtablePtr->buckets[i]); while (list_iter_hasNext(&it, hashtablePtr->buckets[i])) { void* pairPtr = list_iter_next(&it, hashtablePtr->buckets[i]); printf("%li ", *(long*)(((pair_t*)pairPtr)->secondPtr)); } puts("]"); } }
void *json_object_iter_next(json_t *json, void *iter) { json_object_t *object; if(!json_is_object(json) || iter == NULL) return NULL; object = json_to_object(json); return hashtable_iter_next(&object->hashtable, iter); }
ta_list_t * bitu_plugin_ctx_get_list (bitu_plugin_ctx_t *plugin_ctx) { void *iter; ta_list_t *ret = NULL; iter = hashtable_iter (plugin_ctx->plugins); if (iter == NULL) return NULL; do ret = ta_list_append (ret, hashtable_iter_key (iter)); while ((iter = hashtable_iter_next (plugin_ctx->plugins, iter))); return ret; }
static char * cmd_env (bitu_app_t *app, char **TA_UNUSED(params), int num_params) { void *iter; char *error, *val, *tmp, *pos, *list = NULL; size_t val_size, current_size = 0, full_size = 0, step = 256, lastp = 0; if ((error = _validate_num_params ("env", 0, num_params)) != NULL) return error; iter = hashtable_iter (app->environment); if (iter == NULL) return NULL; do { val = hashtable_iter_key (iter); val_size = strlen (val); current_size += val_size + 1; if (full_size < current_size) { full_size += step; if ((tmp = realloc (list, full_size)) == NULL) { free (list); return NULL; } else list = tmp; } pos = list + lastp; memcpy (pos, val, val_size); memcpy (pos+val_size, "\n", 1); lastp += val_size + 1; } while ((iter = hashtable_iter_next (app->environment, iter))); list[current_size-1] = '\0'; return list; }
void *pc_map_iter_next(pc_map_t *map, void *iter) { return hashtable_iter_next(&map->table, iter); }
void *hashtable_iter(hashtable_t *hashtable) { return hashtable_iter_next(hashtable, &hashtable->list); }
static char * cmd_list (bitu_app_t *app, char **params, int num_params) { char *action, *ret; char *error; if ((error = _validate_num_params ("list", 1, num_params)) != NULL) return error; ret = NULL; action = params[0]; if (strcmp (action, "plugins") == 0) { ta_list_t *plugins, *tmp; char *val, *tmp_val, *current_pos_str; size_t val_size, current_pos, full_size = 0; plugins = bitu_plugin_ctx_get_list (app->plugin_ctx); for (tmp = plugins; tmp; tmp = tmp->next) { val = tmp->data; /* This +1 means the \n at the end of each line. */ val_size = strlen (val) + 1; /* Remembering current end of the full string. */ current_pos = full_size; full_size += val_size; if ((tmp_val = realloc (ret, full_size)) == NULL) { free (ret); return NULL; } else ret = tmp_val; current_pos_str = ret + current_pos; memcpy (current_pos_str, val, val_size); memcpy (current_pos_str + val_size - 1, "\n", 1); } /* Removing the last \n. It is not needed in the end of the * string */ if (ret != NULL) { memcpy (ret + full_size - 1, "\0", 1); ta_list_free (plugins); } } else if (strcmp (action, "commands") == 0) { void *iter; char *val, *tmp, *current_pos_str; size_t val_size, current_pos, full_size = 0; iter = hashtable_iter (app->commands); if (iter == NULL) return NULL; do { val = hashtable_iter_key (iter); /* This +1 means the \n at the end of each line. */ val_size = strlen (val) + 1; /* Remembering current end of the full string. */ current_pos = full_size; full_size += val_size; if ((tmp = realloc (ret, full_size)) == NULL) { free (ret); return NULL; } else ret = tmp; current_pos_str = ret + current_pos; memcpy (current_pos_str, val, val_size); memcpy (current_pos_str + val_size - 1, "\n", 1); } while ((iter = hashtable_iter_next (app->commands, iter))); /* Removing the last \n. It is not needed in the end of the string */ ret[full_size-1] = '\0'; } else ret = strdup ("Possible values are `commands' or `plugins'"); return ret; }