checked in first draft of "Dynamic Uml Mesh Modeler"
[strongswan.git] / src / dumm / main.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <getopt.h>
6 #include <library.h>
7 #include <readline/readline.h>
8 #include <readline/history.h>
9
10 #include "dumm.h"
11
12 /**
13 * show usage information (program arguments)
14 */
15 static void usage()
16 {
17 printf("Usage:\n");
18 printf(" --dir|-d <path> set working dir to <path>\n");
19 printf(" --help|-h show this help\n");
20 }
21
22 /**
23 * show usage information (commandline arguments)
24 */
25 static void help()
26 {
27 printf("start name=<name> [options] start a guest named <name>\n");
28 printf(" additional options:\n");
29 printf(" kernel=<uml-kernel>\n");
30 printf(" master=<read-only root files>\n");
31 printf(" memory=<guest memory in MB>\n");
32 printf("guests list running guests\n");
33 printf("help show this help\n");
34 printf("quit kill quests and exit\n");
35 }
36
37 /**
38 * start an UML guest
39 */
40 static void start(umli_t *umli, char *line)
41 {
42 enum {
43 NAME = 0,
44 MASTER,
45 KERNEL,
46 MEMORY,
47 };
48 char *const opts[] = {
49 [NAME] = "name",
50 [MASTER] = "master",
51 [KERNEL] = "kernel",
52 [MEMORY] = "memory",
53 NULL
54 };
55 char *value;
56 char *name = NULL;
57 char *kernel = NULL;
58 char *master = NULL;
59 int mem = 0;
60
61 while (TRUE)
62 {
63 switch (getsubopt(&line, opts, &value))
64 {
65 case NAME:
66 name = value;
67 continue;
68 case MASTER:
69 master = value;
70 continue;
71 case KERNEL:
72 kernel = value;
73 continue;
74 case MEMORY:
75 if (value)
76 {
77 mem = atoi(value);
78 }
79 continue;
80 default:
81 break;
82 }
83 break;
84 }
85 if (name == NULL)
86 {
87 printf("option 'name' is required.\n");
88 help();
89 return;
90 }
91 if (kernel == NULL)
92 {
93 kernel = "./linux";
94 }
95 if (master == NULL)
96 {
97 master = "master";
98 }
99 if (mem == 0)
100 {
101 mem = 128;
102 }
103
104 if (umli->start_guest(umli, name, kernel, master, mem))
105 {
106 printf("starting guest '%s'\n", name);
107 }
108 else
109 {
110 printf("starting guest '%s' failed\n", name);
111 }
112 }
113
114 /**
115 * list running UML guests
116 */
117 static void guests(umli_t *umli)
118 {
119 iterator_t *iterator;
120 guest_t *guest;
121
122 iterator = umli->create_guest_iterator(umli);
123 while (iterator->iterate(iterator, (void**)&guest))
124 {
125 printf("%s\n", guest->get_name(guest));
126 }
127 iterator->destroy(iterator);
128 }
129
130 /**
131 * main routine, parses args and reads from console
132 */
133 int main(int argc, char *argv[])
134 {
135 umli_t *umli;
136 char *line = NULL;
137
138 while (TRUE)
139 {
140 struct option options[] = {
141 {"dir", 1, 0, 0},
142 {"help", 0, 0, 0},
143 {0, 0, 0, 0}
144 };
145
146 switch (getopt_long(argc, argv, "d:h", options, NULL))
147 {
148 case -1:
149 break;
150 case 'd':
151 if (chdir(optarg))
152 {
153 printf("changing to directory '%s' failed.\n", optarg);
154 return 1;
155 }
156 continue;
157 case 'h':
158 usage();
159 return 0;
160 default:
161 usage();
162 return 1;
163 }
164 break;
165 }
166
167 umli = umli_create();
168
169 while (TRUE)
170 {
171 enum {
172 QUIT = 0,
173 HELP,
174 START,
175 GUESTS,
176 };
177 char *const opts[] = {
178 [QUIT] = "quit",
179 [HELP] = "help",
180 [START] = "start",
181 [GUESTS] = "guests",
182 NULL
183 };
184 char *pos, *value;
185
186 free(line);
187 line = readline("dumm# ");
188 if (line == NULL || *line == '\0')
189 {
190 continue;
191 }
192
193 add_history(line);
194 pos = line;
195 while (*pos != '\0')
196 {
197 if (*pos == ' ')
198 {
199 *pos = ',';
200 }
201 pos++;
202 }
203 pos = line;
204 switch (getsubopt(&pos, opts, &value))
205 {
206 case QUIT:
207 free(line);
208 break;
209 case HELP:
210 help();
211 continue;
212 case START:
213 start(umli, pos);
214 continue;
215 case GUESTS:
216 guests(umli);
217 continue;
218 default:
219 printf("command unknown: '%s'\n", line);
220 continue;
221 }
222 break;
223 }
224 umli->destroy(umli);
225 clear_history();
226 return 0;
227 }
228