瀏覽代碼

The clish now undertand -h (help), -v (version) and -s (set konfd socket) options.

git-svn-id: https://klish.googlecode.com/svn/trunk@242 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 14 年之前
父節點
當前提交
877d51f349
共有 5 個文件被更改,包括 109 次插入14 次删除
  1. 95 11
      bin/clish.cpp
  2. 1 1
      clish/shell.h
  3. 1 1
      clish/shell/shell_new.c
  4. 12 0
      clish/shell/shell_pwd.c
  5. 0 1
      clish/shell/shell_spawn.c

+ 95 - 11
bin/clish.cpp

@@ -3,11 +3,22 @@
 //
 // A simple client for libclish
 //-------------------------------------
-#include "clish/private.h"
 
-static 
-clish_shell_hooks_t my_hooks = 
-{
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+
+#include "clish/shell.h"
+#include "clish/internal.h"
+
+#ifndef VERSION
+#define VERSION 1.2.2
+#endif
+#define QUOTE(t) #t
+#define version(v) printf("%s\n", QUOTE(v))
+
+static clish_shell_hooks_t my_hooks = {
     NULL, /* don't worry about init callback */
     clish_access_callback,
     NULL, /* don't worry about cmd_line callback */
@@ -17,13 +28,53 @@ clish_shell_hooks_t my_hooks =
     NULL  /* don't register any builtin functions */
 };
 
-//---------------------------------------------------------
-int main(int argc, const char **argv)
+static void help(int status, const char *argv0);
+
+/*--------------------------------------------------------- */
+int main(int argc, char **argv)
 {
 	bool_t running;
 	int result = -1;
 	clish_shell_t * shell;
 
+	/* Command line options */
+	const char *socket_path = KONFD_SOCKET_PATH;
+
+	static const char *shortopts = "hvs:";
+/*	static const struct option longopts[] = {
+		{"help",	0, NULL, 'h'},
+		{"version",	0, NULL, 'v'},
+		{"socket",	1, NULL, 's'},
+		{NULL,		0, NULL, 0}
+	};
+*/
+	/* Parse command line options */
+	optind = 0;
+	while(1) {
+		int opt;
+/*		opt = getopt_long(argc, argv, shortopts, longopts, NULL); */
+		opt = getopt(argc, argv, shortopts);
+		if (-1 == opt)
+			break;
+		switch (opt) {
+		case 's':
+			socket_path = optarg;
+			break;
+		case 'h':
+			help(0, argv[0]);
+			exit(0);
+			break;
+		case 'v':
+			version(VERSION);
+			exit(0);
+			break;
+		default:
+			help(-1, argv[0]);
+			exit(-1);
+			break;
+		}
+	}
+
 	shell = clish_shell_new(&my_hooks, NULL, stdin, stdout);
 	if (!shell) {
 		fprintf(stderr, "Cannot run clish.\n");
@@ -31,6 +82,8 @@ int main(int argc, const char **argv)
 	}
 	/* Load the XML files */
 	clish_shell_load_files(shell);
+	/* Set communication to the konfd */
+	clish_shell__set_socket(shell, socket_path);
 	/* Execute startup */
 	running = clish_shell_startup(shell);
 	if (!running) {
@@ -39,11 +92,11 @@ int main(int argc, const char **argv)
 		return -1;
 	}
 
-	if(argc > 1) {
-		int i = 1;
-		while(argc--) {
+	if(optind < argc) {
+		int i;
+		for (i = optind; i < argc; i++) {
 			/* Run the commands from the file */
-			result = clish_shell_from_file(shell, argv[i++]);
+			result = clish_shell_from_file(shell, argv[i]);
 		}
 	} else {
 		/* The interactive shell */
@@ -55,4 +108,35 @@ int main(int argc, const char **argv)
 
 	return result ? 0 : -1;
 }
-//---------------------------------------------------------
+
+/*--------------------------------------------------------- */
+/* Print help message */
+static void help(int status, const char *argv0)
+{
+	const char *name = NULL;
+
+	if (!argv0)
+		return;
+
+	/* Find the basename */
+	name = strrchr(argv0, '/');
+	if (name)
+		name++;
+	else
+		name = argv0;
+
+	if (status != 0) {
+		fprintf(stderr, "Try `%s -h' for more information.\n",
+			name);
+	} else {
+		printf("Usage: %s [options]\n", name);
+		printf("CLI utility. "
+			"The part of the klish project.\n");
+		printf("Options:\n");
+		printf("\t-v, --version\tPrint version.\n");
+		printf("\t-h, --help\tPrint this help.\n");
+		printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
+			"of the konfd daemon.\n");
+	}
+}
+

+ 1 - 1
clish/shell.h

@@ -336,7 +336,7 @@ FILE *clish_shell__get_istream(const clish_shell_t * instance);
 FILE *clish_shell__get_ostream(const clish_shell_t * instance);
 void clish_shell__set_lockfile(clish_shell_t * instance, const char * path);
 char * clish_shell__get_lockfile(clish_shell_t * instance);
-
+int clish_shell__set_socket(clish_shell_t * instance, const char * path);
 int clish_shell_spawn(clish_shell_t * instance,
 	const pthread_attr_t * attr);
 int clish_shell_wait(clish_shell_t * instance);

+ 1 - 1
clish/shell/shell_new.c

@@ -41,7 +41,7 @@ clish_shell_init(clish_shell_t * this,
 	this->current_file = NULL;
 	this->cfg_pwdv = NULL;
 	this->cfg_pwdc = 0;
-	this->client = konf_client_new(KONFD_SOCKET_PATH);
+	this->client = NULL;
 	this->lockfile = lub_string_dup(CLISH_LOCK_PATH);
 
 	/* Create internal ptypes and params */

+ 12 - 0
clish/shell/shell_pwd.c

@@ -121,3 +121,15 @@ char * clish_shell__get_lockfile(clish_shell_t * this)
 
 	return this->lockfile;
 }
+
+/*--------------------------------------------------------- */
+int clish_shell__set_socket(clish_shell_t * this, const char * path)
+{
+	if (!this || !path)
+		return -1;
+
+	konf_client_free(this->client);
+	this->client = konf_client_new(path);
+
+	return 0;
+}

+ 0 - 1
clish/shell/shell_spawn.c

@@ -271,7 +271,6 @@ bool_t clish_shell_from_file(clish_shell_t * this,
 	return _from_file(this, BOOL_FALSE, NULL, filename);
 }
 
-
 /*-------------------------------------------------------- */
 bool_t clish_shell_loop(clish_shell_t * this)
 {