Command.c (4928B)
1 #include <EGong/Util/Command.h> 2 #include <EGong/Util/Log.h> 3 #include <EGong/Util/Misc.h> 4 #include <EGong/Interfaces/CMD.h> 5 #include <stdint.h> 6 #include <stdlib.h> 7 #include <stdio.h> 8 9 unsigned int EGong_command_argc(struct EGong_command *command){ 10 if(command->type.type>EGONG_COMMAND_ARG2){ 11 return 2; 12 } 13 else if(command->type.type>EGONG_COMMAND_ARG1){ 14 return 1; 15 } 16 else if(command->type.type>EGONG_COMMAND_ARG0){ 17 return 0; 18 } 19 return 0; 20 } 21 struct EGong_command *EGong_command_match(struct EGong_command_array *commands, const char *data, unsigned int match_type){ 22 unsigned int i; 23 struct EGong_command *command=NULL; 24 for(i=0; i<commands->array.element_count; i++){ 25 command=(commands->array.elements+(uintptr_t)(commands->array.element_size*i)); 26 if((match_type&EGONG_COMMAND_MATCH_SHORT) && strcmp(command->shortname, data)==0){ 27 break; 28 } 29 if((match_type&EGONG_COMMAND_MATCH_LONG) && strcmp(command->longname, data)==0){ 30 break; 31 } 32 command=NULL; 33 } 34 return command; 35 } 36 void EGong_command_print_help(struct EGong_command_array *commands, int destination, char *appendix){ 37 unsigned int i; 38 size_t appendixlength=strlen(appendix); 39 struct EGong_command *cmd; 40 for(i=0; i<commands->array.element_count; i++){ 41 cmd=(struct EGong_command *)((intptr_t)commands->array.elements+(intptr_t)(commands->array.element_size*i)); 42 write(destination, appendix, appendixlength); 43 write(destination, "-", sizeof("-")); 44 write(destination, cmd->shortname, strlen(cmd->shortname)); 45 write(destination, "/--", sizeof("/--")); 46 write(destination, cmd->longname, strlen(cmd->longname)); 47 write(destination, ": ", sizeof(": ")); 48 write(destination, cmd->description, strlen(cmd->description)); 49 write(destination,"\n", sizeof("\n")); 50 } 51 } 52 void *EGong_command_exec_from_char(struct EGong_command *command, char **args){ 53 void *argv[command->type.args]; 54 unsigned int i=0; 55 for(i=0; i<command->type.args; i++){ 56 argv[i]=EGong_command_convert_from_char(command, args[i]); 57 } 58 EGong_command_exec(command, argv); 59 } 60 void *EGong_command_convert_from_char(struct EGong_command *command, char *arg){ 61 void *ret=NULL; 62 switch(command->type.type){ 63 case EGONG_COMMAND_BOOL_SET: 64 ret=(void *)(uintptr_t)((*arg=='1') ? 1 : 0); 65 break; 66 case EGONG_COMMAND_BIT_SET: 67 case EGONG_COMMAND_UINT_SET: 68 case EGONG_COMMAND_FUNC_UINT: 69 ret=(void *)(uintptr_t)(unsigned int)atoi(arg); 70 break; 71 case EGONG_COMMAND_FUNC_POINTER: 72 do_log("Can't convert char* to pointer", LOG_TYPE_NORMAL, LOG_LEVEL_ERROR); 73 break; 74 case EGONG_COMMAND_FUNC_CHAR: 75 case EGONG_COMMAND_FUNC_2CHAR: 76 case EGONG_COMMAND_FUNC_STRING: 77 case EGONG_COMMAND_STREX_SET: 78 ret=(void *)arg; 79 break; 80 } 81 return ret; 82 } 83 int EGong_command_exec(struct EGong_command *command, void **data){ 84 String str; 85 String_Ex *strex; 86 char *charptr; 87 size_t len; 88 switch(command->type.type){ 89 case EGONG_COMMAND_BOOL_FALSE: 90 *(unsigned int *)command->pointer=0; 91 break; 92 case EGONG_COMMAND_BOOL_TRUE: 93 *(unsigned int *)command->pointer=1; 94 break; 95 case EGONG_COMMAND_BOOL_SET: 96 *(unsigned int *)command->pointer=(**(char **)data=='1') ? 1 : 0; 97 break; 98 case EGONG_COMMAND_STREX_SET: 99 strex=(String_Ex *)command->pointer; 100 charptr=((char **)data)[0]; 101 len=strlen(charptr); 102 if(strex->str.length<len){ 103 if(strex->allocd){ 104 strex->str.data=realloc(strex->str.data, len); 105 } 106 else{ 107 strex->str.data=malloc(len); 108 } 109 strex->allocd=1; 110 } 111 strncpy(strex->str.data, charptr, len); 112 break; 113 case EGONG_COMMAND_BIT_SET: 114 if((unsigned int)((intptr_t *)data)[1]){ 115 *(unsigned int *)command->pointer|=(unsigned int)((intptr_t *)data)[0]; 116 } 117 else{ 118 *(unsigned int *)command->pointer&=~(unsigned int)((intptr_t *)data)[0]; 119 } 120 break; 121 case EGONG_COMMAND_UINT_INC: 122 if(*(unsigned int *)command->pointer<=(unsigned int)-1) 123 (*(unsigned int *)command->pointer)++; 124 break; 125 case EGONG_COMMAND_UINT_DEC: 126 if(*(unsigned int *)command->pointer>(unsigned int)0) 127 (*(unsigned int *)command->pointer)--; 128 break; 129 case EGONG_COMMAND_UINT_SET: 130 (*(unsigned int *)command->pointer)=*(unsigned int *)data; 131 break; 132 case EGONG_COMMAND_FUNC_VOID: 133 ((int(*)(void))command->pointer)(); 134 break; 135 case EGONG_COMMAND_FUNC_UINT: 136 ((int(*)(unsigned int))command->pointer)(*(unsigned int*)data); 137 break; 138 case EGONG_COMMAND_FUNC_CHAR: 139 ((int(*)(char *))command->pointer)(*(char **)data); 140 break; 141 case EGONG_COMMAND_FUNC_STRING: 142 str.data=*(char **)data; 143 str.length=strlen(str.data); 144 ((int(*)(String *))command->pointer)(&str); 145 break; 146 case EGONG_COMMAND_FUNC_POINTER: 147 ((void(*)(void *))command->pointer)(*(void **)data); 148 break; 149 case EGONG_COMMAND_FUNC_2CHAR: 150 ((int(*)(char *, char *))command->pointer)(((char **)data)[0],((char **)data)[1]); 151 break; 152 default: 153 do_log("Unknown command type", LOG_TYPE_NORMAL, LOG_LEVEL_ERROR); 154 return -1; 155 } 156 return 0; 157 }