socsvn commit: r323785 - soc2017/kneitinger/libbe-head/usr.bin/be
kneitinger at FreeBSD.org
kneitinger at FreeBSD.org
Wed Jun 21 07:14:12 UTC 2017
Author: kneitinger
Date: Wed Jun 21 07:14:10 2017
New Revision: 323785
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=323785
Log:
Add parser for be activate command
Modified:
soc2017/kneitinger/libbe-head/usr.bin/be/be.c
Modified: soc2017/kneitinger/libbe-head/usr.bin/be/be.c
==============================================================================
--- soc2017/kneitinger/libbe-head/usr.bin/be/be.c Wed Jun 21 06:44:56 2017 (r323784)
+++ soc2017/kneitinger/libbe-head/usr.bin/be/be.c Wed Jun 21 07:14:10 2017 (r323785)
@@ -31,6 +31,7 @@
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
+#include <unistd.h>
static int be_activate( int argc, char *argv[]);
static int be_create( int argc, char *argv[]);
@@ -68,12 +69,12 @@
* Represents a relationship between the command name and the parser action
* that handles it.
*/
-struct cmd_map_entry {
+struct command_map_entry {
const char *command;
int (*fn)(int argc, char *argv[]);
};
-static struct cmd_map_entry command_map[] = {
+static struct command_map_entry command_map[] = {
{ "activate", be_activate },
{ "create", be_create },
{ "destroy", be_destroy },
@@ -85,11 +86,56 @@
{ "unmount", be_unmount },
};
+static int
+get_cmd_index(char *cmd, int *index)
+{
+ int map_size = sizeof(command_map) / sizeof (struct command_map_entry);
+
+ for (int i=0; i < map_size; ++i) {
+ if (strcmp(cmd, command_map[i].command) == 0) {
+ *index = i;
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
static int
be_activate( int argc, char *argv[])
{
- return(EX_USAGE);
+ int opt;
+ bool temp;
+ char *bootenv;
+
+ temp = false;
+ while((opt = getopt(argc, argv, "t")) != -1) {
+ switch(opt) {
+ case 't':
+ temp = true;
+ break;
+ default:
+ fprintf(stderr, "be activate: unknown option '-%c'\n",
+ optopt);
+ usage(false);
+ return(EX_USAGE);
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if(argc != 1) {
+ fprintf(stderr, "be activate: wrong number of arguments\n");
+ usage(false);
+ }
+
+ bootenv = argv[0];
+
+ /* activate logic goes here */
+
+ return 0;
}
static int
@@ -143,7 +189,8 @@
int
main(int argc, char *argv[])
{
- char *command;
+ char *command;
+ int command_index;
if (argc < 2) {
fprintf(stderr, "missing command\n");
@@ -162,5 +209,13 @@
if (strcmp(command, "-?") == 0 || strcmp(command, "-h") == 0)
usage(true);
- return 0;
+
+ if (get_cmd_index(command, &command_index)) {
+ fprintf(stderr, "unknown command: %s\n", command);
+ usage(false);
+ exit(EX_USAGE);
+ }
+
+
+ return command_map[command_index].fn(argc-1,argv+1);
}
More information about the svn-soc-all
mailing list