Browse Source

Merge of Fredrik Hultin command_line

tags/v0.9.3
Marty Connor 18 years ago
parent
commit
41af7457a8

+ 3
- 0
src/Makefile View File

@@ -91,6 +91,7 @@ CFLAGS		+= -I include -I arch/$(ARCH)/include -I . -DARCH=$(ARCH)
91 91
 CFLAGS		+= -Os -ffreestanding
92 92
 CFLAGS		+= -Wall -W
93 93
 CFLAGS		+= -g
94
+CFLAGS		+= -DCONSOLE_DUAL -DCOMCONSOLE=0x3F8 -DCONSPEED=9600
94 95
 CFLAGS		+= $(EXTRA_CFLAGS)
95 96
 ASFLAGS		+= $(EXTRA_ASFLAGS)
96 97
 LDFLAGS		+= $(EXTRA_LDFLAGS)
@@ -145,6 +146,8 @@ SRCDIRS		+= interface/pxe
145 146
 SRCDIRS		+= tests
146 147
 SRCDIRS		+= crypto
147 148
 SRCDIRS		+= hci/mucurses
149
+SRCDIRS		+= commandline
150
+SRCDIRS		+= commandline/commands
148 151
 
149 152
 # NON_AUTO_SRCS lists files that are excluded from the normal
150 153
 # automatic build system.

+ 81
- 0
src/commandline/cmdline.c View File

@@ -0,0 +1,81 @@
1
+#include <console.h>
2
+#include "etherboot.h"
3
+#include "cmdline.h"
4
+#include "cmdlinelib.h"
5
+#include "cmdlist.h"
6
+
7
+
8
+#define CMDL_DELAY (2000 * TICKS_PER_SEC) / 1000;
9
+
10
+void cmdl_exec_cmdline();
11
+char cmdl_spin();
12
+
13
+void cmdl_start()
14
+{
15
+	unsigned int stop;
16
+	//int spin;
17
+
18
+	//printf("gPXE %s (GPL) etherboot.org ...  ", VERSION);
19
+	printf("gPXE %s (GPL) etherboot.org\n", VERSION);
20
+
21
+	stop = currticks() + CMDL_DELAY;
22
+	
23
+	while(currticks() < stop){
24
+	
25
+		/*if(spin++ % 250 == 0){
26
+			putchar(8);
27
+			putchar(cmdl_spin());
28
+		}*/
29
+		
30
+		if(iskey()){
31
+			if(getchar() == 2){
32
+				putchar('\n');
33
+				cmdl_exec_cmdline();
34
+				break;
35
+			}else{
36
+				putchar('\n');
37
+				printf("Skipping command line.\n");
38
+				break;
39
+			}
40
+		}
41
+	}
42
+	putchar('\n');
43
+
44
+	// empty the input buffer
45
+	while(iskey()) {
46
+		getchar();
47
+	}
48
+}
49
+
50
+/*char cmdl_spin()
51
+{
52
+	static int state;*/
53
+	//int spinner[4] = {'-', '\\', '|', '/'}; <- undefined reference to memcpy!
54
+/*	int spinner[4];
55
+	
56
+	spinner[0] = '-';
57
+	spinner[1] = '\\';
58
+	spinner[2] = '|';
59
+	spinner[3] = '/';
60
+
61
+	return spinner[state++ % 4];
62
+}*/
63
+
64
+void cmdl_exec_cmdline(){
65
+	cmd_line* cmd;
66
+	cmd = cmdl_create();
67
+	
68
+	cmdl_setputchar(cmd, putchar);
69
+	cmdl_setgetchar(cmd, getchar);
70
+	cmdl_setprintf(cmd, printf);
71
+
72
+	cmdl_setpropmt(cmd, "gPXE>");
73
+
74
+	printf("Welcome to Etherboot\n\n");
75
+
76
+
77
+	cmdl_enterloop(cmd);
78
+
79
+	cmdl_free(cmd);
80
+}
81
+

+ 564
- 0
src/commandline/cmdlinelib.c View File

@@ -0,0 +1,564 @@
1
+#include "cmdlinelib.h"
2
+#include "command.h"
3
+#include <gpxe/tables.h>
4
+#include <console.h>
5
+#include <malloc.h>
6
+#include <string.h>
7
+#include <stdarg.h>
8
+
9
+static struct command cmd_start[0] __table_start ( commands );
10
+static struct command cmd_end[0] __table_end ( commands );
11
+
12
+void cmdl_setgetchar(cmd_line* cmd, cmdl_getchar_t in)
13
+{
14
+	cmd->getchar = in;
15
+}
16
+
17
+void cmdl_setputchar(cmd_line* cmd, cmdl_putchar_t in)
18
+{
19
+	cmd->putchar = in;
20
+}
21
+
22
+void cmdl_setprintf(cmd_line* cmd, cmdl_printf_t in)
23
+{
24
+	cmd->printf = in;
25
+}
26
+      
27
+int cmdl_getexit(cmd_line* cmd)
28
+{
29
+	if(cmdl_check(cmd) && !cmd->exit){
30
+		return 0;
31
+	}else{
32
+		return 1;
33
+	}
34
+
35
+}
36
+
37
+void cmdl_setexit(cmd_line* cmd, int exit)
38
+{
39
+	if(cmdl_check(cmd)){
40
+		cmd->exit = exit;
41
+	}
42
+}
43
+
44
+int cmdl_printf(cmd_line* cmd, const char *format, ...)
45
+{
46
+	int ret;
47
+        char string[CMDL_BUFFER_SIZE];
48
+        va_list ap;
49
+
50
+        va_start(ap, format);
51
+        ret = vsprintf(string, format, ap);
52
+        cmdl_addstr(cmd, string);
53
+        va_end(ap);
54
+        return ret;
55
+}
56
+
57
+void cmdl_addstr(cmd_line* cmd, char* str)
58
+{
59
+	unsigned int i;
60
+	for(i = 0; i < strlen(str); i++){
61
+		cmdl_addchar(cmd, str[i]);
62
+	}
63
+}
64
+
65
+/*void cmdl_addoutput_str(cmd_line* cmd, char output[CMDL_OUTPUT_SIZE])
66
+{
67
+	if(cmdl_check(cmd) && output != NULL){
68
+		if(!cmd->has_output){
69
+			cmdl_clearoutput(cmd);
70
+		}
71
+		strncat(cmd->output, output, CMDL_OUTPUT_SIZE);
72
+		cmd->has_output = 1;
73
+	}
74
+}*/
75
+
76
+/*char* cmdl_getoutput(cmd_line* cmd)
77
+{
78
+	if(cmdl_check(cmd) && cmd->has_output){
79
+		cmd->has_output = 0;
80
+		return cmd->output;
81
+	}else{
82
+		return "";
83
+	}
84
+}*/
85
+
86
+void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE])
87
+{
88
+	if(cmdl_check(cmd) && prompt != NULL){
89
+		strncat(cmd->prompt, prompt, CMDL_PROMPT_SIZE);
90
+	}
91
+}
92
+
93
+char *cmdl_getprompt(cmd_line* cmd)
94
+{
95
+	if(cmdl_check(cmd)){
96
+		return cmd->prompt;
97
+	}else{
98
+		return "";
99
+	}
100
+}
101
+
102
+char* cmdl_getbuffer(cmd_line* cmd){
103
+	if(cmdl_check(cmd)){
104
+		return cmd->buffer;
105
+	}else{
106
+		return "";
107
+	}
108
+}
109
+
110
+void cmdl_enterloop(cmd_line* cmd)
111
+{
112
+	while(!cmdl_getexit(cmd)){
113
+		if(cmd->refresh){
114
+			cmd->printf("%s %s", cmd->prompt, cmd->buffer);
115
+			cmd->refresh = 0;
116
+		}
117
+//		cmd->printf("Got %d\n", cmd->getchar());
118
+		cmdl_parsechar(cmd, cmd->getchar());
119
+	}
120
+}
121
+
122
+void cmdl_addreplace(cmd_line* cmd, char in)
123
+{
124
+	if(cmd->cursor < CMDL_BUFFER_SIZE - 2){
125
+		cmd->buffer[cmd->cursor] = in;
126
+		cmd->cursor++;
127
+		cmd->putchar((int)in);
128
+	}
129
+}
130
+
131
+void cmdl_addinsert(cmd_line* cmd, char in)
132
+{
133
+	int i;
134
+	int to;
135
+	if(cmd->cursor < CMDL_BUFFER_SIZE - 2 && cmd->cursor >= 0){
136
+		if(strlen(cmd->buffer) < CMDL_BUFFER_SIZE - 2){
137
+			to = strlen(cmd->buffer);
138
+		}else{
139
+			to = CMDL_BUFFER_SIZE - 2;
140
+		}
141
+			for(i=to; i > cmd->cursor; i--){
142
+				cmd->buffer[i] = cmd->buffer[i-1];
143
+			}
144
+			cmd->buffer[cmd->cursor] = in;
145
+
146
+			for(i=cmd->cursor; i < to; i++){
147
+				cmd->putchar(cmd->buffer[i]);
148
+			}
149
+			
150
+			for(i=cmd->cursor; i < to - 1; i++){
151
+				cmd->putchar(CMDLK_BS);
152
+			}
153
+			cmd->cursor++;
154
+			//cmdl_movecursor(cmd, CMDL_RIGHT);
155
+	}
156
+}
157
+
158
+void cmdl_addchar(cmd_line* cmd, char in){
159
+	if(cmd->insert){
160
+		cmdl_addinsert(cmd, in);
161
+	}else{
162
+		cmdl_addreplace(cmd, in);
163
+	}
164
+}
165
+
166
+void cmdl_parsechar(cmd_line* cmd, char in)
167
+{
168
+	if(cmdl_check(cmd)){
169
+		if(in >= 32){
170
+			cmdl_addchar(cmd, in);
171
+		}else{
172
+			switch(in){
173
+				case CMDLK_BS:
174
+					if(cmdl_movecursor(cmd, CMDL_LEFT)){
175
+						cmdl_del(cmd);
176
+					}
177
+					break;
178
+
179
+				case CMDLK_RETURN:
180
+					cmd->putchar('\n');
181
+					cmdl_exec(cmd);
182
+					cmd->refresh = 1;
183
+					break;
184
+
185
+				case CMDLK_BW:
186
+					cmdl_movecursor(cmd, CMDL_LEFT);
187
+					break;
188
+
189
+				case CMDLK_FW:
190
+					//cmdl_movecursor(cmd, CMDL_RIGHT);
191
+					break;
192
+				
193
+				case CMDLK_TAB:
194
+					cmdl_tabcomplete(cmd);
195
+					break;
196
+
197
+			}
198
+		}
199
+	}
200
+}
201
+
202
+void cmdl_tabcomplete(cmd_line *cmd)
203
+{
204
+	struct command *ccmd;
205
+	int count=0;
206
+	char* result[CMDL_MAX_TAB_COMPLETE_RESULT];
207
+
208
+	for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
209
+		if(!strncmp(ccmd->name, cmd->buffer, strlen(cmd->buffer))){
210
+			if(count <= CMDL_MAX_TAB_COMPLETE_RESULT){
211
+				result[count++] = (char*)(ccmd->name);
212
+			}
213
+		}
214
+	}
215
+	
216
+
217
+	if( count == 1 ){
218
+		cmdl_addstr(cmd, (char*)(result[0] + strlen(cmd->buffer)));
219
+		cmd->tabstate = 0;
220
+		cmdl_addchar(cmd, ' ');
221
+	} else if( count > 1 ) {
222
+		int i, i2, minlen=CMDL_BUFFER_SIZE, same=1;
223
+		char last;
224
+
225
+		for(i = 0; i < count; i ++) {
226
+			if(minlen > (int)strlen( result[i] ) ){
227
+				minlen = strlen(result[i]);
228
+			}
229
+		
230
+		}
231
+		if((int)strlen(cmd->buffer) < minlen){
232
+			for(i = strlen(cmd->buffer); i < minlen; i++){
233
+				last = result[0][i];
234
+				for(i2 = 1; i2 < count; i2 ++) {
235
+					if(result[i2][i] != last){
236
+						same = 0;
237
+						break;
238
+					}
239
+				}
240
+				if(same){
241
+					cmdl_addchar(cmd, last);
242
+				}
243
+				
244
+			}
245
+		}
246
+		cmd->tabstate++;
247
+	}
248
+	
249
+	if(count > 1 && cmd->tabstate > 1){
250
+		int i;
251
+		cmd->tabstate = 0;
252
+		cmd->refresh = 1;
253
+		cmd->putchar('\n');
254
+		for(i = 0; i < count; i ++){
255
+			cmd->printf("%s\t", result[i]);
256
+		}
257
+		cmd->putchar('\n');
258
+	}
259
+
260
+	
261
+
262
+}
263
+
264
+
265
+void cmdl_exec(cmd_line* cmd)
266
+{
267
+	cmdl_param_list* params;
268
+	int unknown=1;
269
+	struct command *ccmd;
270
+
271
+	params = cmdl_getparams(cmd->buffer);
272
+	
273
+	if(params == NULL){
274
+		cmdl_clearbuffer(cmd);
275
+		return;
276
+	}
277
+
278
+	if(params->argc > 0){
279
+		if(!strcmp(params->argv[0], "exit") || !strcmp(params->argv[0], "quit")){
280
+			cmdl_setexit(cmd, 1);
281
+/*		}else if(!strcmp(params->argv[0], "help")){
282
+			if(params->argc > 1){
283
+				cmdl_builtin_help(cmd, params->argv[1]);
284
+			}else{
285
+				cmdl_builtin_help(cmd, "");
286
+			}*/
287
+		}else{
288
+			for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
289
+				if(!strcmp(ccmd->name, params->argv[0])){
290
+					unknown = 0;
291
+					ccmd->exec(params->argc, params->argv);
292
+					break;
293
+				}
294
+			}
295
+			if(unknown){
296
+				cmd->printf("%s: unknown command\n", params->argv[0]);
297
+			}
298
+		}
299
+	}
300
+
301
+	free(params);	
302
+	cmdl_clearbuffer(cmd);
303
+}
304
+
305
+/*void cmdl_builtin_help(cmd_line* cmd, char* command){
306
+	struct command *ccmd;
307
+	int unknown = 1;
308
+	if(strcmp(command, "") == 0){
309
+		cmd->printf("Built in commands:\n\n\thelp\t\tCommand usage help (\"help help\" for more info)\n\texit, quit\t\tExit the command line and boot\n\nCompiled in commands:\n\n");
310
+
311
+		for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
312
+			cmd->printf ("\t%s\t\t%s\n", ccmd->name, ccmd->desc );
313
+		}
314
+	}else{
315
+		if(!strcmp(command, "help")){
316
+			cmd->printf("help - The help command\n\nUsage: help <command>\n\n\tExample:\n\t\thelp help\n");
317
+		}else if(!strcmp(command, "exit") || !strcmp(command, "quit")){
318
+			cmd->printf("exit, quit - The quit command\n\nUsage:\nquit or exit\n\n\tExample:\n\t\texit\n");
319
+		}else{
320
+			for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
321
+				if(!strcmp(ccmd->name, command)){
322
+					unknown = 0;
323
+					cmd->printf ("\t%s - %s\n\nUsage:\n%s\n", ccmd->name, ccmd->desc, ccmd->usage );
324
+					break;
325
+				}
326
+				if(unknown){
327
+					cmd->printf("\"%s\" isn't compiled in (does it exist?).\n", command);
328
+				}
329
+			}
330
+		}
331
+		
332
+	}
333
+}*/
334
+
335
+cmdl_param_list* cmdl_getparams(const char* command){
336
+	cmdl_param_list* this;
337
+	char *result = NULL;
338
+	int count=0;
339
+	char *command2;
340
+	
341
+	this = (cmdl_param_list*)malloc(sizeof(cmdl_param_list));
342
+	
343
+	if(this == NULL){
344
+		return NULL;
345
+	}
346
+
347
+	command2 = malloc(strlen(command) + 1);
348
+	
349
+	this->argc=0;
350
+
351
+	strcpy(command2, command);
352
+	result = strtok(command2, " ");
353
+	
354
+	while( result != NULL ) {
355
+		this->argc++;
356
+		result = strtok( NULL, " ");
357
+	}
358
+	
359
+	this->argv = (char**)malloc(sizeof(char*) * this->argc);
360
+	if(this->argv == NULL){
361
+		free(this);
362
+		return NULL;
363
+	}
364
+	
365
+	
366
+	strcpy(command2, command);
367
+	result = strtok(command2, " ");
368
+	
369
+	while( result != NULL && this->argc > count) {
370
+		this->argv[count] = (char*)malloc(sizeof(char) * (strlen(result) + 1));
371
+		if(this->argv[count] == NULL){
372
+			free(this);
373
+			return NULL;
374
+		}
375
+		strcpy(this->argv[count], result);
376
+		count++;
377
+		result = strtok( NULL, " ");
378
+	}   
379
+	free(command2);	
380
+	return this;
381
+}
382
+
383
+/*char* cmdl_parse_getcmd(cmd_line* cmd){
384
+	int i;
385
+	char* ret;
386
+	ret = (char*)malloc(1);
387
+	ret[0] = 0;
388
+
389
+	for(i=0; i < CMDL_BUFFER_SIZE - 1; i++){
390
+		if(cmd->buffer[i + 1] == ' ' || cmd->buffer[i + 1] == '\0'){
391
+			free(ret);
392
+			ret = (char*)malloc(i+1);
393
+			strncat(ret, cmd->buffer, i+1);
394
+			break;
395
+		}
396
+	}
397
+	return ret;
398
+}*/
399
+
400
+void cmdl_clearbuffer(cmd_line* cmd)
401
+{
402
+	if(cmdl_check(cmd)){
403
+		int i;
404
+		cmd->cursor = 0;
405
+		for(i=0; i < CMDL_BUFFER_SIZE; i++){
406
+			cmd->buffer[i] = 0;
407
+		}
408
+	}
409
+}
410
+
411
+/*void cmdl_clearoutput(cmd_line* cmd)
412
+{
413
+	if(cmdl_check(cmd)){
414
+		int i;
415
+		for(i=0; i < CMDL_OUTPUT_SIZE; i++){
416
+			cmd->output[i] = 0;
417
+		}
418
+	}
419
+}*/
420
+
421
+int cmdl_movecursor(cmd_line* cmd, int direction)
422
+{
423
+	if(cmdl_check(cmd)){
424
+		switch(direction){
425
+			case CMDL_LEFT:
426
+				if(cmd->cursor > 0){
427
+					cmd->cursor--;
428
+					cmd->putchar(CMDLK_BS);
429
+				}else{
430
+					return 0;
431
+				}
432
+				break;
433
+			case CMDL_RIGHT:
434
+				if(cmd->cursor < CMDL_BUFFER_SIZE - 2){
435
+					cmd->cursor++;
436
+					cmd->putchar(' ');
437
+				}else{
438
+					return 0;
439
+				}
440
+				break;
441
+		}
442
+	}
443
+	return 1;
444
+}
445
+
446
+void cmdl_del(cmd_line* cmd)
447
+{
448
+	if(cmdl_check(cmd) && cmd->cursor < CMDL_BUFFER_SIZE - 2 && cmd->cursor >= 0){
449
+		int i;
450
+		for(i = cmd->cursor; i < (int)strlen(cmd->buffer); i++){
451
+			cmd->buffer[i] = cmd->buffer[i + 1];
452
+			if(!cmd->buffer[i]){
453
+				cmd->putchar(' ');
454
+			}else{
455
+				cmd->putchar(cmd->buffer[i]);
456
+			}
457
+		}
458
+		for(i = cmd->cursor; i < (int)strlen(cmd->buffer) + 1; i++){
459
+			cmd->putchar(CMDLK_BS);
460
+		}
461
+	}
462
+}
463
+
464
+
465
+int cmdl_check(cmd_line* cmd)
466
+{
467
+	if(
468
+		cmd != NULL && 
469
+		cmd->buffer != NULL &&
470
+		cmd->prompt != NULL &&
471
+		cmd->cursor >= 0 && 
472
+		cmd->cursor < CMDL_BUFFER_SIZE - 1 &&
473
+		cmd->buffer[CMDL_BUFFER_SIZE - 1] == 0 &&
474
+		cmd->prompt[CMDL_PROMPT_SIZE - 1] == 0
475
+	){
476
+		return 1;
477
+	}else{
478
+		return 0;
479
+	}
480
+}
481
+
482
+cmd_line* cmdl_create()
483
+{
484
+	cmd_line* this;
485
+	int i;
486
+	
487
+	/* Initiate the command line */
488
+	
489
+	this = (cmd_line*)malloc(sizeof(cmd_line));
490
+	
491
+	if(this == NULL){
492
+		return NULL;
493
+	}
494
+	
495
+
496
+	/* Allocate output buffer */
497
+	
498
+	/*this->output = (char*)malloc(CMDL_OUTPUT_SIZE);
499
+	if(this->output == NULL){
500
+		free(this);
501
+		return NULL;
502
+	}*/
503
+	
504
+/*	for(i = 0; i < CMDL_OUTPUT_SIZE; i++){
505
+		this->output[i] = 0;
506
+	}*/
507
+
508
+	/* Allocate command line buffer */
509
+	
510
+	this->buffer = (char*)malloc(CMDL_BUFFER_SIZE);
511
+	if(this->buffer == NULL){
512
+		free(this);
513
+		return NULL;
514
+	}
515
+	
516
+	for(i = 0; i < CMDL_BUFFER_SIZE; i++){
517
+		this->buffer[i] = 0;
518
+	}
519
+	
520
+	/* Allocate prompt buffer */
521
+	
522
+	this->prompt = (char*)malloc(CMDL_PROMPT_SIZE);
523
+	if(this->prompt == NULL){
524
+		free(this);
525
+		return NULL;
526
+	}
527
+	
528
+	for(i = 0; i < CMDL_PROMPT_SIZE; i++){
529
+		this->prompt[i] = 0;
530
+	}
531
+	
532
+	/* Initiate cursor position etc.*/
533
+	
534
+	this->cursor = 0;
535
+	//this->has_output = 0;
536
+	this->exit = 0;
537
+	this->refresh = 1;
538
+	this->tabstate = 0;
539
+	this->insert = 0;
540
+
541
+	/* set callbacks to NULL */
542
+
543
+	this->getchar = NULL;
544
+	this->putchar = NULL;
545
+	this->printf = NULL;
546
+
547
+	/* List the commands */
548
+
549
+	struct command *cmd;
550
+
551
+	printf ( "Compiled in commands: ");
552
+	for ( cmd = cmd_start ; cmd < cmd_end ; cmd++ ) {
553
+		printf("%s ", cmd->name);
554
+	}
555
+	printf("\n");
556
+
557
+	return this;
558
+}
559
+
560
+void cmdl_free(cmd_line* cmd)
561
+{
562
+	free(cmd);
563
+}
564
+

+ 48
- 0
src/commandline/commands/help.c View File

@@ -0,0 +1,48 @@
1
+#include "command.h"
2
+#include "console.h"
3
+#include <string.h>
4
+#include <gpxe/tables.h>
5
+
6
+static struct command cmd_start[0] __table_start ( commands );
7
+static struct command cmd_end[0] __table_end ( commands );
8
+
9
+void help_req(){}
10
+
11
+static int cmd_help_exec ( int argc, char **argv ) {
12
+
13
+	struct command *ccmd;
14
+	int unknown = 1;
15
+	if(argc == 1){
16
+		printf("Built in commands:\n\n\texit, quit\t\tExit the command line and boot\n\nCompiled in commands:\n\n");
17
+
18
+		for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
19
+			printf ("\t%s\t\t%s\n", ccmd->name, ccmd->desc );
20
+		}
21
+	}else{
22
+		if(!strcmp(argv[1], "exit") || !strcmp(argv[1], "quit")){
23
+			printf("exit, quit - The quit command\n\nUsage:\nquit or exit\n\n\tExample:\n\t\texit\n");
24
+		}else{
25
+			for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
26
+				if(!strcmp(ccmd->name, argv[1])){
27
+					unknown = 0;
28
+					printf ("\t%s - %s\n\nUsage:\n%s\n", ccmd->name, ccmd->desc, ccmd->usage );
29
+					break;
30
+				}
31
+			}
32
+			if(unknown){
33
+				printf("\"%s\" isn't compiled in (does it exist?).\n", argv[1]);
34
+			}
35
+		}
36
+		
37
+	}
38
+	return 0;
39
+}
40
+
41
+struct command help_command __command = {
42
+	.name = "help",
43
+	.usage = "help <command>\n\n\tExample:\n\t\thelp help\n",
44
+	.desc = "The help command",
45
+	.exec = cmd_help_exec,
46
+};
47
+
48
+

+ 22
- 0
src/commandline/commands/test.c View File

@@ -0,0 +1,22 @@
1
+#include "command.h"
2
+#include "console.h"
3
+
4
+void test_req(){}
5
+
6
+static int cmd_test_exec ( int argc, char **argv ) {
7
+	int i;
8
+
9
+	printf("Hello, world!\nI got the following arguments passed to me: \n");
10
+	for(i = 0; i < argc; i++){
11
+		printf("%d: \"%s\"\n", i, argv[i]);
12
+	}
13
+	return 0;
14
+}
15
+
16
+struct command test_command __command = {
17
+	.name = "test",
18
+	.usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest",
19
+	.desc = "Does nothing",
20
+	.exec = cmd_test_exec,
21
+};
22
+

+ 22
- 0
src/commandline/commands/test2.c View File

@@ -0,0 +1,22 @@
1
+#include "command.h"
2
+#include "console.h"
3
+
4
+void test2_req(){}
5
+
6
+static int cmd_test2_exec ( int argc, char **argv ) {
7
+	int i;
8
+
9
+	printf("Hello, world!\nI got the following arguments passed to me: \n");
10
+	for(i = 0; i < argc; i++){
11
+		printf("%d: \"%s\"\n", i, argv[i]);
12
+	}
13
+	return 0;
14
+}
15
+
16
+struct command test2_command __command = {
17
+	.name = "test2",
18
+	.usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest2",
19
+	.desc = "Does nothing",
20
+	.exec = cmd_test2_exec,
21
+};
22
+

+ 31
- 0
src/core/cmdline.c View File

@@ -0,0 +1,31 @@
1
+#include "cmdline.h"
2
+#include "cmdlinelib.h"
3
+#include <console.h>
4
+
5
+void cmdl_start()
6
+{
7
+	cmd_line* cmd;
8
+	
9
+	cmd = cmdl_create();
10
+	
11
+	cmdl_setpropmt(cmd, "?>");
12
+
13
+	cmdl_printf(cmd, "Welcome to Etherboot\n\n");
14
+	
15
+	while(!cmdl_getexit(cmd)){
16
+		int i;
17
+		
18
+		printf("%s%s %s", cmdl_getoutput(cmd), cmdl_getprompt(cmd), cmdl_getbuffer(cmd));
19
+		
20
+		cmdl_addchar(cmd, getchar());
21
+		
22
+		/* TODO HACK temporary clear line */
23
+		putchar(0xd);
24
+		for(i=0; i < 79; i++){
25
+			putchar(0x20);
26
+		}
27
+		putchar(0xd);
28
+	}
29
+	cmdl_free(cmd);
30
+}
31
+

+ 272
- 0
src/core/cmdlinelib.c View File

@@ -0,0 +1,272 @@
1
+#include "cmdlinelib.h"
2
+#include <console.h>
3
+#include <malloc.h>
4
+#include <string.h>
5
+#include <stdarg.h>
6
+
7
+int cmdl_getexit(cmd_line* cmd)
8
+{
9
+	if(cmdl_check(cmd) && !cmd->exit){
10
+		return 0;
11
+	}else{
12
+		return 1;
13
+	}
14
+
15
+}
16
+
17
+void cmdl_setexit(cmd_line* cmd, int exit)
18
+{
19
+	if(cmdl_check(cmd)){
20
+		cmd->exit = exit;
21
+	}
22
+}
23
+
24
+int cmdl_printf(cmd_line* cmd, const char *format, ...)
25
+{
26
+	int ret;
27
+        char string[CMDL_OUTPUT_SIZE];
28
+        va_list ap;
29
+
30
+        va_start(ap, format);
31
+        ret = vsprintf(string, format, ap);
32
+        cmdl_addoutput_str(cmd, string);
33
+        va_end(ap);
34
+        return ret;
35
+}
36
+
37
+
38
+void cmdl_addoutput_str(cmd_line* cmd, char output[CMDL_OUTPUT_SIZE])
39
+{
40
+	if(cmdl_check(cmd) && output != NULL){
41
+		if(!cmd->has_output){
42
+			cmdl_clearoutput(cmd);
43
+		}
44
+		strncat(cmd->output, output, CMDL_OUTPUT_SIZE);
45
+		cmd->has_output = 1;
46
+	}
47
+}
48
+
49
+char* cmdl_getoutput(cmd_line* cmd)
50
+{
51
+	if(cmdl_check(cmd) && cmd->has_output){
52
+		cmd->has_output = 0;
53
+		return cmd->output;
54
+	}else{
55
+		return "";
56
+	}
57
+}
58
+
59
+void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE])
60
+{
61
+	if(cmdl_check(cmd) && prompt != NULL){
62
+		strncat(cmd->prompt, prompt, CMDL_PROMPT_SIZE);
63
+	}
64
+}
65
+
66
+char *cmdl_getprompt(cmd_line* cmd)
67
+{
68
+	if(cmdl_check(cmd)){
69
+		return cmd->prompt;
70
+	}else{
71
+		return "";
72
+	}
73
+}
74
+
75
+char* cmdl_getbuffer(cmd_line* cmd){
76
+	if(cmdl_check(cmd)){
77
+		return cmd->buffer;
78
+	}else{
79
+		return "";
80
+	}
81
+}
82
+
83
+void cmdl_addchar(cmd_line* cmd, char in)
84
+{
85
+	if(in >= 32){
86
+		if(cmdl_check(cmd)){
87
+			cmd->buffer[cmd->cursor] = in;
88
+			cmdl_movecursor(cmd, CMDL_RIGHT);
89
+		}
90
+	}else{
91
+		switch(in){
92
+			case 0x08: /* Backspace */
93
+
94
+				cmdl_delat(cmd, cmd->cursor);
95
+				cmdl_movecursor(cmd, CMDL_LEFT);
96
+				break;
97
+			case 0x0a:
98
+			case 0x0d: /* Enter */
99
+				cmdl_exec(cmd);
100
+				break;
101
+		}
102
+	}
103
+}
104
+
105
+void cmdl_exec(cmd_line* cmd)
106
+{
107
+	char* command;
108
+	cmdl_printf(cmd, "%s %s\n", cmd->prompt, cmd->buffer);
109
+
110
+	command = cmdl_parse_getcmd(cmd);
111
+	if(strlen(command) != 0){
112
+		if(strcmp(command, "exit") == 0 || strcmp(command, "quit") == 0){
113
+			cmdl_setexit(cmd, 1);
114
+		}else if(strcmp(command, "help") == 0){
115
+			cmdl_printf(cmd, "Don't panic\n");
116
+		}else{
117
+			cmdl_printf(cmd, "%s: unknown command\n", command);
118
+		}
119
+	}
120
+	free(command);
121
+	
122
+	cmdl_clearbuffer(cmd);
123
+}
124
+
125
+char* cmdl_parse_getcmd(cmd_line* cmd){
126
+	int i;
127
+	char* ret;
128
+	ret = (char*)malloc(1);
129
+	ret[0] = 0;
130
+
131
+	for(i=0; i < CMDL_BUFFER_SIZE - 1; i++){
132
+		if(cmd->buffer[i + 1] == ' ' || cmd->buffer[i + 1] == '\0'){
133
+			free(ret);
134
+			ret = (char*)malloc(i+1);
135
+			strncat(ret, cmd->buffer, i+1);
136
+			break;
137
+		}
138
+	}
139
+	return ret;
140
+}
141
+
142
+void cmdl_clearbuffer(cmd_line* cmd)
143
+{
144
+	if(cmdl_check(cmd)){
145
+		int i;
146
+		cmd->cursor = 0;
147
+		for(i=0; i < CMDL_BUFFER_SIZE; i++){
148
+			cmd->buffer[i] = 0;
149
+		}
150
+	}
151
+}
152
+
153
+void cmdl_clearoutput(cmd_line* cmd)
154
+{
155
+	if(cmdl_check(cmd)){
156
+		int i;
157
+		for(i=0; i < CMDL_OUTPUT_SIZE; i++){
158
+			cmd->output[i] = 0;
159
+		}
160
+	}
161
+}
162
+
163
+void cmdl_movecursor(cmd_line* cmd, int direction)
164
+{
165
+	if(cmdl_check(cmd)){
166
+		switch(direction){
167
+			case CMDL_LEFT:
168
+				if(cmd->cursor > 0){
169
+					cmd->cursor--;
170
+				}
171
+				break;
172
+			case CMDL_RIGHT:
173
+				if(cmd->cursor < CMDL_BUFFER_SIZE - 1){
174
+					cmd->cursor++;
175
+				}
176
+				break;
177
+		}
178
+	}
179
+}
180
+
181
+void cmdl_delat(cmd_line* cmd, int at)
182
+{
183
+	if(cmdl_check(cmd) && at < CMDL_BUFFER_SIZE - 1 && at >= 0){
184
+		int i;
185
+		for(i = at; i < CMDL_BUFFER_SIZE - 1; i++){
186
+			cmd->buffer[i] = cmd->buffer[i + 1];
187
+		}
188
+	}
189
+}
190
+
191
+
192
+int cmdl_check(cmd_line* cmd)
193
+{
194
+	if(
195
+		cmd != NULL && 
196
+		cmd->buffer != NULL &&
197
+		cmd->prompt != NULL &&
198
+		cmd->cursor >= 0 && 
199
+		cmd->cursor < CMDL_BUFFER_SIZE - 1 &&
200
+		cmd->buffer[CMDL_BUFFER_SIZE - 1] == 0 &&
201
+		cmd->prompt[CMDL_PROMPT_SIZE - 1] == 0
202
+	){
203
+		return 1;
204
+	}else{
205
+		return 0;
206
+	}
207
+}
208
+
209
+cmd_line* cmdl_create()
210
+{
211
+	cmd_line* this;
212
+	int i;
213
+	
214
+	// Initiate the command line
215
+	
216
+	this = (cmd_line*)malloc(sizeof(cmd_line));
217
+	
218
+	if(this == NULL){
219
+		return NULL;
220
+	}
221
+	
222
+
223
+	/* Allocate output buffer */
224
+	
225
+	this->output = (char*)malloc(CMDL_OUTPUT_SIZE);
226
+	if(this->output == NULL){
227
+		free(this);
228
+		return NULL;
229
+	}
230
+	
231
+	for(i = 0; i < CMDL_OUTPUT_SIZE; i++){
232
+		this->output[i] = 0;
233
+	}
234
+
235
+	/* Allocate command line buffer */
236
+	
237
+	this->buffer = (char*)malloc(CMDL_BUFFER_SIZE);
238
+	if(this->buffer == NULL){
239
+		free(this);
240
+		return NULL;
241
+	}
242
+	
243
+	for(i = 0; i < CMDL_BUFFER_SIZE; i++){
244
+		this->buffer[i] = 0;
245
+	}
246
+	
247
+	/* Allocate prompt buffer */
248
+	
249
+	this->prompt = (char*)malloc(CMDL_PROMPT_SIZE);
250
+	if(this->prompt == NULL){
251
+		free(this);
252
+		return NULL;
253
+	}
254
+	
255
+	for(i = 0; i < CMDL_PROMPT_SIZE; i++){
256
+		this->prompt[i] = 0;
257
+	}
258
+	
259
+	/* Initiate cursor position */
260
+	
261
+	this->cursor = 0;
262
+	this->has_output = 0;
263
+	this->exit = 0;
264
+	
265
+	return this;
266
+}
267
+
268
+void cmdl_free(cmd_line* cmd)
269
+{
270
+	free(cmd);
271
+}
272
+

+ 3
- 0
src/core/main.c View File

@@ -22,6 +22,7 @@ Literature dealing with the network protocols:
22 22
 #include "disk.h"
23 23
 #include "timer.h"
24 24
 #include "cpu.h"
25
+#include "cmdline.h"
25 26
 #include "console.h"
26 27
 #include <gpxe/init.h>
27 28
 #include "image.h"
@@ -155,6 +156,8 @@ int main ( void ) {
155 156
 	netdev = next_netdev ();
156 157
 	if ( netdev ) {
157 158
 		test_dhcp ( netdev );
159
+		//cmdl_start();
160
+		//test_aoeboot ( &static_single_netdev );
158 161
 	} else {
159 162
 		printf ( "No network device found\n" );
160 163
 	}

+ 8
- 0
src/include/cmdline.h View File

@@ -0,0 +1,8 @@
1
+#ifndef CMDLINE_H
2
+#define CMDLINE_H
3
+
4
+/* Command line external functions */
5
+
6
+void cmdl_start();
7
+
8
+#endif

+ 99
- 0
src/include/cmdlinelib.h View File

@@ -0,0 +1,99 @@
1
+/* Command line library */
2
+#ifndef CMDLINELIB_H
3
+#define CMDLINELIB_H
4
+
5
+#define CMDL_BUFFER_SIZE 256
6
+//#define CMDL_OUTPUT_SIZE 256
7
+#define CMDL_PROMPT_SIZE 8
8
+#define CMDL_MAX_TAB_COMPLETE_RESULT 256
9
+
10
+typedef int (*cmdl_putchar_t)(int);
11
+typedef int (*cmdl_printf_t)( const char *format, ... );
12
+typedef int (*cmdl_getchar_t)();
13
+
14
+#ifndef NULL
15
+#define NULL    ((void *)0)
16
+#endif
17
+
18
+enum{
19
+	CMDL_LEFT,
20
+	CMDL_RIGHT
21
+};
22
+
23
+enum{
24
+	CMDLK_FW=6,
25
+	CMDLK_BW=2,
26
+	CMDLK_BS=8,
27
+	CMDLK_HOME=2,
28
+	CMDLK_END=5,
29
+	CMDLK_DELTOEND=11,
30
+	CMDLK_DELARG=23,
31
+	CMDLK_ENTER=0x0d,
32
+	CMDLK_RETURN=0x0a,
33
+	CMDLK_TAB=9
34
+};
35
+
36
+typedef struct{
37
+	
38
+	// buffers
39
+
40
+	//char* output;
41
+	char* buffer;
42
+	char* prompt;
43
+
44
+	// options and values
45
+
46
+	int cursor;
47
+	//int has_output;
48
+	int exit;
49
+	int refresh;
50
+	int tabstate;
51
+	int insert;
52
+
53
+	// callbacks
54
+	
55
+	cmdl_putchar_t putchar;
56
+	cmdl_getchar_t getchar;
57
+	cmdl_printf_t printf;
58
+
59
+}cmd_line;
60
+
61
+typedef struct{
62
+	int argc;
63
+	char **argv;
64
+}cmdl_param_list;
65
+
66
+void cmdl_setputchar(cmd_line* cmd, cmdl_putchar_t in);
67
+void cmdl_setgetchar(cmd_line* cmd, cmdl_getchar_t in);
68
+void cmdl_setprintf(cmd_line* cmd, cmdl_printf_t in);
69
+
70
+//void cmdl_builtin_help(cmd_line* cmd, char* command);
71
+
72
+void cmdl_parsechar(cmd_line* cmd, char in);
73
+
74
+void cmdl_addreplace(cmd_line* cmd, char in);
75
+void cmdl_addinsert(cmd_line* cmd, char in);
76
+void cmdl_enterloop(cmd_line* cmd);
77
+void cmdl_exec(cmd_line* cmd);
78
+void cmdl_setexit(cmd_line* cmd, int exit);
79
+int cmdl_getexit(cmd_line* cmd);
80
+void cmdl_clearoutput(cmd_line* cmd);
81
+void cmdl_clearbuffer(cmd_line* cmd);
82
+int cmdl_printf(cmd_line* cmd, const char *format, ...);
83
+char* cmdl_getoutput(cmd_line* cmd);
84
+//void cmdl_addoutput_str(cmd_line* cmd, char output[CMDL_OUTPUT_SIZE]);
85
+void cmdl_addstr(cmd_line* cmd, char* str);
86
+int cmdl_movecursor(cmd_line* cmd, int direction);
87
+char* cmdl_getbuffer(cmd_line* cmd);
88
+void cmdl_addchar(cmd_line* cmd, char in);
89
+int cmdl_check(cmd_line* cmd);
90
+void cmdl_del(cmd_line* cmd);
91
+cmd_line* cmdl_create();
92
+void cmdl_free(cmd_line* cmd);
93
+char *cmdl_getprompt(cmd_line* cmd);
94
+void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE]);
95
+cmdl_param_list* cmdl_getparams(const char* command);
96
+void cmdl_tabcomplete(cmd_line *cmd);
97
+
98
+#endif
99
+

+ 16
- 0
src/include/cmdlist.h View File

@@ -0,0 +1,16 @@
1
+#ifndef COMMANDLIST_H
2
+#define COMMANDLIST_H
3
+
4
+void test_req();
5
+void test2_req();
6
+void help_req();
7
+
8
+void commandlist()
9
+{
10
+	test_req();
11
+	test2_req();
12
+	help_req();
13
+}
14
+
15
+#endif
16
+

+ 15
- 0
src/include/command.h View File

@@ -0,0 +1,15 @@
1
+#ifndef COMMAND_H
2
+#define COMMAND_H
3
+
4
+#include <gpxe/tables.h>
5
+
6
+struct command {
7
+	const char *name;	 	     				// The name of the command
8
+	const char *usage;						// Description of how to use the command
9
+	const char *desc;						// Short description of the command
10
+	int ( *exec ) ( int argc, char **argv);				// The command function to call
11
+};
12
+
13
+#define __command __table ( commands, 01 )
14
+#endif
15
+

Loading…
Cancel
Save