Index: main/storage.c
===================================================================
--- main/storage.c	(revision 90669)
+++ main/storage.c	(working copy)
@@ -104,7 +104,7 @@
 	}
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&storage_engines, tmp, list) {
 		if (!strcasecmp(name, tmp->name)) {
-			AST_RWLIST_REMOVE_CURRENT(&storage_engines, list);
+			AST_RWLIST_REMOVE_CURRENT(list);
 			free(tmp);
 			res = 0;
 		}
@@ -113,14 +113,13 @@
 	AST_RWLIST_UNLOCK(&storage_engines);
 
 	if (!res) {
-		if (option_verbose > 1)
-			ast_verbose(VERBOSE_PREFIX_2 "Unregistered storage engine '%s'\n", name);
+		ast_verb(2, "Unregistered storage engine '%s'\n", name);
 	} else
 		ast_log(LOG_WARNING, "Tried to unregister storage engine '%s', already unregistered\n", name);
 
 	return res;
 }
-
+//NOTE shouldn't it return -1/1 on error if fl doesn't exist
 static int empty_filelist(struct ast_storage_filelist *fl)
 {
 	struct ast_storage_fileinst *fi;
@@ -130,7 +129,7 @@
 
 	AST_RWLIST_WRLOCK(&fl->files);
 	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&fl->files, fi, list) {
-		AST_RWLIST_REMOVE_CURRENT(&fl->files, list);
+		AST_RWLIST_REMOVE_CURRENT(list);
 		free(fi->ent);
 		free(fi);
 	}
@@ -282,7 +281,7 @@
 	return count;
 }
 
-static int storage_show_file(int fd, int argc, char *argv[])
+static char *handle_storage_show_file(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct ast_storage *st;
 	struct ast_storage_fileinst *fi;
@@ -293,10 +292,23 @@
 	int count_files = 0;
 	/* XXX We want to remove the need to pass in a char array like this now.. */
 	char blah[PATH_MAX];
-	if (argc != 4)
-		return RESULT_SHOWUSAGE;
 
-	stringp = ast_strdupa(argv[3]);
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "storage show file";
+		e->usage =
+			"Usage: storage show file <engine://filename>\n"
+			"       Displays information about a file via the storage engine\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+	
+	if (a->argc != 4)
+		return CLI_SHOWUSAGE;
+
+	stringp = ast_strdupa(a->argv[3]);
 	if ((fn = strstr(stringp, "://"))) {
 		*fn = '\0';
 		fn += 3;
@@ -304,14 +316,14 @@
 	sename = stringp;
 
 	if (!(ast_storage_getbyname(sename))) {
-		ast_cli(fd, "No storage engine named '%s' found.\n", sename);
-		return RESULT_SUCCESS;
+		ast_cli(a->fd, "No storage engine named '%s' found.\n", sename);
+		return CLI_SUCCESS;
 	}
 
 	if (!(st = ast_storage_request(sename, fn)))
-		ast_cli(fd, "Could not create storage engine named '%s'.\n", sename);
+		ast_cli(a->fd, "Could not create storage engine named '%s'.\n", sename);
 	else {
-		ast_cli(fd, "File information\n");
+		ast_cli(a->fd, "File information\n");
 		/* We don't care about format/extension here, so just send NULL */
 		if (!(ast_storage_get(st, st->path, NULL, blah, sizeof(blah)))) {
 			AST_RWLIST_TRAVERSE(&st->fl->files, fi, list) {
@@ -323,80 +335,62 @@
 				}
 
 				count_files++;
-				ast_cli(fd, "Name  : %s\n", fi->ent->name);
-				ast_cli(fd, "Format: %s\n", ast_getformatname(ast_getformatbyextension(ext)));
+				ast_cli(a->fd, "Name  : %s\n", fi->ent->name);
+				ast_cli(a->fd, "Format: %s\n", ast_getformatname(ast_getformatbyextension(ext)));
 			}
 		}
-		ast_cli(fd, "\n");
-		ast_cli(fd, "Found %d matching file(s).\n", count_files);
+		ast_cli(a->fd, "\n");
+		ast_cli(a->fd, "Found %d matching file(s).\n", count_files);
 	}
 	ast_storage_release(st);
-	return RESULT_SUCCESS;
+	return CLI_SUCCESS;
 }
 
-static int storage_show_engines(int fd, int argc, char *argv[])
+static char *handle_storage_show_engines(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct ast_storage_be *be;
 	int count_se = 0;
 
-	if (argc != 3)
-		return RESULT_SHOWUSAGE;
-	ast_cli(fd, "Storage Engine\n");
+	switch(cmd) {
+	case CLI_INIT:
+		e->command = "storage show engines";
+		e->usage =
+			"Usage: storage show engines\n"
+			"       Displays currently registered storage engines (if any)\n";
+		return NULL;
+	case CLI_GENERATE:
+			return NULL;
+	}
 
+	if(a->argc != 3)
+		return CLI_SHOWUSAGE;
+
+	ast_cli(a->fd, "Storage Engine\n");
+
 	if (AST_RWLIST_RDLOCK(&storage_engines)) {
 		ast_log(LOG_WARNING, "Unable to lock storage engine list\n");
-		return RESULT_FAILURE;
+		return CLI_FAILURE;
 	}
 
 	AST_RWLIST_TRAVERSE(&storage_engines, be, list) {
-		ast_cli(fd, "%s\n", be->name);
+		ast_cli(a->fd, "%s\n", be->name);
 		count_se++;
 	}
 	AST_RWLIST_UNLOCK(&storage_engines);
-	ast_cli(fd, "%d storage engine(s) registered.\n", count_se);
-	return RESULT_SUCCESS;
+	ast_cli(a->fd, "%d storage engine(s) registered.\n", count_se);
+	return CLI_SUCCESS;
 }
 
-static int storage_show_engine(int fd, int argc, char *argv[])
+static char *complete_storage_show_engines(struct ast_cli_args *a)
 {
 	struct ast_storage_be *be;
-
-	if (argc != 4)
-		return RESULT_SHOWUSAGE;
-
-	if ((be = ast_storage_getbyname(argv[3]))) {
-		ast_cli(fd, "Storage Engine: %s\n", be->name);
-		ast_cli(fd, "Can get       : %s\n", be->get ? "yes" : "no");
-#if 0
-		ast_cli(fd, "Can put       : %s\n", be->put ? "yes" : "no");
-#endif
-		ast_cli(fd, "Can del       : %s\n", be->del ? "yes" : "no");
-		ast_cli(fd, "Can opendir   : %s\n", be->opendir ? "yes" : "no");
-		ast_cli(fd, "Can readdir   : %s\n", be->readdir ? "yes" : "no");
-		ast_cli(fd, "Can closedir  : %s\n", be->closedir ? "yes" : "no");
-		if (be->status) {
-			ast_cli(fd, "\n");
-			ast_cli(fd, "Additional status\n");
-			/* XXX Need to rethink this.
-			 * This requires a struct ast_storage *, and we can't cast to it from ast_storage_be
-			be->status((struct ast_storage *)be, fd);
-			 */
-		}
-	} else
-		ast_cli(fd, "No storage engine named '%s' found.\n", argv[3]);
-	return RESULT_SUCCESS;
-}
-
-static char *complete_storage_show_engines(const char *line, const char *word, int pos, int state)
-{
-	struct ast_storage_be *be;
 	char *ret = NULL;
 	int which = 0;
-	int wordlen = strlen(word);
+	int wordlen = strlen(a->word);
 
 	AST_RWLIST_RDLOCK(&storage_engines);
 	AST_RWLIST_TRAVERSE(&storage_engines, be, list) {
-		if (!strncasecmp(word, be->name, wordlen) && ++which > state) {
+		if (!strncasecmp(a->word, be->name, wordlen) && ++which > a->n) {
 			ret = strdup(be->name);
 			break;
 		}
@@ -405,31 +399,53 @@
 
 	return ret;
 }
+static char *handle_storage_show_engine(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ast_storage_be *be;
 
-char storage_show_file_usage[] =
-"Usage: storage show file <engine://filename>\n"
-"       Displays information about a file via the storage engine\n";
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "storage show engine";
+		e->usage =
+			"Usage: storage show engine <engine>\n"
+			"       Displays capabilities of requested storage engine\n";
+		return NULL;
+	case CLI_GENERATE:
+		return complete_storage_show_engines(a);
+	}
 
-char storage_show_engines_usage[] = 
-"Usage: storage show engines\n"
-"       Displays currently registered storage engines (if any)\n";
+	if (a->argc != 4)
+		return CLI_SHOWUSAGE;
 
-char storage_show_engine_usage[] =
-"Usage: storage show engine <engine>\n"
-"       Displays capabilities of requested storage engine\n";
+	if ((be = ast_storage_getbyname(a->argv[3]))) {
+		ast_cli(a->fd, "Storage Engine: %s\n", be->name);
+		ast_cli(a->fd, "Can get       : %s\n", be->get ? "yes" : "no");
+#if 0
+		ast_cli(a->fd, "Can put       : %s\n", be->put ? "yes" : "no");
+#endif
+		ast_cli(a->fd, "Can del       : %s\n", be->del ? "yes" : "no");
+		ast_cli(a->fd, "Can opendir   : %s\n", be->opendir ? "yes" : "no");
+		ast_cli(a->fd, "Can readdir   : %s\n", be->readdir ? "yes" : "no");
+		ast_cli(a->fd, "Can closedir  : %s\n", be->closedir ? "yes" : "no");
+		if (be->status) {
+			ast_cli(a->fd, "\n");
+			ast_cli(a->fd, "Additional status\n");
+			/* XXX Need to rethink this.
+			 * This requires a struct ast_storage *, and we can't cast to it from ast_storage_be
+			be->status((struct ast_storage *)be, fd);
+			 */
+		}
+	} else
+		ast_cli(a->fd, "No storage engine named '%s' found.\n", a->argv[3]);
+	return CLI_SUCCESS;
+}
 
-struct ast_cli_entry cli_storage[] = {
-	{ { "storage", "show", "file", NULL },
-	storage_show_file, "Displays information for a file",
-	storage_show_file_usage },
 
-	{ { "storage", "show", "engines" },
-	storage_show_engines, "Displays storage engines",
-	storage_show_engines_usage },
 
-	{ { "storage", "show", "engine", NULL },
-	storage_show_engine, "Displays storage engine capabilities",
-	storage_show_engine_usage, complete_storage_show_engines },
+struct ast_cli_entry cli_storage[] = {
+	AST_CLI_DEFINE(handle_storage_show_engines,"Show a storage engine"),
+	AST_CLI_DEFINE(handle_storage_show_engine,"List of storage engines"),
+	AST_CLI_DEFINE(handle_storage_show_file,"Show a file within a storage engine"),
 };
 
 static const struct ast_storage_be null_se = {
