[svn-commits] rizzo: branch rizzo/astobj2 r47833 - /team/rizzo/astobj2/channels/chan_sip.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Sat Nov 18 14:38:38 MST 2006


Author: rizzo
Date: Sat Nov 18 15:38:37 2006
New Revision: 47833

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47833
Log:
move "sip prune realtime ..." to new cli structure


Modified:
    team/rizzo/astobj2/channels/chan_sip.c

Modified: team/rizzo/astobj2/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/channels/chan_sip.c?view=diff&rev=47833&r1=47832&r2=47833
==============================================================================
--- team/rizzo/astobj2/channels/chan_sip.c (original)
+++ team/rizzo/astobj2/channels/chan_sip.c Sat Nov 18 15:38:37 2006
@@ -1464,8 +1464,6 @@
 static char *complete_sip_user(const char *word, int state, int flags2);
 static char *complete_sip_show_user(const char *line, const char *word, int pos, int state);
 static char *complete_sipnotify(const char *line, const char *word, int pos, int state);
-static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state);
-static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state);
 static int sip_show_channel(int fd, int argc, char *argv[]);
 static int sip_show_history(int fd, int argc, char *argv[]);
 static int sip_notify(int fd, int argc, char *argv[]);
@@ -1514,7 +1512,6 @@
 static struct sip_user *realtime_user(const char *username);
 static void update_peer(struct sip_peer *p, int expiry);
 static struct sip_peer *realtime_peer(const char *peername, struct sockaddr_in *sin);
-static int sip_prune_realtime(int fd, int argc, char *argv[]);
 
 /*--- Internal UA client handling (outbound registrations) */
 static int ast_sip_ouraddrfor(struct in_addr *them, struct in_addr *us);
@@ -10074,7 +10071,7 @@
 }
 
 /*! \brief Remove temporary realtime objects from memory (CLI) */
-static int sip_prune_realtime(int fd, int argc, char *argv[])
+static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	struct sip_peer *peer;
 	struct sip_user *user;
@@ -10084,65 +10081,75 @@
 	char *name = NULL;
 	regex_t regexbuf;
 
-	switch (argc) {
+	if (cmd == CLI_INIT) {
+		e->command = "sip prune realtime [peer|user|all] [all|like]";
+		e->usage =
+		"Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
+		"       Prunes object(s) from the cache.\n"
+		"       Optional regular expression pattern is used to filter the objects.\n";
+		return NULL;
+	} else if (cmd == CLI_GENERATE) {
+		if (a->pos == 4) {
+			if (strcasestr(a->line, "realtime peer"))
+				return complete_sip_peer(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
+			else if (strcasestr(a->line, "realtime user"))
+				return complete_sip_user(a->word, a->n, SIP_PAGE2_RTCACHEFRIENDS);
+		}
+		return NULL;
+	}
+	switch (a->argc) {
 	case 4:
-		if (!strcasecmp(argv[3], "user"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "peer"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "like"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "all")) {
+		name = a->argv[3];
+		/* we accept a name in position 3, but keywords are not good. */
+		if (!strcasecmp(name, "user") || !strcasecmp(name, "peer") ||
+		    !strcasecmp(name, "like"))
+			return CLI_SHOWUSAGE;
+		pruneuser = prunepeer = TRUE;
+		if (!strcasecmp(name, "all")) {
 			multi = TRUE;
-			pruneuser = prunepeer = TRUE;
-		} else {
-			pruneuser = prunepeer = TRUE;
-			name = argv[3];
-		}
+			name = NULL;
+		}
+		/* else a single name, already set */
 		break;
 	case 5:
-		if (!strcasecmp(argv[4], "like"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "all"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "like")) {
+		/* sip prune realtime {user|peer|like} name */
+		name = a->argv[4];
+		if (!strcasecmp(a->argv[3], "user"))
+			pruneuser = TRUE;
+		else if (!strcasecmp(a->argv[3], "peer"))
+			prunepeer = TRUE;
+		else if (!strcasecmp(a->argv[3], "like")) {
+			pruneuser = prunepeer = TRUE;
 			multi = TRUE;
-			name = argv[4];
-			pruneuser = prunepeer = TRUE;
-		} else if (!strcasecmp(argv[3], "user")) {
-			pruneuser = TRUE;
-			if (!strcasecmp(argv[4], "all"))
-				multi = TRUE;
-			else
-				name = argv[4];
-		} else if (!strcasecmp(argv[3], "peer")) {
-			prunepeer = TRUE;
-			if (!strcasecmp(argv[4], "all"))
-				multi = TRUE;
-			else
-				name = argv[4];
 		} else
-			return RESULT_SHOWUSAGE;
+			return CLI_SHOWUSAGE;
+		if (!strcasecmp(a->argv[4], "like"))
+			return CLI_SHOWUSAGE;
+		if (!multi && !strcasecmp(a->argv[4], "all")) {
+			multi = TRUE;
+			name = NULL;
+		}
 		break;
 	case 6:
-		if (strcasecmp(argv[4], "like"))
-			return RESULT_SHOWUSAGE;
-		if (!strcasecmp(argv[3], "user")) {
+		name = a->argv[5];
+		multi = TRUE;
+		/* sip prune realtime {user|peer} like name */
+		if (strcasecmp(a->argv[4], "like"))
+			return CLI_SHOWUSAGE;
+		if (!strcasecmp(a->argv[3], "user")) {
 			pruneuser = TRUE;
-			name = argv[5];
-		} else if (!strcasecmp(argv[3], "peer")) {
+		} else if (!strcasecmp(a->argv[3], "peer")) {
 			prunepeer = TRUE;
-			name = argv[5];
 		} else
-			return RESULT_SHOWUSAGE;
+			return CLI_SHOWUSAGE;
 		break;
 	default:
-		return RESULT_SHOWUSAGE;
+		return CLI_SHOWUSAGE;
 	}
 
 	if (multi && name) {
 		if (regcomp(&regexbuf, name, REG_EXTENDED | REG_NOSUB))
-			return RESULT_SHOWUSAGE;
+			return CLI_SHOWUSAGE;
 	}
 
 	if (multi) {
@@ -10164,9 +10171,9 @@
 			} while (0) );
 			if (pruned) {
 				ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, sip_destroy_peer);
-				ast_cli(fd, "%d peers pruned.\n", pruned);
+				ast_cli(a->fd, "%d peers pruned.\n", pruned);
 			} else
-				ast_cli(fd, "No peers found to prune.\n");
+				ast_cli(a->fd, "No peers found to prune.\n");
 			ASTOBJ_CONTAINER_UNLOCK(&peerl);
 		}
 		if (pruneuser) {
@@ -10187,37 +10194,37 @@
 			} while (0) );
 			if (pruned) {
 				ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, sip_destroy_user);
-				ast_cli(fd, "%d users pruned.\n", pruned);
+				ast_cli(a->fd, "%d users pruned.\n", pruned);
 			} else
-				ast_cli(fd, "No users found to prune.\n");
+				ast_cli(a->fd, "No users found to prune.\n");
 			ASTOBJ_CONTAINER_UNLOCK(&userl);
 		}
 	} else {
 		if (prunepeer) {
 			if ((peer = ASTOBJ_CONTAINER_FIND_UNLINK(&peerl, name))) {
 				if (!ast_test_flag(&peer->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
-					ast_cli(fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
+					ast_cli(a->fd, "Peer '%s' is not a Realtime peer, cannot be pruned.\n", name);
 					ASTOBJ_CONTAINER_LINK(&peerl, peer);
 				} else
-					ast_cli(fd, "Peer '%s' pruned.\n", name);
+					ast_cli(a->fd, "Peer '%s' pruned.\n", name);
 				unref_peer(peer);
 			} else
-				ast_cli(fd, "Peer '%s' not found.\n", name);
+				ast_cli(a->fd, "Peer '%s' not found.\n", name);
 		}
 		if (pruneuser) {
 			if ((user = ASTOBJ_CONTAINER_FIND_UNLINK(&userl, name))) {
 				if (!ast_test_flag(&user->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
-					ast_cli(fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
+					ast_cli(a->fd, "User '%s' is not a Realtime user, cannot be pruned.\n", name);
 					ASTOBJ_CONTAINER_LINK(&userl, user);
 				} else
-					ast_cli(fd, "User '%s' pruned.\n", name);
+					ast_cli(a->fd, "User '%s' pruned.\n", name);
 				unref_user(user);
 			} else
-				ast_cli(fd, "User '%s' not found.\n", name);
-		}
-	}
-
-	return RESULT_SUCCESS;
+				ast_cli(a->fd, "User '%s' not found.\n", name);
+		}
+	}
+
+	return CLI_SUCCESS;
 }
 
 /*! \brief Print codec list from preference to CLI/manager */
@@ -10944,23 +10951,6 @@
 	return NULL;
 }
 
-/*! \brief Support routine for 'sip prune realtime peer' CLI */
-static char *complete_sip_prune_realtime_peer(const char *line, const char *word, int pos, int state)
-{
-	if (pos == 4)
-		return complete_sip_peer(word, state, SIP_PAGE2_RTCACHEFRIENDS);
-	return NULL;
-}
-
-/*! \brief Support routine for 'sip prune realtime user' CLI */
-static char *complete_sip_prune_realtime_user(const char *line, const char *word, int pos, int state)
-{
-	if (pos == 4)
-		return complete_sip_user(word, state, SIP_PAGE2_RTCACHEFRIENDS);
-
-	return NULL;
-}
-
 /*!
  * When running functions on the list of pvt's (dialogs), it is convenient
  * to have the arguments all in one structure. This not only makes it easy
@@ -11653,10 +11643,6 @@
 "       Shows all details on one SIP peer and the current status.\n"
 "       Option \"load\" forces lookup of peer in realtime storage.\n";
 
-static char prune_realtime_usage[] =
-"Usage: sip prune realtime [peer|user] [<name>|all|like <pattern>]\n"
-"       Prunes object(s) from the cache.\n"
-"       Optional regular expression pattern is used to filter the objects.\n";
 
 static char show_reg_usage[] =
 "Usage: sip show registry\n"
@@ -17512,18 +17498,7 @@
 	sip_show_user, "Show details on specific SIP user",
 	show_user_usage, complete_sip_show_user },
 
-	{ { "sip", "prune", "realtime", NULL },
-	sip_prune_realtime, "Prune cached Realtime object(s)",
-	prune_realtime_usage },
-
-	{ { "sip", "prune", "realtime", "peer", NULL },
-	sip_prune_realtime, "Prune cached Realtime peer(s)",
-	prune_realtime_usage, complete_sip_prune_realtime_peer },
-
-	{ { "sip", "prune", "realtime", "user", NULL },
-	sip_prune_realtime, "Prune cached Realtime user(s)",
-	prune_realtime_usage, complete_sip_prune_realtime_user },
-
+	NEW_CLI(sip_prune_realtime, "Prune cached Realtime users/peers"),
 	NEW_CLI(sip_do_debug, "Enable/Disable SIP debugging"),
 
 	{ { "sip", "history", NULL },



More information about the svn-commits mailing list