Bladeren bron

cmdlinelib.c now calls system() rather than doing its own tokenisation

(which was extremely heavy on calls to malloc()).

Moved include/command.h to include/gpxe/command.h, since it's
gPXE-specific.
tags/v0.9.3
Michael Brown 17 jaren geleden
bovenliggende
commit
7de5d32ff5

+ 3
- 169
src/commandline/cmdlinelib.c Bestand weergeven

@@ -1,5 +1,5 @@
1 1
 #include "cmdlinelib.h"
2
-#include "command.h"
2
+#include <gpxe/command.h>
3 3
 #include <gpxe/tables.h>
4 4
 #include <console.h>
5 5
 #include <malloc.h>
@@ -62,27 +62,6 @@ void cmdl_addstr(cmd_line* cmd, char* str)
62 62
 	}
63 63
 }
64 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 65
 void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE])
87 66
 {
88 67
 	if(cmdl_check(cmd) && prompt != NULL){
@@ -178,7 +157,8 @@ void cmdl_parsechar(cmd_line* cmd, char in)
178 157
 
179 158
 				case CMDLK_RETURN:
180 159
 					cmd->putchar('\n');
181
-					cmdl_exec(cmd);
160
+					system ( cmd->buffer );
161
+					cmdl_clearbuffer(cmd);
182 162
 					cmd->refresh = 1;
183 163
 					break;
184 164
 
@@ -261,142 +241,6 @@ void cmdl_tabcomplete(cmd_line *cmd)
261 241
 
262 242
 }
263 243
 
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 244
 void cmdl_clearbuffer(cmd_line* cmd)
401 245
 {
402 246
 	if(cmdl_check(cmd)){
@@ -408,16 +252,6 @@ void cmdl_clearbuffer(cmd_line* cmd)
408 252
 	}
409 253
 }
410 254
 
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 255
 int cmdl_movecursor(cmd_line* cmd, int direction)
422 256
 {
423 257
 	if(cmdl_check(cmd)){

+ 9
- 26
src/commandline/commands/help.c Bestand weergeven

@@ -1,7 +1,7 @@
1
-#include "command.h"
2
-#include "console.h"
3 1
 #include <string.h>
2
+#include <vsprintf.h>
4 3
 #include <gpxe/tables.h>
4
+#include <gpxe/command.h>
5 5
 
6 6
 static struct command cmd_start[0] __table_start ( commands );
7 7
 static struct command cmd_end[0] __table_end ( commands );
@@ -12,36 +12,19 @@ static int cmd_help_exec ( int argc, char **argv ) {
12 12
 
13 13
 	struct command *ccmd;
14 14
 	int unknown = 1;
15
-	if(argc == 1){
16
-		printf("Available commands:\n\n  exit - Exit the command line and boot\n");
17
-
18
-		for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
19
-			printf ("  %s - %s\n", ccmd->name, ccmd->desc );
20
-		}
21
-	}else{
22
-		if(!strcmp(argv[1], "exit") || !strcmp(argv[1], "quit")){
23
-			printf("exit - Exit the command line and boot\n\nUsage:\n  exit\n");
24
-		}else{
25
-			for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
26
-				if(!strcmp(ccmd->name, argv[1])){
27
-					unknown = 0;
28
-					printf ("%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
-		
15
+
16
+
17
+	printf("Available commands:\n\n  exit - Exit the command line and boot\n");
18
+
19
+	for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
20
+		printf ("  %s\n", ccmd->name );
37 21
 	}
22
+
38 23
 	return 0;
39 24
 }
40 25
 
41 26
 struct command help_command __command = {
42 27
 	.name = "help",
43
-	.usage = "help <command>\n\nExample:\n  help help\n",
44
-	.desc = "The help command",
45 28
 	.exec = cmd_help_exec,
46 29
 };
47 30
 

+ 2
- 5
src/commandline/commands/nvo_cmd.c Bestand weergeven

@@ -3,10 +3,11 @@
3 3
 #include <string.h>
4 4
 #include <errno.h>
5 5
 #include <vsprintf.h>
6
-#include <command.h>
6
+#include <getopt.h>
7 7
 #include <gpxe/nvo.h>
8 8
 #include <gpxe/dhcp.h>
9 9
 #include <gpxe/settings.h>
10
+#include <gpxe/command.h>
10 11
 
11 12
 void nvo_cmd_req() {}
12 13
 
@@ -41,8 +42,6 @@ static int show_exec ( int argc, char **argv ) {
41 42
 
42 43
 struct command show_command __command = {
43 44
 	.name = "show",
44
-	.usage = "show <identifier>\n",
45
-	.desc = "Show stored options",
46 45
 	.exec = show_exec,
47 46
 };
48 47
 
@@ -78,7 +77,5 @@ static int set_exec ( int argc, char **argv ) {
78 77
 
79 78
 struct command set_command __command = {
80 79
 	.name = "set",
81
-	.usage = "set <identifier> <value>\n",
82
-	.desc = "Set stored option",
83 80
 	.exec = set_exec,
84 81
 };

+ 2
- 4
src/commandline/commands/test.c Bestand weergeven

@@ -1,5 +1,5 @@
1
-#include "command.h"
2
-#include "console.h"
1
+#include <vsprintf.h>
2
+#include <gpxe/command.h>
3 3
 
4 4
 void test_req(){}
5 5
 
@@ -15,8 +15,6 @@ static int cmd_test_exec ( int argc, char **argv ) {
15 15
 
16 16
 struct command test_command __command = {
17 17
 	.name = "test",
18
-	.usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest",
19
-	.desc = "Does nothing",
20 18
 	.exec = cmd_test_exec,
21 19
 };
22 20
 

+ 2
- 4
src/commandline/commands/test2.c Bestand weergeven

@@ -1,5 +1,5 @@
1
-#include "command.h"
2
-#include "console.h"
1
+#include <vsprintf.h>
2
+#include <gpxe/command.h>
3 3
 
4 4
 void test2_req(){}
5 5
 
@@ -15,8 +15,6 @@ static int cmd_test2_exec ( int argc, char **argv ) {
15 15
 
16 16
 struct command test2_command __command = {
17 17
 	.name = "test2",
18
-	.usage = "A test command\nIt does nothing at all\n\nExample:\n\ttest2",
19
-	.desc = "Does nothing",
20 18
 	.exec = cmd_test2_exec,
21 19
 };
22 20
 

+ 0
- 15
src/include/command.h Bestand weergeven

@@ -1,15 +0,0 @@
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
-

Laden…
Annuleren
Opslaan