1 #include "cmdlinelib.h"
3 #include <gpxe/tables.h>
9 static struct command cmd_start[0] __table_start ( commands );
10 static struct command cmd_end[0] __table_end ( commands );
12 void cmdl_setgetchar(cmd_line* cmd, cmdl_getchar_t in)
17 void cmdl_setputchar(cmd_line* cmd, cmdl_putchar_t in)
22 void cmdl_setprintf(cmd_line* cmd, cmdl_printf_t in)
27 int cmdl_getexit(cmd_line* cmd)
29 if(cmdl_check(cmd) && !cmd->exit){
37 void cmdl_setexit(cmd_line* cmd, int exit)
44 int cmdl_printf(cmd_line* cmd, const char *format, ...)
47 char string[CMDL_BUFFER_SIZE];
51 ret = vsprintf(string, format, ap);
52 cmdl_addstr(cmd, string);
57 void cmdl_addstr(cmd_line* cmd, char* str)
60 for(i = 0; i < strlen(str); i++){
61 cmdl_addchar(cmd, str[i]);
65 /*void cmdl_addoutput_str(cmd_line* cmd, char output[CMDL_OUTPUT_SIZE])
67 if(cmdl_check(cmd) && output != NULL){
69 cmdl_clearoutput(cmd);
71 strncat(cmd->output, output, CMDL_OUTPUT_SIZE);
76 /*char* cmdl_getoutput(cmd_line* cmd)
78 if(cmdl_check(cmd) && cmd->has_output){
86 void cmdl_setpropmt(cmd_line* cmd, char prompt[CMDL_PROMPT_SIZE])
88 if(cmdl_check(cmd) && prompt != NULL){
89 strncat(cmd->prompt, prompt, CMDL_PROMPT_SIZE);
93 char *cmdl_getprompt(cmd_line* cmd)
102 char* cmdl_getbuffer(cmd_line* cmd){
110 void cmdl_enterloop(cmd_line* cmd)
112 while(!cmdl_getexit(cmd)){
114 cmd->printf("%s %s", cmd->prompt, cmd->buffer);
117 // cmd->printf("Got %d\n", cmd->getchar());
118 cmdl_parsechar(cmd, cmd->getchar());
122 void cmdl_addreplace(cmd_line* cmd, char in)
124 if(cmd->cursor < CMDL_BUFFER_SIZE - 2){
125 cmd->buffer[cmd->cursor] = in;
127 cmd->putchar((int)in);
131 void cmdl_addinsert(cmd_line* cmd, char in)
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);
139 to = CMDL_BUFFER_SIZE - 2;
141 for(i=to; i > cmd->cursor; i--){
142 cmd->buffer[i] = cmd->buffer[i-1];
144 cmd->buffer[cmd->cursor] = in;
146 for(i=cmd->cursor; i < to; i++){
147 cmd->putchar(cmd->buffer[i]);
150 for(i=cmd->cursor; i < to - 1; i++){
151 cmd->putchar(CMDLK_BS);
154 //cmdl_movecursor(cmd, CMDL_RIGHT);
158 void cmdl_addchar(cmd_line* cmd, char in){
160 cmdl_addinsert(cmd, in);
162 cmdl_addreplace(cmd, in);
166 void cmdl_parsechar(cmd_line* cmd, char in)
170 cmdl_addchar(cmd, in);
174 if(cmdl_movecursor(cmd, CMDL_LEFT)){
186 cmdl_movecursor(cmd, CMDL_LEFT);
190 //cmdl_movecursor(cmd, CMDL_RIGHT);
194 cmdl_tabcomplete(cmd);
202 void cmdl_tabcomplete(cmd_line *cmd)
204 struct command *ccmd;
206 char* result[CMDL_MAX_TAB_COMPLETE_RESULT];
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);
218 cmdl_addstr(cmd, (char*)(result[0] + strlen(cmd->buffer)));
220 cmdl_addchar(cmd, ' ');
221 } else if( count > 1 ) {
222 int i, i2, minlen=CMDL_BUFFER_SIZE, same=1;
225 for(i = 0; i < count; i ++) {
226 if(minlen > (int)strlen( result[i] ) ){
227 minlen = strlen(result[i]);
231 if((int)strlen(cmd->buffer) < minlen){
232 for(i = strlen(cmd->buffer); i < minlen; i++){
234 for(i2 = 1; i2 < count; i2 ++) {
235 if(result[i2][i] != last){
241 cmdl_addchar(cmd, last);
249 if(count > 1 && cmd->tabstate > 1){
254 for(i = 0; i < count; i ++){
255 cmd->printf("%s\t", result[i]);
265 void cmdl_exec(cmd_line* cmd)
267 cmdl_param_list* params;
269 struct command *ccmd;
271 params = cmdl_getparams(cmd->buffer);
274 cmdl_clearbuffer(cmd);
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]);
285 cmdl_builtin_help(cmd, "");
288 for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
289 if(!strcmp(ccmd->name, params->argv[0])){
291 ccmd->exec(params->argc, params->argv);
296 cmd->printf("%s: unknown command\n", params->argv[0]);
302 cmdl_clearbuffer(cmd);
305 /*void cmdl_builtin_help(cmd_line* cmd, char* command){
306 struct command *ccmd;
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");
311 for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
312 cmd->printf ("\t%s\t\t%s\n", ccmd->name, ccmd->desc );
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");
320 for ( ccmd = cmd_start ; ccmd < cmd_end ; ccmd++ ) {
321 if(!strcmp(ccmd->name, command)){
323 cmd->printf ("\t%s - %s\n\nUsage:\n%s\n", ccmd->name, ccmd->desc, ccmd->usage );
327 cmd->printf("\"%s\" isn't compiled in (does it exist?).\n", command);
335 cmdl_param_list* cmdl_getparams(const char* command){
336 cmdl_param_list* this;
341 this = (cmdl_param_list*)malloc(sizeof(cmdl_param_list));
347 command2 = malloc(strlen(command) + 1);
351 strcpy(command2, command);
352 result = strtok(command2, " ");
354 while( result != NULL ) {
356 result = strtok( NULL, " ");
359 this->argv = (char**)malloc(sizeof(char*) * this->argc);
360 if(this->argv == NULL){
366 strcpy(command2, command);
367 result = strtok(command2, " ");
369 while( result != NULL && this->argc > count) {
370 this->argv[count] = (char*)malloc(sizeof(char) * (strlen(result) + 1));
371 if(this->argv[count] == NULL){
375 strcpy(this->argv[count], result);
377 result = strtok( NULL, " ");
383 /*char* cmdl_parse_getcmd(cmd_line* cmd){
386 ret = (char*)malloc(1);
389 for(i=0; i < CMDL_BUFFER_SIZE - 1; i++){
390 if(cmd->buffer[i + 1] == ' ' || cmd->buffer[i + 1] == '\0'){
392 ret = (char*)malloc(i+1);
393 strncat(ret, cmd->buffer, i+1);
400 void cmdl_clearbuffer(cmd_line* cmd)
405 for(i=0; i < CMDL_BUFFER_SIZE; i++){
411 /*void cmdl_clearoutput(cmd_line* cmd)
415 for(i=0; i < CMDL_OUTPUT_SIZE; i++){
421 int cmdl_movecursor(cmd_line* cmd, int direction)
428 cmd->putchar(CMDLK_BS);
434 if(cmd->cursor < CMDL_BUFFER_SIZE - 2){
446 void cmdl_del(cmd_line* cmd)
448 if(cmdl_check(cmd) && cmd->cursor < CMDL_BUFFER_SIZE - 2 && cmd->cursor >= 0){
450 for(i = cmd->cursor; i < (int)strlen(cmd->buffer); i++){
451 cmd->buffer[i] = cmd->buffer[i + 1];
455 cmd->putchar(cmd->buffer[i]);
458 for(i = cmd->cursor; i < (int)strlen(cmd->buffer) + 1; i++){
459 cmd->putchar(CMDLK_BS);
465 int cmdl_check(cmd_line* cmd)
469 cmd->buffer != NULL &&
470 cmd->prompt != NULL &&
472 cmd->cursor < CMDL_BUFFER_SIZE - 1 &&
473 cmd->buffer[CMDL_BUFFER_SIZE - 1] == 0 &&
474 cmd->prompt[CMDL_PROMPT_SIZE - 1] == 0
482 cmd_line* cmdl_create()
487 /* Initiate the command line */
489 this = (cmd_line*)malloc(sizeof(cmd_line));
496 /* Allocate output buffer */
498 /*this->output = (char*)malloc(CMDL_OUTPUT_SIZE);
499 if(this->output == NULL){
504 /* for(i = 0; i < CMDL_OUTPUT_SIZE; i++){
508 /* Allocate command line buffer */
510 this->buffer = (char*)malloc(CMDL_BUFFER_SIZE);
511 if(this->buffer == NULL){
516 for(i = 0; i < CMDL_BUFFER_SIZE; i++){
520 /* Allocate prompt buffer */
522 this->prompt = (char*)malloc(CMDL_PROMPT_SIZE);
523 if(this->prompt == NULL){
528 for(i = 0; i < CMDL_PROMPT_SIZE; i++){
532 /* Initiate cursor position etc.*/
535 //this->has_output = 0;
541 /* set callbacks to NULL */
543 this->getchar = NULL;
544 this->putchar = NULL;
547 /* List the commands */
551 printf ( "Available commands: ");
552 for ( cmd = cmd_start ; cmd < cmd_end ; cmd++ ) {
553 printf("%s ", cmd->name);
560 void cmdl_free(cmd_line* cmd)