Pārlūkot izejas kodu

Use C declaration in lub/list.h

git-svn-id: https://klish.googlecode.com/svn/trunk@562 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 12 gadi atpakaļ
vecāks
revīzija
e492f70f90
2 mainītis faili ar 29 papildinājumiem un 38 dzēšanām
  1. 23 7
      bin/clish.cpp
  2. 6 31
      lub/list.h

+ 23 - 7
bin/clish.cpp

@@ -23,6 +23,7 @@
 #include <langinfo.h>
 #endif
 
+#include "lub/list.h"
 #include "clish/shell.h"
 #include "clish/internal.h"
 
@@ -52,7 +53,7 @@ int main(int argc, char **argv)
 {
 	int running;
 	int result = -1;
-	clish_shell_t * shell;
+	clish_shell_t *shell = NULL;
 
 	/* Command line options */
 	const char *socket_path = KONFD_SOCKET_PATH;
@@ -69,12 +70,14 @@ int main(int argc, char **argv)
 	FILE *outfd = stdout;
 	bool_t istimeout = BOOL_FALSE;
 	int timeout = 0;
+	lub_list_t *cmds; /* Commands defined by -c */
+	lub_list_node_t *iter;
 
 	/* Signal vars */
 	struct sigaction sigpipe_act;
 	sigset_t sigpipe_set;
 
-	static const char *shortopts = "hvs:ledx:w:i:bqu8okt:";
+	static const char *shortopts = "hvs:ledx:w:i:bqu8okt:c:";
 #ifdef HAVE_GETOPT_H
 	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
@@ -93,6 +96,7 @@ int main(int argc, char **argv)
 		{"log",		0, NULL, 'o'},
 		{"check",	0, NULL, 'k'},
 		{"timeout",	1, NULL, 't'},
+		{"command",	1, NULL, 'c'},
 		{NULL,		0, NULL, 0}
 	};
 #endif
@@ -110,6 +114,9 @@ int main(int argc, char **argv)
 	setlocale(LC_ALL, "");
 #endif
 
+	/* Var initialization */
+	cmds = lub_list_new(NULL);
+
 	/* Parse command line options */
 	optind = 0;
 	while(1) {
@@ -185,7 +192,7 @@ int main(int argc, char **argv)
 	/* Validate command line options */
 	if (utf8 && bit8) {
 		fprintf(stderr, "The -u and -8 options can't be used together.\n");
-		return -1;
+		goto end;
 	}
 
 	/* Create shell instance */
@@ -194,7 +201,7 @@ int main(int argc, char **argv)
 	shell = clish_shell_new(&my_hooks, NULL, NULL, outfd, stop_on_error);
 	if (!shell) {
 		fprintf(stderr, "Cannot run clish.\n");
-		return -1;
+		goto end;
 	}
 	/* Load the XML files */
 	clish_shell_load_scheme(shell, xml_path);
@@ -248,18 +255,27 @@ int main(int argc, char **argv)
 	running = clish_shell_startup(shell);
 	if (running) {
 		fprintf(stderr, "Cannot startup clish.\n");
-		clish_shell_delete(shell);
-		return -1;
+		goto end;
 	}
 
 	/* Main loop */
 	result = clish_shell_loop(shell);
 
+end:
 	/* Cleanup */
-	clish_shell_delete(shell);
+	if (shell)
+		clish_shell_delete(shell);
 	if (quiet)
 		fclose(outfd);
 
+	/* Delete each cmds element */
+	while ((iter = lub_list__get_head(cmds))) {
+		lub_list_del(cmds, iter);
+		free(lub_list_node__get_data(iter));
+		lub_list_node_free(iter);
+	}
+	lub_list_free(cmds);
+
 	return result;
 }
 

+ 6 - 31
lub/list.h

@@ -1,6 +1,8 @@
 #ifndef _lub_list_h
 #define _lub_list_h
+
 #include <stddef.h>
+#include "lub/c_decl.h"
 
 /****************************************************************
  * TYPE DEFINITIONS
@@ -32,43 +34,15 @@ typedef struct lub_list_s lub_list_t;
  */
 typedef struct lub_list_node_s lub_list_iterator_t;
 
+_BEGIN_C_DECL
 /****************************************************************
  * LIST OPERATIONS
  **************************************************************** */
 /**
  * This operation initialises an instance of a list.
  */
-extern lub_list_t *lub_list_new(lub_list_compare_fn compareFn);
-
-/**
- * This operation is called to initialise a "clientnode" ready for
- * insertion into a tree. This is only required once after the memory
- * for a node has been allocated.
- *
- * \pre none
- *
- * \post The node is ready to be inserted into a tree.
- */
-extern lub_list_node_t *lub_list_node_new(void *data);
-
-/*****************************************
- * NODE MANIPULATION OPERATIONS
- ***************************************** */
-/**
- * This operation adds a client node to the specified tree.
- *
- * \pre The tree must be initialised
- * \pre The clientnode must be initialised
- * 
- * \return
- * 0 if the "clientnode" is added correctly to the tree.
- * If another "clientnode" already exists in the tree with the same key, then
- * -1 is returned, and the tree remains unchanged.
- *
- * \post If the bintree "node" is already part of a tree, then an
- * assert will fire.
- */
-
+lub_list_t *lub_list_new(lub_list_compare_fn compareFn);
+lub_list_node_t *lub_list_node_new(void *data);
 void lub_list_free(lub_list_t *list);
 void lub_list_node_free(lub_list_node_t *node);
 inline lub_list_node_t *lub_list__get_head(lub_list_t *list);
@@ -83,5 +57,6 @@ lub_list_node_t *lub_list_add(lub_list_t *list, void *data);
 void lub_list_del(lub_list_t *list, lub_list_node_t *node);
 inline void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src);
 
+_END_C_DECL
 #endif				/* _lub_list_h */