CMD.c (5155B)
1 #include <EGong/Interfaces.h> 2 #include <EGong/Interfaces/CMD.h> 3 #include <EGong/Interfaces.h> 4 #include <EGong/Util/Log.h> 5 #include <EGong/Util/Misc.h> 6 #include <EGong/Util/Dependencies.h> 7 #include <EGong/Util/Command.h> 8 #include <EGong/Util/Config.h> 9 #include <stddef.h> 10 #include <stdint.h> 11 #if defined(USE_GTK) || defined(USE_WINGUI) 12 #include <EGong/Interfaces/GUI.h> 13 #endif 14 #ifdef _WIN32 15 #include <stdio.h> 16 #endif 17 int EGong_global_argc; 18 char **EGong_global_argv; 19 20 struct EGong_command_if_cmd EGong_if_cmd_commands[]={ 21 { 22 .command={ 23 .longname="verbose", 24 .shortname="v", 25 .description="Increase verbosity", 26 .type={.type=EGONG_COMMAND_UINT_DEC, .args=0}, 27 .pointer=&EGong_global_configuration.log.level 28 }, 29 .execute=EGONG_IF_CMD_SETUP 30 }, 31 { 32 .command={ 33 .longname="quiet", 34 .shortname="q", 35 .description="Decrease verbosity", 36 .type={.type=EGONG_COMMAND_UINT_INC, .args=0}, 37 .pointer=&EGong_global_configuration.log.level 38 }, 39 .execute=EGONG_IF_CMD_SETUP 40 }, 41 { 42 .command={ 43 .longname="help", 44 .shortname="h", 45 .description="Displays the help message", 46 .type={.type=EGONG_COMMAND_FUNC_VOID, .args=0}, 47 .pointer=&EGong_if_cmd_help 48 }, 49 .execute=EGONG_IF_CMD_SETUP 50 }, 51 { 52 .command={ 53 .longname="interface", 54 .shortname="I", 55 .description="Select an interface", 56 .type={.type=EGONG_COMMAND_FUNC_CHAR, .args=1}, 57 .pointer=&EGong_if_cmd_interfaceselect 58 }, 59 .execute=EGONG_IF_CMD_SETUP 60 }, 61 { 62 .command={ 63 .longname="send-message", 64 .shortname="s", 65 .description="Sends a message", 66 .type={.type=EGONG_COMMAND_FUNC_CHAR, .args=1}, 67 .pointer=&EGong_if_cmd_send_message 68 }, 69 .execute=EGONG_IF_CMD_CYCLE 70 }, 71 { 72 .command={ 73 .longname="bind-ip", 74 .shortname="b", 75 .description="Specifies IP to bind to", 76 .type={.type=EGONG_COMMAND_STREX_SET, .args=1}, 77 .pointer=&EGong_global_configuration.server.bind_ip 78 }, 79 .execute=EGONG_IF_CMD_SETUP 80 }, 81 { 82 .command={ 83 .longname="port", 84 .shortname="p", 85 .description="Port to listen on", 86 .type={.type=EGONG_COMMAND_UINT_SET, .args=1}, 87 .pointer=&EGong_global_configuration.server.port 88 }, 89 .execute=EGONG_IF_CMD_SETUP 90 }, 91 #if defined(USE_GTK) || defined(USE_WINGUI) 92 { 93 .command={ 94 .longname="windows-message", 95 .shortname="w", 96 .description="Opens the Windows-Message dialog", 97 .type={.type=EGONG_COMMAND_FUNC_VOID, .args=0}, 98 .pointer=&EGong_if_cmd_gui_msg 99 }, 100 .execute=EGONG_IF_CMD_CYCLE 101 }, 102 #endif 103 }; 104 struct EGong_command_array EGong_if_cmd_commandarray={.array={ 105 .elements=&EGong_if_cmd_commands, 106 .element_size=sizeof(*EGong_if_cmd_commands), 107 .element_count=CSIZEOF(EGong_if_cmd_commands), 108 }}; 109 110 #if defined(USE_GTK) || defined(USE_WINGUI) 111 int EGong_if_cmd_gui_msg(void){ 112 do_log("Stub", LOG_TYPE_NORMAL, LOG_LEVEL_WARNING); 113 return EGONG_INTERFACE_RETURN_OK; 114 } 115 #endif 116 int EGong_if_cmd_send_message(char *msg){ 117 EGong_deps_init(EGONG_DEP_SOCKETS); 118 String mess={.data=msg, .length=strlen(msg)}; 119 EGong_cnc_sendmessage_broad(&mess); 120 return EGONG_INTERFACE_RETURN_OK; 121 } 122 int EGong_if_cmd_help(void){ 123 STDWRITE("Usage: "); 124 STDWRITED(EGong_global_argv[0]); 125 STDWRITE("\n"); 126 EGong_command_print_help(&EGong_if_cmd_commandarray, STDOUT_FILENO, "\t"); 127 return EGONG_INTERFACE_RETURN_OK; 128 } 129 int EGong_if_cmd_interfaceselect(const char *data){ 130 struct EGong_interface *interface; 131 132 interface=EGong_interface_get_from_id(data[0]); 133 if(interface==NULL){ 134 do_log("Malformed interface specified", LOG_TYPE_NORMAL, LOG_LEVEL_WARNING); 135 return -EGONG_INTERFACE_RETURN_ERROR; 136 } 137 EGong_interface_activate(interface); 138 return EGONG_INTERFACE_RETURN_OK; 139 } 140 141 struct EGong_command_if_cmd *EGong_if_cmd_get_from_char(const char *commandline){ 142 const char *valptr=commandline; 143 while(*++valptr=='-'); 144 return (struct EGong_command_if_cmd *)EGong_command_match(&EGong_if_cmd_commandarray,valptr,EGONG_COMMAND_MATCH_ANY); 145 } 146 int EGong_if_cmd_exec(unsigned int execution, struct EGong_interface *interface, String *msg, char **dest){ 147 int i; 148 char *arg; 149 struct EGong_command_if_cmd *command=NULL; 150 for(i=1; i<EGong_global_argc; i++){ 151 arg=EGong_global_argv[i]; 152 command=EGong_if_cmd_get_from_char(arg); 153 if(command==NULL){ 154 do_log_call_composite(LOG_COMPOSE_SRC, LOG_TYPE_NORMAL, LOG_LEVEL_WARNING, EGong_global_configuration.log.destinations, "Command \"", arg, "\" not found", NULL); 155 continue; 156 } 157 if((command->execute&execution)>0){ 158 EGong_command_exec_from_char((struct EGong_command *)command, &EGong_global_argv[i+1]); 159 } 160 i+=command->command.type.args; 161 command=NULL; 162 } 163 return EGONG_INTERFACE_RETURN_OK; 164 } 165 int EGong_if_cmd_setup(struct EGong_interface *interface){ 166 EGong_if_cmd_exec(EGONG_IF_CMD_SETUP, interface, NULL, NULL); 167 return EGONG_INTERFACE_RETURN_OK; 168 } 169 170 int EGong_if_cmd_cycle(struct EGong_interface *interface, String *msg, char **dest){ 171 EGong_if_cmd_exec(EGONG_IF_CMD_CYCLE, interface, NULL, NULL); 172 EGong_interface_deactivate(interface); 173 return EGONG_INTERFACE_RETURN_OK; 174 } 175 176 int EGong_if_cmd_shutdown(struct EGong_interface *interface){ 177 EGong_if_cmd_exec(EGONG_IF_CMD_SHUTDOWN, interface, NULL, NULL); 178 return EGONG_INTERFACE_RETURN_OK; 179 }