diff options
author | Martin Willi <martin@strongswan.org> | 2007-07-24 14:22:56 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2007-07-24 14:22:56 +0000 |
commit | 5c7da9d402a71992a5dc47c5ab25fd6f1479f5a7 (patch) | |
tree | ab6389a4a12386f6e2d5dc84a33849833a2a9ba6 /src/dumm/main.c | |
parent | c019260e01af6f184c6396774e038cb35b5f53fc (diff) | |
download | strongswan-5c7da9d402a71992a5dc47c5ab25fd6f1479f5a7.tar.bz2 strongswan-5c7da9d402a71992a5dc47c5ab25fd6f1479f5a7.tar.xz |
checked in first draft of "Dynamic Uml Mesh Modeler"
Diffstat (limited to 'src/dumm/main.c')
-rw-r--r-- | src/dumm/main.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/dumm/main.c b/src/dumm/main.c new file mode 100644 index 000000000..26896c2df --- /dev/null +++ b/src/dumm/main.c @@ -0,0 +1,228 @@ +#define _GNU_SOURCE + +#include <stdio.h> +#include <unistd.h> +#include <getopt.h> +#include <library.h> +#include <readline/readline.h> +#include <readline/history.h> + +#include "dumm.h" + +/** + * show usage information (program arguments) + */ +static void usage() +{ + printf("Usage:\n"); + printf(" --dir|-d <path> set working dir to <path>\n"); + printf(" --help|-h show this help\n"); +} + +/** + * show usage information (commandline arguments) + */ +static void help() +{ + printf("start name=<name> [options] start a guest named <name>\n"); + printf(" additional options:\n"); + printf(" kernel=<uml-kernel>\n"); + printf(" master=<read-only root files>\n"); + printf(" memory=<guest memory in MB>\n"); + printf("guests list running guests\n"); + printf("help show this help\n"); + printf("quit kill quests and exit\n"); +} + +/** + * start an UML guest + */ +static void start(umli_t *umli, char *line) +{ + enum { + NAME = 0, + MASTER, + KERNEL, + MEMORY, + }; + char *const opts[] = { + [NAME] = "name", + [MASTER] = "master", + [KERNEL] = "kernel", + [MEMORY] = "memory", + NULL + }; + char *value; + char *name = NULL; + char *kernel = NULL; + char *master = NULL; + int mem = 0; + + while (TRUE) + { + switch (getsubopt(&line, opts, &value)) + { + case NAME: + name = value; + continue; + case MASTER: + master = value; + continue; + case KERNEL: + kernel = value; + continue; + case MEMORY: + if (value) + { + mem = atoi(value); + } + continue; + default: + break; + } + break; + } + if (name == NULL) + { + printf("option 'name' is required.\n"); + help(); + return; + } + if (kernel == NULL) + { + kernel = "./linux"; + } + if (master == NULL) + { + master = "master"; + } + if (mem == 0) + { + mem = 128; + } + + if (umli->start_guest(umli, name, kernel, master, mem)) + { + printf("starting guest '%s'\n", name); + } + else + { + printf("starting guest '%s' failed\n", name); + } +} + +/** + * list running UML guests + */ +static void guests(umli_t *umli) +{ + iterator_t *iterator; + guest_t *guest; + + iterator = umli->create_guest_iterator(umli); + while (iterator->iterate(iterator, (void**)&guest)) + { + printf("%s\n", guest->get_name(guest)); + } + iterator->destroy(iterator); +} + +/** + * main routine, parses args and reads from console + */ +int main(int argc, char *argv[]) +{ + umli_t *umli; + char *line = NULL; + + while (TRUE) + { + struct option options[] = { + {"dir", 1, 0, 0}, + {"help", 0, 0, 0}, + {0, 0, 0, 0} + }; + + switch (getopt_long(argc, argv, "d:h", options, NULL)) + { + case -1: + break; + case 'd': + if (chdir(optarg)) + { + printf("changing to directory '%s' failed.\n", optarg); + return 1; + } + continue; + case 'h': + usage(); + return 0; + default: + usage(); + return 1; + } + break; + } + + umli = umli_create(); + + while (TRUE) + { + enum { + QUIT = 0, + HELP, + START, + GUESTS, + }; + char *const opts[] = { + [QUIT] = "quit", + [HELP] = "help", + [START] = "start", + [GUESTS] = "guests", + NULL + }; + char *pos, *value; + + free(line); + line = readline("dumm# "); + if (line == NULL || *line == '\0') + { + continue; + } + + add_history(line); + pos = line; + while (*pos != '\0') + { + if (*pos == ' ') + { + *pos = ','; + } + pos++; + } + pos = line; + switch (getsubopt(&pos, opts, &value)) + { + case QUIT: + free(line); + break; + case HELP: + help(); + continue; + case START: + start(umli, pos); + continue; + case GUESTS: + guests(umli); + continue; + default: + printf("command unknown: '%s'\n", line); + continue; + } + break; + } + umli->destroy(umli); + clear_history(); + return 0; +} + |