Browse Source

Add command logging callback

git-svn-id: https://klish.googlecode.com/svn/trunk@490 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
b061b40228
7 changed files with 49 additions and 15 deletions
  1. 14 1
      Makefile.in
  2. 1 0
      bin/clish.cpp
  3. 1 0
      clish/internal.h
  4. 1 0
      clish/module.am
  5. 11 7
      clish/shell.h
  6. 13 6
      clish/shell/shell_execute.c
  7. 8 1
      clish/shell/shell_startup.c

+ 14 - 1
Makefile.in

@@ -194,6 +194,7 @@ am__dirstamp = $(am__leading_dot)dirstamp
 am_libclish_la_OBJECTS = clish/libclish_la-callback_access.lo \
 	clish/libclish_la-callback_script.lo \
 	clish/libclish_la-callback_config.lo \
+	clish/libclish_la-callback_log.lo \
 	clish/command/libclish_la-command.lo \
 	clish/command/libclish_la-command_dump.lo \
 	clish/param/libclish_la-param.lo \
@@ -713,7 +714,7 @@ bin_konf_LDADD = \
 	liblub.la
 
 libclish_la_SOURCES = clish/callback_access.c clish/callback_script.c \
-	clish/callback_config.c clish/private.h \
+	clish/callback_config.c clish/callback_log.c clish/private.h \
 	clish/command/command.c clish/command/command_dump.c \
 	clish/command/private.h clish/param/param.c \
 	clish/param/param_dump.c clish/param/private.h \
@@ -961,6 +962,8 @@ clish/libclish_la-callback_script.lo: clish/$(am__dirstamp) \
 	clish/$(DEPDIR)/$(am__dirstamp)
 clish/libclish_la-callback_config.lo: clish/$(am__dirstamp) \
 	clish/$(DEPDIR)/$(am__dirstamp)
+clish/libclish_la-callback_log.lo: clish/$(am__dirstamp) \
+	clish/$(DEPDIR)/$(am__dirstamp)
 clish/command/$(am__dirstamp):
 	@$(MKDIR_P) clish/command
 	@: > clish/command/$(am__dirstamp)
@@ -1614,6 +1617,8 @@ mostlyclean-compile:
 	-rm -f clish/libclish_la-callback_access.lo
 	-rm -f clish/libclish_la-callback_config.$(OBJEXT)
 	-rm -f clish/libclish_la-callback_config.lo
+	-rm -f clish/libclish_la-callback_log.$(OBJEXT)
+	-rm -f clish/libclish_la-callback_log.lo
 	-rm -f clish/libclish_la-callback_script.$(OBJEXT)
 	-rm -f clish/libclish_la-callback_script.lo
 	-rm -f clish/nspace/libclish_la-nspace.$(OBJEXT)
@@ -1932,6 +1937,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@bin/$(DEPDIR)/konfd.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@clish/$(DEPDIR)/libclish_la-callback_access.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@clish/$(DEPDIR)/libclish_la-callback_config.Plo@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@clish/$(DEPDIR)/libclish_la-callback_log.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@clish/$(DEPDIR)/libclish_la-callback_script.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@clish/action/$(DEPDIR)/libclish_la-action.Plo@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@clish/action/$(DEPDIR)/libclish_la-action_dump.Plo@am__quote@
@@ -2141,6 +2147,13 @@ clish/libclish_la-callback_config.lo: clish/callback_config.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@	$(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclish_la_CFLAGS) $(CFLAGS) -c -o clish/libclish_la-callback_config.lo `test -f 'clish/callback_config.c' || echo '$(srcdir)/'`clish/callback_config.c
 
+clish/libclish_la-callback_log.lo: clish/callback_log.c
+@am__fastdepCC_TRUE@	$(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclish_la_CFLAGS) $(CFLAGS) -MT clish/libclish_la-callback_log.lo -MD -MP -MF clish/$(DEPDIR)/libclish_la-callback_log.Tpo -c -o clish/libclish_la-callback_log.lo `test -f 'clish/callback_log.c' || echo '$(srcdir)/'`clish/callback_log.c
+@am__fastdepCC_TRUE@	$(am__mv) clish/$(DEPDIR)/libclish_la-callback_log.Tpo clish/$(DEPDIR)/libclish_la-callback_log.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='clish/callback_log.c' object='clish/libclish_la-callback_log.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclish_la_CFLAGS) $(CFLAGS) -c -o clish/libclish_la-callback_log.lo `test -f 'clish/callback_log.c' || echo '$(srcdir)/'`clish/callback_log.c
+
 clish/command/libclish_la-command.lo: clish/command/command.c
 @am__fastdepCC_TRUE@	$(LIBTOOL)  --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclish_la_CFLAGS) $(CFLAGS) -MT clish/command/libclish_la-command.lo -MD -MP -MF clish/command/$(DEPDIR)/libclish_la-command.Tpo -c -o clish/command/libclish_la-command.lo `test -f 'clish/command/command.c' || echo '$(srcdir)/'`clish/command/command.c
 @am__fastdepCC_TRUE@	$(am__mv) clish/command/$(DEPDIR)/libclish_la-command.Tpo clish/command/$(DEPDIR)/libclish_la-command.Plo

+ 1 - 0
bin/clish.cpp

@@ -41,6 +41,7 @@ static clish_shell_hooks_t my_hooks = {
     clish_script_callback,
     NULL, /* don't worry about fini callback */
     clish_config_callback,
+    clish_log_callback,
     NULL  /* don't register any builtin functions */
 };
 

+ 1 - 0
clish/internal.h

@@ -17,6 +17,7 @@ extern clish_shell_access_fn_t clish_access_callback;
 extern clish_shell_script_fn_t clish_script_callback;
 extern clish_shell_script_fn_t clish_dryrun_callback;
 extern clish_shell_config_fn_t clish_config_callback;
+extern clish_shell_log_fn_t clish_log_callback;
 
 _END_C_DECL
 

+ 1 - 0
clish/module.am

@@ -5,6 +5,7 @@ libclish_la_SOURCES = \
     clish/callback_access.c \
     clish/callback_script.c \
     clish/callback_config.c \
+    clish/callback_log.c \
     clish/private.h
 
 libclish_la_CFLAGS = @LUB_CFLAGS@ @LUBHEAP_CFLAGS@ $(DEBUG_CFLAGS)

+ 11 - 7
clish/shell.h

@@ -192,6 +192,9 @@ typedef bool_t clish_shell_access_fn_t(
          * current user must be a member to grant access to a command.  
          */
 					      const char *access);
+typedef int clish_shell_log_fn_t(
+	clish_context_t *context,
+	const char *line, int retcode);
  /**
   * A hook function used as a built in command callback
   * 
@@ -233,13 +236,14 @@ typedef struct {
  * They will populate an instance of this structure and pass it into the 
  */
 typedef struct {
-	clish_shell_init_fn_t *init_fn;	       /**< Initialisation call       */
-	clish_shell_access_fn_t *access_fn;    /**< Access control call       */
-	clish_shell_cmd_line_fn_t *cmd_line_fn;/**< Command line logging call */
-	clish_shell_script_fn_t *script_fn;    /**< script evaluation call    */
-	clish_shell_fini_fn_t *fini_fn;	       /**< Finalisation call         */
-	clish_shell_config_fn_t *config_fn;    /**< Config call               */
-	const clish_shell_builtin_t *cmd_list;/**< NULL terminated list      */
+	clish_shell_init_fn_t *init_fn;         /* Initialisation call */
+	clish_shell_access_fn_t *access_fn;     /* Access control call */
+	clish_shell_cmd_line_fn_t *cmd_line_fn; /* Command line logging call */
+	clish_shell_script_fn_t *script_fn;     /* script evaluation call */
+	clish_shell_fini_fn_t *fini_fn;         /* Finalisation call */
+	clish_shell_config_fn_t *config_fn;     /* Config call */
+	clish_shell_log_fn_t *log_fn;           /* Logging call */
+	const clish_shell_builtin_t *cmd_list;  /* NULL terminated list */
 } clish_shell_hooks_t;
 /*-----------------
  * meta functions

+ 13 - 6
clish/shell/shell_execute.c

@@ -187,6 +187,7 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	sigset_t old_sigs;
 	struct sigaction old_sigint, old_sigquit;
 	clish_view_t *cur_view = clish_shell__get_view(this);
+	char *line = clish_shell__get_line(context);
 
 	assert(cmd);
 	action = clish_command__get_action(cmd);
@@ -212,7 +213,8 @@ int clish_shell_execute(clish_context_t *context, char **out)
 		if (-1 == lock_fd) {
 			fprintf(stderr, "Can't open lockfile %s.\n",
 				lock_path);
-			return -1; /* can't open file */
+			result = -1;
+			goto error; /* can't open file */
 		}
 		for (i = 0; i < CLISH_LOCK_WAIT; i++) {
 			res = flock(lock_fd, LOCK_EX | LOCK_NB);
@@ -231,7 +233,8 @@ int clish_shell_execute(clish_context_t *context, char **out)
 		}
 		if (res) {
 			fprintf(stderr, "Can't get lock.\n");
-			return -1; /* can't get the lock */
+			result = -1;
+			goto error; /* can't get the lock */
 		}
 	}
 
@@ -272,6 +275,10 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	if (!result && this->client_hooks->config_fn)
 		this->client_hooks->config_fn(context);
 
+	/* Call logging callback */
+	if (this->client_hooks->log_fn)
+		this->client_hooks->log_fn(context, line, result);
+
 	/* Unlock the lockfile */
 	if (lock_fd != -1) {
 		flock(lock_fd, LOCK_UN);
@@ -282,14 +289,14 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	if (!result) {
 		clish_view_t *view = clish_command__get_view(cmd);
 		/* Save the PWD */
-		if (view) {
-			char *line = clish_shell__get_line(context);
+		if (view)
 			clish_shell__set_pwd(this, line, view,
 				clish_command__get_viewid(cmd), context);
-			lub_string_free(line);
-		}
 	}
 
+error:
+	lub_string_free(line);
+
 	return result;
 }
 

+ 8 - 1
clish/shell/shell_startup.c

@@ -11,16 +11,23 @@ int clish_shell_startup(clish_shell_t *this)
 {
 	const char *banner;
 	clish_context_t context;
+	int res = 0;
 
 	assert(this->startup);
 	banner = clish_command__get_detail(this->startup);
 	if (banner)
 		tinyrl_printf(this->tinyrl, "%s\n", banner);
+
 	context.shell = this;
 	context.cmd = this->startup;
 	context.pargv = NULL;
+	/* Call log initialize */
+	if (this->client_hooks->log_fn)
+		this->client_hooks->log_fn(&context, NULL, 0);
+	/* Call startup script */
+	res = clish_shell_execute(&context, NULL);
 
-	return clish_shell_execute(&context, NULL);
+	return res;
 }
 
 /*----------------------------------------------------------- */