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

celix git commit: CELIX-415: Small improvement in signal handling and use of colors in the shell commands

Repository: celix
Updated Branches:
  refs/heads/develop 7d3792212 -> c28213989


CELIX-415: Small improvement in signal handling and use of colors in the shell commands


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/c2821398
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/c2821398
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/c2821398

Branch: refs/heads/develop
Commit: c2821398979ffdec94837f8ce7ebd514408240cb
Parents: 7d37922
Author: Pepijn Noltes <pe...@gmail.com>
Authored: Mon Nov 13 21:42:52 2017 +0100
Committer: Pepijn Noltes <pe...@gmail.com>
Committed: Mon Nov 13 21:42:52 2017 +0100

----------------------------------------------------------------------
 .../private/src/dm_shell_activator.c            |  6 ++---
 dependency_manager/readme.md                    |  5 ----
 framework/private/src/celix_launcher.c          | 15 +++++++----
 framework/public/include/bundle_context.h       |  2 ++
 shell/CMakeLists.txt                            |  2 +-
 shell/README.md                                 |  9 +++++--
 shell/private/src/activator.c                   |  1 +
 shell/private/src/lb_command.c                  | 20 +++++++++++++--
 shell/private/src/shell.c                       |  1 +
 shell/public/include/shell_constants.h          | 27 ++++++++++++++++++++
 shell_tui/private/src/shell_tui.c               | 16 +++++++-----
 11 files changed, 78 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/dependency_manager/private/src/dm_shell_activator.c
----------------------------------------------------------------------
diff --git a/dependency_manager/private/src/dm_shell_activator.c b/dependency_manager/private/src/dm_shell_activator.c
index 833051b..4d6f507 100644
--- a/dependency_manager/private/src/dm_shell_activator.c
+++ b/dependency_manager/private/src/dm_shell_activator.c
@@ -26,12 +26,10 @@
 
 #include <constants.h>
 #include "bundle_context.h"
-#include "service_registration.h"
 #include "command.h"
 
 #include "dm_shell_list_command.h"
-
-#define DM_SHELL_USE_ANSI_COLORS "DM_SHELL_USE_ANSI_COLORS"
+#include "shell_constants.h"
 
 struct bundle_instance {
     service_registration_pt reg;
@@ -55,7 +53,7 @@ celix_status_t bundleActivator_create(bundle_context_pt context, void **userData
 
     bi->dmHandle.context = context;
     const char* config = NULL;
-    bundleContext_getPropertyWithDefault(context, DM_SHELL_USE_ANSI_COLORS, "true", &config);
+    bundleContext_getPropertyWithDefault(context, SHELL_USE_ANSI_COLORS, SHELL_USE_ANSI_COLORS_DEFAULT_VALUE, &config);
     bi->dmHandle.useColors = config != NULL && strncmp("true", config, 5) == 0;
 
     (*userData) = bi;

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/dependency_manager/readme.md
----------------------------------------------------------------------
diff --git a/dependency_manager/readme.md b/dependency_manager/readme.md
index 864fc3a..8b9ce71 100644
--- a/dependency_manager/readme.md
+++ b/dependency_manager/readme.md
@@ -121,11 +121,6 @@ There is support for retrieving information of the dm components with
 use of the `dm` command. This command will print all known dm component,
 their state, provided interfaces and required interfaces.
 
-#### Dependency Manager Shell Config Options
-
-- DM_SHELL_USE_ANSI_COLORS - Wether to use ANSI colors when printing dm
-info. default is true.
-
 ### References
 
 For more information examples please see

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/framework/private/src/celix_launcher.c
----------------------------------------------------------------------
diff --git a/framework/private/src/celix_launcher.c b/framework/private/src/celix_launcher.c
index e853f53..ba83f25 100644
--- a/framework/private/src/celix_launcher.c
+++ b/framework/private/src/celix_launcher.c
@@ -73,11 +73,16 @@ int celixLauncher_launchWithArgs(int argc, char *argv[]) {
 		config_file = DEFAULT_CONFIG_FILE;
 	}
 
-
-	// Set signal handler
-	(void) signal(SIGINT, shutdown_framework);
-	(void) signal(SIGUSR1, ignore);
-	(void) signal(SIGUSR2, ignore);
+	struct sigaction sigact;
+	memset(&sigact, 0, sizeof(sigact));
+	sigact.sa_handler = shutdown_framework;
+	sigaction(SIGINT,  &sigact, NULL);
+	sigaction(SIGTERM, &sigact, NULL);
+
+	memset(&sigact, 0, sizeof(sigact));
+	sigact.sa_handler = ignore;
+	sigaction(SIGUSR1,  &sigact, NULL);
+	sigaction(SIGUSR2,  &sigact, NULL);
 
 	int rc = celixLauncher_launch(config_file, &framework);
 	if (rc == 0) {

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/framework/public/include/bundle_context.h
----------------------------------------------------------------------
diff --git a/framework/public/include/bundle_context.h b/framework/public/include/bundle_context.h
index cef7b44..c674e9c 100644
--- a/framework/public/include/bundle_context.h
+++ b/framework/public/include/bundle_context.h
@@ -33,6 +33,8 @@
  * Framework.
  */
 typedef struct bundleContext *bundle_context_pt;
+typedef struct bundleContext bundle_context_t;
+
 
 #include "service_factory.h"
 #include "service_listener.h"

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/shell/CMakeLists.txt b/shell/CMakeLists.txt
index 3ffb9e1..974d2ff 100644
--- a/shell/CMakeLists.txt
+++ b/shell/CMakeLists.txt
@@ -43,7 +43,7 @@ if (SHELL)
     
     install_bundle(shell
     	HEADERS
-    		public/include/shell.h public/include/command.h
+    		public/include/shell.h public/include/command.h public/include/shell_constants.h
 	)
 
 	include_directories("public/include")

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/README.md
----------------------------------------------------------------------
diff --git a/shell/README.md b/shell/README.md
index 71511c3..b830edc 100644
--- a/shell/README.md
+++ b/shell/README.md
@@ -1,4 +1,4 @@
-## Shell
+# Shell
 
 The Celix Shell provides a service interface which can be used to interact with the Celix framework. Note that it does not offer a user interface. This modular approach enables having multiple frontends, e.g. textual or graphical.
 
@@ -19,5 +19,10 @@ While the shell can be extended with additional commands by other bundles, it al
 
 Further information about a command can be retrieved by using `help` combined with the command.
 
-###### CMake options
+## Shell Config Options
+
+- SHELL_USE_ANSI_COLORS - Wether shell command are allowed to use
+ANSI colors when printing info. default is true.
+
+## CMake options
     BUILD_SHELL=ON

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/private/src/activator.c
----------------------------------------------------------------------
diff --git a/shell/private/src/activator.c b/shell/private/src/activator.c
index 3dddf07..ac298d8 100644
--- a/shell/private/src/activator.c
+++ b/shell/private/src/activator.c
@@ -24,6 +24,7 @@
  *  \copyright	Apache License, Version 2.0
  */
 #include <stdlib.h>
+#include <string.h>
 
 #include "shell_private.h"
 #include "bundle_activator.h"

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/private/src/lb_command.c
----------------------------------------------------------------------
diff --git a/shell/private/src/lb_command.c b/shell/private/src/lb_command.c
index 82812d5..5a97bf8 100644
--- a/shell/private/src/lb_command.c
+++ b/shell/private/src/lb_command.c
@@ -28,13 +28,22 @@
 
 #include "array_list.h"
 #include "bundle_context.h"
-
 #include "std_commands.h"
+#include "shell_constants.h"
+
+static const char * const EVEN_COLOR = "\033[1m"; //bold
+static const char * const ODD_COLOR = "\033[3m";  //italic
+static const char * const END_COLOR = "\033[0m";
 
 static char * psCommand_stateString(bundle_state_e state); 
 
 celix_status_t psCommand_execute(void *_ptr, char *command_line_str, FILE *out_ptr, FILE *err_ptr) {
     celix_status_t status = CELIX_SUCCESS;
+    bundle_context_t* ctx = _ptr;
+
+    const char* config = NULL;
+    bundleContext_getPropertyWithDefault(ctx, SHELL_USE_ANSI_COLORS, SHELL_USE_ANSI_COLORS_DEFAULT_VALUE, &config);
+    bool useColors = config != NULL && strncmp("true", config, 5) == 0;
 
     bundle_context_pt context_ptr = _ptr;
     array_list_pt bundles_ptr     = NULL;
@@ -149,7 +158,14 @@ celix_status_t psCommand_execute(void *_ptr, char *command_line_str, FILE *out_p
             }
 
             if (sub_status == CELIX_SUCCESS) {
-                fprintf(out_ptr, "  %-5ld %-12s %s\n", id, state_str, name_str);
+                if (useColors) {
+                    const char* start = i % 2 == 0 ? EVEN_COLOR : ODD_COLOR;
+                    const char* end = END_COLOR;
+                    fprintf(out_ptr, "%s  %-5ld %-12s %s%s\n", start, id, state_str, name_str, end);
+                } else { //no colors
+                    fprintf(out_ptr, "  %-5ld %-12s %s\n", id, state_str, name_str);
+                }
+
             }
 
             if (sub_status != CELIX_SUCCESS) {

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/private/src/shell.c
----------------------------------------------------------------------
diff --git a/shell/private/src/shell.c b/shell/private/src/shell.c
index e4fef47..ac8603e 100644
--- a/shell/private/src/shell.c
+++ b/shell/private/src/shell.c
@@ -31,6 +31,7 @@
 
 #include "shell_private.h"
 
+
 #include "utils.h"
 
 celix_status_t shell_getCommands(shell_pt shell_ptr, array_list_pt *commands_ptr);

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell/public/include/shell_constants.h
----------------------------------------------------------------------
diff --git a/shell/public/include/shell_constants.h b/shell/public/include/shell_constants.h
new file mode 100644
index 0000000..1e7f875
--- /dev/null
+++ b/shell/public/include/shell_constants.h
@@ -0,0 +1,27 @@
+/**
+ *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.
+ */
+
+
+#ifndef SHELL_CONSTANTS_H_
+#define SHELL_CONSTANTS_H_
+
+#define SHELL_USE_ANSI_COLORS "SHELL_USE_ANSI_COLORS"
+#define SHELL_USE_ANSI_COLORS_DEFAULT_VALUE "true"
+
+#endif /* SHELL_CONSTANTS_H_ */

http://git-wip-us.apache.org/repos/asf/celix/blob/c2821398/shell_tui/private/src/shell_tui.c
----------------------------------------------------------------------
diff --git a/shell_tui/private/src/shell_tui.c b/shell_tui/private/src/shell_tui.c
index f421e6e..de16a20 100644
--- a/shell_tui/private/src/shell_tui.c
+++ b/shell_tui/private/src/shell_tui.c
@@ -23,22 +23,19 @@
  *  \author    	<a href="mailto:dev@celix.apache.org">Apache Celix Project Team</a>
  *  \copyright	Apache License, Version 2.0
  */
+#include <sys/time.h>
+#include <sys/select.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <termios.h>
 #include <unistd.h>
-#include <sys/time.h>
-#include <sys/select.h>
 
 #include "bundle_context.h"
-#include "bundle_activator.h"
-#include "linked_list.h"
 #include "shell.h"
 #include "shell_tui.h"
 #include "utils.h"
 #include <signal.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include "history.h"
 
@@ -145,10 +142,11 @@ static void* shellTui_runnable(void *data) {
     memset(&ctx, 0, sizeof(ctx));
     ctx.hist = historyCreate();
 
-    //setup term
     struct termios term_org, term_new;
     if (shellTui->useAnsiControlSequences) {
         tcgetattr(STDIN_FILENO, &term_org);
+
+
         term_new = term_org;
         term_new.c_lflag &= ~(ICANON | ECHO);
         tcsetattr(STDIN_FILENO, TCSANOW, &term_new);
@@ -180,8 +178,12 @@ static void* shellTui_runnable(void *data) {
             }
         }
     }
-    tcsetattr(STDIN_FILENO,  TCSANOW, &term_org); //TODO or after SIGINT
+
     historyDestroy(ctx.hist);
+    if (shellTui->useAnsiControlSequences) {
+        tcsetattr(STDIN_FILENO, TCSANOW, &term_org);
+    }
+
 
     return NULL;
 }