You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@celix.apache.org by ab...@apache.org on 2013/09/19 11:11:20 UTC

svn commit: r1524663 - in /incubator/celix/trunk/shell: ./ private/include/ private/src/

Author: abroekhuis
Date: Thu Sep 19 09:11:19 2013
New Revision: 1524663

URL: http://svn.apache.org/r1524663
Log:
CELIX-84: Updated commands to show usage when incorrect arguments are used, added a help command to print a listing of available commands. Also added sorting for the ps command.

Added:
    incubator/celix/trunk/shell/private/include/help_command.h
    incubator/celix/trunk/shell/private/src/help_command.c
Modified:
    incubator/celix/trunk/shell/CMakeLists.txt
    incubator/celix/trunk/shell/private/src/inspect_command.c
    incubator/celix/trunk/shell/private/src/install_command.c
    incubator/celix/trunk/shell/private/src/ps_command.c
    incubator/celix/trunk/shell/private/src/shell.c
    incubator/celix/trunk/shell/private/src/start_command.c
    incubator/celix/trunk/shell/private/src/stop_command.c
    incubator/celix/trunk/shell/private/src/uninstall_command.c
    incubator/celix/trunk/shell/private/src/update_command.c

Modified: incubator/celix/trunk/shell/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/CMakeLists.txt?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/CMakeLists.txt (original)
+++ incubator/celix/trunk/shell/CMakeLists.txt Thu Sep 19 09:11:19 2013
@@ -33,6 +33,7 @@ if (SHELL)
     	private/src/uninstall_command 
     	private/src/log_command
     	private/src/inspect_command
+    	private/src/help_command
         
         private/include/inspect_command.h
         private/include/install_command.h
@@ -43,6 +44,7 @@ if (SHELL)
         private/include/stop_command.h
         private/include/uninstall_command.h
         private/include/update_command.h
+        private/include/help_command.h
     )
 
 	include_directories("public/include")

Added: incubator/celix/trunk/shell/private/include/help_command.h
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/include/help_command.h?rev=1524663&view=auto
==============================================================================
--- incubator/celix/trunk/shell/private/include/help_command.h (added)
+++ incubator/celix/trunk/shell/private/include/help_command.h Thu Sep 19 09:11:19 2013
@@ -0,0 +1,33 @@
+/**
+ *Licensed to the Apache Software Foundation (ASF) under one
+ *or more contributor license agreements.  See the NOTICE file
+ *distributed with this work for additional information
+ *regarding copyright ownership.  The ASF licenses this file
+ *to you under the Apache License, Version 2.0 (the
+ *"License"); you may not use this file except in compliance
+ *with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *Unless required by applicable law or agreed to in writing,
+ *software distributed under the License is distributed on an
+ *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ *specific language governing permissions and limitations
+ *under the License.
+ */
+/*
+ * start_command.h
+ *
+ *  \date       Aug 20, 2010
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+
+#ifndef HELP_COMMAND_H_
+#define HELP_COMMAND_H_
+
+command_pt helpCommand_create(bundle_context_pt context);
+void helpCommand_destroy(command_pt command);
+
+#endif /* HELP_COMMAND_H_ */

Added: incubator/celix/trunk/shell/private/src/help_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/help_command.c?rev=1524663&view=auto
==============================================================================
--- incubator/celix/trunk/shell/private/src/help_command.c (added)
+++ incubator/celix/trunk/shell/private/src/help_command.c Thu Sep 19 09:11:19 2013
@@ -0,0 +1,129 @@
+/**
+ *Licensed to the Apache Software Foundation (ASF) under one
+ *or more contributor license agreements.  See the NOTICE file
+ *distributed with this work for additional information
+ *regarding copyright ownership.  The ASF licenses this file
+ *to you under the Apache License, Version 2.0 (the
+ *"License"); you may not use this file except in compliance
+ *with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *Unless required by applicable law or agreed to in writing,
+ *software distributed under the License is distributed on an
+ *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ *specific language governing permissions and limitations
+ *under the License.
+ */
+/*
+ * start_command.c
+ *
+ *  \date       Aug 20, 2010
+ *  \author    	<a href="mailto:celix-dev@incubator.apache.org">Apache Celix Project Team</a>
+ *  \copyright	Apache License, Version 2.0
+ */
+#include <stdlib.h>
+#include <string.h>
+
+#include "command_impl.h"
+#include "array_list.h"
+#include "bundle_context.h"
+#include "bundle.h"
+#include "shell.h"
+
+void helpCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *));
+
+command_pt helpCommand_create(bundle_context_pt context) {
+	command_pt command = (command_pt) malloc(sizeof(*command));
+	command->bundleContext = context;
+	command->name = "help";
+	command->shortDescription = "display available command usage and description.";
+	command->usage = "start [<command> ...]";
+	command->executeCommand = helpCommand_execute;
+	return command;
+}
+
+void helpCommand_destroy(command_pt command) {
+	free(command);
+}
+
+
+void helpCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
+	service_reference_pt shellService = NULL;
+	bundleContext_getServiceReference(command->bundleContext, (char *) SHELL_SERVICE_NAME, &shellService);
+
+	if (shellService != NULL) {
+		shell_service_pt shell = NULL;
+		bundleContext_getService(command->bundleContext, shellService, (void **) &shell);
+
+		if (shell != NULL) {
+			char delims[] = " ";
+			char * sub = NULL;
+			char outString[256];
+
+			sub = strtok(line, delims);
+			sub = strtok(NULL, delims);
+
+			if (sub == NULL) {
+				int i;
+				array_list_pt commands = shell->getCommands(shell->shell);
+				for (i = 0; i < arrayList_size(commands); i++) {
+					char *name = arrayList_get(commands, i);
+					sprintf(outString, "%s\n", name);
+					out(outString);
+				}
+				out("\nUse 'help <command-name>' for more information.\n");
+			} else {
+				bool found = false;
+				while (sub != NULL) {
+					int i;
+					array_list_pt commands = shell->getCommands(shell->shell);
+					for (i = 0; i < arrayList_size(commands); i++) {
+						char *name = arrayList_get(commands, i);
+						if (strcmp(sub, name) == 0) {
+							char *desc = shell->getCommandDescription(shell->shell, name);
+							char *usage = shell->getCommandUsage(shell->shell, name);
+
+							if (found) {
+								out("---\n");
+							}
+							found = true;
+							sprintf(outString, "Command     : %s\n", name);
+							out(outString);
+							sprintf(outString, "Usage       : %s\n", usage);
+							out(outString);
+							sprintf(outString, "Description : %s\n", desc);
+							out(outString);
+						}
+					}
+					sub = strtok(NULL, delims);
+				}
+			}
+
+		}
+	}
+
+//	char delims[] = " ";
+//	char * sub = NULL;
+//	char outString[256];
+//	sub = strtok(line, delims);
+//	sub = strtok(NULL, delims);
+//	if (sub == NULL) {
+//		err("Incorrect number of arguments.\n");
+//		sprintf(outString, "%s\n", command->usage);
+//		out(outString);
+//	} else {
+//		while (sub != NULL) {
+//			long id = atol(sub);
+//			bundle_pt bundle = NULL;
+//			bundleContext_getBundleById(command->bundleContext, id, &bundle);
+//			if (bundle != NULL) {
+//				bundle_startWithOptions(bundle, 0);
+//			} else {
+//				err("Bundle id is invalid.\n");
+//			}
+//			sub = strtok(NULL, delims);
+//		}
+//	}
+}

Modified: incubator/celix/trunk/shell/private/src/inspect_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/inspect_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/inspect_command.c (original)
+++ incubator/celix/trunk/shell/private/src/inspect_command.c Thu Sep 19 09:11:19 2013
@@ -28,7 +28,7 @@
 
 #include <apr_strings.h>
 
-#include "command_private.h"
+#include "command_impl.h"
 #include "array_list.h"
 #include "bundle_context.h"
 #include "bundle.h"
@@ -60,6 +60,7 @@ void inspectCommand_destroy(command_pt c
 
 void inspectCommand_execute(command_pt command, char * commandline, void (*out)(char *), void (*err)(char *)) {
 	celix_status_t status = CELIX_SUCCESS;
+	char outString[256];
 	char *token;
 	char *commandStr = apr_strtok(commandline, " ", &token);
 	char *type = apr_strtok(NULL, " ", &token);
@@ -91,9 +92,13 @@ void inspectCommand_execute(command_pt c
 			}
 		} else {
 			out("INSPECT: Too few arguments\n");
+			sprintf(outString, "%s\n", command->usage);
+			out(outString);
 		}
 	} else {
 		out("INSPECT: Too few arguments\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
 	}
 }
 

Modified: incubator/celix/trunk/shell/private/src/install_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/install_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/install_command.c (original)
+++ incubator/celix/trunk/shell/private/src/install_command.c Thu Sep 19 09:11:19 2013
@@ -54,38 +54,45 @@ void installCommand_execute(command_pt c
 	char delims[] = " ";
 	char * sub = NULL;
 	char info[256];
+	char outString[256];
 
 	// ignore the command
 	sub = strtok(line, delims);
 	sub = strtok(NULL, delims);
 	
-	info[0] = '\0';
-	while (sub != NULL) {
-		bundle_pt bundle = NULL;
-		installCommand_install(command, &bundle, strdup(sub), out, err);
-		if (bundle != NULL) {
-			long id;
-			bundle_archive_pt archive = NULL;
-			char bundleId[sizeof(id) + 1];
+	if (sub == NULL) {
+		err("Incorrect number of arguments.\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
+	} else {
+		info[0] = '\0';
+		while (sub != NULL) {
+			bundle_pt bundle = NULL;
+			installCommand_install(command, &bundle, strdup(sub), out, err);
+			if (bundle != NULL) {
+				long id;
+				bundle_archive_pt archive = NULL;
+				char bundleId[sizeof(id) + 1];
 
-			if (strlen(info) > 0) {
-				strcat(info, ", ");
+				if (strlen(info) > 0) {
+					strcat(info, ", ");
+				}
+				bundle_getArchive(bundle, &archive);
+				bundleArchive_getId(archive, &id);
+				sprintf(bundleId, "%ld", id);
+				strcat(info, bundleId);
 			}
-			bundle_getArchive(bundle, &archive);
-			bundleArchive_getId(archive, &id);
-			sprintf(bundleId, "%ld", id);
-			strcat(info, bundleId);
+			sub = strtok(NULL, delims);
+		}
+		if (strchr(info, ',') != NULL) {
+			out("Bundle IDs: ");
+			out(info);
+			out("\n");
+		} else if (strlen(info) > 0) {
+			out("Bundle ID: ");
+			out(info);
+			out("\n");
 		}
-		sub = strtok(NULL, delims);
-	}
-	if (strchr(info, ',') != NULL) {
-		out("Bundle IDs: ");
-		out(info);
-		out("\n");
-	} else if (strlen(info) > 0) {
-		out("Bundle ID: ");
-		out(info);
-		out("\n");
 	}
 }
 

Modified: incubator/celix/trunk/shell/private/src/ps_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/ps_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/ps_command.c (original)
+++ incubator/celix/trunk/shell/private/src/ps_command.c Thu Sep 19 09:11:19 2013
@@ -81,8 +81,39 @@ void psCommand_execute(command_pt comman
 
 		sprintf(line, "  %-5s %-12s %s\n", "ID", "State", msg);
 		out(line);
-		for (i = 0; i < arrayList_size(bundles); i++) {
-			bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i);
+
+		unsigned int size = arrayList_size(bundles);
+		bundle_pt bundlesA[size];
+		for (i = 0; i < size; i++) {
+			bundlesA[i] = arrayList_get(bundles, i);
+		}
+
+		int j;
+		for(i=0; i < size - 1; i++) {
+			for(j=i+1; j < size; j++) {
+				bundle_pt first = bundlesA[i];
+				bundle_pt second = bundlesA[j];
+
+				bundle_archive_pt farchive = NULL, sarchive = NULL;
+				long fid, sid;
+
+				bundle_getArchive(first, &farchive);
+				bundleArchive_getId(farchive, &fid);
+				bundle_getArchive(second, &sarchive);
+				bundleArchive_getId(sarchive, &sid);
+
+				if(fid > sid)
+				{
+					 // these three lines swap the elements bundles[i] and bundles[j].
+					 bundle_pt temp = bundlesA[i];
+					 bundlesA[i] = bundlesA[j];
+					 bundlesA[j] = temp;
+				}
+			}
+		}
+		for (i = 0; i < size; i++) {
+			//bundle_pt bundle = (bundle_pt) arrayList_get(bundles, i);
+			bundle_pt bundle = bundlesA[i];
 			bundle_archive_pt archive = NULL;
 			long id;
 			bundle_state_e state;

Modified: incubator/celix/trunk/shell/private/src/shell.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/shell.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/shell.c (original)
+++ incubator/celix/trunk/shell/private/src/shell.c Thu Sep 19 09:11:19 2013
@@ -41,6 +41,7 @@
 #include "update_command.h"
 #include "log_command.h"
 #include "inspect_command.h"
+#include "help_command.h"
 
 #include "utils.h"
 
@@ -81,6 +82,10 @@ struct shellServiceActivator {
     service_registration_pt inspectCommand;
 	command_pt inspectCmd;
 	command_service_pt inspectCmdSrv;
+
+	service_registration_pt helpCommand;
+	command_pt helpCmd;
+	command_service_pt helpCmdSrv;
 };
 
 static celix_status_t shell_createCommandService(apr_pool_t *pool, command_pt command, command_service_pt *commandService);
@@ -190,6 +195,7 @@ celix_status_t bundleActivator_create(bu
 	((struct shellServiceActivator *) (*userData))->updateCommand = NULL;
 	((struct shellServiceActivator *) (*userData))->logCommand = NULL;
 	((struct shellServiceActivator *) (*userData))->inspectCommand = NULL;
+	((struct shellServiceActivator *) (*userData))->helpCommand = NULL;
 	((struct shellServiceActivator *) (*userData))->registration = NULL;
 
 	//(*userData) = &(*activator);
@@ -255,6 +261,10 @@ celix_status_t bundleActivator_start(voi
             activator->inspectCmd = inspectCommand_create(context);
             shell_createCommandService(pool, activator->inspectCmd, &activator->inspectCmdSrv);
 			bundleContext_registerService(context, (char *) COMMAND_SERVICE_NAME, activator->inspectCmdSrv, NULL, &activator->inspectCommand);
+
+			activator->helpCmd = helpCommand_create(context);
+			shell_createCommandService(pool, activator->helpCmd, &activator->helpCmdSrv);
+			bundleContext_registerService(context, (char *) COMMAND_SERVICE_NAME, activator->helpCmdSrv, NULL, &activator->helpCommand);
 	    }
 	}
 
@@ -284,6 +294,7 @@ celix_status_t bundleActivator_stop(void
 	serviceRegistration_unregister(activator->updateCommand);
 	serviceRegistration_unregister(activator->logCommand);
 	serviceRegistration_unregister(activator->inspectCommand);
+	serviceRegistration_unregister(activator->helpCommand);
 	status = bundleContext_removeServiceListener(context, activator->listener);
 
 	if (status == CELIX_SUCCESS) {
@@ -295,6 +306,7 @@ celix_status_t bundleActivator_stop(void
         updateCommand_destroy(activator->updateCmd);
         logCommand_destroy(activator->logCmd);
         inspectCommand_destroy(activator->inspectCmd);
+        inspectCommand_destroy(activator->helpCmd);
 
         free(activator->shellService);
         activator->shellService = NULL;

Modified: incubator/celix/trunk/shell/private/src/start_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/start_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/start_command.c (original)
+++ incubator/celix/trunk/shell/private/src/start_command.c Thu Sep 19 09:11:19 2013
@@ -51,17 +51,24 @@ void startCommand_destroy(command_pt com
 void startCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
 	char delims[] = " ";
 	char * sub = NULL;
+	char outString[256];
 	sub = strtok(line, delims);
 	sub = strtok(NULL, delims);
-	while (sub != NULL) {
-		long id = atol(sub);
-        bundle_pt bundle = NULL;
-		bundleContext_getBundleById(command->bundleContext, id, &bundle);
-		if (bundle != NULL) {
-			bundle_startWithOptions(bundle, 0);
-		} else {
-			err("Bundle id is invalid.");
+	if (sub == NULL) {
+		err("Incorrect number of arguments.\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
+	} else {
+		while (sub != NULL) {
+			long id = atol(sub);
+			bundle_pt bundle = NULL;
+			bundleContext_getBundleById(command->bundleContext, id, &bundle);
+			if (bundle != NULL) {
+				bundle_startWithOptions(bundle, 0);
+			} else {
+				err("Bundle id is invalid.\n");
+			}
+			sub = strtok(NULL, delims);
 		}
-		sub = strtok(NULL, delims);
 	}
 }

Modified: incubator/celix/trunk/shell/private/src/stop_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/stop_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/stop_command.c (original)
+++ incubator/celix/trunk/shell/private/src/stop_command.c Thu Sep 19 09:11:19 2013
@@ -51,23 +51,32 @@ void stopCommand_destroy(command_pt comm
 void stopCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
     char delims[] = " ";
 	char * sub = NULL;
+	char outString[256];
+
 	sub = strtok(line, delims);
 	sub = strtok(NULL, delims);
-	while (sub != NULL) {
-		bool numeric;
-		utils_isNumeric(sub, &numeric);
-		if (numeric) {
-			long id = atol(sub);
-			bundle_pt bundle = NULL;
-			bundleContext_getBundleById(command->bundleContext, id, &bundle);
-			if (bundle != NULL) {
-				bundle_stopWithOptions(bundle, 0);
+
+	if (sub == NULL) {
+		err("Incorrect number of arguments.\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
+	} else {
+		while (sub != NULL) {
+			bool numeric;
+			utils_isNumeric(sub, &numeric);
+			if (numeric) {
+				long id = atol(sub);
+				bundle_pt bundle = NULL;
+				bundleContext_getBundleById(command->bundleContext, id, &bundle);
+				if (bundle != NULL) {
+					bundle_stopWithOptions(bundle, 0);
+				} else {
+					err("Bundle id is invalid.");
+				}
 			} else {
-				err("Bundle id is invalid.");
+				err("Bundle id should be a number (bundle id).\n");
 			}
-		} else {
-			err("Bundle id should be a number (bundle id).\n");
+			sub = strtok(NULL, delims);
 		}
-		sub = strtok(NULL, delims);
 	}
 }

Modified: incubator/celix/trunk/shell/private/src/uninstall_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/uninstall_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/uninstall_command.c (original)
+++ incubator/celix/trunk/shell/private/src/uninstall_command.c Thu Sep 19 09:11:19 2013
@@ -52,17 +52,26 @@ void uninstallCommand_destroy(command_pt
 void uninstallCommand_execute(command_pt command, char * line, void (*out)(char *), void (*err)(char *)) {
 	char delims[] = " ";
 	char * sub = NULL;
+	char outString[256];
+
 	sub = strtok(line, delims);
 	sub = strtok(NULL, delims);
-	while (sub != NULL) {
-		long id = atol(sub);
-		bundle_pt bundle = NULL;
-		bundleContext_getBundleById(command->bundleContext, id, &bundle);
-		if (bundle != NULL) {
-		    bundle_uninstall(bundle);
-		} else {
-			err("Bundle id is invalid.");
+
+	if (sub == NULL) {
+		err("Incorrect number of arguments.\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
+	} else {
+		while (sub != NULL) {
+			long id = atol(sub);
+			bundle_pt bundle = NULL;
+			bundleContext_getBundleById(command->bundleContext, id, &bundle);
+			if (bundle != NULL) {
+				bundle_uninstall(bundle);
+			} else {
+				err("Bundle id is invalid.");
+			}
+			sub = strtok(NULL, delims);
 		}
-		sub = strtok(NULL, delims);
 	}
 }

Modified: incubator/celix/trunk/shell/private/src/update_command.c
URL: http://svn.apache.org/viewvc/incubator/celix/trunk/shell/private/src/update_command.c?rev=1524663&r1=1524662&r2=1524663&view=diff
==============================================================================
--- incubator/celix/trunk/shell/private/src/update_command.c (original)
+++ incubator/celix/trunk/shell/private/src/update_command.c Thu Sep 19 09:11:19 2013
@@ -58,10 +58,16 @@ void updateCommand_execute(command_pt co
     bundle_pt bundle = NULL;
 	char delims[] = " ";
 	char * sub = NULL;
+	char outString[256];
+
 	sub = strtok(line, delims);
-	// Read bundle id
 	sub = strtok(NULL, delims);
-	if (sub != NULL) {
+
+	if (sub == NULL) {
+		err("Incorrect number of arguments.\n");
+		sprintf(outString, "%s\n", command->usage);
+		out(outString);
+	} else {
 		long id = atol(sub);
 		bundleContext_getBundleById(command->bundleContext, id, &bundle);
 		if (bundle != NULL) {